Re: Problem with text widget




"O'CONNOR, Steve" <SOCONNOR@baea.com.au> writes:

> I think that you have hit the common problem of not realizing the widget
> before use.
> 
> I dont think that there is a single programmer using GTK that has not
> run into this problem. Think of it as a rite of passage rather than any
> stupidity on your part. I will attempt to 
> explain the subtle nature of this problem ....
> 
> ------------------------------------------------
> Unrelated NOTE -
> 
> Any complaints that GDK/GTK produce display the opposite .
>    ie - GDK asserts that (gdk_window_ref != NULL), so if gdk_window_ref
> is NULL, then 
>          you get the message window != NULL coming up as a comlpaint.
> This can be
>          a little confusing the first time around.
> 
> ----------------------------------------------------------
 
Perhaps it would be less confusing if GLIB said

** WARNING **: Assertion failed: file gtkstyle.c: line 266 (gtk_style_attach): 
   "window != NULL"

Or something.

> Now, on the subject of realizing Widgets ...  When you create
> Windows and Widgets in GTK, the structures to hold them are all
> created, but the actual cooresponding XWindow objects are not
> created yet. As soon as you do a gtk_widget_show (), or otherwise
> display the window, all of the XWindow peer objects are created, and
> the widgets become useable.
>
>      Note however, that between the first declaration of the widget,
> and the drawing of the widget, the actual widget exists in a
> phantasmal state of limbo. If you call any routines that try and do
> useful things with this widget during the state of limbo, GDK will
> complain that the 'window' is NULL. So, it looks to me like you are
> trying to set the text in the field BEFORE the field is drawn.

This is generally quite accuracate; but...

There are many useful things you can do with a widget before
it is realized. With a few exceptions (adding text to a Text widget
is one of them), the only things you can't do are things that
involve accessing widget->window directly. (widget->window is
the GdkWindow / X Window corresponding to the GTK widget)

>      Finally, it is quite valid, and desireable to initialize
> widgets before displaying them by doing a set_text, setting fonts
> and colours, etc. 

- gtk_entry_set_text() for doesn't require a realized widget.
- setting fonts and colors using styles is something you usually
  do by modifiying the style, which you can do before realizing 
  the widget. (And, in fact, in the 1.0 release branch, will
  work better if you do before realizing the widget)

> To do this, call gtk_widget_realize() on the
> top-level window widget, and the magic peer Xwindow objects will be
> created but not displayed yet. This makes all the widgets in that
> window usable, so you can then init them before display.

A good explaination. I do want to emphasize though, that
simply realizing everything as soon as you create it is inefficient,
and can cause other problems. (for instance, gtk_widget_set_events()
must be called _before_ the widget is realized).

I think the rule that I stated above, that you only need to realize()
when:

 - accessing widget->window

Is pretty accurate, with just a few exceptions:

 - inserting text into a text widget 
 - setting up some DND stuff currently.
 - ???

It would be nice to get rid of these exceptions...

Regards,
                                        Owen



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