|
|
| Author |
Message |
|
|
janetsmith
Joined: 23 Oct 2006 Posts: 4
|
Posted: Mon Oct 23, 2006 4:14 am Post subject: How to refer to other widget inside a function? |
|
|
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
|
Posted: Tue Oct 24, 2006 1:56 am Post subject: |
|
|
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
|
Posted: Tue Oct 24, 2006 3:41 am Post subject: |
|
|
Thanks,
Can u some me some code to illustrate your idea? |
|
| Back to top |
|
 |
janetsmith
Joined: 23 Oct 2006 Posts: 4
|
Posted: Tue Oct 24, 2006 4:28 am Post subject: |
|
|
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
|
Posted: Tue Oct 24, 2006 5:49 pm Post subject: |
|
|
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
|
Posted: Tue Oct 24, 2006 7:13 pm Post subject: |
|
|
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
|
Posted: Wed Oct 25, 2006 10:44 pm Post subject: |
|
|
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
|
Posted: Thu Oct 26, 2006 4:35 am Post subject: |
|
|
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
|
Posted: Thu Oct 26, 2006 2:22 pm Post subject: |
|
|
| 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
|
Posted: Mon Nov 13, 2006 5:33 am Post subject: |
|
|
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
|
Posted: Mon Nov 13, 2006 3:38 pm Post subject: |
|
|
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
|
Posted: Tue Nov 14, 2006 4:10 am Post subject: thank u |
|
|
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
|
|
| Back to top |
|
 |
|