设置对话框不可调整大小或创建另一个gtk窗口

时间:2013-01-11 18:12:08

标签: c gtk gtk3

我有一个包含4个按钮的gtk_window 其中一个按钮将打开一个文件选择对话框(另一个功能) - 当选择文件时 - 将显示一个带有3 gtk_entry(此功能)的对话框。

static void function_with_3_gtk_entry (gchar *message, gpointer mainwin){
   GtkWidget *dialog, *label, *content_area, *entry1, *entry2, *entry3;
   /* Create the widgets */
   dialog = gtk_dialog_new_with_buttons ("Nome File", mainwin, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_OK, GTK_RESPONSE_NONE, NULL);
   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
   entry1 = gtk_entry_new();
   entry2 = gtk_entry_new();
   entry3 = gtk_entry_new();
   gtk_widget_set_size_request(dialog, 250, 200);

   /* Ensure that the dialog box is destroyed when the user responds */
   g_signal_connect_swapped (dialog, "response", G_CALLBACK (gtk_widget_destroy), dialog);

   /* Add the label, and show everything we've added to the dialog */
   gtk_container_add (GTK_CONTAINER (content_area), entry1);
   gtk_container_add (GTK_CONTAINER (content_area), entry2);
   gtk_container_add (GTK_CONTAINER (content_area), entry3);
   gtk_widget_show_all (dialog);
}

我的问题是:

  1. 我可以在此函数中使用另一个gtk_window而不是gtk_dialog吗?
  2. 如何设置对话框不可调整大小

1 个答案:

答案 0 :(得分:3)

下面的代码段有两个功能:

  • create_dialog()创建一个无法按要求调整大小的对话框。

  • create_window()根据请求在函数内创建一个窗口。

希望这有帮助。

#include <gtk/gtk.h>


/* Create a dialog, which cannot be resized by the user. */

void create_dialog(GtkWidget *button, gpointer window) {
    GtkWidget *dialog, *label, *content_area;

    /* New label for dialog content */
    label = gtk_label_new("This is a dialog!");

    /* Make a new dialog with an 'OK' button */
    dialog = gtk_dialog_new_with_buttons("This is a dialog, which (shouldn't | can't) be resized!", window, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_OK, GTK_RESPONSE_NONE, NULL);

    /* Add label to dialog */
    content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
    gtk_container_add(GTK_CONTAINER(content_area), label);

    /* Destroy dialog properly */
    g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);

    /* Set dialog to not resize. */
    gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);

    gtk_widget_show_all(dialog);
}

/* Create a window, while in a function! */

void create_window(GtkWidget *button, gpointer window) {
    GtkWidget *new_window, *label;

    /*New label for dialog content */
    label = gtk_label_new("This is a window, created in a function!");

    /* Create new window */    
    new_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    /* Add label to window */
    gtk_container_add(GTK_CONTAINER(new_window), label);

    gtk_widget_show_all(new_window);
}

/* Boring implementation bits */
gint main(gint argc, char **argv) {

    /* Initialise GTK+ */
    gtk_init(&argc, &argv);

    GtkWidget *main_win, *dialog_button, *window_button, *button_container;

    /* Create a button, one for each function. */    
    dialog_button = gtk_button_new_with_label("Create dialog!");    
    window_button = gtk_button_new_with_label("Create window!");

    /* Pack buttons into a box. */
    button_container = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
    gtk_box_pack_start(GTK_BOX(button_container), dialog_button, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(button_container), window_button, FALSE, FALSE, 0);

    /* Connect signals to button callback functions */    
    g_signal_connect(dialog_button, "clicked", G_CALLBACK(create_dialog), main_win);
    g_signal_connect(window_button, "clicked", G_CALLBACK(create_window), main_win);

    /* Create a new window, show it, and run GTK+ */
    main_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(main_win, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_container_add(GTK_CONTAINER(main_win), button_container);
    gtk_widget_show_all(main_win);
    gtk_main();
    return 0;
}