Skip to content

Instantly share code, notes, and snippets.

@eventualbuddha
Created April 11, 2011 22:02
Show Gist options
  • Save eventualbuddha/914470 to your computer and use it in GitHub Desktop.
Save eventualbuddha/914470 to your computer and use it in GitHub Desktop.
Simple Cappuccino app that demonstrates this bug: https://github.com/280north/cappuccino/pull/1225
/*
* AppController.j
* CPTableViewRetargetedDropBug
*
* Created by You on April 11, 2011.
* Copyright 2011, Your Company All rights reserved.
*/
@import <Foundation/CPObject.j>
var TestDropType = @"TestDropType";
@implementation AppController : CPObject
{
CPTableView tableView;
CPTextField label;
BOOL allowDropOn;
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
contentView = [theWindow contentView];
label = [CPTextField labelWithTitle:@"Drag a row around. With the checkbox checked the table view should not allow dropping an item on top of another item, but should instead draw the feedback view above the hovered row."];
[label setLineBreakMode:CPLineBreakByWordWrapping];
[label setFrameSize:[[label stringValue] sizeWithFont:[label font] inWidth:500]];
[label setFrameSize:CGSizeMake([label frame].size.width, [label frame].size.height+10)];
[label setFrameOrigin:CGPointMake(20, 20)];
[contentView addSubview:label];
var allowDropOnCheckbox = [CPCheckBox checkBoxWithTitle:@"Allow dropping one on top of another"];
[allowDropOnCheckbox setFrameOrigin:CGPointMake(CGRectGetMinX([label frame]), CGRectGetMaxY([label frame])+5)];
[allowDropOnCheckbox setTarget:self];
[allowDropOnCheckbox setAction:@selector(toggleAllowDropOn:)];
allowDropOn = NO;
[allowDropOnCheckbox setState:allowDropOn ? CPOnState : CPOffState];
[contentView addSubview:allowDropOnCheckbox];
var scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY([allowDropOnCheckbox frame])+10, 500, 300)];
tableView = [[CPTableView alloc] initWithFrame:[scrollView bounds]];
[tableView setCornerView:nil];
[tableView setHeaderView:nil];
[tableView registerForDraggedTypes:[CPArray arrayWithObject:TestDropType]];
[tableView setBackgroundColor:[CPColor colorWithWhite:196.0/255.0 alpha:1.0]];
[tableView setDataSource:self];
[tableView addTableColumn:[[CPTableColumn alloc] initWithIdentifier:@"row"]];
[scrollView setDocumentView:tableView];
[contentView addSubview:scrollView];
[theWindow orderFront:self];
}
- (void)toggleAllowDropOn:(id)sender
{
allowDropOn = [sender state] == CPOnState;
}
- (CPInteger)numberOfRowsInTableView:(CPTableView)aTableView
{
return 10;
}
- (id)tableView:(CPTableView)aTableView objectValueForTableColumn:(CPTableColumn)tableColumn row:(CPInteger)rowIndex
{
return [rowIndex stringValue];
}
- (BOOL)tableView:(CPTableView)aTableView writeRowsWithIndexes:(CPIndexSet)rowIndexes toPasteboard:(CPPasteboard)pasteboard
{
var data = [CPKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pasteboard declareTypes:[CPArray arrayWithObject:TestDropType] owner:self];
[pasteboard setData:data forType:TestDropType];
return YES;
}
- (CPDragOperation)tableView:(CPTableView)aTableView validateDrop:(CPDraggingInfo)info proposedRow:(int)row proposedDropOperation:(CPTableViewDropOperation)operation
{
if ([info draggingSource] == tableView)
{
if (!allowDropOn && operation == CPTableViewDropOn)
[tableView setDropRow:row dropOperation:CPTableViewDropAbove];
return CPDragOperationMove;
}
else
{
return CPDragOperationNone;
}
}
- (BOOL)tableView:(CPTableView)aTableView acceptDrop:(id <CPDraggingInfo>)info row:(int)row dropOperation:(CPTableViewDropOperation)operation
{
return NO;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment