home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / Xt-FAQ < prev    next >
Internet Message Format  |  1997-04-20  |  105KB

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