«previous next»

Windows Interface (continued)

Menu definition and execution

Windows menus are simple to implement in SwiftForth

SwiftForth provides a simple means of defining menus and relating their items to Forth words. The mechanism provides for context-sensitive item execution by the user, and is easily extensible by simply defining a Forth name that directly corresponds to the menu text. For example, the "Hello World" demo in the figure shown here includes File and About menus, defined by:

    MENU HELLO-MENU
        POPUP "&File"
            MI_EXIT MENUITEM "E&xit"
        END-POPUP
       POPUP "&Help"
            MI_ABOUT MENUITEM "&About"
        END-POPUP
    END-MENU

Behaviors may be attached using extensible switch structures. The menu items MI_EXIT and MI_ABOUT defined in the above popups may be assigned behaviors like this:

    [SWITCH HELLO-COMMANDS ZERO
       MI_EXIT  RUN: ( -- res )   HELLO-CLOSE 0 ;
       MI_ABOUT RUN: ( -- res )   HELLO-ABOUT 0 ;
    SWITCH]

The code following the RUN: is an un-named definition attached to the respective menu item. Predefined words may also be attached by replacing RUN: with RUNS <name>.

Printer support

Output via standard Windows printer functions is supported. Any console output may be redirected to a Windows printer or to an arbitrary text file. For example:

    >PRINT HERE 100 DUMP

…will prompt the user to select a printer and print the memory dump on it. You may also print the contents of the Command window.

Exception handler

Windows exceptions, which are caught by the standard exception filter, are trapped, logged, and generate a simple Forth -9998 THROW which the user can capture and handle. Errors during callback are also caught and handled.

If an exception occurs during interactive debugging, a dialog box displays the basic error message and buttons to clear both stacks and return control to SwiftForth, to exit SwiftForth, or to display complete information from the exception frame, including registers and the top eight locations of the data and return stacks (only available on Windows exceptions, not Forth exceptions).

Exception handler display

Note that hardware interrupts are not available in a Windows environment and, thus, are not supported. The conceptual function of an interrupt is replaced by a Windows "event." That is, when a key is pressed, you do not get a "keyboard interrupt" but a Windows "keyboard event," which is delivered and processed quite differently.

Serial port support

Serial ports (e.g., COM1) can be read or written, using a small library of words provided that are equivalent to KEY?, KEY, and EMIT.

«previous next»