home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / tclMotif-1.4 / examples / xmeditor < prev    next >
Encoding:
Text File  |  1995-06-29  |  6.7 KB  |  310 lines

  1. #!/usr/local/bin/moat
  2.  
  3. #######################################################
  4. #
  5. # A simple text editor
  6. #
  7. # This is based on the xmeditor that is an example
  8. # program in each Motif release, and is briefly
  9. # described in the Motif Programmers Manual
  10. #
  11. ######################################################
  12.  
  13. global fileSaved
  14. set fileSaved 1
  15.  
  16. global currentFile
  17. set currentFile ""
  18.  
  19. ########################################################
  20. #
  21. # file menu callbacks
  22. #
  23.  
  24. # openFileCB
  25.  
  26. proc openFileCB {} {
  27.     global fileSaved 
  28.     global currentFile
  29.  
  30.     if {! $fileSaved } {
  31.     if {[saveFileDialog]} {
  32.         saveFile
  33.     }
  34.     }
  35.  
  36.     xmFileSelectionDialog .fsd managed
  37.     .fsd.Help unmanageChild
  38.     .fsd okCallback {loadFile %w %value}
  39.     .fsd cancelCallback {[.fsd parent] destroyWidget}
  40. }
  41.  
  42. # newFileCB
  43.  
  44. proc newFileCB {} {
  45.     global fileSaved 
  46.     global currentFile
  47.  
  48.     if {! $fileSaved } {
  49.     if {[saveFileDialog]} {
  50.         saveFile
  51.     }
  52.     }
  53.  
  54.     xmPromptDialog .pd managed \
  55.     -selectionLabelString "Enter filename:"
  56.     .pd.Help unmanageChild
  57.     .pd okCallback {newFile %w %value}
  58.     .pd cancelCallback {[.pd parent] destroyWidget}
  59. }
  60.  
  61. #############################################################
  62. #
  63. # callback to handle "save as..." selection
  64. #
  65. proc saveFileAsCB {} {
  66.     xmFileSelectionDialog .fsd managed
  67.     .fsd.Help unmanageChild
  68.     .fsd okCallback {
  69.     set currentFile %value
  70.     saveFile
  71.     %w destroyWidget
  72.     }
  73.     .fsd cancelCallback {[.fsd parent] destroyWidget}
  74. }
  75.  
  76. #############################################################
  77. #
  78. # saveFileDialog
  79. #
  80. # modal dialog to check if a file should be saved
  81. # note the globals "answer" and "stillModal". This
  82. # is because the callbacks are executed in global
  83. # context
  84.  
  85. proc saveFileDialog {} {
  86.     global currentFile
  87.     global answer
  88.     global stillModal
  89.  
  90.     xmErrorDialog .fileSaveDialog managed \
  91.     -messageString "Save $currentFile?" \
  92.     -dialogStyle dialog_full_application_modal
  93.     .fileSaveDialog okCallback {set stillModal 0; set answer 1}
  94.     .fileSaveDialog cancelCallback {set stillModal 0; set answer 0}
  95.     .fileSaveDialog.Help unmanageChild
  96.  
  97.     # this dialog has to run modal:
  98.     set stillModal 1
  99.     while {$stillModal} {
  100.         . processEvent
  101.     }
  102.     .fileSaveDialog destroyWidget
  103.  
  104.     return $answer
  105. }
  106.  
  107.  
  108. ####################################################
  109. #
  110. # save the text contents to the current file
  111. # put up an error dialog if it can't be written
  112. #
  113. proc saveFile {} {
  114.     global currentFile
  115.     global fileSaved
  116.  
  117.     if {[catch {set fileID [open $currentFile w]}] > 0} {
  118.     errorDialog "Can't open $currentFile for writing"
  119.     return
  120.     }
  121.  
  122.     set str [.main.text getString]
  123.     puts $fileID $str
  124.     close $fileID
  125.  
  126.     set fileSaved 1
  127. }
  128.  
  129. ###################################################################
  130. #
  131. # load a file 
  132. # if not valid, put up an error dialog
  133. #
  134. proc loadFile {w file} {
  135.     global currentFile
  136.     global fileSaved
  137.  
  138.     [$w parent] destroyWidget
  139.  
  140.     if {[catch {set fileID [open $file r]}] > 0} {
  141.     errorDialog "Can't open $file for reading"
  142.     return
  143.     }
  144.     set currentFile $file
  145.  
  146.     .main.text setString ""
  147.     while {[gets $fileID str] >= 0} {
  148.     .main.text insert [.main.text getLastPosition]] "$str\n"
  149.     }
  150.     close $fileID
  151.     set fileSaved 1
  152. }
  153.  
  154. ###############################################################
  155. #
  156. # set new file and clear text contents
  157. #
  158. proc newFile {w file} {
  159.     global currentFile
  160.  
  161.     [$w parent] destroyWidget
  162.     set currentFile $file
  163.     .main.text setString ""
  164. }
  165.  
  166. #######################################################
  167. # leave the X world
  168. #
  169. proc quitFileCB {} {
  170.     global fileSaved 
  171.     global currentFile
  172.  
  173.     if {! $fileSaved } {
  174.     if {[saveFileDialog]} {
  175.         saveFile
  176.     }
  177.     }
  178.     exit 0
  179. }
  180.  
  181. ##########################################################
  182. #
  183. # show an error dialog for the message
  184. #
  185. proc errorDialog {errMsg} {
  186.     xmErrorDialog .err managed \
  187.     -messageString $errMsg
  188.     .err okCallback {[.err parent] destroyWidget}
  189.     .err.Cancel unmanageChild
  190.     .err.Help unmanageChild
  191. }
  192.  
  193. #########################################################
  194. #
  195. # help callbacks
  196. #
  197. #########################################################
  198.  
  199. #########################################################
  200. #
  201. # helpAboutCB
  202. #
  203.  
  204. proc helpAboutCB {} {
  205.     xmInformationDialog .helpAboutDialog managed \
  206.     -messageAlignment alignment_center \
  207.     -messageString "About xmeditor\n\
  208. Jan Newmarch\n\
  209. Version 1.0\n\
  210. January, 1993"
  211.     .helpAboutDialog okCallback {[%w parent] destroyWidget}
  212.     .helpAboutDialog.Cancel unmanageChild
  213.     .helpAboutDialog.Help unmanageChild
  214. }
  215.  
  216. #########################################################
  217. #
  218. # X world starts here
  219. #
  220. #########################################################
  221. xtAppInitialize \
  222.     -fallbackResources {
  223.         {*XmText.columns: 80}
  224.         {*XmText.rows: 24}
  225.     }
  226.  
  227. ##########################################################
  228. #
  229. # main window to hold every thing
  230. #
  231. xmMainWindow .main managed
  232.  
  233. #########################################################
  234. #
  235. # menu system along top
  236. #
  237.  
  238. # menu bar
  239. xmMenuBar .main.menuBar managed
  240.  
  241. # file pulldown
  242. xmPulldownMenu .main.filePane
  243. xmCascadeButton .main.menuBar.File managed \
  244.     -subMenuId .main.filePane
  245.  
  246. xmPushButton .main.filePane.New managed
  247. .main.filePane.New activateCallback newFileCB
  248.  
  249. xmPushButton .main.filePane.Open managed
  250. .main.filePane.Open activateCallback openFileCB
  251.  
  252. xmPushButton .main.filePane.Save managed
  253. .main.filePane.Save activateCallback "saveFile"
  254.  
  255. xmPushButton .main.filePane.SaveAs managed
  256. .main.filePane.SaveAs activateCallback "saveFileAsCB"
  257.  
  258. xmPushButton .main.filePane.Quit managed
  259. .main.filePane.Quit activateCallback quitFileCB
  260.  
  261. # edit pulldown
  262. xmPulldownMenu .main.editPane
  263. xmCascadeButton .main.menuBar.Edit managed \
  264.     -subMenuId .main.editPane
  265.  
  266. xmPushButton .main.editPane.Cut managed
  267. .main.editPane.Cut activateCallback {.main.text cut}
  268.  
  269. xmPushButton .main.editPane.Copy managed
  270. .main.editPane.Copy activateCallback {.main.text copy}
  271.  
  272. xmPushButton .main.editPane.Paste managed
  273. .main.editPane.Paste activateCallback {.main.text paste}
  274.  
  275. xmPushButton .main.editPane.Clear managed
  276. .main.editPane.Clear activateCallback {
  277.         .main.text getSelectionPosition left right
  278.         .main.text replace $left $right ""
  279.         }
  280.  
  281. #
  282. # help
  283. #
  284. xmPulldownMenu .main.helpPane
  285. xmCascadeButton .main.menuBar.Help managed \
  286.     -subMenuId .main.helpPane
  287. .main.menuBar setValues \
  288.     -menuHelpWidget .main.menuBar.Help
  289.  
  290. xmPushButton .main.helpPane.about managed \
  291.     -labelString "About..."
  292. .main.helpPane.about activateCallback helpAboutCB
  293.  
  294. ##########################################################
  295. #
  296. # edit window
  297. #
  298. xmScrolledText .main.text managed \
  299.     -editMode Multi_line_edit
  300. .main.text modifyVerifyCallback {set fileSaved 0}
  301.  
  302.  
  303. .main setValues -menuBar .main.menuBar \
  304.         -workWindow [.main.text parent]
  305.  
  306. . realizeWidget
  307. . mainLoop
  308.  
  309.