Progress
Programming
Handbook


Adding Functionality to an Editor

The following example, p-edit2.p, provides a more functional editor than p-edit1.p. This example lets you read an operating system file into the editor, make modifications to it, and then save the results. To do this, the example uses a menu bar and the READ–FILE and SAVE–FILE methods of the editor widget:

p-edit2.p
DEFINE VARIABLE e AS CHARACTER VIEW-AS EDITOR
                      INNER-CHARS 70 INNER-LINES 15 SCROLLBAR-VERTICAL.
DEFINE VARIABLE filename AS CHARACTER FORMAT "x(60)".
DEFINE VARIABLE status-ok AS LOGICAL.

DEFINE SUB-MENU filemenu
   MENU-ITEM fopen LABEL "Open..."
   MENU-ITEM fsave LABEL "Save As..."
   MENU-ITEM fexit LABEL "E&xit".
   
DEFINE MENU mainbar MENUBAR
   SUB-MENU filemenu LABEL "File".

FORM
  e
  WITH FRAME edit-frame.

FORM
  "Enter Filename:" SKIP
  filename
  WITH FRAME file-spec NO-LABELS VIEW-AS DIALOG-BOX.

ON RETURN OF filename IN FRAME file-spec
   APPLY "GO" TO filename.

ON CHOOSE OF MENU-ITEM fopen
   DO:
      FRAME file-spec:TITLE = "Open File".
      UPDATE filename WITH FRAME file-spec. 
      status-ok = e:READ-FILE(filename) IN FRAME edit-frame.
      IF NOT status-ok
      THEN MESSAGE "Could not read" filename.
   END. 
ON CHOOSE OF MENU-ITEM fsave
   DO:
      FRAME file-spec:TITLE = "Save File".
      UPDATE filename WITH FRAME file-spec.
      status-ok = e:SAVE-FILE(filename) IN FRAME edit-frame.
      IF NOT status-ok
      THEN MESSAGE "Could not write to" filename. 
   END.        
  
CURRENT-WINDOW:MENUBAR = MENU mainbar:HANDLE.
                 
ENABLE e WITH FRAME edit-frame NO-LABELS.

WAIT-FOR CHOOSE OF MENU-ITEM fexit OR
         WINDOW-CLOSE OF CURRENT-WINDOW. 

When you run this example, you can use the menu items to read from a file or type text directly into the editor.

You can increase the functionality of this editor further by adding additional options to the menu bar and using additional editor methods. For example, you can add an Edit submenu that includes a search–and–replace option using the REPLACE method.

NOTE: When you save the editor text to a file, some characters save differently, depending on your operating system. For example, RETURN key input writes out on Windows as x0d0d0a. On UNIX, this writes out as x0d0a. Similarly, the end-of-line on Windows writes out as <CR><LF>. On UNIX, this writes out as <CR> alone. Future versions of Progress might address these issues, allowing for improved portability among platforms.


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095