Re: double click between headers to autosize column ala iTunes?
Re: double click between headers to autosize column ala iTunes?
- Subject: Re: double click between headers to autosize column ala iTunes?
- From: George Orthwein <email@hidden>
- Date: Tue, 13 Feb 2007 10:18:48 -0500
I ended up with a solution to my first question. Detecting a double-
click anywhere in the header is easy enough:
- (void)tableView:(NSTableView *)tableView
mouseDownInHeaderOfTableColumn:(NSTableColumn *)tableColumn
{
NSEvent *event = [NSApp currentEvent];
if ([event clickCount]==2)
NSLog(@"DoubleClick means resize TableColumn %@",[tableColumn
identifier]);
}
It gets complex though, trying to figure out if the mousedown is in
the last 5 pixels of the TableColumn's header. Actually, the resize
zone seems to be the last 3 pixels of the column plus the first 2
pixels of the next. I decided to just shift the mouse location over 2
pixels when testing for the hit.
Getting the column header rect is a bit indirect. At first, I used
NSTableHeaderView's headerRectOfColumn: which caused several
frustrating hours trying to resolve errors where I was off by a
single pixel. It stems from the fact that the rect it returns has an
integral origin but a float value for the width. No consistent round/
ceil/floor allowed the error to disappear. I finally switched to
NSTableView's rectOfColumn: and everything worked! (It gives an
integral width.)
(Aside: NSTableHeaderView's resizedColumn looks potentially useful,
but I couldn't get it to return anything. I'm guessing it's only
useful during a drag operation.)
So this is what I have currently:
- (void)tableView:(NSTableView *)tableView
mouseDownInHeaderOfTableColumn:(NSTableColumn *)tableColumn
{
// tableColumn is actually useless info because the resizing cursor
zone overlaps both adjacent columns.
// The resizing zone appears to be the last 3 pixels of the column,
and the first 2 pixels of the next column.
NSEvent *event = [NSApp currentEvent];
NSPoint location = [event locationInWindow];
NSTableHeaderView *header = [tableView headerView];
location = [header convertPoint:location fromView:nil];
// offset point 2 pixels to bring it entirely onto the column being
resized
if (location.x>=2.0)
location.x-=2;
int columnIndex = [header columnAtPoint:location];
//NSRect columnRectt = [header headerRectOfColumn:columnIndex];
NSRect columnRect = [tableView rectOfColumn:columnIndex];
// slice the 5 pixels on the right edge where the resize happens
NSRect slice, remainder;
NSDivideRect (columnRect, &slice, &remainder, 5, NSMaxXEdge);
if (NSPointInRect(location,slice)==YES) && [event clickCount]==2)
NSLog(@"autosize column: %@",[[[[tableView tableColumns]
objectAtIndex:columnIndex] headerCell] stringValue]);
}
(Aside #2: I'm pretty sure the documentation of NSDivideRect/
NSRectEdge is incorrect. The slice value is always the part between
the edge and the amount specified. Feedback sent.)
As for actually setting the size of the column, it probably greatly
depends on the cell contents. I'm going to look into NSCell's
cellSize: and cellSizeForBounds:.
George
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden