GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Apr 16, 2014 10:16 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: glade_xml_signal_connect_data struct and local vars
PostPosted: Thu Jan 03, 2008 4:22 pm 
Offline
Familiar Face

Joined: Tue Dec 25, 2007 4:51 pm
Posts: 10
Ok, there must be a really simple way of doing this, that I'm just overlooking. Let's say I have a button, and upon click I want to pass 3 widgets, and 2 local variables. (one points to a dir, and the other points to a file). So far, I have this:

Code:
#define GLADE_FILE "/usr/share/testapp/testapp.glade"
typedef struct
{
   GtkWidget       *host_entry;
   GtkWidget       *port_entry;
   GtkWidget       *options_entry;
} MyWidgets;

void on_connect_btn_clicked(GtkButton *widget, gpointer user_data)
{
   MyWidgets      *widgets;
   widgets = (MyWidgets *)user_data;

   const gchar *port;
   const gchar *user;
   const gchar *options;
   
   host = gtk_entry_get_text (GTK_ENTRY (widgets->host_entry));
   port = gtk_entry_get_text (GTK_ENTRY (widgets->port_entry));
   options = gtk_entry_get_text (GTK_ENTRY (widgets->options_entry));

   //how do I reference *appdir and *appfile here???
}

int main (int argc, char *argv[])
{
   GladeXML      *gxml;
   MyWidgets      *widgets;
   const gchar *user    = g_get_home_dir();
   gchar *appdir       = g_build_path("/", user, ".testapp", NULL);
   gchar *appfile       = g_build_filename(appdir, "settings", NULL);

   widgets = g_slice_new (MyWidgets);
   gtk_init (&argc, &argv);
   gxml = glade_xml_new (GLADE_FILE, NULL, NULL);

   widgets->main_dialog = glade_xml_get_widget (gxml, "main_dialog");
   widgets->host_entry = glade_xml_get_widget (gxml, "host_entry");
   widgets->port_entry = glade_xml_get_widget (gxml, "port_entry");   
   widgets->options_entry = glade_xml_get_widget (gxml, "options_entry");   

   glade_xml_signal_connect_data (gxml, "on_connect_btn_clicked",
                              G_CALLBACK (on_connect_btn_clicked),
                              widgets);

...obviously this is just a snippet, as I'm trying to keep it simple to explain my question. What is the proper (best?) way to pass the pointers *appdir and *appfile into the glade_xml_signal_connect_data function, along with the already working "widgets" parameter?

Thanks,
-Lup


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 03, 2008 6:57 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Put 'em in the struct.

Code:
typedef struct
{
    GtkWidget         *host_entry;
    GtkWidget         *port_entry;
    GtkWidget         *options_entry;
    gchar                *appdir;
    gchar                *appfile;
} MyWidgets;

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 03, 2008 10:04 pm 
Offline
Familiar Face

Joined: Tue Dec 25, 2007 4:51 pm
Posts: 10
I thought about that, but it loses it's value. I mean, I assign the pointers within main, however I have it listed in MyWidgets struct. So when I go to call it from within on_connect_btn_clicked, all I get is a (null) value. As a test, I tried this:

Code:
typedef struct
{
    GtkWidget         *host_entry;
    GtkWidget         *port_entry;
    GtkWidget         *options_entry;
    gchar          *appdir;
    gchar          *appfile;
} MyWidgets;

void on_connect_btn_clicked(GtkButton *widget, gpointer user_data)
{
   MyWidgets      *widgets;
   widgets = (MyWidgets *)user_data;
   printf("appdir = %s\n", widgets->appdir);
}

int main (int argc, char *argv[])
{
    GladeXML        *gxml;
    MyWidgets        *widgets;
    const gchar *user     = g_get_home_dir();
    gchar *appdir         = g_build_path("/", user, ".testapp", NULL);
    gchar *appfile         = g_build_filename(appdir, "settings", NULL);

    widgets = g_slice_new (MyWidgets);
    gtk_init (&argc, &argv);
    gxml = glade_xml_new (GLADE_FILE, NULL, NULL);
...and when I click the button, I get "appdir = (null)" in the console.

It makes sense (that's it is failing), because I first have it appear in the struct, but then define the value in main(). I wouldn't think it should work that way....unless, I'm misunderstanding the syntax/method of passing a struct.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 03, 2008 11:12 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Once in your struct, you don't want to declare local pointers in main. They are part of the struct already declared in main. The g_slice_new allocated the space to store the pointers.

Code:
typedef struct
{
    GtkWidget         *host_entry;
    GtkWidget         *port_entry;
    GtkWidget         *options_entry;
    gchar             *appdir;
    gchar             *appfile;
} MyWidgets;

void on_connect_btn_clicked(GtkButton *widget, gpointer user_data)
{
    MyWidgets        *widgets;
    widgets = (MyWidgets *)user_data;
    printf("appdir = %s\n", widgets->appdir);
}

int main (int argc, char *argv[])
{
    GladeXML           *gxml;
    MyWidgets          *widgets;
    const gchar *user  = g_get_home_dir();
   
    widgets = g_slice_new (MyWidgets);
   
    widgets->appdir = g_build_path("/", user, ".testapp", NULL);
    widgets->appfile = g_build_filename(appdir, "settings", NULL);
   
    gtk_init (&argc, &argv);
    gxml = glade_xml_new (GLADE_FILE, NULL, NULL);

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject: Re: glade_xml_signal_connect_data struct and local vars
PostPosted: Fri Jan 04, 2008 1:45 am 
Offline
Familiar Face

Joined: Tue Dec 25, 2007 4:51 pm
Posts: 10
Lupine wrote:
Ok, there must be a really simple way of doing this, that I'm just overlooking.
...see, I told you ;-)

Seriously, Thanks!!! This simple step was driving me crazy...I can now put the finishing touches on my first full fledge C/Glade application!!!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 3:11 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Your welcome, hope you're having fun.

After that, you may want to brush up on your C skills and get really well aquanted with structs, typedefs, pointers, memory allocation, and macros.

Cheers.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 3:32 pm 
Offline
Familiar Face

Joined: Tue Dec 25, 2007 4:51 pm
Posts: 10
Will do, this test-project has already taught me a LOT, however I still don't fully understand some of the basics. But, since I learn by example, you, this site, and good ol' Google has really helped.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 4:18 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
I must admit, I knew very little C programming (basically, had only done a hello world) before starting in with GTK+. People will discourage that practice and insist you learn the command line first, but it can work.

You need to be disciplined though and really work hard when you're learning fundamental C concepts and GTK+ GUI programming in tandem. I had the "C Programming Language" book at my side and made sure I understood every single line of code as I went.

We often use shortcuts in GTK which we *should* know. For example, when you do:

/* assume window is GtkWindow *window; */

GtkWidget *widget = GTK_WIDGET (window);

That "casting macro" is actually

GtkWidget *widget = (GtkWidget*)window;

Which any beginning C book will explain. So in those type of cases you need to start in the GTK manual or even the GTK+ source code and figure out what GTK_WIDGET really is--then find out what that means in the world of C.


Good luck with you learning!

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 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