Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saving gtk window position

Tags:

c

linux

gtk

I'm trying to save the gtk window position(absolute) to restore it wehn I open the applicaiton again

here's my code so far:

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

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

What am I doing wrong.

like image 440
Adel Ahmed Avatar asked Dec 09 '15 00:12

Adel Ahmed


1 Answers

gtk_window_get_position usually does a best guess but you cannot rely on it because

the X Window System does not specify a way to obtain the geometry of the decorations placed on a window by the window manager.

(from gtk_window_get_position reference)

To see the function in action, try something like below:

#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

If you wish the window to appear at a specific location, you could try something like:

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

However the above function should be placed after

gtk_widget_show_all(window);

because

most window managers ignore requests for initial window positions (instead using a user-defined placement algorithm) and honor requests after the window has already been shown.

(from gtk_window_move reference)

like image 160
sjsam Avatar answered Sep 28 '22 05:09

sjsam