[Openmcl-devel] Two quick questions

Ralf Stoye stoye at stoye.com
Sun Nov 4 22:07:34 UTC 2007

Am 04.11.2007 um 20:12 schrieb Ron Garret:
> 1.  What is the difference between marked and selected text in an
> NSTextInput object?

Documentation on this Topic:

found this by googling for "cocoa selected text marked text", sixth  
link was inside Apple's Documentation: ("Text Editing Programming  
Guide for Cocoa") and seemed appropriate, there i found:
"An additional aspect of selection, related to input management, is  
the range of marked text. As the input manager interprets keyboard  
input, it can mark incomplete input in a special way. The text view  
displays this marked text differently from the selection, using  
temporary attributes that affect only display, not layout or storage.  
For example, NSTextView uses marked text to display a combination  
key, such as Option-E, which places an acute accent character above  
the character entered next. When the user types Option-E, the text  
view displays an acute accent in a yellow highlight box, indicating  
that it is marked text, rather than final input. When the user types  
the next character, the text view displays it as a single accented  
character, and the marked text highlight disappears. The markedRange  
method returns the range of any marked text, and markedTextAttributes  
returns the attributes used to highlight the marked text. You can  
change these attributes using setMarkedTextAttributes:."

Btw, I think that most questions regarding cocoa are easily answered  
by working through some tutorials or by looking up the documentation   
provided by apple.
One good source of information is installing XCode and ask its  
"Documentation" or read articles on cocoadevcentral.com or similar  
eg. how to create a window, how to attach some controls, how to write  
a draw-view-contents function,...

Here some code i wrote some years ago (2005) - maybe out of date and  
*surely* very dirty - but it shows some bits about "how to use cocoa":
It creates a window with a rect ( called redball) wich is adjustable  
by a slider and a colorcontrol.
The problem to draw a circle instead of an rectangle is left to the  
reader ;-)
(hint: we want to create a spline -, search for spline in the web,  
find NSBezierPath , search for Bezier in xcode, find  
bezierPathWithOvalInRect, maybe draw it with -stroke, ...)

------------------------- snip: contents of file "redball.lisp"
;;;; -*- Mode: Lisp; Package: CCL -*-

;;; redball.lisp
;;; one of the apple examples converted...
;;; a window with a frame, change location, size and color
;;; test by loading this file and evaluate
;;; (ccl::ball-setup)

(in-package "CCL")

(eval-when (:compile-toplevel :load-toplevel :execute)
   (require "COCOA"))

;; ignoring the mvc we put the model into the view objects...
(defclass ball-view (ns:ns-view)
   ((ball-center :accessor ball-view-center :foreign-type :<NSP>oint)
    (ball-radius :accessor ball-view-radius :foreign-type :float)
    (ball-color  :accessor ball-view-color  :foreign-type :id))
   (:metaclass ns:+ns-object))

;; init for cocoa
(define-objc-method ((:id :init-with-frame (:<NSR>ect frameRect))  
   (let ((self (send-super :init-with-frame frameRect)))
     ;;(format t "~% :init-with-frame was called")
     (unless (%null-ptr-p self)
       (setf (ball-view-center self) (make-record :<NSP>oint :x  
30.0 :y 30.0)
	    (ball-view-radius self) 15.0
	    (ball-view-color self) (send (@class ns:ns-color) 'blue-color))

;; release:
;; since we allocate the ball-view-center (nspoint record) we have to  
release it
(define-objc-method ((:void dealloc) ball-view)
   (format T "~%deaallocing ~p" self)
   (when (typep (ball-view-center self) 'macptr)
     (CCL::FREE (ball-view-center self))
     (setf (ball-view-center self) nil))
   (send-super 'dealloc))

;; define the function called by the system to draw our ball-view:
(define-objc-method ((:void :draw-rect (:<NSR>ect rect)) ball-view)
   (with-slots (ball-center ball-radius ball-color) self
     (let* ((rect-x (- (rref ball-center :<nsp>oint.x)
	   (rect-y (- (rref ball-center :<nsp>oint.y)
	   (rect-wh (* 2 ball-radius)))
       (slet ((ballrect (ns-make-rect rect-x rect-y rect-wh rect-wh)))
	;; fill view white
	(send (the ns-color (send (@class ns-color) 'white-color)) 'set)
	(#_NSRectFill rect)
	;; draw ball in its color
	(send (the ns-color ball-color) 'set)
	(send (@class ns-bezier-path) :stroke-Rect ballrect)))))

;; model: ball-set-radius
;; historically misnamed ( do-it)
(define-objc-method ((:void :do-it (:id sender)) ball-view)
   ;(format T "~%hello with ~a" (send sender 'double-value))
   (setf (ball-view-radius self) (send sender 'float-value))
   (send self :set-Needs-Display t))

;; model: set-center of ball
(define-objc-method ((:void :mouse-down (:id event))
   (slet* ((event-location (send event 'location-In-Window))
	  (view-location (send self :convert-Point event-location :from-View  
    ;(format T "~%got mda with x:~d and y:~d" (pref view- 
location :<NSP>oint.x) (pref view-location :<NSP>oint.y))
     (let ((pt (ball-view-center self)))
       (setf (pref pt :<nsp>oint.x) (pref view-location :<NSP>oint.x)
	    (pref pt :<nsp>oint.y) (pref view-location :<NSP>oint.y))
       (send self :set-Needs-Display t))))

;; model: set-color
(define-objc-method ((:void :ball-view-set-color (:id sender))
   (send (ball-view-color self) 'release)
   (setf (ball-view-color self) (send (send sender 'color) 'retain))
   (send self :set-Needs-Display t))

;; let the user drag the center of our ball...
(define-objc-method ((:void :mouse-Dragged (:id event))
   (slet* ((event-location (send event 'location-In-Window))
	  (view-location (send self :convert-Point event-location :from-View  
    ;(format T "~%got mda with x:~d and y:~d" (pref view- 
location :<NSP>oint.x) (pref view-location :<NSP>oint.y))
     (let ((pt (ball-view-center self)))
       (setf (rref pt :<nsp>oint.x) (pref view-location :<NSP>oint.x)
	    (rref pt :<nsp>oint.y) (pref view-location :<NSP>oint.y))
       (send self :set-Needs-Display t))))

;; just for debugging: ball-setup puts some objc objects into this  
vars, so we can peek around
;; eg. you can (send *last-ball-view* :do-it (2.0)

(defvar *last-ball-window* nil)
(defvar *last-ball-view* nil) ;; will contain the last generated
(defvar *last-slider* nil)

;; setup things normally setup via a nib-file:
(defun ball-setup ()
     (slet ((wr (ns-make-rect 100.0 350.0 400.0 400.0)) ; window
	   (vr (ns-make-rect 0.0 30.0 400.0 370.0)) ; ball-view
	   (sr (ns-make-rect 10.0 0.0 350.0 30.0)) ; slider
	   (cwr (ns-make-rect 360.0 3.0 25.0 25.0))) ; colorwell
       (let ((w (make-instance
		:with-content-rect wr
		:style-mask (logior #$NSTitledWindowMask
		:backing #$NSBackingStoreBuffered
		:defer t)))
	(setf *last-ball-window* w)
	(send w :set-title #@"Ball Window")
	;; (inspect (send w 'content-view))
	(let ((c-view (send w 'content-view))
	      (my-view (make-instance 'ball-view :with-frame vr))
	      (my-slider (make-instance 'ns:ns-slider :with-frame sr))
	      (my-colorwell (make-instance 'ns:ns-color-well :with-frame cwr)))
	  (setf *last-ball-view* my-view
		*last-slider* my-slider)
	  (send my-slider :set-target my-view)
	  (send my-slider :set-action (@selector "doIt:"))
	  (send my-slider :set-min-value 10.d0)
	  (send my-slider :set-max-value 100.d0)
	  (send my-colorwell :set-target my-view)
	  (send my-colorwell :set-action (@selector "ballViewSetColor:"))

	  (send c-view :add-subview my-view)
	  (send c-view :add-subview my-slider)
	  (send c-view :add-subview my-colorwell)
					; (send w :set-delegate my-view)) ;; for what??

	  (send w :make-key-and-order-front nil)

;; what about:
(defmacro tell (object &rest messages)
   (let ((code (mapcar #'(lambda (mess) `(send ,object , at mess))  
     `(progn , at code)))

(tell my-slider
       (:set-target my-view)
       (:set-action (@selector "doIt:")))

; (ccl::ball-setup)
; (send *last-slider* 'double-Value)

------------------------- end snip: contents of file "redball.lisp"

Ralf Stoye

More information about the Openmcl-devel mailing list