2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 1, or (at your option)
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #import "BetterTableView.h"
24 @interface BetterTableView ( PrivateAPI
)
26 - (NSString
*)_copyString
;
28 - (IBAction
)copy
:(id)sender
;
29 - (IBAction
)paste
:(id)sender
;
30 - (IBAction
)cut
:(id)sender
;
31 - (IBAction
)delete:(id)sender
;
36 @implementation BetterTableView
41 if ( self = [super init
] ) {
48 - (id)initWithCoder
:(NSCoder
*)coder
50 if ( self = [super initWithCoder
:coder
] ) {
58 - (BOOL)validateMenuItem
:(NSMenuItem
*)menuItem
60 SEL selector
= [menuItem action
];
62 if ( selector
== @selector(selectAll
:) ) {
66 if ( _canCopyPaste
) {
67 if ( selector
== @selector(paste
:) ) {
68 id delegate
= [self delegate
];
69 NSPasteboard
*pb
= [NSPasteboard generalPasteboard
];
71 // allow the delegate specify the type of data
72 if ( [delegate respondsToSelector
:@selector(tableViewPasteboardType
:)] ) {
73 type
= [delegate tableViewPasteboardType
:self];
75 if ( type
&& [pb availableTypeFromArray
:[NSArray arrayWithObject
:type
]] ) {
79 if ( [self selectedRow
] != -1 ) {
80 if ( selector
== @selector(copy
:) ) {
83 else if ( selector
== @selector(cut
:) || selector
== @selector(delete:) ) {
91 - (IBAction
)copy
:(id)sender
93 if ( _canCopyPaste
&& [self selectedRow
] != -1 ) {
94 id delegate
= [self delegate
];
95 NSPasteboard
*pb
= [NSPasteboard generalPasteboard
];
97 // allow the delegate specify the type of data
98 if ( [delegate respondsToSelector
:@selector(tableViewPasteboardType
:)] ) {
99 type
= [delegate tableViewPasteboardType
:self];
102 [pb declareTypes
:[NSArray arrayWithObjects
:NSStringPboardType
, type
, nil] owner
:self];
103 // allow the delegate to copy data
104 if ( [delegate respondsToSelector
:@selector(tableView
:copyRows
:)] ) {
105 [pb setData
:[delegate tableView
:self copyRows
:[self selectedRows
]] forType
:type
];
109 [pb declareTypes
:[NSArray arrayWithObjects
:NSStringPboardType
, nil] owner
:self];
111 [pb setString
:[self _copyString
] forType
:NSStringPboardType
];
115 - (IBAction
)paste
:(id)sender
117 if ( _canCopyPaste
) {
118 id delegate
= [self delegate
];
119 NSPasteboard
*pb
= [NSPasteboard generalPasteboard
];
120 NSString
*type
= nil;
121 // allow the delegate specify the type of data
122 if ( [delegate respondsToSelector
:@selector(tableViewPasteboardType
:)] ) {
123 type
= [delegate tableViewPasteboardType
:self];
125 if ( type
&& [pb availableTypeFromArray
:[NSArray arrayWithObject
:type
]] ) {
126 // allow the delegate to paste data
127 if ( [delegate respondsToSelector
:@selector(tableView
:pasteRowsWithData
:)] ) {
128 [delegate tableView
:self pasteRowsWithData
:[pb dataForType
:type
]];
134 - (IBAction
)cut
:(id)sender
137 [self delete:sender
];
140 - (IBAction
)delete:(id)sender
142 if ( _canDelete
&& [self selectedRow
] != -1 ) {
143 id delegate
= [self delegate
];
144 if ( [delegate respondsToSelector
:@selector(tableView
:deleteRows
:)] ) {
145 [delegate tableView
:self deleteRows
:[self selectedRows
]];
151 - (void)keyDown
:(NSEvent
*)theEvent
153 unsigned short keyCode
= [theEvent keyCode
];
154 // if something is selected and deleting is enabled and the delete key was pressed
155 if ( _canDelete
&& [self selectedRow
] != -1 && (keyCode
== 0x75 || keyCode
== 0x33) ) {
156 // a delete key was pressed
160 else if ( keyCode
== 0x24 || keyCode
== 0x4C ) {
161 id delegate
= [self delegate
];
162 if ( [delegate respondsToSelector
:@selector(tableViewDidReceiveEnterKey
:)] ) {
163 if ( [delegate tableViewDidReceiveEnterKey
:self] ) {
168 else if ( keyCode
== 0x31 ) {
170 id delegate
= [self delegate
];
171 if ( [delegate respondsToSelector
:@selector(tableViewDidReceiveSpaceKey
:)] ) {
172 if ( [delegate tableViewDidReceiveSpaceKey
:self] ) {
177 [super keyDown
:theEvent
];
188 - (void)setCanDelete
:(BOOL)flag
196 return _canCopyPaste
;
199 - (void)setCanCopyPaste
:(BOOL)flag
201 _canCopyPaste
= flag
;
205 - (NSArray
*)selectedRows
207 return [[self selectedRowEnumerator
] allObjects
];
213 - (NSString
*)_copyString
215 NSArray
*rows
, *columns
;
216 int i
, j
, top
, columnCount
;
217 NSMutableString
*string
;
218 id delegate
= [self delegate
];
221 if ( ![delegate respondsToSelector
:@selector(tableView
:objectValueForTableColumn
:row
:)] ) {
225 string
= [[NSMutableString alloc
] init
];
227 columns
= [self tableColumns
];
228 columnCount
= [self numberOfColumns
];
230 // loop thru all selected cells and put the text into the string
231 rows
= [self selectedRows
];
233 for ( i
= 0; i
< top
; i
++ ) {
234 int row
= [[rows objectAtIndex
:i
] unsignedIntValue
];
235 for ( j
= 0; j
< columnCount
; j
++ ) {
236 id object
= [delegate tableView
:self objectValueForTableColumn
:[columns objectAtIndex
:j
] row
:row
];
237 [string appendFormat
:@
"%@%@", j
> 0? @
"\t" : @
"", object
];
239 if ( i
+ 1 != top
) {
240 [string appendString
:@
"\n"];
243 return [string autorelease
];