Using global variables for widgets



I am new to C programming and GTK so please bear with me.  I took a C programming class in college where my instructor would not let us create global variables in any program we wrote.  We were to create them in the function they were used in and pass a pointer to any other functions they were needed in.  He explained that this was good programming practice and that there were times when global variables were useful, but not in any of the programs we were writing.

 

Now, three years later I have taken up C again (dusting off the old textbooks) and am getting into GTK.  I have been playing around with test programs and need some program design advice.  I have a couple widgets that when I press a button, I want them to disappear.  However, with the g_signal_connect function, I can’t seem to find a way to pass a pointer of each of these widgets that I want hidden.  What I end up doing is creating a global variable for each widget that I want hidden and hiding them with a callback function.

 

This is how I have been doing it:

 

For conciseness and clarity, I have left out a lot of code, just put in the bare minimum so you can understand what I am doing.

 

 

// global variables

GtkWidget *box;

GtkWidget *label;

GtkWidget *button;

 

 

int main() {

     

      button = gtk_button_new_with_label("Click Me");

      g_signal_connect(G_OBJECT(button), "clicked",

                              G_CALLBACK(hide_widgets), NULL);   

      return 0;

}

 

 

//function to hide widgets

void hide_widgets() {

      gtk_widget_hide(GTK_WIDGET(label));

      gtk_widget_hide(GTK_WIDGET(button));

gtk_widget_hide(GTK_WIDGET(box));

}

 

 

Is this correct, I’m pretty sure I don’t have to make these widgets global, but I don’t know how to do it otherwise, since the g_signal_connect can only pass one pointer as the data argument.  Can anyone give me a little guidance here or am I too far off the path.

 

Thanks for your time and any help you can give,

Matt

 

 



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