home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 16 / 16.iso / w / w055 / 4.ddi / SOURCES.LIF / SETUP.PEL < prev    next >
Encoding:
Text File  |  1990-09-27  |  26.8 KB  |  1,085 lines

  1. # $Header:   P:/source/ppee/macros/setup.pev   1.34   20 Aug 1990 13:48:16   skipr  $
  2.  
  3. ##############################################################################
  4. #
  5. #           Sage Software - POLYTRON Division
  6. #             1700 NW 167th Place
  7. #               Beaverton, OR 97006
  8. #
  9. #   Copyright 1990, Sage Software, Inc.
  10. #
  11. #   Permission is hereby granted for licensed users of Sage Professional
  12. #   Editor and PolyAwk to copy and modify this source code for their own
  13. #   personal use.  These derivative works may be distributed only to other
  14. #   licensed Sage Professional Editor and PolyAwk users.  All other usage
  15. #   is prohibited without express written permission from Sage Software.
  16. #
  17. ##############################################################################
  18.  
  19. #### $Workfile:   setup.pel  $: Configuration Menus
  20.  
  21. local    emulationMode    = ""
  22. local    windowOptions    = ""
  23. local    windowColors    = ""
  24.  
  25. local    inColorsMenu
  26. local    sampleColorsWindow
  27. local    colorBarWindow
  28.  
  29. #############################################
  30. #                        #
  31. #         MENU COLORS            #
  32. #                        #
  33. #    BLACK    = 0    DGRAY    = 0x8        #
  34. #    BLUE    = 1    LBLUE    = 0x9        #
  35. #    GREEN    = 2    LGREEN    = 0xa        #
  36. #    CYAN    = 3    LCYAN    = 0xb        #
  37. #    RED    = 4    LRED    = 0xc        #
  38. #    PURPLE    = 5    LPURPLE    = 0xd        #
  39. #    BROWN    = 6    YELLOW    = 0xe        #
  40. #    GRAY    = 7    WHITE    = 0xf        #
  41. #                        #
  42. #############################################
  43.  
  44. # a few useful color combinations:
  45. local    CyanOnBlack    = 0x03
  46. local    GrayOnBlack    = 0x07
  47. local    WhiteOnBlack    = 0x0f
  48. #local    BlackOnBlue    = 0x10
  49. local    CyanOnBlue    = 0x13
  50. #local    GrayOnBlue    = 0x17
  51. #local    GreenOnBlue    = 0x1a
  52. #local    YellowOnBlue    = 0x1f
  53. local    WhiteOnBlue    = 0x1f
  54. #local    CyanOnGreen    = 0x23
  55. local    YellowOnGreen    = 0x2e
  56. local    WhiteOnGreen    = 0x2f
  57. #local    WhiteOnRed    = 0x4f
  58. #local    PurpleOnPurple    = 0x5d
  59. #local    WhiteOnPurple    = 0x5f
  60. #local    GrayOnBrown    = 0x67
  61. #local    YellowOnBrown    = 0x6e
  62. #local    WhiteOnBrown    = 0x6f
  63. local    BlackOnGray    = 0x70
  64. #local    PurpleOnGray    = 0x75
  65. local    GrayOnGray    = 0x78
  66.  
  67. # default colors assiciated with the "windowColors" variable defined above
  68. local    textColor    = CyanOnBlack
  69. local    highlightColor    = BlackOnGray
  70. local    borderColor    = GrayOnBlack
  71. local    titleColor    = GrayOnGray
  72.  
  73.  
  74. ##############
  75. # setup menu #
  76. ##############
  77.  
  78. global function setup() {
  79.  
  80.     local    priorTextColor        = default_color_text
  81.     local    priorHighlightColor    = default_color_highlight
  82.     local    priorBorderColor    = default_color_border
  83.     local    priorTitleColor        = default_color_title
  84.     local    priorLineNumbers    = default_linenumber_format
  85.     local    priorWindow        = current_window
  86.     local    priorBuffer        = current_buffer
  87.     local    fullScreenWindow
  88.     local    choice = 1
  89.  
  90.     # general initialization
  91.  
  92.     # colors to use for illustrateWindow functions
  93.     textColor    = default_color_text
  94.     highlightColor    = default_color_highlight
  95.     borderColor    = default_color_border
  96.     titleColor    = default_color_title
  97.  
  98.     # turn off visible line numbers
  99.     default_linenumber_format = ""
  100.  
  101.     # set up the screen
  102.  
  103.     if ( !and( color_background, 0x100 ) ) {
  104.         # turn off blinking
  105.         color_background = or( color_background, 0x100 )
  106.     }
  107.     display_update()
  108.  
  109.     default_color_text = GrayOnBlack
  110.     fullScreenWindow = create_window( \
  111.             0, 0, \
  112.             display_width, display_height, \
  113.             WINDOW_SYSTEM )
  114.     if ( fullScreenWindow ) {
  115.         current_window = fullScreenWindow
  116.         display_update()
  117.     }
  118.     default_color_text = textColor
  119.  
  120.     # invoke setup menu
  121.  
  122.     while (1) {
  123.         initMenu( 20,4, 40,11, WhiteOnGreen, YellowOnGreen, \
  124.             "Select Editor Defaults Menu" )
  125.  
  126.         addMenuItem( "emulation mode", \
  127.             "Emulate key actions and appearance of other editors")
  128.  
  129.         addMenuItem( "window options", \
  130.             "Set options governing the appearance of a window")
  131.  
  132.         addMenuItem( "colors", \
  133.             "Select colors to use for windows and text")
  134.  
  135.         addMenuItem( "save settings in SAGEEDIT.CFG", \
  136.             "Write a new \"SAGEEDIT.CFG\" file with the current settings")
  137.  
  138.         addMenuItem( "temporarily accept settings", \
  139.             "use new options without saving to \"SAGEEDIT.CFG\" file" )
  140.  
  141.         choice = getMenuSelection( choice )
  142.  
  143.         deleteMenu()
  144.  
  145.         if ( choice == 0 ) {
  146.             # Esc key - reset options
  147.             emulationMode    = \
  148.             windowColors    = \
  149.             windowOptions    = ""
  150.             break
  151.         } else if ( choice == 1 ) {
  152.             selectEmulationMode()
  153.             choice = 2
  154.         } else if ( choice == 2 ) {
  155.             setWindowOptions()
  156.             choice = 3
  157.         } else if ( choice == 3 ) {
  158.             selectColors()
  159.             choice = 4
  160.         } else if ( choice == 4 ) {
  161.             # write new config file
  162.             saveStartupFile()
  163.             break
  164.         } else if ( choice == 5 ) {
  165.             # use new values without saving to config file
  166.             break
  167.         }
  168.  
  169.     } # while (1)
  170.  
  171.     # restore 'prior' variables
  172.  
  173.     if ( fullScreenWindow ) {
  174.         delete_window( fullScreenWindow )
  175.     }
  176.  
  177.     if ( emulationMode != "" ) {
  178.         if ( emulationMode != emulation_mode ) {
  179.             execute_function( emulationMode )
  180.             if ( emulationMode == emulation_mode ) {
  181.                 message( emulationMode " enabled" )
  182.             }
  183.         }
  184.     }
  185.  
  186.     if ( windowColors == "" ) {
  187.         # restore colors
  188.         default_color_text    = priorTextColor
  189.         default_color_highlight    = priorHighlightColor
  190.         default_color_border    = priorBorderColor
  191.         default_color_title    = priorTitleColor
  192.     } else {
  193.         # set to colors chosen in the colors menu
  194.         default_color_text    = color_text        = textColor
  195.         default_color_highlight    = color_highlight    = highlightColor
  196.         default_color_border    = color_border        = borderColor
  197.         default_color_title    = color_title        = titleColor
  198.     }
  199.  
  200.     if ( windowOptions != "" ) {
  201.         default_window_flags    = windowOptions
  202.  
  203.  
  204.         if ( current_window != dialog_window ) {
  205.             window_flags = default_window_flags
  206.         }
  207.     }
  208.  
  209.     # line numbers
  210.     default_linenumber_format = priorLineNumbers
  211.  
  212.     # avoid making a system window/buffer current during startup
  213.     if ( priorWindow )
  214.         current_window = priorWindow
  215.     next_buffer()
  216.     current_buffer = priorBuffer
  217. }
  218.  
  219. #######################
  220. # EMULATION MODE MENU #
  221. #######################
  222.  
  223. local function selectEmulationMode() {
  224.  
  225.     local    choice = 1
  226.  
  227.     makeWindow( 7,1, 66,10, WhiteOnBlue, CyanOnBlue, "" )
  228.  
  229.     put( 2,4,"Choose the Editor Emulation Mode you prefer, or hit the Esc")
  230.     put( 3,4,"key to retain the present settings.  Your selection will")
  231.     put( 4,4,"determine what meaning is given to your keystrokes and what")
  232.     put( 5,4,"commands are available.  Native Mode is the mode in which")
  233.     put( 6,4,"the Sage Professional Editor was designed to operate.  Other")
  234.     put( 7,4,"modes provide familiar commands for users of other editors.")
  235.  
  236.     initMenu( 22,12, 30,12, WhiteOnBlue, CyanOnBlue, \
  237.             "Editor Emulation Modes" )
  238.  
  239.     addMenuItem("Native Mode", "")
  240.     addMenuItem("Brief Emulation", "")
  241.     addMenuItem("VI Emulation", "")
  242.     addMenuItem("WordStar Emulation", "")
  243.     addMenuItem("Epsilon Emulation", "")
  244.     addMenuItem("MS Word Emulation", "")
  245.  
  246.     choice = getMenuSelection( choice )
  247.  
  248.     if ( choice == 1 ) {
  249.         emulationMode = "native"
  250.     } else if ( choice == 2 ) {
  251.         emulationMode = "brief"
  252.     } else if ( choice == 3 ) {
  253.         emulationMode = "vi"
  254.     } else if ( choice == 4 ) {
  255.         emulationMode = "wordstar"
  256.     } else if ( choice == 5 ) {
  257.         emulationMode = "epsilon"
  258.     } else if ( choice == 6 ) {
  259.         emulationMode = "msword"
  260.     }
  261.  
  262.     deleteMenu()
  263.     delete_window()
  264. }
  265.  
  266. #######################
  267. # WINDOW OPTIONS MENU #
  268. #######################
  269.  
  270. local function setWindowOptions() {
  271.  
  272.     local    menuWindow
  273.     local    sampleWindow
  274.     local    choice = 1
  275.     local    scrollBars = WINDOW_BORDER
  276.  
  277.     initMenu( 5,2, 40,12, WhiteOnBlue, CyanOnBlue, "Window Options" )
  278.  
  279.     addMenuItem( "Borders", "" )
  280.     addMenuItem( "Ruler", "" )
  281.     addMenuItem( "Title Bar", "" )
  282.     addMenuItem( "Vertical Scroll Bar", "" )
  283.     addMenuItem( "Horizontal Scroll Bar", "" )
  284.     addMenuItem( "Accept Current Settings" )
  285.  
  286.     menuWindow = current_window
  287.  
  288.     # initialize a sample window
  289.  
  290.     default_color_text    = textColor
  291.     default_color_highlight    = highlightColor
  292.     default_color_border    = borderColor
  293.     default_color_title    = titleColor
  294.  
  295.     sampleWindow = illustrateWindowSettings()
  296.  
  297.     while ( choice > 0 && choice < 6 ) {
  298.  
  299.         current_window = menuWindow
  300.         choice = getMenuSelection( choice )
  301.         current_window = sampleWindow
  302.  
  303.         # keep track of the status of the scroll bars
  304.         if ( and( window_flags, WINDOW_BORDERS ) ) {
  305.             scrollBars = and( window_flags, WINDOW_BORDERS )
  306.         }
  307.  
  308.         if ( choice == 1 ) {
  309.             # toggle borders, retaining state of scroll bars
  310.             window_flags = xor( window_flags, scrollBars )
  311.             if ( and( window_flags, WINDOW_BORDERS ) ) {
  312.                 window_flags = or( window_flags, \
  313.                         WINDOW_BORDER )
  314.             }
  315.  
  316.         } else if ( choice == 2 ) {
  317.             # ruler
  318.             window_flags = xor( window_flags, WINDOW_RULER )
  319.  
  320.         } else if ( choice == 3 ) {
  321.             # title bar - cycle between top/bottom/neither
  322.             if ( and( window_flags, WINDOW_TITLEA ) ) {
  323.                 window_flags = or( \
  324.                     xor( window_flags, WINDOW_TITLEA ),
  325.                     WINDOW_TITLEB )
  326.             } else if ( and( window_flags, WINDOW_TITLEB ) ) {
  327.                 window_flags = xor( window_flags, \
  328.                         WINDOW_TITLEB )
  329.             } else {
  330.                 window_flags = or( window_flags, \
  331.                         WINDOW_TITLEA )
  332.             }
  333.  
  334.         } else if ( choice == 4 ) {
  335.             # vert scroll bar - cycle between right/left/neither
  336.             if ( and( window_flags, WINDOW_SB_RIGHT ) ) {
  337.                 window_flags = or( \
  338.                     xor( window_flags, WINDOW_SB_RIGHT ),
  339.                     WINDOW_SB_LEFT )
  340.             } else if ( and( window_flags, WINDOW_SB_LEFT ) ) {
  341.                 window_flags = xor( window_flags, \
  342.                         WINDOW_SB_LEFT )
  343.             } else {
  344.                 window_flags = or( window_flags, \
  345.                         WINDOW_SB_RIGHT )
  346.             }
  347.  
  348.         } else if ( choice == 5 ) {
  349.             # horiz scroll bar
  350.             window_flags = xor( window_flags, WINDOW_SB_BELOW )
  351.  
  352.         } else if ( choice == 6 ) {
  353.             windowOptions = window_flags
  354.         }
  355.  
  356.     }
  357.  
  358.     delete_window( sampleWindow )
  359.     deleteMenu()
  360. }
  361.  
  362. ######################
  363. # WINDOW COLORS MENU #
  364. ######################
  365.  
  366. local function selectColors() {
  367.  
  368.     local    menuWindow
  369.     local    instructWindow
  370.     local    choice = 1
  371.  
  372.     initMenu( 5,1, 40,12, WhiteOnBlue, CyanOnBlue, "Colors" )
  373.  
  374.     addMenuItem( "Normal Text", "" )
  375.     addMenuItem( "Highlighted Text", "" )
  376.     addMenuItem( "Borders", "" )
  377.     addMenuItem( "Title and Scroll Bars", "" )
  378.     addMenuItem( "Reset Colors", "" )
  379.     addMenuItem( "Accept Current Settings" )
  380.  
  381.     menuWindow = current_window
  382.  
  383.     colorBarWindow = initColorBar()
  384.  
  385.     default_color_text    = textColor
  386.     default_color_highlight    = highlightColor
  387.     default_color_border    = borderColor
  388.     default_color_title    = titleColor
  389.  
  390.     while ( choice ) {
  391.  
  392.         # update the colors in the sample window
  393.         sampleColorsWindow = illustrateWindowColors()
  394.  
  395.         # get menu selection
  396.         current_window = menuWindow
  397.         inColorsMenu = 1
  398.         choice = getMenuSelection( choice )
  399.         inColorsMenu = 0
  400.  
  401.         # activate keyboard-driven color matrix
  402.         if ( choice >= 1 && choice <= 4 ) {
  403.  
  404.             # put up the instructions
  405.             if ( !instructWindow ) {
  406.                 instructWindow = makeWindow( \
  407.                         5, 1, \
  408.                         40, 12, \
  409.                         WhiteOnBlue, CyanOnBlue, \
  410.                         "Colors" )
  411.     
  412.                 ctr( 2,    "Use arrow keys to select colors:" )
  413.                 put( 4, 2, "Move cursor horizontally to change" )
  414.                 put( 5, 2, "the foreground color." )
  415.                 put( 7, 2, "Move cursor vertically to change" )
  416.                 put( 8, 2, "the background color." )
  417.             }
  418.             current_window = instructWindow
  419.             display_update()
  420.  
  421.             # get color bar selection
  422.             colorBarCmd( choice )
  423.  
  424.         } else if ( choice == 5 ) {
  425.             # reset to neutral colors
  426.             default_color_text    = textColor    = CyanOnBlack
  427.             default_color_highlight    = highlightColor= BlackOnGray
  428.             default_color_border    = borderColor    = GrayOnBlack
  429.             default_color_title    = titleColor    = GrayOnGray
  430.  
  431.         } else if ( choice == 6 ) {
  432.             # accept current settings
  433.             windowColors = TRUE
  434.             choice = 0
  435.  
  436.         } else {            # Esc key
  437.             windowColors = ""
  438.         }
  439.  
  440.         delete_window( sampleColorsWindow )
  441.     }
  442.  
  443.     delete_window( colorBarWindow )
  444.     if ( instructWindow ) {
  445.         current_window = instructWindow
  446.         delete_window()
  447.     }
  448.     current_window = menuWindow
  449.     deleteMenu()
  450. }
  451.  
  452. ###
  453. # illustrateWindowSettings()
  454. #
  455. # used in window options menu to display a sample window using the
  456. # currently defined settings
  457.  
  458. local    sampleBuffer
  459.  
  460. local function illustrateWindowSettings() {
  461.  
  462.     current_window = create_window( 35,15, 40,9 )
  463.  
  464.     if ( !sampleBuffer ) {
  465.         current_buffer = sampleBuffer = create_buffer( \
  466.                 "", \
  467.                 "", \
  468.                 BUFFER_SYSTEM + BUFFER_NO_UNDO + BUFFER_REAL_SPACE_ONLY )
  469.         attach_window_buffer( current_window, current_buffer )
  470.         display_update()
  471.         insert_string( "\nThis window simulates an editor\n" )
  472.         insert_string( "window.  Try the <Enter> key at\n" )
  473.         insert_string( "each menu item to change the\n" )
  474.         insert_string( "appearance of this window.\n" )
  475.         window_name = buffer_name = "Current Settings"
  476.         goto_buffer_top()
  477.     } else {
  478.         current_buffer = sampleBuffer
  479.         attach_window_buffer( current_window, current_buffer )
  480.     }
  481.  
  482.     highlight_window( \
  483.             9, current_line, \
  484.             1, current_line_length, \
  485.             color_highlight )
  486.  
  487.     return current_window
  488. }
  489.  
  490. ###
  491. # illustrateWindowColors()
  492. #
  493. # used in window colors menu to display a sample window using the
  494. # currently defined colors
  495.  
  496. local function illustrateWindowColors() {
  497.  
  498.     current_window = create_window( \
  499.             5, 14, \
  500.             40, 9, \
  501.             WINDOW_SYSTEM + WINDOW_STANDARD    )
  502.  
  503.     window_name = "Currently Selected Colors"
  504.  
  505.     window_cursor_x = 5
  506.     window_cursor_y = 2
  507.     write_window( "This is normal text." )
  508.  
  509.     window_cursor_x = 5
  510.     window_cursor_y = 4
  511.     write_window( "This is highlighted text." )
  512.  
  513.     highlight_window( 5, 4, window_cursor_x - 5, 1, color_highlight )
  514.  
  515.     return current_window
  516. }
  517.  
  518. ###
  519. # initColorBar()
  520. # colorBarCmd( choice )
  521. # setNewColor( choice, x, y )
  522. #
  523. # Choose a color from a matrix of foreground and background colors
  524.  
  525. local function initColorBar() {
  526.  
  527.     local    fg, bg
  528.     local    c = 0
  529.  
  530.     current_window = create_window( \
  531.             54, 4, \
  532.             18, 18, \
  533.             WINDOW_SYSTEM + WINDOW_BORDER )
  534.     color_border = CyanOnBlack
  535.  
  536.     # keep the cursor out of the way
  537.     window_cursor_x = 0
  538.     window_cursor_y = 0
  539.     display_update()
  540.  
  541.     for ( bg=0; bg<16; bg++ ) {
  542.         # write_window( "" )
  543.         for ( fg=0; fg<16; fg++ ) {
  544.             write_window( "" )
  545.             highlight_window( fg, bg, 1, 1, c++ )
  546.         }
  547.     }
  548.     window_cursor_x = 0
  549.     window_cursor_y = 0
  550.  
  551.     return current_window
  552. }
  553.  
  554. local function colorBarCmd( choice ) {
  555.  
  556.     local    color
  557.     local    key
  558.     local    scancode
  559.     local    w, x, y
  560.  
  561.     current_window = colorBarWindow
  562.  
  563.     # get initial value of color in question
  564.     if ( choice == 1 ) {
  565.         color = textColor
  566.     } else if ( choice == 2 ) {
  567.         color = highlightColor
  568.     } else if ( choice == 3 ) {
  569.         color = borderColor
  570.     } else if ( choice == 4 ) {
  571.         color = titleColor
  572.     }
  573.  
  574.     if ( color < 0 || color > 255 ) {
  575.         color = WhiteOnBlack
  576.     }
  577.  
  578.     window_cursor_x    = and( color, 15 )
  579.     window_cursor_y    = shiftr( color, 4 )
  580.  
  581.     while ( 1 ) {
  582.  
  583.         # get color bar command
  584.         key = get_command()
  585.         scancode = shiftr( key, 8 )    # ignore NumLock status
  586.  
  587.         if ( scancode == SCANCODE_LEFT_PRESS ||            \
  588.                 scancode == SCANCODE_LEFT_CLICK ) {
  589.  
  590.             # update text cursor in response to a mouse press
  591.             if ( !and( x = mouse_display_x - window_text_x0, -16 ) \
  592.               && !and( y = mouse_display_y - window_text_y0, -16 )) {
  593.                     window_cursor_x = x
  594.                     window_cursor_y = y
  595.             }
  596.  
  597.             if ( and( mouse_buttons, BOTH_BUTTONS )) {
  598.                 # continue while either mouse button is down
  599.                 ungetkey( shiftl( SCANCODE_LEFT_PRESS, 8 ))
  600.  
  601.             } else if ( window_contains(            \
  602.                     mouse_event_x, mouse_event_y )    \
  603.                     && !window_border_contains(    \
  604.                         mouse_event_x, mouse_event_y )) {
  605.                 # They wound up in the text region.
  606.                 # Consider it a selection.
  607.                 ungetkey( KEYCODE_ENTER )
  608.             }
  609.  
  610.         } else if ( scancode == SCANCODE_UP ) {
  611.             # decrement background
  612.             window_cursor_y = (window_cursor_y + 15) % 16
  613.  
  614.         } else if ( scancode == SCANCODE_DOWN ) {
  615.             # increment background
  616.             window_cursor_y = (window_cursor_y + 1) % 16
  617.  
  618.         } else if ( scancode == SCANCODE_LEFT ) {
  619.             # decrement foreground
  620.             window_cursor_x = (window_cursor_x + 15) % 16
  621.  
  622.         } else if ( scancode == SCANCODE_RIGHT ) {
  623.             # increment foreground
  624.             window_cursor_x = (window_cursor_x + 1) % 16
  625.  
  626.         } else if ( key == KEYCODE_ENTER    \
  627.                 || key == KEYCODE_KEYPAD_ENTER ) {
  628.             # save colors
  629.             textColor    = default_color_text
  630.             highlightColor    = default_color_highlight
  631.             borderColor    = default_color_border
  632.             titleColor    = default_color_title
  633.             break
  634.  
  635.         } else if ( key == KEYCODE_ESC ) {
  636.             # restore colors
  637.             default_color_text    = textColor
  638.             default_color_highlight    = highlightColor
  639.             default_color_border    = borderColor
  640.             default_color_title    = titleColor
  641.             break
  642.         }
  643.  
  644.         setNewColor( choice,
  645.                 window_cursor_x,
  646.                 window_cursor_y )
  647.     }
  648. }
  649.  
  650. local function setNewColor( choice, x, y ) {
  651.     local    color
  652.  
  653.     if (x < 0 || x > 15 || y < 0 || y > 15) {
  654.         return
  655.     }
  656.  
  657.     color = or( x, shiftl( y, 4 ))
  658.  
  659.     if ( choice == 1 ) {
  660.         default_color_text    = color
  661.     } else if ( choice == 2 ) {
  662.         default_color_highlight    = color
  663.     } else if ( choice == 3 ) {
  664.         default_color_border    = color
  665.     } else if ( choice == 4 ) {
  666.         default_color_title    = color
  667.     }
  668.  
  669.     # update the colors in the sample window
  670.     delete_window( sampleColorsWindow )
  671.     sampleColorsWindow = illustrateWindowColors()
  672.     current_window = colorBarWindow
  673.     display_update()
  674. }
  675.  
  676.  
  677. #####################
  678. # WRITE CONFIG FILE #
  679. #####################
  680.  
  681. global function saveStartupFile() {
  682.     local    cfgName
  683.     local    priorBuffer = current_buffer
  684.     local    cf
  685.     local    str
  686.     local   searchFlag = 0x0034     # ignore case | regex | forward
  687.  
  688.     # locate the config file in the current directory or SAGEEDIT path
  689.     if ( !(cfgName = locate_sageedit_file( SAGEEDIT_FILE_CONFIG ))) {
  690.         cfgName = editor_path( SAGEEDIT_FILE_CONFIG )
  691.     }
  692.  
  693.     # create a buffer for the config file
  694.     #
  695.     # (the buffer is created as a regular buffer rather than as a system
  696.     # buffer so that if the user has manually edited the config file, the
  697.     # changes made in the existing buffer will be reflected here and vice
  698.     # versa)
  699.     #
  700.     if ( !(cf = create_buffer( \
  701.             "", \
  702.             cfgName, \
  703.             BUFFER_NO_UNDO + BUFFER_REAL_SPACE_ONLY ))) {
  704.         return 0
  705.     }
  706.  
  707.     current_buffer = cf
  708.     goto_buffer_top()
  709.  
  710.     #
  711.     # clean out conflicting options
  712.     #
  713.     if ( emulationMode ) {
  714.             while ( search( \
  715.                 "^[ /t]*(native|brief|vi|wordstar|epsilon|msword)>", \
  716.                 searchFlag ) ) {
  717.             delete_line()
  718.             goto_buffer_top()
  719.         }
  720.     }
  721.     if ( windowOptions != "" ) {
  722.             while ( search( "^[ /t]*default_window_flags", searchFlag ) ) {
  723.             delete_line()
  724.             goto_buffer_top()
  725.         }
  726.     }
  727.     if ( windowColors != "" ) {
  728.             while ( search( "^[ /t]*(default_color_|color_background)", \
  729.                     searchFlag ) ) {
  730.             delete_line()
  731.             goto_buffer_top()
  732.         }
  733.     }
  734.  
  735.     #
  736.     # insert the new options
  737.     #
  738.     str = "# Sage Professional Editor - Configuration File"
  739.            if ( !search( "^" str, searchFlag ) ) {
  740.         insert_string( str "\n" )
  741.     }
  742.            if ( !search( "^[ /t]*\\$STATE\\$", searchFlag ) ) {
  743.         goto_buffer_bottom()
  744.     }
  745.     if ( emulationMode ) {
  746.         insert_string( emulationMode "\n" )
  747.     }
  748.     if ( windowOptions != "" ) {
  749.         insert_string( "default_window_flags=" windowOptions "\n" )
  750.     }
  751.     if ( windowColors != "" ) {
  752.         insert_string( "color_background=" color_background "\n")
  753.         insert_string( "default_color_text=" default_color_text "\n" )
  754.         insert_string( "default_color_highlight=" default_color_highlight "\n" )
  755.         insert_string( "default_color_border=" default_color_border "\n" )
  756.         insert_string( "default_color_title=" default_color_title "\n" )
  757.     }
  758.  
  759.     write_buffer()
  760.     delete_buffer()
  761.     current_buffer = priorBuffer
  762.  
  763.     return TRUE
  764. }
  765.  
  766. # ---------------------------------------------------------------------------
  767.  
  768. #################
  769. # menu utilites    #
  770. #################
  771.  
  772. # initMenu - create a window for use as a menu
  773. # deleteMenu - remove window created by initMenu
  774. # addMenuItem - add an item to the menu
  775. # getMenuSelection - select an item from the menu
  776.  
  777. local    _menuArray
  778. local    _menuHelpArray
  779. local    _nMenuItems
  780. local    _menuOriginalWindow
  781. local    _messageWindowRequired
  782.  
  783. global function initMenu( x,y, wide,high, c1,c2, header ) {
  784.  
  785.     _menuOriginalWindow = current_window
  786.  
  787.     makeWindow( x,y, wide,high, c1,c2, header )
  788.  
  789.     delete _menuArray
  790.     delete _menuHelpArray
  791.     _messageWindowRequired = 0
  792.     _nMenuItems = 0
  793. }
  794.  
  795. global function deleteMenu() {
  796.     delete_window()
  797.     if ( _menuOriginalWindow ) {
  798.         current_window = _menuOriginalWindow
  799.     }
  800. }
  801.  
  802. global function addMenuItem( item, helpText ) {
  803.     _nMenuItems++
  804.     _menuArray[ _nMenuItems ] = item
  805.     if ( helpText != "" ) {
  806.         _menuHelpArray[ _nMenuItems ] = helpText
  807.         _messageWindowRequired = 1
  808.     }
  809.  
  810.     put( _nMenuItems+3, 4, "  " _menuArray[ _nMenuItems ] )
  811. }
  812.  
  813. global function getMenuSelection( choice ) {
  814.  
  815.     local    key
  816.     local    scancode
  817.     local    prevChoice
  818.     local    result = 0
  819.     local    i
  820.  
  821.     if ( _messageWindowRequired ) {
  822.         initMenuMsg( window_y0 + window_height + 1, \
  823.                 color_text, color_border )
  824.     }
  825.  
  826.     do {
  827.         highlightChoice( choice, prevChoice )
  828.         prevChoice = choice
  829.  
  830.         # get menu command
  831.         key = get_command()
  832.         scancode = shiftr( key, 8 )    # ignore NumLock status
  833.  
  834.         if ( key == KEYCODE_ENTER || key == KEYCODE_KEYPAD_ENTER ) {
  835.             result = choice
  836.             break
  837.         } else if ( scancode == SCANCODE_HOME ) {
  838.             choice = 1
  839.         } else if ( scancode == SCANCODE_END ) {
  840.             choice = _nMenuItems
  841.         } else if ( scancode == SCANCODE_UP ) {
  842.             if ( choice > 1 ) {
  843.                 choice--
  844.             }
  845.         } else if ( scancode == SCANCODE_DOWN ) {
  846.             if ( choice < _nMenuItems ) {
  847.                 choice++
  848.             }
  849.         } else if ( scancode == SCANCODE_LEFT_PRESS ||        \
  850.                 scancode == SCANCODE_LEFT_CLICK ) {
  851.             choice = mouseMenuSelection( choice )
  852.         }
  853.  
  854.     } while ( key != KEYCODE_ESC )
  855.  
  856.     # clear message line
  857.     if ( _messageWindowRequired ) {
  858.         endMenuMsg()
  859.     }
  860.  
  861.     return result
  862. }
  863.  
  864. # Process a mouse press from within getMenuSelection.
  865. # Tracks the mouse cursor, highlights and returns the current menu item.
  866. #
  867. local function mouseMenuSelection( prevChoice ){
  868.     local    y0 = window_text_y0 + 2
  869.     local    choice = prevChoice
  870.     local    where
  871.     local    prevWindow
  872.  
  873.     if ( window_contains( mouse_event_x, mouse_event_y )) {
  874.         if ( mouse_position(1) == MOUSE_IN_TEXT ) {
  875.  
  876.             # keep choice up-to-date and highlighted, based on
  877.             # the current mouse position
  878.             do {
  879.                 choice = mouse_display_y - y0
  880.  
  881.                 if ( choice < 1 ) {
  882.                     choice = 1
  883.                 } else if ( choice > _nMenuItems ) {
  884.                     choice = _nMenuItems
  885.                 }
  886.  
  887.                 highlightChoice( choice, prevChoice )
  888.                 prevChoice = choice
  889.  
  890.             } while( and( mouse_buttons, BOTH_BUTTONS ))
  891.  
  892.             # if they end up in the text region, consider
  893.             # it a selection
  894.             if ( window_contains( mouse_event_x, mouse_event_y ) &&
  895.                     !window_border_contains(
  896.                         mouse_event_x, mouse_event_y )) {
  897.                 ungetkey( KEYCODE_ENTER )
  898.             }
  899.         }
  900.  
  901.     } else if ( inColorsMenu ) {
  902.         # Provide special handling for mouse actions outside of the
  903.         # main colors menu.  Normally, mouse actions outside of a
  904.         # menu are ignored during setup.
  905.         #
  906.         prevWindow = current_window
  907.         if ( window_containing( mouse_event_x, mouse_event_y ) ==
  908.                 colorBarWindow && choice <= 4 ) {
  909.             # mouse event in colorBar matrix
  910.             current_window = colorBarWindow
  911.             do {
  912.                 # get current foreground and background colors
  913.                 setNewColor( choice,
  914.                     mouse_display_x - window_text_x0,
  915.                     mouse_display_y - window_text_y0 )
  916.             } while ( and( mouse_buttons, BOTH_BUTTONS ))
  917.  
  918.             # fix color changes
  919.             textColor    = default_color_text
  920.             highlightColor    = default_color_highlight
  921.             borderColor    = default_color_border
  922.             titleColor    = default_color_title
  923.  
  924.         } else if ( window_containing( mouse_event_x, mouse_event_y ) ==
  925.                 sampleColorsWindow ) {
  926.             # mouse event in sample window
  927.             where = mouse_position(1)
  928.             if ( where == MOUSE_IN_TEXT ) {
  929.                 if ( mouse_event_y != 19 ||
  930.                         mouse_event_x < 11 &&
  931.                         mouse_event_x > 35 ) {
  932.                     choice = 1    # normal text
  933.                 } else {
  934.                     choice = 2    # highlit text
  935.                 }
  936.             } else if ( and( where, MOUSE_N + MOUSE_E )) {
  937.                 choice = 4        # title bar
  938.             } else if ( and( where, MOUSE_S + MOUSE_W )) {
  939.                 choice = 3        # rest of border
  940.             }
  941.         }
  942.         current_window = prevWindow
  943.     }
  944.     return choice
  945. }
  946.  
  947. ## function to highlight the current menu item
  948. #
  949. local function highlightChoice( choice, prevChoice ){
  950.     if ( choice != prevChoice ) {
  951.  
  952.         # display help text for current selection
  953.         if ( _messageWindowRequired ) {
  954.             menuMsg( _menuHelpArray[ choice ] )
  955.         }
  956.  
  957.         # un-highlight the previous selection
  958.         highlight_window( \
  959.                 0, prevChoice + 2, \
  960.                 window_text_width, 1, \
  961.                 color_text )
  962.  
  963.         # update the cursor position
  964.         window_cursor_x = 4
  965.         window_cursor_y = choice + 2
  966.  
  967.         # highlight the current selection
  968.         highlight_window( \
  969.                 1, choice + 2, \
  970.                 window_text_width - 2, 1, \
  971.                 BlackOnGray )
  972.  
  973.         display_update()
  974.     }
  975. }
  976.  
  977. # ---------------------------------------------------------------------------
  978.  
  979. # low level menu window utilities
  980.  
  981. # makeWindow    - create a text-only window suitable for use with menus
  982. # put        - display a line at specified position in window
  983. # ctr        - center a line at specified row in the window
  984. # initMenuMsg    - make a place for descriptive text to get printed
  985. # menuMsg    - display a one line description for the current menu item
  986. # endMenuMsg    - remove window created by initMenuMsg()
  987. # get_command    - wait for and return a key, after updating the display
  988.  
  989. # create a window and make it current
  990. global function makeWindow( x,y, wide,high, c1,c2, header ) {
  991.  
  992.     local    priorTextColor        = default_color_text
  993.     local    priorBorderColor    = default_color_border
  994.  
  995.     default_color_text = c1
  996.     default_color_border = c2
  997.  
  998.     current_window = create_window( \
  999.             x,y, \
  1000.             wide,high, \
  1001.             WINDOW_SYSTEM + WINDOW_BORDER )
  1002.  
  1003.     if ( header ) {
  1004.         ctr( 2, header )
  1005.     }
  1006.  
  1007.     default_color_text   = priorTextColor
  1008.     default_color_border = priorBorderColor
  1009.  
  1010.     return current_window
  1011. }
  1012.  
  1013. # display a line at specified position in current window
  1014. global function put( row, col, str ) {
  1015.     window_cursor_x = col
  1016.     window_cursor_y = row - 1
  1017.     write_window( str )
  1018. }
  1019.  
  1020. # center a line at specified row in the current window
  1021. global function ctr( row, str ) {
  1022.     local    col = (window_text_width - length(str)) / 2
  1023.     if ( col < 1 ) {
  1024.         col = 1
  1025.     }
  1026.     put( row, col, str )
  1027. }
  1028.  
  1029.  
  1030.  
  1031. # make a place for messages and prompts to get printed
  1032.  
  1033. local    _messageWindow
  1034. local    _messageLength
  1035.  
  1036. global function initMenuMsg( row, colors1, colors2 ) {
  1037.  
  1038.     local    priorWindow = current_window
  1039.  
  1040.     _messageWindow = makeWindow(
  1041.             1, row, \
  1042.             display_width - 2, 3, \
  1043.             colors1, colors2, \
  1044.             "" )
  1045.     _messageLength = 0
  1046.  
  1047.     if ( priorWindow ) {
  1048.         current_window = priorWindow
  1049.     }
  1050. }
  1051.  
  1052. global function menuMsg( str ) {
  1053.  
  1054.     local    priorWindow = current_window
  1055.     local    n = _messageLength - length( str )
  1056.  
  1057.     current_window = _messageWindow
  1058.     put( 0, 0, str )
  1059.  
  1060.     # blank text remaining from prior message
  1061.     if ( n > 0 ) {
  1062.         write_window( strrepeat( " ", n ) )
  1063.     }
  1064.     _messageLength = length( str )
  1065.  
  1066.     if ( priorWindow ) {
  1067.         current_window = priorWindow
  1068.     }
  1069. }
  1070.  
  1071. global function endMenuMsg() {
  1072.     delete_window( _messageWindow )
  1073.     _messageWindow = 0
  1074. }
  1075.  
  1076. # wait for and return a key
  1077. local function get_command() {
  1078.     local key
  1079.  
  1080.     display_update()
  1081.     key = getkey()
  1082.     flush_keyboard()
  1083.     return key
  1084. }
  1085.