home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xaw / TextPop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-25  |  41.0 KB  |  1,360 lines

  1. /* $XConsortium: TextPop.c,v 1.22 91/07/25 18:10:22 rws Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1989 by the Massachusetts Institute of Technology,
  5. Cambridge, Massachusetts.
  6.  
  7.                         All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its 
  10. documentation for any purpose and without fee is hereby granted, 
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in 
  13. supporting documentation, and that the names of Digital or MIT not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.  
  16.  
  17. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  19. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27. /************************************************************
  28.  *
  29.  * This file is broken up into three sections one dealing with
  30.  * each of the three popups created here:
  31.  *
  32.  * FileInsert, Search, and Replace.
  33.  *
  34.  * There is also a section at the end for utility functions 
  35.  * used by all more than one of these dialogs.
  36.  *
  37.  * The following functions are the only non-static ones defined
  38.  * in this module.  They are located at the begining of the
  39.  * section that contains this dialog box that uses them.
  40.  * 
  41.  * void _XawTextInsertFileAction(w, event, params, num_params);
  42.  * void _XawTextDoSearchAction(w, event, params, num_params);
  43.  * void _XawTextDoReplaceAction(w, event, params, num_params);
  44.  * void _XawTextInsertFile(w, event, params, num_params);
  45.  *
  46.  *************************************************************/
  47.  
  48. #include <X11/IntrinsicP.h>
  49. #include <X11/StringDefs.h>
  50. #include <X11/Shell.h> 
  51.  
  52. #include <X11/Xaw/TextP.h>
  53. #include <X11/Xaw/AsciiText.h>
  54. #include <X11/Xaw/Cardinals.h>
  55. #include <X11/Xaw/Command.h>
  56. #include <X11/Xaw/Form.h>
  57. #include <X11/Xaw/Toggle.h>
  58. #include <X11/Xmu/CharSet.h>
  59. #include <stdio.h>
  60. #include <X11/Xos.h>        /* for O_RDONLY */
  61. #include <errno.h>
  62.  
  63. extern int errno, sys_nerr;
  64. extern char* sys_errlist[];
  65.  
  66. #define INSERT_FILE ("Enter Filename:")
  67.  
  68. #define SEARCH_LABEL_1  ("Use <Tab> to change fields.")
  69. #define SEARCH_LABEL_2  ("Use ^q<Tab> for <Tab>.")
  70. #define DISMISS_NAME  ("cancel")
  71. #define DISMISS_NAME_LEN 6
  72. #define FORM_NAME     ("form")
  73. #define LABEL_NAME    ("label")
  74. #define TEXT_NAME     ("text")
  75.  
  76. #define R_OFFSET      1
  77.  
  78. static void CenterWidgetOnPoint(), PopdownSearch(), DoInsert(), _SetField();
  79. static void InitializeSearchWidget(), SetResource(), SetSearchLabels();
  80. static void DoReplaceOne(), DoReplaceAll();
  81. static Widget CreateDialog(), GetShell();
  82. static void SetWMProtocolTranslations();
  83. static Boolean DoSearch(), SetResourceByName(), Replace();
  84. static String GetString();
  85.  
  86. static void AddInsertFileChildren();
  87. static Boolean InsertFileNamed();
  88. static void AddSearchChildren();
  89.  
  90. static char radio_trans_string[] =
  91.     "<Btn1Down>,<Btn1Up>:   set() notify()";
  92.  
  93. static char search_text_trans[] = 
  94.   "~Shift<Key>Return:      DoSearchAction(Popdown) \n\
  95.    Shift<Key>Return:       DoSearchAction() SetField(Replace) \n\
  96.    Ctrl<Key>q,<Key>Tab:    insert-char()    \n\
  97.    Ctrl<Key>c:             PopdownSearchAction() \n\
  98.    <Btn1Down>:             select-start() SetField(Search) \n\
  99.    <Key>Tab:               DoSearchAction() SetField(Replace)";
  100.  
  101. static char rep_text_trans[] = 
  102.   "~Shift<Key>Return:      DoReplaceAction(Popdown) \n\
  103.    Shift<Key>Return:       SetField(Search) \n\
  104.    Ctrl<Key>q,<Key>Tab:    insert-char()     \n\
  105.    Ctrl<Key>c:             PopdownSearchAction() \n\
  106.    <Btn1Down>:             select-start() DoSearchAction() SetField(Replace)\n\
  107.    <Key>Tab:               SetField(Search)";
  108.  
  109. /************************************************************
  110.  * 
  111.  * This section of the file contains all the functions that 
  112.  * the file insert dialog box uses.
  113.  *
  114.  ************************************************************/
  115.  
  116. /*    Function Name: _XawTextInsertFileAction
  117.  *    Description: Action routine that can be bound to dialog box's 
  118.  *                   Text Widget that will insert a file into the main
  119.  *                   Text Widget.
  120.  *    Arguments:   (Standard Action Routine args) 
  121.  *    Returns:     none.
  122.  */
  123.  
  124. /* ARGSUSED */
  125. void 
  126. _XawTextInsertFileAction(w, event, params, num_params)
  127. Widget w;
  128. XEvent *event;
  129. String * params;
  130. Cardinal * num_params;
  131. {
  132.   DoInsert(w, (XtPointer) XtParent(XtParent(XtParent(w))), NULL);
  133. }
  134.  
  135. /*    Function Name: _XawTextInsertFile
  136.  *    Description: Action routine that can be bound to the text widget
  137.  *                   it will popup the insert file dialog box.
  138.  *    Arguments:   w - the text widget.
  139.  *                   event - X Event (used to get x and y location).
  140.  *                   params, num_params - the parameter list.
  141.  *    Returns:     none.
  142.  *
  143.  * NOTE:
  144.  *
  145.  * The parameter list may contain one entry.
  146.  *
  147.  *  Entry:  This entry is optional and contains the value of the default
  148.  *          file to insert.
  149.  */
  150.  
  151. void 
  152. _XawTextInsertFile(w, event, params, num_params)
  153. Widget w;
  154. XEvent *event;
  155. String * params;
  156. Cardinal * num_params;
  157. {
  158.   TextWidget ctx = (TextWidget)w;
  159.   char * ptr;
  160.   XawTextEditType edit_mode;
  161.   Arg args[1];
  162.  
  163.   XtSetArg(args[0], XtNeditType,&edit_mode);
  164.   XtGetValues(ctx->text.source, args, ONE);
  165.   
  166.   if (edit_mode != XawtextEdit) {
  167.     XBell(XtDisplay(w), 0);
  168.     return;
  169.   }
  170.  
  171.   if (*num_params == 0) 
  172.     ptr = "";
  173.   else 
  174.     ptr = params[0];
  175.     
  176.   if (!ctx->text.file_insert) {
  177.     ctx->text.file_insert = CreateDialog(w, ptr, "insertFile",
  178.                      AddInsertFileChildren);
  179.     XtRealizeWidget(ctx->text.file_insert);
  180.     SetWMProtocolTranslations(ctx->text.file_insert);
  181.   }
  182.  
  183.   CenterWidgetOnPoint(ctx->text.file_insert, event);
  184.   XtPopup(ctx->text.file_insert, XtGrabNone);
  185. }
  186.  
  187. /*    Function Name: PopdownFileInsert
  188.  *    Description: Pops down the file insert button.
  189.  *    Arguments: w - the widget that caused this action.
  190.  *                 closure - a pointer to the main text widget that
  191.  *                           popped up this dialog.
  192.  *                 call_data - *** NOT USED ***.
  193.  *    Returns: none.
  194.  */
  195.  
  196. /* ARGSUSED */
  197. static void 
  198. PopdownFileInsert(w, closure, call_data)
  199. Widget w;            /* The Dialog Button Pressed. */
  200. XtPointer closure;        /* Text Widget. */
  201. XtPointer call_data;        /* unused */
  202. {
  203.   TextWidget ctx = (TextWidget) closure;
  204.  
  205.   XtPopdown( ctx->text.file_insert );
  206.   (void) SetResourceByName( ctx->text.file_insert, LABEL_NAME, 
  207.                XtNlabel, (XtArgVal) INSERT_FILE);
  208. }
  209.  
  210. /*    Function Name: DoInsert
  211.  *    Description: Actually insert the file named in the text widget
  212.  *                   of the file dialog.
  213.  *    Arguments:   w - the widget that activated this callback.
  214.  *                   closure - a pointer to the text widget to insert the
  215.  *                             file into.
  216.  *    Returns: none.
  217.  */
  218.  
  219. /* ARGSUSED */
  220. static void 
  221. DoInsert(w, closure, call_data)
  222. Widget w;            /* The Dialog Button Pressed. */
  223. XtPointer closure;        /* Text Widget */
  224. XtPointer call_data;        /* unused */
  225. {
  226.   TextWidget ctx = (TextWidget) closure;
  227.   char buf[BUFSIZ], msg[BUFSIZ];
  228.   Widget temp_widget;
  229.  
  230.   sprintf(buf, "%s.%s", FORM_NAME, TEXT_NAME);
  231.   if ( (temp_widget = XtNameToWidget(ctx->text.file_insert, buf)) == NULL ) {
  232.     strcpy(msg, 
  233.        "*** Error: Could not get text widget from file insert popup");
  234.   }
  235.   else 
  236.     if (InsertFileNamed( (Widget) ctx, GetString( temp_widget ))) {
  237.       PopdownFileInsert(w, closure, call_data);
  238.       return;
  239.     }
  240.     else
  241.       sprintf( msg, "*** Error: %s ***",
  242.           (errno > 0 && errno < sys_nerr) ?
  243.           sys_errlist[errno] : "Can't open file" );
  244.   
  245.  
  246.   (void)SetResourceByName(ctx->text.file_insert, 
  247.               LABEL_NAME, XtNlabel, (XtArgVal) msg);
  248.   XBell(XtDisplay(w), 0);
  249. }
  250.  
  251. /*    Function Name: InsertFileNamed
  252.  *    Description: Inserts a file into the text widget.
  253.  *    Arguments: tw - The text widget to insert this file into.
  254.  *                 str - name of the file to insert.
  255.  *    Returns: TRUE if the insert was sucessful, FALSE otherwise.
  256.  */
  257.  
  258. static Boolean
  259. InsertFileNamed(tw, str)
  260. Widget tw;
  261. char *str;
  262. {
  263.   int fid;
  264.   XawTextBlock text;
  265.   char buf[BUFSIZ];
  266.   XawTextPosition start_pos, pos;
  267.  
  268.   if ( (str == NULL) || (strlen(str) == 0) || 
  269.        ((fid = open(str, O_RDONLY)) <= 0))
  270.     return(FALSE);
  271.  
  272.   start_pos = pos = XawTextGetInsertionPoint(tw);
  273.   text.firstPos = 0;
  274.   text.format = FMT8BIT;
  275.  
  276.   while ((text.length = read(fid, buf, BUFSIZ)) > 0) {
  277.     text.ptr = buf;
  278.     if (XawTextReplace(tw, pos, pos, &text) != XawEditDone) {
  279.       /*
  280.        * If the replace failed then remove what we have 
  281.        * replaced so far, and return an error.
  282.        */
  283.       text.length = 0;
  284.       (void) XawTextReplace(tw, start_pos, pos, &text);
  285.       (void) close(fid);
  286.       return(FALSE);
  287.     }
  288.     pos += text.length;
  289.   }
  290.   (void) close(fid);
  291.   XawTextSetInsertionPoint(tw, pos);
  292.   return(TRUE);
  293. }
  294.  
  295. /*    Function Name: AddInsertFileChildren
  296.  *    Description: Adds all children to the InsertFile dialog widget.
  297.  *    Arguments: form - the form widget for the insert dialog widget.
  298.  *                 ptr - a pointer to the initial string for the Text Widget.
  299.  *                 tw - the main text widget.
  300.  *    Returns: none
  301.  */
  302.  
  303. static void
  304. AddInsertFileChildren(form, ptr, tw)
  305. Widget form, tw;
  306. char * ptr;
  307. {
  308.   Arg args[10];
  309.   Cardinal num_args;
  310.   Widget label, text, cancel, insert;
  311.   XtTranslations trans;
  312.  
  313.   num_args = 0;
  314.   XtSetArg(args[num_args], XtNlabel, INSERT_FILE);num_args++;
  315.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  316.   XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
  317.   XtSetArg(args[num_args], XtNresizable, TRUE ); num_args++;
  318.   XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++;
  319.   label = XtCreateManagedWidget(LABEL_NAME, labelWidgetClass, form,
  320.                 args, num_args);
  321.   
  322.   num_args = 0;
  323.   XtSetArg(args[num_args], XtNfromVert, label); num_args++;
  324.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  325.   XtSetArg(args[num_args], XtNright, XtChainRight); num_args++;
  326.   XtSetArg(args[num_args], XtNeditType, XawtextEdit); num_args++;
  327.   XtSetArg(args[num_args], XtNresizable, TRUE); num_args++;
  328.   XtSetArg(args[num_args], XtNresize, XawtextResizeWidth); num_args++;
  329.   XtSetArg(args[num_args], XtNstring, ptr); num_args++;
  330.   text = XtCreateManagedWidget(TEXT_NAME, asciiTextWidgetClass, form,
  331.                 args, num_args);
  332.  
  333.   num_args = 0;
  334.   XtSetArg(args[num_args], XtNlabel, "Insert File"); num_args++;
  335.   XtSetArg(args[num_args], XtNfromVert, text); num_args++;
  336.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  337.   XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
  338.   insert = XtCreateManagedWidget("insert", commandWidgetClass, form,
  339.                  args, num_args);
  340.  
  341.   num_args = 0;
  342.   XtSetArg(args[num_args], XtNlabel, "Cancel"); num_args++;
  343.   XtSetArg(args[num_args], XtNfromVert, text); num_args++;
  344.   XtSetArg(args[num_args], XtNfromHoriz, insert); num_args++;
  345.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  346.   XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
  347.   cancel = XtCreateManagedWidget(DISMISS_NAME, commandWidgetClass, form,
  348.                  args, num_args);
  349.  
  350.   XtAddCallback(cancel, XtNcallback, PopdownFileInsert, (XtPointer) tw);
  351.   XtAddCallback(insert, XtNcallback, DoInsert, (XtPointer) tw);
  352.  
  353.   XtSetKeyboardFocus(form, text);
  354.  
  355. /*
  356.  * Bind <CR> to insert file.
  357.  */
  358.  
  359.   trans = XtParseTranslationTable("<Key>Return: InsertFileAction()");
  360.   XtOverrideTranslations(text, trans);
  361.  
  362. }
  363.  
  364. /************************************************************
  365.  * 
  366.  * This section of the file contains all the functions that 
  367.  * the search dialog box uses.
  368.  *
  369.  ************************************************************/
  370.  
  371. /*    Function Name: _XawTextDoSearchAction
  372.  *    Description: Action routine that can be bound to dialog box's 
  373.  *                   Text Widget that will search for a string in the main 
  374.  *                   Text Widget.
  375.  *    Arguments:   (Standard Action Routine args) 
  376.  *    Returns:     none.
  377.  *
  378.  * Note:
  379.  * 
  380.  * If the search was sucessful and the argument popdown is passed to
  381.  * this action routine then the widget will automatically popdown the 
  382.  * search widget.
  383.  */
  384.  
  385. /* ARGSUSED */
  386. void 
  387. _XawTextDoSearchAction(w, event, params, num_params)
  388. Widget w;
  389. XEvent *event;
  390. String * params;
  391. Cardinal * num_params;
  392. {
  393.   TextWidget tw = (TextWidget) XtParent(XtParent(XtParent(w)));
  394.   Boolean popdown = FALSE;
  395.  
  396.   if ( (*num_params == 1) &&
  397.        ((params[0][0] == 'p') || (params[0][0] == 'P')) )
  398.       popdown = TRUE;
  399.     
  400.   if (DoSearch(tw->text.search) && popdown)
  401.     PopdownSearch(w, (XtPointer) tw->text.search, NULL);
  402. }
  403.  
  404. /*    Function Name: _XawTextPopdownSearchAction
  405.  *    Description: Action routine that can be bound to dialog box's 
  406.  *                   Text Widget that will popdown the search widget.
  407.  *    Arguments:   (Standard Action Routine args) 
  408.  *    Returns:     none.
  409.  */
  410.  
  411. /* ARGSUSED */
  412. void 
  413. _XawTextPopdownSearchAction(w, event, params, num_params)
  414. Widget w;
  415. XEvent *event;
  416. String * params;
  417. Cardinal * num_params;
  418. {
  419.   TextWidget tw = (TextWidget) XtParent(XtParent(XtParent(w)));
  420.  
  421.   PopdownSearch(w, (XtPointer) tw->text.search, NULL);
  422. }
  423.  
  424. /*    Function Name: PopdownSeach
  425.  *    Description: Pops down the search widget and resets it.
  426.  *    Arguments: w - *** NOT USED ***.
  427.  *                 closure - a pointer to the search structure.
  428.  *                 call_data - *** NOT USED ***.
  429.  *    Returns: none
  430.  */
  431.  
  432. /* ARGSUSED */
  433. static void 
  434. PopdownSearch(w, closure, call_data)
  435. Widget w;            
  436. XtPointer closure;        
  437. XtPointer call_data;        
  438. {
  439.   struct SearchAndReplace * search = (struct SearchAndReplace *) closure;
  440.  
  441.   XtPopdown( search->search_popup );
  442.   SetSearchLabels(search, SEARCH_LABEL_1, SEARCH_LABEL_2, FALSE);
  443. }
  444.  
  445. /*    Function Name: SearchButton
  446.  *    Description: Performs a search when the button is clicked.
  447.  *    Arguments: w - *** NOT USED **.
  448.  *                 closure - a pointer to the search info.
  449.  *                 call_data - *** NOT USED ***.
  450.  *    Returns: 
  451.  */
  452.  
  453. /* ARGSUSED */
  454. static void 
  455. SearchButton(w, closure, call_data)
  456. Widget w;            
  457. XtPointer closure;        
  458. XtPointer call_data;
  459. {
  460.   (void) DoSearch( (struct SearchAndReplace *) closure );
  461. }
  462.  
  463. /*    Function Name: _XawTextSearch
  464.  *    Description: Action routine that can be bound to the text widget
  465.  *                   it will popup the search dialog box.
  466.  *    Arguments:   w - the text widget.
  467.  *                   event - X Event (used to get x and y location).
  468.  *                   params, num_params - the parameter list.
  469.  *    Returns:     none.
  470.  *
  471.  * NOTE:
  472.  *
  473.  * The parameter list contains one or two entries that may be the following.
  474.  *
  475.  * First Entry:   The first entry is the direction to search by default.
  476.  *                This arguement must be specified and may have a value of
  477.  *                "left" or "right".
  478.  *
  479.  * Second Entry:  This entry is optional and contains the value of the default
  480.  *                string to search for.
  481.  */
  482.  
  483. #define SEARCH_HEADER ("Text Widget - Search():")
  484.  
  485. void 
  486. _XawTextSearch(w, event, params, num_params)
  487. Widget w;
  488. XEvent *event;
  489. String * params;
  490. Cardinal * num_params;
  491. {
  492.   TextWidget ctx = (TextWidget)w;
  493.   XawTextScanDirection dir;
  494.   char * ptr, buf[BUFSIZ];
  495.   XawTextEditType edit_mode;
  496.   Arg args[1];
  497.  
  498. #ifdef notdef
  499.   if (ctx->text.source->Search == NULL) {
  500.       XBell(XtDisplay(w), 0);
  501.       return;
  502.   }
  503. #endif
  504.  
  505.   if ( (*num_params < 1) || (*num_params > 2) ) {
  506.     sprintf(buf, "%s %s\n%s", SEARCH_HEADER, "This action must have only", 
  507.         "one or two parameters");
  508.     XtAppWarning(XtWidgetToApplicationContext(w), buf);
  509.     return;
  510.   }
  511.   else if (*num_params == 1) 
  512.     ptr = "";
  513.   else 
  514.     ptr = params[1];
  515.  
  516.   switch(params[0][0]) {
  517.   case 'b':            /* Left. */
  518.   case 'B':
  519.     dir = XawsdLeft;
  520.     break;
  521.   case 'f':            /* Right. */
  522.   case 'F':
  523.     dir = XawsdRight;
  524.     break;
  525.   default:
  526.     sprintf(buf, "%s %s\n%s", SEARCH_HEADER, "The first parameter must be",
  527.         "Either 'backward' or 'forward'");
  528.     XtAppWarning(XtWidgetToApplicationContext(w), buf);
  529.     return;
  530.   }
  531.  
  532.   if (ctx->text.search== NULL) {
  533.     ctx->text.search = XtNew(struct SearchAndReplace);
  534.     ctx->text.search->search_popup = CreateDialog(w, ptr, "search",
  535.                           AddSearchChildren);
  536.     XtRealizeWidget(ctx->text.search->search_popup);
  537.     SetWMProtocolTranslations(ctx->text.search->search_popup);
  538.   }
  539.   else if (*num_params > 1) {
  540.     XtVaSetValues(ctx->text.search->search_text, XtNstring, ptr, NULL);
  541.   }
  542.  
  543.   XtSetArg(args[0], XtNeditType,&edit_mode);
  544.   XtGetValues(ctx->text.source, args, ONE);
  545.  
  546.   InitializeSearchWidget(ctx->text.search, dir, (edit_mode == XawtextEdit));
  547.  
  548.   CenterWidgetOnPoint(ctx->text.search->search_popup, event);
  549.   XtPopup(ctx->text.search->search_popup, XtGrabNone);
  550. }
  551.  
  552. /*    Function Name: InitializeSearchWidget
  553.  *    Description: This function initializes the search widget and
  554.  *                   is called each time the search widget is poped up.
  555.  *    Arguments: search - the search widget structure.
  556.  *                 dir - direction to search. 
  557.  *                 replace_active - state of the sensitivity for the 
  558.  *                                  replace button.
  559.  *    Returns: none.
  560.  */
  561.  
  562. static void
  563. InitializeSearchWidget(search, dir, replace_active)
  564. struct SearchAndReplace * search;
  565. XawTextScanDirection dir;
  566. Boolean replace_active;
  567. {
  568.   SetResource(search->rep_one, XtNsensitive, (XtArgVal) replace_active);
  569.   SetResource(search->rep_all, XtNsensitive, (XtArgVal) replace_active);
  570.   SetResource(search->rep_label, XtNsensitive, (XtArgVal) replace_active);
  571.   SetResource(search->rep_text, XtNsensitive, (XtArgVal) replace_active);
  572.  
  573.   switch (dir) {
  574.   case XawsdLeft:
  575.     SetResource(search->left_toggle, XtNstate, (XtArgVal) TRUE);
  576.     break;
  577.   case XawsdRight:
  578.     SetResource(search->right_toggle, XtNstate, (XtArgVal) TRUE);
  579.     break;
  580.   default:
  581.     break;
  582.   }
  583. }  
  584.  
  585. /*    Function Name: AddSearchChildren
  586.  *    Description: Adds all children to the Search Dialog Widget.
  587.  *    Arguments: form - the form widget for the search widget.
  588.  *                 ptr - a pointer to the initial string for the Text Widget.
  589.  *                 tw - the main text widget.
  590.  *    Returns: none.
  591.  */
  592.  
  593. static void
  594. AddSearchChildren(form, ptr, tw)
  595. Widget form, tw;
  596. char * ptr;
  597. {
  598.   Arg args[10];
  599.   Cardinal num_args;
  600.   Widget cancel, search_button, s_label, s_text, r_text;
  601.   XtTranslations trans;
  602.   struct SearchAndReplace * search = ((TextWidget) tw)->text.search;
  603.  
  604.   num_args = 0;
  605.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  606.   XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
  607.   XtSetArg(args[num_args], XtNresizable, TRUE ); num_args++;
  608.   XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++;
  609.   search->label1 = XtCreateManagedWidget("label1", labelWidgetClass,
  610.                      form, args, num_args);
  611.  
  612.   num_args = 0;
  613.   XtSetArg(args[num_args], XtNfromVert, search->label1); num_args++;
  614.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  615.   XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
  616.   XtSetArg(args[num_args], XtNresizable, TRUE ); num_args++;
  617.   XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++;
  618.   search->label2 = XtCreateManagedWidget("label2", labelWidgetClass,
  619.                      form, args, num_args);
  620.   
  621. /* 
  622.  * We need to add R_OFFSET to the radio_data, because the value zero (0)
  623.  * has special meaning.
  624.  */
  625.  
  626.   num_args = 0;
  627.   XtSetArg(args[num_args], XtNlabel, "Backward"); num_args++;
  628.   XtSetArg(args[num_args], XtNfromVert, search->label2); num_args++;
  629.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  630.   XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
  631.   XtSetArg(args[num_args], XtNradioData, (caddr_t) XawsdLeft + R_OFFSET);
  632.   num_args++;
  633.   search->left_toggle = XtCreateManagedWidget("backwards", toggleWidgetClass, 
  634.                           form, args, num_args);
  635.  
  636.   num_args = 0;
  637.   XtSetArg(args[num_args], XtNlabel, "Forward"); num_args++;
  638.   XtSetArg(args[num_args], XtNfromVert, search->label2); num_args++;
  639.   XtSetArg(args[num_args], XtNfromHoriz, search->left_toggle); num_args++;
  640.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  641.   XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
  642.   XtSetArg(args[num_args], XtNradioGroup, search->left_toggle); num_args++;
  643.   XtSetArg(args[num_args], XtNradioData, (caddr_t) XawsdRight + R_OFFSET);
  644.   num_args++;
  645.   search->right_toggle = XtCreateManagedWidget("forwards", toggleWidgetClass, 
  646.                            form, args, num_args);
  647.  
  648.   {
  649.     XtTranslations radio_translations;
  650.  
  651.     radio_translations = XtParseTranslationTable(radio_trans_string);
  652.     XtOverrideTranslations(search->left_toggle, radio_translations);
  653.     XtOverrideTranslations(search->right_toggle, radio_translations);
  654.   }
  655.  
  656.   num_args = 0;
  657.   XtSetArg(args[num_args], XtNfromVert, search->left_toggle); num_args++;
  658.   XtSetArg(args[num_args], XtNlabel, "Search for:  ");num_args++;
  659.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  660.   XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
  661.   XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++;
  662.   s_label = XtCreateManagedWidget("searchLabel", labelWidgetClass,
  663.                   form, args, num_args);
  664.  
  665.   num_args = 0;
  666.   XtSetArg(args[num_args], XtNfromVert, search->left_toggle); num_args++;
  667.   XtSetArg(args[num_args], XtNfromHoriz, s_label); num_args++;
  668.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  669.   XtSetArg(args[num_args], XtNright, XtChainRight); num_args++;
  670.   XtSetArg(args[num_args], XtNeditType, XawtextEdit); num_args++;
  671.   XtSetArg(args[num_args], XtNresizable, TRUE); num_args++;
  672.   XtSetArg(args[num_args], XtNresize, XawtextResizeWidth); num_args++;
  673.   XtSetArg(args[num_args], XtNstring, ptr); num_args++;
  674.   s_text = XtCreateManagedWidget("searchText", asciiTextWidgetClass, form,
  675.                  args, num_args);
  676.   search->search_text = s_text;
  677.  
  678.   num_args = 0;
  679.   XtSetArg(args[num_args], XtNfromVert, s_text); num_args++;
  680.   XtSetArg(args[num_args], XtNlabel, "Replace with:");num_args++;
  681.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  682.   XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
  683.   XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++;
  684.   search->rep_label = XtCreateManagedWidget("replaceLabel", labelWidgetClass,
  685.                         form, args, num_args);
  686.  
  687.   num_args = 0;
  688.   XtSetArg(args[num_args], XtNfromHoriz, s_label); num_args++;
  689.   XtSetArg(args[num_args], XtNfromVert, s_text); num_args++;
  690.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  691.   XtSetArg(args[num_args], XtNright, XtChainRight); num_args++;
  692.   XtSetArg(args[num_args], XtNeditType, XawtextEdit); num_args++;
  693.   XtSetArg(args[num_args], XtNresizable, TRUE); num_args++;
  694.   XtSetArg(args[num_args], XtNresize, XawtextResizeWidth); num_args++;
  695.   XtSetArg(args[num_args], XtNstring, ""); num_args++;
  696.   r_text = XtCreateManagedWidget("replaceText", asciiTextWidgetClass,
  697.                  form, args, num_args);
  698.   search->rep_text = r_text;
  699.   
  700.   num_args = 0;
  701.   XtSetArg(args[num_args], XtNlabel, "Search"); num_args++;
  702.   XtSetArg(args[num_args], XtNfromVert, r_text); num_args++;
  703.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  704.   XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
  705.   search_button = XtCreateManagedWidget("search", commandWidgetClass, form,
  706.                     args, num_args);
  707.  
  708.   num_args = 0;
  709.   XtSetArg(args[num_args], XtNlabel, "Replace"); num_args++;
  710.   XtSetArg(args[num_args], XtNfromVert, r_text); num_args++;
  711.   XtSetArg(args[num_args], XtNfromHoriz, search_button); num_args++;
  712.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  713.   XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
  714.   search->rep_one = XtCreateManagedWidget("replaceOne", commandWidgetClass,
  715.                       form, args, num_args);
  716.  
  717.   num_args = 0;
  718.   XtSetArg(args[num_args], XtNlabel, "Replace All"); num_args++;
  719.   XtSetArg(args[num_args], XtNfromVert, r_text); num_args++;
  720.   XtSetArg(args[num_args], XtNfromHoriz, search->rep_one); num_args++;
  721.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  722.   XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
  723.   search->rep_all = XtCreateManagedWidget("replaceAll", commandWidgetClass,
  724.                       form, args, num_args);
  725.  
  726.   num_args = 0;
  727.   XtSetArg(args[num_args], XtNlabel, "Cancel"); num_args++;
  728.   XtSetArg(args[num_args], XtNfromVert, r_text); num_args++;
  729.   XtSetArg(args[num_args], XtNfromHoriz, search->rep_all); num_args++;
  730.   XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
  731.   XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
  732.   cancel = XtCreateManagedWidget(DISMISS_NAME, commandWidgetClass, form,
  733.                  args, num_args);
  734.  
  735.   XtAddCallback(search_button, XtNcallback, SearchButton, (XtPointer) search);
  736.   XtAddCallback(search->rep_one, XtNcallback, DoReplaceOne, (XtPointer) search);
  737.   XtAddCallback(search->rep_all, XtNcallback, DoReplaceAll, (XtPointer) search);
  738.   XtAddCallback(cancel, XtNcallback, PopdownSearch, (XtPointer) search);
  739.  
  740. /*
  741.  * Initialize the text entry fields.
  742.  */
  743.  
  744.   {
  745.     Pixel color;
  746.     num_args = 0;
  747.     XtSetArg(args[num_args], XtNbackground, &color); num_args++;
  748.     XtGetValues(search->rep_text, args, num_args);
  749.     num_args = 0;
  750.     XtSetArg(args[num_args], XtNborderColor, color); num_args++;
  751.     XtSetValues(search->rep_text, args, num_args);
  752.     XtSetKeyboardFocus(form, search->search_text);
  753.   }
  754.  
  755.   SetSearchLabels(search, SEARCH_LABEL_1, SEARCH_LABEL_2, FALSE);
  756.  
  757. /*
  758.  * Bind Extra translations.
  759.  */
  760.  
  761.   trans = XtParseTranslationTable(search_text_trans);
  762.   XtOverrideTranslations(search->search_text, trans);
  763.  
  764.   trans = XtParseTranslationTable(rep_text_trans);
  765.   XtOverrideTranslations(search->rep_text, trans);
  766. }
  767.  
  768. /*    Function Name: DoSearch
  769.  *    Description: Performs a search.
  770.  *    Arguments: search - the serach structure.
  771.  *    Returns: TRUE if sucessful.
  772.  */
  773.  
  774. /* ARGSUSED */
  775. static Boolean
  776. DoSearch(search)
  777. struct SearchAndReplace * search;
  778. {
  779.   char msg[BUFSIZ];
  780.   Widget tw = XtParent(search->search_popup);
  781.   XawTextPosition pos;
  782.   XawTextScanDirection dir;
  783.   XawTextBlock text;
  784.  
  785.   text.ptr = GetString(search->search_text);
  786.   text.length = strlen(text.ptr);
  787.   text.firstPos = 0;
  788.   text.format = FMT8BIT;
  789.   
  790.   dir = (XawTextScanDirection)(int) ((caddr_t)XawToggleGetCurrent(search->left_toggle) -
  791.                 R_OFFSET);
  792.   
  793.   pos = XawTextSearch( tw, dir, &text);
  794.   
  795.   if (pos == XawTextSearchError) 
  796.     sprintf( msg, "Could not find string '%s'.", text.ptr);
  797.   else {
  798.     if (dir == XawsdRight)
  799.       XawTextSetInsertionPoint( tw, pos + text.length);
  800.     else
  801.       XawTextSetInsertionPoint( tw, pos);
  802.     
  803.     XawTextSetSelection( tw, pos, pos + text.length);
  804.     search->selection_changed = FALSE; /* selection is good. */
  805.     return(TRUE);
  806.   }
  807.   
  808.   XawTextUnsetSelection(tw);
  809.   SetSearchLabels(search, msg, "", TRUE);
  810.   return(FALSE);
  811. }
  812.  
  813. /************************************************************
  814.  * 
  815.  * This section of the file contains all the functions that 
  816.  * the replace dialog box uses.
  817.  *
  818.  ************************************************************/
  819.  
  820. /*    Function Name: _XawTextDoReplaceAction
  821.  *    Description: Action routine that can be bound to dialog box's 
  822.  *                   Text Widget that will replace a string in the main 
  823.  *                   Text Widget.
  824.  *    Arguments:   (Standard Action Routine args) 
  825.  *    Returns:     none.
  826.  */
  827.  
  828. /* ARGSUSED */
  829. void 
  830. _XawTextDoReplaceAction(w, event, params, num_params)
  831. Widget w;
  832. XEvent *event;
  833. String * params;
  834. Cardinal * num_params;
  835. {
  836.   TextWidget ctx = (TextWidget) XtParent(XtParent(XtParent(w)));
  837.   Boolean popdown = FALSE;
  838.  
  839.   if ( (*num_params == 1) &&
  840.        ((params[0][0] == 'p') || (params[0][0] == 'P')) )
  841.     popdown = TRUE;
  842.  
  843.   if (Replace( ctx->text.search, TRUE, popdown) && popdown)
  844.     PopdownSearch(w, (XtPointer) ctx->text.search, NULL);
  845. }
  846.  
  847. /*    Function Name: DoReplaceOne
  848.  *    Description:  Replaces the first instance of the string
  849.  *                     in the search dialog's text widget
  850.  *                    with the one in the replace dialog's text widget.
  851.  *    Arguments: w - *** Not Used ***.
  852.  *                 closure - a pointer to the search structure.
  853.  *                 call_data - *** Not Used ***.
  854.  *    Returns: none.
  855.  */
  856.  
  857. /* ARGSUSED */
  858. static void 
  859. DoReplaceOne(w, closure, call_data)
  860. Widget w;            /* The Button Pressed. */
  861. XtPointer closure;        /* Text Widget. */
  862. XtPointer call_data;        /* unused */
  863. {
  864.   Replace( (struct SearchAndReplace *) closure, TRUE, FALSE);
  865. }
  866.  
  867. /*    Function Name: DoReplaceOne
  868.  *    Description:  Replaces every instance of the string
  869.  *                    in the search dialog's text widget
  870.  *                    with the one in the replace dialog's text widget.
  871.  *    Arguments: w - *** Not Used ***.
  872.  *                 closure - a pointer to the search structure.
  873.  *                 call_data - *** Not Used ***.
  874.  *    Returns: none.
  875.  */
  876.  
  877. /* ARGSUSED */
  878. static void 
  879. DoReplaceAll(w, closure, call_data)
  880. Widget w;            /* The Button Pressed. */
  881. XtPointer closure;        /* Text Widget. */
  882. XtPointer call_data;        /* unused */
  883. {
  884.   Replace( (struct SearchAndReplace *) closure, FALSE, FALSE);
  885. }
  886.  
  887. /*    Function Name: Replace
  888.  *    Description: This is the function that does the real work of
  889.  *                   replacing strings in the main text widget.
  890.  *    Arguments: tw - the Text Widget to replce the string in.
  891.  *                 once_only - If TRUE then only replace the first one found.
  892.  *                             other replace all of them.
  893.  *                 show_current - If true then leave the selection on the 
  894.  *                                string that was just replaced, otherwise
  895.  *                                move it onto the next one.
  896.  *    Returns: none.
  897.  */
  898.  
  899. static Boolean
  900. Replace(search, once_only, show_current)
  901. struct SearchAndReplace * search;
  902. Boolean once_only, show_current;
  903. {
  904.   XawTextPosition pos, new_pos, end_pos;
  905.   XawTextScanDirection dir;
  906.   XawTextBlock find, replace;
  907.   Widget tw = XtParent(search->search_popup);
  908.   int count = 0;
  909.   
  910.   find.ptr = GetString( search->search_text);
  911.   find.length = strlen(find.ptr);
  912.   find.firstPos = 0;
  913.   find.format = FMT8BIT;
  914.  
  915.   replace.ptr = GetString(search->rep_text);
  916.   replace.length = strlen(replace.ptr);
  917.   replace.firstPos = 0;
  918.   replace.format = FMT8BIT;
  919.     
  920.   dir = (XawTextScanDirection)(int) ((caddr_t)XawToggleGetCurrent(search->left_toggle) -
  921.                 R_OFFSET);
  922.  
  923.   while (TRUE) {
  924.     if (count != 0) {
  925.       new_pos = XawTextSearch( tw, dir, &find);
  926.       
  927.       if ( (new_pos == XawTextSearchError) ) {
  928.     if (count == 0) {
  929.       char msg[BUFSIZ];
  930.  
  931.       sprintf( msg, "%s %s %s", "*** Error: Could not find string '",
  932.           find.ptr, "'. ***");
  933.       SetSearchLabels(search, msg, "", TRUE);
  934.       return(FALSE);
  935.     }
  936.     else
  937.       break;
  938.       }
  939.       pos = new_pos;
  940.       end_pos = pos + find.length;
  941.     }
  942.     else {
  943.       XawTextGetSelectionPos(tw, &pos, &end_pos);
  944.  
  945.       if (search->selection_changed) {
  946.     SetSearchLabels(search, "Selection has been modified, aborting.",
  947.             "", TRUE);
  948.     return(FALSE);
  949.       }
  950.       if (pos == end_pos) 
  951.       return(FALSE);
  952.     }
  953.  
  954.     if (XawTextReplace(tw, pos, end_pos, &replace) != XawEditDone) {
  955.       char msg[BUFSIZ];
  956.       
  957.       sprintf( msg, "'%s' with '%s'. ***", find.ptr, replace.ptr);
  958.       SetSearchLabels(search, "*** Error while replacing", msg, TRUE);
  959.       return(FALSE);
  960.     }      
  961.  
  962.     if (dir == XawsdRight)
  963.       XawTextSetInsertionPoint( tw, pos + replace.length);
  964.     else
  965.       XawTextSetInsertionPoint( tw, pos);
  966.  
  967.     if (once_only) 
  968.       if (show_current)
  969.     break;
  970.       else {
  971.     DoSearch(search);
  972.     return(TRUE);
  973.       }
  974.     count++;
  975.   }
  976.  
  977.   if (replace.length == 0)
  978.     XawTextUnsetSelection(tw);
  979.   else
  980.     XawTextSetSelection( tw, pos, pos + replace.length);
  981.  
  982.   return(TRUE);
  983. }
  984.  
  985. /*    Function Name: SetSearchLabels
  986.  *    Description: Sets both the search labels, and also rings the bell
  987.  *    Arguments: search - the search structure.
  988.  *                 msg1, msg2 - message to put in each search label.
  989.  *                 bell - if TRUE then ring bell.
  990.  *    Returns: none.
  991.  */
  992.  
  993. static void
  994. SetSearchLabels(search, msg1, msg2, bell)
  995. struct SearchAndReplace * search;
  996. String msg1, msg2;
  997. Boolean bell;
  998. {
  999.   (void) SetResource( search->label1, XtNlabel, (XtArgVal) msg1);
  1000.   (void) SetResource( search->label2, XtNlabel, (XtArgVal) msg2);
  1001.   if (bell) 
  1002.     XBell(XtDisplay(search->search_popup), 0);
  1003. }
  1004.  
  1005. /************************************************************
  1006.  * 
  1007.  * This section of the file contains utility routines used by
  1008.  * other functions in this file.
  1009.  *
  1010.  ************************************************************/
  1011.  
  1012.  
  1013. /*    Function Name: _XawTextSetField
  1014.  *    Description: Action routine that can be bound to dialog box's 
  1015.  *                   Text Widget that will send input to the field specified.
  1016.  *    Arguments:   (Standard Action Routine args) 
  1017.  *    Returns:     none.
  1018.  */
  1019.  
  1020. /* ARGSUSED */
  1021. void 
  1022. _XawTextSetField(w, event, params, num_params)
  1023. Widget w;
  1024. XEvent *event;
  1025. String * params;
  1026. Cardinal * num_params;
  1027. {
  1028.   struct SearchAndReplace * search;
  1029.   Widget new, old;
  1030.  
  1031.   search = ((TextWidget) XtParent(XtParent(XtParent(w))))->text.search;
  1032.  
  1033.   if (*num_params != 1) {
  1034.     SetSearchLabels(search, "*** Error: SetField Action must have",
  1035.             "exactly one argument. ***", TRUE);
  1036.     return;
  1037.   }
  1038.   switch (params[0][0]) {
  1039.   case 's':
  1040.   case 'S':
  1041.     new = search->search_text;
  1042.     old = search->rep_text;
  1043.     break;
  1044.   case 'r':
  1045.   case 'R':
  1046.     old = search->search_text;
  1047.     new = search->rep_text;
  1048.     break;
  1049.   default:
  1050.     SetSearchLabels(search, "*** Error: SetField Action's first Argument must",
  1051.             "be either 'Search' or 'Replace'. ***", TRUE);
  1052.     return;
  1053.   }
  1054.   _SetField(new, old);
  1055. }
  1056.  
  1057. /*    Function Name: SetField
  1058.  *    Description: Sets the current text field.
  1059.  *    Arguments: new, old - new and old text fields.
  1060.  *    Returns: none
  1061.  */
  1062.  
  1063. static void
  1064. _SetField(new, old)
  1065. Widget new, old;
  1066. {
  1067.   Arg args[2];
  1068.   Pixel new_border, old_border, old_bg;
  1069.  
  1070.   if (!XtIsSensitive(new)) {
  1071.     XBell(XtDisplay(old), 0);    /* Don't set field to an inactive Widget. */
  1072.     return;
  1073.   }
  1074.  
  1075.   XtSetKeyboardFocus(XtParent(new), new); 
  1076.                         
  1077.   XtSetArg(args[0], XtNborderColor, &old_border);
  1078.   XtSetArg(args[1], XtNbackground, &old_bg);
  1079.   XtGetValues(new, args, TWO);
  1080.  
  1081.   XtSetArg(args[0], XtNborderColor, &new_border);
  1082.   XtGetValues(old, args, ONE);
  1083.  
  1084.   if (old_border != old_bg)    /* Colors are already correct, return. */
  1085.       return;
  1086.  
  1087.   SetResource(old, XtNborderColor, (XtArgVal) old_border);
  1088.   SetResource(new, XtNborderColor, (XtArgVal) new_border);
  1089. }
  1090.  
  1091. /*    Function Name: SetResourceByName
  1092.  *    Description: Sets a resource in any of the dialog children given
  1093.  *                   name of the child and the shell widget of the dialog.
  1094.  *    Arguments: shell - shell widget of the popup.
  1095.  *                 name - name of the child.
  1096.  *                 res_name - name of the resource.
  1097.  *                 value - the value of the resource.
  1098.  *    Returns: TRUE if sucessful.
  1099.  */
  1100.  
  1101. static Boolean
  1102. SetResourceByName(shell, name, res_name, value)
  1103. Widget shell;
  1104. char * name, * res_name;
  1105. XtArgVal value;
  1106. {
  1107.   Widget temp_widget;
  1108.   char buf[BUFSIZ];
  1109.  
  1110.   sprintf(buf, "%s.%s", FORM_NAME, name);
  1111.  
  1112.   if ( (temp_widget = XtNameToWidget(shell, buf)) != NULL) {
  1113.     SetResource(temp_widget, res_name, value);
  1114.     return(TRUE);
  1115.   }
  1116.   return(FALSE);
  1117. }
  1118.  
  1119. /*    Function Name: SetResource
  1120.  *    Description: Sets a resource in a widget
  1121.  *    Arguments: w - the widget.
  1122.  *                 res_name - name of the resource.
  1123.  *                 value - the value of the resource.
  1124.  *    Returns: none.
  1125.  */
  1126.  
  1127. static void
  1128. SetResource(w, res_name, value)
  1129. Widget w;
  1130. char * res_name;
  1131. XtArgVal value;
  1132. {
  1133.   Arg args[1];
  1134.   
  1135.   XtSetArg(args[0], res_name, value);
  1136.   XtSetValues( w, args, ONE );
  1137. }
  1138.  
  1139. /*    Function Name: GetString
  1140.  *    Description:   Gets the value for the string in the popup.
  1141.  *    Arguments:     text - the text widget whose string we will get.
  1142.  *    Returns:       the string.
  1143.  */
  1144.  
  1145. static String
  1146. GetString(text)
  1147. Widget text;
  1148. {
  1149.   String string;
  1150.   Arg args[1];
  1151.  
  1152.   XtSetArg( args[0], XtNstring, &string );
  1153.   XtGetValues( text, args, ONE );
  1154.   return(string);
  1155. }
  1156.  
  1157. /*    Function Name: CenterWidgetOnPoint.
  1158.  *    Description: Centers a shell widget on a point relative to
  1159.  *                   the root window.
  1160.  *    Arguments: w - the shell widget.
  1161.  *                 event - event containing the location of the point
  1162.  *    Returns: none.
  1163.  *
  1164.  * NOTE: The widget is not allowed to go off the screen.
  1165.  */
  1166.  
  1167. static void
  1168. CenterWidgetOnPoint(w, event)
  1169. Widget w;
  1170. XEvent *event;
  1171. {
  1172.   Arg args[3];
  1173.   Cardinal num_args;
  1174.   Dimension width, height, b_width;
  1175.   Position x, y, max_x, max_y;
  1176.   
  1177.   if (event != NULL) {
  1178.     switch (event->type) {
  1179.     case ButtonPress:
  1180.     case ButtonRelease:
  1181.       x = event->xbutton.x_root;
  1182.       y = event->xbutton.y_root;
  1183.       break;
  1184.     case KeyPress:
  1185.     case KeyRelease:
  1186.       x = event->xkey.x_root;
  1187.       y = event->xkey.y_root;
  1188.       break;
  1189.     default:
  1190.       return;
  1191.     }
  1192.   }
  1193.   
  1194.   num_args = 0;
  1195.   XtSetArg(args[num_args], XtNwidth, &width); num_args++;
  1196.   XtSetArg(args[num_args], XtNheight, &height); num_args++;
  1197.   XtSetArg(args[num_args], XtNborderWidth, &b_width); num_args++;
  1198.   XtGetValues(w, args, num_args);
  1199.  
  1200.   width += 2 * b_width;
  1201.   height += 2 * b_width;
  1202.  
  1203.   x -= ( (Position) width/2 );
  1204.   if (x < 0) x = 0;
  1205.   if ( x > (max_x = (Position) (XtScreen(w)->width - width)) ) x = max_x;
  1206.  
  1207.   y -= ( (Position) height/2 );
  1208.   if (y < 0) y = 0;
  1209.   if ( y > (max_y = (Position) (XtScreen(w)->height - height)) ) y = max_y;
  1210.   
  1211.   num_args = 0;
  1212.   XtSetArg(args[num_args], XtNx, x); num_args++;
  1213.   XtSetArg(args[num_args], XtNy, y); num_args++;
  1214.   XtSetValues(w, args, num_args);
  1215. }
  1216.  
  1217. /*    Function Name: CreateDialog
  1218.  *    Description: Actually creates a dialog.
  1219.  *    Arguments: parent - the parent of the dialog - the main text widget.
  1220.  *                 ptr - initial_string for the dialog.
  1221.  *                 name - name of the dialog.
  1222.  *                 func - function to create the children of the dialog.
  1223.  *    Returns: the popup shell of the dialog.
  1224.  * 
  1225.  * NOTE:
  1226.  *
  1227.  * The function argument is passed the following arguements.
  1228.  *
  1229.  * form - the from widget that is the dialog.
  1230.  * ptr - the initial string for the dialog's text widget.
  1231.  * parent - the parent of the dialog - the main text widget.
  1232.  */
  1233.  
  1234. static Widget
  1235. CreateDialog(parent, ptr, name, func)
  1236. Widget parent;
  1237. String ptr, name;
  1238. void (*func)();
  1239. {
  1240.   Widget popup, form;
  1241.   Arg args[5];
  1242.   Cardinal num_args;
  1243.  
  1244.   num_args = 0;
  1245.   XtSetArg(args[num_args], XtNiconName, name); num_args++;
  1246.   XtSetArg(args[num_args], XtNgeometry, NULL); num_args++;
  1247.   XtSetArg(args[num_args], XtNallowShellResize, TRUE); num_args++;
  1248.   XtSetArg(args[num_args], XtNtransientFor, GetShell(parent)); num_args++;
  1249.   popup = XtCreatePopupShell(name, transientShellWidgetClass, 
  1250.                  parent, args, num_args);
  1251.   
  1252.   form = XtCreateManagedWidget(FORM_NAME, formWidgetClass, popup,
  1253.                    NULL, ZERO);
  1254.  
  1255.   (*func) (form, ptr, parent);
  1256.   return(popup);
  1257. }
  1258.  
  1259.  /*    Function Name: GetShell
  1260.   *     Description: Walks up the widget hierarchy to find the
  1261.   *         nearest shell widget.
  1262.   *     Arguments: w - the widget whose parent shell should be returned.
  1263.   *     Returns: The shell widget among the ancestors of w that is the
  1264.   *         fewest levels up in the widget hierarchy.
  1265.   */
  1266.  
  1267. static Widget
  1268. GetShell(w)
  1269. Widget w;
  1270. {
  1271.     while ((w != NULL) && !XtIsShell(w))
  1272.     w = XtParent(w);
  1273.     
  1274.     return (w);
  1275. }
  1276.  
  1277. static Boolean InParams(str, p, n)
  1278.     String str;
  1279.     String *p;
  1280.     Cardinal n;
  1281. {
  1282.     int i;
  1283.     for (i=0; i < n; p++, i++)
  1284.     if (! XmuCompareISOLatin1(*p, str)) return True;
  1285.     return False;
  1286. }
  1287.  
  1288. static char *WM_DELETE_WINDOW = "WM_DELETE_WINDOW";
  1289.  
  1290. static void WMProtocols(w, event, params, num_params)
  1291.     Widget w;        /* popup shell */
  1292.     XEvent *event;
  1293.     String *params;
  1294.     Cardinal *num_params;
  1295. {
  1296.     Atom wm_delete_window;
  1297.     Atom wm_protocols;
  1298.  
  1299.     wm_delete_window = XInternAtom(XtDisplay(w), WM_DELETE_WINDOW, True);
  1300.     wm_protocols = XInternAtom(XtDisplay(w), "WM_PROTOCOLS", True);
  1301.  
  1302.     /* Respond to a recognized WM protocol request iff 
  1303.      * event type is ClientMessage and no parameters are passed, or
  1304.      * event type is ClientMessage and event data is matched to parameters, or
  1305.      * event type isn't ClientMessage and parameters make a request.
  1306.      */
  1307. #define DO_DELETE_WINDOW InParams(WM_DELETE_WINDOW, params, *num_params)
  1308.  
  1309.     if ((event->type == ClientMessage &&
  1310.      event->xclient.message_type == wm_protocols &&
  1311.      event->xclient.data.l[0] == wm_delete_window &&
  1312.      (*num_params == 0 || DO_DELETE_WINDOW))
  1313.     || 
  1314.     (event->type != ClientMessage && DO_DELETE_WINDOW)) {
  1315.  
  1316. #undef DO_DELETE_WINDOW
  1317.  
  1318.     Widget cancel;
  1319.     char descendant[DISMISS_NAME_LEN + 2];
  1320.     sprintf(descendant, "*%s", DISMISS_NAME);
  1321.     cancel = XtNameToWidget(w, descendant);
  1322.     if (cancel) XtCallCallbacks(cancel, XtNcallback, (XtPointer)NULL);
  1323.     }
  1324. }
  1325.  
  1326. static void SetWMProtocolTranslations(w)
  1327.     Widget    w;    /* realized popup shell */
  1328. {
  1329.     int i;
  1330.     XtAppContext app_context;
  1331.     Atom wm_delete_window;
  1332.     static XtTranslations compiled_table;    /* initially 0 */
  1333.     static XtAppContext *app_context_list;    /* initially 0 */
  1334.     static Cardinal list_size;            /* initially 0 */
  1335.  
  1336.     app_context = XtWidgetToApplicationContext(w);
  1337.  
  1338.     /* parse translation table once */
  1339.     if (! compiled_table) compiled_table = XtParseTranslationTable
  1340.     ("<Message>WM_PROTOCOLS: XawWMProtocols()\n");
  1341.  
  1342.     /* add actions once per application context */
  1343.     for (i=0; i < list_size && app_context_list[i] != app_context; i++) ;
  1344.     if (i == list_size) {
  1345.     XtActionsRec actions[1];
  1346.     actions[0].string = "XawWMProtocols";
  1347.     actions[0].proc = WMProtocols;
  1348.     list_size++;
  1349.     app_context_list = (XtAppContext *) XtRealloc
  1350.         ((char *)app_context_list, list_size * sizeof(XtAppContext));
  1351.     XtAppAddActions(app_context, actions, 1);
  1352.     app_context_list[i] = app_context;
  1353.     }
  1354.  
  1355.     /* establish communication between the window manager and each shell */
  1356.     XtAugmentTranslations(w, compiled_table);
  1357.     wm_delete_window = XInternAtom(XtDisplay(w), WM_DELETE_WINDOW, False);
  1358.     (void) XSetWMProtocols(XtDisplay(w), XtWindow(w), &wm_delete_window, 1);
  1359. }
  1360.