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

  1.                         *PROFESSIONAL GEM*
  2.                            by Tim Oren
  3.                  Column #11 - GEM Hooks and Hacks
  4.                      An Insider's AES Tricks
  5.  
  6.      Welcome  to  the  eleventh episode of ST PRO  GEM,  which  is
  7. devoted to exploring some of the little documented,  but powerful,
  8. features  of GEM.   Like the authors of most complex systems,  the
  9. GEM  programmers left behind a set of "hooks",  powerful  features
  10. which would aid them in enhancing the system later.  I am going to
  11. lay  out  a number of these methods which have served me  well  in
  12. making  creative use of the AES.   You will find that most of them
  13. concern  the object and form libraries,  since I was most involved
  14. in those parts of GEM.  There are probably many more useful tricks
  15. waiting  to be found in other parts of GEM,  so if you happen onto
  16. one, please let me know in the Feedback!  Before you begin,be sure
  17. to  pick up the download for this column:  GMCL11.C in DL3 of PCS-
  18. 57.
  19.  
  20.                          POWERFUL OBJECTS
  21.  
  22.     The  first  four tricks all involve  augmenting  standard  AES
  23. objects.   This is a powerful technique for two  reasons.   First,
  24. you  can  take  advantage  of the  regular  AES  object  and  form
  25. libraries  to draw and handle most of your objects,  so that  your
  26. program need only process the exceptions.  Second, you can use the
  27. RCS  to  copy  the  special  objects  into  multiple  dialogs   or
  28. resources.   These  four tricks are Extended Object  Types,  User-
  29. defined Objects,  TOUCHEXIT,  and INDIRECT.  Let's look at each of
  30. them in turn.
  31.  
  32.                       EXTENDED OBJECT TYPES
  33.  
  34.     If you look at the AES Object Library documentation,  you will
  35. notice that the values for the OB_TYPE field in an object are  all
  36. 32 or less.   This means that a number of bits are unused in  this
  37. field.   In fact,  the AES completely ignores the top byte of  the
  38. OB_TYPE field.   In addition, the RCS ignores the top byte, but it
  39. also  preserves  its value when an object  is  read,  written,  or
  40. copied.
  41.  
  42.      This  gives  you one byte per object to use as you  see  fit.
  43. Since  the  processing of an object or dialog is (so far)  in  the
  44. hands of the AES,  the best uses of Extended Types are in flagging
  45. methods for initializing an object or handling its value when  the
  46. dialog  completes.
  47.  
  48.      For  example,  you  might  have  several  dialogs  containing
  49. editable numeric fields.   The Extended Type of each numeric field
  50. could be set to the index of the corresponding value in an  array.
  51. Then your application's dialog initialization code could scan  the
  52. object  tree  for  such objects,  pick up  the  appropriate  value
  53. from the array and convert it to ASCII,  storing the result in the
  54. resource's  string  area.   When the dialog was finished,  another
  55. pass could be made to reconvert the ASCII to binary and store away
  56. the results in the array.   (Note that the map_tree() utility from
  57. column #5 will scan an entire resource tree.)
  58.  
  59.      Another  application  is  to assign  uniform  codes  to  exit
  60. buttons  in  dialogs.   If you give every "OK" button an  Extended
  61. Type  of one,  and every "Cancel" button an Extended Type of  two,
  62. then  you  needn't  worry about naming every  exit  object.   Just
  63. examine  the Extended Type of the object returned by form_do,  and
  64. proceed accordingly.
  65.  
  66.      The catch, of course, is that you have to find a way to enter
  67. the  Extended  Type code in the first place.   Version 1.0 of  the
  68. RCS, as shipped with the Atari developer's kit, makes no provision
  69. for this.  So you have your choice of two methods for creating the
  70. first  object with each Extended Type code.
  71.  
  72.      First,  you can dump out a C source of a resource, insert the
  73. new type code by hand,  and regenerate the resource with STCREATE.
  74. Alternately,  you could carefully modify the binary resource using
  75. SID.   You will probably want to reread the AES object manual,  ST
  76. PRO GEM #4 and #5,  and use the C source as a guide when doing so.
  77. In both cases, you should make things easy on yourself by creating
  78. a  one  dialog resource with only a single object other  than  the
  79. root.   Version  2.0  of  the RCS will let you directly  enter  an
  80. Extended Type, but it has not yet been released for the ST by DRI.
  81.  
  82.      Once  you have created a prototype extended object by  either
  83. method, you can use the RCS to propogate it.  The safest way is to
  84. use the MERGE option to add the modified tree to the resource  you
  85. are building.  Then copy the prototype object via the clipboard to
  86. your dialog(s), deleting the extra tree when you are done.  If you
  87. are  using several different extended objects,  you can use  MERGE
  88. and clipboard copies to get them all into one tree which will then
  89. become your own object library.
  90.  
  91.      The  second way of using RCS is easier,  but more  dangerous.
  92. If you want to try the following technique,  BACK UP YOUR RCS DISK
  93. FIRST!   Put  simply,  the RCS does not care what is in its dialog
  94. partbox. It will make copies of anything that it finds there! This
  95. gives you the option of using the RCS on ITS OWN RESOURCE in order
  96. to add your customized objects to the partbox.
  97.  
  98.      To do this, open RCS.RSC from the RCS.  Since there is no DEF
  99. file,  you will get a collection of question mark icons.   Use the
  100. NAME option to make TREE5 into a DIALOG.    Open it,  and you will
  101. see the dialog partbox.
  102.  
  103.      Now you can use the MERGE technique described above to insert
  104. your  customized  objects.   Then SAVE the modified resource,  and
  105. rerun the RCS.  Your new objects should now appear in the partbox.
  106. If  you added several,  you may have to stretch the partbox to see
  107. them  all.   You can now make copies of the new objects just  like
  108. any other part.  (Note: DO NOT modify the alert or menu partboxes,
  109. you will probably crash the RCS.)
  110.  
  111.                        USER-DEFINED OBJECTS
  112.  
  113.     The one type of object which was not discussed in the  earlier
  114. articles  on  AES objects was G_USERDEF,  the  programmer  defined
  115. object.   This  is  the  hook  for  creating  objects  with  other
  116. appearances  beyond those provided by the standard  AES.   By  the
  117. way, you should note that the G_PROGDEF and APPLBLK mnemonics used
  118. in  the  AES documents are incorrect;  the actual  names  as  used
  119. defined OBDEFS.H are G_USERDEF and USERBLK.
  120.  
  121.      The  RCS does not support the creation of G_USERDEF  objects,
  122. since  it  has  no idea how they will be drawn  by  your  program.
  123. Instead,  you  must insert a dummy object into your resource where
  124. you  want  the  G_USERDEF  to appear,  and  patch  it  after  your
  125. application performs its resource load.
  126.  
  127.      You   must  replace  the  object's  existing   OB_TYPE   with
  128. G_USERDEF,  though you may still use the upper byte as an Extended
  129. Type.   You  must  also  change the OB_SPEC field to be  a  32-bit
  130. pointer  to  a USERBLK structure.   An USERBLK is simply two  LONG
  131. (32-bit)  fields.   The  first is the address of the drawing  code
  132. associated  with  the  user  defined object.   The  second  is  an
  133. arbitrary 32-bit value assigned to the object by your application.
  134.  
  135.      You can designate objects for conversion to G_USERDEFs in the
  136. normal fashion by assigning them names which are referenced one by
  137. one in your initialization code.   You can also combine two tricks
  138. by using the Extended Type field as a designator for objects to be
  139. converted to G_USERDEF.  Each tree can then be scanned for objects
  140. to  be converted.   There is a short code segment in the  download
  141. which demonstrates this technique.
  142.  
  143.      My  usual  convention  is to define new  drawing  objects  as
  144. variants  of  existing objects,  using the Extended Type field  to
  145. designate the particular variation.   Thus an Extended Type of one
  146. might designate a G_BUTTON with rounded corners,  while a value of
  147. two  could  flag  a G_STRING of boldface text.   When  using  this
  148. technique,  the  RCS can be used to build a rough facsimile of the
  149. dialog  by inserting the basic object type as  placeholders.   The
  150. existing  OB_SPEC  information can then be copied  to  the  second
  151. position in the USERBLK when the object is initialized.
  152.  
  153.      One final note before moving on:  There is no reason that the
  154. USERBLK  cannot be extended beyond two fields.   You might want to
  155. add  extra words to store more information related to drawing  the
  156. object, such as its original type.
  157.  
  158.      The  AES will call your drawing code whenever  the  G_USERDEF
  159. needs  to be drawn.   This occurs when you make an objc_draw  call
  160. for its tree,  or when an objc_change occurs for that object.   If
  161. your user-defined object is in a menu drop-drop, then your drawing
  162. code will be called any time the user exposes that menu.
  163.  
  164.      Before  getting  into the details of the AES  to  application
  165. calling  sequence,  some  warnings are in order.   First,  remember
  166. that your drawing code will execute in the AES' context, using its
  167. stack.   Therefore,  be careful not to overuse the stack with deep
  168. recursion, long parameter lists, or large dynamic arrays.  Second,
  169. the  AES  is NOT re-entrant,  so you may not make ANY calls to  it
  170. from within a G_USERDEF procedure.   You may,  of course, call the
  171. VDI.   Finally,  realize  that drawing code associated with a menu
  172. object may be called by the AES at any time.   Exercise great care
  173. in  sharing  data  space between such code and  the  rest  of  the
  174. application!
  175.  
  176.      When your drawing code is called by the AES, the stack is set
  177. up  as if a normal procedure call had occured.   There will be one
  178. parameter  on the stack:  a 32-bit pointer to a PARMBLK structure.
  179. This structure lies in the AES' data space, so do not write beyond
  180. its end!
  181.  
  182.      The  PARMBLK contains 15 words.   The first two are the  long
  183. address of the object tree being drawn,  and the third word is the
  184. number of the G_USERDEF object.   You may need these values if the
  185. same  drawing  code is used for more than one object  at  a  time.
  186. Words  four  and five contain the previous  and  current  OB_STATE
  187. values of the object.  If these values are different, your drawing
  188. code  is  being  called in response  to  an  objc_change  request.
  189. Otherwise, the active AES call is an objc_draw.
  190.  
  191.      Words six through nine contain the object's rectangle on  the
  192. screen.   Remember  that  you cannot call objc_offset  within  the
  193. drawing code,  so you will need these values!  The next four words
  194. contain the clipping rectangle specified in the active objc_change
  195. or objc_draw call.   You should set the VDI clip rectangle to this
  196. value before doing any output to the screen.
  197.  
  198.      The last two words in the PARMBLK contain a copy of the extra
  199. 32-bit parameter from the object's USERBLK.   If you have followed
  200. the  method of copying an OB_SPEC into this location,  these words
  201. will be your pointer to a string, or BITBLK, or whatever.
  202.  
  203.      When  your drawing routine is done,  it should return a  zero
  204. value  to  the AES.   This is a "magic" value;  anything else will
  205. stop the drawing operation.
  206.  
  207.      The download contains a sample drawing routine which  defines
  208. one extended drawing object,  a rounded rectangle button.  You can
  209. use  this procedure as a starting point for your own User  Defined
  210. objects.
  211.  
  212.               PUT ANYTHING YOU WANT ON THE DESKTOP!
  213.  
  214.      In  ST  PRO GEM #2,  I described the use  of  the  WF_NEWDESK
  215. wind_set  call to substitute your own object tree for  the  normal
  216. green  desktop background.   If the tree you supply contains  User
  217. Defined  objects,  you can draw whatever you want on the  desktop!
  218. Some  of the things you might try are free hand drawings  imported
  219. in  metafile  format from EasyDraw,  or whole  screen  bit  images
  220. generated by Degas.   If you do the latter, you will have to store
  221. the entire image off screen and blit parts of it to the display as
  222. requested.
  223.  
  224.      In  any case,  remember that your desktop drawing code can be
  225. called any time that a window is moved,  so exercise the same care
  226. as with a menu drawer.   Also, be aware that making the WF_NEWDESK
  227. call  does  not force an immediate redraw of the desktop.   To  do
  228. that, do a form_dial(3) call for the entire desktop rectangle.
  229.  
  230.                         THE TOUCHEXIT FLAG
  231.  
  232.    The  TOUCHEXIT attribute is an alternative to  the  more  usual
  233. EXIT.   When the TOUCHEXIT bit is set in an object's OB_FLAG word,
  234. the form_do routine will exit immediately when the mouse button is
  235. pressed  with  the  cursor  over  the  object.    Your  code   can
  236. immediately take control of the mouse and display, without waiting
  237. for the release of the button.  This method is used for generating
  238. effects such as slider bars within otherwise normal dialogs.
  239.  
  240.      The  easiest  way to code a TOUCHEXIT handler is to  place  a
  241. loop  around the form_do call.   If the object number returned  is
  242. TOUCHEXIT,  then the animation procedure is called,  followed by a
  243. resumption   of  the  form_do  (WITHOUT  recalling  form_dial   or
  244. objc_draw!).   If the object returned is a normal EXIT, the dialog
  245. is  complete and control flows to the cleanup code.
  246.  
  247.      There is one idiosyncrasy of TOUCHEXIT which should be noted.
  248. When the AES "notices" that the mouse button has been pressed over
  249. a TOUCHEXIT,  it immediately retests the button state.   If it has
  250. already  been  released,  it  waits to see if  a  double click  is
  251. performed.  If so, the object number returned by form_do will have
  252. its  high  bit set.   If you don't care about double clicks,  your
  253. code should mask off this flag.   However, you may want to use the
  254. double click to denote some enhanced action.  For example, the GEM
  255. file selector uses a double click on one of the file name  objects
  256. to indicate a selection plus immediate exit.
  257.  
  258.                         THE INDIRECT FLAG
  259.  
  260.     If the INDIRECT bit is set in an object's OB_STATE  word,  the
  261. AES interprets the 32-bit OB_SPEC field as a pointer to the memory
  262. location  in which the ACTUAL OB_SPEC is to be found.   Like  User
  263. Defined objects,  this capability is not supported by the RCS,  so
  264. you  have to set up the INDIRECT bit and alter the OB_SPEC at  run
  265. time.
  266.  
  267.      The value of INDIRECT is that you can use it to associate  an
  268. AES  object with other data or code.   The general technique is to
  269. set  up  a table with a spare 32-bit location  at  its  beginning.
  270. Then,  when initializing the application's resource,  you move the
  271. true  OB_SPEC  into  this location,  set the  INDIRECT  flag,  and
  272. replace the OB_SPEC field with a pointer to the table.  The object
  273. behaves normally during drawing and form handling. However, if you
  274. receive  its  number  from  form_do  or  objc_find,  you  have  an
  275. immediate  pointer to the associated table,  without having to  go
  276. through a lookup procedure.
  277.  
  278.      This  technique works well in programs like the GEM  Desktop.
  279. Each  G_ICON object is set up with INDIRECT.   Its OB_SPEC goes to
  280. the  beginning of a data area defining the associated  file.   The
  281. blank  location at the beginning of file table is filled  up  with
  282. the former OB_SPEC, which points to a ICONBLK.
  283.  
  284.      You  can also combine INDIRECT with TOUCHEXIT  when  creating
  285. objects  that  must change when they are clicked by a  user.   For
  286. instance,  a  color  selection  box  might be linked  to  a  table
  287. containing  the various OB_SPEC values through which  the  program
  288. will cycle.   Each time the user clicked on the box, the TOUCHEXIT
  289. routine would advance the table pointer,  copy the next value into
  290. the  dummy OB_SPEC location at the front of the table,  and redraw
  291. the object in its new appearance.
  292.  
  293.      A  programmer  who wanted to follow a  truly  object-oriented
  294. "Smalltalkish" approach could use the INDIRECT method to bind  AES
  295. drawing  object to tables of associated procedures  or  "methods".
  296. For instance, one procedure could be flagged for use when the user
  297. clicked  on the object,  one when the object was dragged,  one for
  298. double-click,  and  so on.   If the table structure was capable of
  299. indicating  that  the true method was stored in another  table,  a
  300. rudimentary form of class inheritance could be obtained.
  301.  
  302.                        INSTANT CO-ROUTINES
  303.  
  304.      We turn to the AES event and message system for  this  trick.
  305. While   some  languages  like  Modula  2  provide  a  method   for
  306. implementing  co-routines,  there  is  no such  capability  in  C.
  307. However,  we  can  effectively  fake it by  using  the  AES  event
  308. library.
  309.  
  310.      As  already  seen in an earlier column,  an  application  can
  311. write a message to its own event queue using the appl_write  call.
  312. Usually, this is a redraw message, but there is nothing to prevent
  313. you from using this facility to send messages from one routine  in
  314. your program to another.  To set up co-routines using this method,
  315. they  would  be  coded  as separate  procedures  called  from  the
  316. application's main event loop.
  317.  
  318.      When  one  of the co-routines wanted to call  the  other,  it
  319. would  post  a message containing the request and  any  associated
  320. parameters into the application's queue and then return.  The main
  321. loop  would find the message and make the appropriate call to  the
  322. second co-routine.  It it was necessary to then re-enter the first
  323. co-routine  at  the  calling point,  the  original  message  could
  324. contain an imbedded reply message to be sent back when the request
  325. was  complete.   A  simple switch structure could then be used  to
  326. resume at the appropriate point.
  327.  
  328.      There  are two potential problems in using this method.   The
  329. first is the limited capacity of the application event queue.  The
  330. queue  contains  eight entries.    While the AES  economizes  this
  331. space  by  merging redraws and multiple events,  it  cannot  merge
  332. messages.   Because  of this limit,  you must be extremely careful
  333. when  one  message received has the potential to generate  two  or
  334. more  messages sent.   Unless this situation is carefully managed,
  335. you  can get a sort of "cancer" which will overflow the queue  and
  336. probably crash your application.
  337.  
  338.      The second danger involves race conditions.   Message sent by
  339. the  application  are posted to the end of the  queue.   If  other
  340. events  have occurred,  such as mouse clicks or keyboard  presses,
  341. they will be seen and processed ahead of the application generated
  342. message.   This  implies  that you cannot use this method  if  the
  343. program must complete its action before a new user generated event
  344. can be processed.
  345.  
  346.                         THAT'S ALL FOR NOW
  347.  
  348.     Hopefully these hints will keep you profitably occupied for  a
  349. while.   ST PRO GEM number twelve will return to the topic of user
  350. interfaces.   Reaction  to the first article on this  subject  was
  351. mostly  positive,  but a lot of folks wanted to see real  code  as
  352. well.   In response to your feedback,  there will also be code for
  353. implemented  your  own "mouse sensitive" objects  which  highlight
  354. when the cursor touches them.   This will be presented as part  of
  355. an alternate form manager.
  356.  
  357.                          UPDATE: ATARI ST
  358.  
  359.       I  have  recently gotten more  information  on  some  topics
  360. mentioned in earlier articles.   These notes will bring you up  to
  361. date:
  362.  
  363.      A number of developers reported that they were unable to get
  364. the  self-redraw  technique described in ST PRO GEM  #2  to  work.
  365. This is usually due to a bug in the appl_init binding in Alcyon C.
  366. The  value  returned from the function,  which would  normally  be
  367. assigned  to gl_apid,  is coming back as garbage.   To work around
  368. the problem,  declare EXTERN WORD gl_apid;  in your program and DO
  369. NOT  assign the value from appl_init.   The binding WILL make  the
  370. assignment.  A tip of the hat to Russ Wetmore for this report.
  371.  
  372.      The   last  column  mentioned  that  turning  off  the   clip
  373. rectangle  while drawing graphics text will speed things  up.   It
  374. turns  out that the VDI will also run at the non-clipped speed  if
  375. the  ENTIRE  string  to  be written is  within  the  current  clip
  376. rectangle.  To compound the problem, there is a one-off bug in the
  377. detection  algorithm  for  the right  edge.   That  is,  the  clip
  378. rectangle  has to be one pixel BEYOND the right edge of  the  text
  379. for the fast write to work.
  380.  
  381.      The Feedback in ST PRO GEM #10 mentioned that there are known
  382. bugs  in  the  Alcyon C floating point  library.   In  fact,  this
  383. library  has been replaced with a new,  debugged version in recent
  384. shipments  of the Toolkit.   If you need to use floating point and
  385. have  run into this bug,  you should be able to get an update from
  386. Atari.   Also,  check  the  Atari  Developer's SIG (PCS-57) for  a
  387. possible download.
  388.  
  389.      In addition, it turns out there is an undocumented feature in
  390. Alcyon  C  which allows you to imbed assembly code  in-line.   Try
  391. using:
  392.  
  393.      asm(".....");
  394.  
  395. where the dots are replaced with an assembly instruction.  You get
  396. one instruction per asm(),  one asm() per line.  Thanks to Leonard
  397. Tramiel for both of the above tidbits.
  398.