GTK+ Forums

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

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: About Dialogs
PostPosted: Mon Oct 15, 2007 9:01 pm 
Offline

Joined: Mon Oct 15, 2007 8:55 pm
Posts: 4
Hi. I have just started GTK programming and am on my way to building my first application (using Glade being developed in Anjuta). Currently I am trying to make an about dialog box.

Creating the window is easy, a simple glade template does most of the work for me. In my main application I have a button which when clicked calls a callback which does:
Code:
GladeXML *gxml = glade_xml_new(GLADE_FILE, "aboutdialog", NULL);
GtkWidget *aboutdialog = glade_xml_get_widget(gxml, "aboutdialog");
glade_xml_signal_autoconnect(gxml);

gtk_widget_show(aboutdialog);


However, at the moment it is possible to open up many about dialogs, while I only want one to be open (with it being brought into focus should the button be pressed while it is open).

My problem is that I am not sure how to manage the dialogs state (if it is open or not). I could use a global and then attach a delete_event handler to the aboutdialog, but it is a bit ugly. What is the recommended way to do it?

Regards, Freddie.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 2:50 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
You can run it as a dialog:

Code:
        GladeXML *gxml = glade_xml_new (GLADE_FILE, "aboutdialog", NULL);
        GtkWidget *dialog = glade_xml_get_widget (gxml, "aboutdialog");
        gtk_dialog_run (GTK_DIALOG (dialog));
       
        /* clean up */
        gtk_widget_destroy (dialog);
        g_object_unref (gxml);

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 3:59 pm 
Offline

Joined: Mon Oct 15, 2007 8:55 pm
Posts: 4
Micah Carrick wrote:
You can run it as a dialog:

Code:
        GladeXML *gxml = glade_xml_new (GLADE_FILE, "aboutdialog", NULL);
        GtkWidget *dialog = glade_xml_get_widget (gxml, "aboutdialog");
        gtk_dialog_run (GTK_DIALOG (dialog));
       
        /* clean up */
        gtk_widget_destroy (dialog);
        g_object_unref (gxml);

Thanks! Other than not needing the gtk_widget_destroy call (I get a critical error telling me it is not a widget, possibly because I hooked the response and close signals up to that already) it works fine.

It seems to be application modal, however, is there any way to make it so that it can be up while the rest of the application continues as usual?

Regards, Freddie.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 4:31 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
You can read all about GtkDialog in the API: http://library.gnome.org/devel/gtk/2.11/GtkDialog.html

There is a function gtk_window_set_modal() as well as a description of GtkDialog and it's properties.

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 7:40 pm 
Offline

Joined: Mon Oct 15, 2007 8:55 pm
Posts: 4
Well, I think I have found a way to do it:
Code:
        static GtkWidget *aboutdialog = NULL;

        if (!GTK_IS_WIDGET(aboutdialog))
        {
                GladeXML *gxml = glade_xml_new(GLADE_FILE, "aboutdialog", NULL);
                aboutdialog = glade_xml_get_widget(gxml, "aboutdialog");
                glade_xml_signal_autoconnect(gxml);
                g_object_unref(gxml);
        }

        gtk_widget_show(aboutdialog);
        gtk_window_present(GTK_WINDOW(aboutdialog));

This seems to do what I want!

Thank you very much for your help Micah Carrick.

On a side note the Glade docs say that the parsed XML is kept around for later use, does this mean that there is little overhead to calling glade_xml_new, assuming the file is the same each time?

Regards, Freddie.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 8:27 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
My understanding is that you will be fine with what you are doing. Especially since you are using g_object_unref on the gxml object. Because people typically only use an About dialog once--if any.

So, you're only building that node of the XML file when that bit of code runs and you're unreferencing it.

And just a note to other people reading this thread... there are many means to manipulate dialogs... modal, on top of parent, etc. Furthermore, the GNOME Human Interface Guidelines cover when you should use modal dialogs and when you should not.

This dialog is modal (default). You cannot interact with parent window and cannot open another dialog until this one is closed.
Code:
    GladeXML *gxml = glade_xml_new (GLADE_FILE, "aboutdialog1", NULL);
    GtkWidget *dialog = glade_xml_get_widget (gxml, "aboutdialog1");
    gtk_dialog_run (GTK_DIALOG (dialog));
   
    /* clean up */
    gtk_widget_destroy (dialog);
    g_object_unref (gxml);


This dialog is NOT modal. You can open up multiple AND interact with parent:
Code:
    GladeXML *gxml = glade_xml_new (GLADE_FILE, "aboutdialog1", NULL);
    GtkWidget *dialog = glade_xml_get_widget (gxml, "aboutdialog1");
    g_object_unref (gxml);
   
    gtk_widget_show_all (dialog);
   
    g_signal_connect (G_OBJECT (dialog), "response",
            G_CALLBACK (gtk_widget_destroy), NULL);


This dialog is NOT modal thus you CAN interact with parent, however, you cannot open up multiple versions:
Code:
    static GtkWidget *dialog = NULL;
   
    if (GTK_IS_WIDGET(dialog)) return;
   
    GladeXML *gxml = glade_xml_new (GLADE_FILE, "aboutdialog1", NULL);
    dialog = glade_xml_get_widget (gxml, "aboutdialog1");
    g_object_unref (gxml);
   
    gtk_widget_show_all (dialog);
   
    g_signal_connect (G_OBJECT (dialog), "response",
            G_CALLBACK (gtk_widget_destroy), NULL);

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


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 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