home *** CD-ROM | disk | FTP | other *** search
- /* SOURCE FILE *****************************************************
- * WTSERVER.C - Writing Tools API Server Sample Application
- * This is a sample writing tool which translates
- * to and from pig latin.
- *******************************************************************
- * Copyright (C) 1993 WordPerfect Corp., All Rights Reserved
- *******************************************************************/
-
- #include "wtserver.h"
- #include "wtserver.xbm" /* icon bitmap */
-
- #include <X11/cursorfont.h>
- #include <Xm/Xm.h>
- #include <Xm/CascadeB.h>
- #include <Xm/CutPaste.h>
- #include <Xm/Form.h>
- #include <Xm/Label.h>
- #include <Xm/List.h>
- #include <Xm/MessageB.h>
- #include <Xm/Protocols.h>
- #include <Xm/PushB.h>
- #include <Xm/RowColumn.h>
- #include <Xm/Separator.h>
- #include <Xm/Text.h>
- #include <Xm/ToggleB.h>
-
- #include <unistd.h>
- #include <wtcomm.h>
-
-
- /*----------------------------------------------------------
- External Functions
- ------------------------------------------------------------*/
- extern void TermSession();
- extern WTCOMM WTInitComm(Widget, char *, int, int);
- extern void WTCloseComm(WTCOMM);
-
- /*----------------------------------------------------------
- External Variables
- ------------------------------------------------------------*/
-
- /*----------------------------------------------------------
- Menu Definitions
- ------------------------------------------------------------*/
- /* Translate menu items */
- static char *TlTransStr[] = {
- "Native to Pig Latin", "Pig Latin to Native",
- "Word", "Document", "To End of Document", "Selection", 0
- };
- static char TlTransMne[] = {
- 'P', 'N', 'W', 'D', 'E', 'S', 0
- };
-
- /* Edit menu items */
- static char *TlEditStr[] = {
- "Undo", "Cut", "Copy", "Paste", 0
- };
- static char TlEditMne[] = {
- 'U', 't', 'C', 'P', 0
- };
-
- /* Help menu items */
- static char *TlHelpStr[] = {
- "About WTServer...", 0
- };
- static char TlHelpMne[] = {
- 'A', 0
- };
-
- /* Push buttons */
- static char *TlButtons[] = {
- "Start", "Skip", "ReTranslate", "Translate All", "Close", 0
- };
-
- /*----------------------------------------------------------
- Internal Variables
- ------------------------------------------------------------*/
- BOOL inAPI = FALSE; /* in a WT Session? */
- TL_TRANS CurSel = TL_TRANS_DOC; /* Default in API is translate Doc */
- BOOL inBlock = FALSE; /* is the client in a selected block? */
- WTCOMM comm_id; /* conversation handle */
- WTBUF sndbuf[BUFSIZE]; /* buffer for outgoing messages */
- WTBUF rcvbuf[BUFSIZE]; /* buffer for return messages */
- WTBUF winbuf[BUFSIZE]; /* buffer for client window name */
- Widget Server;
- static XmFontList FontList;
- static Cursor waitcursor = (Cursor) 0;
- static Cursor normalcursor = (Cursor) 0;
- static Pixmap Icon; /* Icon for sample server application */
- static Widget Text; /* Text widget for server (for Edit) */
- static Widget TransMode; /* Label for translation mode */
- static Widget TransScope; /* Label for translation scope */
- static Widget Word; /* Text box for Word */
- static Widget Translation; /* Text box for Translation */
- static Widget editButtons[TL_EDIT_CNT]; /* Edit menu buttons */
- static Widget transButtons[TL_TRANS_CNT]; /* Translate menu buttons */
- static Widget Buttons[TL_BUT_CNT]; /* Push buttons */
- static Widget TransOpts; /* Translation options dialog */
- static Widget TransList; /* List of possible translations */
- static char *prevText; /* text before previous edit */
- static XmTextPosition prevSelLeft = 0xff, /* previous selection */
- prevSelRight = 0;
- static Widget msgDialog;
- static BOOL MsgBoxRet = FALSE;
- static TL_TRANS Mode = TL_TRANS_NATPIG; /* Translation mode */
- static BOOL inLoop = FALSE; /* has the user decided what to check? */
-
- static WTBUF txtbuf[TEXTSIZE+WORDSIZE]; /* buffer for client text */
- static int txtsize; /* total size of the text in the buffer */
- static int txtbeg; /* offset of the current word */
- static int txtend; /* end offset of the word */
- static WTENDBLOCK endflag; /* reason for end of last text block */
- static char gbuf[BUFSIZE]; /* global buffer */
-
- /*----------------------------------------------------------
- Internal Functions
- ------------------------------------------------------------*/
- XmString StringCreate(char *);
- char *XmStringToString(XmString str);
- void NormalCursor(Widget);
- void WaitCursor(Widget);
- void SetDialogState();
- void quitTool(char *);
- BOOL MsgBox(MSGBOX, Widget, char *, char *);
- static void ServerInit(Widget);
- static void ServerDestroyCB(Widget, XtPointer, XtPointer);
- static void ServerExit();
- static void TransCB(Widget, XtPointer, XtPointer);
- static void EditCB(Widget, XtPointer, XmAnyCallbackStruct *);
- static void SetEditButtons(Widget, XtPointer, XtPointer);
- static void HelpCB(Widget, XtPointer, XtPointer);
- static void ButtonCB(Widget, XtPointer, XtPointer);
- static void free_cursors(Widget);
- static void change_cursors(Widget, Cursor);
- static void MsgBoxCB(Widget, XtPointer, XtPointer);
- static void SetTranslateText();
- static void TextFocusCB(Widget, XtPointer, XtPointer);
- static BOOL NatToPig(char *, char *);
- static BOOL PigToNat(char *, char *);
- static void InitText(void);
- static void SetButtons();
- static void DeHilite();
- static BOOL NextWord(char *);
- static void NextBlock(int);
- static void PutWords(char *, char *);
- static void XLateAll(char *, char *);
- static void ReplaceWord(char *);
- static void PromptTranslateOptions(char *);
- static void TransOptCB(Widget, XtPointer, XtPointer);
- static BOOL isvowel(char);
-
- /*COMMENT***************************************************
- ;main
- Title: WTAPI Sample Server Main Function
- In: argc, argv
- Out: none
- Xin: none
- Xout: none
- Return: exit status
- Notes:
- ***********************************************************/
- main(int argc, char *argv[])
- {
- XtAppContext appcontext;
- Widget toplevel;
- int read_fd, write_fd; /* pipe read/write file descriptors */
- String app_class; /* parent's application class */
- int arg;
-
- #ifdef DEBUG
- /*
- * Wait when invoked to give time to attach debugger.
- * Loop until file "/tmp/debugloop" no longer exists or until
- * the loop is exited using the debugger.
- */
- while (access("/tmp/debugloop", F_OK) == 0) {
- sleep(10);
- }
- #endif /* DEBUG */
- /*
- * We need to check for -wtapi and the application class
- * (-appClass) in the command-line arguments.
- */
- app_class = "WTServer"; /* default */
- read_fd = write_fd = -1; /* defaults */
- for (arg = 0; arg < argc; arg++) {
- if (strcmp(argv[arg], "-wtapi") == 0) {
- inAPI = TRUE;
- if ((arg + 2) < argc) {
- read_fd = atoi(argv[arg+1]);
- write_fd = atoi(argv[arg+2]);
- }
- arg += 2;
- }
- if ((strcmp(argv[arg], "-appClass") == 0) && ((arg + 1) < argc)) {
- app_class = argv[arg+1];
- arg++;
- }
- }
- toplevel = XtVaAppInitialize(&appcontext, app_class,
- NULL, 0, &argc, (char **)argv, NULL,
- XmNallowShellResize, True,
- NULL);
- Server = toplevel;
- if (inAPI) {
- comm_id = WTInitComm(toplevel, argv[0], read_fd, write_fd);
- if (!comm_id) {
- MsgBox(MB_OK, Server, "WTServer Error",
- "Unable to Establish Writing Tools API Communication!");
- inAPI = FALSE;
- }
- }
- ServerInit(toplevel);
- XtAppMainLoop(appcontext);
- } /* main */
-
- /*COMMENT***************************************************
- ;ServerInit
- Title: Initialize WTAPI Sample Server Application
- In: toplevel - toplevel widget of application
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes:
- ***********************************************************/
- static void ServerInit(Widget toplevel)
- {
- Atom deleteWindow; /* for XmAddWMProtocol... */
- XmString xmstr; /* Motif compound string */
- Widget form, /* main Server form */
- menubar, /* menu bar */
- button, /* cascade or push button */
- label, label2, /* labels */
- pd, /* pulldown menu */
- rowcol, /* rowcolumn */
- separator; /* separator */
- TL_TRANS trans; /* loop variable */
- TL_EDIT edit; /* loop variable */
- TL_HELP help; /* loop variable */
- TL_BUT but; /* loop variable */
- XFontStruct *font;
- char *str;
-
- /*
- * Set up icon and dialog destroy callbacks for main Server window.
- */
- Server = toplevel;
- Icon = XCreateBitmapFromData(XtDisplay(toplevel),
- RootWindowOfScreen(XtScreen(toplevel)),
- wtserver_bits, wtserver_width, wtserver_height);
- XtVaSetValues(Server,
- XtNtitle, "WTAPI Sample Server Application",
- XmNiconName, "WTServer",
- XmNiconPixmap, Icon,
- XmNdeleteResponse, XmDO_NOTHING,
- NULL);
- XtAddCallback(Server, XmNdestroyCallback, ServerDestroyCB, 0);
- deleteWindow = XmInternAtom(XtDisplay(Server), "WM_DELETE_WINDOW", False);
- XmAddWMProtocols(Server, &deleteWindow, 1);
- XmAddWMProtocolCallback(Server, deleteWindow, ServerDestroyCB, 0);
- /*
- * Create the main Server form.
- */
- FontList = NULL;
- if (font = XLoadQueryFont(XtDisplay(Server),
- "-adobe-helvetica-medium-r-normal--14-100-100-100-p-76-iso8859-1"))
- {
- FontList = XmFontListCreate(font, XmSTRING_DEFAULT_CHARSET);
- }
- form = XtVaCreateManagedWidget("tlform", xmFormWidgetClass, Server,
- XmNfontList, FontList,
- NULL);
- /*
- * Create the menu bar.
- */
- menubar = XmCreateMenuBar(form, "tlmenu", 0, 0);
- /*
- * Create the Translate menu.
- */
- button = XtVaCreateManagedWidget("tltranslate",
- xmCascadeButtonWidgetClass, menubar,
- XmNlabelString, xmstr = StringCreate("Translate"),
- XmNmnemonic, 'T',
- XmNsubMenuId, pd = XmCreatePulldownMenu(menubar, "tlTransPD", 0, 0),
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- if (!inAPI) {
- CurSel = TL_TRANS_WORD;
- } else {
- CurSel = TL_TRANS_DOC;
- }
- for (trans = TL_TRANS_NATPIG; trans < TL_TRANS_CNT; trans++) {
- if (trans == TL_TRANS_WORD) {
- separator = XtVaCreateManagedWidget("sep",
- xmSeparatorWidgetClass, pd,
- NULL);
- }
- transButtons[trans] =
- button = XtVaCreateManagedWidget(TlTransStr[trans],
- xmToggleButtonWidgetClass, pd,
- XmNlabelString, xmstr = StringCreate(TlTransStr[trans]),
- XmNmnemonic, TlTransMne[trans],
- XmNindicatorType, XmONE_OF_MANY,
- XmNvisibleWhenOff, True,
- XmNsensitive, True,
- XmNfontList, FontList,
- NULL);
- if ((trans == Mode) || (trans == CurSel)) {
- XtVaSetValues(button, XmNset, True, NULL);
- }
- XmStringFree(xmstr);
- XtAddCallback(button, XmNvalueChangedCallback, TransCB, (XtPointer)(int)trans);
- }
- /*
- * Create the Edit menu.
- */
- button = XtVaCreateManagedWidget("tledit",
- xmCascadeButtonWidgetClass, menubar,
- XmNlabelString, xmstr = StringCreate("Edit"),
- XmNmnemonic, 'E',
- XmNsubMenuId, pd = XmCreatePulldownMenu(menubar, "tlEditPD", 0, 0),
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- XtAddCallback(button, XmNcascadingCallback, SetEditButtons, 0);
- for (edit = TL_EDIT_UNDO; edit < TL_EDIT_CNT; edit++) {
- editButtons[edit] = button = XtVaCreateManagedWidget(TlEditStr[edit],
- xmPushButtonWidgetClass, pd,
- XmNlabelString, xmstr = StringCreate(TlEditStr[edit]),
- XmNmnemonic, TlEditMne[edit],
- XmNvisibleWhenOff, True,
- XmNsensitive, True,
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- XtAddCallback(button, XmNactivateCallback, (XtCallbackProc)EditCB, (XtPointer)(int)edit);
- if (edit == TL_EDIT_UNDO) {
- separator = XtVaCreateManagedWidget("sep",
- xmSeparatorWidgetClass, pd,
- NULL);
- }
- }
- /*
- * Create the Help menu.
- */
- button = XtVaCreateManagedWidget("clhelp",
- xmCascadeButtonWidgetClass, menubar,
- XmNlabelString, xmstr = StringCreate("Help"),
- XmNmnemonic, 'H',
- XmNsubMenuId, pd = XmCreatePulldownMenu(menubar, "clHelpPD", 0, 0),
- XmNfontList, FontList,
- NULL);
- XtVaSetValues(menubar,
- XmNmenuHelpWidget, button,
- NULL);
- XmStringFree(xmstr);
- for (help = TL_HELP_ABOUT; help < TL_HELP_CNT; help++) {
- button = XtVaCreateManagedWidget(TlHelpStr[help],
- xmPushButtonWidgetClass, pd,
- XmNlabelString, xmstr = StringCreate(TlHelpStr[help]),
- XmNmnemonic, TlHelpMne[help],
- XmNvisibleWhenOff, True,
- XmNsensitive, True,
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- XtAddCallback(button, XmNactivateCallback, HelpCB, (XtPointer)(int)help);
- }
- XtVaSetValues(menubar,
- XmNtopAttachment, XmATTACH_FORM,
- XmNtopOffset, 0,
- XmNleftAttachment, XmATTACH_FORM,
- XmNleftOffset, 0,
- XmNrightAttachment, XmATTACH_FORM,
- XmNrightOffset, 0,
- NULL);
- XtManageChild(menubar);
- /*
- * Create the Translation Mode labels.
- */
- label = XtVaCreateManagedWidget("modelabel",
- xmLabelWidgetClass, form,
- XmNlabelString, xmstr = StringCreate("Translation Mode:"),
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNtopWidget, menubar,
- XmNtopOffset, 10,
- XmNleftAttachment, XmATTACH_FORM,
- XmNleftOffset, 10,
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- TransMode = XtVaCreateManagedWidget("mode",
- xmLabelWidgetClass, form,
- XmNlabelString, xmstr = StringCreate(TlTransStr[Mode]),
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNtopWidget, menubar,
- XmNtopOffset, 10,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNleftWidget, label,
- XmNleftOffset, 10,
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- /*
- * Create the Translation Mode labels.
- */
- TransScope = XtVaCreateManagedWidget("scope",
- xmLabelWidgetClass, form,
- XmNlabelString, xmstr = StringCreate(TlTransStr[CurSel]),
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNtopWidget, TransMode,
- XmNtopOffset, 10,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftWidget, TransMode,
- XmNleftOffset, 0,
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- label2 = XtVaCreateManagedWidget("scopelabel",
- xmLabelWidgetClass, form,
- XmNlabelString, xmstr = StringCreate("Translate:"),
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, TransScope,
- XmNtopOffset, 0,
- XmNrightAttachment, XmATTACH_WIDGET,
- XmNrightWidget, TransScope,
- XmNrightOffset, 10,
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- /*
- * Create Word box label and text box.
- */
- Word = XtVaCreateManagedWidget("word",
- xmTextWidgetClass, form,
- XmNcolumns, 25,
- XmNbackground, WhitePixelOfScreen(XtScreen(form)),
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNtopWidget, TransScope,
- XmNtopOffset, 10,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftWidget, TransScope,
- XmNleftOffset, 0,
- XmNfontList, FontList,
- NULL);
- XtAddCallback(Word, XmNfocusCallback, TextFocusCB, 0);
- label = XtVaCreateManagedWidget("wordlabel",
- xmLabelWidgetClass, form,
- XmNlabelString, xmstr = StringCreate("Word:"),
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, Word,
- XmNtopOffset, 7,
- XmNrightAttachment, XmATTACH_WIDGET,
- XmNrightWidget, Word,
- XmNrightOffset, 10,
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- /*
- * Create Word box label and text box.
- */
- Translation = XtVaCreateManagedWidget("translation",
- xmTextWidgetClass, form,
- XmNcolumns, 25,
- XmNbackground, WhitePixelOfScreen(XtScreen(form)),
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNtopWidget, Word,
- XmNtopOffset, 10,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftWidget, Word,
- XmNleftOffset, 0,
- XmNfontList, FontList,
- NULL);
- XtAddCallback(Translation, XmNfocusCallback, TextFocusCB, 0);
- label2 = XtVaCreateManagedWidget("translabel",
- xmLabelWidgetClass, form,
- XmNlabelString, xmstr = StringCreate("Translation:"),
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, Translation,
- XmNtopOffset, 7,
- XmNrightAttachment, XmATTACH_WIDGET,
- XmNrightWidget, Translation,
- XmNrightOffset, 10,
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- /*
- * Create the push buttons.
- */
- rowcol = XtVaCreateWidget("rowcol",
- xmRowColumnWidgetClass, form,
- XmNpacking, XmPACK_COLUMN,
- XmNnumColumns, 1,
- XmNorientation, XmVERTICAL,
- XmNentryAlignment, XmALIGNMENT_CENTER,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNleftWidget, Translation,
- XmNleftOffset, 10,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNtopWidget, menubar,
- XmNtopOffset, 5,
- XmNrightAttachment, XmATTACH_FORM,
- XmNrightOffset, 10,
- XmNbottomAttachment, XmATTACH_FORM,
- XmNbottomOffset, 10,
- XmNfontList, FontList,
- NULL);
- for (but = TL_BUT_START; but < TL_BUT_CNT; but++) {
- if (!inAPI && (but == TL_BUT_TRANSLATE)) {
- str = "Translate";
- } else {
- str = TlButtons[but];
- }
- Buttons[but] = button = XtVaCreateManagedWidget(TlButtons[but],
- xmPushButtonWidgetClass, rowcol,
- XmNlabelString, xmstr = StringCreate(TlButtons[but]),
- XmNmarginLeft, 4,
- XmNmarginRight, 4,
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- XtAddCallback(button, XmNactivateCallback, ButtonCB, (XtPointer)(int)but);
- }
- XtManageChild(rowcol);
- /*
- * Set initial dialog state.
- */
- SetDialogState();
- /*
- * Pop up the Server dialog.
- */
- XtPopup(Server, XtGrabNone);
- } /* ServerInit */
-
- /*COMMENT***************************************************
- ;ServerDestroyCB
- Title: Handle destroy of Server dialog.
- In: none
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes:
- ***********************************************************/
- static void ServerDestroyCB(Widget w, XtPointer cldata, XtPointer cbdata)
- {
- if (inAPI) {
- if (comm_id) {
- TermSession();
- WTCloseComm(comm_id);
- }
- comm_id = 0;
- }
- if (FontList) {
- XmFontListFree(FontList);
- }
- if (Icon) {
- XFreePixmap(XtDisplayOfObject(Server), Icon); /* free pixmap */
- }
- free_cursors(Server);
- Server = 0;
- ServerExit(); /* clean up and exit server */
- } /* ServerDestroyCB */
-
- /*COMMENT***************************************************
- ;ServerExit
- Title: Clean up and exit WTAPI Sample Application
- In: none
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes:
- ***********************************************************/
- static void ServerExit()
- {
- exit(0);
- } /* ServerExit */
-
- /*COMMENT***************************************************
- ;TextFocusCB
- Title: Clean up and exit WTAPI Sample Application
- In: none
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes:
- ***********************************************************/
- static void TextFocusCB(Widget w, XtPointer cldata, XtPointer cbdata)
- {
- Text = w; /* which text box has last had focus */
- } /* TextFocusCB */
-
- /*COMMENT***************************************************
- ;ButtonCB
- Title: Perform button action
- In: cldata - which button was pushed
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes:
- ***********************************************************/
- static void ButtonCB(Widget w, XtPointer cldata, XtPointer cbdata)
- {
- TL_BUT but = (TL_BUT) cldata;
- char *word;
- char oldword[BUFSIZE];
- char newword[BUFSIZE];
-
- switch (but) {
- case TL_BUT_START:
- if (!inAPI) {
- word = XmTextGetString(Word);
- if (word) {
- strncpy(oldword, word, BUFSIZE-1);
- oldword[BUFSIZE-1] = 0;
- XtFree(word);
- } else {
- oldword[0] = 0;
- }
- if (Mode == TL_TRANS_NATPIG) {
- NatToPig(oldword, newword);
- } else {
- PigToNat(oldword, newword);
- }
- XtVaSetValues(Translation, XmNvalue, newword, NULL);
- break;
- } else {
- if (!inLoop) {
- InitText();
- inLoop = TRUE;
- SetButtons();
- } else { /* Replace */
- word = XmTextGetString(Translation);
- if (word) {
- strncpy(newword, word, BUFSIZE-1);
- newword[BUFSIZE-1] = 0;
- XtFree(word);
- } else {
- newword[0] = 0;
- }
- ReplaceWord(newword);
- }
- }
- /* fall through to go to the next word */
- case TL_BUT_SKIP:
- if (but == TL_BUT_SKIP) { /* not just falling through */
- DeHilite();
- }
- if (!NextWord(oldword)) {
- MsgBox(MB_OK, Server, "About WTServer", "Translation Complete");
- inLoop = FALSE;
- SetButtons();
- } else {
- if (Mode == TL_TRANS_NATPIG) {
- NatToPig(oldword, newword);
- } else {
- PigToNat(oldword, newword);
- }
- PutWords(oldword, newword);
- }
- break;
- case TL_BUT_RETRANS:
- word = XmTextGetString(Word);
- if (word) {
- strncpy(oldword, word, BUFSIZE-1);
- oldword[BUFSIZE-1] = 0;
- XtFree(word);
- } else {
- oldword[0] = 0;
- }
- if (Mode == TL_TRANS_NATPIG) {
- NatToPig(oldword, newword);
- } else {
- PigToNat(oldword, newword);
- }
- XtVaSetValues(Translation, XmNvalue, newword, NULL);
- break;
- case TL_BUT_TRANSALL:
- if (!inLoop) {
- InitText();
- inLoop = TRUE;
- SetButtons();
- if (!NextWord(oldword)) {
- MsgBox(MB_OK, Server, "WTServer", "Translation Complete");
- } else {
- if (Mode == TL_TRANS_NATPIG) {
- NatToPig(oldword, newword);
- } else {
- PigToNat(oldword, newword);
- }
- }
- } else {
- word = XmTextGetString(Translation);
- if (word) {
- strncpy(newword, word, BUFSIZE-1);
- newword[BUFSIZE-1] = 0;
- XtFree(word);
- } else {
- newword[0] = 0;
- }
- }
- XLateAll(oldword, newword);
- MsgBox(MB_OK, Server, "WTServer", "Translation Complete");
- inLoop = FALSE;
- SetButtons();
- break;
- case TL_BUT_CLOSE:
- XtDestroyWidget(Server);
- break;
- default:
- break;
- }
- } /* ButtonCB */
-
- /*COMMENT***************************************************
- ;quitTool
- Title: Quit the writing tool
- In: msg - optional message to output before quitting the tool
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes:
- ***********************************************************/
- void quitTool(char *msg)
- {
- if (msg) {
- MsgBox(MB_OK, Server, "WTServer", msg);
- }
- XtDestroyWidget(Server);
- } /* quitTool */
-
- /*COMMENT***************************************************
- ;StringCreate
- Title: Initialize WTAPI Sample Application
- In: toplevel - toplevel widget of application
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes:
- ***********************************************************/
- XmString StringCreate(char *str)
- {
- return(XmStringCreateLtoR(str, XmSTRING_DEFAULT_CHARSET));
- } /* StringCreate */
-
- /* FUNCTION ********************************************************
- ;XmStringToString
- Title: Converts an XmString to a native string
- In: str - XmString to convert
- Out: none
- Return: pointer to native string (needs to be freed)
- Notes:
- *******************************************************************/
- char *XmStringToString(XmString str)
- {
- Boolean separator;
- char *ptr = 0, *tmp;
- XmStringContext context;
- XmStringCharSet charset;
- XmStringDirection direction;
-
- if (XmStringInitContext(&context, str)) {
- tmp = charset = 0;
- while (XmStringGetNextSegment(context,
- &tmp, &charset, &direction, &separator))
- {
- if (!strcmp((char *)charset,(char *)_XmStringGetCurrentCharset()) ||
- !strcmp((char *)charset,
- (char *)"FONTLIST_DEFAULT_TAG_STRING"))
- {
- int i;
-
- if (!ptr) {
- ptr = (char *)calloc(strlen(tmp)+1, 1);
- } else {
- ptr = (char *)realloc(ptr, strlen(tmp)+strlen(ptr)+1);
- }
- if (ptr) {
- char *sptr;
-
- for (i = strlen(ptr), sptr = tmp; *sptr; sptr++) {
- ptr[i++] = *sptr;
- }
- ptr[i] = 0;
- }
- else break;
- }
- XtFree(tmp);
- XtFree(charset);
- tmp = charset = 0;
- }
- XmStringFreeContext(context);
- if (tmp) {
- XtFree(tmp);
- }
- if (charset) {
- XtFree(charset);
- }
- }
- return ptr;
- } /* XmStringToString */
-
- /*COMMENT***************************************************
- ;TransCB
- Title: Translate menu actions.
- In: cldata - Translate action to perform
- Out: none
- Xin: Client - toplevel Client widget
- Xout: none
- Return: none
- Notes:
- ***********************************************************/
- static void TransCB(Widget w, XtPointer cldata, XtPointer cbdata)
- {
- TL_TRANS trans = (TL_TRANS)(int)cldata;
- XmToggleButtonCallbackStruct *cb = (XmToggleButtonCallbackStruct *)cbdata;
-
- /*
- * To enforce radio behavior among the two sets of buttons on the
- * Translate menu we only allow the user to select which option they
- * want. If the click on an option that is already toggled on we don't
- * allow it to be toggled off.
- */
- if (!cb->set) {
- XtVaSetValues(transButtons[trans], XmNset, True, NULL);
- return;
- }
-
- switch (trans) {
- case TL_TRANS_NATPIG: /* Native to Pig Latin */
- Mode = trans;
- XtVaSetValues(transButtons[TL_TRANS_PIGNAT], XmNset, False, NULL);
- SetTranslateText();
- break;
- case TL_TRANS_PIGNAT: /* Pig Latin to Native */
- Mode = trans;
- XtVaSetValues(transButtons[TL_TRANS_NATPIG], XmNset, False, NULL);
- SetTranslateText();
- break;
- case TL_TRANS_WORD:
- case TL_TRANS_DOC:
- case TL_TRANS_ENDDOC:
- case TL_TRANS_SELECT:
- XtVaSetValues(transButtons[CurSel], XmNset, False, NULL);
- CurSel = trans;
- XtVaSetValues(transButtons[CurSel], XmNset, True, NULL);
- SetTranslateText();
- break;
- default:
- break; /* invalid trans, do nothing */
- }
- } /* TransCB */
-
- /*COMMENT***************************************************
- ;EditCB
- Title: File menu actions.
- In: cldata - Edit action to perform
- Out: none
- Xin: Server - toplevel Server widget
- Xout: none
- Return: none
- Notes:
- ***********************************************************/
- static void EditCB(Widget w, XtPointer cldata, XmAnyCallbackStruct *cbdata)
- {
- TL_EDIT edit = (TL_EDIT)(int)cldata; /* which edit action to perform */
- Time time = cbdata->event->xbutton.time;
- char *txt, *prevtxt = 0;
- XmTextPosition prevleftsel = 0, prevrightsel = 0;
- int status = 0; /* status of call to clipboard routines */
- int len = 0; /* length of text in clipboard */
- BOOL changed = FALSE; /* did text change? */
- static Widget oldText; /* for Undo, text widget last changed */
-
- /*
- * If UNDO then we need to save the previous text and selection before
- * we update them to be the current text and selection.
- */
- if (edit == TL_EDIT_UNDO) {
- if (prevText) {
- prevtxt = prevText; /* use the same pointer */
- prevText = 0; /* we'll allocate more space for this later */
- }
- prevleftsel = prevSelLeft;
- prevrightsel = prevSelRight;
- Text = oldText;
- }
- if (!Text) {
- return;
- }
- oldText = Text;
- /*
- * Save the current text and selection as the previous text and selection
- * for possible use with UNDO later.
- */
- if (prevText) {
- XtFree(prevText);
- }
- prevText = XmTextGetString(Text);
- if (!XmTextGetSelectionPosition(Text, &prevSelLeft, &prevSelRight)) {
- prevSelLeft = prevSelRight = 0;
- }
- /*
- * Perform the Edit action.
- */
- switch (edit) {
- case TL_EDIT_UNDO: /* Undo */
- if (prevtxt) {
- XmTextSetString(Text, prevtxt);
- XtFree(prevtxt);
- if (prevleftsel < prevrightsel) {
- XmTextSetSelection(Text, prevleftsel, prevrightsel, time);
- }
- } else {
- XmTextSetString(Text, "");
- XmTextClearSelection(Text, time);
- }
- break;
- case TL_EDIT_CUT: /* Cut */
- if (!XmTextCut(Text, time)) {
- XBell(XtDisplay(Text), 100); /* beep */
- }
- break;
- case TL_EDIT_COPY: /* Copy */
- if (!XmTextCopy(Text, time)) {
- XBell(XtDisplay(Text), 100); /* beep */
- }
- break;
- case TL_EDIT_PASTE: /* Paste */
- if (!XmTextPaste(Text)) {
- XBell(XtDisplay(Text), 100); /* beep */
- }
- break;
- default: /* unrecognized action */
- break; /* do nothing */
- }
- } /* EditCB */
-
- /*COMMENT***************************************************
- ;SetEditButtons
- Desc: Set the sensitivity on the Edit buttons.
- In: none
- Out: none
- Xin: none
- Xout: none
- Return: none
- ***********************************************************/
- static void SetEditButtons(Widget w, XtPointer cldata, XtPointer cbdata)
- {
- char *sel; /* selected text */
- int status = 0; /* status of call to clipboard routines */
- int length = 0; /* length of text in clipboard */
-
- if (!Text) {
- return;
- }
- /*
- * If no edits yet then turn Undo off.
- */
- if (prevSelLeft == 0xff && prevSelRight == 0) {
- XtSetSensitive(editButtons[TL_EDIT_UNDO], False);
- } else {
- XtSetSensitive(editButtons[TL_EDIT_UNDO], True);
- }
- /*
- * If there is selected text in the box then turn on Cut and Copy.
- */
- sel = XmTextGetSelection(Text);
- if (sel && *sel) {
- XtSetSensitive(editButtons[TL_EDIT_CUT], True);
- XtSetSensitive(editButtons[TL_EDIT_COPY], True);
- } else {
- XtSetSensitive(editButtons[TL_EDIT_CUT], False);
- XtSetSensitive(editButtons[TL_EDIT_COPY], False);
- }
- if (sel) {
- XtFree(sel);
- }
- /*
- * Turn Paste on if there is text in the clipboard.
- */
- do {
- status = XmClipboardInquireLength(XtDisplay(w), XtWindow(w),
- "STRING", &length);
- } while (status == ClipboardLocked);
- if ((status != ClipboardNoData) && (length > 0)) {
- XtSetSensitive(editButtons[TL_EDIT_PASTE], True);
- } else {
- XtSetSensitive(editButtons[TL_EDIT_PASTE], False);
- }
- } /* SetEditButtons */
-
- /*COMMENT***************************************************
- ;HelpCB
- Title: Help menu actions.
- In: cldata - Help action to perform
- Out: none
- Xin: Server - toplevel Server widget
- Xout: none
- Return: none
- Notes:
- ***********************************************************/
- static void HelpCB(Widget w, XtPointer cldata, XtPointer cbdata)
- {
- TL_HELP help = (TL_HELP)(int)cldata; /* which help action to perform */
- switch (help) {
- case TL_HELP_ABOUT:
- MsgBox(MB_OK, Server, "About WTServer",
- "Writing Tools API\nSample Server Application\n\nVersion 0.6");
- break;
- default: /* unrecognized action */
- break; /* do nothing */
- }
- } /* HelpCB */
-
- /*COMMENT***************************************************
- ;change_cursors
- Title: Turn on a cursor for a widget and its ancestors
- In: w = widget
- cursor = the cursor
- Out: none
- Xin: none
- Xout: none
- Ret: none
- Notes:
- ***********************************************************/
- static void change_cursors(Widget w, Cursor cursor)
- {
- while (w) {
- if (XtDisplayOfObject(w) && XtWindowOfObject(w)) {
- XDefineCursor(
- XtDisplayOfObject(w),
- XtWindowOfObject(w),
- cursor);
- XSync(XtDisplayOfObject(w), False);
- }
- w = XtParent(w);
- }
- } /* change_cursors */
-
- /*COMMENT***************************************************
- ;WaitCursor
- Title: Turn on wait cursor (Watch)
- In: w = widget
- Out: none
- Xin: none
- Xout: none
- Ret: none
- Notes:
- ***********************************************************/
- void WaitCursor(Widget w)
- {
- if (!waitcursor) {
- waitcursor = XCreateFontCursor(XtDisplayOfObject(w), XC_watch);
- }
- change_cursors(w, waitcursor);
- } /* WaitCursor */
-
- /*COMMENT***************************************************
- ;NormalCursor
- Title: Turn on normal cursor (Arrow)
- In: w = widget
- Out: none
- Xin: none
- Xout: none
- Ret: none
- Notes:
- ***********************************************************/
- void NormalCursor(Widget w)
- {
- if (!normalcursor) {
- normalcursor = XCreateFontCursor(XtDisplayOfObject(w), XC_left_ptr);
- }
- change_cursors(w, normalcursor);
- } /* NormalCursor */
-
- /*COMMENTS********************************
- ;free_cursors
- Title: Free cursors we created
- In: w - any widget on same display as cursors
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes:
- *END**************************************/
- static void free_cursors(Widget w)
- {
- if (normalcursor) {
- XFreeCursor(XtDisplay(w), normalcursor);
- }
- if (waitcursor) {
- XFreeCursor(XtDisplay(w), waitcursor);
- }
- normalcursor = 0; /* for safety */
- waitcursor = 0; /* for safety */
- } /* free_cursors */
-
- /*COMMENT***************************************************
- ;MsgBoxCB
- Title: MsgBox dialog callback
- In: cldata - 1 = Yes, 0 = No
- Out: none
- Return: none
- Xin: none
- Xout: none
- Notes:
- ***********************************************************/
- static void MsgBoxCB(Widget w, XtPointer cldata, XtPointer cbdata)
- {
- MsgBoxRet = ((int)cldata == 1);
- if (msgDialog) {
- XtDestroyWidget(msgDialog);
- }
- msgDialog = 0;
- } /* MsgBoxCB */
-
- /*COMMENT***************************************************
- ;MsgBox
- Title: Bring up an OK or Yes/No message dialog.
- In: type - type of message box
- parent - parent widget
- title - dialog title
- message - message/question for dialog
- Out: none
- Xin: none
- Xout: none
- Return: TRUE - Yes, FALSE - No
- Notes:
- ***********************************************************/
- BOOL MsgBox(MSGBOX type, Widget parent, char *title, char *message)
- {
- Arg args[14]; /* Args for XtSetArg() */
- Cardinal argcnt = 0; /* number of Args set */
- XmString titleStr, messageStr, yesStr, noStr;
- XEvent event;
-
- MsgBoxRet = False;
- titleStr = StringCreate(title);
- messageStr = StringCreate(message);
- if (type == MB_YESNO) {
- yesStr = StringCreate("Yes");
- noStr = StringCreate("No");
- } else {
- yesStr = StringCreate("OK");
- }
- argcnt = 0;
- XtSetArg(args[argcnt], XmNdialogTitle, titleStr); argcnt++;
- XtSetArg(args[argcnt], XmNmessageString, messageStr); argcnt++;
- XtSetArg(args[argcnt], XmNokLabelString, yesStr); argcnt++;
- XtSetArg(args[argcnt], XmNtextFontList, FontList); argcnt++;
- XtSetArg(args[argcnt], XmNbuttonFontList, FontList); argcnt++;
- XtSetArg(args[argcnt], XmNlabelFontList, FontList); argcnt++;
- XtSetArg(args[argcnt], XmNdialogStyle,
- XmDIALOG_PRIMARY_APPLICATION_MODAL); argcnt++;
- XtSetArg(args[argcnt], XmNmessageAlignment, XmALIGNMENT_CENTER); argcnt++;
- if (type == MB_YESNO) {
- XtSetArg(args[argcnt], XmNcancelLabelString, noStr); argcnt++;
- msgDialog = XmCreateQuestionDialog(parent, "quesDialog", args, argcnt);
- } else {
- msgDialog = XmCreateInformationDialog(parent, "infoDialog", args, argcnt);
- }
- XmStringFree(titleStr);
- XmStringFree(messageStr);
- XmStringFree(yesStr);
- if (type == MB_YESNO) {
- XmStringFree(noStr);
- XtAddCallback(msgDialog, XmNcancelCallback, MsgBoxCB, 0);
- } else {
- XtUnmanageChild(XmMessageBoxGetChild(msgDialog,XmDIALOG_CANCEL_BUTTON));
- }
- XtAddCallback(msgDialog, XmNokCallback, MsgBoxCB, (XtPointer) 1);
- XtUnmanageChild(XmMessageBoxGetChild(msgDialog, XmDIALOG_HELP_BUTTON));
- XtManageChild(msgDialog);
- /*
- * Process events until dialog is closed.
- */
- for (;;) {
- XtAppNextEvent(XtWidgetToApplicationContext(parent), &event);
- XtDispatchEvent(&event);
- if (!msgDialog) {
- break;
- }
- }
- return(MsgBoxRet);
- } /* MsgBox */
-
- /*COMMENT***************************************************
- ;NatToPig
- Title: Convert a native word to pig latin
- In: natword - native word
- Out: pigword - word in pig latin
- Xin: none
- Xout: none
- Return: TRUE - success, FALSE - didn't translate word
- Notes: natword must be null terminated
- ***********************************************************/
- static BOOL NatToPig(char *natword, char *pigword)
- {
- int length, vowlen;
-
- length = strlen(natword); /* length of whole word */
- for (vowlen = 0; vowlen < length; ++vowlen) { /* locate first vowel */
- if (isvowel(natword[vowlen])) {
- break;
- }
- if ( (vowlen > 0) &&
- ((natword[vowlen] == 'y') || (natword[vowlen] == 'Y')))
- break;
- }
- if (vowlen == length) {
- if (length >= BUFSIZE-1) {
- natword[BUFSIZE-1] = 0;
- }
- strcpy(pigword, natword);
- return FALSE; /* no vowels, don't change the word */
- } else if (vowlen == 0) {
- if (length >= BUFSIZE-4) {
- natword[BUFSIZE-4] = 0;
- }
- strcpy(pigword, natword);
- strcat(pigword, "hay"); /* starts with vowel, copy 'hay' to end */
- } else {
- if (length >= BUFSIZE-3) {
- natword[BUFSIZE-3] = 0;
- }
- strcpy(pigword, natword + vowlen); /* rearrange to piglatin format */
- strncat(pigword, natword, vowlen);
- strcat(pigword, "ay"); /* add piglatin suffix */
- }
- return TRUE;
- } /* NatToPig */
-
- /*COMMENT***************************************************
- ;PigToNat
- Title: Convert a pig latin word to native
- In: pigword - native word
- Out: natword - word in pig latin
- Xin: none
- Xout: none
- Return: TRUE - success, FALSE - didn't translate word
- Notes: pigword must be null terminated
- ***********************************************************/
- static BOOL PigToNat(char *pigword, char *natword)
- {
- int length = strlen(pigword);
-
- /* check for a valid pig latin word */
- if (((pigword[length-1] != 'y') && (pigword[length-1] != 'Y')) ||
- ((pigword[length-2] != 'a') && (pigword[length-2] != 'A')))
- {
- MsgBox(MB_OK, Server, "WTServer Error", "Not a Valid Pig Latin Word!");
- natword[0] = 0;
- return FALSE;
- }
- if (!isvowel(pigword[0]) &&
- (pigword[0] != 'y') && (pigword[0] != 'Y'))
- {
- MsgBox(MB_OK, Server, "WTServer Error", "Not a Valid Pig Latin Word!");
- natword[0] = 0;
- return FALSE;
- }
- /* it was a valid word, go ahead and offer translation possiblities */
- PromptTranslateOptions(pigword);
- strcpy(natword, gbuf); /* copy the new word */
- return TRUE;
- } /* PigToNat */
-
- /*COMMENT***************************************************
- ;InitText
- Title: Initialize the text buffers
- In: none
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes: none
- ***********************************************************/
- static void InitText(void)
- {
- WTQTEXTBLOCKP inmsg;
- WTRTEXTBLOCKP rtmsg;
- WTSTATUS status;
-
- /* load parms into query parm table */
- inmsg = (WTQTEXTBLOCKP)sndbuf;
- inmsg->msgid = WTQ_TEXTBLOCK;
- inmsg->frompos = WTP_REL;
- inmsg->fromloc = 0;
- inmsg->topos = WTP_REL;
- inmsg->toloc = 0;
- inmsg->qtype = WTB_INIT;
- inmsg->containers = WT_FALSE;
- inmsg->tracking = WT_FALSE;
- inmsg->size = TEXTSIZE;
- inmsg->windowTool = XtWindow(Server);
- /* figure out units from CurSel */
- switch (CurSel) {
- case TL_TRANS_WORD:
- inmsg->fromunit = WTU_WORD;
- inmsg->tounit = WTU_WORD;
- break;
- case TL_TRANS_DOC:
- inmsg->fromunit = WTU_DOCUMENT;
- inmsg->tounit = WTU_DOCUMENT;
- break;
- case TL_TRANS_ENDDOC:
- inmsg->fromunit = WTU_WORD;
- inmsg->tounit = WTU_DOCUMENT;
- break;
- case TL_TRANS_SELECT:
- inmsg->fromunit = WTU_SELECTION;
- inmsg->tounit = WTU_SELECTION;
- break;
- default:
- break;
- }
- rtmsg = (WTRTEXTBLOCKP)rcvbuf;
- status = wtqTlSend(comm_id, WTQ_TEXTBLOCK, sndbuf, sizeof(WTQTEXTBLOCK),
- NULL, 0, WTR_TEXTBLOCK, rcvbuf, sizeof(WTRTEXTBLOCK), txtbuf, TEXTSIZE);
- switch (status) {
- case WTS_OK:
- break;
- case WTS_COMERROR:
- XBell(XtDisplay(Server), 100);
- MsgBox(MB_OK, Server, "WTServer Error",
- "Writing Tools API communication error!");
- ServerDestroyCB(0, 0, 0);
- break;
- default:
- XBell(XtDisplay(Server), 100);
- MsgBox(MB_OK, Server, "WTServer Error",
- "Action could not be completed.");
- break;
- }
- txtsize = (int)rtmsg->size;
- txtbeg = txtend = 0;
- endflag = rtmsg->endblock;
- return;
- } /* InitText */
-
- /*COMMENT***************************************************
- ;SetTranslateText
- Title: Update the Translation Mode and Translate text.
- In: cldata - Translate action to perform
- Out: none
- Xin: TransMode, TransScope
- Xout: none
- Return: none
- Notes:
- ***********************************************************/
- static void SetTranslateText()
- {
- XmString xmstr;
-
- xmstr = StringCreate(TlTransStr[Mode]);
- XtVaSetValues(TransMode, XmNlabelString, xmstr, NULL);
- XmStringFree(xmstr);
- xmstr = StringCreate(TlTransStr[CurSel]);
- XtVaSetValues(TransScope, XmNlabelString, xmstr, NULL);
- XmStringFree(xmstr);
- }/* SetTranslateText */
-
- /*COMMENT***************************************************
- ;SetButtons
- Title: Set the state of the buttons in the dialog
- In: none
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes: none
- ***********************************************************/
- static void SetButtons()
- {
- XmString xmstr;
-
- if (inAPI) {
- if (inLoop) {
- xmstr = StringCreate("Replace");
- XtVaSetValues(Buttons[TL_BUT_START], XmNlabelString, xmstr, NULL);
- XmStringFree(xmstr);
- XtSetSensitive(Buttons[TL_BUT_SKIP], True);
- } else {
- xmstr = StringCreate("Start");
- XtVaSetValues(Buttons[TL_BUT_START], XmNlabelString, xmstr, NULL);
- XmStringFree(xmstr);
- XtSetSensitive(Buttons[TL_BUT_SKIP], False);
- }
- XtSetSensitive(Buttons[TL_BUT_RETRANS], True);
- XtSetSensitive(Buttons[TL_BUT_TRANSALL], True);
- XtSetSensitive(Buttons[TL_BUT_CLOSE], True);
- } else {
- xmstr = StringCreate("Translate");
- XtVaSetValues(Buttons[TL_BUT_START], XmNlabelString, xmstr, NULL);
- XmStringFree(xmstr);
- XtSetSensitive(Buttons[TL_BUT_SKIP], False);
- XtSetSensitive(Buttons[TL_BUT_RETRANS], False);
- XtSetSensitive(Buttons[TL_BUT_TRANSALL], False);
- XtSetSensitive(Buttons[TL_BUT_CLOSE], True);
- }
- } /* SetButtons */
-
- /*COMMENT***************************************************
- ;SetDialogState
- Title: Set the initial state of the main dialog
- In: none
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes: none
- ***********************************************************/
- void SetDialogState()
- {
- if (inAPI) {
- XtSetSensitive(transButtons[TL_TRANS_WORD], True);
- XtSetSensitive(transButtons[TL_TRANS_DOC], True);
- XtSetSensitive(transButtons[TL_TRANS_ENDDOC], True);
- if (inBlock) {
- XtSetSensitive(transButtons[TL_TRANS_SELECT], True);
- XtVaSetValues(transButtons[CurSel], XmNset, False, NULL);
- CurSel = TL_TRANS_SELECT;
- XtVaSetValues(transButtons[CurSel], XmNset, True, NULL);
- SetTranslateText();
- } else {
- XtSetSensitive(transButtons[TL_TRANS_SELECT], False);
- }
- } else {
- XtSetSensitive(transButtons[TL_TRANS_WORD], True);
- XtSetSensitive(transButtons[TL_TRANS_DOC], False);
- XtSetSensitive(transButtons[TL_TRANS_ENDDOC], False);
- XtSetSensitive(transButtons[TL_TRANS_SELECT], False);
- }
- SetButtons(); /* set the buttons too */
- SetTranslateText(); /* set translation mode and scope */
- return;
- } /* SetDialogState */
-
- /*COMMENT***************************************************
- ;DeHilite
- Title: Dehighlights the text in the client buffer
- In: none
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes: none
- ***********************************************************/
- static void DeHilite()
- {
- WTQDEHILITEP inmsg = (WTQDEHILITEP)sndbuf;
- WTSTATUS status;
-
- inmsg->msgid = WTQ_DEHILITE;
- inmsg->count = 0; /* dehilite all */
- status = wtqTlSend(comm_id, WTQ_DEHILITE, sndbuf, sizeof(WTQ_DEHILITE),
- NULL, 0, WTR_DEHILITE, rcvbuf, sizeof(WTR_DEHILITE), NULL, 0);
- switch (status) {
- case WTS_OK:
- break;
- case WTS_COMERROR:
- XBell(XtDisplay(Server), 100);
- MsgBox(MB_OK, Server, "WTServer Error",
- "Writing Tools API communication error!");
- ServerDestroyCB(0, 0, 0);
- break;
- default :
- XBell(XtDisplay(Server), 100);
- MsgBox(MB_OK, Server, "WTServer Error",
- "Action could not be completed.");
- break;
- }
- return;
- } /* DeHilite */
-
- /*COMMENT***************************************************
- ;NextWord
- Title: Get the next word from a phrase and increment pointer
- In: none
- Out: word - retrieved word
- Xin: none
- Xout: none
- Return: TRUE - another word was available, FALSE - no more words
- Notes: none
- ***********************************************************/
- static BOOL NextWord(char *word)
- {
- WTQGOTOP ingoto;
- WTQHILITEP inlite;
- WTSTATUS status;
-
- for (;;) { /* loop until we get a word */
- for (txtbeg = txtend; /* start at end of last word */
- (txtbeg < txtsize) && !isalpha(txtbuf[txtbeg]);
- txtbeg++) /* empty */; /* locate start position of next word */
- if (txtbeg == txtsize) { /* reached end of block */
- if (endflag == WTE_ENDQUERY) {
- return FALSE; /* no more text */
- } else {
- NextBlock(0); /* put next block at txtbuf[0] */
- }
- #if 0
- /*
- * This code handles the case where the client may send a partial word that
- * was truncated because it wouldn't fit in the block being sent. If
- * this type of checking is desired then the server must keep track of
- * which block the text is in when identifying goto, highlight, and replacement
- * positions.
- */
- } else if ((endflag == WTE_FULL) && (txtsize - txtbeg) < WORDSIZE) {
- NextBlock(txtsize - txtbeg);
- #endif
- } else {
- break; /* word is OK */
- }
- }
- for (txtend = txtbeg;
- (txtend < txtsize) && isalpha(txtbuf[txtend]);
- txtend++) /* empty */; /* locate end postion of word */
- /* save word found */
- memcpy((char *)word, (char *)&txtbuf[txtbeg], (txtend - txtbeg));
- word[txtend - txtbeg] = 0x0; /* null terminate word */
- /* goto the word in the client buffer and highlight it */
- ingoto = (WTQGOTOP)sndbuf;
- ingoto->msgid = WTQ_GOTO;
- ingoto->blockpos = WTP_REL;
- ingoto->blocknum = 0; /* the current block */
- ingoto->offsetpos = WTP_END;
- ingoto->offsetloc = txtsize - txtbeg;
- status = wtqTlSend(comm_id, WTQ_GOTO, sndbuf, sizeof(WTQGOTO), NULL, 0,
- WTR_GOTO, rcvbuf, sizeof(WTRGOTO), NULL, 0);
- switch (status) {
- case WTS_OK:
- break;
- case WTS_COMERROR:
- XBell(XtDisplay(Server), 100);
- MsgBox(MB_OK, Server, "WTServer Error",
- "Writing Tools API communication error!");
- ServerDestroyCB(0, 0, 0);
- break;
- default :
- XBell(XtDisplay(Server), 100);
- MsgBox(MB_OK, Server, "WTServer Error",
- "Action could not be completed.");
- break;
- }
- inlite = (WTQHILITEP)sndbuf;
- inlite->msgid = WTQ_HILITE;
- inlite->count = txtend - txtbeg;
- status = wtqTlSend(comm_id, WTQ_HILITE, sndbuf, sizeof(WTQHILITE), NULL, 0,
- WTR_HILITE, rcvbuf, sizeof(WTRHILITE), NULL, 0);
- switch (status) {
- case WTS_OK:
- break;
- case WTS_COMERROR:
- XBell(XtDisplay(Server), 100);
- MsgBox(MB_OK, Server, "WTServer Error",
- "Writing Tools API communication error!");
- ServerDestroyCB(0, 0, 0);
- break;
- default :
- XBell(XtDisplay(Server), 100);
- MsgBox(MB_OK, Server, "WTServer Error",
- "Action could not be completed.");
- break;
- }
- return TRUE;
- } /* NextWord */
-
- /*COMMENT***************************************************
- ;NextBlock
- Title: Retrieves the next block of text from the client.
- In: loc - location in current txtbuf to place the text
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes: none
- ***********************************************************/
- static void NextBlock(int loc)
- {
- WTQNEXTTEXTBLOCKP inmsg;
- WTRTEXTBLOCKP rtmsg;
- WTSTATUS status;
-
- inmsg = (WTQNEXTTEXTBLOCKP)sndbuf;
- rtmsg = (WTRTEXTBLOCKP)rcvbuf;
- inmsg->msgid = WTQ_NEXTTEXTBLOCK;
- inmsg->size = TEXTSIZE;
- if (loc) {
- memcpy((char *)txtbuf, (char *)&txtbuf[txtsize - loc], loc);
- }
- status = wtqTlSend(comm_id, WTQ_NEXTTEXTBLOCK,
- sndbuf, sizeof(WTQNEXTTEXTBLOCK), NULL, 0, WTR_TEXTBLOCK,
- rcvbuf, sizeof(WTRTEXTBLOCK), txtbuf + loc, TEXTSIZE);
- switch (status) {
- case WTS_OK:
- break;
- case WTS_COMERROR:
- XBell(XtDisplay(Server), 100);
- MsgBox(MB_OK, Server, "WTServer Error",
- "Writing Tools API communication error!");
- ServerDestroyCB(0, 0, 0);
- break;
- default :
- XBell(XtDisplay(Server), 100);
- MsgBox(MB_OK, Server, "WTServer Error",
- "Action could not be completed.");
- break;
- }
- endflag = rtmsg->endblock;
- txtsize = (int)rtmsg->size + loc;
- txtbeg = txtend = 0;
- return;
- } /* NextBlock */
-
- /*COMMENT***************************************************
- ;PutWords
- Title: Output the native and pig latin words to dialog
- In: natword = the native word
- pigword = the pig latin translation
- Out: none
- Xin: none
- Xout: none
- Return: none
- Notes: none
- ***********************************************************/
- static void PutWords(char *natword, char *pigword)
- {
- XtVaSetValues(Word, XmNvalue, natword, NULL);
- XtVaSetValues(Translation, XmNvalue, pigword, NULL);
- } /* PutWords */
-
- /* FUNCTION ********************************************************
- ;XLateAll
- Title: Translate a block of text
- In: oldword - old word
- newword - new word
- Out: none
- Return: none
- Notes:
- *******************************************************************/
- static void XLateAll(char *oldword, char *newword)
- {
- for (;;) {
- ReplaceWord(newword); /* replace the current word */
- if (!NextWord(oldword)) { /* get next word */
- break;
- }
- if (Mode == TL_TRANS_NATPIG) {
- NatToPig(oldword, newword);
- } else {
- PigToNat(oldword, newword);
- }
- }
- return;
- } /* XLateAll */
-
- /* FUNCTION ********************************************************
- ;ReplaceWord
- Title: Replace inWord with outWord
- In: none
- Out: newword - the word you want placed in the document
- Return:
- Notes: the document in the cursor must be positioned on the old word
- *******************************************************************/
- static void ReplaceWord(char *newword)
- {
- WTQREPLACEP inmsg;
- WTSTATUS status;
- int oldlen, newlen;
-
- oldlen = txtend - txtbeg; /* len of oldword */
- newlen = strlen(newword); /* len of newword */
- /* wtapi replace */
- inmsg = (WTQREPLACEP)sndbuf;
- inmsg->msgid = WTQ_REPLACE; /* message id */
- inmsg->language.script = 0x00; /* Roman */
- inmsg->language.language = 0x00; /* English */
- inmsg->language.region = 0x0001; /* U.S.A. */
- inmsg->depth = 0; /* relative depth of insertion */
- inmsg->count = oldlen; /* len of deleted word */
- inmsg->newblock = WT_FALSE;
- inmsg->size = newlen; /* len of inserted word */
- status = wtqTlSend(comm_id, WTQ_REPLACE, sndbuf, sizeof(WTQREPLACE),
- (WTBUFP)newword, (WTSIZE)newlen,
- WTR_REPLACE, rcvbuf, sizeof(WTRREPLACE),
- NULL, 0);
- switch (status) {
- case WTS_OK:
- break;
- case WTS_COMERROR:
- XBell(XtDisplay(Server), 100);
- MsgBox(MB_OK, Server, "WTServer Error",
- "Writing Tools API communication error!");
- ServerDestroyCB(0, 0, 0);
- break;
- default :
- XBell(XtDisplay(Server), 100);
- MsgBox(MB_OK, Server, "WTServer Error",
- "Action could not be completed.");
- break;
- }
- return;
- } /* ReplaceWord */
-
- /* FUNCTION ********************************************************
- ;PromptTranslateOptions
- Title: Translate a block of text
- In: pigword - pig latin word
- Out: none
- Return: none
- Notes: Assumes a valid pig latin word
- *******************************************************************/
- static void PromptTranslateOptions(char *pigword)
- {
- Arg args[10];
- Cardinal argcnt;
- XmString xmstr;
- Atom deleteWindow; /* for XmAddWMProtocol... */
- Widget form, /* form */
- wordlabel, /* label for pig latin word box */
- wordbox, /* box to display pig latin word */
- translabel, /* label for list of translations */
- separator, /* separator */
- button; /* OK button */
- int cons; /* position of consonant for start of option word */
- int len; /* length of pigword */
- char *p, *q; /* pointers */
- char oldword[BUFSIZE]; /* copy of pigword */
- char newword[BUFSIZE]; /* possible translation word */
- int cnt; /* number of option words */
- XEvent event;
-
- len = strlen(pigword);
- if (len < 3) {
- return; /* can't be a valid pig latin word */
- }
-
- argcnt = 0;
- XtSetArg(args[argcnt], XtNtitle, "Select a Translation");
- argcnt++;
- form = XmCreateFormDialog(Server, "trform", args, argcnt);
- TransOpts = XtParent(form);
- XtAddCallback(TransOpts, XmNdestroyCallback, TransOptCB, 0);
- deleteWindow = XmInternAtom(XtDisplay(TransOpts), "WM_DELETE_WINDOW",False);
- XmAddWMProtocols(TransOpts, &deleteWindow, 1);
- XmAddWMProtocolCallback(TransOpts, deleteWindow, TransOptCB, 0);
- /*
- * Create the Pig Latin Word label and box.
- */
- wordlabel = XtVaCreateManagedWidget("wordlabel",
- xmLabelWidgetClass, form,
- XmNlabelString, xmstr = StringCreate("Pig Latin Word:"),
- XmNtopAttachment, XmATTACH_FORM,
- XmNtopOffset, 10,
- XmNleftAttachment, XmATTACH_FORM,
- XmNleftOffset, 10,
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- wordbox = XtVaCreateManagedWidget("wordbox",
- xmTextWidgetClass, form,
- XmNrows, 1,
- XmNcolumns, 25,
- XmNvalue, pigword,
- XmNeditable, False,
- XmNbackground, WhitePixelOfScreen(XtScreen(form)),
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNtopWidget, wordlabel,
- XmNtopOffset, 2,
- XmNleftAttachment, XmATTACH_FORM,
- XmNleftOffset, 10,
- XmNrightAttachment, XmATTACH_FORM,
- XmNrightOffset, 10,
- XmNfontList, FontList,
- NULL);
- /*
- * Create the Translation label and list.
- */
- translabel = XtVaCreateManagedWidget("translabel",
- xmLabelWidgetClass, form,
- XmNlabelString, xmstr = StringCreate("Select a Translation:"),
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNtopWidget, wordbox,
- XmNtopOffset, 10,
- XmNleftAttachment, XmATTACH_FORM,
- XmNleftOffset, 10,
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- TransList = XtVaCreateManagedWidget("translist",
- xmListWidgetClass, form,
- XmNvisibleItemCount, 5,
- XmNcolumns, 25,
- XmNvalue, pigword,
- XmNeditable, False,
- XmNbackground, WhitePixelOfScreen(XtScreen(form)),
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNtopWidget, translabel,
- XmNtopOffset, 2,
- XmNleftAttachment, XmATTACH_FORM,
- XmNleftOffset, 10,
- XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightWidget, wordbox,
- XmNrightOffset, 0,
- XmNfontList, FontList,
- NULL);
- XtAddCallback(TransList, XmNdefaultActionCallback, TransOptCB, (XtPointer)1);
- /*
- * Create separator.
- */
- separator = XtVaCreateManagedWidget("sep",
- xmSeparatorWidgetClass, form,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNtopWidget, TransList,
- XmNtopOffset, 10,
- XmNleftAttachment, XmATTACH_FORM,
- XmNleftOffset, 0,
- XmNrightAttachment, XmATTACH_FORM,
- XmNrightOffset, 0,
- NULL);
- /*
- * Create the OK button.
- */
- button = XtVaCreateManagedWidget("OK",
- xmPushButtonWidgetClass, form,
- XmNlabelString, xmstr = StringCreate("OK"),
- XmNmarginLeft, 40,
- XmNmarginRight, 40,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNtopWidget, separator,
- XmNtopOffset, 10,
- XmNleftAttachment, XmATTACH_FORM,
- XmNleftOffset, 60,
- XmNbottomAttachment, XmATTACH_FORM,
- XmNbottomOffset, 10,
- XmNfontList, FontList,
- NULL);
- XmStringFree(xmstr);
- XtAddCallback(button, XmNactivateCallback, TransOptCB, (XtPointer)1);
- /*
- * Build the options list. The alogrithm is:
- * 1. If the consonant immediately preceding "ay" is an 'h', then include
- * the word created by simply stripping "hay" from the end.
- * 2. For each consonant before the trailing "ay" up to previous vowel:
- * a. Put consonant and any trailing consonants up to "ay" at
- * beginning of word.
- * b. Concatenate the remainder of word (from beginning up to the
- * beginning consonant being used) after these initial consonants.
- * c. Add word to list.
- */
- cnt = 0;
- if (pigword[len-3] == 'h') {
- strcpy(newword, pigword);
- newword[len-3] = 0;
- XmListAddItem(TransList, xmstr = StringCreate(newword), 0);
- XmStringFree(xmstr);
- cnt++;
- }
- for (cons = len-3; !isvowel(pigword[cons]); cons--) {
- strcpy(oldword, pigword);
- q = &oldword[cons];
- p = newword;
- while (*q && !isvowel(*q)) {
- *p++ = *q++;
- }
- *p = 0;
- oldword[cons] = 0;
- strcat(newword, oldword);
- XmListAddItem(TransList, xmstr = StringCreate(newword), 0);
- XmStringFree(xmstr);
- cnt++;
- }
- /* select first item in list */
- if (cnt < 1) {
- MsgBox(MB_OK, Server, "WTServer Error", "Not a Valid Pig Latin Word!");
- XtDestroyWidget(TransOpts);
- return;
- } else {
- XmListSelectPos(TransList, 1, True); /* select first item */
- }
- /*
- * Pop up the dialog.
- */
- XtManageChild(form);
- /*
- * Loop processing events until the user is finished with this dialog.
- */
- for (;;) {
- XtAppNextEvent(XtWidgetToApplicationContext(Server), &event);
- XtDispatchEvent(&event);
- if (!TransOpts) {
- break;
- }
- }
- } /* PromptTranslateOptions */
-
- /*COMMENT***************************************************
- ;TransOptCB
- Title: OK callback for Translation Options dialog
- In: cldata - 0 = destroy callback, 1 = OK or default action callback
- Out: none
- Return: none
- Xin: none
- Xout: gbuf - selected translation word
- Notes:
- ***********************************************************/
- static void TransOptCB(Widget w, XtPointer cldata, XtPointer cbdata)
- {
- int *lst = 0; /* list of selected items in list */
- int cnt = 0; /* number of selected items in list */
- XmStringTable items = 0; /* items in list */
- char *word; /* selected word from list */
-
- if (TransOpts) {
- /*
- * Get selected item.
- */
- if (!XmListGetSelectedPos(TransList, &lst, &cnt) ||
- !lst || (cnt != 1))
- {
- if (lst) {
- XtFree((char *)lst);
- }
- gbuf[0] = 0;
- return; /* no item selected */
- }
- XtVaGetValues(TransList, XmNitems, &items, NULL);
- word = XmStringToString(items[lst[0]-1]);
- XtFree((char *)lst);
- if (word) {
- strcpy(gbuf, word);
- free(word);
- } else {
- gbuf[0] = 0;
- }
-
- XtDestroyWidget(TransOpts);
- }
- TransOpts = 0;
- } /* TransOptCB */
-
- /* FUNCTION ********************************************************
- ;isvowel
- Title: Check if a character is a vowel (not including 'y')
- In: c - character to check
- Out: none
- Return: none
- Notes:
- *******************************************************************/
- static BOOL isvowel(char c)
- {
- return ((c == 'a') || (c == 'A') ||
- (c == 'e') || (c == 'E') ||
- (c == 'i') || (c == 'I') ||
- (c == 'o') || (c == 'O') ||
- (c == 'u') || (c == 'U'));
- } /* isvowel */
-