Programmatically added subviews in table view not responding to mouse down events - cocoa

I have 3 subclasses: a Block class, a Row class and a Table class. All are subclasses of NSView.
I have a Table added with IB which programmatically displays 8 rows, each of which displays 8 blocks. I overrode the mouseDown: method in Block to change the background color to red, but it doesn't work. Still if I add a block directly on top of the Table with IB it does work so I can't understand why it won't work in the first case.
Here's the implementation code for Block and Row (Table's implementation works the same way as Row's):
//block.m
- (void)drawRect:(NSRect)dirtyRect
{
[color set];
[NSBezierPath fillRect:dirtyRect];
}
-(void)mouseDown:(NSEvent *)theEvent
{
color = [NSColor redColor];
checked = YES;
[self setNeedsDisplay:YES];
}
//row.m
- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor blueColor] set];
[NSBezierPath fillRect:dirtyRect];
int x;
for(x=0; x<8; x++){
int margin = x*2;
NSRect rect = NSMakeRect(0, 50*x+margin, 50, 50);
Block *block = [[Block alloc] initWithFrame:rect];
[self addSubview:block];
}
}

Are you aware that NSTableView will use NSCell objects for it's drawing, and not an NSView? If not, investigate NSCell - using that for custom drawing in an NSTable is the way to go.

I understood the problem... since the mouseDown implementation would cause the block to redraw, and so even its superview, it would call Table's drawRect: method causing it to draw new blocks on top of the old ones, and so it would seem never to change color. So I created a property for Table called isFirstAppearance initially set to YES which if YES makes the table draw the rows and sets itself to NO.

Related

Hidden view in NSStackView not hiding?

I have created a vertical NSStackView that contains two NSView subclasses (they are just NSViews that draw a background color). I have the stack view set to detach hidden views. I have set one of the views to be hidden.
Neither view hides in the stack view.
To make sure I'm not insane, I also set up two of the same NSViews next to each other, hiding one. Sure enough, one does hide.
The stack view's distribution is set to Fill Proportionally (not that that seems to matter).
In IB the behavior seems correct; one of the views hides.
I must be missing something incredibly obvious here, right?
In case it is relevant, the NSView subclass:
#import "ViewWithBackgroundColor.h"
#implementation ViewWithBackgroundColor
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
[self.backgroundColor set];
[NSBezierPath fillRect:dirtyRect];
if(self.bottomBorderColor != nil) {
NSBezierPath *linePath = [[NSBezierPath alloc] init];
[self.bottomBorderColor set];
linePath.lineWidth = 2.0;
[linePath moveToPoint:NSMakePoint(0, 0)];
[linePath lineToPoint:NSMakePoint(dirtyRect.size.width, 0)];
[linePath stroke];
}
}
- (NSColor *) backgroundColor {
if (_backgroundColor) {
return _backgroundColor;
} else {
return [NSColor clearColor];
}
}
#end
This looks like an issue with IB and stack view (please file a bug report if you already haven't).
To workaround it you could either:
Don't hide the button in IB, and set it to be hidden at runtime.
or
Uncheck the 'Detaches Hidden Views' stack view property in IB (visible in your screen shot), and set it at runtime with -[NSStackView setDetachesHiddenViews:].

Insertion Point in NSTextField missing

I have an NSTextField in a container:
[textField setFrameOrigin:NSMakePoint(0, -t.frame.size.height)];
content = [[NSView alloc] init];
[content setWantLayer:YES]
content.layer=[CALayer layer];
[content addSubview:textField];
[content scaleUnitSquareToSize:NSMakeSize(1, -1)];
content.frame=textField.frame;
content.layer.backgroundColor=textBGColor.CGColor;
The container itself is located in a view with
[view scaleUnitSquareToSize:NSMakeSize(1, -1)];
This is all for obtaining a top left origin for the TextField and it works great, the only problem consist in the InsertionPoint not drawing (at least not in the visible frame).
I presume the InsertionPoint is either not Scaled or translated with the TextField. Other possibility is that InsertionPoint can't be drawn in a layer backed View.
Is there a way to display the InsertionPoint cursor ?
EDIT
After trying all the possibilities out, it seems the InsertionPoint (and the focusRing) are not drawing because of its frame being positioned out of the superviews bounds and its dirtyDrawRect. Is there a way to remove the clipping of an NSView ? I need to be able to place my TextField on every absolute position possible.
I found a way through: implementing the drawing myself.
1) giving a custom TextView as Editor for the window.
- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
{
if (!myCustomFieldEditor) {
myCustomFieldEditor = [[TextView alloc] init];
[myCustomFieldEditor setFieldEditor:YES];
}
return myCustomFieldEditor;
}
2) Overiding the drawInsertionPoint method in the custom TextView class.
-(void)drawInsertionPointInRect:(NSRect)rect color:(NSColor *)color turnedOn:(BOOL)flag{
[color set];
NSRectFill(rect);
[super drawInsertionPointInRect:rect color:color turnedOn:flag];
}
For insertion point just make your textfield to first responder.
[myTextField becomeFirstResponder];

select a view by clicking osx 10.6

i created a image view
for(int i=0; i<pcount; i++)
{
int x = rand() % 350;
int y = rand() % 350;
NSRect rect = NSMakeRect((x+10),(y+10), 200, 200);
//NSImageView *imageView
imageView1 = [[NSImageView alloc]initWithFrame:rect];
[imageView1 setTag:i];
// imageView = [[NSImageView alloc]initWithFrame:rect];
// [imageView1 rotateByAngle:rand() % 150];
[imageView1 setImageScaling:NSScaleToFit];
[imageView1 canBecomeKeyView];
NSImage *theImage = [[NSImage alloc]initWithContentsOfURL:(NSURL*)[patharray objectAtIndex:(i)]];
[imageView1 setImage:theImage];
[[imageView1 cell] setHighlighted:YES];
[[layoutCustom view] addSubview:imageView1 positioned:NSWindowMovedEventType relativeTo:nil];}
now how can select each image view by mouse click ? thanks in advance.
I'm assuming here that you have your reasons for not using existing collection views. So from what I read in your code you have layoutCustom.view, which contains a bunch of NSImageViews. Here are two options:
In your layoutCustom object implement the mouseDown: (or mouseUp: or both). Take the event location convert it view coordinates and look for any subview for which CGRectContainsPoint(subview.frame, mouseDownPoint) return YES. You should select that view.
Subclass NSImageView and implement mouseDown: (or mouseUp: or both). On mouseDown: simply set a "selected" flag. Either the view can draw something itself when selected or the layoutCustom object can observe the property and draw the selection accordingly.
I would prefer option 1 because it simpler, requires fewer classes and fewer interactions between objects.
// Option 1 (in layoutCustom class)
- (void) mouseDown:(NSEvent*)theEvent {
CGPoint mouseDownPoint = [self convertPoint:theEvent.locationInWindow fromView:nil];
for (NSView *view in self.subviews) {
if (CGRectContainsPoint(view.frame, mouseDownPoint)) {
// Do something to remember the selection.
// Draw the selection in drawRect:
[self setNeedsDisplay:YES];
}
}
}
// Option 2 (in Custom subclass of NSImage)
- (void) mouseDown:(NSEvent*)theEvent {
self.selected = !self.selected;
}
// Option 2 (in layoutCustom class)
- (void) addSubview:(NSView*)view positioned:(NSWindowOrderingMode)place relativeTo:(NSView*)otherView {
[super addSubview:view positioned:place relativeTo:otherView];
[self startObservingSubview:view];
}
- (void) willRemoveSubview:(NSView*)view {
[self stopObservingSubview:view];
}
- (void) startObservingSubview:(NSView*)view {
// Register your KVO here
// You MUST implement observeValueForKeyPath:ofObject:change:context:
}
- (void) stopObservingSubview:(NSView*)view {
// Remove your KVO here
}
I've got a better idea: Instead of fighting with converting mouse clicks in a view to coordinates and then figuring out how to map it to the right subview or sub-image, why not have one big (or scrolling?) view and then add your images as giant "NSButton" objects (set to custom type), where the button images can be the images you want to add.
As for how to select each image? You can either subclass "NSButton" and keep track of some custom data within it, or you can use a "tag" to figure out which button was pressed in your "IBAction" method and then decide what to do with it.
Another approach might be to embed your images into NSTableView cells...

draw separately in 2 custom views in one class

In IB, put 2 custom views into one window. I don't see any way to give them separate names. In Inspector-Info I had to use the same name, the class name, for both of them in the drop-down menu. I tried
DrawRect: NSRect bounds = [self bounds];
[[NSColor greenColor] set];
[NSBezierPath fillRect:bounds];
which filled both custom views with green. But I would like to fill, draw, etc. independently in each custom view. How can I address each view separately? Multiple custom views, later. Or does Cocoa require only one view per class?
This is probably trivial, but Google and the similar questions list here didn't come up with anything close. There's a lot of about multiple view controllers, but I don't need to switch views.
IB will show you the names of the classes that you can assign to an object. If you have only one custom class (eg. "myCustomClass") then it will only show that one in the drop-down menu.
I think the best solution to your problem, if you want tu use only one class, is to put the drawing code in two separate functions and assign each view an IBOutlet, then call the function from the controller class.
//Add this to your interface
NSNumber *myColor;
//Add/Edit the following functions
- (void)drawRect:(NSRect)aRect
{
//Some code...
if ([myColor intValue]) [self drawGreen];
else [self drawRed];
//Some code...
}
- (void)drawGreen
{
NSRect bounds = [self bounds];
[[NSColor greenColor] set];
[NSBezierPath fillRect:bounds];
}
- (void)drawRed
{
NSRect bounds = [self bounds];
[[NSColor redColor] set];
[NSBezierPath fillRect:bounds];
}
- (void)drawRedOrGreen:(int)aColor
{
myColor = [NSNumber numberWithInt:aColor];
}
You have to add the following two lines to your controller's interface
IBOutlet myCustomClass *customView1;
IBOutlet myCustomClass *customView2;
And you have to set each view's color.
This will set it when it loads for the first time.
- (void)awakeFromNib
{
[customView1 drawRedOrGreen:1]; //Green
[customView2 drawRedOrGreen:0]; //Red
}
This way each view will be coloured differently.
An alternative solution would be to create two separate custom classes (eg. "myCustomClass1" and "myCustomClass2") which would have their own drawing code...

NSView Not updating?

Im working on a drag n' drop view and found some handlers for drag and drop actions on the web. I want to make it so it turns blue when the user drags a file over the drag and drop area and gray again when they exit the drag and drop area. The issues is its not updating when you drag your mouse over it or exit it. Heres some of the code:
- (void)drawRect:(NSRect)rect
{
NSRect bounds = [self bounds];
[[NSColor grayColor] set];
[NSBezierPath fillRect:bounds];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
NSRect bounds = [self bounds];
[[NSColor blueColor] set];
[NSBezierPath fillRect:bounds];
return NSDragOperationCopy;
}
- (void)draggingExited:(id <NSDraggingInfo>)sender {
NSRect bounds = [self bounds];
[[NSColor grayColor] set];
[NSBezierPath fillRect:bounds];
}
Thanks for any help.
Are you calling [yourView: setNeedsDisplay] anywhere?
This is how you let the drawing framework know it needs to message your UIView subclass with drawRect:, so you should do it whenever things have changed. In your case, this probably means when the mouse enters or exits the drop area.
Drawing only works when a context (like a canvas for painting) is set up for you to draw into. When the framework calls -drawRect: it has set up a drawing context for you, so drawing commands like -[NSColor set] and -[NSBezierPath fillRect:] work as you expect.
Outside of -drawRect: there is usually no drawing context set up. Using drawing commands outside of -drawRect: is like waving a paintbrush in the air; there's no canvas, so no painting happens.
In 99.99% of cases, all view drawing should be kept within -drawRect: because NSView does a lot of work that you don't want to do to get the drawing context set up correctly and efficiently.
So, how do you change your view's drawing within your -draggingEntered: and -draggingExited: methods? By side effects.
You're doing the same thing in all three cases: 1) Setting a color and 2) Drawing a rectangle. The only difference is the color changes in each method. So, why not control which color you use in -drawRect: with an ivar, like so:
- (void)draggingEntered:(id <NSDraggingInfo>)sender {
drawBlueColorIvar = YES;
// ...
}
Then in -drawRect: you do this:
- (void)drawRect:(NSRect)rect {
NSColor *color = drawBlueColorIvar ? [NSColor blueColor] : [NSColor grayColor];
[color set];
[NSBezierPath fillRect:rect];
}
(Notice I didn't use [self bounds]. It is more efficient to just draw into the "dirty" rect, when possible.)
Finally, you need some way to tell the framework that your view needs to redraw when drawBlueColorIvar changes. The framework won't draw anything unless it's told it needs to. As Chris Cooper said, you do this with [self setNeedsDisplay:YES]. This should go after any place you change drawBlueColorIvar.

Resources