0
votes

how can i use gtk signal for exiting a window and then a diolog message showed that questions that Are you sure? and by clicking in Yes button it closes all of windows? this is my code :

void show_queExit(GtkWidget *widget, gpointer window)
{
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new(GTK_WINDOW(window),
            GTK_DIALOG_DESTROY_WITH_PARENT,
            GTK_MESSAGE_QUESTION,
            GTK_BUTTONS_YES_NO,
            "Are you sure to exit?");
    gtk_window_set_title(GTK_WINDOW(dialog), "Exit");
    gint result = gtk_dialog_run (GTK_DIALOG (dialog));

    if (result==GTK_RESPONSE_YES)
    {
        g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit),NULL);
        gtk_widget_destroy (dialog);
        printf("\n***\n");
    }
    else
        gtk_widget_destroy (dialog);
}

and this is my main function: int main(int argc, char **argv) {

    gtk_init(&argc, &argv);

    GtkWidget *window;
    GtkToolItem *exitIcon;
    GtkWidget *toolbar;


    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    toolbar = gtk_toolbar_new();
    gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);


    exitIcon = gtk_tool_button_new_from_stock(GTK_STOCK_QUIT);
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), exitIcon, -1);


    gtk_window_set_default_size(GTK_WINDOW(window), win_width, win_height);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

    g_signal_connect(G_OBJECT(exitIcon), "clicked",G_CALLBACK(gtk_main_quit), NULL);
    g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}
1
It's not totally clear what you want but I'm guessing you'd like to add a handler to "delete-event" signal of the toplevel window, and show your dialog from that handler.Jussi Kukkonen
excuse me because of my bad english.yes i want to do so.how can i do?user3204018

1 Answers

0
votes

Somethign like this:

static gboolean on_delete_event (GtkWidget *window,
                                 GdkEvent  *event,
                                 gpointer   data)
{
    GtkWidget *dialog;
    int answer;

    dialog = gtk_message_dialog_new (GTK_WINDOW(window),
                                     GTK_DIALOG_DESTROY_WITH_PARENT,
                                     GTK_MESSAGE_QUESTION,
                                     GTK_BUTTONS_YES_NO,
                                     "Are you sure to exit?");
    gtk_window_set_title (GTK_WINDOW(dialog), "Exit");

    answer = gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);

    if (answer == GTK_RESPONSE_YES) {
        g_print ("Yes\n");
        return FALSE;
    } else {
        g_print ("No\n");
        return TRUE;
    }

}

Then in main() or some initialization function:

g_signal_connect (window, "delete-event",
                  G_CALLBACK (on_delete_event), NULL);

There are several ways to handle the app exit -- you could just call gtk_main_quit() from the function above, or you can do what I think you tried originally: let GTK destroy the window when user clicks YES, and call gtk_main_quit() from the destroy signal handler.

In any case connecting the exit icon "clicked" signal to gtk_main_quit() is wrong if you want the dialog to come up: You should connect the signal to a function that you write that calls gtk_window_close().

g_signal_connect (exitIcon, "clicked",
                  G_CALLBACK (on_exit_clicked), window);