• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: double click between headers to autosize column ala iTunes?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >double click between headers to autosize column ala iTunes? (From: George Orthwein <email@hidden>)

  • Prev by Date: Re: Add nscontrol to the Main Window using code?
  • Next by Date: Sanity Check on Memory Leaks
  • Previous by thread: double click between headers to autosize column ala iTunes?
  • Next by thread: About nswindow animation?
  • Index(es):
    • Date
    • Thread