Re: [gtk-list] How do I send arguments with signals?



Bawer Dagdeviren wrote:
> 
> Hi, I have a question about sending arguments to signal functions.
> The following code snippet shows what I WANT to do, but have not been
> able to do:
> 
> #include <gtk/gtk.h>
> 
> void hello (GtkWidget *label)

This is not a GtkSignalFunc function, and I would have thought generated
an error during compilation!.

The correct version would be 

void hello(GtkWidget *widget, gpointer data)

The data would be the label widget and I'm not entirely sure what the
widget represents, never had to use it!


> The problem is this: I can't seem to alter the caption of the label. Am
> I doing something weird with the pointers? I tried this block of code
> (well not really THIS block of code :-) ) but I get an error that speak
> of some conversion error between a Button and Label, so I changed
> hello() to:
> void hello(GtkWidget *button, GtkWidget *label)....
> assuming that somehow the signal sends the button-object as an argument.
> I really think I'm out in the dark here, I take full responsibility for
> all the stupidity contained within this text :-)
> 
> Looking forward to your answers:
> 
> Bawer Dagdeviren.
> 
> --
> To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com < /dev/null

OK. I have generated a small program which does exactly what you want. I
think it is correct, but I'm open to criticism, but since it doesn't
really do anything - why?

One thing that puzzles me, and perhaps someone may be able to enlightent
me on this, is why most of the functions are declared static??  I've
never seen another program use so many static functions as the GTK/GIMP
does...... not criticism, but not being a Computer Scientist I'm just
curious. I just do it to follow suite, without really understanding the
importance of it!

All the best,

Mike

-- 
  "We can't solve problems with the same
      thinking we used to create them."
                        - Albert Einstein

---

Here it is :-
/*
 * Simple program which changes the text of the label on user input.
 *
 */

#include <gtk/gtk.h>

/* GtkSignalFunc callback for a user click on the change label button*/
static void change_label(GtkWidget *widget, gpointer data) {
  
  GtkWidget *label;
  
  if ((label = (GtkWidget *)data) == NULL) 
    return;

  gtk_label_set(GTK_LABEL(label), "Hello again");

}

static void destroy_window(GtkWidget *widget, gpointer data) {
  
}

/* create a window with a label in it and a close button */
static void create_window() {
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *label;
  GtkWidget *box1;

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_signal_connect(GTK_OBJECT(window), "destroy",
		     (GtkSignalFunc) destroy_window,
		     &window);
  gtk_window_set_title(GTK_WINDOW(window), "Test Label");
  gtk_container_border_width(GTK_CONTAINER(window), 0);
  
  box1 = gtk_vbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(window), box1);
  gtk_widget_show(box1);
  
  /* create the label object */
  label = gtk_label_new("Hello");
  gtk_box_pack_start(GTK_BOX(box1), label, TRUE, TRUE, 2);
  gtk_widget_show(label);

  /* create a button whose responsibility it is to call 
     the change_label function */
  button = gtk_button_new_with_label("Change");
  gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 2);

  /* attach to the "clicked" event and pass the label as data */
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
		      (GtkSignalFunc) change_label,
		      label);
  gtk_widget_show(button);

  /* a button to close the window when we are done! */
  button = gtk_button_new_with_label("Close");
  gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 2);
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
		      (GtkSignalFunc)gtk_exit,
		      NULL);
  gtk_widget_show(button);
  gtk_widget_show(window);
}

int main(int argc, char *argv[]){
  
  gtk_init(&argc, &argv);
  
  create_window();
  
  gtk_main();

  return 0;

}



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