749N/A/* $XConsortium: TextPop.c,v 1.32 95/01/16 20:10:20 kaleb Exp $ */
749N/A
749N/A/*
749N/A
749N/ACopyright (c) 1989, 1994 X Consortium
749N/A
749N/APermission is hereby granted, free of charge, to any person obtaining a copy
749N/Aof this software and associated documentation files (the "Software"), to deal
749N/Ain the Software without restriction, including without limitation the rights
749N/Ato use, copy, modify, merge, publish, distribute, sublicense, and/or sell
749N/Acopies of the Software, and to permit persons to whom the Software is
749N/Afurnished to do so, subject to the following conditions:
749N/A
749N/AThe above copyright notice and this permission notice shall be included in
749N/Aall copies or substantial portions of the Software.
749N/A
749N/ATHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
749N/AIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
749N/AFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
749N/AX CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
749N/AAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
749N/ACONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
749N/A
749N/AExcept as contained in this notice, the name of the X Consortium shall not be
749N/Aused in advertising or otherwise to promote the sale, use or other dealings
749N/Ain this Software without prior written authorization from the X Consortium.
749N/A
749N/A*/
749N/A
749N/A/************************************************************
749N/A *
749N/A * This file is broken up into three sections one dealing with
749N/A * each of the three popups created here:
749N/A *
749N/A * FileInsert, Search, and Replace.
749N/A *
749N/A * There is also a section at the end for utility functions
749N/A * used by all more than one of these dialogs.
749N/A *
749N/A * The following functions are the only non-static ones defined
749N/A * in this module. They are located at the begining of the
749N/A * section that contains this dialog box that uses them.
749N/A *
749N/A * void _XawTextInsertFileAction(w, event, params, num_params);
749N/A * void _XawTextDoSearchAction(w, event, params, num_params);
749N/A * void _XawTextDoReplaceAction(w, event, params, num_params);
749N/A * void _XawTextInsertFile(w, event, params, num_params);
749N/A *
749N/A *************************************************************/
749N/A
749N/A#include <X11/IntrinsicP.h>
749N/A#include <X11/StringDefs.h>
749N/A#include <X11/Shell.h>
749N/A
749N/A#include <X11/Xaw/TextP.h>
749N/A#include <X11/Xaw/AsciiText.h>
749N/A#include <X11/Xaw/Cardinals.h>
749N/A#include <X11/Xaw/Command.h>
749N/A#include <X11/Xaw/Form.h>
749N/A#include <X11/Xaw/Toggle.h>
749N/A#include <X11/Xmu/CharSet.h>
749N/A#include "XawI18n.h"
749N/A#include <stdio.h>
749N/A#include <X11/Xos.h> /* for O_RDONLY */
749N/A#include <errno.h>
749N/A
749N/A#ifdef _LP64
749N/Aextern int errno;
749N/A#else /* _LP64 */
749N/A#ifdef X_NOT_STDC_ENV
749N/Aextern int errno;
749N/A#endif
749N/A#endif /* _LP64 */
749N/A
749N/Astatic char* INSERT_FILE = "Enter Filename:";
749N/A
749N/Astatic char* SEARCH_LABEL_1 = "Use <Tab> to change fields.";
749N/Astatic char* SEARCH_LABEL_2 = "Use ^q<Tab> for <Tab>.";
749N/Astatic char* DISMISS_NAME = "cancel";
749N/A#define DISMISS_NAME_LEN 6
749N/Astatic char* FORM_NAME = "form";
749N/Astatic char* LABEL_NAME = "label";
749N/Astatic char* TEXT_NAME = "text";
749N/A
749N/A#define R_OFFSET 1
749N/A
749N/Astatic void CenterWidgetOnPoint(), PopdownSearch(), DoInsert(), _SetField();
749N/Astatic void InitializeSearchWidget(), SetResource(), SetSearchLabels();
749N/Astatic void DoReplaceOne(), DoReplaceAll();
749N/Astatic Widget CreateDialog(), GetShell();
749N/Astatic void SetWMProtocolTranslations();
749N/Astatic Boolean DoSearch(), SetResourceByName(), Replace();
749N/Astatic String GetString();
749N/A
749N/Astatic String GetStringRaw();
749N/A
749N/Astatic void AddInsertFileChildren();
749N/Astatic Boolean InsertFileNamed();
749N/Astatic void AddSearchChildren();
749N/A
749N/Astatic char radio_trans_string[] =
749N/A "<Btn1Down>,<Btn1Up>: set() notify()";
749N/A
749N/Astatic char search_text_trans[] =
749N/A "~Shift<Key>Return: DoSearchAction(Popdown) \n\
749N/A Shift<Key>Return: DoSearchAction() SetField(Replace) \n\
749N/A Ctrl<Key>q,<Key>Tab: insert-char() \n\
749N/A Ctrl<Key>c: PopdownSearchAction() \n\
749N/A <Btn1Down>: select-start() SetField(Search) \n\
749N/A <Key>Tab: DoSearchAction() SetField(Replace)";
749N/A
749N/Astatic char rep_text_trans[] =
749N/A "~Shift<Key>Return: DoReplaceAction(Popdown) \n\
749N/A Shift<Key>Return: SetField(Search) \n\
749N/A Ctrl<Key>q,<Key>Tab: insert-char() \n\
749N/A Ctrl<Key>c: PopdownSearchAction() \n\
749N/A <Btn1Down>: select-start() DoSearchAction() SetField(Replace)\n\
749N/A <Key>Tab: SetField(Search)";
749N/A
749N/A/************************************************************
749N/A *
749N/A * This section of the file contains all the functions that
749N/A * the file insert dialog box uses.
749N/A *
749N/A ************************************************************/
749N/A
749N/A/* Function Name: _XawTextInsertFileAction
749N/A * Description: Action routine that can be bound to dialog box's
749N/A * Text Widget that will insert a file into the main
749N/A * Text Widget.
749N/A * Arguments: (Standard Action Routine args)
749N/A * Returns: none.
749N/A */
749N/A
749N/A/* ARGSUSED */
749N/Avoid
749N/A_XawTextInsertFileAction(w, event, params, num_params)
749N/AWidget w;
749N/AXEvent *event;
749N/AString * params;
749N/ACardinal * num_params;
749N/A{
749N/A DoInsert(w, (XtPointer) XtParent(XtParent(XtParent(w))), (XtPointer)NULL);
749N/A}
749N/A
749N/A/* Function Name: _XawTextInsertFile
749N/A * Description: Action routine that can be bound to the text widget
749N/A * it will popup the insert file dialog box.
749N/A * Arguments: w - the text widget.
749N/A * event - X Event (used to get x and y location).
749N/A * params, num_params - the parameter list.
749N/A * Returns: none.
749N/A *
749N/A * NOTE:
749N/A *
749N/A * The parameter list may contain one entry.
749N/A *
749N/A * Entry: This entry is optional and contains the value of the default
749N/A * file to insert.
749N/A */
749N/A
749N/Avoid
749N/A_XawTextInsertFile(w, event, params, num_params)
749N/AWidget w;
749N/AXEvent *event;
749N/AString * params;
749N/ACardinal * num_params;
749N/A{
749N/A TextWidget ctx = (TextWidget)w;
749N/A char * ptr;
749N/A XawTextEditType edit_mode;
749N/A Arg args[1];
749N/A
749N/A XtSetArg(args[0], XtNeditType,&edit_mode);
749N/A XtGetValues(ctx->text.source, args, ONE);
749N/A
749N/A if (edit_mode != XawtextEdit) {
749N/A XBell(XtDisplay(w), 0);
749N/A return;
749N/A }
749N/A
749N/A if (*num_params == 0)
749N/A ptr = "";
749N/A else
749N/A ptr = params[0];
749N/A
749N/A if (!ctx->text.file_insert) {
749N/A ctx->text.file_insert = CreateDialog(w, ptr, "insertFile",
749N/A AddInsertFileChildren);
749N/A XtRealizeWidget(ctx->text.file_insert);
749N/A SetWMProtocolTranslations(ctx->text.file_insert);
749N/A }
749N/A
749N/A CenterWidgetOnPoint(ctx->text.file_insert, event);
749N/A XtPopup(ctx->text.file_insert, XtGrabNone);
749N/A}
749N/A
749N/A/* Function Name: PopdownFileInsert
749N/A * Description: Pops down the file insert button.
749N/A * Arguments: w - the widget that caused this action.
749N/A * closure - a pointer to the main text widget that
749N/A * popped up this dialog.
749N/A * call_data - *** NOT USED ***.
749N/A * Returns: none.
749N/A */
749N/A
749N/A/* ARGSUSED */
749N/Astatic void
749N/APopdownFileInsert(w, closure, call_data)
749N/AWidget w; /* The Dialog Button Pressed. */
749N/AXtPointer closure; /* Text Widget. */
749N/AXtPointer call_data; /* unused */
749N/A{
749N/A TextWidget ctx = (TextWidget) closure;
749N/A
749N/A XtPopdown( ctx->text.file_insert );
749N/A (void) SetResourceByName( ctx->text.file_insert, LABEL_NAME,
749N/A XtNlabel, (XtArgVal) INSERT_FILE);
749N/A}
749N/A
749N/A/* Function Name: DoInsert
749N/A * Description: Actually insert the file named in the text widget
749N/A * of the file dialog.
749N/A * Arguments: w - the widget that activated this callback.
749N/A * closure - a pointer to the text widget to insert the
749N/A * file into.
749N/A * Returns: none.
749N/A */
749N/A
749N/A/* ARGSUSED */
749N/Astatic void
749N/ADoInsert(w, closure, call_data)
749N/AWidget w; /* The Dialog Button Pressed. */
749N/AXtPointer closure; /* Text Widget */
749N/AXtPointer call_data; /* unused */
749N/A{
749N/A TextWidget ctx = (TextWidget) closure;
749N/A char buf[BUFSIZ], msg[BUFSIZ];
749N/A Widget temp_widget;
749N/A
749N/A (void) sprintf(buf, "%s.%s", FORM_NAME, TEXT_NAME);
749N/A if ( (temp_widget = XtNameToWidget(ctx->text.file_insert, buf)) == NULL ) {
749N/A (void) strcpy(msg,
749N/A "*** Error: Could not get text widget from file insert popup");
749N/A }
749N/A else
749N/A if (InsertFileNamed( (Widget) ctx, GetString( temp_widget ))) {
749N/A PopdownFileInsert(w, closure, call_data);
749N/A return;
749N/A }
749N/A else
749N/A (void) sprintf( msg, "*** Error: %s ***", strerror(errno));
749N/A
749N/A (void)SetResourceByName(ctx->text.file_insert,
749N/A LABEL_NAME, XtNlabel, (XtArgVal) msg);
749N/A XBell(XtDisplay(w), 0);
749N/A}
749N/A
749N/A/* Function Name: InsertFileNamed
749N/A * Description: Inserts a file into the text widget.
749N/A * Arguments: tw - The text widget to insert this file into.
749N/A * str - name of the file to insert.
749N/A * Returns: TRUE if the insert was sucessful, FALSE otherwise.
749N/A */
749N/A
749N/A
749N/Astatic Boolean
749N/AInsertFileNamed(tw, str)
749N/AWidget tw;
749N/Achar *str;
749N/A{
749N/A FILE *file;
749N/A XawTextBlock text;
749N/A XawTextPosition pos;
749N/A
749N/A if ( (str == NULL) || (strlen(str) == 0) ||
749N/A ((file = fopen(str, "r")) == NULL))
749N/A return(FALSE);
749N/A
749N/A pos = XawTextGetInsertionPoint(tw);
749N/A
749N/A fseek(file, 0L, 2);
749N/A
749N/A
749N/A text.firstPos = 0;
749N/A text.length = (ftell(file))/sizeof(unsigned char);
749N/A text.ptr = XtMalloc((text.length + 1) * sizeof(unsigned char));
749N/A text.format = XawFmt8Bit;
749N/A
749N/A fseek(file, 0L, 0);
749N/A if (fread(text.ptr, sizeof(unsigned char), text.length, file) != (size_t)text.length)
749N/A XtErrorMsg("readError", "insertFileNamed", "XawError",
749N/A "fread returned error.", NULL, NULL);
749N/A
749N/A /* DELETE if (text.format == XawFmtWide) {
749N/A wchar_t* _XawTextMBToWC();
749N/A wchar_t* wstr;
749N/A wstr = _XawTextMBToWC(XtDisplay(tw), text.ptr, &(text.length));
749N/A wstr[text.length] = NULL;
749N/A XtFree(text.ptr);
749N/A text.ptr = (char *)wstr;
749N/A } else {
749N/A (text.ptr)[text.length] = '\0';
749N/A }*/
749N/A
749N/A if (XawTextReplace(tw, pos, pos, &text) != XawEditDone) {
749N/A XtFree(text.ptr);
749N/A fclose(file);
749N/A return(FALSE);
749N/A }
749N/A pos += text.length;
749N/A XtFree(text.ptr);
749N/A fclose(file);
749N/A XawTextSetInsertionPoint(tw, pos);
749N/A return(TRUE);
749N/A}
749N/A
749N/A
749N/A/* Function Name: AddInsertFileChildren
749N/A * Description: Adds all children to the InsertFile dialog widget.
749N/A * Arguments: form - the form widget for the insert dialog widget.
749N/A * ptr - a pointer to the initial string for the Text Widget.
749N/A * tw - the main text widget.
749N/A * Returns: none
749N/A */
749N/A
749N/Astatic void
749N/AAddInsertFileChildren(form, ptr, tw)
749N/AWidget form, tw;
749N/Achar * ptr;
749N/A{
749N/A Arg args[10];
749N/A Cardinal num_args;
749N/A Widget label, text, cancel, insert;
749N/A XtTranslations trans;
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNlabel, INSERT_FILE);num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNresizable, TRUE ); num_args++;
749N/A XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++;
749N/A label = XtCreateManagedWidget (LABEL_NAME, labelWidgetClass, form,
749N/A args, num_args);
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNfromVert, label); num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainRight); num_args++;
749N/A XtSetArg(args[num_args], XtNeditType, XawtextEdit); num_args++;
749N/A XtSetArg(args[num_args], XtNresizable, TRUE); num_args++;
749N/A XtSetArg(args[num_args], XtNresize, XawtextResizeWidth); num_args++;
749N/A XtSetArg(args[num_args], XtNstring, ptr); num_args++;
749N/A text = XtCreateManagedWidget(TEXT_NAME, asciiTextWidgetClass, form,
749N/A args, num_args);
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNlabel, "Insert File"); num_args++;
749N/A XtSetArg(args[num_args], XtNfromVert, text); num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
749N/A insert = XtCreateManagedWidget("insert", commandWidgetClass, form,
749N/A args, num_args);
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNlabel, "Cancel"); num_args++;
749N/A XtSetArg(args[num_args], XtNfromVert, text); num_args++;
749N/A XtSetArg(args[num_args], XtNfromHoriz, insert); num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
749N/A cancel = XtCreateManagedWidget(DISMISS_NAME, commandWidgetClass, form,
749N/A args, num_args);
749N/A
749N/A XtAddCallback(cancel, XtNcallback, PopdownFileInsert, (XtPointer) tw);
749N/A XtAddCallback(insert, XtNcallback, DoInsert, (XtPointer) tw);
749N/A
749N/A XtSetKeyboardFocus(form, text);
749N/A
749N/A/*
749N/A * Bind <CR> to insert file.
749N/A */
749N/A
749N/A trans = XtParseTranslationTable("<Key>Return: InsertFileAction()");
749N/A XtOverrideTranslations(text, trans);
749N/A
749N/A}
749N/A
749N/A/************************************************************
749N/A *
749N/A * This section of the file contains all the functions that
749N/A * the search dialog box uses.
749N/A *
749N/A ************************************************************/
749N/A
749N/A/* Function Name: _XawTextDoSearchAction
749N/A * Description: Action routine that can be bound to dialog box's
749N/A * Text Widget that will search for a string in the main
749N/A * Text Widget.
749N/A * Arguments: (Standard Action Routine args)
749N/A * Returns: none.
749N/A *
749N/A * Note:
749N/A *
749N/A * If the search was sucessful and the argument popdown is passed to
749N/A * this action routine then the widget will automatically popdown the
749N/A * search widget.
749N/A */
749N/A
749N/A/* ARGSUSED */
749N/Avoid
749N/A_XawTextDoSearchAction(w, event, params, num_params)
749N/AWidget w;
749N/AXEvent *event;
749N/AString * params;
749N/ACardinal * num_params;
749N/A{
749N/A TextWidget tw = (TextWidget) XtParent(XtParent(XtParent(w)));
749N/A Boolean popdown = FALSE;
749N/A
749N/A if ( (*num_params == 1) &&
749N/A ((params[0][0] == 'p') || (params[0][0] == 'P')) )
749N/A popdown = TRUE;
749N/A
749N/A if (DoSearch(tw->text.search) && popdown)
749N/A PopdownSearch(w, (XtPointer) tw->text.search, (XtPointer)NULL);
749N/A}
749N/A
749N/A/* Function Name: _XawTextPopdownSearchAction
749N/A * Description: Action routine that can be bound to dialog box's
749N/A * Text Widget that will popdown the search widget.
749N/A * Arguments: (Standard Action Routine args)
749N/A * Returns: none.
749N/A */
749N/A
749N/A/* ARGSUSED */
749N/Avoid
749N/A_XawTextPopdownSearchAction(w, event, params, num_params)
749N/AWidget w;
749N/AXEvent *event;
749N/AString * params;
749N/ACardinal * num_params;
749N/A{
749N/A TextWidget tw = (TextWidget) XtParent(XtParent(XtParent(w)));
749N/A
749N/A PopdownSearch(w, (XtPointer) tw->text.search, (XtPointer)NULL);
749N/A}
749N/A
749N/A/* Function Name: PopdownSeach
749N/A * Description: Pops down the search widget and resets it.
749N/A * Arguments: w - *** NOT USED ***.
749N/A * closure - a pointer to the search structure.
749N/A * call_data - *** NOT USED ***.
749N/A * Returns: none
749N/A */
749N/A
749N/A/* ARGSUSED */
749N/Astatic void
749N/APopdownSearch(w, closure, call_data)
749N/AWidget w;
749N/AXtPointer closure;
749N/AXtPointer call_data;
749N/A{
749N/A struct SearchAndReplace * search = (struct SearchAndReplace *) closure;
749N/A
749N/A XtPopdown( search->search_popup );
749N/A SetSearchLabels(search, SEARCH_LABEL_1, SEARCH_LABEL_2, FALSE);
749N/A}
749N/A
749N/A/* Function Name: SearchButton
749N/A * Description: Performs a search when the button is clicked.
749N/A * Arguments: w - *** NOT USED **.
749N/A * closure - a pointer to the search info.
749N/A * call_data - *** NOT USED ***.
749N/A * Returns:
749N/A */
749N/A
749N/A/* ARGSUSED */
749N/Astatic void
749N/ASearchButton(w, closure, call_data)
749N/AWidget w;
749N/AXtPointer closure;
749N/AXtPointer call_data;
749N/A{
749N/A (void) DoSearch( (struct SearchAndReplace *) closure );
749N/A}
749N/A
749N/A/* Function Name: _XawTextSearch
749N/A * Description: Action routine that can be bound to the text widget
749N/A * it will popup the search dialog box.
749N/A * Arguments: w - the text widget.
749N/A * event - X Event (used to get x and y location).
749N/A * params, num_params - the parameter list.
749N/A * Returns: none.
749N/A *
749N/A * NOTE:
749N/A *
749N/A * The parameter list contains one or two entries that may be the following.
749N/A *
749N/A * First Entry: The first entry is the direction to search by default.
749N/A * This arguement must be specified and may have a value of
749N/A * "left" or "right".
749N/A *
749N/A * Second Entry: This entry is optional and contains the value of the default
749N/A * string to search for.
749N/A */
749N/A
749N/A#define SEARCH_HEADER ("Text Widget - Search():")
749N/A
749N/Avoid
749N/A_XawTextSearch(w, event, params, num_params)
749N/AWidget w;
749N/AXEvent *event;
749N/AString * params;
749N/ACardinal * num_params;
749N/A{
749N/A TextWidget ctx = (TextWidget)w;
749N/A XawTextScanDirection dir;
749N/A char * ptr, buf[BUFSIZ];
749N/A XawTextEditType edit_mode;
749N/A Arg args[1];
749N/A
749N/A#ifdef notdef
749N/A if (ctx->text.source->Search == NULL) {
749N/A XBell(XtDisplay(w), 0);
749N/A return;
749N/A }
749N/A#endif
749N/A
749N/A if ( (*num_params < 1) || (*num_params > 2) ) {
749N/A (void) sprintf(buf, "%s %s\n%s", SEARCH_HEADER,
749N/A "This action must have only",
749N/A "one or two parameters");
749N/A XtAppWarning(XtWidgetToApplicationContext(w), buf);
749N/A return;
749N/A }
749N/A
749N/A if (*num_params == 2 )
749N/A ptr = params[1];
749N/A else
749N/A if (_XawTextFormat(ctx) == (int)XawFmtWide) {
749N/A /*This just does the equivalent of ptr = ""L, a waste because params[1] isnt W aligned.*/
749N/A ptr = (char *)XtMalloc(sizeof(wchar_t));
749N/A *((wchar_t*)ptr) = (wchar_t)0;
749N/A } else
749N/A ptr = "";
749N/A
749N/A switch(params[0][0]) {
749N/A case 'b': /* Left. */
749N/A case 'B':
749N/A dir = XawsdLeft;
749N/A break;
749N/A case 'f': /* Right. */
749N/A case 'F':
749N/A dir = XawsdRight;
749N/A break;
749N/A default:
749N/A (void) sprintf(buf, "%s %s\n%s", SEARCH_HEADER,
749N/A "The first parameter must be",
749N/A "Either 'backward' or 'forward'");
749N/A XtAppWarning(XtWidgetToApplicationContext(w), buf);
749N/A return;
749N/A }
749N/A
749N/A if (ctx->text.search== NULL) {
749N/A ctx->text.search = XtNew(struct SearchAndReplace);
749N/A ctx->text.search->search_popup = CreateDialog(w, ptr, "search",
749N/A AddSearchChildren);
749N/A XtRealizeWidget(ctx->text.search->search_popup);
749N/A SetWMProtocolTranslations(ctx->text.search->search_popup);
749N/A }
749N/A else if (*num_params > 1) {
749N/A XtVaSetValues(ctx->text.search->search_text, XtNstring, ptr, NULL);
749N/A }
749N/A
749N/A XtSetArg(args[0], XtNeditType,&edit_mode);
749N/A XtGetValues(ctx->text.source, args, ONE);
749N/A
749N/A InitializeSearchWidget(ctx->text.search, dir, (edit_mode == XawtextEdit));
749N/A
749N/A CenterWidgetOnPoint(ctx->text.search->search_popup, event);
749N/A XtPopup(ctx->text.search->search_popup, XtGrabNone);
749N/A}
749N/A
749N/A/* Function Name: InitializeSearchWidget
749N/A * Description: This function initializes the search widget and
749N/A * is called each time the search widget is poped up.
749N/A * Arguments: search - the search widget structure.
749N/A * dir - direction to search.
749N/A * replace_active - state of the sensitivity for the
749N/A * replace button.
749N/A * Returns: none.
749N/A */
749N/A
749N/Astatic void
749N/AInitializeSearchWidget(search, dir, replace_active)
749N/Astruct SearchAndReplace * search;
749N/AXawTextScanDirection dir;
749N/ABoolean replace_active;
749N/A{
749N/A SetResource(search->rep_one, XtNsensitive, (XtArgVal) replace_active);
749N/A SetResource(search->rep_all, XtNsensitive, (XtArgVal) replace_active);
749N/A SetResource(search->rep_label, XtNsensitive, (XtArgVal) replace_active);
749N/A SetResource(search->rep_text, XtNsensitive, (XtArgVal) replace_active);
749N/A
749N/A switch (dir) {
749N/A case XawsdLeft:
749N/A SetResource(search->left_toggle, XtNstate, (XtArgVal) TRUE);
749N/A break;
749N/A case XawsdRight:
749N/A SetResource(search->right_toggle, XtNstate, (XtArgVal) TRUE);
749N/A break;
749N/A default:
749N/A break;
749N/A }
749N/A}
749N/A
749N/A/* Function Name: AddSearchChildren
749N/A * Description: Adds all children to the Search Dialog Widget.
749N/A * Arguments: form - the form widget for the search widget.
749N/A * ptr - a pointer to the initial string for the Text Widget.
749N/A * tw - the main text widget.
749N/A * Returns: none.
749N/A */
749N/A
749N/Astatic void
749N/AAddSearchChildren(form, ptr, tw)
749N/AWidget form, tw;
749N/Achar * ptr;
749N/A{
749N/A Arg args[10];
749N/A Cardinal num_args;
749N/A Widget cancel, search_button, s_label, s_text, r_text;
749N/A XtTranslations trans;
749N/A struct SearchAndReplace * search = ((TextWidget) tw)->text.search;
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNresizable, TRUE ); num_args++;
749N/A XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++;
749N/A search->label1 = XtCreateManagedWidget("label1", labelWidgetClass, form,
749N/A args, num_args);
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNfromVert, search->label1); num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNresizable, TRUE ); num_args++;
749N/A XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++;
749N/A search->label2 = XtCreateManagedWidget("label2", labelWidgetClass, form,
749N/A args, num_args);
749N/A
749N/A/*
749N/A * We need to add R_OFFSET to the radio_data, because the value zero (0)
749N/A * has special meaning.
749N/A */
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNlabel, "Backward"); num_args++;
749N/A XtSetArg(args[num_args], XtNfromVert, search->label2); num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNradioData, (XPointer) XawsdLeft + R_OFFSET);
749N/A num_args++;
749N/A search->left_toggle = XtCreateManagedWidget("backwards", toggleWidgetClass,
749N/A form, args, num_args);
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNlabel, "Forward"); num_args++;
749N/A XtSetArg(args[num_args], XtNfromVert, search->label2); num_args++;
749N/A XtSetArg(args[num_args], XtNfromHoriz, search->left_toggle); num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNradioGroup, search->left_toggle); num_args++;
749N/A XtSetArg(args[num_args], XtNradioData, (XPointer) XawsdRight + R_OFFSET);
749N/A num_args++;
749N/A search->right_toggle = XtCreateManagedWidget("forwards", toggleWidgetClass,
749N/A form, args, num_args);
749N/A
749N/A {
749N/A XtTranslations radio_translations;
749N/A
749N/A radio_translations = XtParseTranslationTable(radio_trans_string);
749N/A XtOverrideTranslations(search->left_toggle, radio_translations);
749N/A XtOverrideTranslations(search->right_toggle, radio_translations);
749N/A }
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNfromVert, search->left_toggle); num_args++;
749N/A XtSetArg(args[num_args], XtNlabel, "Search for: ");num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++;
749N/A s_label = XtCreateManagedWidget("searchLabel", labelWidgetClass, form,
749N/A args, num_args);
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNfromVert, search->left_toggle); num_args++;
749N/A XtSetArg(args[num_args], XtNfromHoriz, s_label); num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainRight); num_args++;
749N/A XtSetArg(args[num_args], XtNeditType, XawtextEdit); num_args++;
749N/A XtSetArg(args[num_args], XtNresizable, TRUE); num_args++;
749N/A XtSetArg(args[num_args], XtNresize, XawtextResizeWidth); num_args++;
749N/A XtSetArg(args[num_args], XtNstring, ptr); num_args++;
749N/A s_text = XtCreateManagedWidget("searchText", asciiTextWidgetClass, form,
749N/A args, num_args);
749N/A search->search_text = s_text;
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNfromVert, s_text); num_args++;
749N/A XtSetArg(args[num_args], XtNlabel, "Replace with:");num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++;
749N/A search->rep_label = XtCreateManagedWidget("replaceLabel", labelWidgetClass,
749N/A form, args, num_args);
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNfromHoriz, s_label); num_args++;
749N/A XtSetArg(args[num_args], XtNfromVert, s_text); num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainRight); num_args++;
749N/A XtSetArg(args[num_args], XtNeditType, XawtextEdit); num_args++;
749N/A XtSetArg(args[num_args], XtNresizable, TRUE); num_args++;
749N/A XtSetArg(args[num_args], XtNresize, XawtextResizeWidth); num_args++;
749N/A XtSetArg(args[num_args], XtNstring, ""); num_args++;
749N/A r_text = XtCreateManagedWidget("replaceText", asciiTextWidgetClass,
749N/A form, args, num_args);
749N/A search->rep_text = r_text;
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNlabel, "Search"); num_args++;
749N/A XtSetArg(args[num_args], XtNfromVert, r_text); num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
749N/A search_button = XtCreateManagedWidget("search", commandWidgetClass, form,
749N/A args, num_args);
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNlabel, "Replace"); num_args++;
749N/A XtSetArg(args[num_args], XtNfromVert, r_text); num_args++;
749N/A XtSetArg(args[num_args], XtNfromHoriz, search_button); num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
749N/A search->rep_one = XtCreateManagedWidget("replaceOne", commandWidgetClass,
749N/A form, args, num_args);
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNlabel, "Replace All"); num_args++;
749N/A XtSetArg(args[num_args], XtNfromVert, r_text); num_args++;
749N/A XtSetArg(args[num_args], XtNfromHoriz, search->rep_one); num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
749N/A search->rep_all = XtCreateManagedWidget("replaceAll", commandWidgetClass,
749N/A form, args, num_args);
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNlabel, "Cancel"); num_args++;
749N/A XtSetArg(args[num_args], XtNfromVert, r_text); num_args++;
749N/A XtSetArg(args[num_args], XtNfromHoriz, search->rep_all); num_args++;
749N/A XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++;
749N/A XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++;
749N/A cancel = XtCreateManagedWidget(DISMISS_NAME, commandWidgetClass, form,
749N/A args, num_args);
749N/A
749N/A XtAddCallback(search_button, XtNcallback, SearchButton, (XtPointer) search);
749N/A XtAddCallback(search->rep_one, XtNcallback, DoReplaceOne, (XtPointer) search);
749N/A XtAddCallback(search->rep_all, XtNcallback, DoReplaceAll, (XtPointer) search);
749N/A XtAddCallback(cancel, XtNcallback, PopdownSearch, (XtPointer) search);
749N/A
749N/A/*
749N/A * Initialize the text entry fields.
749N/A */
749N/A
749N/A {
749N/A Pixel color;
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNbackground, &color); num_args++;
749N/A XtGetValues(search->rep_text, args, num_args);
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNborderColor, color); num_args++;
749N/A XtSetValues(search->rep_text, args, num_args);
749N/A XtSetKeyboardFocus(form, search->search_text);
749N/A }
749N/A
749N/A SetSearchLabels(search, SEARCH_LABEL_1, SEARCH_LABEL_2, FALSE);
749N/A
749N/A/*
749N/A * Bind Extra translations.
749N/A */
749N/A
749N/A trans = XtParseTranslationTable(search_text_trans);
749N/A XtOverrideTranslations(search->search_text, trans);
749N/A
749N/A trans = XtParseTranslationTable(rep_text_trans);
749N/A XtOverrideTranslations(search->rep_text, trans);
749N/A}
749N/A
749N/A/* Function Name: DoSearch
749N/A * Description: Performs a search.
749N/A * Arguments: search - the serach structure.
749N/A * Returns: TRUE if sucessful.
749N/A */
749N/A
749N/A/* ARGSUSED */
749N/Astatic Boolean
749N/ADoSearch(search)
749N/Astruct SearchAndReplace * search;
749N/A{
749N/A Widget tw = XtParent(search->search_popup);
749N/A XawTextPosition pos;
749N/A XawTextScanDirection dir;
749N/A XawTextBlock text;
749N/A
749N/A TextWidget ctx = (TextWidget)tw;
749N/A
749N/A text.ptr = GetStringRaw(search->search_text);
749N/A if ((text.format = _XawTextFormat(ctx)) == XawFmtWide)
749N/A text.length = wcslen((wchar_t*)text.ptr);
749N/A else
749N/A text.length = strlen(text.ptr);
749N/A text.firstPos = 0;
749N/A
749N/A dir = (XawTextScanDirection)(int) ((XPointer)XawToggleGetCurrent(search->left_toggle) -
749N/A R_OFFSET);
749N/A
749N/A pos = XawTextSearch( tw, dir, &text);
749N/A
749N/A
749N/A /* The Raw string in find.ptr may be WC I can't use here, so I re - call
749N/A GetString to get a tame version. */
749N/A
749N/A if (pos == XawTextSearchError) {
749N/A char msgbuf[BUFSIZ];
749N/A char* msg;
749N/A char* fmt = "Could not find string \"%s\"";
749N/A int len = strlen (fmt) + strlen (GetString (search->search_text));
749N/A
749N/A if (len < sizeof msgbuf) msg = msgbuf;
749N/A else msg = XtMalloc (len + 1);
749N/A if (msg == NULL) {
749N/A msg = msgbuf;
749N/A strcpy (msg, "Could not find string.");
749N/A } else
749N/A (void) sprintf (msg, fmt, GetString (search->search_text));
749N/A XawTextUnsetSelection(tw);
749N/A SetSearchLabels(search, msg, "", TRUE);
749N/A if (msg != msgbuf) XtFree (msg);
749N/A return FALSE;
749N/A }
749N/A if (dir == XawsdRight)
749N/A XawTextSetInsertionPoint( tw, pos + text.length);
749N/A else
749N/A XawTextSetInsertionPoint( tw, pos);
749N/A
749N/A XawTextSetSelection( tw, pos, pos + text.length);
749N/A search->selection_changed = FALSE; /* selection is good. */
749N/A return TRUE;
749N/A}
749N/A
749N/A/************************************************************
749N/A *
749N/A * This section of the file contains all the functions that
749N/A * the replace dialog box uses.
749N/A *
749N/A ************************************************************/
749N/A
749N/A/* Function Name: _XawTextDoReplaceAction
749N/A * Description: Action routine that can be bound to dialog box's
749N/A * Text Widget that will replace a string in the main
749N/A * Text Widget.
749N/A * Arguments: (Standard Action Routine args)
749N/A * Returns: none.
749N/A */
749N/A
749N/A/* ARGSUSED */
749N/Avoid
749N/A_XawTextDoReplaceAction(w, event, params, num_params)
749N/AWidget w;
749N/AXEvent *event;
749N/AString * params;
749N/ACardinal * num_params;
749N/A{
749N/A TextWidget ctx = (TextWidget) XtParent(XtParent(XtParent(w)));
749N/A Boolean popdown = FALSE;
749N/A
749N/A if ( (*num_params == 1) &&
749N/A ((params[0][0] == 'p') || (params[0][0] == 'P')) )
749N/A popdown = TRUE;
749N/A
749N/A if (Replace( ctx->text.search, TRUE, popdown) && popdown)
749N/A PopdownSearch(w, (XtPointer) ctx->text.search, (XtPointer)NULL);
749N/A}
749N/A
749N/A/* Function Name: DoReplaceOne
749N/A * Description: Replaces the first instance of the string
749N/A * in the search dialog's text widget
749N/A * with the one in the replace dialog's text widget.
749N/A * Arguments: w - *** Not Used ***.
749N/A * closure - a pointer to the search structure.
749N/A * call_data - *** Not Used ***.
749N/A * Returns: none.
749N/A */
749N/A
749N/A/* ARGSUSED */
749N/Astatic void
749N/ADoReplaceOne(w, closure, call_data)
749N/AWidget w; /* The Button Pressed. */
749N/AXtPointer closure; /* Text Widget. */
749N/AXtPointer call_data; /* unused */
749N/A{
749N/A Replace( (struct SearchAndReplace *) closure, TRUE, FALSE);
749N/A}
749N/A
749N/A/* Function Name: DoReplaceOne
749N/A * Description: Replaces every instance of the string
749N/A * in the search dialog's text widget
749N/A * with the one in the replace dialog's text widget.
749N/A * Arguments: w - *** Not Used ***.
749N/A * closure - a pointer to the search structure.
749N/A * call_data - *** Not Used ***.
749N/A * Returns: none.
749N/A */
749N/A
749N/A/* ARGSUSED */
749N/Astatic void
749N/ADoReplaceAll(w, closure, call_data)
749N/AWidget w; /* The Button Pressed. */
749N/AXtPointer closure; /* Text Widget. */
749N/AXtPointer call_data; /* unused */
749N/A{
749N/A Replace( (struct SearchAndReplace *) closure, FALSE, FALSE);
749N/A}
749N/A
749N/A/* Function Name: Replace
749N/A * Description: This is the function that does the real work of
749N/A * replacing strings in the main text widget.
749N/A * Arguments: tw - the Text Widget to replce the string in.
749N/A * once_only - If TRUE then only replace the first one found.
749N/A * other replace all of them.
749N/A * show_current - If true then leave the selection on the
749N/A * string that was just replaced, otherwise
749N/A * move it onto the next one.
749N/A * Returns: none.
749N/A */
749N/A
749N/Astatic Boolean
749N/AReplace(search, once_only, show_current)
749N/Astruct SearchAndReplace * search;
749N/ABoolean once_only, show_current;
749N/A{
749N/A XawTextPosition pos, new_pos, end_pos;
749N/A XawTextScanDirection dir;
749N/A XawTextBlock find, replace;
749N/A Widget tw = XtParent(search->search_popup);
749N/A int count = 0;
749N/A
749N/A TextWidget ctx = (TextWidget)tw;
749N/A
749N/A find.ptr = GetStringRaw( search->search_text);
749N/A if ((find.format = _XawTextFormat(ctx)) == XawFmtWide)
749N/A find.length = wcslen((wchar_t*)find.ptr);
749N/A else
749N/A find.length = strlen(find.ptr);
749N/A find.firstPos = 0;
749N/A
749N/A replace.ptr = GetStringRaw(search->rep_text);
749N/A replace.firstPos = 0;
749N/A if ((replace.format = _XawTextFormat(ctx)) == XawFmtWide)
749N/A replace.length = wcslen((wchar_t*)replace.ptr);
749N/A else
749N/A replace.length = strlen(replace.ptr);
749N/A
749N/A dir = (XawTextScanDirection)(int) ((XPointer)XawToggleGetCurrent(search->left_toggle) -
749N/A R_OFFSET);
749N/A /* CONSTCOND */
749N/A while (TRUE) {
749N/A if (count != 0) {
749N/A new_pos = XawTextSearch( tw, dir, &find);
749N/A
749N/A if ( (new_pos == XawTextSearchError) ) {
749N/A if (count == 0) {
749N/A char msgbuf[BUFSIZ];
749N/A char* msg;
749N/A int len;
749N/A char* fmt = "*** Error: Could not find string \"%s\" ***";
749N/A
749N/A len = strlen (fmt) + strlen (GetString (search->search_text));
749N/A if (len < sizeof msgbuf) msg = msgbuf;
749N/A else msg = XtMalloc (len + 1);
749N/A if (msg == NULL) {
749N/A msg = msgbuf;
749N/A strcpy (msg, "*** Error: Could not find string. ***");
749N/A } else
749N/A (void) sprintf (msg, fmt, GetString (search->search_text));
749N/A SetSearchLabels(search, msg, "", TRUE);
749N/A if (msg != msgbuf) XtFree (msg);
749N/A return(FALSE);
749N/A }
749N/A else
749N/A break;
749N/A }
749N/A pos = new_pos;
749N/A end_pos = pos + find.length;
749N/A }
749N/A else {
749N/A XawTextGetSelectionPos(tw, &pos, &end_pos);
749N/A
749N/A if (search->selection_changed) {
749N/A SetSearchLabels(search, "Selection has been modified, aborting.",
749N/A "", TRUE);
749N/A return(FALSE);
749N/A }
749N/A if (pos == end_pos)
749N/A return(FALSE);
749N/A }
749N/A
749N/A if (XawTextReplace(tw, pos, end_pos, &replace) != XawEditDone) {
749N/A char msgbuf[BUFSIZ];
749N/A char* msg;
749N/A int len;
749N/A char* fmt = "'%s' with '%s'. ***";
749N/A
749N/A len = strlen (fmt) + strlen (find.ptr) + strlen (replace.ptr);
749N/A if (len < sizeof msgbuf) msg = msgbuf;
749N/A else msg = XtMalloc (len + 1);
749N/A if (msg == NULL) {
749N/A msg = msgbuf;
749N/A strcpy (msg, "long strings");
749N/A } else
749N/A (void) sprintf( msg, fmt, find.ptr, replace.ptr);
749N/A SetSearchLabels(search, "*** Error while replacing", msg, TRUE);
749N/A if (msg != msgbuf) XtFree (msg);
749N/A return(FALSE);
749N/A }
749N/A
749N/A if (dir == XawsdRight)
749N/A XawTextSetInsertionPoint( tw, pos + replace.length);
749N/A else
749N/A XawTextSetInsertionPoint( tw, pos);
749N/A
749N/A if (once_only)
749N/A if (show_current)
749N/A break;
749N/A else {
749N/A DoSearch(search);
749N/A return(TRUE);
749N/A }
749N/A count++;
749N/A }
749N/A
749N/A if (replace.length == 0)
749N/A XawTextUnsetSelection(tw);
749N/A else
749N/A XawTextSetSelection( tw, pos, pos + replace.length);
749N/A
749N/A return(TRUE);
749N/A}
749N/A
749N/A/* Function Name: SetSearchLabels
749N/A * Description: Sets both the search labels, and also rings the bell
749N/A * Arguments: search - the search structure.
749N/A * msg1, msg2 - message to put in each search label.
749N/A * bell - if TRUE then ring bell.
749N/A * Returns: none.
749N/A */
749N/A
749N/Astatic void
749N/ASetSearchLabels(search, msg1, msg2, bell)
749N/Astruct SearchAndReplace * search;
749N/AString msg1, msg2;
749N/ABoolean bell;
749N/A{
749N/A (void) SetResource( search->label1, XtNlabel, (XtArgVal) msg1);
749N/A (void) SetResource( search->label2, XtNlabel, (XtArgVal) msg2);
749N/A if (bell)
749N/A XBell(XtDisplay(search->search_popup), 0);
749N/A}
749N/A
749N/A/************************************************************
749N/A *
749N/A * This section of the file contains utility routines used by
749N/A * other functions in this file.
749N/A *
749N/A ************************************************************/
749N/A
749N/A
749N/A/* Function Name: _XawTextSetField
749N/A * Description: Action routine that can be bound to dialog box's
749N/A * Text Widget that will send input to the field specified.
749N/A * Arguments: (Standard Action Routine args)
749N/A * Returns: none.
749N/A */
749N/A
749N/A/* ARGSUSED */
749N/Avoid
749N/A_XawTextSetField(w, event, params, num_params)
749N/AWidget w;
749N/AXEvent *event;
749N/AString * params;
749N/ACardinal * num_params;
749N/A{
749N/A struct SearchAndReplace * search;
749N/A Widget new, old;
749N/A
749N/A search = ((TextWidget) XtParent(XtParent(XtParent(w))))->text.search;
749N/A
749N/A if (*num_params != 1) {
749N/A SetSearchLabels(search, "*** Error: SetField Action must have",
749N/A "exactly one argument. ***", TRUE);
749N/A return;
749N/A }
749N/A switch (params[0][0]) {
749N/A case 's':
749N/A case 'S':
749N/A new = search->search_text;
749N/A old = search->rep_text;
749N/A break;
749N/A case 'r':
749N/A case 'R':
749N/A old = search->search_text;
749N/A new = search->rep_text;
749N/A break;
749N/A default:
749N/A SetSearchLabels(search, "*** Error: SetField Action's first Argument must",
749N/A "be either 'Search' or 'Replace'. ***", TRUE);
749N/A return;
749N/A }
749N/A _SetField(new, old);
749N/A}
749N/A
749N/A/* Function Name: SetField
749N/A * Description: Sets the current text field.
749N/A * Arguments: new, old - new and old text fields.
749N/A * Returns: none
749N/A */
749N/A
749N/Astatic void
749N/A_SetField(new, old)
749N/AWidget new, old;
749N/A{
749N/A Arg args[2];
749N/A Pixel new_border, old_border, old_bg;
749N/A
749N/A if (!XtIsSensitive(new)) {
749N/A XBell(XtDisplay(old), 0); /* Don't set field to an inactive Widget. */
749N/A return;
749N/A }
749N/A
749N/A XtSetKeyboardFocus(XtParent(new), new);
749N/A
749N/A XtSetArg(args[0], XtNborderColor, &old_border);
749N/A XtSetArg(args[1], XtNbackground, &old_bg);
749N/A XtGetValues(new, args, TWO);
749N/A
749N/A XtSetArg(args[0], XtNborderColor, &new_border);
749N/A XtGetValues(old, args, ONE);
749N/A
749N/A if (old_border != old_bg) /* Colors are already correct, return. */
749N/A return;
749N/A
749N/A SetResource(old, XtNborderColor, (XtArgVal) old_border);
749N/A SetResource(new, XtNborderColor, (XtArgVal) new_border);
749N/A}
749N/A
749N/A/* Function Name: SetResourceByName
749N/A * Description: Sets a resource in any of the dialog children given
749N/A * name of the child and the shell widget of the dialog.
749N/A * Arguments: shell - shell widget of the popup.
749N/A * name - name of the child.
749N/A * res_name - name of the resource.
749N/A * value - the value of the resource.
749N/A * Returns: TRUE if sucessful.
749N/A */
749N/A
749N/Astatic Boolean
749N/ASetResourceByName(shell, name, res_name, value)
749N/AWidget shell;
749N/Achar * name, * res_name;
749N/AXtArgVal value;
749N/A{
749N/A Widget temp_widget;
749N/A char buf[BUFSIZ];
749N/A char* bufp;
749N/A int len = strlen (FORM_NAME) + strlen (name) + 1;
749N/A Boolean ret = FALSE;
749N/A
749N/A if (len < sizeof buf) bufp = buf;
749N/A else bufp = XtMalloc (len + 1);
749N/A
749N/A if (bufp == NULL) return FALSE;
749N/A
749N/A (void) sprintf(bufp, "%s.%s", FORM_NAME, name);
749N/A
749N/A if ( (temp_widget = XtNameToWidget(shell, bufp)) != NULL) {
749N/A SetResource(temp_widget, res_name, value);
749N/A ret = TRUE;
749N/A }
749N/A if (bufp != buf) XtFree (bufp);
749N/A return ret;
749N/A}
749N/A
749N/A/* Function Name: SetResource
749N/A * Description: Sets a resource in a widget
749N/A * Arguments: w - the widget.
749N/A * res_name - name of the resource.
749N/A * value - the value of the resource.
749N/A * Returns: none.
749N/A */
749N/A
749N/Astatic void
749N/ASetResource(w, res_name, value)
749N/AWidget w;
749N/Achar * res_name;
749N/AXtArgVal value;
749N/A{
749N/A Arg args[1];
749N/A
749N/A XtSetArg(args[0], res_name, value);
749N/A XtSetValues( w, args, ONE );
749N/A}
749N/A
749N/A/* Function Name: GetString{Raw}
749N/A * Description: Gets the value for the string in the popup.
749N/A * Arguments: text - the text widget whose string we will get.
749N/A *
749N/A * GetString returns the string as a MB.
749N/A * GetStringRaw returns the exact buffer contents suitable for a search.
749N/A *
749N/A */
749N/A
749N/Astatic String
749N/AGetString(text)
749N/AWidget text;
749N/A{
749N/A String string;
749N/A Arg args[1];
749N/A
749N/A XtSetArg( args[0], XtNstring, &string );
749N/A XtGetValues( text, args, ONE );
749N/A return(string);
749N/A}
749N/A
749N/Astatic String
749N/AGetStringRaw(tw)
749N/AWidget tw;
749N/A{
749N/A TextWidget ctx = (TextWidget)tw;
749N/A XawTextPosition last;
749N/A char *_XawTextGetText();
749N/A
749N/A last = XawTextSourceScan(ctx->text.source, 0, XawstAll, XawsdRight,
749N/A ctx->text.mult, TRUE);
749N/A return (_XawTextGetText(ctx, 0, last));
749N/A}
749N/A
749N/A/* Function Name: CenterWidgetOnPoint.
749N/A * Description: Centers a shell widget on a point relative to
749N/A * the root window.
749N/A * Arguments: w - the shell widget.
749N/A * event - event containing the location of the point
749N/A * Returns: none.
749N/A *
749N/A * NOTE: The widget is not allowed to go off the screen.
749N/A */
749N/A
749N/Astatic void
749N/ACenterWidgetOnPoint(w, event)
749N/AWidget w;
749N/AXEvent *event;
749N/A{
749N/A Arg args[3];
749N/A Cardinal num_args;
749N/A Dimension width, height, b_width;
749N/A Position x, y, max_x, max_y;
749N/A
749N/A if (event != NULL) {
749N/A switch (event->type) {
749N/A case ButtonPress:
749N/A case ButtonRelease:
749N/A x = event->xbutton.x_root;
749N/A y = event->xbutton.y_root;
749N/A break;
749N/A case KeyPress:
749N/A case KeyRelease:
749N/A x = event->xkey.x_root;
749N/A y = event->xkey.y_root;
749N/A break;
749N/A default:
749N/A return;
749N/A }
749N/A }
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNwidth, &width); num_args++;
749N/A XtSetArg(args[num_args], XtNheight, &height); num_args++;
749N/A XtSetArg(args[num_args], XtNborderWidth, &b_width); num_args++;
749N/A XtGetValues(w, args, num_args);
749N/A
749N/A width += 2 * b_width;
749N/A height += 2 * b_width;
749N/A
749N/A x -= ( (Position) width/2 );
749N/A if (x < 0) x = 0;
749N/A if ( x > (max_x = (Position) (XtScreen(w)->width - width)) ) x = max_x;
749N/A
749N/A y -= ( (Position) height/2 );
749N/A if (y < 0) y = 0;
749N/A if ( y > (max_y = (Position) (XtScreen(w)->height - height)) ) y = max_y;
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNx, x); num_args++;
749N/A XtSetArg(args[num_args], XtNy, y); num_args++;
749N/A XtSetValues(w, args, num_args);
749N/A}
749N/A
749N/A/* Function Name: CreateDialog
749N/A * Description: Actually creates a dialog.
749N/A * Arguments: parent - the parent of the dialog - the main text widget.
749N/A * ptr - initial_string for the dialog.
749N/A * name - name of the dialog.
749N/A * func - function to create the children of the dialog.
749N/A * Returns: the popup shell of the dialog.
749N/A *
749N/A * NOTE:
749N/A *
749N/A * The function argument is passed the following arguements.
749N/A *
749N/A * form - the from widget that is the dialog.
749N/A * ptr - the initial string for the dialog's text widget.
749N/A * parent - the parent of the dialog - the main text widget.
749N/A */
749N/A
749N/Astatic Widget
749N/ACreateDialog(parent, ptr, name, func)
749N/AWidget parent;
749N/AString ptr, name;
749N/Avoid (*func)();
749N/A{
749N/A Widget popup, form;
749N/A Arg args[5];
749N/A Cardinal num_args;
749N/A
749N/A num_args = 0;
749N/A XtSetArg(args[num_args], XtNiconName, name); num_args++;
749N/A XtSetArg(args[num_args], XtNgeometry, NULL); num_args++;
749N/A XtSetArg(args[num_args], XtNallowShellResize, TRUE); num_args++;
749N/A XtSetArg(args[num_args], XtNtransientFor, GetShell(parent)); num_args++;
749N/A popup = XtCreatePopupShell(name, transientShellWidgetClass,
749N/A parent, args, num_args);
749N/A
749N/A form = XtCreateManagedWidget(FORM_NAME, formWidgetClass, popup,
749N/A (ArgList)NULL, ZERO);
749N/A XtManageChild (form);
749N/A
749N/A (*func) (form, ptr, parent);
749N/A return(popup);
749N/A}
749N/A
749N/A /* Function Name: GetShell
749N/A * Description: Walks up the widget hierarchy to find the
749N/A * nearest shell widget.
749N/A * Arguments: w - the widget whose parent shell should be returned.
749N/A * Returns: The shell widget among the ancestors of w that is the
749N/A * fewest levels up in the widget hierarchy.
749N/A */
749N/A
749N/Astatic Widget
749N/AGetShell(w)
749N/AWidget w;
749N/A{
749N/A while ((w != NULL) && !XtIsShell(w))
749N/A w = XtParent(w);
749N/A
749N/A return (w);
749N/A}
749N/A
749N/Astatic Boolean InParams(str, p, n)
749N/A String str;
749N/A String *p;
749N/A Cardinal n;
749N/A{
749N/A int i;
749N/A for (i=0; i < n; p++, i++)
749N/A if (! XmuCompareISOLatin1(*p, str)) return True;
749N/A return False;
749N/A}
749N/A
749N/Astatic char *WM_DELETE_WINDOW = "WM_DELETE_WINDOW";
749N/A
749N/Astatic void WMProtocols(w, event, params, num_params)
749N/A Widget w; /* popup shell */
749N/A XEvent *event;
749N/A String *params;
749N/A Cardinal *num_params;
749N/A{
749N/A Atom wm_delete_window;
749N/A Atom wm_protocols;
749N/A
749N/A wm_delete_window = XInternAtom(XtDisplay(w), WM_DELETE_WINDOW, True);
749N/A wm_protocols = XInternAtom(XtDisplay(w), "WM_PROTOCOLS", True);
749N/A
749N/A /* Respond to a recognized WM protocol request iff
749N/A * event type is ClientMessage and no parameters are passed, or
749N/A * event type is ClientMessage and event data is matched to parameters, or
749N/A * event type isn't ClientMessage and parameters make a request.
749N/A */
749N/A#define DO_DELETE_WINDOW InParams(WM_DELETE_WINDOW, params, *num_params)
749N/A
749N/A if ((event->type == ClientMessage &&
749N/A event->xclient.message_type == wm_protocols &&
749N/A event->xclient.data.l[0] == wm_delete_window &&
749N/A (*num_params == 0 || DO_DELETE_WINDOW))
749N/A ||
749N/A (event->type != ClientMessage && DO_DELETE_WINDOW)) {
749N/A
749N/A#undef DO_DELETE_WINDOW
749N/A
749N/A Widget cancel;
749N/A char descendant[DISMISS_NAME_LEN + 2];
749N/A (void) sprintf(descendant, "*%s", DISMISS_NAME);
749N/A cancel = XtNameToWidget(w, descendant);
749N/A if (cancel) XtCallCallbacks(cancel, XtNcallback, (XtPointer)NULL);
749N/A }
749N/A}
749N/A
749N/Astatic void SetWMProtocolTranslations(w)
749N/A Widget w; /* realized popup shell */
749N/A{
749N/A int i;
749N/A XtAppContext app_context;
749N/A Atom wm_delete_window;
749N/A static XtTranslations compiled_table; /* initially 0 */
749N/A static XtAppContext *app_context_list; /* initially 0 */
749N/A static Cardinal list_size; /* initially 0 */
749N/A
749N/A app_context = XtWidgetToApplicationContext(w);
749N/A
749N/A /* parse translation table once */
749N/A if (! compiled_table) compiled_table = XtParseTranslationTable
749N/A ("<Message>WM_PROTOCOLS: XawWMProtocols()\n");
749N/A
749N/A /* add actions once per application context */
749N/A for (i=0; i < list_size && app_context_list[i] != app_context; i++) ;
749N/A if (i == list_size) {
749N/A XtActionsRec actions[1];
749N/A actions[0].string = "XawWMProtocols";
749N/A actions[0].proc = WMProtocols;
749N/A list_size++;
749N/A app_context_list = (XtAppContext *) XtRealloc
749N/A ((char *)app_context_list, list_size * sizeof(XtAppContext));
749N/A XtAppAddActions(app_context, actions, 1);
749N/A app_context_list[i] = app_context;
749N/A }
749N/A
749N/A /* establish communication between the window manager and each shell */
749N/A XtAugmentTranslations(w, compiled_table);
749N/A wm_delete_window = XInternAtom(XtDisplay(w), WM_DELETE_WINDOW, False);
749N/A (void) XSetWMProtocols(XtDisplay(w), XtWindow(w), &wm_delete_window, 1);
749N/A}