auto-scripts/auto-gtk3-meson


Home Back

#!/bin/sh
 
AUTHOR="Fabio Di Matteo"
VERSION="0.1.3"

 
if [ "$1" = "" ];then
    echo "You must provide the project name as a parameter."
    exit 1 ;
fi
 
mkdir $1 2>/dev/null
mkdir $1/locale

if [ "$?" != 0 ];then
    echo "The project already exists!"
    exit 1 ;
fi
 
cd $1

 
echo "Writing main.c ..."
echo '#include <gtk/gtk.h>                                                

#include "globals.c"
#include "win1.c" 


#include <libintl.h> 
#include <locale.h>

#define MOFILENAME "main"
 
int
main (int argc, char *argv[])
{
    setlocale (LC_ALL, "");
    gchar* MainBinDir = g_path_get_dirname(argv[0]);
    gchar* localeDir = g_build_filename (MainBinDir, "locale", NULL);
    bindtextdomain (MOFILENAME, localeDir);
    bind_textdomain_codeset (MOFILENAME, "UTF-8");
    textdomain (MOFILENAME);
    
    gtk_init (&argc, &argv);
    initWindow1();
    gtk_main ();

  return 0;
 
}

' > main.c

echo "Writing win1.c ..."
echo '
#include <gtk/gtk.h>    
#include "globals.c"
#include "win2.c"


extern GtkBuilder *xml;


void initWindow1()
{
    GError* error = NULL;
    xml = gtk_builder_new ();
    if (!gtk_builder_add_from_resource  (xml, "/org/myapp/res/gui.ui", &error))
      {
        g_warning ("Error on load builder file: %s", error->message);
        g_error_free (error);
      }
    GObject* window1=gtk_builder_get_object (xml,"window1");
    GObject* button12=gtk_builder_get_object (xml,"button12");
    GObject* label1=gtk_builder_get_object (xml,"label1");
    gtk_label_set_text(GTK_LABEL(label1), gettext("Hello World"));
    initWindow2();
    g_signal_connect (button12, "clicked", G_CALLBACK (showWindow2),NULL);
    g_signal_connect (window1, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    
    gtk_window_present(GTK_WINDOW(window1));
}

' > win1.c

echo "Writing win2.c ..."
echo '#include <gtk/gtk.h>    
#include "globals.c"

#include <libintl.h> 
#include <locale.h>

extern GtkBuilder *xml;

GObject* window2, *button2, *label1, *entry2;

static void showWindow2(GtkWidget *widget,gpointer data)
{
    gtk_window_present(GTK_WINDOW(window2));
    }

static void hideWindow2(GtkWidget *widget,gpointer data)
{
    gtk_widget_hide(GTK_WIDGET(window2));
    char* txt=g_strdup_printf("From second window: %s", gtk_entry_get_text(GTK_ENTRY(entry2)));
    gtk_label_set_text(GTK_LABEL(label1),txt);
    }

void initWindow2()
{
    window2=gtk_builder_get_object (xml,"window2");
    button2=gtk_builder_get_object (xml,"button2");
    label1=gtk_builder_get_object (xml,"label1");
    entry2=gtk_builder_get_object (xml,"entry2");
    g_signal_connect (window2, "delete-event", G_CALLBACK (hideWindow2), NULL);
    g_signal_connect (button2, "clicked", G_CALLBACK (hideWindow2), NULL);

}

'>win2.c

echo "Writing globals.c ..."
echo 'GtkBuilder *xml;

'>globals.c



 
echo "Genero il file delle risorse..."
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
        <gresources>
          <gresource prefix=\"/org/myapp/res\">
            <file>gui.ui</file>
          </gresource>
        </gresources>" >resource.xml

echo "Writing builder gui file..."
echo '<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.40.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="window1">
    <property name="name">window1</property>
    <property name="can-focus">False</property>
    <property name="title" translatable="yes">Test</property>
    <child>
      <!-- n-columns=1 n-rows=2 -->
      <object class="GtkGrid" id="grid1">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <property name="column-homogeneous">True</property>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <property name="label" translatable="yes">label1</property>
            <attributes>
              <attribute name="scale" value="4"/>
            </attributes>
          </object>
          <packing>
            <property name="left-attach">0</property>
            <property name="top-attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button12">
            <property name="label" translatable="yes">open window 2</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
          </object>
          <packing>
            <property name="left-attach">0</property>
            <property name="top-attach">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkWindow" id="window2">
    <property name="can-focus">False</property>
    <child>
      <!-- n-columns=1 n-rows=3 -->
      <object class="GtkGrid">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <property name="column-homogeneous">True</property>
        <child>
          <object class="GtkLabel">
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <property name="label" translatable="yes">Window 2</property>
            <attributes>
              <attribute name="scale" value="4"/>
            </attributes>
          </object>
          <packing>
            <property name="left-attach">0</property>
            <property name="top-attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button2">
            <property name="label" translatable="yes">close window</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
          </object>
          <packing>
            <property name="left-attach">0</property>
            <property name="top-attach">2</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="entry2">
            <property name="visible">True</property>
            <property name="can-focus">True</property>
          </object>
          <packing>
            <property name="left-attach">0</property>
            <property name="top-attach">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
 ' >gui.ui        

echo "Writing meson.build ..."
echo "

project('main', 'c')

gnome = import('gnome')
myresources = gnome.compile_resources(
    'my-resource', 'resource.xml',
     source_dir: '.',
    c_name: 'myresource'
)



run_command('cp', '-R', 'locale', meson.current_build_dir(), check: true)

extra_args= ['-rdynamic']
gtkdep = dependency('gtk+-3.0')
gthread = dependency('gthread-2.0')
gmodule = dependency('gmodule-2.0')

executable('main', myresources,'main.c', 
            dependencies :[gtkdep, gthread, gmodule ],
            c_args : extra_args, 
            install : false, 
            install_dir : '/usr/bin')




">meson.build

echo "To configure the project : meson setup <builddir>"
echo "To compile the project: cd <builddir> ; ninja"
echo "To install the project: sudo ninja install"

Powered by Code, a simple repository browser by Fabio Di Matteo