[Openmcl-devel] Mid-course correction

Ron Garret ron at awun.net
Mon Nov 5 10:40:57 PST 2007

Making good progress on coming up to speed on Cocoa.  Here's my latest  

(defclass test-window () (ns-window ns-view))

(defmethod initialize-instance :after ((w test-window) &rest args)
    (let* ((rect (ns:make-ns-rect 0 0 300 300))
	  (nsw (make-instance 'ns:ns-window
		            :with-content-rect rect
		            :style-mask (logior #$NSTitledWindowMask
			    :backing #$NSBackingStoreBuffered
			    :defer t))
	  (nsv (#/autorelease (make-instance 'empty-view))))
      (setf (slot-value w 'ns-window) nsw)
      (setf (slot-value w 'ns-view) nsv)
      (#/setTitle: nsw #@"Test")
      (#/setContentView: nsw nsv)
      (#/center nsw)
      (#/orderFront: nsw nil)
      (#/contentView nsw))))

(defclass empty-view (ns:ns-view)
   (:metaclass ns:+ns-object))

(setf w (make-instance 'test-window))
(setf v (slot-value w 'ns-view))
(setf b (make-instance 'ns:ns-text-view :init-with-frame (ns:make-ns- 
rect 0 0 300 300)))
(setf ts (#/textStorage b))

(#/addSubview: v b)
(#/insertText: b #@"foo baz bar bing biff")

(setf monaco9 (#/fontWithName:size: (find-class 'ns:ns-font)  
#@"Monaco" 9.0))
(setf helvetica12 (#/fontWithName:size: (find-class 'ns:ns-font)  
#@"Helvetica" 12.0))

(setf fm (#/sharedFontManager (find-class 'ns:ns-font-manager)))
(setf monaco9-bold (#/convertFont:toHaveTrait: fm monaco9 # 
(setf helvetica12-italic (#/convertFont:toHaveTrait: fm helvetica12 # 
(setf helvetica12-bold (#/convertFont:toHaveTrait: fm helvetica12 # 

(#/setSelectedRange: b (ns:make-ns-range 3 5))
(#/addAttribute:value:range: ts #@"NSFont" helvetica12-bold (#/ 
selectedRange b))

It brings up an editor window, inserts some text, and makes some of it  
bold.  Anything I should be doing differently?

I know at least one thing that I'm doing Wrong, namely, making the  
text bold by setting the font to a fixed bold font instead of scanning  
the selected range, pulling out the fonts that are already applied,  
and making them bold.  But this seems like a lot of complexity to do  
something that it seems to me ought to be very simple, so I thought  
I'd post this code and get a mid-course correction before I dove in  

There's another thing that doesn't feel quite right, and that is  
having to gin up both an ns-window and an ns-view and make the ns-view  
be the ContentView of the ns-window.  Back in the good old days of  
MCL, window inherited from view (so you could add subviews directly to  
a window), but ns-window doesn't inherit from ns-view so it seems this  
little jigjog is necessary.  Is that right?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20071105/8de82914/attachment.htm>

More information about the Openmcl-devel mailing list