RE: Using global variables for widgets



Thanks for all the replies.  I will definitely check into these, I knew
there had to be a way.

Thanks again,
Matt

-----Original Message-----
From: Markus Lausser [mailto:sgop users sourceforge net] 
Sent: Friday, March 28, 2003 12:32 PM
To: Childers, Matthew
Cc: gtk-app-devel-list gnome org
Subject: Re: Using global variables for widgets

On Fri, Mar 28, 2003 at 12:03:10PM -0600, Childers, Matthew wrote:
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.

I would suggest several solutions:
1) pack all widgets you like to hide into a struct and pass it as
   data pointer to your callback function (instead of NULL)
2) attach the widget pointers to your button with
   g_object_set_data(G_OBJECT(button), "widget1", widget1);
   In the callback you can access them again with:
   widget1 = g_object_get_data(G_OBJECT(button), "widget1");
   With gtk 1.x you would use gtk_object_(g/s)et_data();
3) Make the widgets static in the .c file and put both the callback
   and the function that creates your widgets there.
4) put all widgets you want to hide in a container (GtkHBox, etc) if
   possible and simply pass the container to your callback and hide it.
  
I personally would perfer solution 4) if possible, if not then i'd use
2)
I dont like 1) cause an additional struct is needed (de/allocation).

Markus.



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