Method

GtkWindowget_size

Declaration [src]

void
gtk_window_get_size (
  GtkWindow* window,
  gint* width,
  gint* height
)

Description [src]

Obtains the current size of window.

If window is not visible on screen, this function return the size GTK+ will suggest to the [window manager][gtk-X11-arch] for the initial window size (but this is not reliably the same as the size the window manager will actually select). See: gtk_window_set_default_size().

Depending on the windowing system and the window manager constraints, the size returned by this function may not match the size set using gtk_window_resize(); additionally, since gtk_window_resize() may be implemented as an asynchronous operation, GTK+ cannot guarantee in any way that this code:

  // width and height are set elsewhere
  gtk_window_resize (window, width, height);

  int new_width, new_height;
  gtk_window_get_size (window, &new_width, &new_height);

will result in new_width and new_height matching width and height, respectively.

This function will return the logical size of the GtkWindow, excluding the widgets used in client side decorations; there is, however, no guarantee that the result will be completely accurate because client side decoration may include widgets that depend on the user preferences and that may not be visibile at the time you call this function.

The dimensions returned by this function are suitable for being stored across sessions; use gtk_window_set_default_size() to restore them when before showing the window.

To avoid potential race conditions, you should only call this function in response to a size change notification, for instance inside a handler for the GtkWidget::size-allocate signal, or inside a handler for the GtkWidget::configure-event signal:

static void
on_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
{
  int new_width, new_height;

  gtk_window_get_size (GTK_WINDOW (widget), &new_width, &new_height);

  ...
}

Note that, if you connect to the GtkWidget::size-allocate signal, you should not use the dimensions of the GtkAllocation passed to the signal handler, as the allocation may contain client side decorations added by GTK+, depending on the windowing system in use.

If you are getting a window size in order to position the window on the screen, you should, instead, simply set the window’s semantic type with gtk_window_set_type_hint(), which allows the window manager to e.g. center dialogs. Also, if you set the transient parent of dialogs with gtk_window_set_transient_for() window managers will often center the dialog over its parent window. It’s much preferred to let the window manager handle these cases rather than doing it yourself, because all apps will behave consistently and according to user or system preferences, if the window manager handles it. Also, the window manager can take into account the size of the window decorations and border that it may add, and of which GTK+ has no knowledge. Additionally, positioning windows in global screen coordinates may not be allowed by the windowing system. For more information, see: gtk_window_set_position().

Parameters

width gint*
 

Return location for width, or NULL.

 The argument will be set by the function.
 The argument can be set to NULL.
 The instance takes ownership of the data, and is responsible for freeing it.
height gint*
 

Return location for height, or NULL.

 The argument will be set by the function.
 The argument can be set to NULL.
 The instance takes ownership of the data, and is responsible for freeing it.