home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / answers / Xt-FAQ < prev    next >
Internet Message Format  |  1993-10-01  |  86KB

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