How to catch click event on NSImageView as subview in NSTableRowView? - cocoa

In NSTableView I have NSTableRowView and in this row there is some NSImageView subview.
Question: how to track click event on this image?

You would need to create NSImageView subclass to handle mouse events. But NSTableView does not allow handling -[NSImageView mouseUp:] and other mouse messages. (I think this is related to how a table handles selection).
In this situation I had to use a borderless "Bevel - Switch" button to handle clicks.

Related

cocoa: NSView touch event

I created a simple cocoa project, and added an NSButton in the window.
Then I added an NSScrollView to the window and hided the NSButton.
However, when I click the scroll view , it is strange that the NSButton action responds!
I guess there is something with the touch event chains, but I failed to find it.
For example, I try to use:
- [NSView becomeFirstResponder];
- [NSView setAcceptsTouchEvents:];
SO what I want is the only the front-most view to become the first responder, and the touch event will not be sent to its superview or so.
Thanks.
This is the view hierarchy:
the scroll view and button are both added to the window view, and the scrollview's frame includes the button's frame. In other words, the button is hidden by the scroll view but still receives click events.
You need to add mouseDown: event in NSScrollView or NSCrollView's View. like this:
-(void)mouseDown:(NSEvent *)theEvent {
NSLog(#"MouseDown in NSView");
}

NSResponder mouseExited when hovering Window title

I have an NSWindow with an NSToolbar and a content view.
I have set the tracking area to be the whole frame of the view.
I wish to have the mouseExited event fired when the cursor leaves the content view and enters the toolbar and/or the window title. What is the best way to achieve this? should I bound the tracking area to just below the toolbar?
The main reason I need this is because my view needs a special cursor. So I'm changing it in mouseEntered and wish to change it again when mouseExited
Thanks
Why not add a cursor rectangle over the entire bounds of the view?

How to pass scroll events to parent NSScrollView

I need fixed-size NSTextViews inside a larger scrolling window. IB requires that the textviews be inside their own NSScrollViews, even though their min/max sizes are fixed so that they won’t actually scroll. When trackpad gestures are made within the textview frames (regardless of whether they have focus), they are captured by the textviews’ scrollviews, so nothing happens.
How do I tell the textviews’ scrollviews to pass scroll events up to the window’s main scrollview? (Or perhaps I should be asking how I tell the window’s main scrollview to handle these events itself and not pass them on to its child scrollviews.)
The IB structure is like this:
window
window’s content view
big scrollview for window (desired target for scroll events)
box
swappable content view in separate xib
scrollview for textview
textview
And, yes, the window does scroll correctly when the textviews do not have focus.
You needn't create a outlet "svActive" to track your super scrollview. Just write this sentence in scrollWheel event:
[[self nextResponder] scrollWheel:event];
this will pass the event to next responder in the responder chain.
IB does not require you have a text view inside a NSScrollView; this is just the default, because most of the time you'll want your view to scroll. Select the NSTextView and choose Layout > Unembed Objects. Note that after this point, you can no longer move or resize your view in IB. This seems to be a bug.
Here's an example of how to put two NSTextViews in a single NSScrollView.
Add two text views next to each other; put some text in them so you can see what's happening.
Select the views; choose Layout > Embed Objects In > Scroll View. This puts them in a generic NSView inside a NSScrollView.
Select the text views; choose Layout > Unembed Objects.
Turn off the springs and struts (autosizing) for each text view, so they don't resize when you shrink the scroll view.
Take note of the height of the document view (here it's 175).
Make the scroll view smaller. This also resizes the document view (NSView).
Restore the document view to its original size (I set the height back to 175).
Done! Scrolling works as you'd expect.
This is really embarrassing. After weeks of putting it off, I made a first attempt to get a subclassed NSScrollView to behave passively — and it turned out to be a no brainer.
Here’s the subclass:
h file:
#import <Cocoa/Cocoa.h>
#interface ScrollViewPassive : NSScrollView {
// This property is assigned a ref to windowController’s main scrollview.
NSScrollView *svActive;
}
#property (nonatomic, retain) NSScrollView *svActive;
#end
m file:
#import "ScrollViewPassive.h"
#implementation ScrollViewPassive
#synthesize svActive;
// Pass any gesture scrolling up to the main, active scrollview.
- (void)scrollWheel:(NSEvent *)event {
[svActive scrollWheel:event];
}
#end
There’s no need to make outlets for these passive scrollviews; I give them their refs to the main scrollview right after their xibs are assigned as content to the NSBox:
[self.boxDisplayingTextViews setContentView:self.subviewCtllr1.view];
// A textview's superview's superview is its scrollview:
((ScrollViewPassive *)[[self.subviewCtllr1.textview1 superview] superview]).svActive = self.scrollviewMain;
That’s it. Works like a charm.
I find that IB3 and Xcode4 both fight you if you try to do this directly, but you can do it indirectly. First, drag the textview out of the scrollview and delete the scrollview. You'll wind up with an orphaned textview. I don't know any way to get IB to allow you to put this into your window, but it'll be in your NIB. Now, attach an IBOutlet to it, and at runtime do a addSubview: and adjust its frame to move it into whatever scrollview you wanted it to be in.
In my experience, NIBs are a good thing, but every complex NIB I've ever worked with needed some final rearranging in code.
Based on #phaibin's answer, here's how you'd do it in Swift 4.2.
First, subclass NSScrollView and override scrollWheel:
class ScrollThingy: NSScrollView{
override func scrollWheel(with event: NSEvent) {
self.nextResponder?.scrollWheel(with: event)
}
}
Then place the ScrollThingy class (or whatever you name it) on the NSScrollView that is wrapped around your NSTextView. The parent NSScrollView will get the scroll event thereafter.

Cocoa - Giving focus to a WebView in an NSStatusItem

I've set the view for my NSStatusItem to a WebView, but I'm not able to capture hover events in the WebView because my NSStatusItem doesn't get focus, like a normal WebView would.
How do I programmatically give my NSStatusItem or its associated view focus so that the embedded WebView will capture mouse events like a normal WebView?
In case anyone cares, one way I can think of is to have the view for the NSStatusItem be a custom NSView that overrides the NSResponder methods mouseEntered:, mouseMoved: and mouseExited:. Then, I would add the WebView as a subview of the custom NSView, and each time the mouse moves inside the NSView, send the coordinates of the mouse to the Javascript in the WebView, which can figure out what element is at the position of the mouse cursor.
Basically, it would be reimplementing mouse hover at its core. Hopefully there's an easier way though.

MouseDragged not released when dragging on other views

I have 2 subclasses of NSView that are subviews to a common superview. They dont overlap and they both intercept mousedragged calls. When I drag from one of the subclasses to the other the mousedragged function will be called until I release the mouse button even when I drag all over the screen. I though the default behavior was for the mousedragged function to be called only when the mouse was over the bounds of the receiver.
Iam also using NSTrackingArea for mouse enter, exit and move events, but from what I've been reading does not involve drag events
Thank you for your time,
Jose.
You could subclass the NSWindow and override sendEvent:. That way, you can intercept the NSLeftMouseDragged events and dispatch them in whatever way you wish.

Resources