Adel Ahmed
Adel Ahmed

Reputation: 638

saving gtk window position

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.

Upvotes: 6

Views: 2591

Answers (2)

Scrooge McDuck
Scrooge McDuck

Reputation: 294

I've just implemented this feature using pygobject; it's not C but it could still be useful to have a look at it.

You can find the code here.

I've used GNOME Builder's default template for a python GNOME application, so it should be super-easy to replicate if you set your project with it.

Upvotes: 0

sjsam
sjsam

Reputation: 21965

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)

Upvotes: 4

Related Questions