GTK实施的消息框 [英] GTK implementation of MessageBox

查看:221
本文介绍了GTK实施的消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力实现使用GTK的Win32的的MessageBox 。使用SDL / OpenGL的应用程序,所以这不是一个GTK应用程序。

I have been trying to implement Win32's MessageBox using GTK. The app using SDL/OpenGL, so this isn't a GTK app.

我处理的初始化( gtk_init )排序的的MessageBox 函数里的东西如下:

I handle the initialisation (gtk_init) sort of stuff inside the MessageBox function as follows:

int MessageBox(HWND hwnd, const char* text, const char* caption, UINT type)
{
    GtkWidget *window = NULL;
    GtkWidget *dialog = NULL;

    gtk_init(&gtkArgc, &gtkArgv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), NULL);
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
    // gcallback calls gtk_main_quit()
    gtk_init_add((GtkFunction)gcallback, NULL);

    if (type & MB_YESNO) {
        dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, text);
    } else {
        dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, text);
    }

    gtk_window_set_title(GTK_WINDOW(dialog), caption);
    gint result = gtk_dialog_run(GTK_DIALOG(dialog));

    gtk_main();

    gtk_widget_destroy(dialog);

    if (type & MB_YESNO) {
        switch (result) {
        default:
        case GTK_RESPONSE_DELETE_EVENT:
        case GTK_RESPONSE_NO:
            return IDNO;
            break;
        case GTK_RESPONSE_YES:
            return IDYES;
            break;
        }
    }

    return IDOK;
} 

现在,我绝不是一个经验丰富的GTK程序员,我意识到,我可能做一些事情(S)可怕的错误。

Now, I am by no means an experienced GTK programmer, and I realise that I'm probably doing something(s) horribly wrong.

不过,我的问题是,最后一个对话框弹出使用此功能总是在那里,直到进程退出。任何想法?

However, my problem is that the last dialog popped up with this function stays around until the process exits. Any ideas?

推荐答案

嗯,好吧。我建议code是这样,那么:

Hmm, ok. I'd suggest code like this, then:

typedef struct {
int type;
int result;
} DialogData;

static gboolean
display_dialog(gpointer user_data)
{
DialogData *dialog_data = user_data;
GtkWidget *dialog;

if (dialog_data->type & MB_YESNO)
dialog = gtk_message_dialog_new(...);
else
dialog = gtk_message_dialog_new(...);

// Set title, etc.

dialog_data->result = gtk_dialog_run(...);

gtk_main_quit(); // Quits the main loop run in MessageBox()

return FALSE;
}

int MessageBox(...)
{
DialogData dialog_data;

dialog_data.type = type;

gtk_idle_add(display_dialog, &dialog_data);

gtk_main();

// Do stuff based on dialog_data.result
}

\r
\r

该结构是因为你需要传递的数据左右一对夫妇件。在 gtk_idle_add()通话会将主回路运行时要运行的方法和闲置的,而 FALSE 回归从 display_dialog()期权价值意味着它只能运行一次。我们从对话框得到的结果后,我们退出主循环。这会导致进入主循环()在主的MessageBox()方法返回,你就可以从那里访问结果

The struct is because you need to pass around a couple pieces of data. The gtk_idle_add() call adds a method to be run when the main loop is running and idle, and the FALSE return value from the display_dialog() call means that it's only run once. After we get the result from the dialog, we quit the main loop. That'll cause the gtk_main() in your main MessageBox() method to return, and you'll be able to access the result from there.

希望这有助于!

这篇关于GTK实施的消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆