home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / program / progem / gem7.asc < prev    next >
Encoding:
Text File  |  1987-10-10  |  16.6 KB  |  377 lines

  1.                         *PROFESSIONAL GEM*
  2.                            By Tim Oren
  3.                    Column #7 - Menu Structures
  4.  
  5.     HAPPY NEW YEAR!  This is article number seven in the ST
  6. PRO GEM series, and the first for 1986.  In this installment,
  7. I will be discussing GEM menu structures and how to use them
  8. in your application.  There is  also a short Feedback
  9. response section.  You will find the download file containing
  10. the code for this column in the file GEMCL7.C in DL3 of the
  11. ATARI16 SIG (PCS-58).
  12.  
  13.     MENU BASICS.  In ST GEM, the menu consists of a bar
  14. across the top of the screen which displays several sub-menu
  15. titles.  Touching one of the titles causes it to highlight,
  16. and an associated "drop-down" to be drawn  directly below on
  17. the screen.  This drop-down may be dismissed by moving  to
  18. another title, or by clicking the mouse off of the drop-down.
  19.  
  20.  
  21.     To make a selection, the mouse is moved over the
  22. drop-down.  Each  valid selection is highlighted when the
  23. mouse touches it.  Clicking the mouse  while over one of
  24. these selections picks that item.  GEM then undraws the
  25. drop-down, and sends a message to your application giving the
  26. object number  of the title bar entry, and the object number
  27. of the drop-down item which  were selected by the user.  The
  28. selected title entry is left highlighted  while your code
  29. processes the request.
  30.  
  31.     MENU STRUCTURES.  The data structure which defines a GEM
  32. menu is (surprise!) an object tree, just like the dialogs and
  33. panels which we have discussed before.  However, the
  34. operations of the GEM menu manager are quite different from
  35. those of the form manager, so the internal design of the menu
  36. tree has some curious constraints.
  37.  
  38.     The best way to understand these constraints is to look
  39. at an  example.  The first item in the download is the object
  40. structure (only) of the menu tree from the GEM Doodle/Demo
  41. sample application.
  42.  
  43.     The ROOT of a menu tree is sized to fit the entire
  44. screen.  To satisfy the visual hierarchy principle (see
  45. article #5), the screen is divided into two parts: THE BAR,
  46. containing the menu titles, and THE SCREEN, while contains
  47. the drop-downs when they are drawn.  Each of these areas is
  48. defined by an object of the same name, which are the only two
  49. objects linked directly below the ROOT of a menu tree.  You
  50. will notice an important implication of this structure:  The
  51. menu titles and their associated drop-downs are stored in
  52. entirely different subtrees of the menu!
  53.  
  54.     While examining THE BAR in the example listing, you may
  55. notice that its OB_HEIGHT is very large (513).  In
  56. hexadecimal this is 0x0201.  This defines a height for THE
  57. BAR of one character plus two pixels used for spacing.  THE
  58. BAR and its subtree are the only objects which are drawn on
  59. the screen in the menu's quiescent state.
  60.  
  61.     The only offspring object of THE BAR is THE ACTIVE.  This
  62. object defines the part of THE BAR which is covered by menu
  63. titles.  The screen rectangle belonging to THE ACTIVE is used
  64. by the GEM screen manager when it  waits for the mouse to
  65. enter an active menu title.  Notice that THE ACTIVE and its
  66. offspring also have OB_HEIGHTs with pixel residues.
  67.  
  68.     The actual menu titles are linked left to right in order
  69. below THE ACTIVE.  Their OB_Xs and OB_WIDTHs are arranged so
  70. that they  completely cover THE ACTIVE.  Normally, the title
  71. objects are typed  G_TITLE, a special type which assures that
  72. the title bar margins are correctly drawn.
  73.  
  74.     THE SCREEN is the parent object of the drop-down boxes
  75. themselves. They are linked left to right in an order
  76. identical with their titles, so  that the menu manager can
  77. make the correct correspondence at run-time.  The OB_X of
  78. each drop-down is set so that it is positioned below its
  79. title on the screen.
  80.  
  81.     Notice that it is safe to overlap the drop-downs within a
  82. menu,  since only one of them will be displayed at any time.
  83. There is one constraint on the boxes however:  They must be
  84. no greater than a quarter screen in total size.  This is the
  85. size of the off-screen blit buffer which is used by GEM to
  86. store the screen contents when the drop-down is drawn.  If
  87. you exceed this size, not all the screen under the drop-down
  88. will be restored, or the ST may crash!
  89.  
  90.     The entries within a drop-down are usually G_STRINGs,
  91. which are optimized for drawing speed.  The rectangles of
  92. these entries must  completely cover the drop-down, or the
  93. entire drop-down will be inverted  when the mouse touches an
  94. uncovered area!  Techniques for using objects  other than
  95. G_STRINGs are discussed later in this column.
  96.  
  97.     The first title and its corresponding drop-down are
  98. special.  The title name, by custom, is set to DESK.  The
  99. drop-down must contain exactly  eight G_STRING objects.  The
  100. first (again by custom) is the INFO entry,  which usually
  101. leads to a dialog displaying author and copyright information
  102. for your application.  The next is a separator string of
  103. dashes with the DISABLED flag set.  The following six objects
  104. are dummy strings which GEM fills in with the names of desk
  105. accessories when your menu is loaded.
  106.  
  107.     The purpose of this description of menu trees is to give
  108. you an understanding of what lies "behind the scenes" in the
  109. next section, which describes the run-time menu library
  110. calls.  In practice, the Resource Construction Set provides
  111. "blank menus" which include all of the required elements, and
  112. it also enforces the constraints on internal structure.  You
  113. only need to worry about these if you modify the menu tree
  114. "on-the-fly".
  115.  
  116.     USING THE MENU.  Once you have loaded the application's
  117. resource, you can ask the AES to install your menu.  You must
  118. first get the address of the menu tree within the resource
  119. using:
  120.  
  121.     rsrc_gaddr(R_TREE, MENUTREE, &ad_menu);
  122.  
  123.  assuming that MENUTREE is the name you gave the menu in the
  124. RCS, and that ad_menu is a LONG which will receive the
  125. address.  Then you call the AES to establish the menu:
  126.  
  127.     menu_bar(ad_menu, TRUE);
  128.  
  129.  At this point, the AES draws your menu bar on the screen and
  130. animates it when the user moves the mouse into the title
  131. area.
  132.  
  133.     The AES indicates that the user has made a menu selection
  134. by sending your application a message.  The message type is
  135. MN_SELECTED,  which will be stored in msg[0], the first
  136. location in the message  returned by evnt_multi().
  137.  
  138.     The AES also stores the object number of the selected
  139. menu's  title in msg[3], and the object number of the
  140. selected menu item in msg[4].  Generally, your application
  141. will process menu messages with nested C switch statements.
  142. The outer switch will have one case for each menu title, and
  143. the inner switch statements will have a case for each entry
  144. within the selected menu.  (This implies that you must give a
  145. name to each title and to each menu entry when you create the
  146. menu in the RCS.)
  147.  
  148.     After the user has made a menu selection, the AES leaves
  149. the title of the chosen menu in reverse video to indicate
  150. that your application is busy processing the message.  When
  151. you done with whatever action is indicated, you need to
  152. return the title to a normal state. This is done with
  153.  
  154.     menu_tnormal(ad_menu, msg[3], TRUE);
  155.  
  156.  (Remember that msg[3] is the title's object number.)
  157.  
  158.     When your application is ready to terminate, it should
  159. delete its menu bar.  Do this with the call:
  160. menu_bar(ad_menu, FALSE);
  161.  
  162.     GETTING FANCY.  The techniques above represent the bare
  163. minimum to handle menus.  In most cases, however, you will
  164. want your menus to be more "intelligent" in displaying the
  165. user's options.  For instance, you can prevent many user
  166. errors by disabling inappropriate choices, or you can save
  167. space on drop-downs by showing only one line for a toggle and
  168. altering its text or placing and removing a check mark when
  169. the state is changed.  This section discusses these and other
  170. advanced techniques.
  171.  
  172.     It is a truism of user interface design that the best way
  173. to deal with an error is not to let it happen in the first
  174. place.  It  many cases, you can apply this principle to GEM
  175. menus by disabling  choices which should not be used.  If
  176. your application uses a "selection  precedes action" type of
  177. interface, the type of object selected may  give the
  178. information needed to do this.   Alternately, the state of
  179. the  underlying program may render certain menu choices
  180. illegal.
  181.  
  182.     GEM provides a call to disable and re-enable menu
  183. options. The call is:
  184.  
  185.     menu_ienable(ad_menu, ENTRY, FALSE);
  186.  
  187.  to disable a selection.  The entry will be grayed out when
  188. it is drawn, and will not invert under the mouse and will not
  189. be selected by the user. Substituting TRUE for FALSE
  190. re-enables the option.  ENTRY is the name  of the object
  191. which is being affected, as assigned in the RCS.
  192.  
  193.     Note that menu_ienable() will not normally affect the
  194. appearance or operation of menu TITLE entries.  However,
  195. there is an undocumented feature which allows this.  If ENTRY
  196. is replaced by the object number of a title bar entry with
  197. its top bit set, then the entire associated drop-down will be
  198. disabled or re-enabled as requested, and the title's
  199. appearance will be changed.  But, be warned that this feature
  200. did not work reliably in some early versions of GEM.  Test it
  201. on your copy of ST GEM, and use it with caution when you
  202. cannot control the version under which your application may
  203. run.
  204.  
  205.     It is also possible to disable menu entries by directly
  206. altering the DISABLED attribute within the OB_STATE word.
  207. The routines enab_obj() and disab_obj() in the download show
  208. how this is done.  They are also used in set_menu(), which
  209. follows them immediately.
  210.  
  211.     Set_menu() is a utility which is useful when you wish to
  212. simultaneously enable or disable many entries in the menu
  213. when the program's state changes or a new object is selected
  214. by the user.   It is called with
  215.  
  216.     set_menu(ad_menu, vector);
  217.  
  218.  where vector is a pointer to an array of WORDs.  The first
  219. word of the array determines the default state of menu
  220. entries.  If it is TRUE, then set_menu() enables all entries
  221. in every drop-down of the menu tree, except that the DESK
  222. drop-down is unaffected.  If it is FALSE, then every menu
  223. entry is disabled.
  224.  
  225.     The following entries in the array are the numbers of
  226. menu entries which are to be toggled to the reverse of the
  227. default state. This list is terminated by a zero entry.
  228.  
  229.     The advantage of set_menu() is that it allows you to
  230. build a collection of menu state arrays, and associate one
  231. with each type of user-selected object, program state, and so
  232. on.  Changing the status of the menu tree may then be
  233. accomplished with a single call.
  234.  
  235.     CHECK, PLEASE?  One type of state indicator which may
  236. appear  within a drop-down is a checkmark next to an entry.
  237. You can add the  checkmark with the call:
  238.  
  239.     menu_icheck(ad_menu, ENTRY, TRUE);
  240.  
  241.  and remove it by replacing the TRUE with FALSE.  As above,
  242. ENTRY is the name of the menu entry of interest.  The
  243. checkmark appears inside the left boundary of the entry
  244. object, so leave some space for it.
  245.  
  246.     The menu_icheck() call is actually changing the state of
  247. the CHECKED flag within the entry object's OB_STATE word.  If
  248. necessary, you may alter the flag directly using do_obj() and
  249. undo_obj() from the download.
  250.  
  251.     NOW YOU SEE IT, NOW YOU DON'T.  You can also alter the
  252. text  which appears in a particular menu entry (assuming that
  253. the entry is  a G_STRING object).  The call
  254.  
  255.     menu_text(ad_menu, ENTRY, ADDR(text));
  256.  
  257.  will substitute the null-terminated string pointed to by
  258. text for whatever is currently in ENTRY.  Remember to make
  259. the drop-down wide enough to handle the largest text string
  260. which you may substitute.  In the interests of speed,
  261. G_STRINGs drawn within drop-downs are not clipped, so you may
  262. get garbage characters on the desktop if you do not size the
  263. drop-down properly!
  264.  
  265.     The menu_text() call actually alters the OB_SPEC field of
  266. the menu entry object to point to the string which you
  267. specify.  Since the menu tree is a static data structure
  268. which may be directly accessed by the AES at any time, be
  269. sure that the string is also statically allocated and that it
  270. is not modified without first being delinked from the menu
  271. tree. Failure to do this may result in random crashes when
  272. the user accesses the drop-down!
  273.  
  274.     LUNCH AND DINNER MENUS.  Some applications may have such
  275. a wide  range of operations that they need more than one menu
  276. bar at different  times.  There is no problem with having
  277. more than one menu tree in a  resource, but the AES can only
  278. keep track of one at a time.  Therefore,  to switch menus you
  279. need to use menu_bar(ad_menu1, FALSE); to release  the first
  280. menu, then use menu_bar(ad_menu2, TRUE); to load the second
  281. menu tree.
  282.  
  283.     Changing the entire menu is a drastic action.  Out of
  284. consideration for your user, it should be associated with
  285. some equally obvious change in the application which has just
  286. been manually requested.  An example  might be changing from
  287. spreadsheet to data graphing mode in a multi-function
  288. program.
  289.  
  290.     DO IT YOURSELF.  In a future column, I will discuss how
  291. to set up user-defined drawing objects.  If you have already
  292. discovered them  on your own, you can use them within a
  293. drop-down or as a title entry.
  294.  
  295.     If the user-defined object is within a drop-down, its
  296. associated drawing code will be called once when the
  297. drop-down is first drawn. It will then be called in
  298. "state-change" mode when the entry is highlighted (inverted).
  299. This allows you to use non-standard methods to show
  300. selection, such as outlines.
  301.  
  302.     If you try to insert a user-defined object within the
  303. menu title area, remember that the G_TITLE object which you
  304. are replacing includes part of the dark margin of the bar.
  305. You will need to experiment with your object drawing code to
  306. replicate this effect.
  307.  
  308.     MAKE PRETTY.  There are a number of menu formatting
  309. conventions which have become standard practice.  Using these
  310. gives your application a recognizable "look-and-feel" and
  311. helps users learn it.  The following section reviews these
  312. conventions, and supplies a few hints and tricks to obtain a
  313. better appearance for you menus.
  314.  
  315.     The second drop-down is customarily used as the FILE
  316. menu.  It  contains options related to loading and saving the
  317. files used by the  application, as well as entries for
  318. clearing the workspace and  terminating the program.
  319.  
  320.     You should avoid crowding the menu bar.  Leave a couple
  321. of spaces between each entry, and try not to use more than
  322. 70% of the bar.  Not only does this look better, but you will
  323. have space for longer words if you translate your application
  324. to a foreign language.
  325.  
  326.     Similarly, avoid cluttering menu drop-downs.  Try to keep
  327. the number of options to no more than ten unless they are
  328. clearly related, such as colors.  Separate off dissimilar
  329. entries with the standard disabled dashes line.  (If you are
  330. using set_menu(), remember to consider the separators when
  331. setting up the state vectors.)
  332.  
  333.     If the number of options grows beyond this bound, it may
  334. be time to move them to a dialog box.  If so, it is a
  335. convention to put three dots following each menu entry which
  336. leads to a dialog. Also, allow a margin on the menu entries.
  337. Two leading blanks and a minimum of one trailing blank is
  338. standard, and allows room for checkmarks if they are used.
  339.  
  340.     Dangerous menu options should be far away from common
  341. used entries, and are best separated with dashed lines.  Such
  342. options should either lead to a confirming go/no-go alert, or
  343. should have associated "undo" options.
  344.  
  345.     After you have finished defining a menu drop-down with
  346. the RCS,  be sure that its entries cover the entire box.
  347. Then use ctrl-click to  select the drop-down itself, and SORT
  348. the entries top to bottom.  This  way the drop-down draws in
  349. smoothly top to bottom.
  350.  
  351.     Finally, it is possible to put entries other than
  352. G_STRINGs into drop-downs.  In the RCS, you will need to
  353. import them via the clipboard from the Dialog mode.
  354.  
  355.     Some non-string object, such as icons and images, will
  356. look odd  when they are inverted under the mouse.  There is a
  357. standard trick for  dealing with this problem.  Insert the
  358. icon or whatever in the drop-down first.  Then get a G_IBOX
  359. object and position and size it so that it covers the first
  360. object as well as the extra area you would like to be
  361. inverted.
  362.  
  363.     Edit the G_IBOX to remove its border, and assign the
  364. entry name  to it.  Since the menu manager uses objc_find(),
  365. it will detect and invert this second object when the mouse
  366. moves into the drop-down.  (To see why, refer to article #5.)
  367. Finally, DO NOT SORT a drop-down which has been set  up this
  368. way!
  369.  
  370.     THAT'S IT FOR NOW!  The next column will discuss some of
  371. the principles of designing GEM interfaces for applications.
  372. This topic is irreverantly known as GEM mythology or
  373. interface religion.  The subject for the following column is
  374. undecided.  I am considering mouse and keyboard messages, VDI
  375. drawing primitives, and the file selector as topics.  Let me
  376. know your preferences in the Feedback!
  377.