GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Confirm quit using dialog on the "delete-event" si

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 515
Location: Portland, OR USA

PostPosted: Thu Jun 07, 2007 12:40 am    Post subject: Confirm quit using dialog on the "delete-event" si Reply with quote

Here's a quick demo on using a confirmation dialog when the user quits the application to verify they really want to quit.

People ask this a lot. Typically you would only want to do this if there is unsaved data. Otherwise it is annoying (IMO).

Compile it using:

Code: (Plaintext)
1
cc -Wall -g `pkg-config --cflags --libs gtk+-2.0`  -o example main.c 


Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

#include <gtk/gtk.h>
 
static gboolean on_window_delete_event (GtkWidget*, GdkEvent*, gpointer);
GtkResponseType confirm_quit_dialog();
               
int
main (int argc, char *argv[])
{
       
        GtkWidget *window;
       
        /* initialize the GTK+ library */
        gtk_init (&argc, &argv);

        /* create main window */
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title (GTK_WINDOW (window), "Quit Dialog Example");
        gtk_container_set_border_width (GTK_CONTAINER (window), 10);
        gtk_widget_set_size_request (window, 200, 100);
       
        /* connect signals */
        g_signal_connect (G_OBJECT(window), "destroy",
                        G_CALLBACK (gtk_main_quit), NULL);
        g_signal_connect (G_OBJECT(window), "delete-event",
                        G_CALLBACK (on_window_delete_event), NULL);       
               
        /* show the main window */
        gtk_widget_show (window);

        gtk_main ();
       
        return 0;     
}

static gboolean
on_window_delete_event (GtkWidget *w, GdkEvent *e, gpointer user_data)
{
        if (confirm_quit_dialog() == GTK_RESPONSE_NO)
        {
                return TRUE;    /* do not quit */
        }
        else return FALSE;      /* quit */
}

GtkResponseType
confirm_quit_dialog()
{
        GtkWidget *dialog;
        GtkResponseType response;
       
        /* create the dialog */
        dialog = gtk_message_dialog_new (NULL,
                    GTK_DIALOG_DESTROY_WITH_PARENT,
                        GTK_MESSAGE_QUESTION,
                        GTK_BUTTONS_YES_NO,
                        "Are you sure you want to quit?");

        gtk_window_set_title(GTK_WINDOW(dialog), "Really Quit?");
       
        /* run the dialog */
        response = gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);   
       
        return response;
}
Back to top
broeisi
Familiar Face


Joined: 18 May 2007
Posts: 8

PostPosted: Fri Jul 13, 2007 11:07 am    Post subject: Reply with quote

Nice example...

Thanks a lot..
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP