Re: Copy and paste from NSTableView
Re: Copy and paste from NSTableView
- Subject: Re: Copy and paste from NSTableView
- From: Dan Wood <email@hidden>
- Date: Fri, 07 Dec 2001 09:49:17 -0800
These menus send their actions to the first responder. You need
to put a method that handles "copy" and "paste" in your first
responder chain, such as your window's delegate. Then just
implement the action, e.g.:
- (IBAction) copy:(id)sender
{
[oMyTable copySelectedRow]; // see below
}
Here's a category I added to NSTableView for handling copy. It
puts the columns into two different pasteboard formats; this may
or may not be what you actually want.
Paste is left as an exercise for the reader....
@implementation NSTableView ( clipboard )
/*" Copy the single selected row from the table. The elements
are separated by newlines, as text (!{NSStringPboardType}), and
by tabs, as tabular text (!NSTabularTextPboardType).
"*/
- (void) copySelectedRow
{
int selectedRow = [self selectedRow];
int numberOfRows = [self numberOfRows];
if ( selectedRow >= 0 && selectedRow < numberOfRows )
{
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSMutableString *tabsBuf = [NSMutableString string];
NSMutableString *textBuf = [NSMutableString string];
NSArray *tableColumns = [self tableColumns];
NSEnumerator *enumerator = [tableColumns objectEnumerator];
NSTableColumn *col;
id dataSource = [self dataSource];
while (nil != (col = [enumerator nextObject]) )
{
id columnValue
= [dataSource tableView:self
objectValueForTableColumn:col row:selectedRow];
NSString *columnString = @"";
if (nil != columnValue)
{
columnString = [columnValue description];
}
[tabsBuf appendFormat:@"%@\t",columnString];
if (![columnString isEqualToString:@""])
{
[textBuf appendFormat:@"%@\n",columnString];
}
}
// delete the last tab. (But don't delete the last CR)
if ([tabsBuf length])
{
[tabsBuf deleteCharactersInRange:NSMakeRange([tabsBuf length]-1, 1)];
}
[pb declareTypes:
[NSArray arrayWithObjects:NSTabularTextPboardType,
NSStringPboardType, nil]
owner:nil];
[pb setString:[NSString stringWithString:textBuf]
forType:NSStringPboardType];
[pb setString:[NSString stringWithString:tabsBuf]
forType:NSTabularTextPboardType];
}
}
On Thursday, December 6, 2001, at 08:12 PM, Peter Sichel wrote:
I'm trying to add support for copy and paste from an NSTableView
and could use a hint on where and how to intercept the Copy command
from the Edit menu to put the selected data into the NSPasteBoard.
Thanks,
- Peter
-- _______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
--
Dan Wood
email@hidden
http://www.karelia.com/
Watson for Mac OS X:
http://www.karelia.com/watson/
Mac OS X Developer: Online Resume:
http://www.karelia.com/resume.html