Re: Style/Theory



"Sean Cody" <umcodysw@cc.umanitoba.ca> writes:

> Where would one store the data applicable to a particular window/widget?
> Let's just say I have a fictitious program which has an entry widget and a
> button.  When the button is clicked the program is to preform a set of
> operations on the entry widget.  Another call back for this entry widget
> would be say on key press or something like that to check to see of the key
> pressed is the enter '\13' key signalling input completion.
> 
> How would the call back know what widget is being referred to? Would the
> char* or the entry widget be global (which doesn't sound nice).  Though what
> about larger input areas with multiple input fields?  How would the button
> call back know where the entry widget is to retrive the data (assuming that
> is is created in a local variable in a dialog creation function)...

Callbacks are called with a pointer to the widget as one of the
function's arguments.  When you call gtk_signal_connect, you can also
pass a user_data pointer, which gets passed on to the callback.  So in
your case with multiple input fields, you can attach the same callback
to more than one field, and it will be able to tell which widget
triggered it and what user_data was supplied when the callback was
attached.  You could have the user_data for each button signal be a
pointer to the associated entry widget (or a structure if you want to
pass more data).  For instance:

    entry = gtk_entry_new ();
    button = gtk_button_new_with_label ("foo");
    (...Code to put the widgets in their parent containers...)
    gtk_signal_connect (GTK_OBJECT (button), "clicked",
                        GTK_SIGNAL_FUNC (callback),
                        entry);

And repeat this with each entry/button pair you want to create.  You
don't have to keep any of the widgets in global variables after you've
finished setting them up how you want.

There is also a way of attaching arbitrary data to GTK objects:
gtk_object_set_data (look in gtkobject.c).  You give the data a name,
and then you can retrieve it with gtk_object_get_data using the same
name.

Hope this helps,

    Andy
--- ----------  - ----- -  -- -- -- -------  -      --  -   -  --     
Andrew Smith                                         amsmith@pmail.net
Future Illusions                              www.future-illusions.com
        -        --  -  -  ---   - -  - - - - -  --------------- ---- 



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