Re: Cells within cells and mouse tracking
Re: Cells within cells and mouse tracking
- Subject: Re: Cells within cells and mouse tracking
- From: Tom Sutcliffe <email@hidden>
- Date: Sun, 20 Jul 2003 21:33:38 +0100
Hi, I'm doing something similar at the moment, this is what I've found:
1) you might have to override setHighlighted: in mycell to pass it on
to the button cell. However for me this was never called by the outline
view so this probably won't help.
2) I ended up doing the highlighting myself. I initially overrode
mouseDown in a custom subclass of NSOutlineView to call a custom method
on the outlineview's delegate. I've included this below, it might help.
Because of how outlineviews reuse their cells (as you obviously know as
you got the outline view to work) my highlighting code is rather nasty.
The call [myCell pleaseHighlightButtonAt: buttonFrame] just marks
buttonFrame as needing to be highlighted. Then, next time through the
cell's drawWithFrame method (which is triggered by calling [myov
reloadItem: object] right after the pleaseHighlight call) I do:
// MyCell's drawing method
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSRect buttonFrame = /* work out the buttonFrame from the cellFrame.
In your case they'd be the same */
if (NSEqualRects(toHighlight, buttonFrame)) {
// This cell needs highlighting
[button setHighlighted:YES];
[button drawWithFrame:buttonFrame inView:controlView];
[button setHighlighted:NO];
toHighlight = NSZeroRect; // Clear the highlight
} else {
[button drawWithFrame:buttonFrame inView:controlView];
}
}
[super drawWithFrame:cellFrame inView:controlView]; // Draw the rest
of the cell
}
This doesn't work quite right with trackMouse, and I'll be looking to
modify it (by overriding trackMouse, startTracking etc in MyCell) so it
works the same as say the tickboxes in project builder's "Groups &
Files" view. I'm also working on a way that won't require an extra
delegate method, but I'm not sure about that yet, it looks necessary at
the moment.
Hope this hasn't just confused the issue further :)
Tom
@interface MyCell : NSTextFieldCell {
NSButtonCell *button;
NSRect toHighlight;
}
// Other stuff here...
@end
// The outlineview's extra delegate method
- (BOOL)myOutlineView:(MyOutlineView *)myov handleMouseDown:(NSEvent
*)theEvent inRow:(int)row inColumn:(int)column {
NSPoint clickedAt = [myov convertPoint:[theEvent locationInWindow]
fromView:nil];
id object = [myov itemAtRow:row];
if (row != -1 && column != -1 {
NSRect cellFrame = [myov frameOfCellAtColumn:column row:row];
NSRect buttonFrame = [myCell buttonFrameForCellFrame:cellFrame];
if (NSMouseInRect(clickedAt, buttonFrame, NO)) {
// Highlight the button
[myCell pleaseHighlightButtonAt: buttonFrame];
[myov reloadItem: object];
BOOL result = [[myCell button] trackMouse:theEvent
inRect:buttonFrame ofView:cov untilMouseUp:NO];
if (result) {
// Perform the button-clicked-on method here
}
// Unhighlight the button, and reflect any state change caused by
the method call above
[myov reloadItem: object reloadChildren: YES];
return result;
}
}
return NO;
}
On Saturday, July 19, 2003, at 03:25 pm, Pete Yandell wrote:
What I'm Trying To Do
I'm creating a subclass of NSCell, called MyCell, that contains several
other cells. This is so that I can have a single cell in an
NSOutlineView contain several controls along with some text.
The Problem
When an NSButtonCell which is set up as a checkbox and contained in
MyCell is clicked, the checkbox's rectangle is not darkened while the
mouse is down like normal checkboxes. The click is handled and tracked
correctly and the checkbox is toggled when the mouse is released, but
the immediate user feedback does not happen while the mouse is being
tracked.
Any idea what I'm missing? I've scoured the documentation and can't
find anything that talks about drawing and highlighting during mouse
tracking in cells.
A simple version of MyCell that contains only an NSButtonCell and
exhibits the described problem is given below.
Pete Yandell
http://pete.yandell.com/
@interface MyCell : NSCell {
NSButtonCell* buttonCell;
}
- (id)init;
- (id)copyWithZone:(NSZone*)zone;
- (void)dealloc;
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView;
- (BOOL)trackMouse:(NSEvent*)theEvent inRect:(NSRect)cellFrame
ofView:(NSView*)controlView untilMouseUp:(BOOL)untilMouseUp;
@end
@implementation MyCell
- (id)init
{
self = [super initTextCell:@""];
if (self != nil) {
buttonCell = [[NSButtonCell alloc] init];
[buttonCell setButtonType:NSSwitchButton];
[buttonCell setTitle:@""];
}
return self;
}
- (id)copyWithZone:(NSZone*)zone
{
MyCell* cell = [super copyWithZone:zone];
cell->buttonCell = [buttonCell retain];
return cell;
}
- (void)dealloc
{
[buttonCell release];
[super dealloc];
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[buttonCell drawWithFrame:cellFrame inView:controlView];
}
- (BOOL)trackMouse:(NSEvent*)theEvent inRect:(NSRect)cellFrame
ofView:(NSView*)controlView untilMouseUp:(BOOL)untilMouseUp
{
return [buttonCell trackMouse:theEvent inRect:cellFrame
ofView:controlView untilMouseUp:untilMouseUp];
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.