Re: Signal Handling problems with own data



On Fri, 2006-08-18 at 07:03 -0700, NicolasA wrote:
I have read all the examples in the gtk tutorial, I have 3 books on gnome
and gtk but I haven't found a single example which shows how to pass more
that 1 widget to the function.

Here you've got a full example:

// save as "test.c"  compile with:
// gcc -g test.c -o test `pkg-config --cflags --libs gtk+-2.0`
// run with ./test

#include <gtk/gtk.h>

typedef struct _TwoWidgets {
  GtkWidget *entry_name;
  GtkWidget *entry_password;
} TwoWidgets;

void dlg_response (GtkDialog *dialog,
                   gint       arg1,
                   gpointer   user_data)    
{
  g_free (user_data);
  gtk_widget_destroy (GTK_WIDGET(dialog));
  gtk_main_quit();
}

void button_clicked (GtkButton *button,
                     gpointer   user_data)   
{
  TwoWidgets* widgets;
  
  widgets = (TwoWidgets*) user_data;
  
  g_print("Name: %s\nPassword: %s\n\n",
      gtk_entry_get_text (GTK_ENTRY(widgets->entry_name)),
      gtk_entry_get_text (GTK_ENTRY(widgets->entry_password)));
}

int main(int argc, char **argv)
{
  GtkWidget *dialog, *dialog_vbox;
  GtkWidget *entry_name,*entry_password;
  GtkWidget *button;
  static GdkPixbuf *pixbuf;
  GtkTreeModel *model;
  TwoWidgets *widgets;
  
  gtk_init (&argc, &argv);
      
  dialog = gtk_dialog_new();  
  dialog_vbox = GTK_DIALOG (dialog)->vbox;
  gtk_widget_show (dialog_vbox);

  entry_name = gtk_entry_new  ();
  gtk_widget_show (entry_name);
  gtk_box_pack_start (GTK_BOX (dialog_vbox), entry_name, TRUE, TRUE, 0);
  
  entry_password = gtk_entry_new  ();
  gtk_widget_show (entry_password);
  gtk_box_pack_start (GTK_BOX (dialog_vbox), entry_password, TRUE, TRUE,
0);

  button = gtk_button_new_from_stock (GTK_STOCK_APPLY);
  gtk_widget_show (button);
  gtk_box_pack_start (GTK_BOX (dialog_vbox), button, FALSE, FALSE, 0);
  
  widgets = g_malloc(sizeof(TwoWidgets));
  widgets->entry_name = entry_name;
  widgets->entry_password = entry_password;
  
  g_signal_connect ( button,
                      "clicked", 
                      G_CALLBACK (button_clicked),
                      widgets );
                      
  g_signal_connect ( dialog,
                      "response", 
                      G_CALLBACK (dlg_response),
                      widgets );                      
                      
  gtk_widget_show (dialog);
  gtk_main ();
  
  return 0;
}



Do I have to use the Gobject, which function of the API should I use?

You can use g_object_set_data and g_object_get_data on the button that
will be passed to the "clicked" callback:

// save as "test2.c"  compile with:
// gcc -g test2.c -o test2 `pkg-config --cflags --libs gtk+-2.0`
// run with ./test2

#include <gtk/gtk.h>

void dlg_response (GtkDialog *dialog,
                   gint       arg1,
                   gpointer   user_data)    
{
  gtk_widget_destroy (GTK_WIDGET(dialog));
  gtk_main_quit();
}

void button_clicked (GtkButton *button,
                     gpointer   user_data)      
{
  gpointer *name, *password;
        
  name = g_object_get_data (G_OBJECT(button), "entry_name");
  password = g_object_get_data (G_OBJECT(button), "entry_password");
        
  if( name && password ){
    g_print("Name: %s\nPassword: %s\n\n",
      gtk_entry_get_text (GTK_ENTRY(name)),
      gtk_entry_get_text (GTK_ENTRY(password)));
  }
}

int main(int argc, char **argv)
{
  GtkWidget *dialog, *dialog_vbox;
  GtkWidget *entry_name,*entry_password;
  GtkWidget *button;
  static GdkPixbuf *pixbuf;
  GtkTreeModel *model;
  
  gtk_init (&argc, &argv);
      
  dialog = gtk_dialog_new();  
  dialog_vbox = GTK_DIALOG (dialog)->vbox;
  gtk_widget_show (dialog_vbox);

  entry_name = gtk_entry_new  ();
  gtk_widget_show (entry_name);
  gtk_box_pack_start (GTK_BOX (dialog_vbox), entry_name, TRUE, TRUE, 0);
  
  entry_password = gtk_entry_new  ();
  gtk_widget_show (entry_password);
  gtk_box_pack_start (GTK_BOX (dialog_vbox), entry_password, TRUE, TRUE,
0);

  button = gtk_button_new_from_stock (GTK_STOCK_APPLY);
  gtk_widget_show (button);
  gtk_box_pack_start (GTK_BOX (dialog_vbox), button, FALSE, FALSE, 0);
  
  g_object_set_data (G_OBJECT(button), 
                     "entry_name", entry_name);    
  g_object_set_data (G_OBJECT(button), 
                     "entry_password", entry_password);
                     
  g_signal_connect ( button,
                      "clicked", 
                      G_CALLBACK (button_clicked),
                      NULL );
                      
  g_signal_connect ( dialog,
                      "response", 
                      G_CALLBACK (dlg_response),
                      NULL );                      
                      
  gtk_widget_show (dialog);
  gtk_main ();
  
  return 0;
}


And if more than one widgets can be passed to the function, how can I access
them ?

This depends on the method you used.
-- 
Iago Rubio




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]