How I add a mouse right-down menu to the NSCollectionViewItem - objective-c

I have a question.How I add a mouse right-down menu to the NSCollectionViewItem.
As an attempt I alse use the Apple's demo app IconCollection.I tryed drag a NSMenu to the IconViewPrototype.xib and connect it to the view's menu outlet in IB.but when build and run,click the mouse right click,nothing happened.I think the NSBox also a subclass for NSView,the mouse right-down menu should be support.

I ended up creating an NSView subclass to use as a View for the CollectionViewItem. In there I set a delegate (connected in IB), and used this to catch the right mouse click and open the menu:
-(void)rightMouseDown:(NSEvent *)theEvent {
NSMenu *menu = [self.delegate menuForCollectionItemView:self];
[menu popUpMenuPositioningItem:[[menu itemArray] objectAtIndex:0]
atLocation:NSZeroPoint
inView:self];
}
This still needs some code to position the menu where the user clicked, but it's a start.
If anyone has a cleaner method I'd love to hear it.

Related

NSEvent from a NSButton

I have a NSTableView and each row contains a button. I also have a menu associated with the table.
The issue is : I want to show the menu on click of button. If possible do not show on right click.
The action method is :
- (IBAction)showMenu:(NSButton *)button {
NSLog(#"show menu");
NSMenu *menu = [self.tableView menu];
NSEvent *event = [[NSEvent alloc] init];
[NSMenu popUpContextMenu:menu
withEvent:event
forView:button];
}
Here what to do with event? If I use nil then the menu is showed at bottom left corner, not next to the button.
Any guidance would be appreciated.
You could try using -[NSMenu popUpMenuPositioningItem:atLocation:inView:]. This method doesn't take an NSEvent argument. Instead, you give it a view and a location in the view's coordinate system, and the menu positions itself (or one of its items) over that location.
But I would suggest you don't use an NSButton at all. If you use an NSPopUpButton instead, it will take care of showing the menu at the correct location on a left-click.

Xcode Plugin Context Menu

I'm trying to add items to the context menu in Xcode with a plugin. To do this, I created a right click mouse event and used it as a parameter for the menuForEvent method of the contentView of the mainWindow. The context menu is never initialized. The mouse click event is initialized, but I'm not sure if it's initialized correctly. My code is below. Am I at least on the right track? Thanks in advance.
NSEvent* mouseClickEvent =[NSEvent mouseEventWithType:NSRightMouseDown location:[NSEvent mouseLocation] modifierFlags:[NSEvent modifierFlags] timestamp:NSTimeIntervalSince1970 windowNumber:[[NSApp mainWindow] windowNumber] context:[[NSApp mainWindow] graphicsContext] eventNumber:0 clickCount:1 pressure:1.0];
NSMenu* rightClickContextMenu = [[[NSApp mainWindow] contentView] menuForEvent: mouseClickEvent];

How to get location of NSStatusItem on the screen

I have a app with NSStatusItem on the menu bar. Now i want to use shortcut keys to call up the NSWindow which should be displayed right bellow the NSStatusItem. With mouse click i just get the position of status item with:
CGRect eventFrame = [[[NSApp currentEvent] window] frame];
But i have no idea how to get this position if i use shortcut to call the window. The NSStatusItem has a custom view.
A similar question is here How to get the on-screen location of an NSStatusItem but no one could give a solution.

Object under mouseDown COCOA

I have a pretty simple question for which I could not find a simple answer.
When using cocoa (osx, xcode) and a method called "mouseDown" which detects if mouse has clicked on a view, how to detect on which object mouse has clicked? I just need a class name so I can know if the user has clicked on, for example NSImageView, WebView, NSTextView or on a NSView it self? Or even better, if I have two NSImageViews on my NSView, how to detect on which one it was clicked?
Cheers.
In your view mouseDown method, you can call the hitTest: method to get the farthest descendant of the receiver in the view hierarchy that was clicked:
So in your view subclass, you could do something like:
- (void)mouseDown:(NSEvent *)theEvent
{
id clickedObject = [self hitTest:[theEvent locationInWindow]];
if ([clickedObject isKindOfClass:[NSImageView class]]) {
NSLog(#"Clicked an ImageView");
} else if ([clickedObject isKindOfClass:[WebView class]]) {
NSLog(#"Clicked a WebView");
}
}
Your question seems a bit odd though, because normally you don't need to do this hit testing yourself.
If you're trying to get a click event when a particular image is clicked, a better way would be to use a borderless button with an image set and then implementing an action method and connecting that to the button.

Show NSWindow on right click in NSTableView

I'd like to display an NSWindow when right clicking an item in an NSTableView, similarly to how the available outlets are shown in Interface Builder when you right click an object:
Unfortunately you can only use an NSMenu subclass as the menu property.
I also didn't find a delegate method of NSTableView that notifies about right clicks.
I was able to subclass NSTableView and implement rightMouseDown: and rightMouseUp: to be notified about those events, but if I set the menu property of the row cells to nil, they are not highlighted when right clicked, even though I call the super implementation):
- (void)rightMouseDown:(NSEvent *)theEvent {
[super rightMouseDown:theEvent];
NSPoint eventLocation = [theEvent locationInWindow];
eventLocation = [self convertPoint:eventLocation fromView:nil];
NSInteger rowIndex = [self rowAtPoint:eventLocation];
NSLog(#"Right clicked at row index %d", rowIndex);
}
I would like to have the highlight effect in the image below but display a window instead of the context menu:
First for the right click: explicitly select the row on right click (e.g. via this message). Then create your own NSWindow descendant, set an own NSView class as contentView and in the view you can draw the black background, rounded borders and what not. Show this window in your right click handler.
You can use an NSPopover, which works quite nicely. A popover creates a window for you, even if it is somewhat hidden. You'll get it from your controls if you send them the window message, and can register to listen for events, for instance.
The whole popover can be created in IB, and just have to implement the showRelativeToRect:ofView:preferredEdge: method in code.
To catch the right click event, you can use rightMouseDown:, which is originally defined in NSResponder, but is overridden in NSView to simply catch the event and show menu and it doesn't pass the event upwards in the responder chain (or the inheritance chain, for that matter). Hence, you simply implement that method to call showRelativeToRect:ofView:preferredEdge:.
You will typically need to have the contents in an NSViewController and its own accompanying nib file.
The NSPopover's contentViewController property can be set in IB, too.
All in all, not much code needed.
This tutorial is useful.