Re: NSTextView attachments and context menus
Re: NSTextView attachments and context menus
- Subject: Re: NSTextView attachments and context menus
- From: Keith Blount <email@hidden>
- Date: Tue, 9 Mar 2010 11:15:55 -0800 (PST)
Hi Martin,
Does something like the following not work for you?
NSTextAttachment *attachment = [[textView textStorage] attribute:NSAttachmentAttributeName atIndex:charIndex effectiveRange:NULL];
if (attachment)// Possibly check it’s of the kind of attachment you want to handle, too
return myCustomMenu;
I just did a quick test and this seemed to work fine with an image attachment. (As I say, I override -menuForEvent: for Tiger support, but I do pretty much the same there, only having to do the extra work and convert the event's -locationInWindow to an index manually.)
All the best,
Keith
----- Original Message ----
From: Martin Hewitson <email@hidden>
To: Keith Blount <email@hidden>
Cc: Martin Hewitson <email@hidden>; email@hidden
Sent: Tue, March 9, 2010 6:58:15 PM
Subject: Re: NSTextView attachments and context menus
Hi Keith,
> As (the other) Martin says, you can subclass NSTextView and override -menuForEvent: for this, which is the best way of doing it if you need to provide support for systems running versions of OS X earlier than Leopard. If you only need to support Leopard or above, though, Leopard introduced a delegate method which should do the same thing and obviate the need to subclass:
>
> - (NSMenu *)textView:(NSTextView *)view menu:(NSMenu *)menu forEvent:(NSEvent *)event atIndex:(NSUInteger)charIndex
>
Yes, I went down this route a little, but I was unable to figure out how to check for an attachment at that character index. I guess I need to look at this again. It seems like the elegant way to go since I only support Leopard and above.
Thanks again,
Martin
> So you should just be able to check for an attachment at charIndex and if one is detected return your own custom menu; otherwise return the standard menu that is passed in. (Note that I've never used this delegate method myself, though, as my app still needs to support Tiger, so I use Martin's way.)
>
> All the best,
> Keith
>
> --- Original Message ---
>
> Dear list,
>
> I have an NSTextView which support dragging files in, either to create a link to the file, or to add the file as an attachment. So far so good.
>
> Now I want to offer the user a context menu to perform operations on the attachment (open, save, etc). So far I was unable to find a way to intercept a 'right-click' on the nstextview to offer a custom context menu. What I did get working is the single left-click version by implementing textView:clickedOnCell:inRect:atIndex: in the text view's delegate. In that method I create a context menu and show it at the mouse location using NSMenu's popUpContextMenu:withEvent:forView:. That works, but with one problem. The context menu that appears has two additional menu items: "Import Image" and "Capture Selection from Screen". So I have three questions:
>
> 1) Is there a better way to achieve what I want?
> 2) Where do these additional menu items come from? Are they services?
> 3) How could I do this with a right-click instead of a single-click?
>
> I have another question about attachments, but I'll post that separately.
>
> Thanking you in advance,
>
> Martin
>
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Martin Hewitson
Albert-Einstein-Institut
Max-Planck-Institut fuer
Gravitationsphysik und Universitaet Hannover
Callinstr. 38, 30167 Hannover, Germany
Tel: +49-511-762-17121, Fax: +49-511-762-5861
E-Mail: email@hidden
WWW: http://www.aei.mpg.de/~hewitson
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden