Re: Buttons inside an NSTextView / NSTextStorage?
Re: Buttons inside an NSTextView / NSTextStorage?
- Subject: Re: Buttons inside an NSTextView / NSTextStorage?
- From: Douglas Davidson <email@hidden>
- Date: Wed, 26 Oct 2005 11:23:03 -0700
On Oct 25, 2005, at 3:11 PM, Keith Blount wrote:
I am currently trying to implement inline comments in
an NSTextView in a similar way to how LyX does it. I
would like an NSButton to appear inline in the text
wherever a note occurs. If the user clicks on the
button, the note will appear next to it. If the user
clicks on the button again, the text will disappear. I
can get the text to appear and disappear using the
usual text replacement methods and some custom text
attributes, but my problem is, how do I get an
NSButton to appear inside the text and react to
clicks? I am pretty sure that I need to use
NSTextAttachment and NSTextAttachmentCell, but upon
searching the net, the archives, the docs and
CocoaDev, it seems that there is not much out there on
how I would go about this (there are a *lot* of
questions out there in the ether, just no real answers
as far as I can find; the only stuff out there on
NSTextAttachments relate to inserting images, which is
a lot easier and involves none of the tough
subclassing/protocol stuff...).
NSTextAttachment holds the contents of an attachment; it is the value
of the NSAttachmentAttributeName attribute for the
NSAttachmentCharacter in the attributed string. The contents are
usually given by an NSFileWrapper, but this is not required; you can
create an empty NSTextAttachment with a nil file wrapper.
NSTextAttachmentCell handles display and user interaction for the
attachment. By default an NSTextAttachment will create an
NSTextAttachmentCell to display itself, depending on the contents of
the attachment; in the generic case this will just be an image cell
displaying an icon.
If you wish, however, you can supply a custom NSTextAttachmentCell
for your attachment. It need not be an member of the class
NSTextAttachmentCell; it only needs to conform to the
NSTextAttachmentCell protocol. Actually, even that is not strictly
necessary; it only needs to implement a few of the basic methods for
sizing and drawing. Most cells already do this.
You will, however, need to deal with mouse events yourself. The
methods you'll probably want to implement would be
wantsToTrackMouseForEvent:inRect:ofView:atCharacterIndex: and
trackMouse:inRect:ofView:atCharacterIndex:. The character index here
should let you determine which portion of the text is relevant.
Douglas Davidson
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden