home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / Other / Coherent / DemonArchives / docs / xt.faq.z / xt.faq
Encoding:
Text File  |  1993-10-12  |  82.6 KB  |  2,073 lines

  1. Archive-name: Xt-FAQ
  2. Version: $Id: FAQ-Xt,v 1.32 1993/10/01 13:47:38 ware Exp $
  3.  
  4.             The X Toolkit Intrinsics F.A.Q
  5.               A monthly posting
  6.  
  7.  
  8. This article contains the answers to some Frequently Asked Questions
  9. (FAQ) from comp.windows.x about the X Toolkit Intrinsics.  To submit
  10. questions (preferably with an answer) send email to: ware@cis.ohio-state.edu
  11.  
  12. This FAQ is available on the archive site rtfm.mit.edu in the directory:
  13.  
  14.     pub/usenet/comp.answers
  15.  
  16. The name under which a FAQ is archived appears in the Archive-name
  17. line at the top of the article.  This FAQ is archived as Xt-FAQ.
  18.  
  19. All code fragments are public domain.  
  20.  
  21.                    Contents
  22.  
  23. 0.  Xt Glossary
  24. 1.  Software Versions
  25. 2.  Related FAQ's
  26. 3.  Why does my app core dump when I use signals/alarms/cthreads?
  27. 4.  How do I use a different visual than the default?
  28. 5.  Which visual should an application use?
  29. 6.  Why do only Shell widgets have a Visual?
  30. 7.  Which visual, depth and colormap do Shells inherit?
  31. 8.  I've done all the above and I still get a BadMatch error.  Why?
  32. 9.  Why doesn't my widget get destroyed when I call XtDestroyWidget()?
  33. 10. How do I exit but still execute the DestroyCallbacks?
  34. 11. How do I resize a Shell widget?
  35. 12. Why can't XtAppAddInput() handle files?
  36. 13. What good books and magazines are there on Xt?
  37. 14. What Widgets are available?
  38. 15. What alternatives to the Intrinsics are there?
  39. 16. How do I pass a float value to XtSetValues?
  40. 17. How do I write a resource converter?
  41. 18. How do I open multiple displays?
  42. 19. What changed from R3 to R4 to R5?
  43. 20. Where are the resources loaded from?
  44. 21. What order are callbacks executed in?
  45. 22. How do I know if a widget is visible?
  46. 23. How do I reparent a widget in Xt, i.e. XtReparentWidget()?
  47. 24. Why use XtMalloc, XtFree, etc?
  48. 25. How to debug an Xt application?
  49. 26. Why don't XtAddInput(), XtAddTimeout() and XtAddWorkProc() work?
  50. 27. What is and how can I implement drag and drop?
  51. 28.+How can I add a C++ member function as a widget callback?
  52. 29.+How can I identify the children of a manager widget?
  53. 30.+Can I use XtMoveWidget(), ... to move widgets I created?
  54. 31.+Why is XtGetValues() on XtNx, XtNy of my top level shell wrong?
  55. 32.+Why do some people use XmN<something> as resource names?
  56. 33.+How do I make my life easier when designing an application?
  57. 34.+Why can't I override translations? Only the first item works.
  58. 35.+Why do I get "Warning: Widget class version mismatch"?
  59. 36.+Where can I get a good file-selector widget?
  60. 37.+Where can I find a hypertext widget or source code?
  61. 38.+What widget is appropriate to use as a drawing canvas?
  62. 39.+What is this link problem with _get_wmShellWidgetClass?
  63. 40.+Why does XtGetValues not work for me (sic)?
  64. 41.+Is this a memory leak in the X11R4 XtDestroyWidget()?!
  65. 42.+Is this a memory leak in the X11R4 deletion of work procs?!
  66. 43.+How do I query the user synchronously using Xt?
  67. 44.+How do I simulate a button press/release event for a widget?
  68.  
  69. ----------------------------------------------------------------------
  70. Subject: 0.  Xt Glossary
  71. ----------------------------------------------------------------------
  72.  
  73. o The Xt Intrinsics implement an object oriented interface to C code
  74.   to allow useful graphical components to be created.  Included with
  75.   this are classes that provide the base functionality: Object,
  76.   RectObj, Core, Composite, Constraint, Shell, OverrideShell, WMShell,
  77.   etc.  The terms "Xt" and "Intrinsics" are used interchangeably,
  78.   however, they are used very precisely to mean a specific library of the X
  79.   window system.  In particular, it does not include the Athena,
  80.   Motif, OLIT or any other widget set.  Without further widgets the
  81.   Intrinsics are not especially useful.
  82.  
  83. o A widget refers to a user interface abstraction created via Xt.  The
  84.   precise use, is any object that is a subclass of the Core class.  It
  85.   is used loosely to refer to anything that is a subclass of the
  86.   Object class although these are more accurately called windowless
  87.   widgets or gadgets.
  88.  
  89. o Xlib is the C interface to the X11 protocol.  It is one layer below
  90.   the Xt Intrinsics.  Typically a widget uses relatively few Xlib
  91.   functions because Xt provides most such services although an
  92.   understanding of Xlib helps with problems.
  93.  
  94. ----------------------------------------------------------------------
  95. Subject: 1.  Software Versions
  96. ----------------------------------------------------------------------
  97.  
  98. The following are the latest versions of Xt based software:
  99.         _____________________________________________________________
  100.     Software    Version        Released    Next Expected
  101.         _____________________________________________________________
  102.     X11R4        patch 18            (none)
  103.     X11R5        patch 25    5/25/93        ??
  104.     Athena Widgets    (see X11R5)
  105.     Motif        1.2.2        3/93        ??
  106.     OLIT        ??        ??        ??
  107.     Xtra        2.5        6/15/92        ??
  108.     Xw        X11R4                (none)
  109.     Xcu        X11R5                (none)
  110.     fwf        3.4        1/11/92        4/93
  111.         _____________________________________________________________
  112.  
  113. ----------------------------------------------------------------------
  114. Subject: 2.  Related FAQ's
  115. ----------------------------------------------------------------------
  116. David B. Lewis (uunet!craft!faq) maintains the FAQ on X.  It
  117. is posted monthly on comp.windows.x and located on ftp.x.org in contrib/FAQ.
  118.  
  119. Liam R. E. Quin (lee@sq.sq.com) posts an FAQ list on Open Look to 
  120. comp.windows.x.  
  121.  
  122. Brian Dealy (dealy@kong.gsfc.nasa.gov) posts an FAQ list on Motif to
  123. comp.windows.x.motif. it is also on ftp.x.org under contrib/Motif-FAQ
  124.  
  125. Peter Ware (ware@cis.ohio-state.edu) posts an FAQ list for
  126. comp.windows.x.intrinsics; it is on ftp.x.org in contrib/FAQ-Xt.
  127.  
  128. ----------------------------------------------------------------------
  129. Subject: 3.  Why does my app core dump when I use signals/alarms/cthreads?
  130. ----------------------------------------------------------------------
  131.  
  132. In brief, Xlib, Xt and most widget sets have no mutual exclusion for
  133. critical sections.  Any interrupt handler is likely to leave one of
  134. the above libraries in an inconsistent state -- such as all the
  135. appropriate flags not yet set, dangling pointers, in the middle of a
  136. list traversal, etc.  Note that the ANSI C standard points out that
  137. behavior of a signal handler is undefined if the signal handler calls
  138. any function other than signal() itself, so this is not a problem
  139. specific to Xlib and Xt; the POSIX specification mentions other
  140. functions which may be called safely but it may not be assumed that
  141. these functions are called by Xlib or Xt functions.
  142.  
  143. The only safe way to deal with signals is to set a flag in the
  144. interrupt handler.  This flag later needs to be checked either by a
  145. work procedure or a timeout callback.  It is incorrect to add either
  146. of these in the interrupt handler.  As another note, it is dangerous
  147. to add a work procedure that never finishes.  This effectively
  148. preempts any work procedures previously added and so they will never
  149. be called.  Another option is to open a pipe, tell the event loop
  150. about the read end using XtAppAddInput() and then the signal handler
  151. can write a byte to the write end of the pipe for each signal.
  152. However, this could deadlock your process if the pipe fills up.
  153.  
  154. Why don't the Intrinsics deal with this problem?  Primarily because it
  155. is supposed to be a portable layer to any hardware and operating
  156. system.   Is that a good enough reason -- I don't think so.
  157.  
  158.         Note: the article in The X Journal 1:4 and the example in O'Reilly
  159. Volume 6 are in error.
  160.  
  161. ----------------------------------------------------------------------
  162. Subject: 4.  How do I use a different visual than the default?
  163. ----------------------------------------------------------------------
  164.  
  165. This requires a more complicated answer than it should.  A window has
  166. three things that are visual specific -- the visual, colormap and
  167. border pixmap.  All widgets have their own Colormap and BorderPixmap
  168. resource; only shell widgets have Visual resources (another questions
  169. deals with why shells have a Visual).  The default value of these
  170. resources is CopyFromParent which does exactly what it says.  In the
  171. shell widget CopyFromParent gets evalulated as DefaultVisualOfScreen
  172. and DefaultColormapOfScreen.  When any one of the three resources is
  173. not properly set, a BadMatch error occurs when the window is
  174. created.  They are not properly set because each of the values depends
  175. on the visual being used.  
  176.  
  177. How to get this to work?  There are two parts to the answer.  The
  178. first is if you want an application to start with a particular visual
  179. and the second is if you want a particular shell within an application
  180. to start with a different visual.  The second is actually easier
  181. because the basic information you need is available.  The first is a
  182. little harder because you'll need to initialize much of the toolkit
  183. yourself in order to determine the needed information.
  184.  
  185. /*
  186.  * Some sample code to start up an application using something other
  187.  * than the default visual.
  188.  *
  189.  * To compile:
  190.  *    cc -g visual.c -o visual -lXaw -lXmu -lXt -lXext -lX11 -lm
  191.  *
  192.  * To run:
  193.  *    ./visual -geometry 300x300 -depth 24 -visual StaticColor -fg blue -bg yellow
  194.  *
  195.  * you need to move the mouse to get the particular visuals colormap
  196.  * to install.
  197.  */
  198.  
  199. #include <X11/Intrinsic.h>
  200. #include <X11/StringDefs.h>
  201. #include <X11/Shell.h>
  202.  
  203. typedef struct
  204. {
  205.     Visual    *visual;
  206.     int    depth;
  207. } OptionsRec;
  208.  
  209. OptionsRec    Options;
  210.  
  211. XtResource resources[] =
  212. {
  213.     {"visual", "Visual", XtRVisual, sizeof (Visual *),
  214.     XtOffsetOf (OptionsRec, visual), XtRImmediate, NULL},
  215.     {"depth", "Depth", XtRInt, sizeof (int),
  216.     XtOffsetOf (OptionsRec, depth), XtRImmediate, NULL},
  217. };
  218.  
  219. XrmOptionDescRec Desc[] =
  220. {
  221.     {"-visual", "*visual", XrmoptionSepArg, NULL},
  222.     {"-depth", "*depth", XrmoptionSepArg, NULL}
  223. };
  224.  
  225.  
  226.  
  227. int
  228. main (argc, argv)
  229.     int        argc;
  230.     char        **argv;
  231. {
  232.     XtAppContext    app;        /* the application context */
  233.     Widget        top;        /* toplevel widget */
  234.     Display        *dpy;        /* display */
  235.     char        **xargv;    /* saved argument vector */
  236.     int        xargc;        /* saved argument count */
  237.     Colormap    colormap;    /* created colormap */
  238.     XVisualInfo    vinfo;        /* template for find visual */
  239.     XVisualInfo    *vinfo_list;    /* returned list of visuals */
  240.     int        count;        /* number of matchs (only 1?) */
  241.     Arg        args[10];
  242.     Cardinal    cnt;
  243.     char        *name = "test";
  244.     char        *class = "Test";
  245.  
  246.     /*
  247.      * save the command line arguments
  248.      */
  249.  
  250.     xargc = argc;
  251.     xargv = (char **) XtMalloc (argc * sizeof (char *));
  252.     bcopy ((char *) argv, (char *) xargv, argc * sizeof (char *));
  253.  
  254.     /*
  255.      * The following creates a _dummy_ toplevel widget so we can
  256.      * retrieve the appropriate visual resource.
  257.      */
  258.     cnt = 0;
  259.     top = XtAppInitialize (&app, class, Desc, XtNumber (Desc), &argc, argv,
  260.                    (String *) NULL, args, cnt);
  261.     dpy = XtDisplay (top);
  262.     cnt = 0;
  263.     XtGetApplicationResources (top, &Options, resources,
  264.                    XtNumber (resources),
  265.                    args, cnt);
  266.     cnt = 0;
  267.     if (Options.visual && Options.visual != DefaultVisualOfScreen (XtScreen (top)))
  268.     {
  269.         XtSetArg (args[cnt], XtNvisual, Options.visual); ++cnt;
  270.         /*
  271.          * Now we create an appropriate colormap.  We could
  272.          * use a default colormap based on the class of the
  273.          * visual; we could examine some property on the
  274.          * rootwindow to find the right colormap; we could
  275.          * do all sorts of things...
  276.          */
  277.         colormap = XCreateColormap (dpy,
  278.                         RootWindowOfScreen (XtScreen (top)),
  279.                         Options.visual,
  280.                         AllocNone);
  281.         XtSetArg (args[cnt], XtNcolormap, colormap); ++cnt;
  282.  
  283.         /*
  284.          * Now find some information about the visual.
  285.          */
  286.         vinfo.visualid = XVisualIDFromVisual (Options.visual);
  287.         vinfo_list = XGetVisualInfo (dpy, VisualIDMask, &vinfo, &count);
  288.         if (vinfo_list && count > 0)
  289.         {
  290.             XtSetArg (args[cnt], XtNdepth, vinfo_list[0].depth);
  291.             ++cnt;
  292.             XFree ((XPointer) vinfo_list);
  293.         }
  294.     }
  295.     XtDestroyWidget (top);
  296.  
  297.  
  298.     /*
  299.      * Now create the real toplevel widget.
  300.      */
  301.     XtSetArg (args[cnt], XtNargv, xargv); ++cnt;
  302.     XtSetArg (args[cnt], XtNargc, xargc); ++cnt;
  303.     top = XtAppCreateShell ((char *) NULL, class,
  304.                 applicationShellWidgetClass,
  305.                 dpy, args, cnt);
  306.  
  307.     /*
  308.      * Display the application and loop handling all events.
  309.      */
  310.     XtRealizeWidget (top);
  311.     XtAppMainLoop (app);
  312.     return (0);
  313. }
  314.  
  315. ----------------------------------------------------------------------
  316. Subject: 5.  Which visual should an application use?
  317. ----------------------------------------------------------------------
  318.  
  319. This is a point that can be argued about but one opinion is there is
  320. no way for an application to know the appropriate visual -- it has to
  321. be specified by the user.  If you disagree with this then your
  322. application probably falls into the category of always using the
  323. default visual or it is hardware specific and expects some particular
  324. visual such as 24bit TrueColor with an OverlayPlane extension (or some
  325. such).
  326.  
  327. Why?  No application runs in isolation.  Depending on the way a server
  328. allocates resources I may not always want your application to run in
  329. TrueColor mode if it is going to mess up my other applications.  I may
  330. be very upset if it chooses to run in GreyScale instead of PsuedoColor
  331. or just monochrome.
  332.  
  333. As an example, on a low end color Sun server there are many different
  334. possible visuals: monochrome, 256 entry colormap, static gray, static
  335. color, and a 3/3/2 TrueColor.  The SGI Iris's offer all the above 
  336. plus 12 bit TrueColor, 24 bit TrueColor, an Overlay Plane.
  337.  
  338. ----------------------------------------------------------------------
  339. Subject: 6.  Why do only Shell widgets have a Visual?
  340. ----------------------------------------------------------------------
  341.  
  342. This is strictly by convention.  It makes it possible for an arbitrary
  343. widget to know that the visual it uses can be found by looking for the
  344. shell widget that is its ancestor and obtaining the visual of that
  345. shell.
  346.  
  347. A widget can have its own visual resource.  If it does, it must have
  348. its own realize method to use the visual when it calls
  349. XCreateWindow().  You should also make this a resource that can be
  350. obtained with XtGetValues() so other widgets can find it.  A
  351. reasonable value is probably XtNvisual.
  352.  
  353. ----------------------------------------------------------------------
  354. Subject: 7.  Which visual, depth and colormap do Shells inherit?
  355. ----------------------------------------------------------------------
  356.  
  357. The default value for these resources are set to CopyFromParent.  This
  358. is interpreted as the DefaultColormapOfScreen(), DefaultDepthOfScreen()
  359. and the default visual of the screen if the widget has no parent -- i.e.
  360. it is an applicationShellWidgetClass and the root of your widget tree.
  361.  
  362. If the parent of the widget is not null, then the shell copies
  363. colormap and depth from its parent and uses CopyFromParent as the
  364. visual.
  365.  
  366. ----------------------------------------------------------------------
  367. Subject: 8.  I've done all the above and I still get a BadMatch error.  Why?
  368. ----------------------------------------------------------------------
  369.  
  370. Some resource converters improperly cache references.  This was
  371. especially true of X11R3 and earlier versions of Motif.
  372.  
  373. ----------------------------------------------------------------------
  374. Subject: 9.  Why doesn't my widget get destroyed when I call XtDestroyWidget()?
  375. ----------------------------------------------------------------------
  376.  
  377. See section 2.8 of the Xt specification.
  378.  
  379. It eventually does get destroyed, just not immediately.  The
  380. Intrinsics destroy a widget in a two-phase process.  First it and all
  381. of its children have a flag set that indicate it is being destroyed.
  382. It is then put on a list of widgets to be destroyed.  This way any
  383. pending X events or further references to that widget can be cleaned
  384. up before the memory is actually freed.  The second phase is then
  385. performed after all callbacks, event handlers, and actions have
  386. completed, before checking for the next X event.  At this point the
  387. list is traversed and each widget's memory is actually free()'d, among
  388. other things.
  389.  
  390. As some further caveats/trivia, the widgets may be destroyed if the
  391. Intrinsics determine that they have no further references to the
  392. widgets on the list.  If so, then the phase 2 destruction occurs
  393. immediately.  Also, if nested event loops are used, widgets placed on
  394. the destroy list before entering the inner event loop are not
  395. destroyed until returning to the outer event loop.
  396.  
  397. ----------------------------------------------------------------------
  398. Subject: 10. How do I exit but still execute the DestroyCallbacks?
  399. ----------------------------------------------------------------------
  400.  
  401. The problem is if a simple and entirely reasonable approach to exiting
  402. an application is used, such as calling exit() directly, then a widget
  403. may not have a chance to clean up any external state -- such as open
  404. sockets, temporary files, allocated X resources, etc.  (this code for
  405. simplicity reasons assumes only a single toplevel widget):
  406.  
  407.  
  408.     Widget
  409.     ToplevelGet (gw)
  410.         Widget        gw;        /* widget to find toplevel */
  411.     {
  412.         Widget        top;
  413.  
  414.         for (top = gw; XtParent (top); top = XtParent (top))
  415.             /* empty */;
  416.         return (top);
  417.     }
  418.  
  419.     void
  420.     ExitCallback (gw, closure, call_data)
  421.         Widget        gw;        /* widget */
  422.         XtPointer    closure;    /* data the app specified */
  423.         XtPointer    call_data;    /* widget specific data */
  424.     {
  425.         Widget        toplevel;
  426.  
  427.         toplevel = ToplevelGet (gw);
  428.         XtUnmapWidget (toplevel);    /* make it disappear quickly */
  429.         XtDestroyWidget (toplevel);
  430.         exit (0);
  431.     }
  432.  
  433. One can see that the above code exit's immediately after destroying
  434. the toplevel widget.  The trouble is the phase 2 destruction may never
  435. occur.  
  436.  
  437. This works for most widgets and most applications but will not work
  438. for those widgets that have any external state.  You might think that
  439. since it works now it will always work but remember that part of the
  440. reason an object oriented approach is used is so one can be ignorant
  441. of the implementation details for each widget.  Which means that the
  442. widget may change and someday require that some external state is
  443. cleaned up by the Destroy callbacks.
  444.  
  445. One alternative is to modify ExitCallback() to set a global flag and
  446. then test for that flag in a private event loop.
  447. Or try the following code:
  448.  
  449.     #include <X11/Intrinsic.h>
  450.  
  451.     extern Widget ToplevelGet (
  452.     #if NeedFunctionPrototypes
  453.         Widget        gw
  454.     #endif
  455.     );
  456.  
  457.     extern Boolean ExitWorkProc (
  458.     #if NeedFunctionPrototypes
  459.         XtPointer    closure
  460.     #endif
  461.     );
  462.  
  463.     extern void ExitCallback (
  464.     #if NeedFunctionPrototypes
  465.         Widget        gw,
  466.         XtPointer    closure,
  467.         XtPointer    call_data
  468.     #endif
  469.     );
  470.  
  471.     Widget
  472.     ToplevelGet (gw)
  473.     Widget        gw;        /* widget to find toplevel */
  474.     {
  475.         Widget        top;
  476.  
  477.         for (top = gw; XtParent (top); top = XtParent (top))
  478.             /* empty */;
  479.         return (top);
  480.     }
  481.  
  482.  
  483.     void
  484.     ExitCallback (gw, closure, call_data)
  485.     Widget        gw;        /* widget */
  486.     XtPointer    closure;    /* data the app specified */
  487.     XtPointer    call_data;    /* widget specific data */
  488.     {
  489.         Widget        toplevel;
  490.  
  491.         toplevel = ToplevelGet (gw);
  492.         XtUnmapWidget (toplevel);    /* make it disappear quickly */
  493.         XtDestroyWidget (toplevel);
  494.         XtAppAddWorkProc (XtWidgetToApplicationContext (gw),
  495.                   ExitWorkProc, (XtPointer) NULL);
  496.     }
  497.  
  498.     Boolean
  499.     ExitWorkProc (closure)
  500.         XtPointer    closure;
  501.     {
  502.         exit (0);
  503.         /*NOTREACHED*/
  504.     }
  505.  
  506.  
  507. ExitCallback() adds a work procedure that will get called when the
  508. application is next idle -- which happens after all the events are
  509. processed and the destroy callbacks are executed.
  510.  
  511. ----------------------------------------------------------------------
  512. Subject: 11. How do I resize a Shell widget?
  513. ----------------------------------------------------------------------
  514.  
  515. After it is realized, one doesn't resize a Shell widget.  The proper
  516. thing is to resize the currently managed child of the Shell widget
  517. using XtSetValues().  The geometry change is then propagated to the
  518. Shell which asks the window manager which may or may not allow the
  519. request.  However, the Shell must have the resource
  520. XtNallowShellResize set to True otherwise it will not even ask the
  521. window manager to grant the request and the Shell will not resize.
  522.  
  523. To change the position of a Shell, use XtSetValues() on the Shell, not
  524. the child, and within the limits of the window manager it should be granted.
  525.  
  526. ----------------------------------------------------------------------
  527. Subject: 12. Why can't XtAppAddInput() handle files?
  528. ----------------------------------------------------------------------
  529.  
  530. It does, however Unix semantics for when I/O is ready for a file does
  531. not fit most peoples' intuitive model.  In Unix terms a file
  532. descriptor is ready for reading whenever the read() call would not
  533. block, ignoring the setting of optional flags that indicate not to
  534. block.  This works as expected for terminals, sockets and pipes.  For
  535. a file the read() will always return but the return indicates an EOF
  536. -- i.e. no more data.  The result is the code in the Intrinsics always
  537. calls the input handler because it always thinks something is about to
  538. be read.  The culprit is the select() system call or on SYSV based
  539. OS's it is the poll() system call.
  540.  
  541. How to get around this on a Unix system?  The best approach is to use
  542. another process to check for available input on the file.  Use a pipe
  543. to connect the application with this other process and pass the file
  544. descriptor from the pipe to XtAppAddInput().  A suitable program on
  545. BSD systems is "tail -f filename".
  546.  
  547. It's rumored that select() on some systems is not _completely_
  548. reliable.  In particular:
  549.  
  550.     - IBM AIX 3.1: this is one where it would work for a while
  551.       (several thousand times) and then stop until some other
  552.       event woke it up. This seemed to be the result of a race
  553.       condition in the Kernel.  IBM claims to have a fix for this.
  554.  
  555.     - Pyramid, doesn't work at all.
  556.  
  557.     - Ultrix (and possibly others where pipes are implemented as
  558.       sockets), wasn't completely broken, but although the writing
  559.       side wrote in 512 byte blocks the reading side received it
  560.       all broken up as if it was being put into the pipe a byte at
  561.       a time.  You can waste a lot of time by reading small blocks
  562.       (get raound it by detecting the situation and having
  563.       select() ignore the pipe for 10 mseconds - by then it had
  564.       been given the whole block).
  565.  
  566.  
  567. Note that all the above descriptions used Unix terminology such as
  568. read(), file descriptor, pipes, etc.  This is an OS dependent area and
  569. may not be identical on all systems.  However the Intrinsic designers
  570. felt it was a common enough operation that it should be included with
  571. part of the toolkit.  Why they didn't also deal with signals at this
  572. point I don't know.
  573.  
  574. ----------------------------------------------------------------------
  575. Subject: 13. What good books and magazines are there on Xt?
  576. ----------------------------------------------------------------------
  577.  
  578. I have a favorite that is the definitive reference.  To my perspective
  579. it offers a reasonable introduction but also goes into the full
  580. details of the Intrinsics.  When I started using it I was already
  581. familiar with Xt and the concepts behind it, so newcomers may or may
  582. not find it useful.  I've always found it accurate and complete, which
  583. means its a 1000 pages.
  584.  
  585. Asente, Paul J., and Swick, Ralph R., "X Window System Toolkit, The
  586.     Complete Programmer's Guide and Specification", Digital Press,
  587.     1990, ISBN 1-55558-051-3, order number EY-E757E-DP; and by
  588.     Prentice-Hall, ISBN 0-13-972191-6. Also available through DEC
  589.     Direct at 1-800-DIGITAL.
  590.  
  591. The other book I commonly recomend to novices is:
  592.  
  593. Young, Doug. "The X Window System: Applications and Programming with
  594.     Xt (Motif Version)," Prentice Hall, 1989 (ISBN 0-13-497074-8).
  595.     (ISBN 0-13-972167-3)
  596.  
  597. And of course O'Reilly has an entire series of manuals on X and Xt.
  598. O'Reilly ordering is 800-998-9938.  In particular, Volume 5 is an Xt
  599. reference done in manual page style.  The 3rd edition is extensively
  600. overhauled and goes far beyond the MIT manual pages.  I'm finding it
  601. very useful.  In particular, the permutted index and references to
  602. other manual pages help a great deal in chasing down related
  603. information.
  604.  
  605. I read two periodicals, "The X Resource" and the "The X Journal".
  606. These are the only two dealing specifically with X.  "The X Resource"
  607. is published quarterly, by O'Reilly, with one of the issues being the
  608. MIT X Consortium Technical Conference Proceedings.  There is no
  609. advertising.  I've found it informative with pretty good depth.  For
  610. orders, call 1-800-998-9938, or email cathyr@ora.com.  For editorial
  611. matters, email adrian@ora.com.  Table of contents are posted at
  612. math.utah.edu in ~ftp/pub/tex/bib in TeX form and on ftp.uu.net in
  613. ~ftp/published/oreilly/xresource in ASCII form.
  614.  
  615.  
  616. "The X Journal" is a bimonthly trade rag with lots of advertising.
  617. The articles are informative and oriented toward a less technical
  618. audience.  I read it more to see what's going on then with an
  619. expectation of learning a great deal (but remember, I represent a
  620. fairly small percentage of people).  Also, they have a pretty good
  621. collection of people on the advisory board and as columnists.  Call
  622. (908) 563-9033.
  623.  
  624. ----------------------------------------------------------------------
  625. Subject: 14. What Widgets are available?
  626. ----------------------------------------------------------------------
  627.  
  628. There are three popular widget sets:
  629.  
  630. Athena    - The set provided with X11.  This is sufficient for most
  631.       purposes but is on the ugly side.  Recently, a 3d look is
  632.       available for ftp on ftp.x.org:/contrib/Xaw3d.tar.Z.
  633. Motif    - From OSF available for a license fee and commonly shipped on
  634.       many workstation vendors platforms (almost everyone but
  635.       Sun).  It looks good and works well but personally I think
  636.       it is poorly implemented.
  637. OLIT    - The Open Look Intrinsics Toolkit is a set of widgets
  638.       implementing Sun's Open Look specification.  Developed by
  639.       AT&T.  I've never used it so can't comment on its quality.
  640.       I've heard rumours that it is a pain to actually get.
  641.  
  642. In addition the following collection of widgets are also available:
  643.  
  644. Xtra    - a library of widgets for sale from Graphical Software
  645.       Technology (310-328-9338).  It includes bar graph, stacked
  646.       bar graph, line graph, pie chart, xy plot, hypertext, help,
  647.       spreadsheet, and data entry form widgets.  I've never seen
  648.       them so I can't comment.
  649. FWF    - The Free Widget Foundation is attempting to collect a set of
  650.       freely available widgets.  Included are a Pixmap editor,
  651.       FileDialog, and a few others.  The current set of widgets
  652.       can be obtained via anonymous ftp from the machine
  653.       a.cs.uiuc.edu (128.174.252.1) in the file pub/fwf.shar.Z.
  654. Xcu    - The Cornell University widgets from Gene Dykes.  One of the
  655.       early widget sets released.  Provides a nice appearance for
  656.       buttons and has a mini command language.  Probably not so
  657.       widely used.
  658. Xs    - The Sony widget set.  This was around during R3 days but
  659.       seemed to disappear.  It looked like it had promise.
  660. Xw    - The HP widgets.  The precursor to Motif.  Originally written
  661.       for R3 there exists diffs to get it to work under R4 & R5.
  662.       Again, a pretty good widget set but has more or less died.
  663.       The precursor to this was the Xray toolkit which was
  664.       originally implemented for X10R4 and apparently provided
  665.       much experience for the designers of Xt.
  666. Xo    - A widget set I'm working on.  It's still primitive but you
  667.       can give it a try in archive.cis.ohio-state.edu:pub/Xo/*
  668.  
  669. The following specialized widgets are also available:
  670.  
  671. Tbl    - Implements a tabular layout of widgets.  Supports Motif
  672.       widgets as children.  Part of Wcl.
  673. Plots    - The Athena Plotting widgets (not the Athena widgets).
  674.       Contact gnb@bby.oz.au or joe@Athena.MIT.EDU.
  675.  
  676. ----------------------------------------------------------------------
  677. Subject: 15. What alternatives to the Intrinsics are there?
  678. ----------------------------------------------------------------------
  679.  
  680.     __________________________________________
  681.     Name        Language    Vendor
  682.     __________________________________________
  683.     Xview        C        Sun
  684.     OI        C++        ParcPlace
  685.     Interviews    C++        Stanford
  686.     Tcl/tk        C        sprite.berkeley.edu
  687.     __________________________________________
  688.  
  689.  
  690. However much I like C and admire the skill in both designing and
  691. implementing the Intrinsics, hopefully some alternative will develop
  692. in the next 3-5 years that uses an object oriented language.  Keep
  693. your eyes open and expect some change about the same time a language
  694. other than C _starts_ gaining acceptance.
  695.  
  696. ----------------------------------------------------------------------
  697. Subject: 16. How do I pass a float value to XtSetValues?
  698. ----------------------------------------------------------------------
  699.  
  700. First, what is going wrong is the structure for an Arg is (essentially)
  701.     typdef struct
  702.     {    
  703.         String    name;
  704.         long    value;
  705.     } Arg;
  706.  
  707. and the code:
  708.     Arg    arg;
  709.  
  710.     XtSetArg (arg, "name", 3.2)
  711.  
  712. expands to
  713.     Arg    arg;
  714.  
  715.     arg.name = "name";
  716.     arg.value = 3.2;
  717.  
  718. you can see that with normal C type conversions, the arg.value
  719. gets the integer "3" instead of the floating point value "3.2".  When
  720. the value is copied into the widget resource, the bit pattern is
  721. wildly different than that required for a floating point value.  So,
  722. how to get around this?
  723.  
  724. The following macro is from the Athena widgets document and I am now
  725. recomending it over the previous suggestions.
  726.  
  727. #define XtSetFloatArg(arg, n, d) \
  728.     if (sizeof(float) > sizeof(XtArgVal)) { \
  729.         XtSetArg(arg, n, &(d)); \
  730.     } else { \
  731.         XtArgVal *ld = (XtArgVal *)&(d); \
  732.         XtSetArg(arg, n, *ld); \
  733.     }
  734.  
  735.  
  736. ----------------------------------------------------------------------
  737. Subject: 17. How do I write a resource converter?
  738. ----------------------------------------------------------------------
  739.  
  740. Courtesy of Rich Thomson (rthomson@dsd.es.com):
  741.  
  742. The following discussion of resource converters assumes R4 (or R5)
  743. Intrinsics.  Resource converters changed between R3 and R4 to allow
  744. for destructors and caching of converted values.
  745.  
  746. There are several main types of resource converters:
  747.  
  748.     string to data type
  749.     data type to string
  750.     data type to data type
  751.  
  752. i) string to data type
  753.     Usually a string to data type converter has a fixed set of strings
  754.     that will be converted to data type values.  This is most often
  755.     used to map enumerated names to enumerated values:
  756.  
  757.     Name        Value
  758.     "True"        1
  759.     "False"        0
  760.  
  761.     In this case, the string to data type converter needs to compare
  762.     the resource value to the list of fixed strings.  This is most
  763.     readily accomplished by the use of the "quark" mechanism of the
  764.     resource manager.  The resource value is turned into a quark,
  765.     which is a unique representation of the string that fits into a
  766.     single word.  Then the resource quark is compared against the
  767.     quarks for the fixed strings representing the enumerated values.
  768.  
  769.     If there are many enumerated strings in the converter (or many
  770.     converters, each with a small number of enumeration strings), then
  771.     a global initialization routine might be used to turn all the
  772.     resource strings into quarks.  That way, the first time one of
  773.     these converters is used, the strings will be turned into quarks
  774.     and held in static variables for use in the next invocation of one
  775.     of the converters.
  776.  
  777. ii) data type to string
  778.     This type of converter is slightly easier than the string to data
  779.     type converters since the use of quarks isn't necessary.  Instead,
  780.     the data type value is simply converted to a string value,
  781.     probably by the use of sprintf.
  782.  
  783.     Data type to string converters are useful for applications that
  784.     wish to convert an internal data type value into a string so that
  785.     they can write out a valid resource specification to a file.  This
  786.     mechanism can be used to provide a "snapshot" of application state
  787.     into a file.  This snapshot can be used to restore the program to
  788.     a known state via the usual X resource database mechanisms.
  789.  
  790.     If you are taking the trouble to write a string to data type
  791.     converter, it isn't much extra effort to write the data type to
  792.     string converter.  Writing both at the same time helps to ensure
  793.     that they are consistent.
  794.  
  795. iii) data type to data type
  796.     This type of converter is used to convert an existing data type
  797.     value to another data type.  For instance, an X pixel value can be
  798.     converted to an RGB data type that contains separate fields for
  799.     red, green and blue.
  800.  
  801.  
  802. The type signature for a resource converter is as follows:
  803.  
  804. typedef Boolean (*XtTypeConverter)(Display *, XrmValuePtr, Cardinal *,
  805.     XrmValuePtr, XrmValuePtr, XtPointer *);
  806.     Display *dpy;
  807.     XrmValuePtr args;
  808.     Cardinal *num_args;
  809.     XrmValuePtr fromVal;
  810.     XrmValuePtr toVal;
  811.     XtPointer *converter_data;
  812.  
  813. When the converter is invoked, the "fromVal" argument points to the source
  814. X resource manager value and the "toVal" argument points to the
  815. destination X resource manager value.  The "converter_data" argument
  816. is an opaque pointer to some converter-specific data that is specified
  817. when the converter is registered.  The "args" and "num_args" arguments
  818. allow extra information to be passed to the converter when it is
  819. invoked.  For instance, the Pixel to RGB structure converter discussed
  820. above would need colormap and visual arguments in which to lookup the
  821. Pixel to obtain the RGB values corresponding to that pixel.
  822.  
  823. Care must be taken with the "toVal" argument.  An XrmValue has the
  824. following type definition and specifies a size and location for a
  825. converted value:
  826.  
  827. typedef struct {
  828.     unsigned int    size;
  829.     caddr_t         addr;
  830. } XrmValue, *XrmValuePtr;
  831.  
  832. When the converter is invoked, the address may point to a location of
  833. the given size for the converted value or the location can be NULL.
  834. In the former case, the converter should ensure that the size of the
  835. destination area is large enough to handle the converted value.  If
  836. the destination area is not large enough, then the converter should
  837. set the size to the amount of space needed and return False.  The
  838. caller can then ensure that enough space is allocated and reinvoke the
  839. converter.  If the size is large enough, then the converter can simply
  840. copy the converted value into the space given and return True.
  841.  
  842. If the location is NULL, then the converter can assign the location to
  843. the address of a static variable containing the converted value and
  844. return True.
  845.  
  846. When writing a group of converters, this code is often repeated and it
  847. becomes convenient to define a macro:
  848.  
  849.     #define DONE(var, type) \
  850.       if (toVal->addr) \
  851.     { \
  852.       if (toVal->size < sizeof(type)) \
  853.         { \
  854.           toVal->size = sizeof(type); \
  855.           return False; \
  856.         } \
  857.       else \
  858.         *((type *) toVal->addr) = var; \
  859.     } \
  860.       else \
  861.     toVal->addr = (caddr_t) &var; \
  862.       toVal->size = sizeof(type); \
  863.       return True;
  864.  
  865.     #define DONESTR(str) \
  866.       if (toVal->addr && toVal->size < sizeof(String)) \
  867.     { \
  868.       toVal->size = sizeof(String); \
  869.       return False; \
  870.     } \
  871.       else \
  872.     toVal->addr = (caddr_t) str; \
  873.       toVal->size = sizeof(String); \
  874.       return True;
  875.  
  876. Inside the converter, it is a good idea to perform a little safety
  877. checking on the "num_args" and "args" arguments to ensure that your
  878. converter is being called properly.
  879.  
  880. Once you have written your converter, you need to register it with the
  881. Intrinsics.  The Intrinsics invokes resource converters when creating
  882. widgets and fetching their resource values from the resource database.
  883.  
  884. To register a converter with a single application context, use
  885. XtAppSetTypeConverter:
  886.  
  887. void XtAppSetTypeConverter(context, from, to, converter, args, num_args,
  888.     cache, destructor)
  889.     XtAppContext context;
  890.     String from;
  891.     String to;
  892.     XtTypeConverter converter;
  893.     XtConvertArgList args;
  894.     Cardinal num_args;
  895.     XtCacheType cache;
  896.     XtDestructor destructor;
  897.  
  898. To register a converter with all application contexts, use
  899. XtSetTypeConverter:
  900.  
  901. void XtSetTypeConverter(from, to, converter, args, num_args,
  902.     cache, destructor)
  903.     String from;
  904.     String to;
  905.     XtTypeConverter converter;
  906.     XtConvertArgList args;
  907.     Cardinal num_args;
  908.     XtCacheType cache;
  909.     XtDestructor destructor;
  910.  
  911. In the R3 Intrinsics, there were the routines XtAppAddConverter and
  912. XtAddConverter; these have been superseded by XtAppSetTypeConverter
  913. and XtSetTypeConverter.  Whenever possible, the newer routines should be
  914. used.
  915.  
  916. When a converter is registered with the Intrinsics, a "cache" argument
  917. specifies how converted resource values are to be cached:
  918.  
  919.     XtCacheNone        Don't cache any converted values
  920.     XtCacheAll        Cache all converted values
  921.     XtCacheByDisplay    Cache converted values on a per display basis
  922.  
  923. Caching converted values that require a round-trip to the server is a
  924. good idea (for instance string to Pixel conversions).
  925.  
  926. The "destructor" argument is a routine that is invoked then the
  927. resource is destroyed, either because its cached reference count has
  928. been decremented to zero or because the widget owning the value is
  929. being destroyed.  XtDestructor has the following type definition:
  930.  
  931. typedef void (*XtDestructor)(XtAppContext, XrmValuePtr, XtPointer,
  932.     XrmValuePtr, Cardinal *);
  933.     XtAppContext context;
  934.     XrmValuePtr to;
  935.     XtPointer converter_data;
  936.     XrmValuePtr args;
  937.     Cardinal *num_args;
  938.  
  939. The destructor is invoked to free any auxiliary storage associated
  940. with the "to" argument, but does not actually free the storage pointed
  941. to by the "to" argument itself (to->addr).  The destructor is passed
  942. the extra arguments that were passed to the converter when the
  943. conversion was performed (for instance, colormap and visual arguments
  944. for the string to Pixel converter since the destructor would need to
  945. free the allocated Pixel from the colormap) as well as the private
  946. data passed in when the converter was registered.
  947.  
  948. Sample converter code can be found in the following files in the MIT
  949. R5 distribution:
  950.  
  951.     mit/lib/Xt/Converters.c
  952.     contrib/lib/PEXt/Converters.c
  953.     contrib/lib/PEXt/Converters.h
  954.  
  955. ----------------------------------------------------------------------
  956. Subject: 18. How do I open multiple displays?
  957. ----------------------------------------------------------------------
  958.  
  959. See "Multi-user Application Software Using Xt", The X Resource, Issue 3,
  960. (Summer 1992) by Oliver Jones for a complete coverage of the issues
  961. involved.  Most of this answer is based on that article.  In a
  962. nutshell, one uses XtOpenDisplay() to add each display to a _single_
  963. application context and then XtCloseDisplay() to shutdown each display
  964. and remove it from the application context.
  965.  
  966. The real problems occur when trying to close down a display.  This can
  967. happen 3 ways:
  968.     1. User selects a "quit" button on one of the displays,
  969.     2. User has window manager send a WM_DELETE_WINDOW message,
  970.     3. Server disconnect -- possibly from a KillClient message,
  971.        server shutdown/crash, or network failure.
  972.  
  973. I'll assume you can deal gracefully with 1 & 2 since it is _merely_ a
  974. problem of translating a Widget to a display and removing that
  975. display.  If not, then read the Oliver Jones article.
  976.  
  977. The third one is difficult to handle.  The following is based on the
  978. Oliver Jones article and I include it here because it is a difficult
  979. problem.
  980.  
  981. The difficulty arises because the Xlib design presumed that an I/O
  982. error is always unrecoverable and so fatal.  This is essentially true
  983. for a single display X based application, but not true for a
  984. multiple display program or an application that does things other than
  985. display information on an X server.  When an X I/O error occurs the
  986. I/O error handler is called and _if_ it returns then an exit()
  987. happens.  The only way around this is to use setjmp/longjmp to avoid
  988. returning to the I/O error handler.  The following code fragment
  989. demonstrates this:
  990.  
  991. #include <setjmp.h>
  992. jmp_buf XIOrecover;
  993.  
  994. void
  995. XIOHandler (dpy)
  996.     Display        *dpy;
  997. {
  998.     destroyDisplay (dpy);
  999.     longjmp (XIOrecover, 1);
  1000. }
  1001.  
  1002. main ()
  1003. {
  1004.     ...
  1005.     if (setjmp (XIOrecover) == 0)
  1006.         XSetIOErrorHandler (XIOHandler);
  1007.     XtAppMainLoop (app_context);
  1008. }
  1009.  
  1010. The destroyDisplay() is something that given a Display pointer can go
  1011. back to the application specific data and perform any necessary
  1012. cleanup.  It should also call XtCloseDisplay().
  1013.  
  1014. For those of you unfamiliar with setjmp/longjmp, when setjmp() is
  1015. first called it returns a 0 and save's enough information in the
  1016. jmp_buf that a latter execution of longjmp() can return the program to
  1017. the same state as if the setjmp() was just executed.  The return value
  1018. of this second setjmp() is the value of the second argument to
  1019. longjmp().  There are several caveats about using these but for this
  1020. purpose it is adequate.
  1021.  
  1022. Some other problems you might run into are resource converters that
  1023. improperly cache resources.  The most likely symptoms are Xlib errors
  1024. such as BadColor, BadAtom, or BadFont.  There may be problems with the
  1025. total number of displays you can open since typically only a limited
  1026. number of file descriptors are available with 32 being a typical
  1027. value.  You may also run into authorization problems when trying to
  1028. connect to a display.
  1029.  
  1030. There was much discussion in comp.windows.x about this topic in
  1031. November of 91.  Robert Scheifler posted an article which basically
  1032. said this is the way it will be and Xlib will not change.
  1033.  
  1034. ----------------------------------------------------------------------
  1035. Subject: 19. What changed from R3 to R4 to R5?
  1036. ----------------------------------------------------------------------
  1037.  
  1038. This addresses only changes in the Intrinsics.  First, the general
  1039. changes for each release are described.  Then a, certainly incomplete,
  1040. list of new functions added and others that are now deprecated are
  1041. listed.  Brevity is a primary goal.
  1042.  
  1043. Much of the following information is retrieved from Chapter 13 of the MIT
  1044. Xt Intrinsics Manual and from O'Reilly Volume 5, 3rd edition.
  1045.  
  1046. From R3 to R4
  1047. - Addition of gadgets (windowless widgets)
  1048. - New resource type converter interface to handle cacheing and
  1049.   additional  data.
  1050. - Variable argument list interface.
  1051. - #define XtSpecificationRelease 4  (added with this release)
  1052. - WMShellPart, TopLevelShellPart & TransientShellPart changed
  1053.   incompatibly.
  1054. - core.initialize, core.set_values added ArgList and count parameters
  1055. - event handlers had continue_to_dispatch parameter added
  1056. - core.set_values_almost specification changed.
  1057. - core.compress_exposure changed to an enumerated data type from Boolean
  1058. - core.class_inited changed to enumerated data type from Boolean
  1059. - constraint.get_values_hook added to extension record
  1060. - core.initialize_hook obsolete as info is passed to core.initialize
  1061. - shell.root_geometry_manager added to extension record
  1062. - core.set_values_hook obsolete as info is passed to core.set_values
  1063. - Calling XtQueryGeometry() must store complete geometry.
  1064. - Added UnrealizeCallback.
  1065. - XtTranslateCoords() actually works under R4.
  1066.  
  1067. From R4 to R5:
  1068. - Psuedo resource baseTranslation added.
  1069. - Searching for app-default, and other files, made more flexible
  1070. - customization resource added.
  1071. - Per-screen resource database.
  1072. - Support permanently allocated strings.
  1073. - Permanetly allocated strings required for several class fields.
  1074. - The args argument to XtAppInitialize, XtVaAppInitialize,
  1075.   XtOpenDisplay, XtDisplayInitialize, and XtInitialize were changed
  1076.   from Cardinal* to int*
  1077. - Many performance improvements (this is summarized from the article
  1078.   "Xt Performance Improvements in Release 5" by Gabe Beged-Dov in "The
  1079.   X Resource", Issue 3):
  1080.     - XrmStringToQuark() augmented with XrmPermStringToQuark() to
  1081.       avoid string copies.  Several fields in the class record are
  1082.       indicated as needing permanent strings.
  1083.     - Using an array of Strings for resources
  1084.     - Callback lists redesigned to use less memory
  1085.     - Translation manager redesigned and rewritten so it takes
  1086.       less memory, translation tables merges are faster, cache of
  1087.       action bindings
  1088.     - Keycode to Keysyms are cached.
  1089.     - Better sharing of GC's with modifiable fields
  1090.     - Window to Widget translation uses less space and faster
  1091.     - Does not malloc space for widget name since quark is available
  1092.     - Widget space is allocated to include the constraints
  1093.     - Over several example programs, about a 26% reduction in
  1094.       memory usage.
  1095.  
  1096. Functions new with R5:
  1097. ----------------------
  1098. XtAllocateGC()        - sharable GC with modifiable fields  
  1099. XtGetActionList()    - get the action table of a class
  1100. XtScreenDatabase()    - return resource database for a screen
  1101. XtSetLanguageProc()    - register language procedure called to set locale
  1102.  
  1103.  
  1104. Functions new with R4:
  1105. ----------------------
  1106. XtAppAddActionHook()    - procedure to call before _every_ action.
  1107. XtAppInitialize()    - lots of initialization work.
  1108. XtAppReleaseCacheRefs()    - decrement cache reference count for converter
  1109. XtAppSetFallbackResources() - specify default resources
  1110. XtAppSetTypeConverter()    - register a new style converter
  1111. XtCallCallbackList()    - directly execute a callback list
  1112. XtCallConverter    ()    - invoke a new style converter
  1113. XtCallbackReleaseCacheRef() - release a cached resource value
  1114. XtCallbackReleaseCacheRefList() - release a list of cached resource values
  1115. XtConvertAndStore()    - find and call a resource converter
  1116. XtDirectConvert()    - Invoke old-style converter
  1117. XtDisplayOfObject()    - Return the display
  1118. XtDisplayStringConversionWarning() - issue a warning about conversion
  1119. XtFindFile()        - Find a file
  1120. XtGetActionKeysym()    - Retrieve keysym & modifies for this action
  1121. XtGetApplicationNameAndClass() - return name and class
  1122. XtGetConstraintResourceList() - get constraints for a widget
  1123. XtGetKeysymTable()    - return keycode-to-keysym mapping table
  1124. XtGetMultiClickTime()    - read the multi-click time
  1125. XtGetSelectionRequest()    - retrieve the SelectionRequest event
  1126. XtGetSelectionValueIncremental() - obtain the selection value incrementally
  1127. XtGetSelectionValuesIncremental() - obtain the selection value incrementally
  1128. XtInitializeWidgetClass() - initialize a widget class manually
  1129. XtInsertEventHanlder()    - register event handler before/after others
  1130. XtInsertRawEventHandler() - register event handler without modify input mask
  1131. XtIsObject()        - test if subclass of Object
  1132. XtIsRectObj()        - test if subclass of RectObj
  1133. XtKeysymToKeyCodeList()    - return list of keycodes
  1134. XtLastTimestampProcessed() - retrieve most recent event time
  1135. XtMenuPopdown        - Action for popping down a widget
  1136. XtMenuPopup        - Action for popping up a widget
  1137. XtOffsetOf        - macro for structure offsets
  1138. XtOwnSelectionIncremental() - make selection data availabe incrementally
  1139. XtPoupSpringLoaded()    - map a spring-loaded popup
  1140. XtRegisterGrabAction()    - indicate action procedure needs a passive grab
  1141. XtRemoveActiohHook()    - remove function called after every action
  1142. XtResolvePathname()    - find a file
  1143. XtScreenOfObject()    - return screen of object.
  1144. XtSetMultiClickTime()    - set the multi-click time
  1145. XtSetWMColormapWindows() - set WM_COLORMAP_WINDOWS for custom colormaps
  1146. XtUngrabButton()    - cancel a passive button grab
  1147. XtUngrabKey()        - cancel a passive key grab
  1148. XtUngrabKeybard()    - release an active keyboard grab
  1149. XtUngrabPointer()    - release an active pointer grab
  1150. XtVa*()            - varags interfaces to a bunch of functions
  1151. XtWindowOfObject()    - return Window of nearest widget ancestor
  1152.  
  1153.  
  1154. Deprecated        Replacement            When
  1155. ----------------------------------------------------------------------
  1156. XtAddActions()        XtAppAddActions()        R3
  1157. XtAddConverter()    XtAppAddConverter()        R3
  1158. XtAddInput()        XtAppAddInput ()        R3
  1159. XtAddTimeout()        XtAppAddTimeout()        R3
  1160. XtAddWorkProc()        XtAppAddWorkProc()        R3
  1161. XtConvert()        XtConvertAndStore()        R4
  1162. XtCreateApplicationShell XtAppCreateShell()        R3
  1163. XtDestroyGC()        XtReleaseGC()            R3
  1164. XtError()        XtAppError()            R3
  1165. XtGetErrorDatabase()    XtAppGetErrorDatabase        R3
  1166. XtGetErrorDatabaseText() XtAppGetErrorDatabaseText    R3
  1167. XtGetSelectionTimeout()    XtAppGetSelectionTimeout    R3
  1168. XtInitialize()        XtAppInitialize()        R3
  1169. XtMainLoop()        XtAppMainLoop()            R3
  1170. MenuPopdown(action)    XtMenuPopdown(action)        R4
  1171. MenuPopup(action)    XtMenuPopup(action)        R4
  1172. XtNextEvent()        XtAppNextEvent()        R3
  1173. XtPeekEvent()        XtAppPeekEvent()        R3
  1174. XtPending()        XtAppPending()            R3
  1175. XtSetErrorHandler()    XtAppSetErrorHandler()        R3
  1176. XtSetErrorMsgHandler    XtAppSetErrorMsgHandler()    R3
  1177. XtSetSelectionTimeout()    XtAppSetSelectionTimeout()    R3
  1178. XtSetWarningHandler()    XtAppSetWarningHandler()    R3
  1179. XtSetWarningMsgHandler() XtAppSetWarningMsgHandler()    R3
  1180. XtWarning()        XtAppWarning()            R3
  1181. XtWarningMsg()        XtAppWarningMsg()        R3
  1182.  
  1183. ----------------------------------------------------------------------
  1184. Subject: 20. Where are the resources loaded from?
  1185. ----------------------------------------------------------------------
  1186.  
  1187. The resources of a widget are filled in from the following places
  1188. (from highest priority to lowest priority):
  1189.  
  1190.     1. Args passed at creation time.
  1191.     2. Command line arguments.
  1192.     3. User's per host defaults file
  1193.     4. User's defaults file.
  1194.     5. User's per application default file.
  1195.     6. System wide per application default file.
  1196.  
  1197. Note that 2-6 are read only once on application startup.  The result
  1198. of steps 3-6 is a single resource database used for further queries.
  1199.  
  1200. The per host defaults file contains customizations for all
  1201. applications executing on a specific computer.  This file is either
  1202. specified with the XENVIRONMENT environment variable or if that is not
  1203. set then the file $HOME/.Xdefaults-<host> is used.
  1204.  
  1205. The user defaults file is either obtained from the RESOURCE_MANAGER
  1206. property on the root window of the display or if that is not set then
  1207. the file $HOME/.Xdefaults is used.  Typically, the program "xrdb" is
  1208. used to set the RESOURCE_MANAGER property.  Please note that this
  1209. should be kept relatively small as each client that connects to the
  1210. display must transfer the property.  A size of around 1-3KByte is
  1211. reasonable.  Some toolkits may track changes to the RESOURCE_MANAGER
  1212. but most do not.
  1213.  
  1214. A user may have many per application default files containing
  1215. customizations specific to each application.  The intrinsics are quite
  1216. flexible on how this file is found.  Read the next part that describes
  1217. the various environment variables and how they effect where this file
  1218. is found.
  1219.  
  1220. The system wide per application default files are typically found in
  1221. /usr/lib/X11/app-defaults.  If such a file is not found then the
  1222. fallback resources are used.  The intrinsics are quite flexible on how
  1223. this file is found.  Read the next part that describes the various
  1224. environment variables and how they effect where this file is found.
  1225.  
  1226. [Thanks to Oliver Jones (oj@pictel.com) for the following, 6/92]
  1227.  
  1228. You can use several environment variables to control how resources are
  1229. loaded for your Xt-based programs -- XFILESEARCHPATH,
  1230. XUSERFILESEARCHPATH, and XAPPLRESDIR.  These environment variables
  1231. control where Xt looks for application-defaults files as an
  1232. application is initializing.  Xt loads at most one app-defaults file
  1233. from the path defined in XFILESEARCHPATH and another from the path
  1234. defined in XUSERFILESEARCHPATH.
  1235.  
  1236. Set XFILESEARCHPATH if software is installed on your system in such a
  1237. way that app-defaults files appear in several different directory
  1238. hierarchies.  Suppose, for example, that you are running Sun's Open
  1239. Windows, and you also have some R4 X applications installed in
  1240. /usr/lib/X11/app-defaults. You could set a value like this for
  1241. XFILESEARCHPATH, and it would cause Xt to look up app-defaults files
  1242. in both /usr/lib/X11 and /usr/openwin/lib (or wherever your
  1243. OPENWINHOME is located):
  1244.  
  1245.     setenv XFILESEARCHPATH /usr/lib/X11/%T/%N:$OPENWINHOME/lib/%T/%N
  1246.  
  1247. The value of this environment variable is a colon-separated list of
  1248. pathnames.  The pathnames contain replacement characters as follows
  1249. (see XtResolvePathname()):
  1250.  
  1251.     %N    The value of the filename parameter, or the
  1252.         application's class name.
  1253.     %T    The value of the file "type".  In this case, the
  1254.         literal string "app-defaults"
  1255.     %C    customization resource (R5 only)
  1256.     %S    Suffix.  None for app-defaults.
  1257.     %L    Language, locale, and codeset (e.g. "ja_JP.EUC")
  1258.     %l    Language part of %L  (e.g. "ja")
  1259.     %t    The territory part of the display's language string
  1260.     %c    The codeset part of the display's language string
  1261.  
  1262. Let's take apart the example.  Suppose the application's class name is
  1263. "Myterm". Also, suppose Open Windows is installed in /usr/openwin.
  1264. (Notice the example omits locale-specific lookup.)
  1265.  
  1266.     /usr/lib/X11/%T/%N        means /usr/lib/X11/app-defaults/Myterm
  1267.     $OPENWINHOME/lib/%T/%N    means /usr/openwin/lib/app-defaults/Myterm
  1268.  
  1269. As the application initializes, Xt tries to open both of the above
  1270. app-defaults files, in the order shown.  As soon as it finds one, it
  1271. reads it and uses it, and stops looking for others.  The effect of
  1272. this path is to search first in /usr/lib/X11, then in /usr/openwin.
  1273.  
  1274. Let's consider another example. This time, let's set
  1275. XUSERFILESEARCHPATH so it looks for the file Myterm.ad in the current
  1276. working directory, then for Myterm in the directory ~/app-defaults.
  1277.  
  1278.     setenv XUSERFILESEARCHPATH ./%N.ad:$HOME/app-defaults/%N
  1279.  
  1280. The first path in the list expands to ./Myterm.ad.  The second expands
  1281. to $HOME/app-defaults/Myterm.  This is a convenient setting for
  1282. debugging because it follows the Imake convention of naming the
  1283. app-defaults file Myterm.ad in the application's source directory, so
  1284. you can run the application from the directory in which you are
  1285. working and still have the resources loaded properly.
  1286.  
  1287. NOTE: when looking for app-default files with XUSERFILESEARCHPATH,
  1288.       for some  bizarre reason, neither the type nor file suffix is
  1289.       defined so %T and %S are useless.
  1290.  
  1291. With R5, there's another twist.  You may specify a customization
  1292. resource value.  For example, you might run the "myterm" application
  1293. like this:
  1294.  
  1295.     myterm -xrm "*customization: -color"
  1296.  
  1297. If one of your pathname specifications had the value
  1298. "/usr/lib/X11/app-defaults/%N%C" then the expanded pathname would be
  1299. "/usr/lib/X11/app-defaults/Myterm-color" because the %C substitution
  1300. character takes on the value of the customization resource.
  1301.  
  1302. The default XFILESEARCHPATH, compiled into Xt, is:
  1303.  
  1304.         /usr/lib/X11/%L/%T/%N%C:\  (R5)
  1305.         /usr/lib/X11/%l/%T/%N%C:\  (R5)
  1306.         /usr/lib/X11/%T/%N%C:\     (R5)
  1307.         /usr/lib/X11/%L/%T/%N:\
  1308.         /usr/lib/X11/%l/%T/%N:\
  1309.         /usr/lib/X11/%T/%N
  1310.  
  1311. (Note: some sites replace /usr/lib/X11 with a ProjectRoot in this
  1312. batch of default settings.)
  1313.  
  1314. The default XUSERFILESEARCHPATH, also compiled into Xt, is 
  1315.  
  1316.         <root>/%L/%N%C:\  (R5)
  1317.         <root>/%l/%N%C:\  (R5)
  1318.         <root>/%N%C:\     (R5)
  1319.         <root>/%L/%N:\
  1320.         <root>/%l/%N:\
  1321.         <root>/%N:
  1322.  
  1323. <root> is either the value of XAPPLRESDIR or the user's home directory
  1324. if XAPPLRESDIR is not set.  If you set XUSERFILESEARCHPATH to some
  1325. value other than the default, Xt ignores XAPPLRESDIR altogether.
  1326.  
  1327. Notice that the quick and dirty way of making your application find
  1328. your app-defaults file in your current working directory is to set
  1329. XAPPLRESDIR to ".", a single dot.  In R3, all this machinery worked
  1330. differently; for R3 compatibilty, many people set their XAPPLRESDIR
  1331. value to "./", a dot followed by a slash.
  1332.  
  1333.  
  1334. ----------------------------------------------------------------------
  1335. Subject: 21. What order are callbacks executed in?
  1336. ----------------------------------------------------------------------
  1337. (Courtesy of Donna Converse, converse@x.org; 5/10/92)
  1338.  
  1339. The Intrinsics library do not guarantee an order.  This is because
  1340. both the widget writer and the application writer have the ability to
  1341. modify the entire contents of the callback list.  Neither one
  1342. currently knows what the other is doing and so the Intrinsics cannot
  1343. guarantee the order of execution.
  1344.  
  1345. The application programmer cannot rely on the widget writer; the
  1346. widget writer is not required to document when the widget will add and
  1347. remove callbacks from the list or what effect this will have;
  1348. therefore the functionality contained in a callback should be
  1349. independent of the functionality contained in other callbacks on the
  1350. list.
  1351.  
  1352. Even though the Xt standard in the definition of XtAddCallback
  1353. says:
  1354.  
  1355.      "callback_name: Specifies the callback list to which the
  1356.      procedure is to be appended."
  1357.  
  1358. you may not infer from the word "appended" that the callback routines
  1359. are called in the same order as they have been added to the callback
  1360. list.
  1361.  
  1362. ----------------------------------------------------------------------
  1363. Subject: 22. How do I know if a widget is visible?
  1364. ----------------------------------------------------------------------
  1365. (Courtesy of Donna Converse, converse@x.org; 5/14/92)
  1366.  
  1367. > I am building a widget needs to know if it is visible. I set the visible
  1368. > interest field in Core and if my window is completely obscured, the Core
  1369. > visible flag goes FALSE. However, if my window is iconified, the flag
  1370. > stays set to TRUE.
  1371.  
  1372. Right, everything is implemented correctly.  This demonstrates a "deficiency"
  1373. in the X protocol, and the Core widget is reflecting the capabilities of the
  1374. protocol.  (The "deficiency" is that the information is available in one way,
  1375. in this case an inconvenient way.)  The Xt specification is accurate, in
  1376. the second and third paragraphs of section 7.10.2, so read this section
  1377. carefully.  The visible field will not change in response to iconification.
  1378.  
  1379. A VisibilityNotify event will not be received when the window goes from
  1380. viewable to unviewable, that is, when the widget or an ancestor is unmapped;
  1381. that is, when iconification occurs.  This is the protocol deficiency.
  1382. Visibility state and viewable state have specific meanings in the X protocol;
  1383. see the glossary in your Xlib and X protocol reference manual.
  1384.  
  1385. > Is this a problem with "mwm" or is there something
  1386. > else which needs to be done?
  1387.  
  1388. You'll see this with any window manager, with no window manager.
  1389.  
  1390. > If the problem is "mwm", what is the fastest
  1391. > way to determine if a window is iconified? 
  1392.  
  1393. As an application writer, keep track with a global Boolean in an action
  1394. routine with translations for MapNotify and UnmapNotify on the Shell widget
  1395. which contains your custom widget.  As the custom widget writer, see the
  1396. map_state field returned by a call to XGetWindowAttributes.  These are
  1397. suggestions.
  1398.  
  1399. ----------------------------------------------------------------------
  1400. Subject: 23. How do I reparent a widget in Xt, i.e. XtReparentWidget()?
  1401. ----------------------------------------------------------------------
  1402.  
  1403. You can't.
  1404.  
  1405. ----------------------------------------------------------------------
  1406. Subject: 24. Why use XtMalloc, XtFree, etc?
  1407. ----------------------------------------------------------------------
  1408.  
  1409. Unfortunately, most code that calls malloc(), realloc() or calloc()
  1410. tends to ignore the possibility of returning NULL.  At best it is
  1411. handled something like:
  1412.  
  1413.     ptr = (type *) malloc (sizeof (type))
  1414.     if (!ptr)
  1415.     {
  1416.         perror ("malloc in xyzzy()");
  1417.         exit (1)
  1418.     }
  1419. To handle this common case the Intrinsics define the functions
  1420. XtMalloc(), XtCalloc(), XtNew(), XtNewString() and XtRealloc() which
  1421. all use the standard C language functions malloc(), calloc() and
  1422. realloc() but execute XtErrorMsg() if a NULL value is returned.  Xt
  1423. error handlers are not supposed to return so this effectively exits.
  1424.  
  1425. In addition, if XtRealloc() is called with a NULL pointer, it uses
  1426. XtMalloc() to get the initial space.  This allows code like:
  1427.  
  1428.     if (!ptr)
  1429.         ptr = (type *) malloc (sizeof (type));
  1430.     else
  1431.         ptr = (type *) realloc (ptr, sizeof (type) * (count + 1));
  1432.     ++count;
  1433.  
  1434. to be written as:
  1435.  
  1436.     ptr = XtRealloc (ptr, sizeof (ptr) * ++count);
  1437.  
  1438. Also, XtFree() accepts a NULL pointer as an argument.  Generally, I've
  1439. found the Xt functions conveniant to use.  However, anytime I'm
  1440. allocating anything potentially large I use the standard functions so
  1441. I can fully recover from not enough memory errors.
  1442.  
  1443. XtNew() and XtNewString() are conveniant macros for allocating a
  1444. structure or copying a string:
  1445.  
  1446.       struct abc *xyzzy;
  1447.       char         *ptr;
  1448.       char         *str = "abcdef";
  1449.  
  1450.       xyzzy = XtNew (struct abc);    /* takes care of type casting */
  1451.       ptr = XtNewString (str);
  1452.  
  1453. A strict interpretation of the Intrinsics reference manual allow an
  1454. implementation to provide functions that are not exchangable with
  1455. malloc() and free().  I.e. code such as:
  1456.  
  1457.      char          *ptr;
  1458.  
  1459.      ptr = XtMalloc (100);
  1460.      /* ... */
  1461.      free (ptr);
  1462.  
  1463. may not work.  Personally, I'd call any implementation that did this
  1464. broken and complain to the vendor.
  1465.  
  1466. A common error for Motif programmers is to use XtFree() on a string
  1467. when they should really be using XmStringFree().
  1468.  
  1469. ----------------------------------------------------------------------
  1470. Subject: 25. How to debug an Xt application?
  1471. ----------------------------------------------------------------------
  1472. First, I'd recomend getting "purify" from Pure Software.  This is a
  1473. great package for tracing memory problems on Sun's.  It's a bit pricey
  1474. at $2750 but I'd still recomend it.  Excuse the marketing blurb
  1475. (contact support@pure.com for more info).
  1476.  
  1477.     Purify inserts additional checking instructions directly into
  1478.     the object code produced by existing compilers.  These
  1479.     instructions check every memory read and write performed by
  1480.     the program under test and detect several types of access
  1481.     errors, such as reading unitialized memory, writing past
  1482.     malloc'd bounds, or writing to freed memory.  Purify inserts
  1483.     checking logic into all of the code in a program, including
  1484.     third party and vendor object-code libraries, and verifies
  1485.     system call interfaces.  In addition, Purify tracks memory
  1486.     usage and identifies individual memory leaks using a novel
  1487.     adaption of garbage collection techniques.  Purify's nearly
  1488.     comprehensive memory access checking slows the target program
  1489.     down typically by a factor of two to five.
  1490.  
  1491. An alternative package that isn't as pricey ($395 for a Sun), runs on
  1492. many Unix's and has pretty similar features is "The SENTINEL Debugging
  1493. Environment".  This replaces malloc() and several other C library
  1494. functions to add additional checks.  (contact cpcahil@virtech.vti.com
  1495. for more info)
  1496.  
  1497. Next, if you are getting any sort of Xlib error, you'll need to run in
  1498. synchronous mode, easily accomplished with the "-sync" command line
  1499. argument or by setting the variable Xdebug to 1 with your debugger.  Then
  1500. set a break point in exit().  This will let you trace back to the
  1501. original Xlib function being called.  If you don't run in synchronous
  1502. mode, then the actual error may have occured any number of calls to
  1503. Xlib previously since the Xlib calls are buffered and replies from the
  1504. server are asynchronous.
  1505.  
  1506. Next, if you are having trouble with window layout, you can use the
  1507. undocumented resource "xtIdentifyWindows" or the class resource
  1508. "XtDebug" to cause the widget name to be identified with each window.
  1509. For example:
  1510.  
  1511.     example% xload -xrm '*XtDebug:true' &
  1512.     example% xwininfo -tree
  1513.          <click in new xload window>
  1514.  
  1515. will give the normal information but the widget name and class of each
  1516. window is included.  This can help for checking the location and size
  1517. of errant widgets.
  1518.  
  1519. Next, if you are having trouble with geometry managers or you want to
  1520. test the way a widget manages it's children, you can try
  1521. ftp.x.org:contrib/libXtGeo.tar.Z.  This acts as a filter
  1522. between any children and a geometry manager and checks the behaviour
  1523. of both.  It's a very clever idea.
  1524.  
  1525. The most unfortunate problem is debugging a callback while the
  1526. application is executing a grab of the keyboard or mouse (such as from
  1527. a pulldown menu).  The server effectively locks up and you'll need to
  1528. go to another machine and kill the debugger manually.  The server
  1529. locks up because the application being debugged has said no one else
  1530. can have access to the keyboard but the application is not stopped
  1531. waiting because the debugger is waiting for your commands.
  1532. Unfortunately you can't give them because all the input is going to
  1533. your application which is stopped.
  1534.  
  1535. The best way to debug this kind of problem is with two machines on
  1536. your desk, running the program under a debugger (or other environment)
  1537. on one machine, and running the application on the other, possibly
  1538. using a command sequence like this:
  1539.  
  1540.     othermachine% xhost +thismachine
  1541.     thismachine% setenv DISPLAY othermachine:0;
  1542.     thismachine% gdb application    # Your favorite debugger.
  1543.     or this:
  1544.     othermachine% xhost +thismachine
  1545.     thismachine% gdb application
  1546.     (gdb) set environment DISPLAY othermachine:0
  1547.     (gdb) run ...
  1548.  
  1549. I believe CodeCenter, a C interpreter/graphical debugger has a method
  1550. of dealing with this by explicitely calling the Xlib functions to
  1551. release any grabs during breakpoints.
  1552.  
  1553. Debugging widget problems requires pretty good debugging skills and
  1554. knowledge of how widgets work.  You can go a long way without knowing
  1555. the internals of a particular widget but not very far without
  1556. understanding how a widget works.  Judicious use of conditional
  1557. breakpoints and adding print statements with the debugger help a great
  1558. deal.
  1559.  
  1560. ----------------------------------------------------------------------
  1561. Subject: 26. Why don't XtAddInput(), XtAddTimeout() and XtAddWorkProc() work?
  1562. ----------------------------------------------------------------------
  1563.    I have got a delicate problem with the three routines XtAddInput,
  1564.    XtAddTimeOut and XtAddWorkProc. The problem I have is that when
  1565.    I use them in my application they seem not to be registred properly.
  1566.    I have made a handy little testprogram where everything works
  1567.    perfect, but in my "real" application nothing happens. 
  1568.  
  1569. The introduction in R3 of the XtApp*() functions obsoleted those
  1570. routines (see Q19 for other changes in R3, R4, and R5).  What happens is
  1571. they use a default application context different then the one you may
  1572. have created.  Since events and timeouts are distributed on a per
  1573. application context basis and you are using two application contexts,
  1574. you won't get those events.
  1575.  
  1576. For example:
  1577.  
  1578.     ...
  1579.     cnt = 0;
  1580.     toplevel = XtAppInitialize(&app, class,
  1581.                    Desc, XtNumber (Desc),
  1582.                    &argc, argv,
  1583.                    Fallback, args, cnt);
  1584.  
  1585.     XtAddTimeOut (...)
  1586.     XtAddWorkProc (...)
  1587.  
  1588.     XtAppMainLoop (app)
  1589.  
  1590. would never invoke the timeout.
  1591.  
  1592. ----------------------------------------------------------------------
  1593. Subject: 27. What is and how can I implement drag and drop?
  1594. ----------------------------------------------------------------------
  1595. (Courtesy of Roger Reynolds, rogerr@netcom.com; 19 Feb 93)
  1596.  
  1597. Drag-n-drop is a buzzword for moving data between clients, in an
  1598. ``intuitive'' fashion.
  1599.  
  1600. Motif Version 1.2 supports drag-n-drop capabilities, OpenLook has
  1601. supported d-n-d all along.  The two protocols are not compatable with
  1602. each other, and so far as I know, they are not published.
  1603.  
  1604. I wrote a package called RDD which is designed to be a flexible public
  1605. protocol for doing drag 'n drop operations between clients.  My
  1606. intention was to provide a tool which would make it easy for people to
  1607. support a "standard" drag-n-drop protocol in the programs they develop
  1608. and contribute or sell, regardless of what widget set is used (as long
  1609. as it is based on Xt).
  1610.  
  1611. The implementation is based upon my understanding of the ICCCM
  1612. conventions, for more details read the code.
  1613.  
  1614. I have heard from dozens of people using RDD who like it and feel that
  1615. it works a whole lot better than Motif 1.2 stuff.  Also, there seem to
  1616. be many who think that it is neat but are constrained to use Motif
  1617. anyway.
  1618.  
  1619. The latest RDD (and some other stuff) is available for ftp from
  1620. netcom.com, in /pub/rogerr.  A (possibly older) version is also
  1621. available on ftp.x.org in /contrib.
  1622.  
  1623. ----------------------------------------------------------------------
  1624. Subject: 28.+How can I add a C++ member function as a widget callback?
  1625. ----------------------------------------------------------------------
  1626. (Courtesy of lanzo@tekelec.com (Mark Lanzo), 7 Sep 1993)
  1627.  
  1628. You can not add a regular C++ member function as a callback to a 
  1629. widget, because the callback function won't be invoked with the
  1630. hidden object pointer "this" which all regular C++ member functions
  1631. require.  However, there is a way to get around this limitation:
  1632. "static" member functions.
  1633.  
  1634. A static member function is a function to which the usual scoping
  1635. and access rules apply, but which is not in fact actually associated
  1636. with a specific instance of an object, and it is NOT called using the 
  1637. special member function calling syntax.  In particular, there is no 
  1638. "this" pointer supplied to the function.  This means that a "static"
  1639. member function is really a lot like a "friend" function except that
  1640. its name is within the scope of the class (in other words, the name
  1641. is prefixed with the class name).
  1642.  
  1643. To demonstrate this, let's create a trivial class "Icon" which includes
  1644. a "button" widget, and a member function "select_CB" which is
  1645. installed as the "XmNselectCallback" on the button.  The class
  1646. declaration would then look something like this:
  1647.  
  1648.     class Icon
  1649.     {
  1650.     private:
  1651.             Widget      button;
  1652.             static void select_CB(Widget, XtPointer, XtPointer);
  1653.  
  1654.     public:
  1655.             Icon(Widget parent);        // Constructor
  1656.     };
  1657.  
  1658. For the example above, I can declare the constructor and callback
  1659. functions like this:
  1660.  
  1661.     Icon::Icon (Widget parent)
  1662.     {
  1663.         button = XtVaCreateWidget ("icon_button", xmPushButtonWidgetClass,
  1664.                       parent, NULL);
  1665.  
  1666.         //  Depending on your compiler, you may be able to specify
  1667.         //  "&Icon::select_CB" simply as "select_CB" in the 
  1668.         //  following statement:
  1669.         XtAddCallback (button,  XmNselectCallback, &Icon::select_CB,
  1670.               (XtPointer) this);
  1671.     }
  1672.  
  1673.     void Icon::select_CB (Widget w, XtPointer userData, XtPointer callData)
  1674.     {
  1675.         Icon * icon = (Icon *) userData;        // Instead of "this"
  1676.  
  1677.             // do whatever you want to do when the icon is selected ...
  1678.     }
  1679.  
  1680. There are two things to note here:
  1681.  
  1682.  *) If you want the static member function to work on a specific 
  1683.     object, you must supply the object pointer (such as by "userData"
  1684.     in the example).  Just like a non-member function.
  1685.  
  1686.  *) Although the function is declared as "static" inside the class
  1687.     definition, you do NOT include the "static" keyword when you
  1688.     actually define the function.
  1689.  
  1690. ----------------------------------------------------------------------
  1691. Subject: 29.+How can I identify the children of a manager widget?
  1692. ----------------------------------------------------------------------
  1693.  
  1694. Use XtGetValues() on XtNchildren (array of widget IDs) and
  1695. XtNnumChildren (number of widgets in array).
  1696.  
  1697.     Widget        *children = NULL;
  1698.     Cardinal    count = 0;
  1699.     Arg        args[10];
  1700.     Cardinal    cnt;
  1701.  
  1702.     cnt = 0;
  1703.     XtSetArg (args[cnt], XtNchildren, &children); ++cnt;
  1704.     XtSetArg (args[cnt], XtNnumChildren, &count); ++cnt;
  1705.     XtGetValues (widget, args, cnt);
  1706.  
  1707. NOTE: This does not get the list of popup children.  One must access
  1708. the private members of Core to do so.
  1709.  
  1710. ----------------------------------------------------------------------
  1711. Subject: 30.+Can I use XtMoveWidget(), ... to move widgets I created?
  1712. ----------------------------------------------------------------------
  1713.  
  1714. No.  In general, XtMoveWidget(), XtResizeWidget(),
  1715. XtMakeGeometryRequest(), and XtConfigureWidget() are for widget
  1716. internals only.  The only way for applications to change widget
  1717. geometry or configuration is through the XtSetValues() interface and
  1718. setting the XtNx, XtNy, XtNwidth and XtNheight resources.  If this
  1719. interface does not give you what you want, you should subclass the
  1720. widget to modify its geometry managers.
  1721.  
  1722. ----------------------------------------------------------------------
  1723. Subject: 31.+Why is XtGetValues() on XtNx, XtNy of my top level shell wrong?
  1724. ----------------------------------------------------------------------
  1725.  
  1726. XtNx and XtNy are the coordinates relative to your shell's parent
  1727. window, which is usually a window manager's frame window.  To
  1728. translate to the root coordinate space, use XtTranslateCoords() or
  1729. XTranslateCoordinates().
  1730.  
  1731. NOTE: XtTranslateCoords() was broken in releases prior to R4.
  1732.  
  1733. ----------------------------------------------------------------------
  1734. Subject: 32.+Why do some people use XmN<something> as resource names?
  1735. ----------------------------------------------------------------------
  1736.  
  1737. Motif adopted the convention of prefixing all their resources with
  1738. XmN instead of XtN.  Why?  It makes some small amount of sense to not
  1739. pollute the Xt name space.  Unfortunately it is not really practical
  1740. because it means every widget set would have to define all the
  1741. resources, including ones defined by the Intrinsics.  Very messy.  But
  1742. heh, OSF is it's own world, right?
  1743.  
  1744. ----------------------------------------------------------------------
  1745. Subject: 33.+How do I make my life easier when designing an application?
  1746. ----------------------------------------------------------------------
  1747.  
  1748. Find out about WCL.  Any other suggestions, anyone?
  1749.  
  1750. ----------------------------------------------------------------------
  1751. Subject: 34.+Why can't I override translations? Only the first item works.
  1752. ----------------------------------------------------------------------
  1753. (Thanks to Timothy J. Horton, 5/91)
  1754.  
  1755. You probably have an extra space after the specification of the first 
  1756. item, like this:
  1757.     basic*text.translations:  #override \
  1758.     Ctrl<Key>a:    beginning-of-line() \n\     
  1759.     Ctrl<Key>e:    end-of-line()
  1760.                           ^ extra space
  1761. The newline after that space is ending the translation definition.
  1762.  
  1763. ----------------------------------------------------------------------
  1764. Subject: 35.+Why do I get "Warning: Widget class version mismatch"?
  1765. ----------------------------------------------------------------------
  1766.  
  1767. This error, which typically goes on to say, "widget 11004 vs.
  1768. intrinsics 11003" indicates that the header files you included when
  1769. building your program didn't match the header files that the Xt
  1770. library you're linking against was built with; check your -I include
  1771. path and -L link-path to be sure.
  1772.  
  1773. However, the problem also occurs when linking against a version of the
  1774. X11R4 Xt library before patch 10; the version number was wrong.  Some
  1775. Sun OW systems, in particular, were shipped with the flawed version of
  1776. the library, and applications which link against the library typically
  1777. give the warnings you have seen.
  1778.  
  1779. ----------------------------------------------------------------------
  1780. Subject: 36.+Where can I get a good file-selector widget?
  1781. ----------------------------------------------------------------------
  1782.  
  1783. The Free Widget Foundation set offers a FileSelector widget, with
  1784. separate directory path and file listing windows, and the
  1785. FileComplete, which has emacs-style file completion and ~ expansion.
  1786.  
  1787. Other available file-requestor widgets include the XiFileSelector from
  1788. Iris Software's book, the xdbx file-selector extracted by David Nedde
  1789. (daven@ivy.wpi.edu), and the FileNominator from the aXe distribution.
  1790.  
  1791. The GhostView, Xfig, and vimage packages also include file-selector
  1792. widgets.
  1793.  
  1794. ----------------------------------------------------------------------
  1795. Subject: 37.+Where can I find a hypertext widget or source code?
  1796. ----------------------------------------------------------------------
  1797.  
  1798. A hypertext widget was posted to comp.sources.x.  It can be found in
  1799. volume 16 of the archives at ftp.uu.net under the name "hman".  The
  1800. distribution includes a hypertext widget with both Athena and Motif
  1801. compatability (set at compile-time) and hman, a Motif-based man reference page
  1802. reader that uses the widget to look up other man topics.  [Joe Shelby
  1803. (shelby@dirac.physics.jmu.edu); 6/93]
  1804.  
  1805. ----------------------------------------------------------------------
  1806. Subject: 38.+What widget is appropriate to use as a drawing canvas?
  1807. ----------------------------------------------------------------------
  1808.  
  1809. Some widget sets have a widget particularly for this purpose -- a
  1810. WorkSpace or DrawingArea which doesn't display anything but lets your Xt 
  1811. application know when it has been re-exposed, resized, and when it has received
  1812. user key and mouse input. 
  1813.  
  1814. The best thing to do for other widget sets -- including the Athena set 
  1815. -- is to create or obtain such a widget; this is preferable to drawing into a 
  1816. core widget and grabbing events with XtAddEventHandler(), which loses a number 
  1817. of benefits of Xt and encapsulation of the functionality .  
  1818.  
  1819. The publicly-available programs xball and xpic include other versions. 
  1820. The Display widget in the XG library (libXG-2.0.tar.Z on ftp.x.org) provides a 
  1821. generic way of drawing graphics in a widget.
  1822.  
  1823. The Athena Widget manual (mit/doc/Xaw/Template in the R5 distribution) 
  1824. includes a tutorial and source code to a simple widget which is suitable for 
  1825. use. 
  1826.  
  1827. The Free Widget Foundation set contains a Canvas widget.
  1828.  
  1829. ----------------------------------------------------------------------
  1830. Subject: 39.+What is this link problem with _get_wmShellWidgetClass?
  1831. ----------------------------------------------------------------------
  1832.  
  1833. In SunOS 4.1.2 Sun fixed a shared-library bug in ld which conflicts
  1834. with the way X builds the shared Xmu library, causing these symbols,
  1835. notably, to be undefined when building some X11 clients:
  1836.  
  1837.     _get_wmShellWidgetClass
  1838.     _get_applicationShellWidgetClass
  1839.  
  1840. Compiling "-Bstatic -lXmu -Bdynamic" appears to work. 
  1841.  
  1842. To solve the problem if you are using OpenWindows 3.0 (X11R4-based
  1843. Xt), please contact your local Sun office and request the following
  1844. patches:
  1845.  
  1846. Patch i.d.      Description
  1847. 100512-02       4.1.x OpenWindows 3.0 libXt Jumbo patch
  1848. 100573-03       4.1.x OpenWindows 3.0 undefined symbols when using
  1849.                         shared libXmu
  1850.  
  1851. [Greg Earle, earle@Sun.COM; 7/92] 
  1852.  
  1853. A source patch for use with the MIT X11R4 libraries was developed by
  1854. Conrad Kimball (cek@sdc.boeing.com); it retrofits into R4 some fixes
  1855. made in R5 to get around this problem. The patch is on ftp.x.org in
  1856. [1/93] contrib/X11R4_sunos4.1.2_patch_version3.Z
  1857.  
  1858. ----------------------------------------------------------------------
  1859. Subject: 40.+Why does XtGetValues not work for me (sic)?
  1860. ----------------------------------------------------------------------
  1861.  
  1862. The XtGetValues interface for retrieving resources from a widget is
  1863. sensitive to the type of variable. Your code may be doing something like this:
  1864.  
  1865.     {
  1866.     Arg args[3];
  1867.     int i;
  1868.     int sensitive;        /* oops; wrong data type */
  1869.     i=0;
  1870.     XtSetArg (args[i], XtNsensitive, &sensitive); i++;
  1871.     XtGetValues(widget, args, i );
  1872.     ...
  1873.     }
  1874.  
  1875. But XtNsensitive is a Boolean, which on most machines is a single byte; 
  1876. declaring the variable "sensitive" as Boolean works properly. This problem 
  1877. comes up often when using particular toolkits that redefine the Xt types 
  1878. Dimension and Position; code that assumes they are int will have similar 
  1879. problems if those types are actually short. In general: you are safe if you
  1880. use the actual type of the resource, as it appears in the widget's man page.
  1881. [11/90]
  1882.  
  1883. ----------------------------------------------------------------------
  1884. Subject: 41.+Is this a memory leak in the X11R4 XtDestroyWidget()?!
  1885. ----------------------------------------------------------------------
  1886.  
  1887. Yes. This is the "unofficial" fix-19 for the X11R4 Destroy.c:
  1888.  
  1889. *** Destroy.c.1.37    Thu Jul 11 15:41:25 1991
  1890. --- lib/Xt/Destroy.c    Thu Jul 11 15:42:23 1991
  1891. ***************
  1892. *** 1,4 ****
  1893. --- 1,5 ----
  1894.   /* $XConsortium: Destroy.c,v 1.37 90/09/28 10:21:32 swick Exp $ */
  1895. + /* Plus unofficial patches in revisions 1.40 and 1.41 */
  1896.   
  1897.   /***********************************************************
  1898.   Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  1899. ***************
  1900. *** 221,239 ****
  1901.        */
  1902.   
  1903.       int i = 0;
  1904. !     DestroyRec* dr = app->destroy_list;
  1905.       while (i < app->destroy_count) {
  1906.       if (dr->dispatch_level >= dispatch_level)  {
  1907.           Widget w = dr->widget;
  1908.           if (--app->destroy_count)
  1909.           bcopy( (char*)(dr+1), (char*)dr,
  1910. !                app->destroy_count*sizeof(DestroyRec)
  1911.                 );
  1912.           XtPhase2Destroy(w);
  1913.       }
  1914.       else {
  1915.           i++;
  1916. -         dr++;
  1917.       }
  1918.       }
  1919.   }
  1920. --- 222,245 ----
  1921.        */
  1922.   
  1923.       int i = 0;
  1924. !     DestroyRec* dr;
  1925.       while (i < app->destroy_count) {
  1926. +     /* XtPhase2Destroy can result in calls to XtDestroyWidget,
  1927. +      * and these could cause app->destroy_list to be reallocated.
  1928. +      */
  1929. +     dr = app->destroy_list + i;
  1930.       if (dr->dispatch_level >= dispatch_level)  {
  1931.           Widget w = dr->widget;
  1932.           if (--app->destroy_count)
  1933.           bcopy( (char*)(dr+1), (char*)dr,
  1934. !                (app->destroy_count - i) * sizeof(DestroyRec)
  1935.                 );
  1936.           XtPhase2Destroy(w);
  1937.       }
  1938.       else {
  1939.           i++;
  1940.       }
  1941.       }
  1942.   }
  1943.  
  1944. [from Donna Converse, converse@x.org]
  1945.  
  1946. ----------------------------------------------------------------------
  1947. Subject: 42.+Is this a memory leak in the X11R4 deletion of work procs?!
  1948. ----------------------------------------------------------------------
  1949.  
  1950. Apparently the X11R4 NextEvent.c`CallWorkProc fails to properly replace
  1951. the work proc record back on the free list correctly.
  1952.  
  1953.         if (delete) {
  1954.             w->next = freeWorkRecs;
  1955.             freeWorkRecs = w->next;    /* should be  =w; */
  1956.         }
  1957.  
  1958. ----------------------------------------------------------------------
  1959. Subject: 43.+How do I query the user synchronously using Xt?
  1960. ----------------------------------------------------------------------
  1961.     
  1962. It is possible to have code which looks like this trivial callback,
  1963. which has a clear flow of control. The calls to AskUser() block until
  1964. answer is set to one of the valid values. If it is not a "yes" answer,
  1965. the code drops out of the callback and back to an event-processing
  1966. loop:
  1967.  
  1968.     void quit(Widget w, XtPointer client, XtPointer call)
  1969.     {
  1970.         int             answer;
  1971.         answer = AskUser(w, "Really Quit?");
  1972.         if (RET_YES == answer)
  1973.             {
  1974.             answer = AskUser(w, "Are You Really Positive?");
  1975.             if (RET_YES == answer)
  1976.                 exit(0);
  1977.                 }
  1978.     }
  1979.  
  1980. A more realistic example might ask whether to create a file or whether
  1981. to overwrite it.
  1982.  
  1983. This is accomplished by entering a second event-processing loop and
  1984. waiting until the user answers the question; the answer is returned to
  1985. the calling function. That function AskUser() looks something like
  1986. this, where the Motif can be replaced with widget-set-specific code to
  1987. create some sort of dialog-box displaying the question string and
  1988. buttons for "OK", "Cancel" and "Help" or equivalents:
  1989.  
  1990.   int AskUser(w, string)
  1991.         Widget          w;
  1992.         char           *string;
  1993.   {
  1994.         int             answer=RET_NONE;    /* some not-used marker */
  1995.         Widget          dialog;            /* could cache&carry, but ...*/
  1996.         Arg             args[3];
  1997.         int             n = 0;
  1998.         XtAppContext    context;
  1999.  
  2000.         n=0;
  2001.         XtSetArg(args[n], XmNmessageString, XmStringCreateLtoR(string,
  2002.                 XmSTRING_DEFAULT_CHARSET)); n++;
  2003.         XtSetArg(args[n], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); n++;
  2004.         dialog = XmCreateQuestionDialog(XtParent(w), string, args, n);
  2005.         XtAddCallback(dialog, XmNokCallback, response, &answer);
  2006.         XtAddCallback(dialog, XmNcancelCallback, response, &answer);
  2007.         XtAddCallback(dialog, XmNhelpCallback, response, &answer);
  2008.         XtManageChild(dialog);
  2009.  
  2010.         context = XtWidgetToApplicationContext (w);
  2011.         while (answer == RET_NONE || XtAppPending(context)) {
  2012.                 XtAppProcessEvent (context, XtIMAll);
  2013.         }
  2014.         XtDestroyWidget(dialog);  /* blow away the dialog box and shell */
  2015.         return answer;
  2016.   }
  2017.  
  2018. The dialog supports three buttons, which are set to call the same
  2019. function when tickled by the user.  The variable answer is set when
  2020. the user finally selects one of those choices:
  2021.  
  2022.   void response(w, client, call)
  2023.         Widget          w;
  2024.         XtPointer client;
  2025.         XtPointer call;
  2026.   {
  2027.   int *answer = (int *) client;
  2028.   XmAnyCallbackStruct *reason = (XmAnyCallbackStruct *) call;
  2029.         switch (reason->reason) {
  2030.         case XmCR_OK:
  2031.                 *answer = RET_YES;    /* some #define value */
  2032.                 break;
  2033.         case XmCR_CANCEL:
  2034.                 *answer = RET_NO; 
  2035.         break;
  2036.         case XmCR_HELP:
  2037.                 *answer = RET_HELP;
  2038.                 break;
  2039.         default:
  2040.                 return;
  2041.         }
  2042. }
  2043.  
  2044. and the code unwraps back to the point at which an answer was needed and
  2045. continues from there.
  2046.  
  2047. [Thanks to Dan Heller (argv@sun.com); further code is in Dan's R3/contrib
  2048. WidgetWrap library. 2/91]
  2049.  
  2050. ----------------------------------------------------------------------
  2051. Subject: 44.+How do I simulate a button press/release event for a widget?
  2052. ----------------------------------------------------------------------
  2053.  
  2054. You can do this using XSendEvent(); it's likely that you're not setting
  2055. the window field in the event, which Xt needs in order to match to the widget
  2056. which should receive the event.
  2057.  
  2058.  If you're sending events to your own application, then you can use 
  2059. XtDispatchEvent() instead. This is more efficient than XSendEvent() in that you
  2060. avoid a round-trip to the server.
  2061.  
  2062. Depending on how well the widget was written, you may be able to call
  2063. its action procedures in order to get the effects you want.
  2064.  
  2065. [courtesy Mark A. Horstman (mh2620@sarek.sbc.com), 11/90]
  2066. -- 
  2067. Pete Ware                    ware@cis.ohio-state.edu
  2068. CIS Dept, Ohio State University            w/ (614) 292-7318
  2069. 228 Bolz Hall, 2036 Neil Ave.            h/ (614) 538-0965
  2070. Columbus, OH 43210
  2071.