Re: How to get label widget size?



Esteban Quijano Vincenzi <equijano videotek tv> writes:
> How could I get the size and position of a widget before its Show()
> command.
> 
> I have the next scenario:
> A GtkWidget  window which size is 300x300
> A GtkLabel     label which text is a little string
> A GtkFixed     fixed which is contained inside window
> 
> label is contained inside fixed at 10, 10 and it behaves correctly.
> When I try to get the size of label with:
> 
>     gdk_window_get_geometry(label->window, &x, &y, &width, &height,
> &depth);
> 
> width and height are 300 x 300!

That's because label->window is just the same as window->window and
fixed->window, because label and fixed are no-window widgets and use
their parent's window.

The current size of a widget is widget->allocation.width,
widget->allocation.height. However this is the CURRENT size. In most
cases you need to be tracking size changes, e.g. by
gtk_signal_connect_after() to the size_allocate signal.
If you don't do that your app will break if the user resizes it or
the window manager is in a bad mood.

The allocation won't necessarily be the same as the final onscreen
size of the widget until the widget is already onscreen. You can get
the "normal" size of the widget with gtk_widget_size_request(widget,
&requisition) and look at requisition.width, requisition.height.

The reason this is complicated, btw, is because it's a bad idea. ;-)
Use real packing containers instead of fixed and you won't need to
know the size most of the time.

Havoc





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]