Re: Buttons inside an NSTextView / NSTextStorage?
Re: Buttons inside an NSTextView / NSTextStorage?
- Subject: Re: Buttons inside an NSTextView / NSTextStorage?
- From: Keith Blount <email@hidden>
- Date: Wed, 26 Oct 2005 15:28:04 -0700 (PDT)
Many thanks for the replies (and an especially big
thanks again to Douglas, who has answered a *lot* of
my questions on the text system and really aided my
understanding of it).
I have got some really good results using a subclass
of NSTextAttachmentCell. I'm using Andreas Meyer's
NSBezierPath additions to draw a very nice-looking
button in -drawWithFrame:inView:, and I'm using
-trackMouse:inRect:ofView:atCharacterIndex: to
determine whether the note should be "expanded" or
"collapsed". If expanded, I just use the regular text
methods to insert the annotation string (held by the
custom cell) inline in the text storage (accessible
via the text view passed in as aTextView) with
temporary display attributes and a custom annotation
attribute. When collapsed, I just look ahead of the
attachment cell for text with the annotation
attribute, save it and remove it from the text...
So far, so good. But there are some issues I would
still like to address. Is there any way, for instance,
that I could select my attachment cell using keyboard
methods? ie. So that if the cursor is at the left of
the cell "button" in the text and I hit the right key,
instead of the cursor moving to the right of the
button, it would select the button first... (I could
then look for "return" key events so that
collapse/uncollapse could be handled through the
keyboard rather than mouse). Generally, I'm just
looking of ways to access an attachment cell via the
keyboard rather than just clicking on it.
Selection-did-change methods are very difficult to use
for this, because you always need to calculate the
direction of the selection change...
Jerome - thanks for your suggestions. I think I am
going to parse text for copying/pasting/exporting in a
manner such as you suggest.
Many thanks,
Keith
P.S. Douglas, if you are still reading, in my searches
on NSTextAttachment, I found a post from you a year
ago stating that you were looking into a bug in which,
if the cursor is inserted next to a text attachment,
typing attributes return to the default ones. This bug
still seems to exist in the latest version of Tiger.
Following a recent(ish) post from an Omni person on
their lists I have fixed this by overriding
-fixAttachmentAttributeInRange: to get rid of only
NSTextAttachment attributes and not font attributes,
but I was wondering if this was going to get fixed by
Apple at all? (Although this is very minor compared to
the NSView redrawing bug that causes text to overwrite
itself - I really hope that bug fix makes it to a
relase soon, but that is veering way OT...) Thanks.
--- Douglas Davidson <email@hidden> wrote:
>
> 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
>
__________________________________
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com
_______________________________________________
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