GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon May 20, 2013 2:07 am

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: [C] How to pass multiple widgets?
PostPosted: Mon Dec 08, 2008 8:22 pm 
Offline
GTK+ Geek

Joined: Thu Dec 20, 2007 5:24 pm
Posts: 56
Location: Turkey
Hello,

Is there an easy way to pass more than 1 widget to a callback function? I guess there is a way to pass a second widget using g_object_set_data but I mean 3, 4, 5 widgets etc...

Edit: I use C language.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2008 8:53 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
The data is usually passed as struct pointer. But you may also use array of gpointers.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2008 9:01 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
Common procedure is to pass a struct containing the variables that the callback needs.
EDIT: Time got away from me and tadeboro posted while I was on the "Post reply" page...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2008 9:58 pm 
Offline
GTK+ Geek

Joined: Thu Dec 20, 2007 5:24 pm
Posts: 56
Location: Turkey
Thank you very much. Struct is a good idea but you also gave me a clue to use a garray or something similar, which might give me opportunity to create a runtime group of widgets to pass :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2008 10:24 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 273
Location: Fortress of solitude
I also use struct, easier..and better idea then global vars.

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM Archlinux with xfce.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM Host: Debian squeeze/backports with xfce; Guest: ArchLinux.


Top
 Profile  
 
 Post subject: Subclassing is cleaner
PostPosted: Fri Dec 19, 2008 8:40 am 
Offline

Joined: Fri Dec 19, 2008 8:23 am
Posts: 4
A cleaner approach than using a struct is to subclass your toplevel and add all other widgets that you may need to access as class members. Though you can do it in plain C, I prefer using gob2 (a c-preprocessor) for this, in which you can simply do the following:

Code:
class My:Applic from Gtk:Window
{
    private GtkWidget *w_menubox = NULL;
    private GtkWidget *w_text_view = NULL;
    private GtkWidget *w_image_dir = NULL;
    private GtkWidget *w_output_dir = NULL;
    :

    public GtkWidget *
    new (int argc, char *argv[])
    {
        MyApplic *self = PA_WIDGET(GET_NEW);

        selfp->w_menubox = new_...();
        :
        return GTK_WIDGET(self);
    }
}


All you then need to do is to pass the self pointer around as userdata and you can access all your widgets from the whole application.

Another uglier approach is to use the g_object_set_data() to assign the other widgets to the data hash table that is part of all g_objects.


Last edited by dov on Mon Dec 22, 2008 5:24 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Subclassing is cleaner
PostPosted: Sat Dec 20, 2008 12:08 am 
Offline
GTK+ Geek

Joined: Wed Dec 19, 2007 9:15 pm
Posts: 61
Location: Glasgow, Scotland
dov wrote:
A cleaner approach than using a struct is to subclass your toplevel and add all other widgets that you may need to access as class members. Though you can do it in plain C, I prefer using gob2 (a c-preprocessor) for this, in which you can simply do the following:


The GObject Builder (GOB) project is esentially dead now, but Vala isn't:

Code:
using Gtk;

class My.Application : Window
{
   private Widget menubox;
   private Widget text_view;
   private Widget w_image_dir;
   private Widget w_output_dir;

   construct
   {
      menubox = new HBox ();
      [etc.]
   }

   static void main (string[] args)
   {
      Gtk.init (ref args);

      var app = new Application ();
      app.show_all ();

      Gtk.main ();
   }
}

_________________
Jabber – bcowan [at] fastmail.co.uk.


Top
 Profile  
 
 Post subject: gob and vala fullfill different niches
PostPosted: Sun Dec 21, 2008 6:10 pm 
Offline

Joined: Fri Dec 19, 2008 8:23 am
Posts: 4
Whether you prefer gob or vala depend on how close you want to be to C. gob has the advantage that you are still using C or C++ for your programming, whereas in Vala you have to create wrappers for all external libraries. After all, Vala is a different language, wheras gob is a preprocessor. So I don't consider gob dead, but your milage may vary.


Top
 Profile  
 
 Post subject: Re: gob and vala fullfill different niches
PostPosted: Sun Dec 21, 2008 11:54 pm 
Offline
GTK+ Geek

Joined: Wed Dec 19, 2007 9:15 pm
Posts: 61
Location: Glasgow, Scotland
dov wrote:
Whether you prefer gob or vala depend on how close you want to be to C. gob has the advantage that you are still using C or C++ for your programming, whereas in Vala you have to create wrappers for all external libraries. After all, Vala is a different language, wheras gob is a preprocessor. So I don't consider gob dead, but your milage may vary.


That is not the case, valac is a meta-compiler which turns Vala code into C. This is obviously very useful for the reasons you mentioned.

_________________
Jabber – bcowan [at] fastmail.co.uk.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group