[Glade-users] C Beginner question



Take a look at this example posted in gtkforums.com: 
http://www.gtkforums.com/about906.html

One method we often employ for passing around references to the widgets 
we need in GTK+ callbacks is by passing a struct. In the example above, 
both the textview widget and the entry widget are passed to the callback 
by placing them in a typdef struct MyWidgets.

So you might have a struct for you application which has the statusbar, 
the window, and say.... an entry which you pass as callback data since 
those are widgets you need to manipulate throughout the lifetime of the 
application. The other widgets you might only need to reference once 
during initialization and thus you only reference those through local 
variables in the function you use to initialize them.

Some applications go one step further and create GObjects for various 
parts of the application but that's getting a bit more advanced than you 
need at this point in time.

As for where you can go for this kind of information, you're at a pretty 
good place now. Good sources are this list and gtk-list, the API 
documentation (do you have devhelp installed?), www.gtkforums.com, and 
looking at source code. Find small, simple applications and look at 
their source code.


- Micah Carrick

  Developer - http://www.micahcarrick.com
  GTK+ Forums - http://www.gtkforums.com



Lupine wrote:
On Mon, 2007-12-24 at 10:33 -0800, Micah Carrick wrote:
  
Did you specify "ssh_user_entry" as the user_data for the "clicked" 
event of the button in the glade file? Otherwise, you might want to use 
glade_xml_signal_connect instead of autoconnect so you can specify the 
user data. Sometimes if you have custom user data, you use 
glade_xml_signal_connect_data for your connections instead.

void on_connect_btn_clicked(GtkButton *button, GtkWidget
*ssh_user_entry)
{
    const gchar *ssh_user_text;
     ssh_user_text = gtk_entry_get_text (GTK_ENTRY (ssh_user_entry));
    printf ("Entry contents: %s\n", ssh_user_text);
}

int main (int argc, char *argv[])
{
    GladeXML *xml;    /* glade xml file */
    GtkWidget *entry;
   
    gtk_init (&argc, &argv);
    xml = glade_xml_new (GLADE_FILE, NULL, NULL);   
   
    /* get a reference to the entry widget */
    entry = glade_xml_get_widget (xml, "ssh_user_entry");
   
    /*
    assumes "on_connect_btn_clicked" is the handler name specified in glade
    and passes the entry widget as user data.
    */
   
    glade_xml_signal_connect_data (xml,
                                   "on_connect_btn_clicked",
                                   G_CALLBACK (on_connect_btn_clicked),
                                   entry);
                                  
    /*
    unlike PHP, you're responsible for freeing memory with C
    Below we are unreferencing the xml object in memory now that we've built
    the UI and obtained references to the widgets we need.
    */
   
    g_object_unref (xml);
   
    gtk_main();

    return 0;
}
    



Ahhh, thanks for the thorough explanation, and sample code.  It makes a
little more sense now.  I'm just a little confused on the format of the
glade_xml_signal_connect_data function.  I'm now wondering how the best
way to pass multiple GtkEntry inputs to that function, or do I go and
grab that information from within the void on_connect_btn_clicked()
function.

Is there any site that I can reference for future, similar, basic
questions?

Thanks again for the same-day response!
-Lup

_______________________________________________
Glade-users maillist  -  Glade-users at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/glade-users

  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/glade-users/attachments/20071224/84bdf41e/attachment-0001.html 




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