GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to refer to other widget inside a function?

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
janetsmith



Joined: 23 Oct 2006
Posts: 4

PostPosted: Mon Oct 23, 2006 4:14 am    Post subject: How to refer to other widget inside a function? Reply with quote

Hi,

I am using Glade to auto-generate the code.
Glade generates 4 files, support.c, main.c, interface.c, callbacks.c.

Let's say inside the main window(window1) has 2 buttons(button1, button2), and a combo box.

I want to store a common function for both button1 & button2 inside another file, util.c

So for the callback,
Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14

void
on_button1_clicked (GtkButton       *button,  gpointer         user_data)
{
    fillCombo();
}


void
on_button2_clicked (GtkButton       *button,  gpointer         user_data)
{
    fillCombo();
}


The question is, how do i make reference to combo1 from the fillCombo() function, in util.c?

Thanks
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Tue Oct 24, 2006 1:56 am    Post subject: Reply with quote

First, the source code generation feature in glade is depreciated. It should not be used and is not even included in Glade 3. Instead, you should use Libglade (GladeXML).

As for referencing the widget, the last parameter in all callback functions is a gpointer data parameter. You can send any pointer type to this parameter (including a GtkComboBox widget).
Back to top
janetsmith



Joined: 23 Oct 2006
Posts: 4

PostPosted: Tue Oct 24, 2006 3:41 am    Post subject: Reply with quote

Thanks,


Can u some me some code to illustrate your idea?
Back to top
janetsmith



Joined: 23 Oct 2006
Posts: 4

PostPosted: Tue Oct 24, 2006 4:28 am    Post subject: Reply with quote

Oh, i have checked what libglade it. Basically it use the concept something similiar to XUL.

It's a nice concept. However, since i am going to deal with the legacy gtk code, I have to learn the old method.



I come a cross a code which using "extern" on every widget. This is one way.

The other one might by pass the pointer in the function call, for instance, doSomething(GtkWidget *widget)

There is a lookup_widget function, But I only manage to use it within a callback function. Is it possible to use this function in other regular function?

What is the general way / elegant way of reference widget? Can you please provide some code or mechanism on passing/referencing the widget?
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Tue Oct 24, 2006 5:49 pm    Post subject: Reply with quote

You would need to send the GtkComboBox widget to the callback function when connecting it. For example:

Code: (Plaintext)
1
2
3
4
5
6
7
g_signal_connect (G_OBJECT (button1), "clicked", G_CALLBACK (on_button1_clicked), (gpointer) combobox);
...
static void
on_button1_clicked (GtkButton *button, GtkComboBox *combobox)
{
  fillCombo (combobox);
}


Remember that, because of the definition of the g_signal_connect() function and the callback function prototype, GtkComboBox can be set to any supported widget type. For example, you can use gpointer, GtkObject, GObject, GtkWidget or any other ancestor of GtkComboBox.

Since you only need the combobox in fillCombo(), you can also do the following because g_signal_connect_swapped() switches the order of the parameters in the callback function:

Code: (Plaintext)
1
2
3
4
5
6
7
g_signal_connect_swapped (G_OBJECT (button1), "clicked", G_CALLBACK (on_button1_clicked), (gpointer) combobox);
...
static void
on_button1_clicked (GtkComboBox *combobox)
{
  fillCombo (combobox);
}


For more information on GTK+, you can check out my book that is being released in March 2007 at
http://book.andrewkrause.net ...
Back to top
janetsmith



Joined: 23 Oct 2006
Posts: 4

PostPosted: Tue Oct 24, 2006 7:13 pm    Post subject: Reply with quote

from your code, the way u pass the combo widget is by specifying it in the g_signal_connect(). Well, this can only pass 1 widget. What if I want to make reference to 2 or more widgets?
Quote:

static void
on_button1_clicked (GtkButton *button, GtkComboBox *combobox)
{
fillCombo (combobox);
setLabel(label);
setEntry(entry);
}
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Wed Oct 25, 2006 10:44 pm    Post subject: Reply with quote

You should hold all of your widgets in a structure & just pass the structure around. You can allocate a structure as the following:

Code: (Plaintext)
1
2
3
4
5
6
typedef struct
{
  GtkWidget *combobox, *etc, *another;
} Widgets;

Widgets *w = (Widgets*) g_malloc (sizeof (Widgets));
Back to top
cnu_sree
GTK+ Geek


Joined: 06 Oct 2006
Posts: 57

PostPosted: Thu Oct 26, 2006 4:35 am    Post subject: Reply with quote

otherwise
simply make the widget as universal variables.
then u can access any number of widgets in single function
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Thu Oct 26, 2006 2:22 pm    Post subject: Reply with quote

This is possible, but you really shouldn't make a habit of declaring a slew of global variables...
Back to top
cnu_sree
GTK+ Geek


Joined: 06 Oct 2006
Posts: 57

PostPosted: Mon Nov 13, 2006 5:33 am    Post subject: Reply with quote

may i know wht is the problem behind declaring as a global variables.
y iam asking is iam get XLib error in the runtime.
so please tell me
in which cases the xlib errors occurs and how can i rectify these errors
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Mon Nov 13, 2006 3:38 pm    Post subject: Reply with quote

There are a ton of time when you can get an Xlib error. Can you be more specific such as posting the error & some code?

Also, the reason I say to avoid global variables is because it's just not good programming practice. You should be passing all necessary parameters through functions. There are a few exceptions, but you shouldn't use global variables in most cases.
Back to top
cnu_sree
GTK+ Geek


Joined: 06 Oct 2006
Posts: 57

PostPosted: Tue Nov 14, 2006 4:10 am    Post subject: thank u Reply with quote

mainly iam getting this xlib error:

Xlib: unexpected async reply (sequence 0x2d5)!
ican u tell me y this error occurs
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Tue Nov 14, 2006 4:41 am    Post subject: Reply with quote

A quick Google found this: http://mail.gnome.org/archives/gtk-app-devel-list/2002-April/msg00209.html

If you are still having the problem after using this solution, let me know and give me some code to work with. I really can't help you any further unless I have something to compile ... hope this helps!
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP