home *** CD-ROM | disk | FTP | other *** search
/ PC World 1998 June / PCWorld_1998-06_cd.bin / software / sharware / grafika / EROICA32 / SAMPLE / IMGDEMO1.ZIP / DIALOG.ASC < prev    next >
Text File  |  1997-12-23  |  23KB  |  567 lines

  1.     HOW TO USE THE BRIEF DIALOG MANAGER PACKAGE
  2.  
  3. WHAT IS THE DIALOG MANAGER?
  4.  
  5. The BRIEF dialog manager is a macro package containing tools for
  6. generating pop-up menus and dialog boxes.  Menus allow a user to perform
  7. complicated tasks by choosing from lists of possible actions.  Dialog
  8. boxes allow a user to answer questions and fill in forms, while possibly
  9. restricting the set of valid answers.
  10.  
  11. Many of BRIEF's standard macros (Help, for example) use the dialog
  12. manager.  You can use the dialog manager in your own macros, too.
  13.  
  14. WHERE IS THE DIALOG MANAGER?
  15.  
  16. Source code for the package is located in the files dialog.m and
  17. dialog.h; the compiled version is in dialog.cm.
  18.  
  19. WHY USE THE DIALOG MANAGER?
  20.  
  21. The BRIEF dialog manager provides a standard user interface for macros.
  22. If you write a macro that uses this interface properly, it will have the
  23. look and feel of the standard commands that are shipped with BRIEF, and
  24. any BRIEF user will be able to use it with minimal effort.
  25.  
  26. Using the dialog manager conserves memory and avoids reinventing the
  27. wheel.  Suppose you frequently use Help and three other macro packages
  28. with separate user interfaces.  That's a lot of macros to load at once;
  29. BRIEF may spend a fair amount of time re-reading the disk.  The macros
  30. will take up far less memory if they reuse the same code.
  31.  
  32. WHAT CAPABILITIES ARE PROVIDED?
  33.  
  34. The dialog manager is essentially just two macro calls, one to create
  35. and process a menu, and one to create and process a dialog box.
  36. Although the dialog manager contains dozens of other macros (as well as
  37. several global variables,) you should seldom need to deal with them.
  38. (Consult the source code for more information.)
  39.  
  40. Menus are pop-up windows, within which one line ("button") is always 
  41. highlighted.  Pressing Enter ("picking") causes an action associated
  42. with the highlighted button to be executed.  The up and down arrow keys
  43. move the highlight up or down.  Windows scroll vertically as needed.
  44.  
  45. Menus offer a choice of several actions.  These actions may invoke other
  46. menus or dialog boxes.  In Help, for example, picking some buttons
  47. causes a window of help information on a particular topic to appear;
  48. picking others causes another menu (more detailed) to appear.
  49.  
  50. Dialog boxes are the other half of the dialog manager.  Dialog boxes are
  51. pop-up windows that contain descriptive text and any of several kinds of
  52. input fields.  A user may move between fields and change the contents of
  53. any field.  These changes can be saved or cancelled as a group.
  54.  
  55. The kinds of input fields are:
  56.     Filenames
  57.     Lists
  58.     Integers
  59.     Nonblank strings
  60.     Strings
  61.  
  62. LISTS
  63.  
  64. Lists, like menus, are multiple-choice situations; however, lists are
  65. choices of values, not actions, and the values are usually mutually
  66. exclusive.  One very common list has two choices: Yes and No.
  67.  
  68. Lists appear on the screen on a single line, with items separated by
  69. whitespace.  A highlight is used to mark the currently selected item
  70. while the user's cursor is on the field; when he or she leaves, the
  71. highlight changes to a pair of parentheses.
  72.  
  73. A list can be paired with a text string, which is usually a question:
  74.  
  75.     Do you want to save your changes?  (Yes)No
  76.     Line drawing style:           Single(Double)Mixed None
  77.  
  78. FILENAMES, INTEGERS, NONBLANK STRINGS, AND STRINGS
  79.  
  80. The number of possible answers to a question is often so great that a
  81. list is awkward or impossible.  In such situations, it's best to let the
  82. user to type in his or her answer, then check it for validity.  The user
  83. can be asked to re-enter an invalid answer.
  84.  
  85. Filenames, Integers, Nonblank strings, and Strings behave identically
  86. except that they use different validation criteria.  Strings are not
  87. checked at all.  Nonblank strings may not be blank.  Integer fields must
  88. contain an integer value, and Filename fields must contain a
  89. syntactically legal path name.
  90.  
  91. Fields are one line deep and may extend from their beginning to the
  92. right edge of the window.  The entire field is highlighted when the
  93. user's cursor enters it; the highlight disappears as soon as anything
  94. happens.  When the user's cursor leaves, the field contents are
  95. validated.
  96.  
  97. KEY ASSIGNMENTS
  98.  
  99. The key assignments for moving around and between menus and dialog boxes
  100. are standardized.  You should not change these key assignments, although
  101. you may add your own to the list.
  102.  
  103. These key assignments are uniform throughout the dialog manager:
  104. Esc        Exit all levels        Keypad minus    Exit 1 level
  105. Ctrl-Home    Top of window        Ctrl-End    End of window
  106.  
  107. Within menus, the additional assignments are:
  108. Enter        Pick from menu
  109. Up arrow    Previous item        Down arrow    Next item
  110. Backspace    Previous item        Space        Next item
  111. Home        First item        End        Last item
  112. PgUp        Previous page        PgDn        Next page
  113. Alphanumeric character sequence   Next item beginning with characters
  114.  
  115. Within any dialog box field, the following keys are defined:
  116. Enter        Next field, or save if on last field (== Ctrl-m)
  117. F10        Save dialog box and exit
  118. Up arrow    Previous field        Down arrow    Next field
  119. Shift-Tab    Previous field        Tab        Previous field
  120.  
  121. Within lists only, additional assignments are:
  122. Left arrow    Previous item        Right arrow    Next item
  123. Backspace    Previous item        Space        Next item
  124. Home        First item        End        Last item
  125. Alphanumeric character    Next item beginning with character
  126.  
  127. Within fields (excepting lists), additional key assignments are:
  128. Left arrow    Left            Right arrow    Right
  129. Backspace    Backspace        Space        Space
  130. Home        Beginning of field    End        End of field
  131. Del        Delete character    Alt-k        Delete to end
  132. Ins, Alt-i    Toggle insert mode    Alt-d        Delete field
  133.  
  134. When a field is highlighted, typing causes the contents of the field to
  135. be replaced by the new characters.
  136.  
  137.     MENUS
  138.  
  139. HOW TO CREATE A MENU
  140.  
  141. There are 3 steps involved in creating a menu.
  142.  
  143. 1.  Create a data file containing the names of the menu buttons and the
  144. actions you want the buttons to invoke.
  145.  
  146. 2.  Write a macro that calls the dialog manager package.
  147.  
  148. 3.  Write the action macros that will handle everything the user does in
  149. the menu.
  150.  
  151. CREATING THE MENU DATA FILE
  152.  
  153. Menu data files are usually given a .mnu extension and kept in the same
  154. directory as the help files (specified by BHELP).
  155.  
  156. A menu file contains one line for each line of the menu.  Each line
  157. consists of a button name and optional additional information.  When
  158. the menu is displayed, the additional information should be invisible
  159. (just past the right edge of the window).  The menu button is normally
  160. centered in the visible part of the line.
  161.  
  162. For fastest operation, each line is normally pre-formatted; in this
  163. example (taken from help.mnu), spaces are used to center the buttons in
  164. the visible part of the window, which is delimited by the semicolons.
  165. Everything to the right of the semicolons is considered "additional
  166. information":
  167.  
  168.    Help on Help    ;display_help "help"
  169.   Quick Reference  ;display_help "keyboard layout"
  170.  Key-Specific Help ;key_specific_help
  171.  
  172. You can also have the menu automatically formatted.  If you do, you
  173. must use a semicolon as the delimiter, and leading/trailing whitespace
  174. around the buttons will be ignored:
  175.  
  176. Help on Help;display_help "help"
  177. Quick Reference;display_help "keyboard layout"
  178. Key-Specific Help;key_specific_help
  179.  
  180. Action macros, described below, are free to do whatever they want with
  181. the additional information.  In this case, the information is a macro
  182. call (complete with a string parameter), and it is passed to
  183. execute_macro.
  184.  
  185. CALLING UP THE MENU
  186.  
  187. The call to create and process a menu is as follows:
  188.  
  189. (_process_menu lx by rx ty title msg filename buf_id action fast
  190.     picked_line picked_text)
  191.  
  192. Parameters 0 through 3 are integers, the coordinates of the left (lx),
  193. bottom (by), right (rx), and top (ty) edges of the window.  These are
  194. absolute screen coordinates; the top left corner of a 25x80 screen is
  195. (0, 0) and the lower right is (24, 79).  If lx equals rx or ty equals
  196. by, the menu is positioned at (lx, ty) and the size will be computed
  197. automatically based on the number of lines on the menu and the column
  198. position of the first semicolon.  (Be careful not to position menus
  199. so close to the bottom and left edges of the screen so that there's
  200. no room for automatic formatting.)
  201.  
  202. Parameter 4 is the title of the menu, a string to be displayed in the
  203. top edge of the window.  It may be at most 12 characters long.
  204.  
  205. Parameter 5 is a string, a message to be displayed in the bottom edge of
  206. the window.  Make sure it is shorter than the edge itself.
  207.  
  208. You should supply either, but not both, of parameters 6 and 7.  Supply
  209. NULL in the place of whichever parameter you omit.  Parameter 6 is the
  210. filename of a menu data file (in the BHELP directory), while parameter 7
  211. is the identifier of a buffer containing a menu data file.  Use
  212. parameter 7 if you have a .mnu file already in memory and want to keep
  213. it there; otherwise, use parameter 6 and the dialog manager will delete
  214. the buffer when you're done with it.  (If you use parameter 7, then the
  215. title you passed in parameter 4 will be ignored, and the buffer name
  216. already in existence will be used.)
  217.  
  218. Note that parameter 6 is assumed to be the name of a file in the
  219. BHELP directory unless an absolute path name is supplied.  The dialog
  220. manager assumes that any name containing the characters /, \, or : is
  221. an absolute path.
  222.  
  223. Parameter 8 is a string containing the name of your main action macro,
  224. described below.
  225.  
  226. Parameter 9 may be omitted.  If it is passed and TRUE, your menu file
  227. is not formatted in any way, saving oodles of time when it pops up.
  228. If you use this parameter, make sure to pre-format your .mnu file.
  229.  
  230. Parameter 10 is an optional integer variable which will be set to the
  231. number of the menu button that the user picks.  It is only valid when
  232. the call to _process_menu returns TRUE.  If the user presses <Esc> or
  233. <Keypad minus> to escape from a menu, this parameter will be zero.
  234.  
  235. Parameter 11 is an optional string variable which will be set to the
  236. text of the menu button that the user picks.  It is only valud when
  237. the call to _process_menu returns TRUE and parameter 10 is nonzero.
  238.  
  239. The dialog manager assumes the values you pass it are correct, so be
  240. careful.
  241.  
  242. When the call to _process_menu is encountered, the menu will pop up and
  243. processing of the user's keystrokes will begin immediately.  Processing
  244. will stop when the user presses Esc (or the keypad minus key), and
  245. control will return to your macro, just after the _process_menu call.
  246. _process_menu returns TRUE if it was able to create and process a menu,
  247. FALSE if an error occurred.
  248.  
  249. EVENTS
  250.  
  251. We have written the dialog manager to be as flexible as possible.
  252. Hence, the system leaves a lot of decisions and a lot of work up to your
  253. action macro.  The dialog manager is event-driven; it quietly goes about
  254. its business until an "event" of significance occurs, at which point it
  255. taps your macro on the shoulder, informs it of the situation, and waits
  256. for a response before continuing.
  257.  
  258. Here's a list of all significant events, for both menus and dialog
  259. boxes.  Matching #define statements are contained in the file
  260. "dialog.h".  You should #include this file in any macro file you write
  261. that calls the dialog manager, so that you can refer to the event types
  262. by symbolic name rather than by number.
  263.  
  264. DIALOG_INIT        The dialog manager has just been invoked
  265. DIALOG_TERM        The dialog manager is exiting
  266. DIALOG_ENTER_FIELD    The cursor has just entered a non-list field
  267. DIALOG_EXIT_FIELD    The cursor has just left a non-list field
  268. DIALOG_ENTER_LIST    The cursor has just entered a list
  269. DIALOG_EXIT_LIST    The cursor has just left a list
  270. DIALOG_ALTER_LIST    The current item in a list has just been changed
  271. DIALOG_ALTER_MENU    The current item in a menu has just been changed
  272. DIALOG_MOVE_MENU    (Same as DIALOG_ALTER_MENU)
  273. DIALOG_PICK_MENU    The user has just selected a menu button with Enter
  274. DIALOG_ESCAPE        The user has just pressed Esc to exit all levels
  275. DIALOG_F10        The user has just pressed F10 to save a dialog box
  276. DIALOG_GREY_MINUS    The user has just pressed the keypad minus key
  277. DIALOG_CREATE_MENU    A menu buffer has just been made current
  278. DIALOG_CREATE_DBOX    A dialog box data buffer "
  279.  
  280. HOW TO WRITE THE MENU ACTION MACROS
  281.  
  282. When you're working with menus, only the invoke/exit events, the
  283. Esc/keypad minus events, and the events for changing or selecting menu
  284. items can happen.  Your job is to provide a single macro that responds
  285. to these events; the name of this macro is parameter 8 in the call to
  286. _process_menu, and the macro is called by _process_menu whenever one of
  287. these events occurs.
  288.  
  289. Suppose you call your action macro "action".  When called, action will
  290. be passed three parameters:
  291.  
  292.     (action event_type line text)
  293.  
  294. The first parameter is an integer matching one of the above #defines.
  295. The second is the number of the current line in the menu (note that this
  296. is also the number a user will see as Line: in the BRIEF message area).
  297. The third is the text of the entire button, including the delimiter and
  298. additional information (if any).
  299.  
  300. In the BRIEF macro language, parameters in the calling function are only
  301. evaluated when the called function explicitly requests the parameter.
  302. This means that, for best performance, your action macro should never
  303. get a parameter it does not need.
  304.  
  305. The normal strategy for processing events within a menu action macro is
  306. as follows:
  307.  
  308. 1.  Get the event type parameter.
  309.  
  310. 2.  If the event type is DIALOG_PICK_MENU, then get the line number or
  311. text parameters and process them as desired.  Note that the dialog
  312. manager does NOT execute an action associated with a button
  313. automatically.  Your action macro must parse the button text and execute
  314. the command.
  315.  
  316. Commonly, a pick event will cause the action macro to pop up a window
  317. of text (such as help) and enter a process under the user's control.
  318. The window will remain visible until the user presses Esc or Keypad
  319. minus to signal he's done.  If the user pressed Esc, the action macro
  320. should push the Esc back into the keyboard buffer so that a DIALOG_ESC
  321. event is generated as soon as the action macro returns.
  322.  
  323. 3.  If the event type is DIALOG_CREATE_MENU, the unformatted menu 
  324. is the current buffer, and its name is the text parameter.  You can
  325. use this event to add buttons to the menu.
  326.  
  327. (If the event type is anything else, you usually don't need to do any
  328. more.  However, if you want to add key assignments to the normal menu
  329. keymap, you can use DIALOG_INIT as a signal to add them, and DIALOG_TERM
  330. as a signal to remove them.  And if you want to preclude the user from
  331. moving to a particular line of the menu (for example, a heading) then
  332. you can use DIALOG_MOVE_MENU.)
  333.  
  334. 3.  Return TRUE.  The only time you should return FALSE is if the event
  335. was DIALOG_MOVE_MENU, and the line number or text was such that you
  336. don't want the cursor to be allowed to move to it (other lines will be
  337. tried until EOF or until DIALOG_MOVE_MENU succeeds).
  338.  
  339. ACTION MACRO EXAMPLE
  340.  
  341. The following macro, when called by the dialog manager, will handle two
  342. types of events.  First, the user will not be allowed to move to line 1
  343. or line 4.  Second, when the user picks a button, this macro will use 
  344. the additional information present in the .mnu file to call another macro.
  345.  
  346. (macro action
  347.    (
  348.       (int      event_type
  349.         line_no
  350.         retval
  351.       )
  352.       (string   button_text)
  353.  
  354.       (get_parm 0 event_type)
  355.       (= retval TRUE)
  356.  
  357.       (switch event_type
  358.  
  359.      DIALOG_MOVE_MENU
  360.         (
  361.            (get_parm 1 line_no)
  362.  
  363.            (if (|| (== line_no 1) (== line_no 4))
  364.           (= retval FALSE)
  365.            )
  366.         )
  367.  
  368.      DIALOG_PICK_MENU
  369.         (
  370.            (get_parm 2 button_text)
  371.  
  372.            ;**   Trim the button name and treat the additional
  373.            ;**   information as a command that can be executed.
  374.  
  375.            (execute_macro (substr button_text
  376.            (+ (index button_text ";") 1)))
  377.         )
  378.       )
  379.       (returns retval)
  380.    )
  381. )
  382.  
  383.     DIALOG BOXES
  384.  
  385. HOW TO CREATE A DIALOG BOX
  386.  
  387. As with menus, there are 3 steps to creating a dialog box: creating a
  388. data file, writing a macro that calls the dialog manager, and writing
  389. action macros.
  390.  
  391. DATA FILES FOR DIALOG BOXES
  392.  
  393. Dialog box data files must be kept in the BHELP directory.  Like menu
  394. files, they contain one line for each item displayed.  Each line
  395. contains the type of the input field, the row and column coordinates
  396. (relative to the window's origin) at which the field should be placed,
  397. and the field's initial contents.  The number of fields is unlimited
  398. because dialog boxes, like menus, can scroll vertically.
  399.  
  400. The row and column coordinates must be surrounded by parentheses and
  401. separated by a comma.  The initial contents must be surrounded by double
  402. quotes.  Whitespace may go just about anywhere, and you can have
  403. comments.  The following example shows a number of legal ways of
  404. defining fields, and descriptive text, in a dialog box:
  405.  
  406. ;**  Get the speed using a 3-item list.
  407. Text at (1, 1) is "Speed:"
  408. List ( 1, 8 ) = "(Slow)Medium Fast"
  409.  
  410. ;**  Get the user's name.  Make sure they enter one.
  411. text (3,1) "Name:"
  412.     nonblank(3,9)  "Your name goes here"
  413. ;**  Prompt for user's age.  Make sure it's numeric.
  414. T    (5, 1)   "Age:"
  415. I(5,9)"24"
  416.  
  417. The first non-whitespace character on a line defines the type of the
  418. field.  A 'T' means that this line is a descriptive text string that can
  419. be displayed only.  The letters F, L, I, N, and S denote Filenames,
  420. Lists, Integers, Nonblank strings, and Strings.
  421.  
  422. All fields, even lists and integers, are really strings.  Hence, you
  423. should format the initial contents of every field as a quoted string, as
  424. in the example above.
  425.  
  426. List strings have a special format.  Items must be separated by single
  427. tab characters (since spaces are allowed in item names).  The item that
  428. is to be initially highlighted should be surrounded by parentheses
  429. instead of tabs.
  430.  
  431. Position lists as you would any other field.  If a list begins with
  432. a (, the dialog manager will compensate for you.
  433.  
  434. If you omit the current item in a list, or if you supply a field that's
  435. too wide for the window, the invalid field will not be displayed and an
  436. error message will.
  437.  
  438. The dialog manager does not check to see if any fields overlap.  Hence,
  439. only one input field is allowed per line.  Text can be placed anywhere
  440. except to the right of an input field.
  441.  
  442. HOW TO CALL UP A DIALOG BOX
  443.  
  444. The call to create and process a dialog box is nearly identical to the
  445. call to create a menu, except that the macro name is _process_dialog_box:
  446.  
  447. (_process_dialog_box lx by rx ty title msg filename buf_id action)
  448.  
  449. All parameters have the same meaning as for _process_menu, except:
  450.  
  451. 1.  There is no automatic sizing for dialog boxes, so lx must not equal
  452. rx, and by must not equal ty.
  453.  
  454. 2.  Parameter 4, the title, is always used, even when a buffer ID is passed
  455. for parameter 7.
  456.  
  457. 3.  There are no parameters 9, 10, or 11.
  458.  
  459. This call may return when the user presses F10, Esc, or the keypad minus
  460. key.  _process_dialog_box returns TRUE if it was successful, FALSE if an
  461. error occurred.
  462.  
  463. HOW TO WRITE THE ACTION MACROS
  464.  
  465. The first two parameters passed to your action function are the event
  466. type and line number.  The third parameter is either (for lists) the
  467. text of the current list item, not counting separator characters, or
  468. (for other field types) the full text of the field, not counting the
  469. newline at the end.
  470.  
  471. Any event may occur in a dialog box except DIALOG_ALTER_MENU or
  472. DIALOG_PICK_MENU.  However, the only events you normally have to worry
  473. about are DIALOG_EXIT_FIELD, DIALOG_EXIT_LIST, and DIALOG_F10.
  474.  
  475. You can perform additional validation on the contents of a field when
  476. the user wants to move to another field (DIALOG_EXIT_FIELD).  After the
  477. dialog manager has performed its built-in type checking on the field,
  478. your action macro will be called.  If your action macro returns FALSE
  479. for any reason, the user will not be allowed to leave the field.  You
  480. can use a String field coupled with your own validation function to
  481. check for almost any value.  Lists are not validated, since all the
  482. possible answers are known in advance.
  483.  
  484. When the user leaves a field or a list (DIALOG_EXIT_FIELD or
  485. DIALOG_EXIT_LIST), keep track of the value of that input field.  There
  486. is no way to determine the value of an input field at any other time.
  487. We recommend you use a separate buffer, where each line represents the
  488. contents of one input field, for dialog boxes containing more than three
  489. or four input fields; otherwise, use string variables to store the
  490. current values.
  491.  
  492. Treat DIALOG_F10 as a signal that the user is satisfied with his or her
  493. changes and wishes to save them.  The current list or field will be
  494. exited before DIALOG_F10 occurs.  When it does, process the values you
  495. have saved in your macro.  (DIALOG_ESC and DIALOG_GREY_MINUS should just
  496. ignore the saved values.)
  497.  
  498. EXAMPLE
  499.  
  500. This data file ("example") defines a dialog box containing two lists.
  501.  
  502. Text (1, 3)  = "Do you want case-sensitive search?"
  503. List (1, 38) = "(Yes)No"
  504. Text (3, 3)  = "Do you want regular expressions?"
  505. List (3, 38) = "(Yes)No"
  506.  
  507. This macro puts up a dialog box containing the above information, and
  508. defines two string variables which are used by the action macro.  Both
  509. variables are set to Yes, since the data file's defaults are Yes.
  510.  
  511. (macro put_up_box
  512.    (
  513.       (string   case_sens
  514.         reg_exp
  515.       )
  516.       (global  case_sens
  517.            reg_exp
  518.       )
  519.  
  520.       (= case_sens "Yes")
  521.       (= reg_exp "Yes")
  522.       (_process_dialog_box 10 15 60 10 "Search" "Set search parameters"
  523.         "example" NULL "action")
  524.    )
  525. )
  526.  
  527. The following macro will process the dialog box, and save the current
  528. values when F10 is pressed:
  529.  
  530. (macro action
  531.    (
  532.       (int    event_type
  533.           line_no
  534.       )
  535.       (string  button_text)
  536.  
  537.       (get_parm 0 event_type)
  538.  
  539.       (switch event_type
  540.      DIALOG_EXIT_LIST
  541.         (
  542.            (get_parm 1 line_no)
  543.            (get_parm 2 button_text)
  544.  
  545.            (if (== line_no 1)
  546.           (= case_sens button_text)
  547.            ;else
  548.           (= reg_exp button_text)
  549.            )
  550.         )
  551.      DIALOG_F10
  552.         (
  553.            (search_case (if (== case_sens "Yes") 0 1))
  554.            (toggle_re (if (== reg_exp "Yes") 1 0))
  555.         )
  556.       )
  557.       (returns TRUE)
  558.    )
  559. )
  560.  
  561. A more sophisticated macro could keep the data file in a buffer and
  562. continuously alter it to make sure the correct values of case
  563. sensitivity and regular expressions were always displayed.
  564.  
  565. That's all you need to know to design menus and dialog boxes in the
  566. BRIEF macro language.  Enjoy.
  567.