How do I force an NSPopUpButtonCell to redisplay itself? - cocoa

I'm creating a custom subclass of NSPopUpButton and NSPopUpButtonCell to display a lone icon on top of the button instead of the usual text.
To do this I'm overriding
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
in my button cell subclass to draw my icon at the appropriate size and location. When the user mouses over the button I then want to change the image. My only problem is I can't figure out how to make the button cell redisplay (by calling drawInterior... again). By default it only seems to redisplay when the button is clicked or when focus is moved to another window. I've tried setNeedsDisplay:YES but that doesn't seem to do it.

The way I found to do this was to insert a call to
[self updateCell:self.cell];
inside my NSPopUpButton subclass immediately after any point I changed the image displayed by the cell.

Related

Disabling the NSTableView row focus ring

How do you disable the focus ring around an NSTableView row when the user right-clicks on it? I can't get it to disappear. Setting focus ring of an individual NSTableViewCell in the table to None has no effect.
Subclass the table view and implement the following method:
- (void)drawContextMenuHighlightForRow:(NSInteger)row {
// do nothing
}
Note:
The blue outline is not the focus ring.
This is an undocumented private method Apple uses to draw the outline. Providing an empty implementation will prevent anything from being drawn, but I am not 100% sure that whether it can pass the review.
New:
Here is how I did it.
You can handle the menu manually. Subclass NSTableRowView or NSTableCellView, then use rightMouseDown: and mouseDown: (check for control key) and then notify your tableViewController (notification or delegate) of the click. Don't forget to pass the event as well, then you can display the menu with the event on the table view without the focus ring.
The above answer is easier, but it may not pass the review, as the author mentioned.
Plus you can show individual menu items for each row (if you have different sorts of views)
Old:
I think the focus ring is defined by NSTableRowView, not NSTableCellView, because it is responsible for the complete row. Try to change the focus ring there. You can subclass NSTableRowView and add it to the tableView via IB or NSTableViewDelegate's method:
- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
If your goal is just displaying a contextual menu, you also can try this.
Wrap the NSOutlineView within another NSView.
Override menuForEvent method on the wrapper view.
Do not set menu on outline-view.
Now the wrapper view shows the menu instead of your outline-view, so it won't show the focus ring. See How do you add context senstive menu to NSOutlineView (ie right click menu) for how to find a node at the menu event.

How make button in NSWindow clickable while a sheet's on top of it

I have created a custom (themed) NSWindow, by creating a borderless window and then recreating all elements of the window border/background inside the content view. I've created the window widgets (close box, zoom box, minimize box) on top of my own fake title bar using -standardWindowButton:forStyleMask:.
Trouble is, when a sheet is presented on top of my custom window (e.g. "save changes...", those buttons do not receive the clicks.
Does anybody know how I can tell NSWindow not to intercept the clicks in my minimize box? It works with a standard NSWindow. When a sheet is up, I can still send both of them to the dock, or zoom the window out.
I thought maybe there's special code in the content view that ignores clicks in subviews while a sheet is up. But it seems as if -hitTest: is called on the content view and returns the minimize widget, but the widget's action never gets triggered.
I guess I could just replace the content view and perform the action in the content view's hitTest if it is the minimize widget ... but that seems a bit ugly.

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");
}

Hiding and showing different scrollviews

So I have two scroll views that I want my XIB to show.
My first scroll view is loaded with the XIB and I want the second to be loaded when a button is pressed.
Does any body know how I can do this?
UIScrollView inherits from UIView. All you have to do is set it's hidden property to hidden. It is a simple set method. By default it will display as long as you add the view.
On your click action or method triggered simply set the property for the UIScrollView like:
[myScrollView setHidden:NO];
And if you want it to disappear again, on a different action:
[myScrollView setHidden:YES];

NSPopUpButtonCell inside custom cell does not pop up when clicked

I've created a custom subclass of NSCell with an NSImageCell, some NSTextFieldCells, and an NSPopUpButtonCell.
I'm initializing the pop up cell using:
myPopUpCell = [[NSPopUpButtonCell alloc] init];
[myPopUpCell setBordered:NO];
[myPopUpCell setAutoenablesItems:NO];
[myPopUpCell addItemsWithTitles:[NSArray arrayWithObjects:#"Item1", #"Item2", #"Item3"]];
And drawing it in drawInteriorWithFrame:inView:
Everything seems to work great, except that when clicking on the pop up cell while running my app the cell does not pop up. Any suggestions about what might be wrong?
Drawing the pop-up button cell in drawInteriorWithFrame:inView: is going to do just that; draw it, but nothing else. Handling click events is unrelated to drawing, so you're going to have to do some work in your custom cell to interpret mouse events, and if they're inside the frame you're using for the pop-up button, pass them on to the button cell. Start by subclassing the methods listed in the NSCell docs under tracking the mouse, such as –trackMouse:inRect:ofView:untilMouseUp:, and you should be be able to figure out what's needed to make the button cell act correctly.
Depending on what you're doing you may actually find it easier to draw the title string yourself, and just use NSMenu's +popUpContextMenu:withEvent:forView:.

Resources