GTK quit dialog box?

1.7k views Asked by At

I am using C with GTK to create a dialog box to confirm an exit.

My main window has a quit button -> Dialog box with Yes and No
Yes should quit the program entirely
No should close the dialog box.

I have tried

dialog = gtk_dialog_new();
...
button = gtk_button_new_with_label("Yes");
g_signal_connect(button, "clicked", G_CALLBACK(close), NULL);

My callback function close is

static void close(GtkWidget* widget, gpointer data)
{
gtk_main_quit();
}

However, my Yes button does not quit anything. Is there a reason for this?

3

There are 3 answers

0
andlabs On

gtk_main_quit() only leaves the current nested main loop. gtk_dialog_run() creates its own nested main loop, so your gtk_main_quit() only breaks out of gtk_dialog_run(), not out of gtk_main().

0
liberforce On

Use the delete-event of your main window to call your popup when the user tries to close the window. Then in your on_delete_event callback, use the value returned by gtk_dialog_run to return the right value. Your on_delete_event callback return value controls if the event is propagated and sends the destroy signal. Call gtk_main_quit from your on_destroy callback. Connect your on_destroy callback to the destroy signal of your main window. VoilĂ  !

0
Joel On

Since you don't need a main loop for that, you should test what runs return:

/* pseudo code*/
if (gtk_dialog_run(dlg) == RESPONSE_YES) {
 gtk_widget_destroy (dlg); # destroys dialog and exits
}