Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the gtk+ window to the center of screen?

Tags:

c

gtk

I've created a new window and want to move it to the center of the screen, and how am I supposed to do it? I've tried below

gtk_widget_hide (GTK_WIDGET (window));
gtk_window_set_position (window, GTK_WIN_POS_CENTER);
gtk_widget_show_all (GTK_WIDGET (window));

but it seems that the gtk_window_set_position (window, GTK_WIN_POS_CENTER) is only triggered once. Is there something I did wrong?

like image 949
richard Avatar asked Jul 28 '13 13:07

richard


2 Answers

you must use gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS) :)

like image 179
Qas Avatar answered Nov 19 '22 08:11

Qas


To tell your application to position in the center, the following should work. I am however not familiar with the function you mention, and perhaps it is as good as this way of doing it. You can use the gtk_window_move() to move your window, the coordinates is by default calculated by default from the upper left corner, it is however possible to change this with:

void gtk_window_set_gravity (GtkWindow *window,
                             GdkGravity gravity);

The available enums is as follows:

GDK_GRAVITY_NORTH_WEST, GDK_GRAVITY_NORTH, GDK_GRAVITY_NORTH_EAST,
GDK_GRAVITY_WEST,GDK_GRAVITY_CENTER, GDK_GRAVITY_EAST, 
GDK_GRAVITY_SOUTH_WEST, GDK_GRAVITY_SOUTH, GDK_GRAVITY_SOUTH_EAST, 
and GDK_GRAVITY_STATIC.

And then use

void gtk_window_move (GtkWindow *window,
                      gint x,
                      gint y);

It is not certain it will change the initial window position, it is up to the Windowmanager to comply or not, as the manual for gtk tells the reader, most window managers ignore initial window positions.

This link will give you a lot of information regarding the function above https://developer.gnome.org/gtk3/3.5/GtkWindow.html#gtk-window-move

like image 25
David Bern Avatar answered Nov 19 '22 07:11

David Bern