1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
   |  
#include <gtk/gtk.h>
 
typedef struct {
 gchar *name;
 gint age;
} person;
 
static void callback(GtkWidget *widget, person *x)
{
  g_print ("name:%s age:%d\n", x->name, x->age);
}
 
int main (int argc, char **argv)
{
  GtkWidget *window, *button;
 
  gtk_init (&argc, &argv);
 
  // Create window
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (G_OBJECT (window), "destroy",G_CALLBACK (gtk_main_quit), NULL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
 
  // array of structures
  person *friends[2];
 
  // populate it
  friends[0] = (person*) g_malloc(sizeof(person));
  friends[0]->name = g_strdup("Joe");
  friends[0]->age  = 25;
 
  friends[1] = (person*) g_malloc(sizeof(person));
  friends[1]->name = g_strdup("Bill");
  friends[1]->age  = 32;
 
  // Create the button and pass values of a person to it
  button = gtk_button_new_with_label ("infos button");
  g_signal_connect (G_OBJECT(button), "clicked",
                    G_CALLBACK(callback), (person*)friends[0]);
 
  gtk_container_add(GTK_CONTAINER(window), button);
  gtk_widget_show_all (window);
 
  gtk_main ();
 
  g_free(friends);
  return 0;
} | 
Partager