home *** CD-ROM | disk | FTP | other *** search
/ Falcon 030 Power 2 / F030_POWER2.iso / ST_STE / MAGS / ICTARI04.ARJ / ictari.04 / C / GEM_TUT / GEM.003 next >
Text File  |  1993-05-12  |  17KB  |  352 lines

  1.                        **Professional GEM**
  2.                            by Tim Oren
  3.  
  4.                         THE DIALOG HANDLER
  5.                              11/7/85
  6.  
  7.  
  8.                        A MEANINGFUL DIALOG
  9.  
  10.    This issue of ST PRO GEM begins an exploration of ST GEM's dialog
  11. handler.  I will discuss basic system calls for presenting the
  12. dialog, and then continue with techniques for initializing and
  13. reading on/off button and "radio" button objects.  We  will also take
  14. some short side-trips into the operation of the GEM Resource
  15. Construction Set to assist you in building these dialogs.
  16.  
  17.    There are a number of short C routines which accompany this column.
  18. These are stored as file GEMCL3.XMO in DL 5 on SIG*ATARI. Before
  19. reading this column, you should visit SIG*ATARI (go pcs-132) and
  20. download this file.
  21.  
  22.                           DEFINING TERMS
  23.  
  24.    A dialog box is an "interactive form" in which the user may enter
  25. text and indicate selections by pointing with the mouse. Dialogs in
  26. GEM are "modal", that is, when a dialog is activated other screen
  27. functions such as menus and  window controls are suspended until the
  28. dialog is completed.
  29.  
  30.    In most cases, the visual structure of a GEM dialog is specified
  31. within your application's resource file.  The GEM Resource
  32. Construction Set (RCS) is used to build a picture of the dialog.
  33.  
  34.    When the RCS writes out a resource, it converts that picture into a
  35. tree of GEM drawing objects and stores this data structure within the
  36. resource.  Before your application can  display the dialog, it must
  37. load this resource file and find the  address of the tree which
  38. defines the dialog.
  39.  
  40.    To load a resource, the AES checks its size and allocates memory for
  41. the load.  It then reads in the resource, adjusting internal pointers
  42. to reflect the load address.  Finally, the object sizes stored in the
  43. resource are converted from characters to pixels using the system
  44. font size.
  45.  
  46.    (A note for those with Macintosh experience:  Although Mac and GEM
  47. resources share a name, there are fundamental differences which can
  48. be misleading.  A Mac resource is a fork within a file; a GEM
  49. resource is a TOS file by itself.  Mac resources may be paged in and
  50. out of memory; GEM resources are monolithic.  GEM resources are
  51. internally tree structured; Mac resources are not.  Finally, Mac
  52. resources include font information, while ST GEM does this with font
  53. loading at the VDI level.)
  54.  
  55.    The resource load is done with the GEM AES call:
  56.  
  57.    ok = rsrc_load(ADDR("MYAPP.RSC"));
  58.  
  59.    "MYAPP" should be replaced with the name of your program. Resources
  60. conventionally have the same primary name as their application, with
  61. the RSC extent name instead of PRG.  The ok flag returned by
  62. rsrc_load will be FALSE is anything went wrong during the load.
  63.  
  64.    The most common causes of failure are the resource not being in the
  65. application's subdirectory, or lack of sufficient memory for GEM to
  66. allocate space for the resource.  If this happens, you must terminate
  67. the program immediately.
  68.  
  69.    Once you have loaded the resource, you find the address of a dialog's
  70. object tree with:
  71.  
  72.    rsrc_gaddr(R_TREE, MYDIALOG, &tree);
  73.  
  74. Tree is a 32-bit variable which will receive the address of the root
  75. node of the tree.
  76.  
  77.    The mnemonic MYDIALOG should be replaced with the name you gave your
  78. dialog when defining it in the RCS.  At the same time that it writes
  79. the resource, RCS generates a corresponding .H file containing tree
  80. and object names.  In order to use these mnemonics within your
  81. program, you must include the name file in your compile:  #include
  82. "MYAPP.H"
  83.  
  84.                             BUG ALERT!
  85.  
  86.    When using the DRI/Alcyon C compiler, .H files must be in the
  87. compiler's home directory or they will not be found.  This is
  88. especially annoying using a two floppy drive ST development system.
  89. The only way around this is to explicitly  reference an alternate
  90. disk in the #include, for instance:  "B:MYAPP.H"
  91.  
  92.    Now that the address of the dialog tree has been found, you are ready
  93. to display it.  The standard (and minimal) sequence for doing so is
  94. given in routine hndl_dial() in the download.  We will now walk
  95. through each step in this procedure.
  96.  
  97.    The form_center call establishes the location of the dialog on the
  98. screen.  Dialog trees generated by the RCS have an undefined origin
  99. (upper-left corner).
  100.  
  101.    Form_center computes the upper-left location necessary to center the
  102. dialog on the screen, and inserts it into the OB_X and OB_Y fields of
  103. the ROOT object of the tree.  It also computes the screen rectangle
  104. which the dialog will occupy on screen and writes its pixel
  105. coordinates into variables xdial, ydial, wdial, and hdial.
  106.  
  107.    There is one peculiarity of form_center which occasionally causes
  108. trouble.  Normally the rectangle returned in xdial, etc., is exactly
  109. the same size as the basic dialog box.
  110.  
  111.    However, when the OUTLINED enhancement has been specified for the
  112. box, form_center adds a three pixel margin to the rectangle returned.
  113. This causes the screen area under the outline to be correctly redrawn
  114. later (see below).  Note that OUTLINED is part of the standard dialog
  115. box in the RCS.  Other enhancements, such as SHADOWED or "outside"
  116. borders are NOT handled in this fashion, and you must compensate  for
  117. them in your code.
  118.  
  119.    The next part of the sequence is a form_dial call with a zero
  120. parameter.  This reserves the screen for the dialog action about to
  121. occur. Note that the C binding given for form_dial in the DRI
  122. documents is in error: there are nine parameters, not five.  The
  123. first set of xywh arguments is actually used with form_dial calls 1
  124. and 2 only, but place holders must be supplied in all cases.
  125.  
  126.    The succeeding form_dial call (parameter one) animates a "zoom box"
  127. on the screen which moves and grows from the first screen rectangle
  128. given to the second rectangle, where the dialog will be displayed.
  129.  
  130.    The use of this call is entirely optional.  In choosing whether to
  131. use it or not, you should consider whether the origin of the "zoom"
  132. is relevant to the  operation.  For instance, a zoom from the menu
  133. bar is relatively meaningless, while a zoom from an object about to
  134. be edited in the dialog provides visual feedback to the user, showing
  135. whether the correct object was chosen.
  136.  
  137.    If the origin is not relevant, then the zoom is just a time-waster.
  138. If  you decide to include these effects, consider a "preferences"
  139. option in  your app which will allow the experienced and jaded user
  140. to turn them off in the interests of speed.
  141.  
  142.    The objc_draw call actually displays the dialog on the screen. Note
  143. that the address of the tree, the beginning drawing object, and the
  144. drawing depth are passed as arguments, as well as the rectangle
  145. allotted for the dialog.
  146.  
  147.    In general, dialogs (and parts of dialogs) are  ALWAYS drawn
  148. beginning at the ROOT (object zero).  When you want to draw  only a
  149. portion of the dialog, adjust the clipping rectangle, but not the
  150. object number.  This ensures that the background of the dialog is
  151. always  drawn correctly.
  152.  
  153.    The objc_xywh() utility in the download can be used to find the
  154. clipping rectangle for any object within a dialog, though you may
  155. have to allow an extra margin is you have used shadows, outlines, or
  156. outside borders with the object.
  157.  
  158.    Calling form_do transfers control to the AES, which animates the
  159. dialog for user interaction.  The address of the dialog tree is
  160. passed as a parameter.  The second paramter is the number of the
  161. editable object at which the text cursor will first be positioned.
  162. If you have no text fields, pass a zero.  Note that again the DRI
  163. documents are in error: passing a -1 default may crash the system.
  164. Also be careful that the default which you specify is actually a text
  165. field; no error checking is performed.
  166.  
  167.    The form_do call returns the number of the object on which the
  168. clicked to terminate the dialog.  Usually this is a button type
  169. object with the EXIT and SELECTABLE attributes set.  Setting the
  170. DEFAULT attribute as well will cause an exit on that object is a
  171. carriage return is struck while in the dialog.
  172.  
  173.    If the top bit of the return is set, it indicates that  the exit
  174. object had the TOUCHEXIT attribute and was selected with a
  175. double-click.  Since very few dialogs use this combination, the
  176. sample code simply masks off the top bit.
  177.  
  178.    The next form_dial call reverses the "zoom box", moving it from the
  179. dialog's location back to the given x,y,w,h.  The same cautions apply
  180. here as above.
  181.  
  182.    The final form_dial call tells GEM that the dialog is complete, and
  183. that the screen area occupied by the dialog is now considered "dirty"
  184. and needs to be redrawn.  Using the methods described in our last
  185. column, GEM then sends redraws to all windows which were overlaid,
  186. and does any necessary redrawing of the menu or desktop itself.
  187.  
  188.    There is one notable "feature" of form_dial(3):  It always redraws
  189. an area which is two pixels wider and higher than your request!  This
  190. was probably included to make sure that drop-shadows were cleaned up,
  191. and is usually innocuous.
  192.  
  193.                           A HANDY TRICK
  194.  
  195.    Use of the form_dial(3) call is not limited to dialogs.  You can
  196. use it to force the system to redraw any part of the screen.  The
  197. advantage of this method is that the redraw area need not lie
  198. entirely within a window, as was necessary with the send_redraw
  199. method detailed in the last column.  A disadvantage is that this
  200. method is  somewhat slower, since the AES has to decide who gets the
  201. redraws.
  202.  
  203.                              CLEAN UP
  204.  
  205.    As a last step, you need to clear the SELECTED flag in the object
  206. which was clicked.  If you do not do this, the object will  be drawn
  207. inverted the next time you call the dialog.  You could clear the flag
  208. with the GEM objc_change call, but it is inefficient since you do
  209. not need to redraw the object.
  210.  
  211.    Instead, use the desel_obj() code in the  download, which modifies
  212. the object's OB_STATE field directly.  Assuming  that ret_obj
  213. contains the exit object returned by hndl_dial, the call:
  214.  
  215.    desel_obj(tree, ret_obj);
  216.  
  217. will do the trick.
  218.  
  219.                               RECAP
  220.  
  221.    The basic dialog handling method I have described contains three
  222. steps: initialization (rsrc_gaddr), dialog presentation (hndl_dial),
  223. and cleanup (desel_obj).
  224.  
  225.    As we build more advanced dialogs, these same basic steps will be
  226. performed, but they will grow more complex. The initialization will
  227. include setting up proper object text and states, and the cleanup
  228. phase will also interrogate the final states of objects to find out
  229. what the user did.
  230.  
  231.                           BUTTON, BUTTON
  232.  
  233.    The simple dialogs described above contain only exit buttons as
  234. active objects.  As such, they are little more than glorified alert
  235. boxes.
  236.  
  237.    We will now increase the complexity a little by considering non-exit
  238. buttons.  These are constructed by setting the SELECTABLE attribute
  239. on a button object.  At run-time, such an object will toggle its
  240. state between selected (highlighted) and non-selected  whenever the
  241. user clicks on it.  (You can set the SELECTABLE attribute  of other
  242. types of objects and use them instead of actual buttons, but  be sure
  243. that the user will be able to figure out what you intend!)
  244.  
  245.    Having non-exit buttons forces us to consider the problem of
  246. initializing them before the dialog, and interrogating and resetting
  247. them afterward.
  248.  
  249.    Since a button is a toggle, it is usually associated with a flag
  250. variable in the program.  As part of the initialization, you should
  251. test the flag variable, and if true call:
  252.  
  253.    sel_obj(tree, BTNOBJ);
  254.  
  255. which will cause the button to appear highlighted when the dialog is
  256. first drawn.  Sel_obj() is in the download.  BTNOBJ is replaced with
  257. the  name you gave your button when you defined it in the RCS.  Since
  258. the button starts out deselected, you don't have to do anything if
  259. your flag variable is false.
  260.  
  261.    After the dialog has completed, you need to check the object's state.
  262. The selectp() utility does so by masking the OB_STATE field.  You can
  263. simply assign the result of this test to your flag variable, but be
  264. sure that the dialog was exited with an OK button, not with a CANCEL!
  265. Again, remember to clean up the button with desel_obj(). (It's often
  266. easiest to deselect all buttons just before you leave the dialog
  267. routine, regardless of the final dialog state.)
  268.  
  269.                       WHO'S GOT THE BUTTON?
  270.  
  271.    Another common use of buttons in a  dialog is to select one of a set
  272. of possible options.  In GEM, such objects are called radio buttons.
  273. This term recalls automobile radio tuners where pushing in one button
  274. pops out any others.  In like fashion, selecting any one of a set of
  275. radio buttons automatically deselects all of the others.
  276.  
  277.    To use the radio button feature, you must do some careful work with
  278. the Resource Construction Set.
  279.  
  280.    First, each member of a set of  radio buttons must be children of the
  281. same parent object within the  object tree. To create this structure,
  282. put a hollow box type object in the  dialog, make it big enough to
  283. hold all of the buttons, and then put the buttons into the box one at
  284. a time.
  285.  
  286.    By nesting the buttons within the  box object, you force them to be
  287. its children.  Each of the buttons must have both the SELECTABLE and
  288. RADIO BUTTON attributes set.  When you are done, you may make the
  289. containing box invisible by setting its border to zero, but do not
  290. FLATTEN it!
  291.  
  292.    Since each radio button represents a different option, you must
  293. usually assign a name to each object.  When initializing the dialog,
  294. you must check which option is currently set, and turn on the
  295. corresponding button only.  A chain of if-then-else structures
  296. assures that only one button will be selected.
  297.  
  298.    At the conclusion of the dialog, you must check each button with
  299. selectp() and make the appropriate adjustments to internal variables.
  300. Again, an if-then-else chain is appropriate since only one button may
  301. be selected.  Either deselect the chosen button within this chain or
  302. do them all at the end.
  303.  
  304.    There is one common use of radio buttons in which you may short-cut
  305. this procedure.  If the buttons each represent one possible value of
  306. a numeric variable, for instance, a set of selector buttons
  307. representing  colors from zero to seven, then you can compute the
  308. initial object directly.
  309.  
  310.    In order for this technique to work, you must use a special
  311. capability of the RCS.  Insert the object corresponding to a zero
  312. value at the top (or left) of your array of buttons, then put the
  313. "one" button below (or right) of it, and so on.
  314.  
  315.    When the buttons are complete,  the SORT operation is used to
  316. guarantee that the top/left object is in fact the first child of the
  317. parent box with the others following in order.  Due to the details of
  318. object tree structure (to be discussed in the next column), this will
  319. guarantee that these objects are contiguous in the resource.
  320.  
  321.    If you assign a name (say BUTTON1) to the first button,  then you can
  322. initialize the correct button with the call:
  323.  
  324.    sel_obj(tree, BUTTON1 + field);
  325.  
  326. where field is the variable of interest.
  327.  
  328.    When the dialog is complete, you can scan the radio buttons to
  329. compute the new value for the underlying variable.  The encode()
  330. procedure in the download will do this.  As always, remember to
  331. deselect the buttons at the end.
  332.  
  333.    You can use offsets or multipliers if your variable's values don't
  334. start with zero or increment by one.  If the values are irregular you
  335. may be able to use a lookup table, at the cost of additional code.
  336.  
  337.                           COMING UP NEXT
  338.  
  339.    In the next column, I will discuss the internal structure of object
  340. trees.  Then we'll use that knowledge to build a piece of code which
  341. will "walk" an entire tree and apply a function to each object.
  342. We'll apply this code to do all of the button deselects  with a
  343. single call!  I'll also look at handling editable text fields and
  344. discuss some ways to alter a dialog's appearance at run-time.
  345.  
  346.                          DISPELL GREMLINS
  347.  
  348.    An editing error caused an omission in the first installment of ST
  349. PRO GEM.  The window components RTARROW and DNARROW should have been
  350. listed along with HSLIDE as the horizontal equivalents of the
  351. vertical slider components which were discussed.
  352.