对话框的响应不能在创建对话框的同一函数中处理。这意味着在一个函数中,您不能创建对话框、处理其输入、关闭该对话框并打开新对话框。这是不可能的,因为当该函数退出时,对话框条目为空,第一个对话框只是显示出来。
应该将其作为一系列单独的事件处理程序完成。所有需要的变量(数据上下文)都应使用指向保存该上下文的结构的指针在这些单独的处理程序之间传递。
下面的完整可运行示例演示了这样的流程。
在第一个对话框处理程序中,我们想要对主窗口和一些容器对象进行操作,也许我们还想要一些其他共享在主窗口小部件之间的数据。为此上下文声明了结构"window_ctx_t"。在该上下文中有一个布尔标志,用于在关闭活动对话框之前锁定新对话框链的创建。
函数"first_dialog_create"创建了带有文本输入字段的第一个对话框。它接收"window_ctx_t",其中存储了用于对话框创建的主窗口指针。现在我们需要连接到该对话框响应处理程序,并为其功能提供足够的数据。在该示例中,我们提供用于读取文本的文本输入指针。
使用提供的数据,响应处理程序"add_new_button"能够读取文本输入,创建附加到主窗口的新对话框,并且还可以对提供的容器进行操作。
当第一个对话框数据上下文不再需要时,通过在对话框销毁处理程序中调用"g_free()"来释放它。
构建示例:
gcc -Wall -g dialogs.c -o dialogs `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`
Example "dialogs.c"
#include <gtk/gtk.h>
typedef struct {
GtkWidget *container;
GtkWindow *window;
gboolean dialogs_created;
} window_ctx_t;
typedef struct {
GtkEntry *entry;
window_ctx_t *window_ctx;
} first_dialog_data_t;
static void first_dialog_create(gpointer context);
static void another_dialog_response(GtkWidget *dialog, gint response_id,
gpointer ctx)
{
window_ctx_t *win_ctx = ctx;
gtk_widget_destroy(dialog);
win_ctx->dialogs_created = FALSE;
}
static void cleanup_first_dialog(GtkWidget *widget, gpointer data)
{
g_free(data);
}
static void add_new_button(GtkWidget *dialog, gint response_id,
gpointer ctx)
{
GtkWidget *button;
GtkWidget *button_text_dialog;
first_dialog_data_t *data = ctx;
if (response_id != GTK_RESPONSE_OK) {
data->window_ctx->dialogs_created = FALSE;
goto Exit;
}
button = gtk_button_new_with_label(gtk_entry_get_text(data->entry));
g_signal_connect_swapped(button, "clicked", G_CALLBACK(first_dialog_create),
data->window_ctx);
gtk_container_add(GTK_CONTAINER(data->window_ctx->container), button);
gtk_widget_show(button);
button_text_dialog = gtk_dialog_new_with_buttons("new button",
data->window_ctx->window, GTK_DIALOG_DESTROY_WITH_PARENT, "OK",
GTK_RESPONSE_OK, NULL);
g_signal_connect(button_text_dialog, "response",
G_CALLBACK(another_dialog_response), (gpointer)data->window_ctx);
gtk_widget_show_all(button_text_dialog);
Exit:
gtk_widget_destroy(dialog);
}
static void first_dialog_create(gpointer context)
{
first_dialog_data_t *data;
window_ctx_t *win_ctx = context;
if (win_ctx->dialogs_created)
return;
win_ctx->dialogs_created = TRUE;
data = g_new0(first_dialog_data_t, 1);
GtkWidget *new_button_dialog = gtk_dialog_new_with_buttons("new button",
win_ctx->window, GTK_DIALOG_DESTROY_WITH_PARENT,"OK",
GTK_RESPONSE_OK, NULL);
GtkWidget *content_area = gtk_dialog_get_content_area(
GTK_DIALOG(new_button_dialog));
GtkWidget *button_name_label = gtk_label_new("Press escape at any time to "
"cancel.\ntype in the button name");
gtk_container_add(GTK_CONTAINER(content_area), button_name_label);
GtkWidget * button_name_entry = gtk_entry_new();
gtk_entry_set_activates_default((GtkEntry*)button_name_entry,TRUE);
gtk_container_add(GTK_CONTAINER(content_area), button_name_entry);
data->entry = (GtkEntry *)button_name_entry;
data->window_ctx = win_ctx;
g_signal_connect(new_button_dialog, "response", G_CALLBACK(add_new_button),
data);
g_signal_connect(new_button_dialog, "destroy",
G_CALLBACK(cleanup_first_dialog), data);
gtk_widget_show_all(new_button_dialog);
}
static void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *box1;
GtkWidget *button;
window_ctx_t win_ctx;
gtk_init (&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
box1 = gtk_hbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), box1);
win_ctx.window = (GtkWindow *)window;
win_ctx.container = box1;
win_ctx.dialogs_created = FALSE;
g_signal_connect (window, "destroy", G_CALLBACK(destroy), NULL);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
button = gtk_button_new_with_label("Start");
g_signal_connect_swapped(button, "clicked", G_CALLBACK(first_dialog_create),
&win_ctx);
gtk_container_add(GTK_CONTAINER(box1), button);
gtk_widget_show_all(window);
gtk_main();
return 0;
}