Strict Standards: Only variables should be passed by reference in /homepages/33/d411990001/htdocs/spip/config/ecran_securite.php on line 283
Initiation à la programmation Gtk+ 3 - Ian’s Web Page

Initiation à la programmation Gtk+ 3

dimanche 10 novembre 2013
par  Yann
popularité : 5%

 Programme de l’article Mook C

PNG - 2.5 ko

#include <gtk/gtk.h>
//gcc `pkg-config --cflags gtk+-3.0` -o first first.c `pkg-config --libs gtk+-3.0`
int main (int   argc, char *argv[])
{
  GtkWidget *window;
  gtk_init (&argc, &argv);
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  gtk_widget_show (window);
  gtk_main ();
  return 0;
}

PNG - 5.4 ko

#include <gtk/gtk.h>
//cc `pkg-config --cflags gtk+-3.0` -o hello hello.c `pkg-config --libs gtk+-3.0`
static void print_hello (GtkWidget *widget, gpointer   data)
{
  g_print ("Hello World\n");
}
static gboolean on_delete_event (GtkWidget *widget, GdkEvent  *event, gpointer   data)
{
  g_print ("delete event occurred\n");
  return TRUE;
}
int main (int   argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *button;
  gtk_init (&argc, &argv);
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Hello");
  g_signal_connect (window, "delete-event", G_CALLBACK (on_delete_event), NULL);
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  button = gtk_button_new_with_label ("Hello World");
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
  gtk_container_add (GTK_CONTAINER (window), button);
  gtk_widget_show (button);
  gtk_widget_show (window);
  gtk_main ();
  return 0;
}

PNG - 6 ko

#include <gtk/gtk.h>
//gcc `pkg-config --cflags gtk+-3.0` -o packing packing.c `pkg-config --libs gtk+-3.0`
static void print_hello (GtkWidget *widget, gpointer   data)
{
  g_print ("Hello World\n");
}
int main (int   argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *grid;
  GtkWidget *button;
  gtk_init (&argc, &argv);
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Grid");
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  grid = gtk_grid_new ();
  gtk_container_add (GTK_CONTAINER (window), grid);
  button = gtk_button_new_with_label ("Button 1");
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1);
  button = gtk_button_new_with_label ("Button 2");
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  gtk_grid_attach (GTK_GRID (grid), button, 1, 0, 1, 1);
  button = gtk_button_new_with_label ("Quit");
  g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
  gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 2, 1);
  gtk_widget_show_all (window);
  gtk_main ();
  return 0;
}

PNG - 92.6 ko

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="window">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="border_width">10</property>
    <property name="title" translatable="yes">window</property>
    <child>
      <object class="GtkGrid" id="grid">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label">Button 1</property>
            <property name="use_action_appearance">False</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
            <property name="use_action_appearance">False</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">0</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button2">
            <property name="label">Button 2</property>
            <property name="use_action_appearance">False</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
            <property name="use_action_appearance">False</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">0</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="quit">
            <property name="label">Quit</property>
            <property name="use_action_appearance">False</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="receives_default">False</property>
            <property name="use_action_appearance">False</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">1</property>
            <property name="width">2</property>
            <property name="height">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
#include <gtk/gtk.h>
//gcc `pkg-config --cflags gtk+-3.0` -o rad rad.c `pkg-config --libs gtk+-3.0`
static void print_hello (GtkWidget *widget, gpointer   data)
{
  g_print ("Hello World\n");
}
int main (int   argc, char *argv[])
{
  GtkBuilder *builder;
  GObject *window;
  GObject *button;
  gtk_init (&argc, &argv);
  /* Construct a GtkBuilder instance and load our UI description */
  builder = gtk_builder_new ();
  gtk_builder_add_from_file (builder, "gtk_foobar.ui", NULL);
  /* Connect signal handlers to the constructed widgets. */
  window = gtk_builder_get_object (builder, "window");
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  button = gtk_builder_get_object (builder, "button1");
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  button = gtk_builder_get_object (builder, "button2");
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  button = gtk_builder_get_object (builder, "quit");
  g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
  gtk_main ();
  return 0;
}

 Programme multiplication

PNG - 59.8 ko

/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * main.c
 * Copyright (C) 2013 Yann Morere <yann@morere.eu>
 *
 * multi is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * multi is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <stdlib.h>
#include <time.h>
typedef struct _Private Private;
static struct _Private
{
        /* ANJUTA: Widgets declaration for multi.ui - DO NOT REMOVE */
};
static struct Private* priv = NULL;
/* For testing propose use the local (not installed) ui file */
/* #define UI_FILE PACKAGE_DATA_DIR"/ui/multi.ui" */
#define UI_FILE "src/multi.ui"
#define TOP_WINDOW "windowMultiplication"
GtkBuilder *builder;
gint a,b,c;
void prepare_new_calculus(int *a, int *b, int *c)
{
 srand(time(NULL));
 *a = rand() % 11;
 *b = rand() % 11;
 *c = (*a)*(*b);
}
/* Signal handlers */
/* Note: These may not be declared static because signal autoconnection
 * only works with non-static methods
 */

void on_buttonValidateAnswer_released (GtkWidget *widget, gpointer data)
{
const gchar *get_result;
gint result;
GtkEntry *entryResult = GTK_ENTRY(gtk_builder_get_object (builder,  "entryAnswer"));  
get_result = gtk_entry_get_text(entryResult);
result=atoi(get_result);
GtkLabel *label_info = GTK_LABEL (gtk_builder_get_object (builder,  "labelAnswer"));
//GtkWindow *main_window = GTK_WINDOW (gtk_builder_get_object (builder, TOP_WINDOW));
//GtkWindow *main_window =  GTK_WINDOW (gtk_builder_get_object (builder, TOP_WINDOW));    
       
if(result==c)
      {
      gtk_label_set_text(GTK_LABEL(label_info),_("Good answer! Another try..."));
      }
 else {
      gtk_label_set_text(GTK_LABEL(label_info),_("Bad answer ! Retry..."));
      }
 
prepare_new_calculus(&a, &b, &c);
GtkLabel *label = GTK_LABEL (gtk_builder_get_object (builder,  "labelQuestion"));
        gtk_label_set_text(GTK_LABEL(label),
                    g_strdup_printf (_("Calculate %d x %d ="), a, b) );
gtk_entry_set_text(GTK_ENTRY(entryResult),"");
}
void on_entryAnswer_activate (GtkWidget *widget, gpointer data)
{
        on_buttonValidateAnswer_released( widget, data);
}
void on_buttonAbout_released (GtkWidget *widget, gpointer data)
{
        GtkWidget * windowAbout = GTK_WIDGET (gtk_builder_get_object (builder, "windowAbout"));
        if (!windowAbout)
        {
                g_critical (_("Widget \"%s\" is missing in file %s."),
                                "windowAbout",
                                UI_FILE);
        }
        gtk_widget_show (windowAbout);
       
}
void on_buttonAboutOk_released(GtkWidget *widget, gpointer data)
{
        GtkWidget * windowAbout = GTK_WIDGET (gtk_builder_get_object (builder, "windowAbout"));
        if (!windowAbout)
        {
                g_critical (_("Widget \"%s\" is missing in file %s."),
                                "windowAbout",
                                UI_FILE);
        }
        gtk_widget_hide (windowAbout);
}
/* Called when the window is closed */
void destroy (GtkWidget *widget, gpointer data)
{
        g_object_unref (builder);
        gtk_main_quit ();
}
void on_buttonQuit_released (GtkWidget *widget, gpointer data)
{
        destroy(widget,data);
}
static GtkWidget* create_window (void)
{
        GtkWidget *window;
       
        GError* error = NULL;
        /* Load UI from file */
        builder = gtk_builder_new ();
        if (!gtk_builder_add_from_file (builder, UI_FILE, &error))
        {
                g_critical (_("Couldn't load builder file: %s"), error->message);
                g_error_free (error);
        }
        /* Auto-connect signal handlers */
        gtk_builder_connect_signals (builder, NULL);
        /* Get the window object from the ui file */
        window = GTK_WIDGET (gtk_builder_get_object (builder, TOP_WINDOW));
        if (!window)
        {
                g_critical (_("Widget \"%s\" is missing in file %s."),
                                TOP_WINDOW,
                                UI_FILE);
        }
        priv = g_malloc (sizeof (struct _Private));
        /* ANJUTA: Widgets initialization for multi.ui - DO NOT REMOVE */
        //g_object_unref (builder);
       
        return window;
}
int
main (int argc, char *argv[])
{
        GtkWidget *window;
#ifdef ENABLE_NLS
        bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
        textdomain (GETTEXT_PACKAGE);
#endif
        bindtextdomain( "multi", "./po" );
        bind_textdomain_codeset ("multi", "UTF-8");
        textdomain( "multi" );
       
        gtk_init (&argc, &argv);
         
        prepare_new_calculus(&a, &b, &c);
        window = create_window ();
        GtkLabel *label = GTK_LABEL (gtk_builder_get_object (builder, "labelQuestion"));
        gtk_label_set_text(GTK_LABEL(label), g_strdup_printf (_("Calculate %d x %d ="), a, b) );
        gtk_widget_show (window);
        gtk_main ();
        g_free (priv);
        return 0;
}
<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="windowAbout">
    <property name="can_focus">False</property>
    <property name="type">popup</property>
    <property name="resizable">False</property>
    <child>
      <object class="GtkBox" id="box3">
        <property name="width_request">300</property>
        <property name="height_request">200</property>
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkBox" id="box4">
            <property name="width_request">250</property>
            <property name="height_request">180</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkImage" id="imageAbout">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="pixbuf">ian.jpg</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkFixed" id="fixed1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <child>
                  <object class="GtkLabel" id="label4">
                    <property name="width_request">100</property>
                    <property name="height_request">80</property>
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="label" translatable="yes">Multiplication
Yann Morère
2013</property>
                  </object>
                  <packing>
                    <property name="x">33</property>
                    <property name="y">44</property>
                  </packing>
                </child>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="buttonAboutOk">
            <property name="label" translatable="yes">Ok</property>
            <property name="use_action_appearance">False</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="use_action_appearance">False</property>
            <signal name="released" handler="on_buttonAboutOk_released" swapped="no"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkWindow" id="windowMultiplication">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Multiplication</property>
    <property name="resizable">False</property>
    <property name="default_width">400</property>
    <property name="default_height">400</property>
    <signal name="destroy" handler="destroy" swapped="no"/>
    <signal name="delete-event" handler="destroy" swapped="no"/>
    <child>
      <object class="GtkBox" id="box1">
        <property name="width_request">250</property>
        <property name="height_request">150</property>
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <property name="homogeneous">True</property>
        <child>
          <object class="GtkLabel" id="labelQuestion">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">Calculate ...</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="entryAnswer">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="invisible_char"></property>
            <signal name="activate" handler="on_entryAnswer_activate" swapped="no"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="buttonValidateAnswer">
            <property name="label" translatable="yes">Validate your answer</property>
            <property name="use_action_appearance">False</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="use_action_appearance">False</property>
            <signal name="released" handler="on_buttonValidateAnswer_released" swapped="no"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="labelAnswer">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">Give your answer</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">3</property>
          </packing>
        </child>
        <child>
          <object class="GtkBox" id="box2">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="homogeneous">True</property>
            <child>
              <object class="GtkButton" id="buttonAbout">
                <property name="label" translatable="yes">About...</property>
                <property name="use_action_appearance">False</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_action_appearance">False</property>
                <signal name="released" handler="on_buttonAbout_released" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="buttonQuit">
                <property name="label" translatable="yes">Quit</property>
                <property name="use_action_appearance">False</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_action_appearance">False</property>
                <signal name="released" handler="on_buttonQuit_released" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">4</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Météo

Metz, 57, France

Conditions météo à 17h30
par weather.com®

Très nuageux

10°C


Très nuageux
  • Vent : 27 km/h - ouest sud-ouest
  • Pression : 985 mbar tendance symbole
Prévisions >>

Prévisions du 22 octobre
par weather.com®

Inconnu

Max 10°C
Min 7°C


Inconnu
  • Vent : N/D km/h
  • Risque de precip. : %
<< Conditions  |  Prévisions >>

Prévisions du 23 octobre
par weather.com®

Orage épars

Max 13°C
Min 11°C


Orage épars
  • Vent : 20 km/h
  • Risque de precip. : 50%
<<  Prévisions  >>

Prévisions du 24 octobre
par weather.com®

Averses

Max 15°C
Min 11°C


Averses
  • Vent : 14 km/h
  • Risque de precip. : 30%
<<  Prévisions  >>

Prévisions du 25 octobre
par weather.com®

Soleil et nuages épars

Max 20°C
Min 8°C


Soleil et nuages épars
  • Vent : 11 km/h
  • Risque de precip. : 10%
<<  Prévisions  >>

Prévisions du 26 octobre
par weather.com®

Soleil voilé

Max 20°C
Min 10°C


Soleil voilé
  • Vent : 9 km/h
  • Risque de precip. : 10%
<<  Prévisions  >>

Prévisions du 27 octobre
par weather.com®

Orage épars

Max 16°C
Min 8°C


Orage épars
  • Vent : 20 km/h
  • Risque de precip. : 40%
<<  Prévisions  >>

Prévisions du 28 octobre
par weather.com®

Nuageux

Max 13°C
Min 10°C


Nuageux
  • Vent : 24 km/h
  • Risque de precip. : 20%
<< Prévisions


Brèves

25 février 2011 - Scilab : Application GUI

Application GUI dans Scilab : Étude des systèmes automatiques. Systèmes Linéaires

9 janvier 2011 - Modèle 3D dans Scilab

Afficher des patchs dans Scilab à partir de Modèle 3D blender. Modèle 3D dans (...)

2 octobre 2010 - Plugins LDraw pour Blender 2.49

Importez vos modèles LEGO (MLCad et autres) dans blender grâce au plugin d’importation (...)