[comp.windows.x] XCreateGC vs XtGetGC

jones@kings.wharton.upenn.edu (Christopher V. Jones) (05/25/90)

I am currently using R3 of the X toolkit and need to use a non-shareable
graphics context for a new widget that I am building.  According to the
Nutshell books, I need to use XCreateGC rather than XtGetGC.  All well
and good.  Except that when should I call XCreateGC.  Originally I
put it in the Initialize method for the widget, but it choked because
the widget was not yet realized.  XtGetGC worked just fine (at least the
GC was created).  I suspect that I should call XCreateGC in the Realize\
method for the widget, but wanted to know if that was correct.

If this is a silly question, my apologies.

Christopher V. Jones
Department of Decision Sciences
The Wharton School
The University of Pennsylvania
Philadelphia, PA 19104

215-898-7290

JONES@wharton.upenn.edu

ben@hpcvlx.cv.hp.com (Benjamin Ellsworth) (05/26/90)

> ...I suspect that I should call XCreateGC in the Realize method for
> the widget, but wanted to know if that was correct.

I don't know precisely what you are trying to do, but for most cases
you are correct.  As I'm sure you are aware, you need a window for
XCreateGC, and you don't have one until the widget is realized.  This
behavior has bit many a beginning widget programmer (myself included).

-----------------------------------------------------------------------
Benjamin Ellsworth      | ben@cv.hp.com                | INTERNET
Hewlett-Packard Company | {backbone}!hplabs!hp-pcd!ben | UUCP
1000 N.E. Circle        | (USA) (503) 750-4980         | FAX
Corvallis, OR 97330     | (USA) (503) 757-2000         | VOICE
-----------------------------------------------------------------------
                     All relevant disclaimers apply.
-----------------------------------------------------------------------

kit@EXPO.LCS.MIT.EDU (Chris D. Peterson) (06/01/90)

> Originally I put it in the Initialize method for the widget, but it choked
> because the widget was not yet realized.  XtGetGC worked just fine (at least 
> the GC was created).  
> I suspect that I should call XCreateGC in the Realize\
> method for the widget, but wanted to know if that was correct.

Putting the CreateGC call in the initialize procedure it probabally the best
solution.  It solves few other problems too, such as saving server resources for
unrealized widget, and saving round trips when performing an XtSetValues on the
color of an unrealized widget.  If I were writing a widget I would try to put
all my Server resource creation code into Realize.  If you do this, remember
that you will have to make a few XtIsRealized() checks in SetValues() and other
places since the GC may not exist yet.

If you really want to create your GC's at Initialization time then you can just
use the RootWindow of the widget's screen when creating your GC.  The only
reason for the window (well, drawable actually) argument to XtCreateGC is so
that the depth of your GC will match the depth of the GC you are operating on.

BTW:  Don't forget to add a destroy proc to your widget so that the GC is freed
      when the widget is destroyed.


						Chris D. Peterson     
						MIT X Consortium 

Net:	 kit@expo.lcs.mit.edu
Phone:   (617) 253 - 9608	
Address: MIT - Room NE43-213