Barra laterale

programmazione:gtkmm:script_scheletro_applicazione

Un script bash che crea lo scheletro di un'applicazione gtkmm con meson

Autore: Fabio Di Matteo
Ultima revisione: 26/04/2023 - 13:00

Avviare lo script come segue:

./auto-gtkmm <nome cartella mio progetto>

viene creato lo scheletro dell'applicazione.

meson setup build 

viene configurato il progetto nella cartella “build”

cd build  
ninja

viene compilato il progetto

auto-gtkmm

#!/bin/bash
 
AUTHOR="Fabio Di Matteo"
VERSION="0.1.1"
 
 
#Controllo se e' stato fornito il nome del progetto
if [ "$1" = "" ];then
	echo "E' necessario fornire il nome del progetto come parametro."
	exit 1 ;
fi
 
#Se la directory del progetto esiste gia' esco.
mkdir $1 2>/dev/null
if [ "$?" != 0 ];then
	echo "Il progetto esiste."
	exit 1 ;
fi
 
cd $1
 
 
echo "Make resorces folder..."
mkdir res
 
 
echo "Make main.cc..."
echo "
#include <gtkmm.h>
#include \"mainwindow.hpp\"
 
int main(int argc, char *argv[])
{
	auto app = Gtk::Application::create(argc, argv, \"org.gtkmm.example\");
	mainWindow mainw;
	return app->run(*mainw.window1);
}
 
" > main.cc ;
 
echo "Make mainwindow.hpp..."
echo "
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
 
#include <gtkmm.h>
 
class mainWindow  
{
	public:
		Glib::RefPtr<Gtk::Builder> builder;
		Gtk::Window *window1;
		Gtk::Button *button1;
		Gtk::Label *label1;
 
		mainWindow();
 
	protected:	
		void on_button_clicked();
 
	private:
		/* add your private declarations */
};
 
#endif /* MAINWINDOW_HPP */ 
" > mainwindow.hpp ;
 
 
echo "Make mainwindow.cpp..."
echo "
#include \"mainwindow.hpp\"
#include <iostream>
 
 
mainWindow::mainWindow()
{
	auto builder = Gtk::Builder::create();
 
	try
	{
		builder->add_from_resource(\"/app/res/gui.ui\");
	}
	catch(const Glib::FileError& ex)
	{
		std::cerr << \"FileError: \" << ex.what() << std::endl;
	}
	catch(const Glib::MarkupError& ex)
	{
		std::cerr << \"MarkupError: \" << ex.what() << std::endl;
	}
	catch(const Gtk::BuilderError& ex)
	{
		std::cerr << \"BuilderError: \" << ex.what() << std::endl;
	}
 
 
 
 
	builder->get_widget(\"window1\", window1);
	builder->get_widget(\"button1\", button1);
	builder->get_widget(\"label1\", label1);
 
	button1->signal_clicked().connect( sigc::mem_fun(*this, &mainWindow::on_button_clicked) );
 
}
 
void mainWindow::on_button_clicked()
{
	static int c;
	c++;
	label1->set_text(\"You have clicked \"+std::to_string(c)+\" times!\");
}
 
 
 
" > mainwindow.cpp ;
 
 
 
 
echo "Make meson.build"
echo "
project('GTKmm', 'cpp')
gtkmm = dependency('gtkmm-3.0')
 
 
 
gnome = import('gnome')
myresources = gnome.compile_resources(
    'my-resources', 'res/resources.xml',
    source_dir: 'res',
    c_name: 'myresources'
)
 
os=build_machine.system()
if os == 'linux'
	executable('simple-meson', 'main.cc',myresources,'mainwindow.cpp', dependencies : gtkmm)
endif
if os == 'windows'
  executable('simple-meson', 'main.cc',myresources,'mainwindow.cpp', dependencies : gtkmm, link_args : ['-mwindows'])
endif
 
" > meson.build;
 
 
echo "Make resources"
echo "
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<gresources>
  <gresource prefix=\"/app/res\">
	<file>gui.ui</file>
  </gresource>
</gresources>
 
 
" > res/resources.xml;
 
 
echo  "
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!-- Generated with glade 3.20.2 -->
<interface>
  <requires lib=\"gtk+\" version=\"3.20\"/>
  <object class=\"GtkWindow\" id=\"window1\">
    <property name=\"can_focus\">False</property>
    <child>
      <object class=\"GtkBox\">
        <property name=\"visible\">True</property>
        <property name=\"can_focus\">False</property>
        <property name=\"orientation\">vertical</property>
        <child>
          <object class=\"GtkLabel\" id=\"label1\">
            <property name=\"visible\">True</property>
            <property name=\"can_focus\">False</property>
            <property name=\"label\" translatable=\"yes\">You have clicked 0 times</property>
            <attributes>
              <attribute name=\"scale\" value=\"5\"/>
            </attributes>
          </object>
          <packing>
            <property name=\"expand\">False</property>
            <property name=\"fill\">True</property>
            <property name=\"position\">0</property>
          </packing>
        </child>
        <child>
          <object class=\"GtkButton\" id=\"button1\">
            <property name=\"label\" translatable=\"yes\">Cliccami</property>
            <property name=\"visible\">True</property>
            <property name=\"can_focus\">True</property>
            <property name=\"receives_default\">True</property>
          </object>
          <packing>
            <property name=\"expand\">False</property>
            <property name=\"fill\">True</property>
            <property name=\"position\">1</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
    </child>
    <child>
      <placeholder/>
    </child>
  </object>
</interface>
 
 
" > res/gui.ui ;
 
echo "#!/bin/bash
 
cd res
echo 'Building resources.c ...'
glib-compile-resources --generate-source resources.xml
mv resources.c ../resources.cpp
 
 
" > compile_resources.sh;
chmod +x compile_resources.sh

programmazione/gtkmm/script_scheletro_applicazione.txt · Ultima modifica: 26/04/2023 - 13:44 da Fabio Di Matteo