|
From: John L. <jo...@jo...> - 2004-10-17 07:24:35
|
I've been doing quite a bit of development with ruby-cocoa, and
completely enjoying the experience!
However, I'm having difficulty dealing with the Cocoa structures like
NSRect, NSRange, and so on. I know there are Ruby classes to manage
these, and they generally work fine. But I find that I cannot pass
those objects directly to the Cocoa methods that use them; instead, I
must call #to_a. For example:
range = self.selectedRange
# at this point, range is an NSRange object
textStorage.removeAttribute(OSX.NSKernAttributeName,
:range, range.to_a) # <-- .to_a needed here!
The same thing seems to be true with NSRect, and probably with any
struct-based "object" (I realize these aren't first-class objects).
Tonight, I ran into a related problem: NSLayoutManager's
-lineFragmentRectForGlyphAtIndex:effectiveRange: method needs a
*pointer* to an NSRange struct. I tried many possibilities, and
finally ended up with this:
# set up a dummy range to have somewhere to store the actual range
range = [0,0].pack('II')
rect = layoutManager.lineFragmentRectForGlyphAtIndex(i,
:effectiveRange, range)
range = NSRange.new(range.unpack('II'))
This seems neither elegant nor portable, although it does work.
Is there a better way to do this?
PS: I have a sense of deja vu when using Ruby with Cocoa. Back in the
mid-1980s, I programmed classic Mac OS (5? 6?) using Think C. All the
Apple reference material was in Pascal, so I got to be pretty good at
*reading* Pascal, but no good at writing it. I feel much the same
nowadays: I can't write very good Obj-C programs, but I can read enough
of it to translate it to Ruby. :)
--
John Labovitz Consulting, LLC
http://mac.johnlabovitz.com
jo...@jo...
AIM/iChat: jslabovitz
+1 503.949.3492
|