Re: Problem sending data to gtk_button on "clicked" event



On Wed, 2005-11-09 at 08:39, Evan Behar wrote:
I've been getting seg-faults when I try to work with data in my button
"clicked" callback functions, so as a test, I compiled and ran the
following program:

#include <gtk/gtk.h>

gchar m1[] = "button 1";
gchar m2[] = "button 2";

static void callback(GtkWidget* widget, GdkEvent *event, gpointer data) {
  g_print ("Hello again - %08X was pressed\n", data);
}

Your problem is that the marshaller for button clicked events only
passes two parameters. Try:

  static void callback(GtkWidget *widget, gpointer data);

static gboolean delete_event( GtkWidget *widget, GdkEvent *event, gpointer data) {
  g_print ("Check it - %08X was pressed\n", data);
  gtk_main_quit();
  return TRUE;
}

int main(int argc, char* argv[]) {
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *box1;
  
  printf("m1: %08X\n",m1);
  printf("m2: %08X\n",m2);
  printf("(gpointer)m1: %08X\n",(gpointer)m1);
  printf("(gpointer)m2: %08X\n",(gpointer)m2);
    
  gtk_init(&argc,&argv);
  
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);  
  g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(delete_event),(gpointer)m1);
   
  box1 = gtk_hbox_new(FALSE,0);  
  gtk_container_add(GTK_CONTAINER(window),box1);
  
  button = gtk_button_new_with_label("Button 1"); 
  g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(callback),(gpointer)m1);  
  gtk_box_pack_start(GTK_BOX(box1),button,TRUE,TRUE,0);  

  gtk_widget_show(button);
  
  button = gtk_button_new_with_label("Button 2");  
  g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(callback),(gpointer)m2);  
  gtk_box_pack_start(GTK_BOX(box1),button,TRUE,TRUE,0);  

  gtk_widget_show(button);  
  gtk_widget_show(box1);  
  gtk_widget_show(window);
  
  gtk_main();
  
  return 0;
}

I ran this program, and clicked on Button 1, then Button 2, and then the close button.

My program output was:

m1: 080491EC
m2: 080491F5
(gpointer)m1: 080491EC
(gpointer)m2: 080491F5
Hello again - 00000002 was pressed
Hello again - 00000002 was pressed
Check it - 080491EC was pressed

Naturally, if I use %s to try and output the data from the button callback functions, it seg faults.

What am I doing wrong?  Is there something else I need to do to be able to send data on a "clicked" event?

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
-- 
Alan M. Evans <ame1 extratech com>




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