How to center one window on the other in GTK?

Asked

Viewed 134 times

4

I have a main window and I want to display small text in a small window, open in the center of the main window. I still can’t get the small window to open in the center of the main.

I tried to use GTK_WINDOW_TOPLEVEL and GTK_WINDOW_POPUP together with the functions gtk_widget_set_parent_window and gtk_widget_set_parent, but did not produce the desired effect. By using gtk_widget_set_parent_window, the window with the warning is not displayed.

The current code to open it:

static void show_warning(char *title, char *text)
{
    GtkWidget *warning_window;
    GtkWidget *box;
    GtkWidget *label;
    GtkWidget *button;

    warning_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(warning_window), title);
    g_signal_connect(GTK_WINDOW(warning_window), "destroy", G_CALLBACK(gtk_widget_destroy), NULL);
    gtk_container_set_border_width(GTK_CONTAINER(warning_window), 20);
    gtk_window_set_resizable(GTK_WINDOW(warning_window), FALSE);    

    //window é a janela principal
    gtk_widget_set_parent(warning_window, window);
    gtk_window_set_position(GTK_WINDOW(warning_window), GTK_WIN_POS_CENTER_ON_PARENT);

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
    gtk_container_add(GTK_CONTAINER(warning_window), box);

    label = gtk_label_new(text);
    gtk_box_pack_start(GTK_BOX(box), label, TRUE, FALSE, 0);

    button = gtk_button_new_with_label("Ok");
    g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), warning_window);
    gtk_box_pack_start(GTK_BOX(box), button, TRUE, FALSE, 0);

    gtk_widget_show_all(warning_window);    
}

How to make the warning window open in the center of the main window?

1 answer

1


I was able to create the window as I wanted using a simpler solution.

static void show_warning(char *title, char *text)
{
    GtkWidget *warning_dialog;
    GtkWidget *content_area;
    GtkWidget *label;

    warning_dialog = gtk_dialog_new_with_buttons
                                    (   title,
                                        GTK_WINDOW(window),
                                        GTK_DIALOG_MODAL,
                                        "Ok",
                                        GTK_RESPONSE_NONE,
                                        NULL                    );

    gtk_container_set_border_width(GTK_CONTAINER(warning_dialog), 10);
    gtk_window_set_resizable(GTK_WINDOW(warning_dialog), FALSE);

    content_area = gtk_dialog_get_content_area(GTK_DIALOG(warning_dialog));
    label = gtk_label_new(text);

    g_signal_connect_swapped(   warning_dialog, 
                                "response", 
                                G_CALLBACK(gtk_widget_destroy), 
                                warning_dialog                  );

    gtk_container_add(GTK_CONTAINER(content_area), label);
    gtk_widget_show_all(warning_dialog);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.