[comp.windows.x] application design question

gthomas@uw-june (Greg Thomas) (08/04/89)

I'm developing an application (using the Xaw widgets) that will have a 
user interface somewhat similar to xfig, i.e., a "canvas" where certain 
drawing operations will be performed, and a "control panel" for selecting 
operations.

Currently, the canvas is implemented as a Label widget (because labels
are simple, and their size is easily controllable).  I add an event
handler for the canvas which processes button, motion, enter/leave, and 
exposure events.  The canvas and the control panel are children of a
Form widget.  Basically, a plain window would be sufficient for the
purpose of drawing.

My question is: Is there a better/easier/cleaner way to implement the canvas?
Any advice is welcome -- thanks!

	greg
	gthomas@cs.washington.edu

kit@EXPO.LCS.MIT.EDU (Chris D. Peterson) (08/04/89)

> Currently, the canvas is implemented as a Label widget (because labels
> are simple, and their size is easily controllable).  I add an event
> handler for the canvas which processes button, motion, enter/leave, and 
> exposure events.  The canvas and the control panel are children of a
> Form widget.  Basically, a plain window would be sufficient for the
> purpose of drawing.

> My question is: Is there a better/easier/cleaner way to implement the canvas?
> Any advice is welcome -- thanks!

Okay, you asked for it :-)

There are two methods of implementing something like this, one is to do what 
you have done, that is to take and existing widget and hack it up with a  few
XLib calls of your own, and thus make it do what you want.  This approach, while
not very clean and pretty does have the advantage of being much  easier to
implement, you need not understand any widget programming to get the behavior
that you desire.  The disadvantage is that the code that handles this is not
usually very self contatined, and unless you are very careful you will lose may
of the advantages that the toolkit gives you, such as well segmented user
interface objects, geometry management,and easy  user customization. 
Subclassability, of course, is now impossible.

The other way to approach this is to write your own widget to do the job.  What
most people fail to realize is that application writers are encouraged to write
their own custom widgets for their applications.  This approach will get you
all of the nice features of Xt, and allow you to seperate all the Xlib calls
into the body of the widget itself.  This make testing of this chunk of 
code much simplier, as it can be extracted into its own text program with ease.
If you are willing to tackel widget programming then I would strongly urge you
to try this approach.

Some specific comments on your particular problem.

If you choose to stay with your current approach then you will save some code by
using the core widget rather than the label widget,  It appears that you are
using none of the things that the label widget provides above those provided by
core, so you may want to just use core instead.  The class name for core is
"widgetClass".  Another thing that you may want to look at is using application
defined action routines rather than the XtAddEventHandler interface that you are
currently using.  This will allow the user to change the event bindings for the
action. (e.g.  S/he would rather draw with button 3 down instead of button 1).

If you want to look into the roll your own widget approach take a look at the
template widget provided in R3 as part of Xaw.

I hope this helps.

						Chris D. Peterson     
						MIT X Consortium 

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