home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk1.iso / answers / motif-faq / part3 < prev    next >
Encoding:
Text File  |  1993-10-25  |  49.9 KB  |  1,291 lines

  1. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!medusa.hookup.net!news.kei.com!sol.ctr.columbia.edu!math.ohio-state.edu!cs.utexas.edu!uunet!haven.umd.edu!news.umbc.edu!cs.umd.edu!kong.gsfc.nasa.gov!vilya.gsfc.nasa.gov!not-for-mail
  2. From: dealy@kong.gsfc.nasa.gov (Brian Dealy)
  3. Newsgroups: news.answers,comp.answers
  4. Subject: Motif FAQ (Part 3 of 5)
  5. Followup-To: poster
  6. Date: 25 Oct 1993 16:09:42 -0400
  7. Organization: NASA/Goddard Space Flight Center
  8. Lines: 1270
  9. Sender: dealy@vilya.gsfc.nasa.gov
  10. Approved: news-answers-request@MIT.Edu
  11. Expires: +1 months
  12. Message-ID: <2ahbq6$10g@vilya.gsfc.nasa.gov>
  13. Reply-To: dealy@kong.gsfc.nasa.gov (Brian Dealy)
  14. NNTP-Posting-Host: vilya.gsfc.nasa.gov
  15. Keywords: FAQ question answer
  16. Archive-name: motif-faq/part3
  17. Last-modified: Oct 15 1993
  18. Version: 3.6
  19. Xref: senator-bedfellow.mit.edu news.answers:13923 comp.answers:2408
  20.  
  21.  
  22.  
  23.  
  24. -----------------------------------------------------------------------------
  25. Subject: 61)  TOPIC: FORM WIDGET
  26.  
  27.  
  28. -----------------------------------------------------------------------------
  29. Subject: 62)  Why don't labels in a Form resize when the label is changed?
  30. I've got some labels in a form. The labels don't resize whenever the label
  31. string resource is changed. As a result, the operator has to resize the window
  32. to see the new label contents. I am using Motif 1.1.
  33.  
  34. Answer: This problem may happen to any widget inside a Form widget. The
  35. problem was that the Form will resize itself when it gets geometry requests
  36. from its children. If its preferred size is not allowed, the Form will
  37. disallow all geometry requests from its children. The workaround is that you
  38. should set any ancestor of the Form to be resizable. For the shell which
  39. contains the Form you should set the shell resource XmNallowShellResize to be
  40. True (by default, it is set to FALSE).  There is currently an inconsistency on
  41. how resizing is being done, and it may get fixed in Motif 1.2.
  42.  
  43. From db@sunbim.be (Danny Backx)
  44.  
  45. Basically what you have to do is set the XmNresizePolicy on the Form to
  46. XmRESIZE_NONE.  The facts seem to be that XmRESIZE_NONE does NOT mean "do not
  47. allow resizes".  You may also have to set XmNresizable on the form to True.
  48.  
  49. -----------------------------------------------------------------------------
  50. Subject: 63)  How can I center a widget in a form?
  51.  
  52. Answer: One of Motif's trickier questions.  The problems are that: Form gives
  53. no support for centering, only for edge attachments, and the widget must stay
  54. in the center if the form or the widget is resized.  Just looking at
  55. horizontal centering (vertical is similar) some solutions are:
  56.  
  57.  a.  Use the table widget instead of Form.
  58.  
  59.  b.  A hack free solution is from Dan Heller:
  60.  
  61.      /* Written by Dan Heller.  Copyright 1991, O'Reilly && Associates.
  62.       * This program is freely distributable without licensing fees and
  63.       * is provided without guarantee or warranty expressed or implied.
  64.       * This program is -not- in the public domain.  This program is
  65.       * taken from the Motif Programming Manual, O'Reilly Volume 6.
  66.       */
  67.  
  68.      /* corners.c -- demonstrate widget layout management for a
  69.       * BulletinBoard widget.  There are four widgets each labeled
  70.       * top-left, top-right, bottom-left and bottom-right.  Their
  71.       * positions in the bulletin board correspond to their names.
  72.       * Only when the widget is resized does the geometry management
  73.       * kick in and position the children in their correct locations.
  74.       */
  75.      #include <Xm/BulletinB.h>
  76.      #include <Xm/PushBG.h>
  77.  
  78.      char *corners[] = {
  79.          "Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right",
  80.      };
  81.  
  82.      static void resize();
  83.  
  84.      main(argc, argv)
  85.      int argc;
  86.      char *argv[];
  87.      {
  88.          Widget toplevel, bboard;
  89.          XtAppContext app;
  90.          XtActionsRec rec;
  91.          int i;
  92.  
  93.          /* Initialize toolkit and create toplevel shell */
  94.          toplevel = XtVaAppInitialize(&app, "Demos", NULL, 0,
  95.              &argc, argv, NULL, NULL);
  96.  
  97.          /* Create your standard BulletinBoard widget */
  98.          bboard = XtVaCreateManagedWidget("bboard",
  99.              xmBulletinBoardWidgetClass, toplevel, NULL);
  100.  
  101.          /* Set up a translation table that captures "Resize" events
  102.           * (also called ConfigureNotify or Configure events).  If the
  103.           * event is generated, call the function resize().
  104.           */
  105.          rec.string = "resize";
  106.          rec.proc = resize;
  107.          XtAppAddActions(app, &rec, 1);
  108.          XtOverrideTranslations(bboard,
  109.              XtParseTranslationTable("<Configure>: resize()"));
  110.  
  111.          /* Create children of the dialog -- a PushButton in each corner. */
  112.          for (i = 0; i < XtNumber(corners); i++)
  113.              XtVaCreateManagedWidget(corners[i],
  114.                  xmPushButtonGadgetClass, bboard, NULL);
  115.  
  116.          XtRealizeWidget(toplevel);
  117.          XtAppMainLoop(app);
  118.      }
  119.  
  120.      /* resize(), the routine that is automatically called by Xt upon the
  121.       * delivery of a Configure event.  This happens whenever the widget
  122.       * gets resized.
  123.       */
  124.      static void
  125.      resize(w, event, args, num_args)
  126.      CompositeWidget w;   /* The widget (BulletinBoard) that got resized */
  127.      XConfigureEvent *event;  /* The event struct associated with the event */
  128.      String args[]; /* unused */
  129.      int *num_args; /* unused */
  130.      {
  131.          WidgetList children;
  132.          int width = event->width;
  133.          int height = event->height;
  134.          Dimension w_width, w_height;
  135.          short margin_w, margin_h;
  136.  
  137.          /* get handle to BulletinBoard's children and marginal spacing */
  138.          XtVaGetValues(w,
  139.              XmNchildren, &children,
  140.              XmNmarginWidth, &margin_w,
  141.              XmNmarginHeight, &margin_h,
  142.              NULL);
  143.  
  144.          /* place the top left widget */
  145.          XtVaSetValues(children[0],
  146.              XmNx, margin_w,
  147.  
  148.              XmNy, margin_h,
  149.              NULL);
  150.  
  151.          /* top right */
  152.          XtVaGetValues(children[1], XmNwidth, &w_width, NULL);
  153.  
  154.          /* To Center a widget in the middle of the BulletinBoard (or Form),
  155.           * simply call:
  156.           *   XtVaSetValues(widget,
  157.                XmNx,    (width - w_width)/2,
  158.                XmNy,    (height - w_height)/2,
  159.                NULL);
  160.           * and return.
  161.           */
  162.          XtVaSetValues(children[1],
  163.              XmNx, width - margin_w - w_width,
  164.              XmNy, margin_h,
  165.              NULL);
  166.          /* bottom left */
  167.          XtVaGetValues(children[2], XmNheight, &w_height, NULL);
  168.          XtVaSetValues(children[2],
  169.  
  170.              XmNx, margin_w,
  171.              XmNy, height - margin_h - w_height,
  172.              NULL);
  173.          /* bottom right */
  174.          XtVaGetValues(children[3],
  175.              XmNheight, &w_height,
  176.              XmNwidth, &w_width,
  177.              NULL);
  178.          XtVaSetValues(children[3],
  179.              XmNx, width - margin_w - w_width,
  180.              XmNy, height - margin_h - w_height,
  181.              NULL);
  182.      }
  183.  
  184.  c.  No uil solution has been suggested, because of the widget size problem
  185.  
  186. -----------------------------------------------------------------------------
  187. Subject: 64)  How do I line up two columns of widgets of different types?  I
  188. have a column of say label widgets, and a column of text widgets and I want to
  189. have them lined up horizontally. The problem is that they are of different
  190. heights. Just putting them in a form or rowcolumn doesn't line them up
  191. properly because the label and text widgets are of different height.
  192.  
  193. If you want the geometry to look like this
  194.  
  195.           -------------------------------------
  196.          |          -------------------------- |
  197.          |a label  |Some text                 ||
  198.          |          -------------------------- |
  199.                            ------------------- |
  200.          |a longer label  |Some more text     ||
  201.          |                 ------------------- |
  202.          |                    ---------------- |
  203.          |a very long label  |Even more text  ||
  204.          |                    ---------------- |
  205.           -------------------------------------
  206.  
  207. try
  208.  
  209. /* Written by Dan Heller.  Copyright 1991, O'Reilly && Associates.
  210.  * This program is freely distributable without licensing fees and
  211.  * is provided without guarantee or warranty expressed or implied.
  212.  * This program is -not- in the public domain.  This program is
  213.  * taken from the Motif Programming Manual, O'Reilly Volume 6.
  214.  */
  215.  
  216. /* text_form.c -- demonstrate how attachments work in Form widgets.
  217.  * by creating a text-entry form type application.
  218.  */
  219.  
  220. #include <Xm/PushB.h>
  221. #include <Xm/PushBG.h>
  222. #include <Xm/LabelG.h>
  223. #include <Xm/Text.h>
  224. #include <Xm/Form.h>
  225.  
  226. char *prompts[] = {
  227.     "Name:", "Phone:", "Address:",
  228.     "City:", "State:", "Zip:",
  229. };
  230.  
  231. main(argc, argv)
  232. int argc;
  233. char *argv[];
  234. {
  235.     Widget toplevel, mainform, subform, label, text;
  236.     XtAppContext app;
  237.     char buf[32];
  238.     int i;
  239.  
  240.     toplevel = XtVaAppInitialize(&app, "Demos", NULL, 0,
  241.         &argc, argv, NULL, NULL);
  242.  
  243.     mainform = XtVaCreateWidget("mainform",
  244.         xmFormWidgetClass, toplevel,
  245.         NULL);
  246.  
  247.     for (i = 0; i < XtNumber(prompts); i++) {
  248.         subform = XtVaCreateWidget("subform",
  249.             xmFormWidgetClass,   mainform,
  250.             /* first one should be attached for form */
  251.             XmNtopAttachment,    i? XmATTACH_WIDGET : XmATTACH_FORM,
  252.             /* others are attached to the previous subform */
  253.             XmNtopWidget,        subform,
  254.             XmNleftAttachment,   XmATTACH_FORM,
  255.             XmNrightAttachment,  XmATTACH_FORM,
  256.             NULL);
  257.         label = XtVaCreateManagedWidget(prompts[i],
  258.             xmLabelGadgetClass,  subform,
  259.             XmNtopAttachment,    XmATTACH_FORM,
  260.             XmNbottomAttachment, XmATTACH_FORM,
  261.             XmNleftAttachment,   XmATTACH_FORM,
  262.             XmNalignment,        XmALIGNMENT_BEGINNING,
  263.             NULL);
  264.         sprintf(buf, "text_%d", i);
  265.         text = XtVaCreateManagedWidget(buf,
  266.             xmTextWidgetClass,   subform,
  267.             XmNtopAttachment,    XmATTACH_FORM,
  268.             XmNbottomAttachment, XmATTACH_FORM,
  269.             XmNrightAttachment,  XmATTACH_FORM,
  270.             XmNleftAttachment,   XmATTACH_WIDGET,
  271.             XmNleftWidget,       label,
  272.             NULL);
  273.         XtManageChild(subform);
  274.     }
  275.     /* Now that all the forms are added, manage the main form */
  276.     XtManageChild(mainform);
  277.  
  278.     XtRealizeWidget(toplevel);
  279.     XtAppMainLoop(app);
  280. }
  281.  
  282. If you resize horizontally it stretches the text widgets.  If you resize
  283. vertically it leaves space under the bottom (if you don't resize, this is not
  284. problem).
  285.  
  286. If you want the text widgets to be lined up on the left, as in
  287.  
  288.           ----------------------------------------
  289.          |                    ------------------- |
  290.          |          a label  |Some text          ||
  291.          |                    ------------------- |
  292.                               ------------------- |
  293.          |   a longer label  |Some more text     ||
  294.          |                    ------------------- |
  295.          |                    ------------------- |
  296.          |a very long label  |Even more text     ||
  297.          |                    ------------------- |
  298.           ----------------------------------------
  299.  
  300. try this
  301.  
  302. /* Written by Dan Heller.  Copyright 1991, O'Reilly && Associates.
  303.  * This program is freely distributable without licensing fees and
  304.  * is provided without guarantee or warranty expressed or implied.
  305.  * This program is -not- in the public domain.  This program is
  306.  * taken from the Motif Programming Manual, O'Reilly Volume 6.
  307.  */
  308.  
  309. /* text_entry.c -- This demo shows how the RowColumn widget can be
  310.  * configured to build a text entry form.  It displays a table of
  311.  * right-justified Labels and Text widgets that extend to the right
  312.  * edge of the Form.
  313.  */
  314. #include <Xm/LabelG.h>
  315. #include <Xm/RowColumn.h>
  316. #include <Xm/Text.h>
  317.  
  318. char *text_labels[] = {
  319.     "Name:", "Phone:", "Address:", "City:", "State:", "Zip:",
  320. };
  321.  
  322. main(argc, argv)
  323. int argc;
  324. char *argv[];
  325. {
  326.     Widget toplevel, rowcol;
  327.     XtAppContext app;
  328.     char buf[8];
  329.     int i;
  330.  
  331.     toplevel = XtVaAppInitialize(&app, "Demos", NULL, 0,
  332.         &argc, argv, NULL, NULL);
  333.  
  334.     rowcol = XtVaCreateWidget("rowcolumn",
  335.         xmRowColumnWidgetClass, toplevel,
  336.         XmNpacking,        XmPACK_COLUMN,
  337.         XmNnumColumns,     XtNumber(text_labels),
  338.         XmNorientation,    XmHORIZONTAL,
  339.         XmNisAligned,      True,
  340.         XmNentryAlignment, XmALIGNMENT_END,
  341.         NULL);
  342.  
  343.     /* simply loop thru the strings creating a widget for each one */
  344.     for (i = 0; i < XtNumber(text_labels); i++) {
  345.         XtVaCreateManagedWidget(text_labels[i],
  346.             xmLabelGadgetClass, rowcol,
  347.             NULL);
  348.         sprintf(buf, "text_%d", i);
  349.         XtVaCreateManagedWidget(buf,
  350.             xmTextWidgetClass, rowcol,
  351.             NULL);
  352.     }
  353.  
  354.     XtManageChild(rowcol);
  355.     XtRealizeWidget(toplevel);
  356.     XtAppMainLoop(app);
  357. }
  358.  
  359. This makes all objects exactly the same size.  It does not resize in nice
  360. ways.
  361.  
  362. If you want the text widgets lined up on the left, and the labels to be the
  363. size of the longest string, resizing nicely both horizontally and vertically,
  364. as in
  365.  
  366.          -------------------------------------
  367.         |                    ---------------- |
  368.         |          a label  |Some text       ||
  369.         |                    ---------------- |
  370.                              ---------------- |
  371.         |   a longer label  |Some more text  ||
  372.         |                    ---------------- |
  373.         |                    ---------------- |
  374.         |a very long label  |Even more text  ||
  375.         |                    ---------------- |
  376.          -------------------------------------
  377.  
  378.  
  379.  
  380. Answer: Do this: to get the widgets lined up horizontally, use a form but
  381. place the widgets using XmATTACH_POSITION.  In the example, attach the top of
  382. the first label to the form, the bottomPosition to 33 (33% of the height).
  383. Attach the topPosition of the second label to 33 and the bottomPosition to 66.
  384. Attach the topPosition of the third label to 66 and the bottom of the label to
  385. the form.  Do the same with the text widgets.
  386.  
  387. To get the label widgets lined up vertically, use the right attachment of
  388. XmATTACH_OPPOSITE_WIDGET: starting from the one with the longest label, attach
  389. widgets on the right to each other. In the example, attach the 2nd label to
  390. the third, and the first to the second.  To get the text widgets lined up,
  391. just attach them on the left to the labels.  To get the text in the labels
  392. aligned correctly, use XmALIGNMENT_END for the XmNalignment resource.
  393.  
  394.         /* geometry for label 2
  395.         */
  396.         n = 0;
  397.         XtSetArg (args[n], XmNalignment, XmALIGNMENT_END); n++;
  398.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  399.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  400.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_POSITION); n++;
  401.         XtSetArg (args[n], XmNtopPosition, 66); n++;
  402.         XtSetValues (label[2], args, n);
  403.  
  404.         /* geometry for label 1
  405.         */
  406.         n = 0;
  407.         XtSetArg (args[n], XmNalignment, XmALIGNMENT_END); n++;
  408.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_POSITION); n++;
  409.         XtSetArg (args[n], XmNbottomPosition, 66); n++;
  410.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_POSITION); n++;
  411.         XtSetArg (args[n], XmNtopPosition, 33); n++;
  412.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  413.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
  414.         XtSetArg (args[n], XmNrightWidget, label[2]); n++;
  415.         XtSetValues (label[1], args, n);
  416.  
  417.         /* geometry for label 0
  418.         */
  419.         n = 0;
  420.         XtSetArg (args[n], XmNalignment, XmALIGNMENT_END); n++;
  421.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_POSITION); n++;
  422.         XtSetArg (args[n], XmNbottomPosition, 33); n++;
  423.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  424.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  425.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
  426.         XtSetArg (args[n], XmNrightWidget, label[1]); n++;
  427.         XtSetValues (label[0], args, n);
  428.  
  429.         /* geometry for text 0
  430.         */
  431.         n = 0;
  432.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  433.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_POSITION); n++;
  434.         XtSetArg (args[n], XmNbottomPosition, 33); n++;
  435.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  436.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  437.         XtSetArg (args[n], XmNleftWidget, label[0]); n++;
  438.         XtSetValues (text[0], args, n);
  439.  
  440.         /* geometry for text 1
  441.         */
  442.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_POSITION); n++;
  443.         XtSetArg (args[n], XmNtopPosition, 33); n++;
  444.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_POSITION); n++;
  445.         XtSetArg (args[n], XmNbottomPosition, 66); n++;
  446.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  447.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  448.         XtSetArg (args[n], XmNleftWidget, label[1]); n++;
  449.         XtSetValues (text[1], args, n);
  450.  
  451.         /* geometry for text 2
  452.         */
  453.         XtSetArg (args[n], XmNtopAttachment, XmATTACH_POSITION); n++;
  454.         XtSetArg (args[n], XmNtopPosition, 66); n++;
  455.         XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  456.         XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  457.         XtSetArg (args[n], XmNleftWidget, label[2]); n++;
  458.         XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  459.         XtSetValues (text[2], args, n);
  460.  
  461.  
  462. -----------------------------------------------------------------------------
  463. Subject: 65)  TOPIC: PUSHBUTTON WIDGET
  464.  
  465. -----------------------------------------------------------------------------
  466. Subject: 66)  Why can't I use accelerators on buttons not in a menu?
  467.  
  468. Answer: It is apparently a difficult feature to implement, but OSF are
  469. considering this for the future. It is problematic trying to use the Xt
  470. accelerators since the Motif method interferes with this.  one workaround
  471. suggested is to duplicate your non-menu button by a button in a menu
  472. somewhere, which does have a menu-accelerator installed.  When the user
  473. invokes what they think is the accelerator for the button they can see Motif
  474. actually invokes the button on the menu that they can't see at the time.
  475. Another method is described below and was contributed by Harald Albrecht of
  476. Institute of Geometry and Practical Mathematics Rhine Westphalia Technical
  477. University Aachen (RWTH Aachen), Germany
  478.  
  479.  
  480. From albrecht@igpm.rwth-aachen.de Thu Jul  8 11:44:21 1993
  481.  
  482. My work-around of this problem looks like this: (I've written that code for a
  483. Motif Object Library in C++ so please forgive me for being object orientated!)
  484. The hack consists of a rewritten message loop which checks for keypresses
  485. <MAlt>+<key>. If MessageLoop() finds such a keypress HandleAcc() ist called
  486. and the widget tree is searched for a suitable widget with the right mnemonic.
  487.  
  488.  
  489. // --------------------------------------------------------------------------
  490. // traverse the widget tree starting with the given widget.  // BOOL
  491. TraverseWidgetTree(Widget w, char *pMnemonic, XKeyEvent *KeyEvent) {
  492.     Widget               wChild;
  493.     WidgetList           ChildList;
  494.     int                  NumChilds, Child;
  495.     KeySym               LabelMnemonic;
  496.     char                 *pMnemonicString;
  497.  
  498. // Check if the widget is a subclass of label -- then it may have an //
  499. accelerator attached...
  500.     if ( XtIsSubclass(w, xmLabelWidgetClass) ) { // ok. Now: get the widget's
  501. mnemonic, convert it to ASCII and compare // it with the Key we're looking
  502. for.          XtVaGetValues(w, XmNmnemonic, &LabelMnemonic, NULL);
  503.         pMnemonicString = XKeysymToString(LabelMnemonic);         if (
  504. pMnemonicString &&              (strcasecmp(pMnemonicString, pMnemonic) == 0)
  505. ) {             // stimulate the keypress
  506. XmProcessTraversal((Widget)w, XmTRAVERSE_CURRENT);             KeyEvent->type
  507. = KeyPress;             KeyEvent->window    = XtWindow(w);
  508. KeyEvent->subwindow = XtWindow(w);             KeyEvent->state     = 0;
  509.             KeyEvent->keycode   =
  510.                 XKeysymToKeycode(XtDisplay(w), XK_space);
  511. XSendEvent(XtDisplay(w), XtWindow(w),                        True,
  512.                        ButtonPressMask, (XEvent*) KeyEvent);
  513. KeyEvent->type      = KeyRelease;             XSendEvent(XtDisplay(w),
  514. XtWindow(w),                        True,
  515. ButtonReleaseMask, (XEvent*) KeyEvent);             return True;         }
  516.     } // if this widget is a subclass of Composite check all the widget's //
  517. childs.
  518.     if ( XtIsSubclass(w, compositeWidgetClass) ) { // if we're in a menu (or
  519. something like that) forget this leaf of the // widget tree!          if (
  520. XtIsSubclass(w, xmRowColumnWidgetClass) ) {             unsigned char
  521. RowColumnType;             XtVaGetValues(w, XmNrowColumnType, &RowColumnType,
  522. NULL);             if ( RowColumnType != XmWORK_AREA ) return False;         }
  523.         XtVaGetValues(w, XmNchildren, &ChildList,
  524. XmNnumChildren, &NumChilds, NULL);         for ( Child = 0; Child < NumChilds;
  525. ++Child ) {             wChild = ChildList[Child];             if (
  526. TraverseWidgetTree(wChild, pMnemonic, KeyEvent) )                 return True;
  527.         }
  528.     }
  529.     return False; } // TraverseWidgetTree // ----------
  530. ---------------------------------------------------------------- // handle
  531. accelerators (keypress MAlt + key) // #define MAX_MAPPING 10 BOOL
  532. HandleAcc(Widget w, XEvent *event) {
  533.            Widget         widget, OldWidget;
  534.     static char           keybuffer[MAX_MAPPING];
  535.            int            CharCount;
  536.     static XComposeStatus composeStatus;
  537.  
  538. // convert KeyPress to ASCII
  539.     CharCount = XLookupString((XKeyEvent*) event,
  540.                               keybuffer, sizeof(keybuffer),
  541.                               NULL, &composeStatus);
  542.     keybuffer[CharCount] = 0; // Only one char is alright -- then search the
  543. widget tree for a widget // with the right mnemonic
  544.     if ( CharCount == 1 ) {         keybuffer[0] = tolower(keybuffer[0]);
  545.         widget = w;         while ( (widget != NULL) &&
  546. !XtIsSubclass(widget, shellWidgetClass) ) {             OldWidget = widget;
  547. widget = XtParent(widget);         }         if ( !widget ) widget =
  548. OldWidget;         return TraverseWidgetTree(widget,
  549. keybuffer, (XKeyEvent*) event);
  550.     }
  551.     return False; // no-one found.  } // HandleAcc // ----------
  552. ---------------------------------------------------------------- // modified
  553. message loop // loops until the Boolean pFlag points to is set to False void
  554. MessageLoop(Boolean *pFlag) {
  555.     XEvent nextEvent;
  556.  
  557.     while ( *pFlag ) {         if ( XtAppPending(AppContext) ) {
  558. XtAppNextEvent(AppContext, &nextEvent);             if ( nextEvent.type ==
  559. KeyPress ) { // Falls es ein Tastendruck ist, bei dem auch noch die ALT-Taste
  560. // (=Modifier 1) gedrueckt ist, koennte es ein Accelerator sein!
  561.                 if ( nextEvent.xkey.state & Mod1Mask )                     if
  562. ( HandleAcc(XtWindowToWidget(nextEvent.xkey.display,
  563. nextEvent.xkey.window),                                    &nextEvent) )
  564.                         continue; // Mitteilung konnte ausgeliefert werden
  565.                                   // und darf daher nicht den ueblichen
  566.                                   // Weg gehen!              }
  567. XtDispatchEvent(&nextEvent);         }
  568.     } } // TApplication::MessageLoop
  569.  
  570.  
  571. Harald Albrecht albrecht@igpm.rwth-aachen.de Institute of Geometry and
  572. Practical Mathematics Rhine Westphalia Technical University Aachen (RWTH
  573. Aachen), Germany
  574.  
  575.  
  576. -----------------------------------------------------------------------------
  577. Subject: 67)  TOPIC: LABEL WIDGET
  578.  
  579. -----------------------------------------------------------------------------
  580. Subject: 68)  How can I align the text in a label (button, etc) widget?
  581.  
  582. Answer: The alignment for the label widget is controlled by the resource
  583. XmNalignment, and the default centers the text. Use this resource to change it
  584. to left or right alignment.  However, when the label (or any descendant) is in
  585. a row column, and XmNisAligned is True (the default), the row column aligns
  586. text using its resource XmNentryAlignment. If you want simultaneous control
  587. over all widgets use this, but otherwise turn XmNisAligned off and do it
  588. individually.
  589.  
  590.  
  591.  
  592. -----------------------------------------------------------------------------
  593. Subject: 69)  Why doesn't label alignment work in a RowColumn?
  594.  
  595. Answer: RowColumn has a  resource XmNisAligned (default True) and and
  596. XmNentryAlignment (default XmALIGNMENT_BEGINNING).  These control alignment of
  597. the labelString in Labels and descendants. Set XmNisAligned to False to turn
  598. this off.
  599.  
  600. -----------------------------------------------------------------------------
  601. Subject: 70)  How can I set a multiline label?
  602. [Last modified: September 92]
  603.  
  604. Answer: In .Xdefaults
  605.  
  606.       *XmLabel*labelString:             Here\nis\nthe\nLabel
  607.  
  608. This method does not seem to work in some of the older Motif 1.0 versions.
  609.  
  610. In code,
  611.  
  612.       char buf[128];
  613.       XmString msg;
  614.       sprintf(buf, "Here\nis\nthe\nLabel");
  615.       msg = XmStringCreateLtoR(buf, XmSTRING_DEFAULT_CHARSET);
  616.       XtSetArg (args[n], XmNlabelString, msg);
  617.  
  618. Gives a four line label, using the escape sequence \n for a newline.  However,
  619. XmStringCreateLtoR() is obsoleted from version 1.1 on, and may disappear.
  620. This is because it it is only in the AES as "trial-use" and has been proposed
  621. for removal from the AES. Realistically, it will probably not be removed from
  622. any backward compatible versions of Motif, but the potential is there.  If it
  623. does disappear (or if you want to avoid using the non-AES compliant
  624. XmSTRING_DEFAULT_CHARSET), try this from Jean-Philippe Martin-Flatin
  625. <syj@ecmwf.co.uk>
  626.  
  627. #include <Xm/Xm.h>
  628. #include <string.h>
  629.  
  630. /*-----------------------------------------------------
  631.     Create a new XmString from a char*
  632.  
  633.     This function can deal with embedded 'newline' and
  634.     is equivalent to the obsolete XmStringCreateLtoR,
  635.     except it does not use non AES compliant charset
  636.     XmSTRING_DEFAULT_CHARSET
  637. ----------------------------------------------------*/
  638. XmString xec_NewString(char *s)
  639. {
  640.     XmString xms1;
  641.     XmString xms2;
  642.     XmString line;
  643.     XmString separator;
  644.     char     *p;
  645.     char     *t = XtNewString(s);   /* Make a copy for strtok not to */
  646.                                     /* damage the original string    */
  647.  
  648.  
  649.     separator = XmStringSeparatorCreate();
  650.     p         = strtok(t,"\n");
  651.     xms1      = XmStringCreateSimple(p);
  652.  
  653.     while (p = strtok(NULL,"\n"))
  654.     {
  655.         line = XmStringCreateSimple(p);
  656.         xms2 = XmStringConcat(xms1,separator);
  657.         XmStringFree(xms1);
  658.         xms1 = XmStringConcat(xms2,line);
  659.         XmStringFree(xms2);
  660.         XmStringFree(line);
  661.     }
  662.  
  663.     XmStringFree(separator);
  664.     XtFree(t);
  665.     return xms1;
  666. }
  667.  
  668.  
  669. Do not use XmStringCreateSimple() - it does not process the newline character
  670. in the way you want.
  671.  
  672. In UIL, you have to explicitly create a compound string with a separator.
  673. Here's what W. Scott Meeks suggests:
  674.  
  675. value nl : compound_string('', seperate=true);
  676.  
  677. object my_label : XmLabel
  678. {
  679.     arguments
  680.     {
  681.         XmNlabelString = 'Here' & nl & 'is' & nl & 'the' & nl & 'Label';
  682.     };
  683. };
  684.  
  685.  
  686. -----------------------------------------------------------------------------
  687. Subject: 71)  How can I have a vertical label?
  688.  
  689. Answer: Make a multiline label with one character per line, as in the last
  690. question. There is no way to make the text rotated by 90 degrees though.
  691.  
  692.  
  693. -----------------------------------------------------------------------------
  694. Subject: 72)  How can I have a Pixmap in a Label?
  695.  
  696. Answer: From Bob Hays (bobhays@spss.com)
  697.  
  698.     Pixmap px_disarm, px_disarm_insens;
  699.  
  700.     Widget Label1;
  701.     Pixel   foreground, background;
  702.     Arg     args[4];
  703.     Arg     arg[] = {
  704.                 { XmNforeground, &foreground },
  705.                 { XmNbackground, &background }
  706.     };
  707.  
  708.     Label1 = XmCreateLabel ( Shell1, "Label1",
  709.                                        (Arg *) NULL, (Cardinal) 0 );
  710.     XtGetValues ( Label1, arg, XtNumber ( arg ) );
  711.     px_disarm =
  712.       XCreatePixmapFromBitmapData(display,
  713.                                 DefaultRootWindow(display),
  714.                                 mtn_bits, mtn_width, mtn_height,
  715.                                 foreground,
  716.                                 background,
  717.                                 DefaultDepth(display,DefaultScreen(display)));
  718.     px_disarm_insens =
  719.       XCreatePixmapFromBitmapData(display,
  720.                                 DefaultRootWindow(display),
  721.                                 mtn_ins_bits, mtn_ins_width, mtn_ins_height,
  722.                                 foreground,
  723.                                 background,
  724.                                 DefaultDepth(display,DefaultScreen(display)));
  725.  
  726.     n = 0;
  727.     XtSetArg(args[n], XmNlabelType, XmPIXMAP);  n++;
  728.     XtSetArg(args[n], XmNlabelPixmap, px_disarm);  n++;
  729.     XtSetArg(args[n], XmNlabelInsensitivePixmap, px_disarm_insens ); n++;
  730.     XtSetValues ( Label1, args, n );
  731.     XtManageChild(Label1);
  732.  
  733. That will cause the foreground and background of your pixmap to be inherited
  734. from the one that would be used by OSF/Motif when the label is displayed.  The
  735. advantage is that this will utilize any resource values the user may have
  736. requested without looking explicitly into the resource database.  And, you
  737. will have a pixmap handy if the application insensitizes the label (without an
  738. XmNlabelInsensitivePixmap your label will go empty if made insensitive).
  739.  
  740. [Bob's original code was for a PushButton. Just change all Label to PushButton
  741. for them.]
  742.  
  743.  
  744. -----------------------------------------------------------------------------
  745. Subject: 73)  TOPIC: DRAWING AREA WIDGET
  746.  
  747. -----------------------------------------------------------------------------
  748. Subject: 74)  How can I send an expose event to a Drawing Area widget?  (or
  749. any other, come to that). I want to send an expose event so that it will
  750. redraw itself.
  751.  
  752. Answer: Use the Xlib call
  753.  
  754.         XClearArea(XtDisplay(w), XtWindow(w), 0, 0, 0, 0, True)
  755.  
  756. This clears the widget's window and generates an expose event in doing so.
  757. The widgets expose action will then redraw it.  This uses a round trip
  758. request.  An alternative, without the round trip is
  759.  
  760. from orca!mesa!rthomson@uunet.uu.net  (Rich Thomson):
  761.  
  762.     Widget da;
  763.     XmDrawingAreaCallbackStruct da_struct;
  764.  
  765.     da_struct.reason = XmCR_EXPOSE;
  766.     da_struct.event = (XEvent *) NULL;
  767.     da_struct.window = XtWindow(da);
  768.  
  769.     XtCallCallbacks(da, XmNexposeCallback, (XtPointer) da_struct);
  770.  
  771.  
  772. -----------------------------------------------------------------------------
  773. Subject: 75)  How can I know when a DrawingArea has been resized?  It
  774. generates an expose event whn it is enlarged, but not when it is shrunk.
  775.  
  776. Answer: Use the resize callback.
  777.  
  778. -----------------------------------------------------------------------------
  779. Subject: 76)  TOPIC: MENUS
  780.  
  781. -----------------------------------------------------------------------------
  782. Subject: 77)  What can I put inside a menu bar?
  783.  
  784. Answer: You can only put cascade buttons in menu bars. No pushbuttons, toggle
  785. buttons or gadgets are allowed. When you create a pulldown menu with parent a
  786. menu bar, its real parent is a shell widget.
  787.  
  788. -----------------------------------------------------------------------------
  789. Subject: 78)  Can I have a cascade button without a submenu in a pulldown
  790. menu?
  791.  
  792. Answer: Yes you can. A cascade button has an activate callback which is called
  793. when you click on it and it doesn't have a submenu. It can have a mnemonic,
  794. but keyboard traversal using the arrow keys in the menu will skip over it.
  795.  
  796. -----------------------------------------------------------------------------
  797. Subject: 79)  Should I have a cascade button without a submenu in a pulldown
  798. menu?
  799.  
  800. Answer: No. This is forbidden by the style guide. Technically you can do it
  801. (see previous question) but if you do it will not be Motif style compliant.
  802. This is unlikely to change - if a "button" is important enough to be in a
  803. pulldown menu bar with no pulldown, it should be a button elsewhere.  (Mind
  804. you, you won't be able to put accelerators on it elsewhere though.)
  805.  
  806. -----------------------------------------------------------------------------
  807. Subject: 80)  What is the best way to create popup menus?
  808. [Last modified: August 92]
  809.  
  810. Susan Murdock Thompson (from OSF): In general, create a popupMenu as the child
  811. from which you will be posting it from (ie: if you have a bulletinBoard with a
  812. PushButton in it and want MB2 on the pushButton to post the popupMenu, create
  813. the popupMenu as a child of the pushButton).  [This parent-child relationship
  814. seems to make a big difference in the behavior of the popups.]  Add an event
  815. handler to handle buttonPress events.  You'll need to check for the correct
  816. button (what you've specified menuPost to be) before posting the menu.
  817.  
  818. To create a popup that can be accessible from within an entire client window,
  819. create it as the child of the top-most widget (but not the shell) and add
  820. event handlers for the top-most widget and children widgets.
  821.  
  822. ie:
  823.  
  824. {
  825.   ....
  826.  
  827.   XtManageChild(rc=XmCreateRowColumn(Shell1, "rc", NULL, 0));
  828.   XtManageChild(label = XmCreateLabel(rc, "label", NULL, 0));
  829.   XtManageChild(text = XmCreateText(rc, "text", NULL, 0));
  830.   XtManageChild(pushbutton = XmCreatePushButton(rc, "pushbutton", NULL, 0));
  831.  
  832.   n = 0;
  833.   XtSetArg(args[n], XmNmenuPost, "<Btn3Down>"); n++;
  834.   popup = XmCreatePopupMenu(rc, "popup", args, n);
  835.  
  836.   XtAddEventHandler(rc, ButtonPressMask, False, PostMenu3, popup);
  837.   XtAddEventHandler(text, ButtonPressMask, False, PostMenu3, popup);
  838.   XtAddEventHandler(label, ButtonPressMask, False, PostMenu3, popup);
  839.   XtAddEventHandler(pushbutton, ButtonPressMask, False, PostMenu3, popup);
  840.  
  841.   XtManageChild(m1 = XmCreatePushButton(popup, "m1", NULL, 0));
  842.   XtManageChild(m2 = XmCreatePushButton(popup, "m2", NULL, 0));
  843.   XtManageChild(m3 = XmCreatePushButton(popup, "m3", NULL, 0));
  844.  
  845.   XtAddCallback(m1, XmNactivateCallback, SayCB, "button M1");
  846.   XtAddCallback(m2, XmNactivateCallback, SayCB, "button M2");
  847.   XtAddCallback(m3, XmNactivateCallback, SayCB, "button M3");
  848.   ...
  849. }
  850.  
  851. /* where PostMenu3 is ... */
  852.  
  853. PostMenu3 (w, popup, event)
  854. Widget w;
  855. Widget popup;
  856. XButtonEvent * event;
  857. {
  858.   printf("menuPost = 3, button %d0, event->button);
  859.  
  860.   if (event->button != Button3)
  861.     return;
  862.   XmMenuPosition(popup, event);
  863.   XtManageChild(popup);
  864. }
  865.  
  866.  
  867.  
  868. -----------------------------------------------------------------------------
  869. Subject: 81)  How do popup menus work?
  870. [Last modified: August 92]
  871.  
  872. Answer:
  873.  
  874. When a popup menu is created as the child of a widget the menu system installs
  875. a translation on the parent of the popup and descendants with an action which:
  876. (1) when 3-rd button (the default for the menuPost resource) is pressed the
  877. cursor changes and the mouse is grabbed for 5 seconds; (2) disables event
  878. handlers on the descendants and the handlers are never called; (3) an event
  879. handler installed on the parent works fine.
  880.  
  881. It is done so that the correct event handler will (in fact) be called.  There
  882. is a grab with owner_events true.  The grab is released by a timer,  but
  883. normally the posted menu shell puts up it's own grab.
  884.  
  885. If you only have widgets then you can use the subwindow field in the event to
  886. identify the original widget.  If you have gadgets or other data that you want
  887. to change the menu for (or use a specific menu for) then you must do a walk of
  888. the parent's children to find the best match.
  889.  
  890. One thing to beware of is that even with the grab,  because the menu system
  891. does a grab with owner events true, you must either have an event handler, or
  892. nothing that will use the event on each widget in the hierarchy of the menu's
  893. parent.  If a child widget has another event handler for button down, it may
  894. swallow the event and do something else.
  895.  
  896.  
  897.  
  898. -----------------------------------------------------------------------------
  899. Subject: 82)  Should I use translation tables or actions for popup menus?
  900. [Last modified: August 92]
  901.  
  902. Answer: The original goal of popupMenus was that the user would not have to
  903. specify an event handler to manage popupMenus; however, that did not become
  904. reality.  Larry Rogers wrote:
  905.  
  906. > There appear to be two ways to manage popup menus.  I
  907. > am curious what the correct way would be:
  908.  
  909. > 1.  Change the translation table of the widget with the
  910. >    popup child to popup the menu.  Note that this does
  911. >    not currently working for many widgets, because aug-
  912. >    menting their translations, even for augment breaks
  913. >    the widget.
  914.  
  915. > 2.  Add an event handler at creation to the widget; then
  916. >    determine if the event that caused the event handler
  917. >    to be called is the current button being used by the
  918. >    menu as its activation button.
  919.  
  920. Susan Murdock Thompson (from OSF) replied: *Theoretically, you should be able
  921. to do both.*  Our documentation says use event handlers.  Our tests for the
  922. toolkit use event handlers and for UIL use translations.  (Although I tried an
  923. event handler with a UIL test and it works).
  924.  
  925. -----------------------------------------------------------------------------
  926. Subject: 83)  What are the known bugs in popup menus?
  927. [Last modified: August 92]
  928.  
  929. Answer: As at Motif 1.1.4, the bugs for which an OSF PIR exists are:
  930.  
  931.    (3)  Menus not being sticky (ie: posted on a Btn CLICK)  [ Note:this
  932.         problem occurs with OptionMenus as well]  (PIR 3435)
  933.  
  934.    (6)  Destroying a widget with an associated popupMenu results in
  935.         "Warning: Attempt to remove non-existant passive grab"         (PIR
  936. 2972)
  937.  
  938.    (7)  Current documentation insufficient regarding requirements for
  939.         success in using PopupMenus.  (PIR 3433)
  940.  
  941.  
  942. -----------------------------------------------------------------------------
  943. Subject: 84)  Can I have multiple popup menus on the same widget?
  944. [Last modified: August 92]
  945.  
  946. Answer: If you want to have several popups (activated by different mouse
  947. buttons) on the same widget..., well, that doesn't work yet.
  948.  
  949. If you want to have several popups on different children... that works.  But
  950. don't put a popup on the parent (manager) widget, or it will rule!
  951.  
  952.  
  953.  
  954. -----------------------------------------------------------------------------
  955. Subject: 85)  TOPIC: INPUT FOCUS
  956.  
  957. -----------------------------------------------------------------------------
  958. Subject: 86) How can I specify the widget that should have the keyboard focus
  959. when my application starts up?  Answer: In Motif 1.2, use XmNinitialFocus on
  960. the manager widget.  thanks to Ken Lee, klee@synoptics.com
  961.  
  962.  
  963. -----------------------------------------------------------------------------
  964. Subject: 87)  How can I direct the keyboard input to a particular widget?
  965.  
  966. Answer: In Motif 1.1 call XmProcessTraversal(target, XmTRAVERSE_CURRENT).  The
  967. widget (and all of its ancestors) does need to be realized BEFORE you call
  968. this. Otherwise it has no effect.  XmProcessTraversal is reported to have many
  969. bugs, so it may not work right.  A common occurrence is that it doesn't move
  970. to the widget, but if you call XmProcessTraversal *twice* in a row, it will.
  971. If you can't get it to work, try this from Kee Hinckley:
  972.  
  973.     // This insane sequence is as follows:
  974.     //      On manage set up a focus callback
  975.     //      On focus callback set up a timer (and get rid of focus callback!)
  976.     //      On timer set the focus (which only works if the parent
  977.     //      has the focus,
  978.     //      which is why we went through all of this garbage)
  979.     // There may be a better way, but I haven't time to try it now.
  980.     //
  981.     static void focusTO(void *data, XtIntervalId *) {
  982.         XmProcessTraversal((Widget) data, XmTRAVERSE_CURRENT);
  983.     }
  984.  
  985.     static void focusCB(Widget w, XtPointer data, XtPointer) {
  986.         XtRemoveCallback(w, XmNfocusCallback, focusCB, data);
  987.         XtAppAddTimeOut(XtWidgetToApplicationContext(w), 0, focusTO, data);
  988.     }
  989.  
  990.     void OmXSetFocus(Widget parent, Widget w) {
  991.         XtAddCallback(parent, XmNfocusCallback, focusCB, w);
  992.     }
  993.  
  994.  
  995. In Motif 1.0 call the undocumented _XmGrabTheFocus(target).
  996.  
  997. Do not use the X or Xt calls such as XtSetKeyboardFocus since this bypasses
  998. the Motif traversal layer and can cause it to get confused.  This can lead to
  999. odd keyboard behaviour elsewhere in your application.
  1000.  
  1001. -----------------------------------------------------------------------------
  1002. Subject: 88)  How can I have a modal dialog which has to be answered before
  1003. the application can continue?
  1004. [Last modified: July 92]
  1005.  
  1006. Answer: The answer depends on whether you are using the Motif window manager
  1007. mwm or not.  Test for this by XmIsMotifWMRunning.
  1008.  
  1009. The window manager mwm knows how to control event passing to dialog widgets
  1010. declared as modal. If the dialog is set to application modal, then no
  1011. interaction with the rest of the application can occur until the dialog is
  1012. destroyed or unmanaged.
  1013.  
  1014. Use the appropriate code in the following program.  There is followup
  1015. discussion after the program.
  1016.  
  1017.  
  1018. /* Written by Dan Heller.  Copyright 1991, O'Reilly && Associates.
  1019.  * This program is freely distributable without licensing fees and
  1020.  * is provided without guarantee or warranty expressed or implied.
  1021.  * This program is -not- in the public domain.  This program is
  1022.  * taken from the Motif Programming Manual, O'Reilly Volume 6.
  1023.  */
  1024.  
  1025. /*
  1026.  * ask_user.c -- create a pushbutton that posts a dialog box
  1027.  * that asks the user a question that requires an immediate
  1028.  * response.  The function that asks the question actually
  1029.  * posts the dialog that displays the question, waits for and
  1030.  * returns the result.
  1031.  */
  1032. #include <X11/Intrinsic.h>
  1033. #include <Xm/DialogS.h>
  1034. #include <Xm/SelectioB.h>
  1035. #include <Xm/RowColumn.h>
  1036. #include <Xm/MessageB.h>
  1037. #include <Xm/PushBG.h>
  1038. #include <Xm/PushB.h>
  1039.  
  1040. XtAppContext app;
  1041.  
  1042. #define YES 1
  1043. #define NO  2
  1044.  
  1045. /* main() --create a pushbutton whose callback pops up a dialog box */
  1046. main(argc, argv)
  1047. char *argv[];
  1048. int argc;
  1049. {
  1050.     Widget parent, button, toplevel;
  1051.     XmString label;
  1052.     void pushed();
  1053.  
  1054.     toplevel = XtAppInitialize(&app, "Demos",
  1055.         NULL, 0, &argc, argv, NULL, NULL, 0);
  1056.  
  1057.     label = XmStringCreateSimple("/bin/rm *");
  1058.     button = XtVaCreateManagedWidget("button",
  1059.         xmPushButtonWidgetClass, toplevel,
  1060.         XmNlabelString,          label,
  1061.         NULL);
  1062.     XtAddCallback(button, XmNactivateCallback,
  1063.         pushed, "Remove Everything?");
  1064.     XmStringFree(label);
  1065.  
  1066.     XtRealizeWidget(toplevel);
  1067.     XtAppMainLoop(app);
  1068. }
  1069.  
  1070. /* pushed() --the callback routine for the main app's pushbutton. */
  1071. void
  1072. pushed(w, question)
  1073. Widget w;
  1074. char *question;
  1075. {
  1076.     if (AskUser(w, question) == YES)
  1077.         puts("Yes");
  1078.     else
  1079.         puts("No");
  1080. }
  1081.  
  1082. /*
  1083.  * AskUser() -- a generalized routine that asks the user a question
  1084.  * and returns the response.
  1085.  */
  1086. AskUser(parent, question)
  1087. char *question;
  1088. {
  1089.     static Widget dialog;
  1090.     XmString text, yes, no;
  1091.     static int answer;
  1092.     extern void response();
  1093.  
  1094.     answer = 0;
  1095.     if (!dialog) {
  1096.         dialog = XmCreateQuestionDialog(parent, "dialog", NULL, 0);
  1097.         yes = XmStringCreateSimple("Yes");
  1098.         no = XmStringCreateSimple("No");
  1099.         XtVaSetValues(dialog,
  1100.             XmNdialogStyle,        XmDIALOG_APPLICATION_MODAL,
  1101.             XmNokLabelString,      yes,
  1102.             XmNcancelLabelString,  no,
  1103.             NULL);
  1104.         XtSetSensitive(
  1105.             XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON), False);
  1106.         XtAddCallback(dialog, XmNokCallback, response, &answer);
  1107.         XtAddCallback(dialog, XmNcancelCallback, response, &answer);
  1108.         /* if the user interacts via the system menu: */
  1109.         XtAddCallback(dialog, XmNpopdownCallback, response, &answer);
  1110.     }
  1111.     text = XmStringCreateSimple(question);
  1112.     XtVaSetValues(dialog,
  1113.         XmNmessageString,      text,
  1114.         NULL);
  1115.     XmStringFree(text);
  1116.     XtManageChild(dialog);
  1117.     XtPopup(XtParent(dialog), XtGrabNone);
  1118.  
  1119.     /* while the user hasn't provided an answer, simulate XtMainLoop.
  1120.      * The answer changes as soon as the user selects one of the
  1121.      * buttons and the callback routine changes its value.  Don't
  1122.      * break loop until XtPending() also returns False to assure
  1123.      * widget destruction.
  1124.      */
  1125.     while (answer == 0 || XtAppPending(app))
  1126.         XtAppProcessEvent(app, XtIMAll);
  1127.     return answer;
  1128. }
  1129.  
  1130. /* response() --The user made some sort of response to the
  1131.  * question posed in AskUser().  Set the answer (client_data)
  1132.  * accordingly and destroy the dialog.
  1133.  */
  1134. void
  1135. response(w, answer, reason)
  1136. Widget w;
  1137. int *answer;
  1138. XmAnyCallbackStruct *reason;
  1139. {
  1140.     switch (reason->reason) {
  1141.         case XmCR_OK:
  1142.             *answer = YES;
  1143.             break;
  1144.         case XmCR_CANCEL:
  1145.             *answer = NO;
  1146.             break;
  1147.         default:
  1148.             *answer = NO;
  1149.             return;
  1150.     }
  1151. }
  1152.  
  1153.  
  1154.  
  1155. If you aren't running a window manager that acknowledges this hint, then you
  1156. may have to grab the pointer (and keyboard) yourself to make sure the user
  1157. doesn't interact with any other widget.  Change the grab flag in XtPopup to
  1158. XtGrabExclusive, and XtRemoveGrab(XtParent(w)) to the response() function.
  1159.  
  1160.  
  1161. -----------------------------------------------------------------------------
  1162. Subject: 89)  TOPIC: MEMORY AND SPEED
  1163.  
  1164. -----------------------------------------------------------------------------
  1165. Subject: 90)  When can I free data structures passed to or retrieved from
  1166. Motif?
  1167.  
  1168. Answer:
  1169.  In most cases, especially for XmStrings and XmFontLists, Motif copies data
  1170. passed to it or retrieved from it, so it may be freed immediately.  Server-
  1171. side resources, such as pixmaps and color cells, however, are not copied, so
  1172. should not be freed.  More recent versions of Motif are better than earlier
  1173. versions and exceptions should be documented.  thanks to klee*synoptics.com
  1174. (Ken Lee)
  1175.  
  1176. -----------------------------------------------------------------------------
  1177. Subject: 91)  Why does my application grow in size?
  1178.  
  1179. Answer: Motif 1.0 has many memory leaks, particularly in XmString
  1180. manipulation.  Switch to Motif 1.1.
  1181.  
  1182. Answer: The Intrinsics have a memory leak in accelerator table management, and
  1183. Motif uses this heavily.  Avoid this by mapping/unmapping widgets rather than
  1184. creating/destroying them, or get  X11R4 fix-15/16/17.
  1185.  
  1186. Answer: The server may grow in size due to its own memory leaks.  Switch to a
  1187. later server.
  1188.  
  1189. Answer: You are responsible for garbage collection in `C'.  Some common cases
  1190. where a piece of memory becomes garbage are
  1191.  
  1192.  a.  Memory is allocated by Motif for XmStrings by the functions
  1193.      XmStringConcat, XmStringCopy, XmStringCreate, XmStringCreateLtoR,
  1194.      XmStringCreateSimple, XmStringDirectionCreate, XmStringNConcat,
  1195.      XmStringNCopy, XmStringSegmentCreate, and XmStringSeparatorCreate.  The
  1196.      values returned by these functions should be freed using XmStringFree
  1197.      when they are no longer needed.
  1198.  
  1199.  b.  Memory is allocated by Motif for ordinary character strings (of type
  1200.      String) by Motif in XmStringGetLtoR, XmStringGetNextComponent, and
  1201.      XmStringGetNextSegment. After using the string, XtFree() it. [Note that
  1202.      XmStrings and Strings are two different data types.  XmStrings are
  1203.      XmStringFree'd, Strings are XtFree'd.]
  1204.  
  1205.  c.  If you have set the label (an XmString) in a label, pushbutton, etc
  1206.      widget, free it after calling XtSetValues() or the widget creation
  1207.      routine by XmStringFree().
  1208.  
  1209.  d.  If you have set text in a text widget, the text widget makes its own
  1210.      copy.  Unless you have a use for it, there is no need to keep your own
  1211.      copy.
  1212.  
  1213.  e.  If you have set the strings in a list widget the list widget makes its
  1214.      own copy.  Unless you have a use for it, there is no need to keep your
  1215.      own copy.
  1216.  
  1217.  f.  When you get the value of a single compound string from a Widget e.g.
  1218.      XmNlabelString, XmNmessageString, ... Motif gives you a copy of its
  1219.      internal value.  You should XmStringFree this when you have finished with
  1220.      it.
  1221.  
  1222.  g.  On the other hand, when you get a value of a Table e.g. XmStringTable for
  1223.      a List, you get a *pointer* to the internal Table, and should not free
  1224.      it.
  1225.  
  1226.  h.  When you get the value of the text in a widget by XmTextGetString or from
  1227.      the resource XmNvalue, you get a copy of the text.  You should XtFree
  1228.      this when you have finished with it.
  1229.  
  1230. Answer: From Josef Nelissen: at least in Motif 1.1.4, X11R4 on a HP 720, the
  1231. XmText/XmTextFieldSetString() functions have a memory leak.  The old
  1232. value/contents of the Widget isn't freed correctly.  To work around this bug,
  1233. one should use a XmText Widget (in single-line-mode) instead of a XmTextField
  1234. Widget (the solution fails with XmTextField Widgets !) and replace any
  1235.  
  1236.        XmTextSetString(text_widget, str);
  1237.  
  1238. by
  1239.  
  1240.        XmTextReplace(text_widget, (XmTextPosition) 0,
  1241.                      XmTextGetLastPosition(text_widget), str);
  1242.  
  1243.  
  1244. -----------------------------------------------------------------------------
  1245. Subject: 92)  Why does my application take a long time to start up?
  1246.  
  1247. Answer: You are probably creating too many widgets at startup time.  Delay
  1248. creating them until needed.  If you have a large number of resources in text
  1249. files (such as in app-defaults), time may be spent reading and parsing it.
  1250.  
  1251. -----------------------------------------------------------------------------
  1252. Subject: 93)  My application is running too slowly. How can I speed it up?
  1253.  
  1254. Answer: Use the R4 rather than R3 server.  It is much faster.
  1255.  
  1256. Answer: The standard memory allocator is not well tuned to Motif, and can
  1257. degrade performance.  Use a better allocator.  e.g. with SCO Unix, link with
  1258. libmalloc.a; use the allocator from GNU emacs; use the allocator from Perl.
  1259.  
  1260. Answer: Avoid lots of widget creation and destruction.  It fragments memory
  1261. and slows everything down.  Popup/popdown, manage/unmanage instead.
  1262.  
  1263. Answer: Set mappedWhenManaged to FALSE, and then call XtMapWidget()
  1264. XtUnmapWidget() rather than managing.
  1265.  
  1266. Answer: Get more memory - your application, the server and the Operating
  1267. System may be spending a lot of time being swapped.
  1268.  
  1269. Answer: If you are doing much XmString work yourself, such as heavy use of
  1270. XmStringCompare, speed may deteriorate due to the large amount of internal
  1271. conversions and malloc'ing.  Try using XmStringByteCompare if appropriate or
  1272. ordinary Ascii strings if you can.
  1273.  
  1274.  
  1275.  
  1276. -----------------------------------------------------------------------------
  1277. Subject: 94)  Why is my application so huge?
  1278.  
  1279. Answer: The typical size of a statically linked Motif app is in the megabytes.
  1280. This is often caused by the size of libXm.a. A large part of this gets linked
  1281. in to even trivial Motif programs. You can reduce the code size by linking
  1282. against shared libraries if they are available.  Running "strip" on the
  1283. executable can often reduce size. Note that the size of the running program
  1284. should be measured by "ps", not by the code size.
  1285.  
  1286. -----------------------------------------------------------------------------
  1287. END OF PART THREE
  1288. -- 
  1289. ..........................
  1290.  
  1291.