GTK应用程序不会退出对话框

时间:2014-09-04 13:58:50

标签: c dialog gtk

我编写了我的第一个更大的GTK + -Application并且在退出应用程序时遇到了一些麻烦。 我希望在对话框中有一个退出按钮,因为通常你应该以全屏模式运行该程序。

首先我试着打电话给" gtk_main_quit"从一个信号直接但它也没有工作。现在我通过一个事件尝试了它,控制台输出工作但是" gtk_main_quit"什么都不做!

有人可以解释我做错了什么吗?如果你想给我一些更好的编码技巧,我也会非常欢迎你!

先谢谢你的帮助!

#include <stdlib.h>
#include <gtk/gtk.h>
#include <time.h>

static gboolean gtk_delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
    g_message("delete event occured\n");

    gtk_main_quit();
    return TRUE;
}

static void check_toggle_fullscreen (GtkToggleButton *checkButton_fullscreen, GtkWindow *window)
{
    if (gtk_toggle_button_get_active(checkButton_fullscreen))
    {
        gtk_window_fullscreen(GTK_WINDOW(window));
    }
    else
    {
        gtk_window_unfullscreen(GTK_WINDOW(window));
    }
}


static gboolean double_clicked (GtkWidget *eventbox, GdkEventButton *event, GtkWindow *window)
{

    GtkWidget *dialog, *hbox, *checkButton_fullscreen, *image, *button_preferences, *button_closeApp;

    dialog = gtk_dialog_new_with_buttons("Schnelleinstellung", window, GTK_DIALOG_MODAL, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);

    checkButton_fullscreen = gtk_check_button_new_with_label("Fullscreen");
    image = gtk_image_new_from_stock(GTK_STOCK_FULLSCREEN, GTK_ICON_SIZE_BUTTON);

    hbox = gtk_hbox_new(FALSE, 10);
    gtk_box_pack_start_defaults (GTK_BOX(hbox), image);
    gtk_box_pack_start_defaults (GTK_BOX(hbox), checkButton_fullscreen);

    if (gdk_window_get_state(gtk_widget_get_window(GTK_WIDGET(window))) & GDK_WINDOW_STATE_FULLSCREEN)
    {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkButton_fullscreen), TRUE);
    }




    button_closeApp = gtk_button_new_from_stock(GTK_STOCK_QUIT);
    button_preferences = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);

    //Fill dialog with content
    gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
    gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), button_preferences);
    gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), button_closeApp);


    g_signal_connect(G_OBJECT(checkButton_fullscreen),  "toggled", G_CALLBACK(check_toggle_fullscreen), (gpointer)window);
    g_signal_connect(G_OBJECT(button_closeApp), "clicked", G_CALLBACK(gtk_delete_event), NULL);



    if (event-> type == GDK_2BUTTON_PRESS)
    {
        gtk_widget_show_all(dialog);

        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
    }



    return FALSE;
}


int main (int argc, char *argv[])
{

    GtkWidget *window;
    GtkWidget *vbox;
    GtkWidget *vbox2;
    GtkWidget *label[5];
    GtkWidget *frame1, *frame2;
    GtkWidget *eventbox;
    PangoFontDescription *font;

    gtk_init(&argc, &argv);


        //Window_TOPLEVEL
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

        gtk_window_set_title(GTK_WINDOW(window),"Abnahme");
        gtk_window_maximize(GTK_WINDOW(window));
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_NONE);

        gtk_window_set_decorated(GTK_WINDOW(window), TRUE);


            vbox   = gtk_vbox_new(FALSE, 10);
            vbox2  = gtk_vbox_new(FALSE, 10);
            frame1 = gtk_frame_new("Naechster Skid");
            frame2 = gtk_frame_new("Warteschlange");
            eventbox = gtk_event_box_new();

            gtk_frame_set_shadow_type(GTK_FRAME(frame1), GTK_SHADOW_IN);
            gtk_frame_set_shadow_type(GTK_FRAME(frame2), GTK_SHADOW_IN);




            label[0] = gtk_label_new("1");
            font = pango_font_description_from_string("Arial 40");
            gtk_widget_modify_font(label[0], font);

            gtk_label_set_markup(GTK_LABEL(label[0]), "<b>Erster Skid</b>");


            int j = 1;
            while(j<5)
            {
                gchar buffer[10];
                label[j] = gtk_label_new("0");
                g_snprintf(buffer, sizeof(buffer), "%i",j+1);
                gtk_label_set_markup(GTK_LABEL(label[j]), buffer);
                gtk_widget_modify_font(label[j], font);
                j++;
            }


            //Level 0
            gtk_container_add(GTK_CONTAINER(window), eventbox);
            gtk_event_box_set_above_child(GTK_EVENT_BOX(eventbox), TRUE);
            gtk_container_add(GTK_CONTAINER(eventbox), vbox);
                //Level 1
                gtk_box_pack_start(GTK_BOX(vbox),frame1, TRUE, TRUE, 0);
                    //Level 2
                    gtk_container_add(GTK_CONTAINER(frame1), label[0]);

                //Level 1
                gtk_box_pack_start(GTK_BOX(vbox),frame2, TRUE, TRUE, 0);
                    //Level 2
                    gtk_container_add(GTK_CONTAINER(frame2), vbox2);
                        //Level 3

                        int i = 1;
                        while(i<5)
                        {
                            gtk_box_pack_start(GTK_BOX(vbox2), label[i], TRUE, TRUE, 0);
                            i++;
                        }


            //Signals
            g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_delete_event), NULL);
            g_signal_connect(G_OBJECT(eventbox), "button_press_event", G_CALLBACK(double_clicked), (gpointer) window);

            gtk_widget_set_events(eventbox, GDK_BUTTON_PRESS_MASK);
            gtk_widget_realize(eventbox);



    /* Enter the main loop */
    gtk_widget_show_all (window);
    gtk_main ();


    return 0;
}

2 个答案:

答案 0 :(得分:1)

gtk_dialog_run()进入递归主循环。 gtk_main_quit()仅退出主循环的最内层递归。

我不知道GTK +是否提供了一种干净的方式来做你想做的事情;你可能不得不以某种方式做到这一点。

答案 1 :(得分:0)

删除gtk_dialog_run()和gtk_widget_destroy()之类的代码可用于退出对话框按钮。

if (event-> type == GDK_2BUTTON_PRESS)
{
    gtk_widget_show_all(dialog);

    /*
     * gtk_dialog_run(GTK_DIALOG(dialog));
     * gtk_widget_destroy(dialog);
     */
}

似乎原因是运行对话框阻止退出应用程序。