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 Dec 22, 2014 3:30 am

All times are UTC




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: How to refer to other widget inside a function?
PostPosted: Mon Oct 23, 2006 4:14 am 
Offline

Joined: Mon Oct 23, 2006 4:04 am
Posts: 4
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:
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 1:56 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
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).

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 3:41 am 
Offline

Joined: Mon Oct 23, 2006 4:04 am
Posts: 4
Thanks,


Can u some me some code to illustrate your idea?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 4:28 am 
Offline

Joined: Mon Oct 23, 2006 4:04 am
Posts: 4
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?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 5:49 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
You would need to send the GtkComboBox widget to the callback function when connecting it. For example:

Code:
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:
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 ...

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 7:13 pm 
Offline

Joined: Mon Oct 23, 2006 4:04 am
Posts: 4
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);
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 10:44 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
You should hold all of your widgets in a structure & just pass the structure around. You can allocate a structure as the following:

Code:
typedef struct
{
  GtkWidget *combobox, *etc, *another;
} Widgets;

Widgets *w = (Widgets*) g_malloc (sizeof (Widgets));

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 4:35 am 
Offline
GTK+ Geek

Joined: Fri Oct 06, 2006 6:37 am
Posts: 57
otherwise
simply make the widget as universal variables.
then u can access any number of widgets in single function


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 2:22 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
This is possible, but you really shouldn't make a habit of declaring a slew of global variables...

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 13, 2006 5:33 am 
Offline
GTK+ Geek

Joined: Fri Oct 06, 2006 6:37 am
Posts: 57
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 13, 2006 3:38 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
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.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject: thank u
PostPosted: Tue Nov 14, 2006 4:10 am 
Offline
GTK+ Geek

Joined: Fri Oct 06, 2006 6:37 am
Posts: 57
mainly iam getting this xlib error:

Xlib: unexpected async reply (sequence 0x2d5)!
ican u tell me y this error occurs


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 4:41 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
A quick Google found this: http://mail.gnome.org/archives/gtk-app- ... 00209.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!

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


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

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 4 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group