home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / lisp / mcl / 1584 < prev    next >
Encoding:
Text File  |  1992-11-13  |  3.7 KB  |  89 lines

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!sdd.hp.com!zaphod.mps.ohio-state.edu!uwm.edu!rpi!usc!elroy.jpl.nasa.gov!ames!data.nas.nasa.gov!taligent!apple!cambridge.apple.com!bill@cambridge.apple.com
  2. From: bill@cambridge.apple.com (Bill St. Clair)
  3. Newsgroups: comp.lang.lisp.mcl
  4. Subject: Re: Working with pointers
  5. Message-ID: <9211131636.AA15247@cambridge.apple.com>
  6. Date: 13 Nov 92 17:41:25 GMT
  7. Sender: info-mcl-request@cambridge.apple.com
  8. Lines: 78
  9. Approved: comp.lang.lisp.mcl@Cambridge.Apple.C0M
  10.  
  11. >I've been playing around all morning, trying to get some resources to work properly.
  12. >I need a pointer to an icon (for an icon-dialog-item) so I used the resource package
  13. >to get a handle, but when I try to use the pointer, all sorts of problems occur.
  14. >
  15.  
  16. >(with-open-resource-file (*my-rsrcs* "Application.rsrc")
  17. >  (defparameter *icon-hndl-1* (get-resource "ICON" "Telephone"))
  18. >  (defparameter *icon-hndl-2* (get-resource "ICON" "Honda")))
  19.  
  20. Here's your first problem. WITH-OPEN-RESOURCE closes the resource
  21. file when it exits. This will deallocate (e.g. #_DisposeHandle) any
  22. resources that belong to that file. If you want to close the
  23. file and keep the resources, you'll have to call DETACH-RESOURCE on them:
  24.  
  25. (defvar *icon-hndl-1*)
  26. (defvar *icon-hndl-2*)
  27. (with-open-resource-file (*my-rsrcs* "Application.rsrc")
  28.   (detach-resource (setq *icon-hndl-1* (get-resource "ICON" "Telephone")))
  29.   (detach-resource (setq *icon-hndl-2* (get-resource "ICON" "Honda"))))
  30.  
  31. >  I next try to get a pointer to use by:
  32. >
  33. >(when *icon-hndl-1*
  34. >  (#_MoveHHi *icon-hndl-1*)
  35. >  (#_HLock *icon-hndl-1*)
  36. >  (defparameter *my-icon-ptr* (%hget-ptr *icon-hndl-1*)))
  37.  
  38. Two problems here. First off, you should say (%get-ptr *icon-hndl-1*),
  39. not (%hget-ptr *icon-hndl-1*), to get the pointer from a handle. %hget-ptr
  40. does a double indirection, %get-ptr does a single indirection.
  41.  
  42. >
  43. >I should now have a locked, dereferenced pointer, but when I use it to define
  44. >and icon-dialog-item I get into trouble:
  45. >
  46. >(setf my-window (make-instance 'ccl::scrolling-window :color-p t))
  47. >(add-subviews my-window (make-instance 'icon-dialog-item
  48. >                     :icon *my-icon-ptr*
  49. >                     :view-position #@(50 0)))
  50. >
  51. >Inspecting *my-icon-ptr* shows it to be:
  52. >    #<A Mac Handle, Unlocked, Size 40 #x263364>
  53. >as it should be.
  54.  
  55. As you may have noticed, ICON-DIALOG-ITEM's VIEW-DRAW-CONTENTS method
  56. calls PLOT-ICON to actually do the work. PLOT-ICON's documentation says:
  57.  
  58. ;;  plot-icon
  59. ;;
  60. ;;  a function for displaying icons.  It can be passed a pointer or a number
  61. ;;    if passed a pointer, it assumes this is a pointer to an icon record.
  62. ;;    if passed a number, it assumes this is the resource id of an icon.
  63. ;;    Draws to the current grafport, so call it inside WITH-FOCUSED-VIEW.
  64.  
  65. Unfortunately, this is not quite right. Instead of "an icon record" it should
  66. say "an icon handle", which is what the #_PlotIcon & #_PlotCIcon traps expect.
  67.  
  68. Here's a working example:
  69.  
  70. (require "ICON-DIALOG-ITEM")
  71. (require "RESOURCES")
  72.  
  73. (defvar *icon-1*)
  74.  
  75. ; DEF-LOAD-POINTERS so that we can compile the file and SAVE-APPLICATION
  76. ; and the resources will be loaded when the application starts up.
  77. ; (Actually, if you want to save an application, you usually put the
  78. ; icons in the application's own resource fork so that the
  79. ; WITH-OPEN-RESOURCE-FILE and DETACH-RESOURCE are unnecessary [an
  80. ; application's resource fork is always open while it is running].)
  81. (def-load-pointers initialize-icons ()
  82.   (with-open-resource-file (refnum "Application.rsrc")
  83.     (detach-resource (setq *icon-1* (get-resource "ICON" "Loudspeaker")))))
  84.  
  85. (setf my-window (make-instance 'window :color-p t))
  86. (add-subviews my-window (make-instance 'icon-dialog-item
  87.                           :icon *icon-1*
  88.                           :view-position #@(50 0)))
  89.