home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / windows / misc / 1746 < prev    next >
Encoding:
Internet Message Format  |  1992-12-28  |  7.4 KB

  1. Xref: sparky comp.windows.misc:1746 comp.lang.objective-c:703
  2. Newsgroups: comp.windows.misc,comp.lang.objective-c
  3. Path: sparky!uunet!psinntp!afs!greg
  4. From: greg@afs.com (Gregory H. Anderson)
  5. Subject: Re: Alternative GUI class hierarchies?
  6. Message-ID: <1992Dec28.170847.420@afs.com>
  7. Sender: greg@afs.com
  8. Reply-To: greg@afs.com
  9. References: <1992Dec27.063724.15059@u.washington.edu>
  10. Date: Mon, 28 Dec 1992 17:08:47 GMT
  11. Lines: 122
  12.  
  13. In article <1992Dec27.063724.15059@u.washington.edu>  
  14. wiml@milton.u.washington.edu (William Lewis) writes:
  15. >    Recently I've been pondering the construction of an object-oriented
  16. > GUI toolkit (particularly, using gcc-objc over X, but that detail is 
  17. > irrelevant), trying to combine the best attributes of the X intrinsics
  18. > and NeXTstep (the two OO GUI toolkits I'm familiar with). One thing that
  19. > strikes me as inelegant about both toolkits is the View/Cell
  20. > distinction (in X, the Widget/Gadget distinction). Although I understand
  21. > the utility of a "lightweight view" for menu selections and so forth,
  22. > I've never really liked it. I've thought of two possible solutions.
  23. >    The first one is really just a workaround. Instead of having
  24. > separate hierarchies for "heavyweight" and "lightweight" objects, each
  25. > object could select at instantiation whether it should be lightweight
  26. > or not. "Views" ("widgets") who are "lightweight" would have to 
  27. > communicate with their closest "heavyweight" ancestor to ask for
  28. > events, drawing requests, to determine the (x,y) offset at which
  29. > to draw (since in X at least, drawing coordinates are relative to the
  30. > destination drawable --- transformation matrices could be very useful
  31. > here) and so forth. This would require a little more work on the part
  32. > of object authors, however, to handle both cases; and it still wouldn't
  33. > simplify the client's structure much (only the display server's). 
  34.  
  35. I think NeXT's architecture solves this problem nicely. TextFields, for  
  36. example, already act primaily as Enclosures. Most of their methods are  
  37. simply "covers" for the underlying Cell methods. The fact that a distinct  
  38. Cell is actually doing the drawing is hidden in the implementation. (So  
  39. well hidden, in fact, that we subclassed TextFields wrong the first time,  
  40. by putting most of the cool new stuff in the View, not the Cell. Duh.)  
  41. Cells draw relative to their bounding (not frame) rectangle, which in fact  
  42. is relative to the destination drawable. So what's the problem?
  43.  
  44. >    The other idea is to divide the object hierarchy even more strictly,
  45. > so that any given object can *only* be in one or another subhierarchy.
  46. > Basically, I'm thinking of dividing all graphical objects into two
  47. > types: those which enclose an area, and those which draw things in
  48. > an area. Only area-enclosing objects would receive events from the
  49. > display server and mouse. Area-enclosing objects would be allowed to do 
  50. > a little drawing, say a frame and a background, but nothing complex 
  51. > like text.  Under X, for instance, "enclosing" objects would (possibly) 
  52. > have their own window, but "drawing" objects would never have a window.
  53.  
  54. This is *exactly* the distinction between NeXT Views and Cells. But the  
  55. strict hierarachy is bad, for a different reason: A View can give the  
  56. handle of its Cell to another View, which can then share the draw code. In  
  57. this way, the exact same contents can be drawn on multiple Windows, in  
  58. differently sized (and attributed) Views. We use this. It's nice.
  59.  
  60. >    Immediately, the question is "but what about objects that both
  61. > enclose area, *and* draw things in that area? Like buttons, sliders,
  62. > text-boxes, menus ... pretty much everything in fact." For these,
  63. > I would have a *third* object which "owns" and manages the enclosing
  64. > and the drawing objects. For example, a Button object might own
  65. > a SqareEnclosure object and a Text (or SmallImage) object. Only
  66. > the enclosure and the text objects would have a visible manifestation. 
  67. > The application programmer would interact only with the Button object,
  68. > but the Button object would use the other two objects to actually
  69. > do its work. This strikes me as having many advantages. Because each
  70. > UI element is more modular, it's easy to swap the subparts around:
  71. > the same Button class could be used for a button with a text label,
  72. > a button with an iconic label, or a button with some application-defined
  73. > image, without changing the Button class at all. Similarly, square
  74. > buttons, rounded buttons, and hexagonal buttons can be created by
  75. > writing a new "enclosure" class and causing the Button to use that
  76. > class instead of the "SquareEnclosure". I think this advantage is
  77. > applicable to other common UI objects as well.
  78.  
  79. Again, for the most part NeXTSTEP works this way. A Form, for example,  
  80. owns a list of FormCells. The FormCells, in turn, each own a TextField  
  81. cell for the purpose of containing the drawing their titles. All of this  
  82. is hidden in the implementation by using "cover" methods.
  83.  
  84. On of the great things about the distinction between Views and Cells is  
  85. that you can take a simple View like a TextField, suck out its Cell as the  
  86. prototype for a Matrix, and voila! you have multiple Cells all acting and  
  87. validating like the original. Instant spreadsheet.
  88.  
  89. >    The main *disadvantage* is that, in one sense, I've worsened the
  90. > original problem: there are now *more* objects floating around in the
  91. > application program than there are in the usual scenario! Three objects 
  92. > just for a button; more for anything more complex. However, I think this
  93. > might not be so bad, because
  94. >      - The individual objects are simpler. There are probably also
  95. > fewer distinct classes in use at any given time, which could reduce the
  96. > RSS of the application.
  97. >      - If written well, the Enclosure class could offer much greater
  98. > control over server-side resource use. The idea of having each object
  99. > select whether it;s lightweight or heavyweight could be used here as  
  100. well. 
  101. >      - Changing the "look" of an application -- say, from Motif-y to
  102. > Next-ish to Mac-like --- might involve changing only the Enclosures
  103. > and some drawers (like sliders), leaving many application-defined
  104. > objects alone. This would have to be designed in, though.
  105.  
  106. In theory, yes. In practice, difficult. NeXT's implementation of the  
  107. Responder chain within responder objects, to give one gotcha example, does  
  108. not port well to X.
  109.  
  110. >      - In Objective-C, all objects are individually dynamically
  111. > allocated. But with some tweaking, it should be possible to allocate 
  112. > space for a Button and both of its slave objects in one *alloc() call.
  113. > The space would be freed when the Button was freed; the Button would
  114. > have the responsibility of calling its component objects' -free methods
  115. > before it deallocates itself. ([Object free] would have to be rewritten
  116. > to check for this sort of situation, of course.) 
  117.  
  118. This already happens. When you init a TextField, it allocs its  
  119. TextFieldCell at the same time.
  120.  
  121. >    I think that combining objects in this way would make the increase
  122. > in the number of objects much less harmful, since the number of
  123. > malloc() blocks would be the same, or even reduced. It would also
  124. > improve the locality of reference when responding to events.
  125.  
  126. If you use allocFromZone, which you should, this problem is solved.
  127.  
  128. --
  129. Gregory H. Anderson          | Thus spake the master programmer: 
  130. Master Programmer / Manager  | "Let the programmers be many and  
  131. Anderson Financial Systems   | the managers few; then all will be 
  132. greg@afs.com  (Nextmail OK)  | productive." -- Tao of Programming
  133.