home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / program / progem / gem13.asc < prev    next >
Text File  |  1987-10-18  |  19KB  |  350 lines

  1. From uwmcsd1!bbn!oberon!bloom-beacon!mit-eddie!rutgers!mcnc!ece-csc!ncrcae!ncr-sd!hp-sdd!hplabs!ucbvax!ucdavis!uop!exodus Sun Oct 18 08:32:46 CDT 1987
  2. Article 1990 of comp.sys.atari.st:
  3. Path: lakesys!uwmcsd1!bbn!oberon!bloom-beacon!mit-eddie!rutgers!mcnc!ece-csc!ncrcae!ncr-sd!hp-sdd!hplabs!ucbvax!ucdavis!uop!exodus
  4. >From: exodus@uop.UUCP (Freddy Kreuger)
  5. Newsgroups: comp.sys.atari.st
  6. Subject: Professional GEM column #13
  7. Keywords: Tim Oren , Progem
  8. Message-ID: <616@uop.UUCP>
  9. Date: 16 Oct 87 23:51:28 GMT
  10. Organization: Somewhere perpendicular to reality...
  11. Lines: 335
  12.  
  13.  
  14.                         *PROFESSIONAL GEM*
  15.                            By Tim Oren
  16.                 Column #13 -   A New Form Manager
  17.  
  18.      This  is  the 13th installment of ST PRO GEM,  and the  first
  19. devoted to explaining a large piece of code.  This article is also
  20. the  second  in a series of three concerning  GEM  user  interface
  21. techniques.   The  code is an alternate form (dialog) manager  for
  22. GEM.   It  is stored as GMCL13.C in DL3 of PCS-58.   You should go
  23. and  download it now,  or you will have no hope of following  this
  24. discussion.
  25.  
  26.      What  is  unique  about this version  of  the  form  manager?
  27. First,  it  implements  all of the functions of the  standard  GEM
  28. form_do  routine,  as  well as adding a "hot spots" feature  which
  29. causes  selectable  objects to become mouse-sensitive,  just  like
  30. the   entries  in  menu  dropdowns.    The  second  (and  obvious)
  31. difference  is that this form manager is provided in  source  code
  32. form.   This  gives you the freedom to examine it and change it to
  33. suit your own needs.
  34.  
  35.      I  have  several  purposes in presenting this  code.   It  is
  36. intended   as  an  example  of  GEM  program  structure,   and  an
  37. application  of  some  of  the  techniques  presented  in  earlier
  38. columns.   It is also relevant to the continuing thread discussing
  39. the necessity of feedback when constructing a user interface.
  40.  
  41.      Also,  this  issue represents the beginning of a  fundamental
  42. change in direction for ST PRO GEM.   Since this column began last
  43. August, Atari ST developers have increased not only in number, but
  44. in sophistication.   A number of books,  as well as back issues of
  45. ST PRO GEM,  are now available to explain the basics of the ST and
  46. GEM.   Quick  answers  to common questions are available  here  on
  47. Compuserve's PCS-57 from Atari itself,  or from helpful members of
  48. the developer community.
  49.  
  50.      To  reflect these changes,  future columns will discuss  more
  51. advanced topics in greater depth, with an accent on code which can
  52. be  adapted  by developers.   The program presented in this  issue
  53. will be a basis for a number of these discussions.   There will be
  54. fewer  "encyclopediac" treatments of AES and VDI  function  calls.
  55. Finally, to give me the time required to create this code or clean
  56. up tools from my "bag of tricks", ST PRO GEM will probably convert
  57. to a monthly format around the start of summer.
  58.  
  59.      ON  WITH  THE SHOW.   Taking your listing in hand,  you  will
  60. quickly notice two things.   First, this program uses the infamous
  61. portability macros,  so that it may be used with Intel versions of
  62. GEM.  Second, the routines are arranged "bottom up", with the main
  63. at the end,  and subroutines going toward the beginning.  (This is
  64. a carry-over from my days with ALGOL and PASCAL.)  You should  now
  65. turn to the form_do entry point near the end of the code.
  66.  
  67.      One change has been made in the standard calling sequence for
  68. form_do.   The  starting  edit field is now a pointer to a  value,
  69. rather  than  the value itself.   The new form_do  overwrites  the
  70. initial value with the number of the object being edited when  the
  71. dialog  terminated.   Using  this  information,  your program  can
  72. restore the situation when the dialog is next called.   As before,
  73. if there is NO editable field, the initial value should be zero.
  74.  
  75.      There are several local variables which maintain vital  state
  76. information during the dialog interaction.  Edit_obj is the number
  77. of  the editable object currently receiving keystrokes.   Next_obj
  78. is  set when the mouse is clicked over an object.   If the  object
  79. happens to be editable,  next_obj becomes the new edit_obj.
  80.  
  81.      Three  variables are associated with the "hot-spot"  feature.
  82. If hot_mode is set to M1_ENTER, then the mouse is outside the area
  83. of the dialog.   If it equals M1_EXIT, then the mouse is currently
  84. in the dialog.   If it is in the dialog, hot_obj indicates whether
  85. there is an active "hot" object.   If its value is NIL (-1),  then
  86. there is no active object.   Otherwise,  it is equal to the number
  87. of the object which is currently "hot",  that is,  inverted on the
  88. screen.   Finally, hot_rect is the current wait rectangle.  If the
  89. mouse is outside of the window, then the wait rectangle equals the
  90. dialog's  ROOT.   If there is a current hot object,  then hot_rect
  91. equals  that  object's screen rectangle.   If the mouse is in  the
  92. dialog,  but  not  within  a hot object,  then the wait  rectangle
  93. defines  the  area within which no further  collision  checks  are
  94. necessary.   This  is  arrived at through an  algorithm  explained
  95. below.
  96.  
  97.      Form_do's initialization code sets up the hot-spot  variables
  98. to  trigger  if  the mouse is within the  dialog.   It  also  sets
  99. starting  values  for edit_obj and next_obj which will  cause  the
  100. edit startup code to be activated.
  101.  
  102.      The main portion of form_do is a loop, exhibiting the type of
  103. event  driven  structure  discussed in the  last  column.   Before
  104. entering the evnt_multi wait,  the status of next_obj and edit_obj
  105. are  checked  to  see if a new object should  be  initialized  for
  106. editing.   If  so,  objc_edit  is called with the EDINIT  function
  107. code.   NOTE:  the objc_edit calling sequence used in this program
  108. differs from the one given in the AES manual,  which is incorrect!
  109. You  should check the bindings you are using to be sure they  will
  110. work with this code, and modify as necessary.
  111.  
  112.      The evnt_multi is set up to wait for a mouse click (single or
  113. double),  for  a  keyboard  input,  or  for the mouse  to  make  a
  114. "significant"  movement,  as  discussed above.   Notice that since
  115. form_do is used as a subroutine, it does not handle messages which
  116. are  normally  processed  by the main loop  of  your  application.
  117. Notice that this creates a mode,  and that this routine as written
  118. handles modal dialogs.   You could,  however, use this code as the
  119. basis for a non-modal dialog handler by drawing the dialog  within
  120. a  window,  and  combining the main loop of form_do with the  main
  121. loop  of your application.   (This possibility may be examined  in
  122. future  columns.   In the meantime,  it is left as an exercise for
  123. the reader.)
  124.  
  125.      The  event  bit vector is returned to the  variable  "which".
  126. Since events are not mutually exclusive,  each possible event type
  127. must be examined in turn before returning to the evnt_multi  call.
  128. The form manager's event handling routines are form_hot, for mouse
  129. rectangle event, form_keybd, for character input, and form_button,
  130. for  mouse  clicks.   Form_keybd  and form_button are  allowed  to
  131. terminate  the  dialog by returning a value of false to  the  loop
  132. control variable "cont".   If termination is imminent, or the user
  133. has  clicked  on a new editable object,  objc_edit is called  with
  134. EDEND  to remove the cursor from the old object.   The normal flow
  135. of control then returns to edit setup and evnt_multi.
  136.  
  137.      A few cleanup actions are performed upon termination.  If the
  138. terminating  object  (stored in next_obj) is not the same  as  the
  139. hot_obj, then a race condition has occured and the hot object must
  140. be cleared with objc_toggle before exiting.   After this test, the
  141. final  edit_obj  value is passed back via the parameter,  and  the
  142. terminating object is returned as the function value.
  143.  
  144.      RELAXEN   UND  WATCHEN  DAS  BLINKENLICHTEN.    Form_hot   is
  145. responsible  for  maintaining on-screen hot-spots,  and  correctly
  146. updating  the  internal hot-spot variables.   It is about  halfway
  147. through the listing.
  148.  
  149.      The first action in form_hot is to determine if the mouse has
  150. just exited an object which is "hot.  In this case, objc_toggle is
  151. called to unhighlight the object and reset the SELECTED flag.
  152.  
  153.      The current mouse position is passed to form_hot by  form_do.
  154. It  is checked against the root rectangle of the dialog to see  if
  155. the mouse is inside the dialog.  If not, the program must wait for
  156. it  to re-enter,  so form_hot sets the rectangle and waiting  mode
  157. accordingly, and returns NIL as the new hot_obj.
  158.  
  159.      When the mouse is within the dialog, a regular objc_find call
  160. determines  the object at which it is pointing.   For an object to
  161. be  mouse-sensitive,  it must be SELECTABLE and not DISABLED.   If
  162. the  found object meets these tests,  the mouse will "hover"  over
  163. the  object,  waiting  to leave its screen rectangle.   Since  the
  164. object might already be SELECTED (and hence drawn reversed),  this
  165. is checked before objc_toggle is called to do the highlighting and
  166. selection  of  the  object,  which becomes the hot_obj.   (If  the
  167. object was already SELECTED, the hot_obj becomes NIL.)
  168.  
  169.      The  toughest  condition  is when the  mouse  is  within  the
  170. dialog,  but  not over a mouse-sensitive object.   The regular GEM
  171. event  structure  will not work,  because it can only wait on  two
  172. rectangles,  and  there  may be many more selectable objects in  a
  173. dialog  tree.   I have found a way around this limitation using  a
  174. combination of the map_tree utility (introduced in ST PRO GEM  #5)
  175. with the principle of visual hierarchy in object trees.
  176.  
  177.      In  summary,  the  algorithm  attempts to  find  the  largest
  178. bounding rectangle around the current mouse position, within which
  179. there are no mouse-sensitive objects.   It starts with a rectangle
  180. equal  to the dialog root,  and successively "breaks" it with  the
  181. rectangle of each mouse-sensitive object.  The next few paragraphs
  182. examine this method in detail.
  183.  
  184.      Since  C  lacks  the  dynamic scoping  of  LISP,  from  which
  185. map_tree was derived, it is necessary to set up some globals to be
  186. used during the rectangle break process.   Br_rect is the GRECT of
  187. the current bounding rectangle.   Br_mx and br_my hold the current
  188. mouse position.   Br_togl is a switch which determines whether the
  189. next  break will be attempted horizontally or  vertically.   After
  190. initializing these variables, form_hot uses map_tree to invoke the
  191. break_obj routine for every object in the dialog.
  192.  
  193.      Break_obj  first intersects the rectangle of the object  with
  194. the  current  bounding  rectangle.   If they  are  disjoint,  then
  195. neither  the object nor any of its offspring can  possible  affect
  196. the  operation,  so FALSE is returned,  causing map_tree to ignore
  197. the subtree.
  198.  
  199.      The  object is next checked to see if it is  mouse-sensitive.
  200. As before, it must be SELECTABLE and not DISABLED, and it must not
  201. be  hidden (this was checked automatically by  objc_find  before).
  202. If  these  conditions are met,  then the object intrudes into  the
  203. current  bounding rectangle.   To maintain the desired  condition,
  204. part of the rectangle must be removed or "broken away".
  205.  
  206.      In  many  cases,  the  break  operation can  be  done  either
  207. horizontally or vertically.  Since we have no prior information as
  208. to which way the mouse will move next,  break_obj uses the br_togl
  209. flag to alternate which direction it will try first.   This should
  210. yield the most nearly square rectangle.
  211.  
  212.      The  break_x and break_y routines are very similar.   In each
  213. case,  the  segment occupied by the breaking object is compared to
  214. the  point  occupied  by the mouse.   If the point is  within  the
  215. segment,  there is no possible break in this dimension,  and FALSE
  216. is  returned.   If  the point lies outside the segment,  then  the
  217. rectangle  may be successfully broken by reducing this  dimension.
  218. This is done, and TRUE is returned to report success.
  219.  
  220.      The break_y routine also employs a look-ahead test to prevent
  221. a possible infinite loop.   It is conceivable,  though not likely,
  222. that someone might nest a non-SELECTABLE object completely  within
  223. another  SELECTABLE object(s).   If the mouse point is within such
  224. an  object,  the  algorithm  will not be able to  select  a  break
  225. dimension.   In the current version, the mouse rectangle is simply
  226. forced  to  a single pixel for this case.   (Note that is  is  NOT
  227. sufficent to simply wait on the non-selectable object's rectangle,
  228. since  other  SELECTABLE objects may overlap it and follow  it  in
  229. tree order.)
  230.  
  231.      Since map_tree examines all possible objects, br_rect will be
  232. the  correct bounding rectangle at completion.   Note that you can
  233. readily  adapt this technique to other uses,  such as hot-spotting
  234. while  dragging  objects.   It  is  much  less  demanding  of  CPU
  235. resources than other methods, such as repetitive objc_finds.
  236.  
  237.      WHAT A CHARACTER!  The form_keybd routine acts as a filter on
  238. character  input.   When  it  recognizes a control  character,  it
  239. processes it and zeroes the keyboard word.  Other chararacters are
  240. passed on to objc_edit to be inserted in edit_obj.  If there is no
  241. editing object, the character goes to the bit bucket.
  242.  
  243.      The   form_keybd   given   implements   the   standard    GEM
  244. functionality with two minor additions.   First, a carriage return
  245. in  a dialog with no DEFAULT exit object is taken as a tab.   This
  246. allows  <CR> to be used "naturally" in dialogs with several  lines
  247. of text input.   Second,  tabs and backtabs "wrap around" from top
  248. to  bottom  of  the dialog,  and are done by "walking  the  tree",
  249. rather  than relying on the LASTOB flag to signal the end  of  the
  250. dialog.   This  allows the new form manager to handle dialog trees
  251. which are not contiguous in memory.
  252.  
  253.      The  code sets up several global variables for use by  mapped
  254. functions.   Fn_obj is the output from both find_tab and find_def.
  255. Fn_dir is an input to find_tab.  Fn_last, fn_prev, and fn_last are
  256. used while searching for tab characters.
  257.  
  258.      A  carriage  return results in a search of the  entire  tree,
  259. using  map_tree and find_def,  for an object with its DEFAULT flag
  260. set.  Its SELECTED flag is set and it is inverted on the screen to
  261. indicate  the action taken.   Form_keybd returns a FALSE to  force
  262. termination  of  the main form_do loop.   If no DEFAULT is  found,
  263. control passes to the tab code.
  264.  
  265.      The  tabbing  procedure is somewhat complicated  because  the
  266. same code is used for forward and backward tabbing.  The old value
  267. of edit_obj (the object being tabbed FROM) is placed into fn_last.
  268. Fn_dir  is set to one for a forward tab,  and zero for a  backward
  269. tab.
  270.  
  271.      The general strategy is to scan the entire tree for  EDITABLE
  272. objects,  always  saving  the  last one found  in  fn_prev.   When
  273. tabbing  forward  fn_last  is checked against  fn_prev.   A  match
  274. indicates  that  the  current object is  the  one  desired.   When
  275. tabbing  backward the current object is checked  against  fn_last.
  276. If  they  match,  fn_prev is the desired object.   This  procedure
  277. requires two passes when the tab "wraps around" the tree, that is,
  278. when  the  desired object as at the opposite end of  the  traverse
  279. from the old editing object.
  280.  
  281.      The  result  of  the  tab  operation  is  written  back  into
  282. form_do's  next_obj parameter,  and becomes the new editing object
  283. at the beginning of the next loop.
  284.  
  285.      BUTTON DOWN.  The form_button procedure is lengthy because it
  286. must  recognize  and  handle  mouse clicks  on  several  types  of
  287. objects:  EDITABLE,  SELECTABLE, and TOUCHEXIT.  The first section
  288. of code rejects other objects, which cannot accept a click.
  289.  
  290.      The  next  piece of form_button makes a special check  for  a
  291. double click on a TOUCHEXIT object.   This will cause the high bit
  292. of the returned object number to be set.   (By the way,  this also
  293. occurs  in  the standard form_do.)  This flag allows  user  dialog
  294. code to perform special processing on the object.
  295.  
  296.      The largest piece of form_button handles the various cases in
  297. which the SELECTABLE flag may be set.   Setting the RBUTTON (radio
  298. button) flag causes all of the object's siblings in the tree to be
  299. deselected  at  the  time the object  is  clicked.   The  do_radio
  300. routine uses the get_parent utility to find the ancestor, and then
  301. performs the deselect/select operation.
  302.  
  303.      If the SELECTABLE object is not TOUCHEXIT, then graf_watchbox
  304. is used to make sure that the mouse button comes back up while  it
  305. is within the object.  Otherwise, the click is cancelled.  Care is
  306. necessary  here,  since the hot-spot code may have already set the
  307. SELECTED flag for the object.   (We cannot be sure of this,  for a
  308. race condition may have occurred!)
  309.  
  310.      If  the SELECTABLE object is TOUCHEXIT,  then the application
  311. has requested that form_do exit without waiting for the button  to
  312. go back up.   In both this and regular form_do,  TOUCHEXIT objects
  313. are  used when you want to provide immediate response  (animation)
  314. within the context of a dialog.
  315.  
  316.      The  final parts of form_button do cleanup.   If the  clicked
  317. object  was  already hot-spotted,  hot_obj must be reset  to  NIL,
  318. otherwise  form_do  will carefully unselect the object  which  has
  319. just been selected!
  320.  
  321.      If  the  EXIT or TOUCHEXIT flags are  in  force,  form_button
  322. returns  FALSE to force the completion of form_do.   For  EDITABLE
  323. objects,  next_obj  is left intact to replace edit_obj during  the
  324. next  loop.   Otherwise,  next_obj has done its job and is zeroed,
  325. and form_button returns TRUE for continuation.
  326.  
  327.      This  concludes the tour of the alternate form_do.   The best
  328. cure for any confusion in this explanation is to compile the  code
  329. into  an  application  and  watch  how  it  runs  with   different
  330. resources,  or attack it with a debugger.
  331.  
  332.      OPERATORS  ARE STANDING BY.   I encourage you to modify  this
  333. code  to meet your particular needs and incorporate it  into  your
  334. application.   I  would  like to request than anyone who comes  up
  335. with  significant improvements (or bug fixes) send them to  me  so
  336. they  can  be made generally available.   You can do this via  the
  337. ANTIC ONLINE Feedback, or by sending E-mail to 76703,202.
  338.  
  339.      Speaking  of  Feedback,  I  would also like comments  on  the
  340. proposed change of direction for the column,  and more suggestions
  341. for  future  topics.   The  next  installment will  be  a  further
  342. discussion  of  interface design.   Topics now queued  for  future
  343. articles  include the file selector and DOS error handling,  a new
  344. object  editor,  and customized drag box and rubber box  routines.
  345. Discussions  on  VDI workstations and printer output are  on  hold
  346. pending  release of the GDOS by Atari.   If there are items  which
  347. you want to appear here, you must let me know!
  348.  
  349.  
  350.