Saving gtk window position

I am trying to keep the gtk window position (absolute) in order to restore it. I reopen applicaiton.

here is my code:

gint x,y;
  gtk_window_get_position(main_window,&x,&y);
  printf("current position is:\nx: %i\ny:%i\n",x,y);

this code is launched when the application exits, I always get: current position: x: 0 y: 0

What am I doing wrong.

+4
source share
1 answer

gtk_window_get_positionusually has a better guess , but you cannot rely on him because

the X Window system does not indicate how to obtain the geometry of the decoration placed in the window by the window manager.

(from gtk_window_get_position link )

To see the function in action, try the following:

#include <gtk/gtk.h>

int main(int argv, char* argc[])
{
    GtkWidget *window, *button;
    gint x, y;

    gtk_init(&argv, &argc);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Test Window");
    button = gtk_button_new_with_label("Close");

    g_signal_connect(G_OBJECT(button), "clicked", 
            G_CALLBACK(gtk_main_quit), (gpointer)NULL);
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);
    gtk_container_add(GTK_CONTAINER(window), button);
    gtk_window_get_position(GTK_WINDOW(window), &x, &y);

    printf("current position is:\nx: %i\ny:%i\n", x, y);
    gtk_window_set_position(GTK_WINDOW(window), 
            GTK_WIN_POS_CENTER_ALWAYS);
    gtk_window_get_position(GTK_WINDOW(window), &x, &y);

    printf("new position is:\nx: %i\ny:%i\n", x, y);
    gtk_widget_show_all(window); 


    gtk_main();
}

Edit

, , - :

 gtk_window_move(GTK_WINDOW(window), 420, 180);

gtk_widget_show_all(window);

( , ) , .

( gtk_window_move )

+3

Source: https://habr.com/ru/post/1619129/


All Articles