[comp.windows.x] Athena widget for drawable surfaces

gnb@bby.oz.au (Gregory N. Bond) (02/20/91)

Warning: novice alert.  This may be a stupid question...

I am using wcl to prototype an X version of an application, that has a
large-ish graph and a pop-up control panel.  I am using the Athena
widgets as I don't have motif yet.  The panel part is OK, but I cannot
work out what sort of widget I need to use to get a resizable drawing
surface (like a Canvas in SunView).

Any ideas, pointers to similar sorts of code, pointers to man pages,
etc.  appreciated.

(For the moment, I am using a large Label widget so I can work on the
rest of the project.  Is there some way to access the pixmap of the
label and use that as a drawing surface?)

--
Gregory Bond, Burdett Buckeridge & Young Ltd, Melbourne, Australia
Internet: gnb@melba.bby.oz.au    non-MX: gnb%melba.bby.oz@uunet.uu.net
Uucp: {uunet,pyramid,ubc-cs,ukc,mcvax,prlb2,nttlab...}!munnari!melba.bby.oz!gnb

cjmchale@cs.tcd.ie (Ciaran McHale) (02/21/91)

In <1991Feb20.040838.26320@melba.bby.oz.au>
gnb@bby.oz.au (Gregory N. Bond) writes:

>Warning: novice alert.  This may be a stupid question...

It's better to ask a stupid question that not to ask at
all.

>[need a drawing area widget]

Several things you could do.

1. Use a core widget to draw in. This has no callbacks so
you use XtAddEventHandler() to add event handlers directly.
This has the disadvantage that your code will be
"hardwiring" in the handlers. It's nicer to use callbacks
which can then be changed in the resource file (especially
since you mention that you're using WCL).

2. Use a Label widget. Set this up so that it will display a
pixmap, p1, rather than a text string. Do all your drawing
to p1. The X11R4 Xlib manual (section 3.2.1) specifies that
if you set the background of a window to be a pixmap then
the X server is free to use the pixmap or make a copy of it.
Thus, when you draw into p1 you're not guaranteed that the
changes will be reflected in the window at the next Expose
event. You can get around this by:

	draw into p1;
	XSetWindowBackgroundPixmap(XtDisplay(a_label_widget),
				   XtWindow(a_label_widget),
				   p1
				  );
	XClearWindow( XtDisplay(a_label_widget), XtWindow(a_label_widget) );

The XClearWindow() call is needed because setting the
background of the window will have no effect until the next
expose event.

This method has the advantage of your application not having
to worry about Expose events since everything is drawn into
a pixmap which forms the background of the window. However,
there are some disadvantages such as:

	a. You still need to use XtAddEventHandler() for
	   events other than Expose, e.g., ButtonPress.
	b. If the pixmap is smaller than the window then it
	   will have a "tiling" effect, i.e., it will be
	   replicated across the window. (You can see this
	   in effect if you use the "xsetroot" command to
	   set the background of the root window to be a
	   bixmap.) You probably don't want this so you have
	   to overcome it by setting up a resize event
	   handler which destroys p1 and and creates a
	   new pixmap, p1', whose size is equal to the new
	   dimsensions of the window. Then draw to p1' and
	   set this to be the background pixmap of the
	   window. (Of course, you'll probably create p1'
	   and use XCopyArea() to copy from p1 to p1' before
	   destroying p1.)
	c. Pixmaps cost memory and large ones should be used
	   only if necessary. If your drawing is not very
	   complex (and hence time comsunming to redraw)
	   then a pixmap might be overkill.
	d. Some X servers place a limit on the maximun size
	   of a pixmap. Typically a pixmap can not be larger
	   than the screen. Windows, however, can be larger
	   than the screen. Thus, if you're trying to kepp a
	   pixmap as large as a window and the user resizes
	   the window to be larger than the screen then your
	   program may break.

3. At the back of the Athena widget set manual is a chapter
on wridget writing. This chapter shows, by exmaple, how to
write your drawing area widget.

4. You could obtain a suitable widget written by someone
else. A program called "xball" was posted to the net
(probably comp.sources.x) last year. This program comes
complete with a drawing area widget which has useful
callbacks (expose, resize, key/button press/release).
If you want it (51K in total for the program, widget and
docs) then send me email and I'll forward it on. One
advantage of it having callbacks is that it's easy to use in
WCL programs.


Regards,
Ciaran.
-- 
Ciaran McHale		"Verbosity says it all"			      ____
Department of Computer Science, Trinity College, Dublin 2, Ireland.   \  /
Telephone: +353-1-772941 ext 1538	FAX: +353-1-772204	       \/
Telex: 93782 TCD EI			email: cjmchale@cs.tcd.ie