3 * The Cheat - The legendary universal game trainer for Mac OS X.
4 * http://www.brokenzipper.com/trac/wiki/TheCheat
6 * Copyright (c) 2003-2011, Charles McGarvey et al.
8 * Distributable under the terms and conditions of the 2-clause BSD
9 * license; see the file COPYING for the legal text of the license.
12 #import "BetterTableView.h"
15 @interface BetterTableView ( PrivateAPI
)
17 - (NSString
*)_copyString
;
19 - (IBAction
)copy
:(id)sender
;
20 - (IBAction
)paste
:(id)sender
;
21 - (IBAction
)cut
:(id)sender
;
22 - (IBAction
)delete:(id)sender
;
27 @implementation BetterTableView
32 if ( self = [super init
] ) {
39 - (id)initWithCoder
:(NSCoder
*)coder
41 if ( self = [super initWithCoder
:coder
] ) {
49 - (BOOL)validateMenuItem
:(NSMenuItem
*)menuItem
51 SEL selector
= [menuItem action
];
53 if ( selector
== @selector(selectAll
:) ) {
57 if ( _canCopyPaste
) {
58 if ( selector
== @selector(paste
:) ) {
59 id delegate
= [self delegate
];
60 NSPasteboard
*pb
= [NSPasteboard generalPasteboard
];
62 // allow the delegate specify the type of data
63 if ( [delegate respondsToSelector
:@selector(tableViewPasteboardType
:)] ) {
64 type
= [delegate tableViewPasteboardType
:self];
66 if ( type
&& [pb availableTypeFromArray
:[NSArray arrayWithObject
:type
]] ) {
70 if ( [self selectedRow
] != -1 ) {
71 if ( selector
== @selector(copy
:) ) {
74 else if ( selector
== @selector(cut
:) || selector
== @selector(delete:) ) {
82 - (IBAction
)copy
:(id)sender
84 if ( _canCopyPaste
&& [self selectedRow
] != -1 ) {
85 id delegate
= [self delegate
];
86 NSPasteboard
*pb
= [NSPasteboard generalPasteboard
];
88 // allow the delegate specify the type of data
89 if ( [delegate respondsToSelector
:@selector(tableViewPasteboardType
:)] ) {
90 type
= [delegate tableViewPasteboardType
:self];
93 [pb declareTypes
:[NSArray arrayWithObjects
:NSStringPboardType
, type
, nil] owner
:self];
94 // allow the delegate to copy data
95 if ( [delegate respondsToSelector
:@selector(tableView
:copyRows
:)] ) {
96 [pb setData
:[delegate tableView
:self copyRows
:[self selectedRows
]] forType
:type
];
100 [pb declareTypes
:[NSArray arrayWithObjects
:NSStringPboardType
, nil] owner
:self];
102 [pb setString
:[self _copyString
] forType
:NSStringPboardType
];
106 - (IBAction
)paste
:(id)sender
108 if ( _canCopyPaste
) {
109 id delegate
= [self delegate
];
110 NSPasteboard
*pb
= [NSPasteboard generalPasteboard
];
111 NSString
*type
= nil;
112 // allow the delegate specify the type of data
113 if ( [delegate respondsToSelector
:@selector(tableViewPasteboardType
:)] ) {
114 type
= [delegate tableViewPasteboardType
:self];
116 if ( type
&& [pb availableTypeFromArray
:[NSArray arrayWithObject
:type
]] ) {
117 // allow the delegate to paste data
118 if ( [delegate respondsToSelector
:@selector(tableView
:pasteRowsWithData
:)] ) {
119 [delegate tableView
:self pasteRowsWithData
:[pb dataForType
:type
]];
125 - (IBAction
)cut
:(id)sender
128 [self delete:sender
];
131 - (IBAction
)delete:(id)sender
133 if ( _canDelete
&& [self selectedRow
] != -1 ) {
134 id delegate
= [self delegate
];
135 if ( [delegate respondsToSelector
:@selector(tableView
:deleteRows
:)] ) {
136 [delegate tableView
:self deleteRows
:[self selectedRows
]];
142 - (void)keyDown
:(NSEvent
*)theEvent
144 unsigned short keyCode
= [theEvent keyCode
];
145 // if something is selected and deleting is enabled and the delete key was pressed
146 if ( _canDelete
&& [self selectedRow
] != -1 && (keyCode
== 0x75 || keyCode
== 0x33) ) {
147 // a delete key was pressed
151 else if ( keyCode
== 0x24 || keyCode
== 0x4C ) {
152 id delegate
= [self delegate
];
153 if ( [delegate respondsToSelector
:@selector(tableViewDidReceiveEnterKey
:)] ) {
154 if ( [delegate tableViewDidReceiveEnterKey
:self] ) {
159 else if ( keyCode
== 0x31 ) {
161 id delegate
= [self delegate
];
162 if ( [delegate respondsToSelector
:@selector(tableViewDidReceiveSpaceKey
:)] ) {
163 if ( [delegate tableViewDidReceiveSpaceKey
:self] ) {
168 [super keyDown
:theEvent
];
179 - (void)setCanDelete
:(BOOL)flag
187 return _canCopyPaste
;
190 - (void)setCanCopyPaste
:(BOOL)flag
192 _canCopyPaste
= flag
;
196 - (NSArray
*)selectedRows
198 return [[self selectedRowEnumerator
] allObjects
];
204 - (NSString
*)_copyString
206 NSArray
*rows
, *columns
;
207 int i
, j
, top
, columnCount
;
208 NSMutableString
*string
;
209 id delegate
= [self delegate
];
212 if ( ![delegate respondsToSelector
:@selector(tableView
:objectValueForTableColumn
:row
:)] ) {
216 string
= [[NSMutableString alloc
] init
];
218 columns
= [self tableColumns
];
219 columnCount
= [self numberOfColumns
];
221 // loop thru all selected cells and put the text into the string
222 rows
= [self selectedRows
];
224 for ( i
= 0; i
< top
; i
++ ) {
225 int row
= [[rows objectAtIndex
:i
] unsignedIntValue
];
226 for ( j
= 0; j
< columnCount
; j
++ ) {
227 id object
= [delegate tableView
:self objectValueForTableColumn
:[columns objectAtIndex
:j
] row
:row
];
228 [string appendFormat
:@
"%@%@", j
> 0? @
"\t" : @
"", object
];
230 if ( i
+ 1 != top
) {
231 [string appendString
:@
"\n"];
234 return [string autorelease
];