editor.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $TOG: editor.c /main/4 1998/04/02 18:18:47 rafi $ */
  24. /*
  25. * (c) Copyright 1993, 1994 Hewlett-Packard Company
  26. * (c) Copyright 1993, 1994 International Business Machines Corp.
  27. * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  28. * (c) Copyright 1993, 1994 Novell, Inc.
  29. */
  30. /*
  31. * editor.c
  32. *
  33. * A simple editor based on the DtEditor widget.
  34. */
  35. #include <stdio.h>
  36. #include <locale.h>
  37. #include <Xm/XmAll.h>
  38. #include <Dt/Editor.h>
  39. /*
  40. * Constants
  41. */
  42. #define ApplicationClass "Editor"
  43. /*
  44. * Types
  45. */
  46. typedef struct _AppData {
  47. XtAppContext appContext;
  48. Display *display;
  49. Widget top;
  50. Widget mainWindow;
  51. Widget menuBar;
  52. Widget editor;
  53. Widget messageTextF;
  54. Widget fileSelectionBox;
  55. Widget cutButton;
  56. Widget copyButton;
  57. Widget clearButton;
  58. Widget deleteButton;
  59. Widget deselectButton;
  60. Widget cutPopupButton;
  61. Widget copyPopupButton;
  62. Boolean wordWrapOn;
  63. Boolean statusLineOn;
  64. Boolean overstrikeModeOn;
  65. } AppData;
  66. /*
  67. * Used to specify whether data is being loaded, inserted, or saved
  68. * from the editor. Used used by the FSB and its callbacks.
  69. */
  70. typedef enum _LoadSaveType {
  71. LOAD_DATA,
  72. INSERT_DATA,
  73. SAVE_DATA
  74. } LoadSaveType;
  75. /*
  76. * Data
  77. */
  78. static AppData ad;
  79. /*
  80. * Functions
  81. */
  82. static void SetResizeHints(void);
  83. static void DisplayMessage(char *);
  84. static Widget CreateMenuBar(Widget);
  85. static Widget CreatePopupMenu(Widget);
  86. static Widget CreateEditor(Widget);
  87. static Widget CreateButton(Widget, String, char, XtCallbackProc, XtPointer);
  88. static Widget CreateToggle(Widget, String, char, XtCallbackProc, XtPointer, Boolean);
  89. static Widget CreateCascade(Widget, String, char, Widget);
  90. static void PopupHandler(Widget, XtPointer, XEvent*, Boolean*);
  91. static void SetSelectionState(Boolean);
  92. /*
  93. * main
  94. */
  95. void main(int argc, char **argv)
  96. {
  97. Arg al[10];
  98. int ac;
  99. XtSetLanguageProc( (XtAppContext)NULL, (XtLanguageProc)NULL,
  100. (XtPointer)NULL );
  101. /* Initialize the application's data */
  102. ad.fileSelectionBox = (Widget) NULL;
  103. ad.wordWrapOn = False;
  104. ad.statusLineOn = False;
  105. ad.overstrikeModeOn = False;
  106. /* Initialize the toolkit and open the display */
  107. ad.top = XtAppInitialize(&ad.appContext, ApplicationClass, NULL, 0,
  108. &argc, argv, NULL, NULL, 0);
  109. ad.display = XtDisplay(ad.top);
  110. /* Create MainWindow. */
  111. ac = 0;
  112. ad.mainWindow = (Widget) XmCreateMainWindow (ad.top, "main", al, ac);
  113. XtManageChild (ad.mainWindow);
  114. /* Create MenuBar in MainWindow. */
  115. ad.menuBar = CreateMenuBar(ad.mainWindow);
  116. XtManageChild(ad.menuBar);
  117. /* Create editor widget in MainWindow. */
  118. ad.editor = CreateEditor(ad.mainWindow);
  119. XtManageChild(ad.editor);
  120. ad.messageTextF = DtEditorGetMessageTextFieldID(ad.editor);
  121. /* Create the editor popup menu */
  122. CreatePopupMenu(ad.editor);
  123. /* Set the main window widgets. */
  124. XmMainWindowSetAreas(ad.mainWindow, ad.menuBar, NULL, NULL, NULL, ad.editor);
  125. /* Realize toplevel widget */
  126. XtRealizeWidget (ad.top);
  127. /* Set the resize increment and minimum window size properties. */
  128. SetResizeHints();
  129. /* Set up menu buttons dependent on selection */
  130. SetSelectionState(False);
  131. XtAppMainLoop(ad.appContext);
  132. }
  133. /************************************************************************
  134. *
  135. * Callbacks
  136. *
  137. ************************************************************************/
  138. /*
  139. * File menu callbacks
  140. */
  141. static void ResetEditorCb(Widget w, XtPointer cd, XtPointer cb)
  142. {
  143. DtEditorReset(ad.editor);
  144. }
  145. static void OpenFileCb( Widget w, XtPointer cd, XtPointer cb)
  146. {
  147. DtEditorErrorCode error;
  148. XmFileSelectionBoxCallbackStruct *fcb = (XmFileSelectionBoxCallbackStruct *) cb;
  149. LoadSaveType LoadSaveFlag = (LoadSaveType)cd;
  150. char *name = (char *) XtMalloc( sizeof(char) * fcb->length + 1 );
  151. name[0] ='\0';
  152. /*
  153. * Get the name of the file & pass it to the editor widget
  154. */
  155. name = XmStringUnparse(fcb->value, NULL, XmCHARSET_TEXT,
  156. XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL);
  157. /*
  158. * Load or insert the file, as specified
  159. */
  160. if ( LoadSaveFlag == LOAD_DATA )
  161. error = DtEditorSetContentsFromFile(ad.editor, name);
  162. else
  163. error = DtEditorInsertFromFile(ad.editor, name);
  164. switch (error)
  165. {
  166. case DtEDITOR_NO_ERRORS:
  167. {
  168. DisplayMessage("File loaded");
  169. break;
  170. }
  171. case DtEDITOR_NULLS_REMOVED:
  172. {
  173. DisplayMessage( "All embedded null characters removed from the file" );
  174. break;
  175. }
  176. case DtEDITOR_READ_ONLY_FILE:
  177. {
  178. DisplayMessage( "The file is write protected" );
  179. break;
  180. }
  181. case DtEDITOR_NONEXISTENT_FILE:
  182. {
  183. DisplayMessage( "Could not find file" );
  184. break;
  185. }
  186. case DtEDITOR_DIRECTORY:
  187. {
  188. DisplayMessage( "The name given is a directory" );
  189. break;
  190. }
  191. case DtEDITOR_CHAR_SPECIAL_FILE:
  192. {
  193. DisplayMessage( "The name given is a character special device" );
  194. break;
  195. }
  196. case DtEDITOR_BLOCK_MODE_FILE:
  197. {
  198. DisplayMessage( "The name given is a block mode device");
  199. break;
  200. }
  201. case DtEDITOR_INSUFFICIENT_MEMORY:
  202. {
  203. DisplayMessage( "Not enough available memory to load file");
  204. break;
  205. }
  206. case DtEDITOR_UNREADABLE_FILE:
  207. default:
  208. {
  209. DisplayMessage( "Could not read the file" );
  210. break;
  211. }
  212. }
  213. /*
  214. * Remove the OK callback so it can be added again with a new value for
  215. * the LoadSaveFlag flag.
  216. */
  217. XtRemoveCallback(w, XmNokCallback, OpenFileCb, cd);
  218. /*
  219. * Remove the FSB dialog & clean up
  220. */
  221. XtUnmanageChild(w);
  222. XtFree (name);
  223. }
  224. static void SaveAsFileCb(Widget w, XtPointer cd, XtPointer cb)
  225. {
  226. DtEditorContentRec cr;
  227. DtEditorErrorCode error;
  228. Boolean overWrite = False,
  229. hardCarriageReturns = True,
  230. markContentsAsSaved = True;
  231. XmFileSelectionBoxCallbackStruct *fcb = (XmFileSelectionBoxCallbackStruct *)cb;
  232. LoadSaveType LoadSaveFlag = (LoadSaveType) cd;
  233. char *name = XtMalloc(sizeof(char) * fcb->length + 1 );
  234. name[0] ='\0';
  235. XmStringGetLtoR(fcb->value, XmFONTLIST_DEFAULT_TAG, &name);
  236. /*
  237. * Ask the widget to save its contents to the named file.
  238. */
  239. error = DtEditorSaveContentsToFile(ad.editor, name, overWrite,
  240. hardCarriageReturns, markContentsAsSaved);
  241. switch(error)
  242. {
  243. case DtEDITOR_NO_ERRORS:
  244. {
  245. DisplayMessage( "The file has been saved" );
  246. break;
  247. }
  248. case DtEDITOR_UNWRITABLE_FILE:
  249. {
  250. DisplayMessage( "The file is read only" );
  251. break;
  252. }
  253. case DtEDITOR_WRITABLE_FILE:
  254. {
  255. DisplayMessage( "File not saved, it already exists" );
  256. break;
  257. }
  258. case DtEDITOR_DIRECTORY:
  259. {
  260. DisplayMessage( "The name given is a directory" );
  261. break;
  262. }
  263. case DtEDITOR_CHAR_SPECIAL_FILE:
  264. {
  265. DisplayMessage( "The name given is a character special device" );
  266. break;
  267. }
  268. case DtEDITOR_BLOCK_MODE_FILE:
  269. {
  270. DisplayMessage( "The name given is a block mode device");
  271. break;
  272. }
  273. case DtEDITOR_SAVE_FAILED:
  274. default:
  275. {
  276. DisplayMessage( "Could not save the file. Check disc space" );
  277. break;
  278. }
  279. }
  280. /*
  281. * Remove the OK callback so it can be added again with a new value for
  282. * the LoadSaveFlag flag.
  283. */
  284. XtRemoveCallback(w, XmNokCallback, SaveAsFileCb, cd);
  285. /*
  286. * Remove the FSB dialog & clean up
  287. */
  288. XtUnmanageChild( w );
  289. XtFree (name);
  290. }
  291. static void CancelOpenCb(Widget w, XtPointer cd, XtPointer cb)
  292. {
  293. /* Remove the OK callback so it can be added again with a new value for
  294. * the LoadSaveFlag flag.
  295. */
  296. XtRemoveCallback(w, XmNokCallback, OpenFileCb, cd);
  297. /* Remove the FSB dialog & clean up */
  298. XtUnmanageChild(w);
  299. }
  300. static void DisplayFsbCb(Widget w, XtPointer cd, XtPointer cb)
  301. {
  302. Arg al[10];
  303. int ac;
  304. XmString tmpStr1, tmpStr2;
  305. LoadSaveType LoadSaveFlag = (LoadSaveType) cd;
  306. /* Create the FSB, if we need to */
  307. if (ad.fileSelectionBox == (Widget) NULL)
  308. {
  309. ac = 0;
  310. ad.fileSelectionBox = XmCreateFileSelectionDialog(ad.mainWindow,
  311. "file_sel_dlg",
  312. al, ac);
  313. XtAddCallback(ad.fileSelectionBox, XmNcancelCallback, CancelOpenCb,
  314. (XtPointer)LoadSaveFlag);
  315. }
  316. /*
  317. * Set FSB title & label depending up whether loading, inserting, or
  318. * saving a container.
  319. */
  320. switch ( LoadSaveFlag )
  321. {
  322. case LOAD_DATA:
  323. {
  324. tmpStr1 = XmStringCreateLocalized("Open a File");
  325. tmpStr2 = XmStringCreateLocalized("File to open");
  326. /*
  327. * Add the OK callback so the curent value of the LoadSaveFlag
  328. * flag is passed in.
  329. */
  330. XtAddCallback(ad.fileSelectionBox, XmNokCallback, OpenFileCb,
  331. (XtPointer)LoadSaveFlag);
  332. break;
  333. }
  334. case INSERT_DATA:
  335. {
  336. tmpStr1 = XmStringCreateLocalized("Include a File");
  337. tmpStr2 = XmStringCreateLocalized("File to include");
  338. /*
  339. * Add the OK callback so the curent value of the LoadSaveFlag
  340. * flag is passed in.
  341. */
  342. XtAddCallback(ad.fileSelectionBox, XmNokCallback, OpenFileCb,
  343. (XtPointer)LoadSaveFlag);
  344. break;
  345. }
  346. case SAVE_DATA:
  347. {
  348. tmpStr1 = XmStringCreateLocalized("Save to File");
  349. tmpStr2 = XmStringCreateLocalized("Save to file");
  350. /*
  351. * Add the OK callback so the save as callback is called.
  352. */
  353. XtAddCallback(ad.fileSelectionBox, XmNokCallback, SaveAsFileCb,
  354. (XtPointer)LoadSaveFlag);
  355. break;
  356. }
  357. default:
  358. {
  359. break;
  360. }
  361. }
  362. XtVaSetValues(ad.fileSelectionBox,
  363. XmNdialogTitle, tmpStr1,
  364. XmNselectionLabelString, tmpStr1,
  365. NULL);
  366. XmStringFree(tmpStr1);
  367. XmStringFree(tmpStr2);
  368. /* Display the FSB */
  369. XtManageChild (ad.fileSelectionBox);
  370. }
  371. static void ExitCb(Widget w, XtPointer cd, XtPointer cb)
  372. {
  373. exit(0);
  374. }
  375. /*****************************************************
  376. *
  377. * Edit menu callbacks
  378. *
  379. */
  380. static void UndoCb(Widget w, XtPointer cd, XtPointer cb)
  381. {
  382. DtEditorUndoEdit(ad.editor);
  383. }
  384. static void CutCb(Widget w, XtPointer cd, XtPointer cb)
  385. {
  386. DtEditorCutToClipboard(ad.editor);
  387. }
  388. static void CopyCb(Widget w, XtPointer cd, XtPointer cb)
  389. {
  390. DtEditorCopyToClipboard(ad.editor);
  391. }
  392. static void PasteCb(Widget w, XtPointer cd, XtPointer cb)
  393. {
  394. DtEditorPasteFromClipboard(ad.editor);
  395. }
  396. static void ClearCb(Widget w, XtPointer cd, XtPointer cb)
  397. {
  398. DtEditorClearSelection(ad.editor);
  399. }
  400. static void DeleteCb(Widget w, XtPointer cd, XtPointer cb)
  401. {
  402. DtEditorDeleteSelection(ad.editor);
  403. }
  404. static void SelectAllCb(Widget w, XtPointer cd, XtPointer cb)
  405. {
  406. DtEditorSelectAll(ad.editor);
  407. }
  408. static void DeselectCb(Widget w, XtPointer cd, XtPointer cb)
  409. {
  410. DtEditorDeselect(ad.editor);
  411. }
  412. static void FindCb(Widget w, XtPointer cd, XtPointer cb)
  413. {
  414. DtEditorInvokeFindChangeDialog(ad.editor);
  415. }
  416. static void SpellCb(Widget w, XtPointer cd, XtPointer cb)
  417. {
  418. DtEditorInvokeSpellDialog(ad.editor);
  419. }
  420. /*****************************************************
  421. *
  422. * Format menu callbacks
  423. *
  424. */
  425. static void FormatAllCb(Widget w, XtPointer cd, XtPointer cb)
  426. {
  427. DtEditorFormat(ad.editor, (DtEditorFormatSettings *) NULL,
  428. DtEDITOR_FORMAT_ALL);
  429. }
  430. static void FormatParaCb(Widget w, XtPointer cd, XtPointer cb)
  431. {
  432. DtEditorFormat(ad.editor, (DtEditorFormatSettings *) NULL,
  433. DtEDITOR_FORMAT_PARAGRAPH);
  434. }
  435. static void InvokeFormatDlgCb(Widget w, XtPointer cd, XtPointer cb)
  436. {
  437. DtEditorInvokeFormatDialog(ad.editor);
  438. }
  439. /*****************************************************
  440. *
  441. * Options menu callbacks
  442. *
  443. */
  444. static void OverstrikeCb(Widget w, XtPointer cd, XtPointer cb)
  445. {
  446. ad.overstrikeModeOn = ad.overstrikeModeOn ? False : True;
  447. XtVaSetValues(ad.editor, DtNoverstrike, ad.overstrikeModeOn, NULL);
  448. }
  449. static void StatusLineCb(Widget w, XtPointer cd, XtPointer cb)
  450. {
  451. ad.statusLineOn = ad.statusLineOn ? False : True;
  452. XtVaSetValues(ad.editor, DtNshowStatusLine, ad.statusLineOn, NULL);
  453. /* Reset the resize increment and minimum window size properties. */
  454. SetResizeHints();
  455. }
  456. static void WordWrapCb(Widget w, XtPointer cd, XtPointer cb)
  457. {
  458. ad.wordWrapOn = ad.wordWrapOn ? False : True;
  459. XtVaSetValues(ad.editor, DtNwordWrap, ad.wordWrapOn, NULL);
  460. }
  461. /*****************************************************
  462. *
  463. * Editor callbacks
  464. *
  465. */
  466. static void HelpCb(Widget w, XtPointer cd, XtPointer cb)
  467. {
  468. DtEditorHelpCallbackStruct *ecb = (DtEditorHelpCallbackStruct *)cb;
  469. DisplayMessage( "Received a request for help");
  470. }
  471. static void TextSelectedCb(Widget w, XtPointer cd, XtPointer cb)
  472. {
  473. SetSelectionState(True);
  474. }
  475. static void TextUnselectedCb(Widget w, XtPointer cd, XtPointer cb)
  476. {
  477. SetSelectionState(False);
  478. }
  479. static void SetSelectionState(Boolean state)
  480. {
  481. XtSetSensitive(ad.cutButton, state);
  482. XtSetSensitive(ad.copyButton, state);
  483. XtSetSensitive(ad.deleteButton, state);
  484. XtSetSensitive(ad.clearButton, state);
  485. XtSetSensitive(ad.deselectButton, state);
  486. XtSetSensitive(ad.cutPopupButton, state);
  487. XtSetSensitive(ad.copyPopupButton, state);
  488. }
  489. /************************************************************************
  490. *
  491. * PROCEDURES
  492. *
  493. ************************************************************************/
  494. /************************************************************************
  495. *
  496. * SetResizeHints - Set the resize increment properties
  497. *
  498. */
  499. static void SetResizeHints(void)
  500. {
  501. XSizeHints sh;
  502. Dimension MBheight;
  503. DtEditorGetSizeHints(ad.editor, &sh);
  504. /*
  505. * Add Menu Bar height to the height of the Editor widget
  506. */
  507. XtVaGetValues( ad.menuBar, XmNheight, &MBheight, NULL);
  508. sh.min_height += MBheight;
  509. sh.base_height += MBheight;
  510. XSetWMSizeHints(ad.display, XtWindow(ad.top), &sh, XA_WM_NORMAL_HINTS);
  511. }
  512. /************************************************************************
  513. *
  514. * DisplayMessage - Display message in DtEditor's message area
  515. *
  516. */
  517. static void DisplayMessage(char *message)
  518. {
  519. XmTextFieldSetString(ad.messageTextF, message);
  520. }
  521. /************************************************************************
  522. *
  523. * CreateFileMenu - Create the File pulldown menu
  524. *
  525. */
  526. static Widget CreateFileMenu(Widget menuBar)
  527. {
  528. Widget fileMenu;
  529. fileMenu = (Widget) XmCreatePulldownMenu (menuBar, "fileMenu", NULL, 0);
  530. CreateCascade(menuBar, "File", 'F', fileMenu);
  531. CreateButton(fileMenu, "New", 'N', ResetEditorCb, NULL);
  532. CreateButton(fileMenu, "Open...", 'O', DisplayFsbCb, (XtPointer)LOAD_DATA);
  533. CreateButton(fileMenu, "Include...", 'I', DisplayFsbCb,
  534. (XtPointer)INSERT_DATA);
  535. XtManageChild(XmCreateSeparatorGadget(fileMenu, "sep1", NULL, 0));
  536. CreateButton(fileMenu, "Save As...", 'A', DisplayFsbCb, (XtPointer)SAVE_DATA);
  537. XtManageChild(XmCreateSeparatorGadget(fileMenu, "sep2", NULL, 0));
  538. CreateButton(fileMenu, "Exit", 'x', ExitCb, NULL);
  539. return (fileMenu);
  540. }
  541. /************************************************************************
  542. *
  543. * CreateEditMenu - Create the Edit pulldown menu
  544. *
  545. */
  546. static Widget CreateEditMenu(Widget menuBar)
  547. {
  548. Widget editMenu;
  549. editMenu = XmCreatePulldownMenu (menuBar, "editMenu", NULL, 0);
  550. CreateCascade(menuBar, "Edit", 'E', editMenu);
  551. CreateButton(editMenu, "Undo", 'U', UndoCb, NULL);
  552. XtManageChild(XmCreateSeparatorGadget(editMenu, "sep1", NULL, 0));
  553. ad.cutButton = CreateButton(editMenu, "Cut", 't', CutCb, NULL);
  554. ad.copyButton = CreateButton(editMenu, "Copy", 'C', CopyCb, NULL);
  555. CreateButton(editMenu, "Paste", 'P', PasteCb, NULL);
  556. ad.clearButton = CreateButton(editMenu, "Clear", 'e', ClearCb, NULL);
  557. ad.deleteButton = CreateButton(editMenu, "Delete", 't', DeleteCb, NULL);
  558. CreateButton(editMenu, "Select All", 'S', SelectAllCb, NULL);
  559. ad.deselectButton = CreateButton(editMenu, "Deselect", 'D', DeselectCb, NULL);
  560. XtManageChild(XmCreateSeparatorGadget(editMenu, "sep2", NULL, 0));
  561. CreateButton(editMenu, "Find/Change...", 'F', FindCb, (XtPointer)ad.editor);
  562. CreateButton(editMenu, "Check Spelling...", 'p', SpellCb, (XtPointer)ad.editor);
  563. return (editMenu);
  564. }
  565. /************************************************************************
  566. *
  567. * CreateFormatMenu - Create the FormatEdit pulldown menu
  568. *
  569. */
  570. static Widget CreateFormatMenu(Widget menuBar)
  571. {
  572. Widget formatMenu;
  573. formatMenu = XmCreatePulldownMenu (menuBar, "formatMenu", NULL, 0);
  574. CreateCascade(menuBar, "Format", 'r', formatMenu);
  575. CreateButton(formatMenu, "Settings...", 'S', InvokeFormatDlgCb, NULL);
  576. CreateButton(formatMenu, "All", 'A', FormatAllCb, NULL);
  577. CreateButton(formatMenu, "Paragraph", 'P', FormatParaCb, NULL);
  578. return (formatMenu);
  579. }
  580. /************************************************************************
  581. *
  582. * CreateOptionsMenu - Create the Options pulldown menu
  583. *
  584. */
  585. static Widget CreateOptionsMenu(Widget menuBar)
  586. {
  587. Widget optionsMenu;
  588. optionsMenu = XmCreatePulldownMenu (menuBar, "optionsMenu", NULL, 0);
  589. CreateCascade(menuBar, "Options", 'O', optionsMenu);
  590. CreateToggle(optionsMenu, "Overstrike", 'O', OverstrikeCb, NULL, ad.overstrikeModeOn);
  591. CreateToggle(optionsMenu, "Word Wrap", 'W', WordWrapCb, NULL,ad.wordWrapOn);
  592. XtManageChild(XmCreateSeparatorGadget(optionsMenu, "sep1", NULL, 0));
  593. CreateToggle(optionsMenu, "Status Line", 'S', StatusLineCb, NULL, ad.statusLineOn);
  594. return (optionsMenu);
  595. }
  596. /*
  597. * CreatePopupMenu - Create popup menu in editor window
  598. */
  599. static Widget CreatePopupMenu(Widget parent)
  600. {
  601. Widget popupMenu;
  602. Widget fileMenu;
  603. Widget editMenu;
  604. popupMenu = XmCreatePopupMenu(parent, "popupMenu", NULL, 0);
  605. XtAddEventHandler(parent, ButtonPressMask, False, PopupHandler,
  606. (XtPointer)popupMenu);
  607. fileMenu = XmCreatePulldownMenu (popupMenu, "fileMenu", NULL, 0);
  608. CreateCascade(popupMenu, "File", 'F', fileMenu);
  609. CreateButton(fileMenu, "Open...", 'O', DisplayFsbCb, (XtPointer)LOAD_DATA);
  610. CreateButton(fileMenu, "Include...", 'I', DisplayFsbCb,
  611. (XtPointer)INSERT_DATA);
  612. CreateButton(fileMenu, "Save As...", 'A', DisplayFsbCb, (XtPointer)SAVE_DATA);
  613. CreateButton(fileMenu, "New", 'N', ResetEditorCb, NULL);
  614. editMenu = XmCreatePulldownMenu (popupMenu, "editMenu", NULL, 0);
  615. CreateCascade(popupMenu, "Edit", 'E', editMenu);
  616. CreateButton(editMenu, "Undo", 'U', UndoCb, (XtPointer)LOAD_DATA);
  617. ad.cutPopupButton = CreateButton(editMenu, "Cut", 't', CutCb, NULL);
  618. ad.copyPopupButton = CreateButton(editMenu, "Copy", 'C', CopyCb, NULL);
  619. CreateButton(editMenu, "Paste", 'P', PasteCb, NULL);
  620. CreateButton(popupMenu, "Find/Change...", 'F', FindCb, (XtPointer)ad.editor);
  621. return (popupMenu);
  622. }
  623. static void PopupHandler(Widget w, XtPointer cd, XEvent *event, Boolean *ctd)
  624. {
  625. if (((XButtonEvent *)event)->button != Button3) return;
  626. XmMenuPosition((Widget)cd, (XButtonEvent *)event);
  627. XtManageChild ((Widget)cd);
  628. }
  629. /*
  630. * CreateMenuBar - Create MenuBar in MainWindow
  631. */
  632. static Widget CreateMenuBar(Widget parent)
  633. {
  634. Widget menuBar;
  635. menuBar = XmCreateMenuBar(parent, "menuBar", NULL, 0);
  636. CreateFileMenu(menuBar);
  637. CreateEditMenu(menuBar);
  638. CreateFormatMenu(menuBar);
  639. CreateOptionsMenu(menuBar);
  640. return (menuBar);
  641. }
  642. /*
  643. * CreateEditor - Create the editor widget
  644. */
  645. static Widget CreateEditor(Widget parent)
  646. {
  647. Widget w;
  648. Arg al[10];
  649. int ac;
  650. /* create editor widget */
  651. ac = 0;
  652. XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
  653. XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
  654. XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
  655. XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
  656. XtSetArg(al[ac], DtNrows, 40); ac++;
  657. XtSetArg(al[ac], DtNcolumns, 80); ac++;
  658. w = DtCreateEditor(parent, "editor", al, ac);
  659. /* Add callbacks */
  660. XtAddCallback(w, DtNtextSelectCallback, TextSelectedCb, (XtPointer) w);
  661. XtAddCallback(w, DtNtextDeselectCallback, TextUnselectedCb, (XtPointer) w);
  662. XtAddCallback(w, XmNhelpCallback, HelpCb, NULL);
  663. return(w);
  664. }
  665. static Widget CreateButton(Widget parent, String label, char mnemonic, XtCallbackProc callback, XtPointer callData)
  666. {
  667. Widget button;
  668. XmString labelString;
  669. Arg al[10];
  670. int ac;
  671. labelString = XmStringCreateLocalized(label);
  672. ac = 0;
  673. XtSetArg(al[ac], XmNlabelString, labelString); ac++;
  674. XtSetArg(al[ac], XmNmnemonic, mnemonic); ac++;
  675. button = XmCreatePushButtonGadget(parent, label, al, ac);
  676. XtAddCallback(button, XmNactivateCallback, callback, callData);
  677. XtManageChild(button);
  678. XmStringFree(labelString);
  679. return(button);
  680. }
  681. static Widget CreateToggle(Widget parent, String label, char mnemonic, XtCallbackProc callback, XtPointer callData, Boolean value)
  682. {
  683. Widget button;
  684. XmString labelString;
  685. Arg al[10];
  686. int ac;
  687. labelString = XmStringCreateLocalized(label);
  688. ac = 0;
  689. XtSetArg(al[ac], XmNlabelString, labelString); ac++;
  690. XtSetArg(al[ac], XmNmnemonic, mnemonic); ac++;
  691. XtSetArg(al[ac], XmNvisibleWhenOff, True); ac++;
  692. XtSetArg(al[ac], XmNset, value); ac++;
  693. button = XmCreateToggleButtonGadget(parent, label, al, ac);
  694. XtAddCallback(button, XmNvalueChangedCallback, callback, callData);
  695. XtManageChild(button);
  696. XmStringFree(labelString);
  697. return(button);
  698. }
  699. static Widget CreateCascade(Widget parent, String label, char mnemonic, Widget subMenu)
  700. {
  701. Widget button;
  702. XmString labelString;
  703. Arg al[10];
  704. int ac;
  705. labelString = XmStringCreateLocalized(label);
  706. ac = 0;
  707. XtSetArg(al[ac], XmNlabelString, labelString); ac++;
  708. XtSetArg(al[ac], XmNsubMenuId, subMenu); ac++;
  709. XtSetArg(al[ac], XmNmnemonic, mnemonic); ac++;
  710. button = XmCreateCascadeButtonGadget(parent, label, al, ac);
  711. XtManageChild(button);
  712. XmStringFree(labelString);
  713. return(button);
  714. }