grtree.sl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. % {DSK}GRTREE.PSL;11 4-FEB-83 16:48:01
  2. (GLOBAL '(GRAPHICSBOXTYPES))
  3. % Tree Drawing Package. To test, do (DLT TX WW) where WW is a window.
  4. (GLISPOBJECTS
  5. (BOXTYPE (ATOM (PROPLIST (DRAWPROGRAM ATOM)
  6. (SIZEPROGRAM ATOM)))
  7. MSG ((DRAW BOXTYPE-DRAW OPEN T)
  8. (ERASE BOXTYPE-ERASE OPEN T)
  9. (SETSIZE BOXTYPE-SETSIZE OPEN T)))
  10. (GRAPHICSBOX (LISTOBJECT (BOXTYPE BOXTYPE)
  11. (START VECTOR)
  12. (SIZE VECTOR)
  13. (CONTENTSOFFSET VECTOR)
  14. (DISPLAYCONTENTS ANYTHING)
  15. (CONTENTSSIZE VECTOR))
  16. MSG ((DRAWIN GRAPHICSBOX-DRAWIN OPEN T)
  17. (ERASEIN GRAPHICSBOX-ERASEIN OPEN T)
  18. (SETSIZE ((SEND BOXTYPE SETSIZE self))))
  19. SUPERS (REGION))
  20. (GRAPHICSTREE (LISTOBJECT (TOPNODE TREE)
  21. (GRTREE TREEELEMENT)
  22. (BOXTYPE BOXTYPE)
  23. (LINESTYPE LINESTYPE)
  24. (SPACING VECTOR))
  25. MSG ((CREATE CREATETREE SPECIALIZE T)
  26. (MATCH MATCHTREE SPECIALIZE T)
  27. (SELECTNODE GRAPHICSTREE-SELECTNODE OPEN T)))
  28. (LISPGRAPHICSTREE (LISTOBJECT (TOPNODE LISPTREE)
  29. (GRTREE TREEELEMENT))
  30. PROP ((BOXTYPE ('RECTANGLE)
  31. RESULT BOXTYPE)
  32. (LINESTYPE ('STRAIGHT)
  33. RESULT LINESTYPE)
  34. (SPACING ('(10 20))
  35. RESULT VECTOR))
  36. SUPERS (GRAPHICSTREE))
  37. (LISPNODEDISPLAY (LISTOBJECT (CONTENTS ANYTHING))
  38. PROP ((DISPLAYSIZE ((A VECTOR WITH X = (NCHARS CONTENTS)
  39. *7 Y = 10))))
  40. MSG ((DRAW STRINGDATA-DRAW)))
  41. (LISPTREE (EXPR ANYTHING)
  42. PROP ((CONTENTS ((A LISPNODEDISPLAY WITH CONTENTS =
  43. (IF EXPR IS ATOMIC THEN EXPR ELSE (CAR EXPR)))))
  44. (SUCCESSORS ((IF EXPR IS ATOMIC THEN NIL ELSE (CDR EXPR)))
  45. RESULT
  46. (LISTOF LISPTREE)))
  47. ADJ ((TERMINAL (EXPR IS ATOMIC))))
  48. (TREEELEMENT (LISTOBJECT (BOX GRAPHICSBOX)
  49. (ORIGINALNODE ANYTHING)
  50. (SUCCESSORS (LISTOF TREEELEMENT))
  51. (DISPLAYSIZE VECTOR))
  52. PROP ((DISPLAYWIDTH (DISPLAYSIZE:X))
  53. (DISPLAYHEIGHT (DISPLAYSIZE:Y))
  54. (TOTALAREA ((VIRTUAL REGION WITH START = TOTALSTART SIZE =
  55. DISPLAYSIZE)))
  56. (TOTALSTART ((VIRTUAL VECTOR WITH X = BOX:START:X + (BOX:SIZE:X
  57. - DISPLAYSIZE:X)
  58. / 2 Y = BOX:START:Y + BOX:SIZE:Y -
  59. DISPLAYSIZE:Y))))
  60. MSG ((DRAWIN TREEELEMENT-DRAWIN)
  61. (SELECTNODE TREEELEMENT-SELECTNODE)))
  62. )
  63. % GSN 14-JAN-83 12:58
  64. (DG BOXTYPE-DRAW (BOXTYPE:BOXTYPE BOX:GRAPHICSBOX W:WINDOW)
  65. (APPLY BOXTYPE:DRAWPROGRAM (LIST BOX 'PAINT
  66. W)))
  67. % GSN 14-JAN-83 12:58
  68. (DG BOXTYPE-ERASE (BOXTYPE:BOXTYPE BOX:GRAPHICSBOX W:WINDOW)
  69. (APPLY BOXTYPE:DRAWPROGRAM (LIST BOX 'ERASE
  70. W)))
  71. % GSN 14-JAN-83 09:52
  72. (DG BOXTYPE-SETSIZE (BOXTYPE:BOXTYPE BOX:GRAPHICSBOX)
  73. (BOX:CONTENTSSIZE _ (SEND BOX:DISPLAYCONTENTS DISPLAYSIZE))(APPLY
  74. BOXTYPE:SIZEPROGRAM
  75. (LIST BOX)))
  76. % GSN 2-FEB-83 12:58
  77. (DG CIRCLESIZE (BOX:GRAPHICSBOX)
  78. (PROG (DIAM)
  79. (DIAM _ BOX:CONTENTSSIZE:IMAGNITUDE + 10)
  80. (BOX:SIZE _ (A VECTOR WITH X = DIAM Y = DIAM))
  81. (BOX:CONTENTSOFFSET _ (A VECTOR WITH X = (DIAM - BOX:CONTENTSSIZE:X)
  82. /2 Y = (DIAM - BOX:CONTENTSSIZE:Y)
  83. /2))))
  84. % GSN 2-FEB-83 11:23
  85. (DG CREATETREE (TR:GRAPHICSTREE)
  86. (SEND TR MATCH TOPNODE))
  87. % GSN 2-FEB-83 14:04
  88. % Draw a Lisp tree.
  89. (DG DLT (EXPR WW:WINDOW)
  90. (PROG (TREE)
  91. (SEND WW CLEAR)
  92. (TREE _ (SEND (A LISPGRAPHICSTREE WITH TOPNODE = EXPR)
  93. CREATE))
  94. (IF TREE:DISPLAYSIZE > WW:SIZE THEN (ERROR 0 "Window is too small")
  95. ELSE
  96. (SEND TREE DRAWIN (AN AREA WITH SIZE = TREE:DISPLAYSIZE START =
  97. (SEND WW CENTEROFFSET TREE:DISPLAYSIZE))
  98. WW))))
  99. % GSN 2-FEB-83 12:16
  100. (DG DRAWGRCIRCLE (BOX:GRAPHICSBOX DSPOP:ATOM W:WINDOW)
  101. (PROG (OLDDS)
  102. (OLDDS _ (CURRENTDISPLAYSTREAM W))
  103. (DSPOPERATION DSPOP)
  104. (DRAWCIRCLE BOX:CENTER:X BOX:CENTER:Y BOX:SIZE:X/2 NIL W)
  105. (CURRENTDISPLAYSTREAM OLDDS)))
  106. % GSN 2-FEB-83 13:12
  107. (DG DRAWGRELLIPSE (BOX:GRAPHICSBOX DSPOP:ATOM W:WINDOW)
  108. (PROG (OLDDS)
  109. (OLDDS _ (CURRENTDISPLAYSTREAM W))
  110. (DSPOPERATION DSPOP)
  111. (DRAWELLIPSE BOX:CENTER:X BOX:CENTER:Y BOX:SIZE:Y/2 BOX:SIZE:X/2 0 NIL
  112. NIL W)
  113. (CURRENTDISPLAYSTREAM OLDDS)))
  114. % GSN 14-JAN-83 13:01
  115. (DG DRAWRECTANGLE (BOX:GRAPHICSBOX DSPOP:ATOM W:WINDOW)
  116. (PROG (OLDDS)
  117. (OLDDS _ (CURRENTDISPLAYSTREAM W))
  118. (DSPOPERATION DSPOP)
  119. (MOVETO BOX:LEFT BOX:BOTTOM)
  120. (DRAWTO BOX:LEFT BOX:TOP)
  121. (DRAWTO BOX:RIGHT BOX:TOP)
  122. (DRAWTO BOX:RIGHT BOX:BOTTOM)
  123. (DRAWTO BOX:LEFT BOX:BOTTOM)
  124. (CURRENTDISPLAYSTREAM OLDDS)))
  125. % GSN 2-FEB-83 13:12
  126. (DG ELLIPSESIZE (BOX:GRAPHICSBOX)
  127. (PROG (DIAM)
  128. (DIAM _ BOX:CONTENTSSIZE:IMAGNITUDE + 10)
  129. (BOX:SIZE _ (A VECTOR WITH X = DIAM Y = BOX:CONTENTSSIZE:Y + 10))
  130. (BOX:CONTENTSOFFSET _ (A VECTOR WITH X = (DIAM - BOX:CONTENTSSIZE:X)
  131. /2 + 1 Y = 6))))
  132. % GSN 14-JAN-83 12:55
  133. (DG GRAPHICSBOX-DRAWIN (BOX:GRAPHICSBOX W:WINDOW)
  134. (SEND BOX:BOXTYPE DRAW BOX W))
  135. % GSN 14-JAN-83 12:55
  136. (DG GRAPHICSBOX-ERASEIN (BOX:GRAPHICSBOX W:WINDOW)
  137. (SEND BOX:BOXTYPE ERASE BOX W))
  138. % GSN 2-FEB-83 16:14
  139. (DG GRAPHICSTREE-SELECTNODE (GT:GRAPHICSTREE V:VECTOR)
  140. (SEND GT:GRTREE SELECTNODE V))
  141. % GSN 3-FEB-83 13:29
  142. % Build a TREEELEMENT structure to match the given tree TR.
  143. (DG MATCHTREE (TR:GRAPHICSTREE NODE:TREE)
  144. (RESULT TREEELEMENT)(PROG (TE SUM MAXH)
  145. (TE _
  146. (A TREEELEMENT WITH ORIGINALNODE = NODE BOX =
  147. (A GRAPHICSBOX WITH BOXTYPE = TR:BOXTYPE
  148. DISPLAYCONTENTS = NODE:CONTENTS)
  149. SUCCESSORS = (FOR X IN NODE:SUCCESSORS
  150. COLLECT
  151. (SEND TR MATCH X))))
  152. (SEND TE:BOX SETSIZE)
  153. (TE:DISPLAYWIDTH _
  154. (IF NODE IS TERMINAL THEN
  155. TE:BOX:WIDTH + TR:SPACING:X
  156. ELSE (SUM_0)
  157. (FOR X IN TE:SUCCESSORS DO
  158. SUM_+X:DISPLAYWIDTH)
  159. (MAX (TE:BOX:WIDTH +
  160. TR:SPACING:X)
  161. SUM)))
  162. (TE:DISPLAYHEIGHT _
  163. (IF NODE IS TERMINAL THEN
  164. TE:BOX:HEIGHT ELSE (MAXH_0)
  165. (FOR X IN TE:SUCCESSORS DO
  166. (MAXH_ (MAX MAXH
  167. X:DISPLAYHEIGHT)))
  168. (TE:BOX:HEIGHT + TR:SPACING:Y
  169. + MAXH)))
  170. (RETURN TE)))
  171. % GSN 2-FEB-83 12:02
  172. (DG RECTANGLESIZE (BOX:GRAPHICSBOX)
  173. (BOX:SIZE _ BOX:CONTENTSSIZE + (A VECTOR WITH X = 10 Y = 10))(
  174. BOX:CONTENTSOFFSET _ (A VECTOR WITH X = 6 Y = 6)))
  175. % GSN 14-JAN-83 14:35
  176. (DG STRINGDATA-DRAW (self:LISPNODEDISPLAY POS:VECTOR W:WINDOW)
  177. (SEND W PRINTAT self:CONTENTS POS))
  178. % GSN 14-JAN-83 14:42
  179. % Draw the subtree beginning with TREE inside area AREA in window W.
  180. (DG TREEELEMENT-DRAWIN (TREE:TREEELEMENT AREA:REGION W:WINDOW)
  181. (PROG (NEWX NEWY SUM FSPN TB)
  182. (IF TREE:DISPLAYSIZE>AREA:SIZE THEN (ERROR 0
  183. "Area is too small for tree."))
  184. (TB:START _ (A VECTOR WITH X = (AREA:LEFT + AREA:RIGHT - TB:SIZE:X)
  185. /2 Y = AREA:TOP - TB:SIZE:Y))
  186. (SEND TB DRAWIN W)
  187. (SEND TB:DISPLAYCONTENTS DRAW TB:START+TB:CONTENTSOFFSET W)
  188. % Now compute positions for successors of top node.
  189. (IF TREE:SUCCESSORS THEN (NEWY _ AREA:TOP - TB:SIZE:Y - 20)
  190. (SUM_0)
  191. (FOR S IN TREE:SUCCESSORS DO SUM_+S:DISPLAYSIZE:X)
  192. % Calculate free space for each box.
  193. (FSPN _ (AREA:SIZE:X - SUM)
  194. /
  195. (LENGTH SUCCESSORS))
  196. (NEWX _ AREA:START:X + FSPN/2)
  197. % Draw each subtree.
  198. (FOR S IN TREE:SUCCESSORS DO
  199. % Draw arc to new subtree.
  200. (SEND W DRAWLINE TB:BOTTOMCENTER
  201. (A VECTOR WITH X = NEWX+S:DISPLAYSIZE:X/2 Y = NEWY))
  202. (SEND S DRAWIN
  203. (AN AREA WITH START =
  204. (A VECTOR WITH X = NEWX Y = AREA:START:Y)
  205. SIZE =
  206. (A VECTOR WITH X = S:DISPLAYSIZE:X Y = NEWY -
  207. AREA:START:Y))
  208. W)
  209. (NEWX_+S:DISPLAYSIZE:X+FSPN)))))
  210. % GSN 2-FEB-83 17:37
  211. (DG TREEELEMENT-SELECTNODE (TE:TREEELEMENT V:VECTOR)
  212. (PROG (RESULT LST TMP)
  213. (IF (SEND TE:BOX CONTAINS? V)
  214. THEN
  215. (RETURN TE)
  216. ELSEIF
  217. (SEND TE:TOTALAREA CONTAINS? V)
  218. THEN
  219. (LST_TE:SUCCESSORS)
  220. (WHILE ~RESULT AND (TMP-_LST)
  221. DO
  222. (RESULT _ (SEND TMP SELECTNODE V)))
  223. (RETURN RESULT))))
  224. (GLISPGLOBALS
  225. (GRAPHICSBOXTYPES (LISTOF BOXTYPE))
  226. )
  227. (PUT 'RECTANGLE
  228. 'DRAWPROGRAM
  229. 'DRAWRECTANGLE)
  230. (PUT 'CIRCLE
  231. 'DRAWPROGRAM
  232. 'DRAWGRCIRCLE)
  233. (PUT 'ELLIPSE
  234. 'DRAWPROGRAM
  235. 'DRAWGRELLIPSE)
  236. (PUT 'RECTANGLE
  237. 'SIZEPROGRAM
  238. 'RECTANGLESIZE)
  239. (PUT 'CIRCLE
  240. 'SIZEPROGRAM
  241. 'CIRCLESIZE)
  242. (PUT 'ELLIPSE
  243. 'SIZEPROGRAM
  244. 'ELLIPSESIZE)
  245. (SETQ GRAPHICSBOXTYPES '(RECTANGLE))
  246. (SETQ TX '(/(+(- B)
  247. (SQRT (-(^ B 2) (* 4 (* A C))
  248. ))) (* 2 A)
  249. ))