home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / MISC / XLSP21TC.ZIP / WINTERP.DOC / text0000.txt < prev   
Encoding:
Text File  |  1991-04-14  |  14.0 KB  |  311 lines

  1. In article <1511@dinl.mmc.UUCP> noren@dinl.UUCP (Charles Noren) writes:
  2. >It's been a while since I've been on the net.  I can no longer
  3. >access comp.lang.lisp.x from our site, I suppose it went away?
  4.  
  5. it's still there...
  6.  
  7. >I've just starting playing with XLISP v2.0, particularly the
  8. >object-oriented features of it.  I've created new classes with
  9. >instance and class variables, and I've used the :new selector
  10. >to do so and it works just fine.  However, I see the :isnew
  11. >selector in the documentation and I was wondering how that works
  12. >compared to :new.
  13.  
  14. When I first looked at XLISP, I too found the documentation on the
  15. object system to be a little terse. Everything becomes much clearer
  16. once you see some examples. 
  17.  
  18. I recently wrote up some documentation on XLISP's object system for
  19. use with WINTERP (an XLISP-based rapid prototyping environment for
  20. applications based on the OSF Motif widgets). The following excerpt
  21. from winterp/doc/winterp.doc may help (get winterp via anonymous ftp
  22. from expo.lcs.mit.edu:oldcontrib/winterp.tar.Z). In particular, your
  23. question about :ISNEW is answered in the "object initialization"
  24. section. 
  25.  
  26.             --------------------
  27.  
  28. * Introduction to XLISP objects and Widgets.
  29.  
  30. WINTERP uses XLISP's object system as its interface to the class hierarchy
  31. of widgets provided by Motif. Specifically, each Motif widget class is
  32. represented by one or more object classes in WINTERP.  In order to best
  33. understand the capabilities of WINTERP's Motif interface, a brief review of
  34. the XLISP object system is in order. You may also want to consult the XLISP
  35. documentation ./winterp/doc/xLisp.doc for a more precise definition of the
  36. object system.
  37.  
  38. XLISP Classes describe the type of a particular object by declaring a set
  39. of variables held in each object. These "instance variables" may only be
  40. accessed by "methods" that respond to "messages" sent to the object.
  41. Methods are defined for particular classes, and functionality of other
  42. classes may be incorporated into new classes via "inheritance". From
  43. XLISP, Motif widget classes look just like normal XLISP objects -- that
  44. means that you can easily extend the functionality of Motif widgets by
  45. adding your own methods to a particular widget class. You may also use
  46. inheritance to attach your own data structures to widgets. The result is
  47. that WINTERP provides a very clean way to interactively rapid-prototype an
  48. application, while also providing mechanisms for code structuring and reuse.
  49. The latter is necessary in evolving from prototype to a structured,
  50. maintainable, and customizable deliverable.
  51.  
  52.  
  53. ** Creating new objects.
  54.  
  55. Create a new instance of a class by sending the message :NEW to
  56. <a_class_instance>:
  57.  
  58.     (SEND <a_class_instance> :NEW <parameters>)
  59.  
  60. <a_class_instance> is in fact an instance of class CLASS. Class CLASS allows
  61. you to define new class instances by specifying the instance variables and
  62. parent class of a particular class.
  63.  
  64.  
  65. ** Declaring a class.
  66.  
  67. To declare a "base class" object, that is, an object with no parent object,
  68. just send message :NEW to the object <CLASS>
  69.  
  70.     (SEND CLASS :NEW '(<ivar0> ... <ivarN>)
  71.              ['(<cvar0> ... <cvarM>)])
  72.  
  73. '(<ivar0> ... (ivarN>) are a list of symbols. Each <ivar-i> names an
  74. instance variable of the class. '(<cvar0> ... <cvarM>)]) are an optional
  75. list of variables that are shared among all instances of that particular
  76. class.
  77.  
  78.  
  79. ** Defining methods.
  80.  
  81. When a "message" is sent to an object, XLISP searches for a "method" to
  82. answer the message. A method is a piece of Lisp code that is executed when
  83. a particular message is sent to an object. Within the code of a method, all
  84. object instance and class variables are accessible. Furthermore, the symbol
  85. 'self' is bound to the object the message was sent to.
  86.  
  87. Methods are defined by sending the message :ANSWER to <a_class_instance>:
  88.  
  89.     (SEND a_class_instance :ANSWER <:msg> <parameters> <code>)
  90.  
  91. where <:msg> is a keyword symbol (a symbol with a ':' prefix) representing
  92. the message; <parameters> are the arguments given along with the message.
  93. See the documentation on "lambda lists" in /winterp/doc/xLisp.doc p.12 for
  94. details.  <code> is a list of s-expressions which get evaluated in response
  95. to a message. The lexical environment that existed for the call to :ANSWER
  96. will be used for value and functional bindings during method evaluation.
  97.  
  98. If you need to remember what the syntax is, consider the memory-helper
  99.     "this class :ANSWERs to :MESSAGE..." == (send <cls> :ANSWER :MESSAGE ...)
  100.  
  101.  
  102. ** Inheritance
  103.  
  104. So far, the object system we just described supports *encapsulation*.
  105. Encapsulation is good programming practice because it helps localize and
  106. detangle complexity. Unfortunately, encapsulation runs counter to
  107. flexibility because making flexible use of an object may require that
  108. certain groups of instance variables be accessed by different layers of new
  109. functionality. Most often, one wants to *reuse* aspects of a particular
  110. class in creating code that specializes and alters the functionality of
  111. that class. A compromise between encapsulation and flexibility is found by
  112. using *inheritance* in an object system. Inheritance is used to allow a
  113.  *subclass* to specialize the functionality of it's *parent class* (aka,
  114. the *superclass*):
  115.  
  116.     (send Class :NEW '(<ivar0> ... <ivarN>)
  117.                          '(<cvar0> ... <cvarM>)
  118.              <superclass>)
  119.  
  120. (<ivar0> ... <ivarN>) is a list of new instance variables in the subclass;
  121. (<cvar0> ... <cvarN>) is a list of new class variables in the subclass;
  122. <superclass> is a class instance representing the parent from which
  123. the new subclass inherits variables and methods.
  124.  
  125. "Inheritance" is occurring because all the instance variables of all the
  126. parent classes of the new subclass become the variables of each subclass
  127. instance. Furthermore, all methods defined on a parent class may also be
  128. used on a subclass instance. Note that while a subclass' methods can access
  129. the variables defined on the parent classes, the reverse isn't true.
  130.  
  131.  
  132. ** Object initialization.
  133.  
  134. As mentioned earlier, new object instances are created by sending the
  135. message :NEW to a class object. Sending the message :NEW to a class
  136. automatically sends message :ISNEW to the newly created instance. By
  137. default :ISNEW on an instance is a no-op method defined on class 'Object',
  138. which is the implicit [(grand)*]parent of all instances. If you want to
  139. initialize the instance/class variables of a particular class, you must
  140. define an :ISNEW method on the class.  Any parameters originally sent to
  141. the :NEW method will be passed on to the :ISNEW method and may be used to
  142. initialize an object to outside-world parameters.
  143.  
  144.  
  145. ** Example of using OOP features of XLISP with Motif widgets:
  146.  
  147. As currently implemented, the Motif class xmListWidgetClass makes it a bit
  148. baroque to create browsers (hopefully this will change in Motif 1.1).  The
  149. problem is that a "browser" is a kind of application that lends itself to
  150. object oriented techniques that are not always straightforward to support
  151. in C. One often has a collection of 'things' that one wants to display in a
  152. 'list' and perform actions on the 'thing' corresponding to the visual
  153. selection of an element in the displayed list. xmListWidgetClass will
  154. display an arrray of XmStrings in a list. When one or more elements in the
  155. list are selected, XmStrings corresponding to the selected elements are
  156. returned. Since the XmStrings you put into the list widget are not the
  157. XmStrings you get out, you must call XmStringCompare on each element of the
  158. collection of 'things' to find out which ones are selected.  Presumably,
  159. you'll want to do more than just get back an XmString...  normally one will
  160. want to access data structures associated with the XmString so as to perform
  161. an action dependent on those structures. This could be done with a lookup
  162. table, but there's also a better way...
  163.  
  164. WINTERP allows us to subclass the Motif list widget so as to make it have
  165. the kind of functionality we want. This subclass will contain an additional
  166. instance variable 'items' which is an array of arbitrary XLISP objects to
  167. be displayed in a textual browser made from the list widget. These objects
  168. can have completely different internal representations -- the only
  169. requirement is that they follow the protocol of being able to respond to
  170. the messages :DISPLAY_STRING and :DEFAULT_ACTION. :DISPLAY_STRING returns a
  171. string representation of the object to be displayed in the browser.
  172. :DEFAULT_ACTION is the action to be performed when a list item is browsed
  173. (by double clicking on the item).
  174.  
  175. The following creates the subclass <List_Browser> from superclass
  176. <XM_LIST_WIDGET_CLASS>:
  177.  
  178.     (setq List_Browser 
  179.         (send Class :NEW        ;create a class inst
  180.             '(items)        ;new instance vars
  181.             '()            ;no class vars
  182.             XM_LIST_WIDGET_CLASS))    ;superclass
  183.  
  184. So now all instances of <List_Browser> will contain an instance variable
  185. <items> and will respond to all the messages understood by the
  186. XM_LIST_WIDGET_CLASS. We want our list browser to behave as described
  187. above, so we define an :ISNEW method to initialize instance variable
  188. <items> to the list of arbitrary objects to be displayed.  <items> gets
  189. initialized to an array of objects, the list widget is created, and a
  190. XmNdefaultActionCallback is setup so that a double click will send the
  191. message :DEFAULT_ACTION to the browsed item:
  192.  
  193.     ;; (send List_Browser :new <items_list> <args-for-the-list-widget>)
  194.     ;; <items_list> is a list of BROWSER_OBJECTs as described above.
  195.     ;; <args-for-the-list-widget> -- these are the arguments that
  196.     ;;       will be passed on to the list widget
  197.     ;;
  198.     (send List_Browser :answer :isnew '(items_list &rest args)
  199.           '(
  200.         (let* (
  201.                (items_end_idx (length items_list))
  202.                (display_items (make-array items_end_idx)))
  203.  
  204.           ;; initialize the 'items' instance variable so that it
  205.           ;; holds all the BROWSER_OBJECTs passed in <items_list>
  206.           (setq items (make-array items_end_idx)) ;create the array
  207.           (do (                         ;copy elts from list to array
  208.                (i    0          (1+ i))
  209.                (elts items_list (cdr elts)))
  210.               ;; loop till no more elts
  211.               ((null elts))
  212.               ;; loop body
  213.               (setf (aref items i) (car elts))
  214.               (setf (aref display_items i) 
  215.                 (send (car elts) :display_string))
  216.               )
  217.  
  218.           ;; initialize the widget, passing in the browser items.
  219.           (apply 'send-super `(:isnew
  220.                        ,@args
  221.                        :xmn_selection_policy :browse_select
  222.                        :xmn_items ,display_items
  223.                        :xmn_item_count ,items_end_idx
  224.                        ))
  225.           )
  226.  
  227.         ;; set up a callback on the list widget initialized above such
  228.         ;; that a double click on the browser-item will send the
  229.         ;; message :default_action to the BROWSER_OBJECT.
  230.         (send-super :add_callback :xmn_default_action_callback
  231.                 '(callback_item_position)
  232.                 '((send (aref items (1- callback_item_position))
  233.                     :default_action))
  234.                 )
  235.         )
  236.           )
  237.  
  238.  
  239. In the above code, SEND-SUPER works just like send, except that it doesn't
  240. require you to give it the object to send the message to.  Instead, it
  241. implicitly assumes that it will be called from within a method, and will
  242. automatically send the message to a superclass of the object's class.  The
  243. (apply 'send-super ...) form is actually calling the :ISNEW (instance
  244. initializer) method on XM_LIST_WIDGET_CLASS, which actually creates the
  245. widget via XmCreateList() or XmCreateScrolledList(). The APPLY '`'
  246. (BACKQUOTE) and '&rest args' (LAMBDA LIST) features of Lisp allow us to
  247. splice in the argument list passed to the instance of List_Browser into the
  248. function that actually creates the widget. Finally, method :add_callback is
  249. the WINTERP equivalent of XtAddCallback(). See the documentation on methods
  250. on WIDGET_CLASS for more details.
  251.  
  252. The Motif List widget also defines a number of "methods" implemented as C
  253. routines such as XmListAddItem(), XmListAddItemUnselected(),
  254. XmListDeleteItem(), and XmListDeletePos(). In WINTERP, we define these as
  255. methods :ADD_ITEM, :ADD_ITEM_UNSELECTED, :DELETE_ITEM, and :DELETE_POS
  256. respectively. Since these methods modify the collection of objects
  257. represented by the list widget, we must update the internal array of
  258. objects <items> to correspond with the items displayed. We do this by
  259. intercepting those messages to the superclass of class <List_Browser> and
  260. handle them in <List_Browser> so as to update the appropriate data:
  261.  
  262.     (send List_Browser :answer :ADD_ITEM '(item position)
  263.           '(
  264.         (setq items (array-insert-pos items (1- position) item))
  265.         (send-super :add_item 
  266.                 (send item :display_string) 
  267.                 position)
  268.         )
  269.           )
  270.  
  271.     (send List_Browser :answer :ADD_ITEM_UNSELECTED '(item position)
  272.           '(
  273.         (setq items (array-insert-pos items (1- position) item))
  274.         (send-super :add_item_unselected 
  275.                 (send item :display_string)
  276.                 position)
  277.         )
  278.           )
  279.  
  280.     (send List_Browser :answer :DELETE_ITEM '(item)
  281.           '(
  282.         ;; this is too lame to implement... requires that we compare
  283.         ;; item with the result of :display_string done on every elt
  284.         ;; of ivar 'items'
  285.         (error "Message :DELETE_ITEM not supported in List_Browser")
  286.         )
  287.           )
  288.  
  289.     (send List_Browser :answer :DELETE_POS '(position)
  290.           '(
  291.         (setq items (array-delete-pos items (1- position)))
  292.         (send-super :delete_pos position)
  293.            )
  294.          )
  295.  
  296. To see how this subclassed list browser is used, and also to see how one
  297. might write a sample application in WINTERP using the object oriented
  298. features of XLISP, see ./winterp/examples/grep-br.lsp.  That file
  299. implements a simple search browser based on the UN*X command 'grep'. See
  300. also ./winterp/examples/mail-br.lsp to see how you can build a simple
  301. mh-based mail browser. Finally, as another example of subclassing Motif
  302. widgets, see ./winterp/examples/radiobox2.lsp.
  303. -------------------------------------------------------------------------------
  304.         Niels Mayer -- hplabs!mayer -- mayer@hplabs.hp.com
  305.           Human-Computer Interaction Department
  306.                Hewlett-Packard Laboratories
  307.                   Palo Alto, CA.
  308.                    *
  309.  
  310.  
  311.