1 #if (!defined(lint) && !defined(SABER))
2 static char Xrcsid
[] = "$XConsortium: TextPop.c,v 1.10 90/02/01 16:21:22 kit Exp $";
3 #endif /* lint && SABER */
5 /***********************************************************
6 Copyright 1989 by the Massachusetts Institute of Technology,
7 Cambridge, Massachusetts.
11 Permission to use, copy, modify, and distribute this software and its
12 documentation for any purpose and without fee is hereby granted,
13 provided that the above copyright notice appear in all copies and that
14 both that copyright notice and this permission notice appear in
15 supporting documentation, and that the names of Digital or MIT not be
16 used in advertising or publicity pertaining to distribution of the
17 software without specific, written prior permission.
19 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
20 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
21 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
22 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
27 ******************************************************************/
29 /************************************************************
31 * This file is broken up into three sections one dealing with
32 * each of the three popups created here:
34 * FileInsert, Search, and Replace.
36 * There is also a section at the end for utility functions
37 * used by all more than one of these dialogs.
39 * The following functions are the only non-static ones defined
40 * in this module. They are located at the begining of the
41 * section that contains this dialog box that uses them.
43 * void _XawTextInsertFileAction(w, event, params, num_params);
44 * void _XawTextDoSearchAction(w, event, params, num_params);
45 * void _XawTextDoReplaceAction(w, event, params, num_params);
46 * void _XawTextInsertFile(w, event, params, num_params);
48 *************************************************************/
51 #include <sys/fcntl.h>
52 #include <X11/StringDefs.h>
53 #include <X11/IntrinsicP.h>
54 #include <X11/Shell.h>
56 #include <./Xaw3_1TextP.h>
57 #include <./Xaw3_1AsciiText.h>
58 #include <./Xaw3_1Cardinals.h>
59 #include <./Xaw3_1Command.h>
60 #include <./Xaw3_1Form.h>
61 #include <./Xaw3_1Toggle.h>
63 extern int errno
, sys_nerr
;
64 extern char* sys_errlist
[];
66 #define INSERT_FILE ("Enter Filename:")
68 #define SEARCH_LABEL_1 ("Use <Tab> to change fields.")
69 #define SEARCH_LABEL_2 ("Use ^q<Tab> for <Tab>.")
70 #define FORM_NAME ("form")
71 #define LABEL_NAME ("label")
72 #define TEXT_NAME ("text")
76 static void CenterWidgetOnPoint(), PopdownSearch(), DoInsert(), _SetField();
77 static void InitializeSearchWidget(), SetResource(), SetSearchLabels();
78 static void DoReplaceOne(), DoReplaceAll();
79 static Widget
CreateDialog();
80 static Boolean
DoSearch(), SetResourceByName(), Replace();
81 static String
GetString();
83 static char radio_trans_string
[] =
84 "<Btn1Down>,<Btn1Up>: set() notify()";
86 static char search_text_trans
[] =
87 "~Shift<Key>Return: DoSearchAction(Popdown) \n\
88 Shift<Key>Return: DoSearchAction() SetField(Replace) \n\
89 Ctrl<Key>q,<Key>Tab: insert-char() \n\
90 Ctrl<Key>c: PopdownSearchAction() \n\
91 <Btn1Down>: select-start() SetField(Search) \n\
92 <Key>Tab: DoSearchAction() SetField(Replace)";
94 static char rep_text_trans
[] =
95 "~Shift<Key>Return: DoReplaceAction(Popdown) \n\
96 Shift<Key>Return: SetField(Search) \n\
97 Ctrl<Key>q,<Key>Tab: insert-char() \n\
98 Ctrl<Key>c: PopdownSearchAction() \n\
99 <Btn1Down>: select-start() DoSearchAction() SetField(Replace)\n\
100 <Key>Tab: SetField(Search)";
102 /************************************************************
104 * This section of the file contains all the functions that
105 * the file insert dialog box uses.
107 ************************************************************/
109 /* Function Name: _XawTextInsertFileAction
110 * Description: Action routine that can be bound to dialog box's
111 * Text Widget that will insert a file into the main
113 * Arguments: (Standard Action Routine args)
119 _XawTextInsertFileAction(w
, event
, params
, num_params
)
123 Cardinal
* num_params
;
125 DoInsert(w
, (caddr_t
) XtParent(XtParent(XtParent(w
))), NULL
);
128 /* Function Name: _XawTextInsertFile
129 * Description: Action routine that can be bound to the text widget
130 * it will popup the insert file dialog box.
131 * Arguments: w - the text widget.
132 * event - X Event (used to get x and y location).
133 * params, num_params - the parameter list.
138 * The parameter list may contain one entry.
140 * Entry: This entry is optional and contains the value of the default
144 static void AddInsertFileChildren(Widget
, char*, Widget
);
147 _XawTextInsertFile(w
, event
, params
, num_params
)
151 Cardinal
* num_params
;
153 TextWidget ctx
= (TextWidget
)w
;
155 XawTextEditType edit_mode
;
158 XtSetArg(args
[0], XtNeditType
,&edit_mode
);
159 XtGetValues(ctx
->text
.source
, args
, ONE
);
161 if (edit_mode
!= XawtextEdit
) {
162 XBell(XtDisplay(w
), 0);
166 if (*num_params
== 0)
171 if (!ctx
->text
.file_insert
) {
172 ctx
->text
.file_insert
= CreateDialog(w
, ptr
, "insertFile",
173 AddInsertFileChildren
);
174 XtRealizeWidget(ctx
->text
.file_insert
);
177 CenterWidgetOnPoint(ctx
->text
.file_insert
, event
);
178 XtPopup(ctx
->text
.file_insert
, XtGrabNone
);
181 /* Function Name: PopdownFileInsert
182 * Description: Pops down the file insert button.
183 * Arguments: w - the widget that caused this action.
184 * closure - a pointer to the main text widget that
185 * popped up this dialog.
186 * call_data - *** NOT USED ***.
192 PopdownFileInsert(w
, closure
, call_data
)
193 Widget w
; /* The Dialog Button Pressed. */
194 caddr_t closure
; /* Text Widget. */
195 caddr_t call_data
; /* unused */
197 TextWidget ctx
= (TextWidget
) closure
;
199 XtPopdown( ctx
->text
.file_insert
);
200 (void) SetResourceByName( ctx
->text
.file_insert
, LABEL_NAME
,
201 XtNlabel
, (XtArgVal
) INSERT_FILE
);
204 /* Function Name: DoInsert
205 * Description: Actually insert the file named in the text widget
206 * of the file dialog.
207 * Arguments: w - the widget that activated this callback.
208 * closure - a pointer to the text widget to insert the
212 static Boolean
InsertFileNamed(Widget
, char *);
216 DoInsert(w
, closure
, call_data
)
217 Widget w
; /* The Dialog Button Pressed. */
218 caddr_t closure
; /* Text Widget */
219 caddr_t call_data
; /* unused */
221 TextWidget ctx
= (TextWidget
) closure
;
222 char buf
[BUFSIZ
], msg
[BUFSIZ
];
225 sprintf(buf
, "%s.%s", FORM_NAME
, TEXT_NAME
);
226 if ( (temp_widget
= XtNameToWidget(ctx
->text
.file_insert
, buf
)) == NULL
) {
228 "*** Error: Could not get text widget from file insert popup");
231 if (InsertFileNamed( (Widget
) ctx
, GetString( temp_widget
))) {
232 PopdownFileInsert(w
, closure
, call_data
);
236 sprintf( msg
, "*** Error: %s ***",
237 (errno
> 0 && errno
< sys_nerr
) ?
238 sys_errlist
[errno
] : "Can't open file" );
241 (void)SetResourceByName(ctx
->text
.file_insert
,
242 LABEL_NAME
, XtNlabel
, (XtArgVal
) msg
);
243 XBell(XtDisplay(w
), 0);
246 /* Function Name: InsertFileNamed
247 * Description: Inserts a file into the text widget.
248 * Arguments: tw - The text widget to insert this file into.
249 * str - name of the file to insert.
250 * Returns: TRUE if the insert was sucessful, FALSE otherwise.
254 InsertFileNamed(tw
, str
)
261 XawTextPosition start_pos
, pos
;
263 if ( (str
== NULL
) || (strlen(str
) == 0) ||
264 ((fid
= open(str
, O_RDONLY
)) <= 0))
267 start_pos
= pos
= XawTextGetInsertionPoint(tw
);
269 text
.format
= FMT8BIT
;
271 while ((text
.length
= read(fid
, buf
, BUFSIZ
)) > 0) {
273 if (XawTextReplace(tw
, pos
, pos
, &text
) != XawEditDone
) {
275 * If the replace failed then remove what we have
276 * replaced so far, and return an error.
279 (void) XawTextReplace(tw
, start_pos
, pos
, &text
);
286 XawTextSetInsertionPoint(tw
, pos
);
290 /* Function Name: AddInsertFileChildren
291 * Description: Adds all children to the InsertFile dialog widget.
292 * Arguments: form - the form widget for the insert dialog widget.
293 * ptr - a pointer to the initial string for the Text Widget.
294 * tw - the main text widget.
299 AddInsertFileChildren(form
, ptr
, tw
)
305 Widget label
, text
, cancel
, insert
;
306 XtTranslations trans
;
309 XtSetArg(args
[num_args
], XtNlabel
, INSERT_FILE
);num_args
++;
310 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
311 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
312 XtSetArg(args
[num_args
], XtNresizable
, TRUE
); num_args
++;
313 XtSetArg(args
[num_args
], XtNborderWidth
, 0 ); num_args
++;
314 label
= XtCreateManagedWidget(LABEL_NAME
, labelWidgetClass
, form
,
318 XtSetArg(args
[num_args
], XtNfromVert
, label
); num_args
++;
319 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
320 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
321 XtSetArg(args
[num_args
], XtNeditType
, XawtextEdit
); num_args
++;
322 XtSetArg(args
[num_args
], XtNresizable
, TRUE
); num_args
++;
323 XtSetArg(args
[num_args
], XtNresize
, XawtextResizeWidth
); num_args
++;
324 XtSetArg(args
[num_args
], XtNstring
, ptr
); num_args
++;
325 text
= XtCreateManagedWidget(TEXT_NAME
, asciiTextWidgetClass
, form
,
329 XtSetArg(args
[num_args
], XtNlabel
, "Insert File"); num_args
++;
330 XtSetArg(args
[num_args
], XtNfromVert
, text
); num_args
++;
331 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
332 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
333 insert
= XtCreateManagedWidget("insert", commandWidgetClass
, form
,
337 XtSetArg(args
[num_args
], XtNlabel
, "Cancel"); num_args
++;
338 XtSetArg(args
[num_args
], XtNfromVert
, text
); num_args
++;
339 XtSetArg(args
[num_args
], XtNfromHoriz
, insert
); num_args
++;
340 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
341 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
342 cancel
= XtCreateManagedWidget("cancel", commandWidgetClass
, form
,
345 XtAddCallback(cancel
, XtNcallback
, (XtCallbackProc
)PopdownFileInsert
,
347 XtAddCallback(insert
, XtNcallback
, (XtCallbackProc
)DoInsert
, (caddr_t
) tw
);
349 XtSetKeyboardFocus(form
, text
);
352 * Bind <CR> to insert file.
355 trans
= XtParseTranslationTable("<Key>Return: InsertFileAction()");
356 XtOverrideTranslations(text
, trans
);
360 /************************************************************
362 * This section of the file contains all the functions that
363 * the search dialog box uses.
365 ************************************************************/
367 /* Function Name: _XawTextDoSearchAction
368 * Description: Action routine that can be bound to dialog box's
369 * Text Widget that will search for a string in the main
371 * Arguments: (Standard Action Routine args)
376 * If the search was sucessful and the argument popdown is passed to
377 * this action routine then the widget will automatically popdown the
383 _XawTextDoSearchAction(w
, event
, params
, num_params
)
387 Cardinal
* num_params
;
389 TextWidget tw
= (TextWidget
) XtParent(XtParent(XtParent(w
)));
390 Boolean popdown
= FALSE
;
392 if ( (*num_params
== 1) &&
393 ((params
[0][0] == 'p') || (params
[0][0] == 'P')) )
396 if (DoSearch(tw
->text
.search
) && popdown
)
397 PopdownSearch(w
, (caddr_t
) tw
->text
.search
, NULL
);
400 /* Function Name: _XawTextPopdownSearchAction
401 * Description: Action routine that can be bound to dialog box's
402 * Text Widget that will popdown the search widget.
403 * Arguments: (Standard Action Routine args)
409 _XawTextPopdownSearchAction(w
, event
, params
, num_params
)
413 Cardinal
* num_params
;
415 TextWidget tw
= (TextWidget
) XtParent(XtParent(XtParent(w
)));
417 PopdownSearch(w
, (caddr_t
) tw
->text
.search
, NULL
);
420 /* Function Name: PopdownSeach
421 * Description: Pops down the search widget and resets it.
422 * Arguments: w - *** NOT USED ***.
423 * closure - a pointer to the search structure.
424 * call_data - *** NOT USED ***.
430 PopdownSearch(w
, closure
, call_data
)
435 struct SearchAndReplace
* search
= (struct SearchAndReplace
*) closure
;
437 XtPopdown( search
->search_popup
);
438 SetSearchLabels(search
, SEARCH_LABEL_1
, SEARCH_LABEL_2
, FALSE
);
441 /* Function Name: SearchButton
442 * Description: Performs a search when the button is clicked.
443 * Arguments: w - *** NOT USED **.
444 * closure - a pointer to the search info.
445 * call_data - *** NOT USED ***.
451 SearchButton(w
, closure
, call_data
)
456 (void) DoSearch( (struct SearchAndReplace
*) closure
);
459 /* Function Name: _XawTextSearch
460 * Description: Action routine that can be bound to the text widget
461 * it will popup the search dialog box.
462 * Arguments: w - the text widget.
463 * event - X Event (used to get x and y location).
464 * params, num_params - the parameter list.
469 * The parameter list contains one or two entries that may be the following.
471 * First Entry: The first entry is the direction to search by default.
472 * This arguement must be specified and may have a value of
475 * Second Entry: This entry is optional and contains the value of the default
476 * string to search for.
479 #define SEARCH_HEADER ("Text Widget - Search():")
481 static void AddSearchChildren(Widget
, char *, Widget
);
484 _XawTextSearch(w
, event
, params
, num_params
)
488 Cardinal
* num_params
;
490 TextWidget ctx
= (TextWidget
)w
;
491 XawTextScanDirection dir
;
492 char * ptr
, buf
[BUFSIZ
];
493 XawTextEditType edit_mode
;
497 if (ctx
->text
.source
->Search
== NULL
) {
498 XBell(XtDisplay(w
), 0);
503 if ( (*num_params
< 1) || (*num_params
> 2) ) {
504 sprintf(buf
, "%s %s\n%s", SEARCH_HEADER
, "This action must have only",
505 "one or two parameters");
506 XtAppWarning(XtWidgetToApplicationContext(w
), buf
);
509 else if (*num_params
== 1)
514 switch(params
[0][0]) {
515 case 'b': /* Left. */
519 case 'f': /* Right. */
524 sprintf(buf
, "%s %s\n%s", SEARCH_HEADER
, "The first parameter must be",
525 "Either 'backward' or 'forward'");
526 XtAppWarning(XtWidgetToApplicationContext(w
), buf
);
530 if (ctx
->text
.search
== NULL
) {
531 ctx
->text
.search
= XtNew(struct SearchAndReplace
);
532 ctx
->text
.search
->search_popup
= CreateDialog(w
, ptr
, "search",
534 XtRealizeWidget(ctx
->text
.search
->search_popup
);
537 XtSetArg(args
[0], XtNeditType
,&edit_mode
);
538 XtGetValues(ctx
->text
.source
, args
, ONE
);
540 InitializeSearchWidget(ctx
->text
.search
, dir
, (edit_mode
== XawtextEdit
));
542 CenterWidgetOnPoint(ctx
->text
.search
->search_popup
, event
);
543 XtPopup(ctx
->text
.search
->search_popup
, XtGrabNone
);
546 /* Function Name: InitializeSearchWidget
547 * Description: This function initializes the search widget and
548 * is called each time the search widget is poped up.
549 * Arguments: search - the search widget structure.
550 * dir - direction to search.
551 * replace_active - state of the sensitivity for the
557 InitializeSearchWidget(search
, dir
, replace_active
)
558 struct SearchAndReplace
* search
;
559 XawTextScanDirection dir
;
560 Boolean replace_active
;
562 SetResource(search
->rep_one
, XtNsensitive
, (XtArgVal
) replace_active
);
563 SetResource(search
->rep_all
, XtNsensitive
, (XtArgVal
) replace_active
);
564 SetResource(search
->rep_label
, XtNsensitive
, (XtArgVal
) replace_active
);
565 SetResource(search
->rep_text
, XtNsensitive
, (XtArgVal
) replace_active
);
569 SetResource(search
->left_toggle
, XtNstate
, (XtArgVal
) TRUE
);
572 SetResource(search
->right_toggle
, XtNstate
, (XtArgVal
) TRUE
);
579 /* Function Name: AddSearchChildren
580 * Description: Adds all children to the Search Dialog Widget.
581 * Arguments: form - the form widget for the search widget.
582 * ptr - a pointer to the initial string for the Text Widget.
583 * tw - the main text widget.
588 AddSearchChildren(form
, ptr
, tw
)
594 Widget cancel
, search_button
, s_label
, s_text
, r_text
;
595 XtTranslations trans
;
596 struct SearchAndReplace
* search
= ((TextWidget
) tw
)->text
.search
;
599 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
600 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
601 XtSetArg(args
[num_args
], XtNresizable
, TRUE
); num_args
++;
602 XtSetArg(args
[num_args
], XtNborderWidth
, 0 ); num_args
++;
603 search
->label1
= XtCreateManagedWidget("label1", labelWidgetClass
,
604 form
, args
, num_args
);
607 XtSetArg(args
[num_args
], XtNfromVert
, search
->label1
); num_args
++;
608 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
609 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
610 XtSetArg(args
[num_args
], XtNresizable
, TRUE
); num_args
++;
611 XtSetArg(args
[num_args
], XtNborderWidth
, 0 ); num_args
++;
612 search
->label2
= XtCreateManagedWidget("label2", labelWidgetClass
,
613 form
, args
, num_args
);
616 * We need to add R_OFFSET to the radio_data, because the value zero (0)
617 * has special meaning.
621 XtSetArg(args
[num_args
], XtNlabel
, "Backward"); num_args
++;
622 XtSetArg(args
[num_args
], XtNfromVert
, search
->label2
); num_args
++;
623 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
624 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
625 XtSetArg(args
[num_args
], XtNradioData
, (caddr_t
) XawsdLeft
+ R_OFFSET
);
627 search
->left_toggle
= XtCreateManagedWidget("backwards", toggleWidgetClass
,
628 form
, args
, num_args
);
631 XtSetArg(args
[num_args
], XtNlabel
, "Forward"); num_args
++;
632 XtSetArg(args
[num_args
], XtNfromVert
, search
->label2
); num_args
++;
633 XtSetArg(args
[num_args
], XtNfromHoriz
, search
->left_toggle
); num_args
++;
634 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
635 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
636 XtSetArg(args
[num_args
], XtNradioGroup
, search
->left_toggle
); num_args
++;
637 XtSetArg(args
[num_args
], XtNradioData
, (caddr_t
) XawsdRight
+ R_OFFSET
);
639 search
->right_toggle
= XtCreateManagedWidget("forwards", toggleWidgetClass
,
640 form
, args
, num_args
);
643 XtTranslations radio_translations
;
645 radio_translations
= XtParseTranslationTable(radio_trans_string
);
646 XtOverrideTranslations(search
->left_toggle
, radio_translations
);
647 XtOverrideTranslations(search
->right_toggle
, radio_translations
);
651 XtSetArg(args
[num_args
], XtNfromVert
, search
->left_toggle
); num_args
++;
652 XtSetArg(args
[num_args
], XtNlabel
, "Search for: ");num_args
++;
653 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
654 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
655 XtSetArg(args
[num_args
], XtNborderWidth
, 0 ); num_args
++;
656 s_label
= XtCreateManagedWidget("searchLabel", labelWidgetClass
,
657 form
, args
, num_args
);
660 XtSetArg(args
[num_args
], XtNfromVert
, search
->left_toggle
); num_args
++;
661 XtSetArg(args
[num_args
], XtNfromHoriz
, s_label
); num_args
++;
662 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
663 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
664 XtSetArg(args
[num_args
], XtNeditType
, XawtextEdit
); num_args
++;
665 XtSetArg(args
[num_args
], XtNresizable
, TRUE
); num_args
++;
666 XtSetArg(args
[num_args
], XtNresize
, XawtextResizeWidth
); num_args
++;
667 XtSetArg(args
[num_args
], XtNstring
, ptr
); num_args
++;
668 s_text
= XtCreateManagedWidget("searchText", asciiTextWidgetClass
, form
,
670 search
->search_text
= s_text
;
673 XtSetArg(args
[num_args
], XtNfromVert
, s_text
); num_args
++;
674 XtSetArg(args
[num_args
], XtNlabel
, "Replace with:");num_args
++;
675 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
676 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
677 XtSetArg(args
[num_args
], XtNborderWidth
, 0 ); num_args
++;
678 search
->rep_label
= XtCreateManagedWidget("replaceLabel", labelWidgetClass
,
679 form
, args
, num_args
);
682 XtSetArg(args
[num_args
], XtNfromHoriz
, s_label
); num_args
++;
683 XtSetArg(args
[num_args
], XtNfromVert
, s_text
); num_args
++;
684 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
685 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
686 XtSetArg(args
[num_args
], XtNeditType
, XawtextEdit
); num_args
++;
687 XtSetArg(args
[num_args
], XtNresizable
, TRUE
); num_args
++;
688 XtSetArg(args
[num_args
], XtNresize
, XawtextResizeWidth
); num_args
++;
689 XtSetArg(args
[num_args
], XtNstring
, ""); num_args
++;
690 r_text
= XtCreateManagedWidget("replaceText", asciiTextWidgetClass
,
691 form
, args
, num_args
);
692 search
->rep_text
= r_text
;
695 XtSetArg(args
[num_args
], XtNlabel
, "Search"); num_args
++;
696 XtSetArg(args
[num_args
], XtNfromVert
, r_text
); num_args
++;
697 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
698 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
699 search_button
= XtCreateManagedWidget("search", commandWidgetClass
, form
,
703 XtSetArg(args
[num_args
], XtNlabel
, "Replace"); num_args
++;
704 XtSetArg(args
[num_args
], XtNfromVert
, r_text
); num_args
++;
705 XtSetArg(args
[num_args
], XtNfromHoriz
, search_button
); num_args
++;
706 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
707 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
708 search
->rep_one
= XtCreateManagedWidget("replaceOne", commandWidgetClass
,
709 form
, args
, num_args
);
712 XtSetArg(args
[num_args
], XtNlabel
, "Replace All"); num_args
++;
713 XtSetArg(args
[num_args
], XtNfromVert
, r_text
); num_args
++;
714 XtSetArg(args
[num_args
], XtNfromHoriz
, search
->rep_one
); num_args
++;
715 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
716 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
717 search
->rep_all
= XtCreateManagedWidget("replaceAll", commandWidgetClass
,
718 form
, args
, num_args
);
721 XtSetArg(args
[num_args
], XtNlabel
, "Cancel"); num_args
++;
722 XtSetArg(args
[num_args
], XtNfromVert
, r_text
); num_args
++;
723 XtSetArg(args
[num_args
], XtNfromHoriz
, search
->rep_all
); num_args
++;
724 XtSetArg(args
[num_args
], XtNleft
, XtChainLeft
); num_args
++;
725 XtSetArg(args
[num_args
], XtNright
, XtChainLeft
); num_args
++;
726 cancel
= XtCreateManagedWidget("cancel", commandWidgetClass
, form
,
729 XtAddCallback(search_button
, XtNcallback
, (XtCallbackProc
)SearchButton
,
731 XtAddCallback(search
->rep_one
, XtNcallback
, (XtCallbackProc
)DoReplaceOne
,
733 XtAddCallback(search
->rep_all
, XtNcallback
, (XtCallbackProc
)DoReplaceAll
,
735 XtAddCallback(cancel
, XtNcallback
, (XtCallbackProc
)PopdownSearch
,
739 * Initialize the text entry fields.
745 XtSetArg(args
[num_args
], XtNbackground
, &color
); num_args
++;
746 XtGetValues(search
->rep_text
, args
, num_args
);
748 XtSetArg(args
[num_args
], XtNborderColor
, color
); num_args
++;
749 XtSetValues(search
->rep_text
, args
, num_args
);
750 XtSetKeyboardFocus(form
, search
->search_text
);
753 SetSearchLabels(search
, SEARCH_LABEL_1
, SEARCH_LABEL_2
, FALSE
);
756 * Bind Extra translations.
759 trans
= XtParseTranslationTable(search_text_trans
);
760 XtOverrideTranslations(search
->search_text
, trans
);
762 trans
= XtParseTranslationTable(rep_text_trans
);
763 XtOverrideTranslations(search
->rep_text
, trans
);
766 /* Function Name: DoSearch
767 * Description: Performs a search.
768 * Arguments: search - the serach structure.
769 * Returns: TRUE if sucessful.
775 struct SearchAndReplace
* search
;
778 Widget tw
= XtParent(search
->search_popup
);
780 XawTextScanDirection dir
;
783 text
.ptr
= GetString(search
->search_text
);
784 text
.length
= strlen(text
.ptr
);
786 text
.format
= FMT8BIT
;
788 dir
= (XawTextScanDirection
) (XawToggleGetCurrent(search
->left_toggle
) -
791 pos
= XawTextSearch( tw
, dir
, &text
);
793 if (pos
== XawTextSearchError
)
794 sprintf( msg
, "Could not find string '%s'.", text
.ptr
);
796 if (dir
== XawsdRight
)
797 XawTextSetInsertionPoint( tw
, pos
+ text
.length
);
799 XawTextSetInsertionPoint( tw
, pos
);
801 XawTextSetSelection( tw
, pos
, pos
+ text
.length
);
802 search
->selection_changed
= FALSE
; /* selection is good. */
806 XawTextUnsetSelection(tw
);
807 SetSearchLabels(search
, msg
, "", TRUE
);
811 /************************************************************
813 * This section of the file contains all the functions that
814 * the replace dialog box uses.
816 ************************************************************/
818 /* Function Name: _XawTextDoReplaceAction
819 * Description: Action routine that can be bound to dialog box's
820 * Text Widget that will replace a string in the main
822 * Arguments: (Standard Action Routine args)
828 _XawTextDoReplaceAction(w
, event
, params
, num_params
)
832 Cardinal
* num_params
;
834 TextWidget ctx
= (TextWidget
) XtParent(XtParent(XtParent(w
)));
835 Boolean popdown
= FALSE
;
837 if ( (*num_params
== 1) &&
838 ((params
[0][0] == 'p') || (params
[0][0] == 'P')) )
841 if (Replace( ctx
->text
.search
, TRUE
, popdown
) && popdown
)
842 PopdownSearch(w
, (caddr_t
) ctx
->text
.search
, (caddr_t
)NULL
);
845 /* Function Name: DoReplaceOne
846 * Description: Replaces the first instance of the string
847 * in the search dialog's text widget
848 * with the one in the replace dialog's text widget.
849 * Arguments: w - *** Not Used ***.
850 * closure - a pointer to the search structure.
851 * call_data - *** Not Used ***.
857 DoReplaceOne(w
, closure
, call_data
)
858 Widget w
; /* The Button Pressed. */
859 caddr_t closure
; /* Text Widget. */
860 caddr_t call_data
; /* unused */
862 Replace( (struct SearchAndReplace
*) closure
, TRUE
, FALSE
);
865 /* Function Name: DoReplaceOne
866 * Description: Replaces every instance of the string
867 * in the search dialog's text widget
868 * with the one in the replace dialog's text widget.
869 * Arguments: w - *** Not Used ***.
870 * closure - a pointer to the search structure.
871 * call_data - *** Not Used ***.
877 DoReplaceAll(w
, closure
, call_data
)
878 Widget w
; /* The Button Pressed. */
879 caddr_t closure
; /* Text Widget. */
880 caddr_t call_data
; /* unused */
882 Replace( (struct SearchAndReplace
*) closure
, FALSE
, FALSE
);
885 /* Function Name: Replace
886 * Description: This is the function that does the real work of
887 * replacing strings in the main text widget.
888 * Arguments: tw - the Text Widget to replce the string in.
889 * once_only - If TRUE then only replace the first one found.
890 * other replace all of them.
891 * show_current - If true then leave the selection on the
892 * string that was just replaced, otherwise
893 * move it onto the next one.
898 Replace(search
, once_only
, show_current
)
899 struct SearchAndReplace
* search
;
900 Boolean once_only
, show_current
;
902 XawTextPosition pos
, new_pos
, end_pos
;
903 XawTextScanDirection dir
;
904 XawTextBlock find
, replace
;
905 Widget tw
= XtParent(search
->search_popup
);
908 find
.ptr
= GetString( search
->search_text
);
909 find
.length
= strlen(find
.ptr
);
911 find
.format
= FMT8BIT
;
913 replace
.ptr
= GetString(search
->rep_text
);
914 replace
.length
= strlen(replace
.ptr
);
915 replace
.firstPos
= 0;
916 replace
.format
= FMT8BIT
;
918 dir
= (XawTextScanDirection
) (XawToggleGetCurrent(search
->left_toggle
) -
923 new_pos
= XawTextSearch( tw
, dir
, &find
);
925 if ( (new_pos
== XawTextSearchError
) ) {
929 sprintf( msg
, "%s %s %s", "*** Error: Could not find string '",
931 SetSearchLabels(search
, msg
, "", TRUE
);
938 end_pos
= pos
+ find
.length
;
941 XawTextGetSelectionPos(tw
, &pos
, &end_pos
);
943 if (search
->selection_changed
) {
944 SetSearchLabels(search
, "Selection has been modified, aborting.",
952 if (XawTextReplace(tw
, pos
, end_pos
, &replace
) != XawEditDone
) {
955 sprintf( msg
, "'%s' with '%s'. ***", find
.ptr
, replace
.ptr
);
956 SetSearchLabels(search
, "*** Error while replacing", msg
, TRUE
);
960 if (dir
== XawsdRight
)
961 XawTextSetInsertionPoint( tw
, pos
+ replace
.length
);
963 XawTextSetInsertionPoint( tw
, pos
);
975 if (replace
.length
== 0)
976 XawTextUnsetSelection(tw
);
978 XawTextSetSelection( tw
, pos
, pos
+ replace
.length
);
983 /* Function Name: SetSearchLabels
984 * Description: Sets both the search labels, and also rings the bell
985 * Arguments: search - the search structure.
986 * msg1, msg2 - message to put in each search label.
987 * bell - if TRUE then ring bell.
992 SetSearchLabels(search
, msg1
, msg2
, bell
)
993 struct SearchAndReplace
* search
;
997 (void) SetResource( search
->label1
, XtNlabel
, (XtArgVal
) msg1
);
998 (void) SetResource( search
->label2
, XtNlabel
, (XtArgVal
) msg2
);
1000 XBell(XtDisplay(search
->search_popup
), 0);
1003 /************************************************************
1005 * This section of the file contains utility routines used by
1006 * other functions in this file.
1008 ************************************************************/
1011 /* Function Name: _XawTextSetField
1012 * Description: Action routine that can be bound to dialog box's
1013 * Text Widget that will send input to the field specified.
1014 * Arguments: (Standard Action Routine args)
1020 _XawTextSetField(w
, event
, params
, num_params
)
1024 Cardinal
* num_params
;
1026 struct SearchAndReplace
* search
;
1029 search
= ((TextWidget
) XtParent(XtParent(XtParent(w
))))->text
.search
;
1031 if (*num_params
!= 1) {
1032 SetSearchLabels(search
, "*** Error: SetField Action must have",
1033 "exactly one argument. ***", TRUE
);
1036 switch (params
[0][0]) {
1039 new = search
->search_text
;
1040 old
= search
->rep_text
;
1044 old
= search
->search_text
;
1045 new = search
->rep_text
;
1048 SetSearchLabels(search
, "*** Error: SetField Action's first Argument must",
1049 "be either 'Search' or 'Replace'. ***", TRUE
);
1052 _SetField(new, old
);
1055 /* Function Name: SetField
1056 * Description: Sets the current text field.
1057 * Arguments: new, old - new and old text fields.
1066 Pixel new_border
, old_border
, old_bg
;
1068 if (!XtIsSensitive(new)) {
1069 XBell(XtDisplay(old
), 0); /* Don't set field to an inactive Widget. */
1073 XtSetKeyboardFocus(XtParent(new), new);
1075 XtSetArg(args
[0], XtNborderColor
, &old_border
);
1076 XtSetArg(args
[1], XtNbackground
, &old_bg
);
1077 XtGetValues(new, args
, TWO
);
1079 XtSetArg(args
[0], XtNborderColor
, &new_border
);
1080 XtGetValues(old
, args
, ONE
);
1082 if (old_border
!= old_bg
) /* Colors are already correct, return. */
1085 SetResource(old
, XtNborderColor
, (XtArgVal
) old_border
);
1086 SetResource(new, XtNborderColor
, (XtArgVal
) new_border
);
1089 /* Function Name: SetResourceByName
1090 * Description: Sets a resource in any of the dialog children given
1091 * name of the child and the shell widget of the dialog.
1092 * Arguments: shell - shell widget of the popup.
1093 * name - name of the child.
1094 * res_name - name of the resource.
1095 * value - the value of the resource.
1096 * Returns: TRUE if sucessful.
1100 SetResourceByName(shell
, name
, res_name
, value
)
1102 char * name
, * res_name
;
1108 sprintf(buf
, "%s.%s", FORM_NAME
, name
);
1110 if ( (temp_widget
= XtNameToWidget(shell
, buf
)) != NULL
) {
1111 SetResource(temp_widget
, res_name
, value
);
1117 /* Function Name: SetResource
1118 * Description: Sets a resource in a widget
1119 * Arguments: w - the widget.
1120 * res_name - name of the resource.
1121 * value - the value of the resource.
1126 SetResource(w
, res_name
, value
)
1133 XtSetArg(args
[0], res_name
, value
);
1134 XtSetValues( w
, args
, ONE
);
1137 /* Function Name: GetString
1138 * Description: Gets the value for the string in the popup.
1139 * Arguments: text - the text widget whose string we will get.
1140 * Returns: the string.
1150 XtSetArg( args
[0], XtNstring
, &string
);
1151 XtGetValues( text
, args
, ONE
);
1155 /* Function Name: CenterWidgetOnPoint.
1156 * Description: Centers a shell widget on a point relative to
1158 * Arguments: w - the shell widget.
1159 * event - event containing the location of the point
1162 * NOTE: The widget is not allowed to go off the screen.
1166 CenterWidgetOnPoint(w
, event
)
1172 Dimension width
, height
, b_width
;
1173 Position x
, y
, max_x
, max_y
;
1175 if (event
!= NULL
) {
1176 switch (event
->type
) {
1179 x
= event
->xbutton
.x_root
;
1180 y
= event
->xbutton
.y_root
;
1184 x
= event
->xkey
.x_root
;
1185 y
= event
->xkey
.y_root
;
1193 XtSetArg(args
[num_args
], XtNwidth
, &width
); num_args
++;
1194 XtSetArg(args
[num_args
], XtNheight
, &height
); num_args
++;
1195 XtSetArg(args
[num_args
], XtNborderWidth
, &b_width
); num_args
++;
1196 XtGetValues(w
, args
, num_args
);
1198 width
+= 2 * b_width
;
1199 height
+= 2 * b_width
;
1201 x
-= ( (Position
) width
/2 );
1203 if ( x
> (max_x
= (Position
) (XtScreen(w
)->width
- width
)) ) x
= max_x
;
1205 y
-= ( (Position
) height
/2 );
1207 if ( y
> (max_y
= (Position
) (XtScreen(w
)->height
- height
)) ) y
= max_y
;
1210 XtSetArg(args
[num_args
], XtNx
, x
); num_args
++;
1211 XtSetArg(args
[num_args
], XtNy
, y
); num_args
++;
1212 XtSetValues(w
, args
, num_args
);
1215 /* Function Name: CreateDialog
1216 * Description: Actually creates a dialog.
1217 * Arguments: parent - the parent of the dialog - the main text widget.
1218 * ptr - initial_string for the dialog.
1219 * name - name of the dialog.
1220 * func - function to create the children of the dialog.
1221 * Returns: the popup shell of the dialog.
1225 * The function argument is passed the following arguements.
1227 * form - the from widget that is the dialog.
1228 * ptr - the initial string for the dialog's text widget.
1229 * parent - the parent of the dialog - the main text widget.
1233 CreateDialog(parent
, ptr
, name
, func
)
1243 XtSetArg(args
[num_args
], XtNiconName
, name
); num_args
++;
1244 XtSetArg(args
[num_args
], XtNgeometry
, NULL
); num_args
++;
1245 XtSetArg(args
[num_args
], XtNallowShellResize
, TRUE
); num_args
++;
1246 XtSetArg(args
[num_args
], XtNsaveUnder
, TRUE
); num_args
++;
1247 popup
= XtCreatePopupShell(name
, transientShellWidgetClass
,
1248 parent
, args
, XtNumber(args
) );
1250 form
= XtCreateManagedWidget(FORM_NAME
, formWidgetClass
, popup
,
1253 (*func
) (form
, ptr
, parent
);