RE: [Glade-users] The best way to use Glade generated code : suggestions to newbies from a newbie



I read your suggestions and amd struggling to understand what problem
you're trying to solve. Could you clarify what pointer manipulation
problems you're facing?

I agree, I use libglade (see below) all the time with no problems at
all.


I, for one, think that this is a rather messy solution and would like
to
suggest something more elegant once I figure out what the issue is!


Again I agree with this.  The beauty of glade is your interface is not
so muddled up in the code.

Vinay

On Sun, 22 Feb 2004, Manish Chakravarty wrote:
1) Make your UI in glade as you normally would.

Yes, do this.

2) Generate the source files

If this tickles your fancy or if you want to see how things work in GTK,
sure, but there is no need for it.

(the first two steps are common with the established procedure)
3) APPEND callbacks.c to main.c
4) include "callbacks.h" in main.c after all other files have been
included
5) Remove all references to callbacks.c in your makefile, but DO NOT
delete
it or move it from its original location

I wouldn't do any of these steps.  I do not use those files at all.  The
only file I need is the glade (XML) file.

6) Now edit the funcitons like on _button_ok_clicked_() to you
heart's
content..

OK, this is how I do it:


Somewhere in my code I have a utility function which assists loading
glade files, you do not have to do it this way, but I find it quicker
and cleaner:

GladeXML *glade_get_file (const gchar *filename,
                          const gchar *root,
                          const gchar *domain,
                          const gchar *first_widget, 
                          ...)
{
        GladeXML *gui = NULL;
        const char *name = NULL;
        GtkWidget **widget_ptr;
        va_list args;
  
        va_start (args, first_widget);
  
        gui = glade_xml_new (filename, root, domain);
        if (!gui) {
                g_warning ("%s: couldn't find necessary glade
file:'%s'", 
                           PRINT_MODULE, filename);
                return NULL;
        }
  
        for (name = first_widget; name; name = va_arg (args, char *)) {
                widget_ptr = va_arg (args, void *);
      
                if (!widget_ptr) {
                        continue;
                }
      
                *widget_ptr = glade_xml_get_widget (gui, name);
      
                if (!*widget_ptr) {
                        g_warning ("%s: glade file:'%s' is missing
widget:'%s'.", 
                                  PRINT_MODULE, filename, name);
                        continue;
                }
        }

        va_end (args);
    
        if(!gui) {
                return NULL;
        }

        return gui;
}

void glade_connect (GladeXML *gui, 
                    gpointer user_data, 
                    gchar *first_widget, 
                    ...)
{
        va_list args;
        const gchar *name = NULL;
        const gchar *signal = NULL;
        GtkWidget *widget = NULL;
        gpointer *callback = NULL;

        va_start (args, first_widget);
        
        for (name = first_widget; name; name = va_arg (args, char*)) {
                signal = va_arg (args, void*);
                callback = va_arg (args, void*);
      
                if (!(widget = glade_xml_get_widget (gui, name))) {
                        g_warning ("%s: glade file is missing
widget:'%s', aborting...", 
                                  PRINT_MODULE, name);
                        continue;
                }
      
                g_signal_connect (widget, signal, G_CALLBACK(callback),
user_data);
        }
  
        va_end (args);
}


Then, I use these functions to both get pointers to the widgets I am
interested in AND to connect the signals I want to be called back for:

Note: Dialog is a private structure with GtkWidget * pointers for all
the widgets I will need to use.


    GladeXML *xml = NULL;

    xml = glade_get_file("mygladefile.xml",
                         "preferences",
                         NULL,
                         "preferences", &dialog->window,
                         NULL);
    
    glade_connect(xml,
                  dialog,
                  "preferences", "response", on_response,
                  "preferences", "destroy", on_destroy,
                  NULL);

    g_object_unref(xml);


For the signals I have connected, I have the functions defined:

void on_destroy(GtkWidget *widget, gpointer user_data)
{
    /* do something */
}

void on_response(GtkDialog *dialog, gint response, gpointer user_data)
{
    gtk_widget_destroy(dialog->window);
}


Any questions, let me know.

Regards,
Martyn



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]