Re: How to get the widget's current width and height?



Tom Liu wrote:

To get the current widget size need a reason? Sorry to say that but
there are a lot of reasons. Sizes and positions are basic stuff!

For example: I have a widget dynamicly draw a clock, I want the clock Just fit into the widget, I need the size information every minutes.

Sure, I can connect the size alloc and remember the size by myself.
But...

Thanks iain just told me:
        GTK_WIDGET (widget)->allocation.width;
        GTK_WIDGET (widget)->allocation.height;
This code works! And thanks GTK+ use C so that we can access all the
private data they have.
When gtk_widget_size_allocate() gets called; or when
a widget gets "realized" for that matter; It is the responsability
of the widget in question to allocate the underlying gdk resources
based on that widgets GtkAllocation. Once the widget has
"allocated underlying gdk resources"; At least the data structures
will be present in memory but its not garaunteed that the actual
X resource is present on the X server's RAC list (i.e. gdk_window_foriegn_new( XID ) might fail with a "bad pixmap" error) unless you make a call to XSync(Display *).

So you see that:
> having a function to query the current size would
> just facilitate writing fundamentally broken code.

Because you can't really know what size you're window is;
You can only know what size your window is "planned to be".

Regards,
                               -Tristan

-----Original Message-----
From: John K Luebs [mailto:jkluebs luebsphoto com] Sent: Tuesday, November 18, 2003 11:52 PM
To: Tom Liu
Cc: gtk-app-devel-list gnome org
Subject: Re: How to get the widget's current width and height?

On Tue, Nov 18, 2003 at 09:29:50AM -0500, Tom Liu wrote:
gdk_drawable_get_size(w->window,&wid,&hi); may work for the widget
which
has a xwindow such as eventbox. For vbox, label, button etc. It will
not
work.
Can't believe there is no function to retrive size of widget?!


There is a good reason for this. The size of a widget (it's allocation) is dynamic (can be changed by the user, WM, etc.), so having a function
to query the current size would just facilitate writing fundamentally
broken code. There's no point in getting a value that could become
invalid the moment you return to the mainloop.

As another poster has told you, you should connect to size-allocate.
This way you know that you always have the correct size, and can react
appropriately.

You should also take a moment to think about why you need the size.
Are you sure this is not something you can do in a more
straightforward manner using the packing mechanisms?

--jkl

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list







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