]> Dogcows Code - chaz/thecheat/blob - BetterTableView.m
The Cheat 1.2
[chaz/thecheat] / BetterTableView.m
1
2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
5 //
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)
9 // any later version.
10 //
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.
15 //
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.
19 //
20
21 #import "BetterTableView.h"
22
23
24 @interface BetterTableView ( PrivateAPI )
25
26 - (NSString *)_copyString;
27
28 - (IBAction)copy:(id)sender;
29 - (IBAction)paste:(id)sender;
30 - (IBAction)cut:(id)sender;
31 - (IBAction)delete:(id)sender;
32
33 @end
34
35
36 @implementation BetterTableView
37
38
39 - (id)init
40 {
41 if ( self = [super init] ) {
42 _canCopyPaste = YES;
43 _canDelete = YES;
44 }
45 return self;
46 }
47
48 - (id)initWithCoder:(NSCoder *)coder
49 {
50 if ( self = [super initWithCoder:coder] ) {
51 _canCopyPaste = YES;
52 _canDelete = YES;
53 }
54 return self;
55 }
56
57
58 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
59 {
60 SEL selector = [menuItem action];
61
62 if ( selector == @selector(selectAll:) ) {
63 return YES;
64 }
65 // support copy/paste
66 if ( _canCopyPaste ) {
67 if ( selector == @selector(paste:) ) {
68 id delegate = [self delegate];
69 NSPasteboard *pb = [NSPasteboard generalPasteboard];
70 NSString *type = nil;
71 // allow the delegate specify the type of data
72 if ( [delegate respondsToSelector:@selector(tableViewPasteboardType:)] ) {
73 type = [delegate tableViewPasteboardType:self];
74 }
75 if ( type && [pb availableTypeFromArray:[NSArray arrayWithObject:type]] ) {
76 return YES;
77 }
78 }
79 if ( [self selectedRow] != -1 ) {
80 if ( selector == @selector(copy:) ) {
81 return YES;
82 }
83 else if ( selector == @selector(cut:) || selector == @selector(delete:) ) {
84 return _canDelete;
85 }
86 }
87 }
88 return NO;
89 }
90
91 - (IBAction)copy:(id)sender
92 {
93 if ( _canCopyPaste && [self selectedRow] != -1 ) {
94 id delegate = [self delegate];
95 NSPasteboard *pb = [NSPasteboard generalPasteboard];
96 NSString *type = nil;
97 // allow the delegate specify the type of data
98 if ( [delegate respondsToSelector:@selector(tableViewPasteboardType:)] ) {
99 type = [delegate tableViewPasteboardType:self];
100 }
101 if ( type ) {
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];
106 }
107 }
108 else {
109 [pb declareTypes:[NSArray arrayWithObjects:NSStringPboardType, nil] owner:self];
110 }
111 [pb setString:[self _copyString] forType:NSStringPboardType];
112 }
113 }
114
115 - (IBAction)paste:(id)sender
116 {
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];
124 }
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]];
129 }
130 }
131 }
132 }
133
134 - (IBAction)cut:(id)sender
135 {
136 [self copy:sender];
137 [self delete:sender];
138 }
139
140 - (IBAction)delete:(id)sender
141 {
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]];
146 }
147 }
148 }
149
150
151 - (void)keyDown:(NSEvent *)theEvent
152 {
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
157 [self delete:nil];
158 return;
159 }
160 else if ( keyCode == 0x24 || keyCode == 0x4C ) {
161 id delegate = [self delegate];
162 if ( [delegate respondsToSelector:@selector(tableViewDidReceiveEnterKey:)] ) {
163 if ( [delegate tableViewDidReceiveEnterKey:self] ) {
164 return;
165 }
166 }
167 }
168 else if ( keyCode == 0x31 ) {
169 // space key
170 id delegate = [self delegate];
171 if ( [delegate respondsToSelector:@selector(tableViewDidReceiveSpaceKey:)] ) {
172 if ( [delegate tableViewDidReceiveSpaceKey:self] ) {
173 return;
174 }
175 }
176 }
177 [super keyDown:theEvent];
178 }
179
180
181 // NEW STUFF HERE
182
183 - (BOOL)canDelete
184 {
185 return _canDelete;
186 }
187
188 - (void)setCanDelete:(BOOL)flag
189 {
190 _canDelete = flag;
191 }
192
193
194 - (BOOL)canCopyPaste
195 {
196 return _canCopyPaste;
197 }
198
199 - (void)setCanCopyPaste:(BOOL)flag
200 {
201 _canCopyPaste = flag;
202 }
203
204
205 - (NSArray *)selectedRows
206 {
207 return [[self selectedRowEnumerator] allObjects];
208 }
209
210
211 // PRIVATE METHODS
212
213 - (NSString *)_copyString
214 {
215 NSArray *rows, *columns;
216 int i, j, top, columnCount;
217 NSMutableString *string;
218 id delegate = [self delegate];
219
220 // check delegate
221 if ( ![delegate respondsToSelector:@selector(tableView:objectValueForTableColumn:row:)] ) {
222 return @"";
223 }
224
225 string = [[NSMutableString alloc] init];
226
227 columns = [self tableColumns];
228 columnCount = [self numberOfColumns];
229
230 // loop thru all selected cells and put the text into the string
231 rows = [self selectedRows];
232 top = [rows count];
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];
238 }
239 if ( i + 1 != top ) {
240 [string appendString:@"\n"];
241 }
242 }
243 return [string autorelease];
244 }
245
246
247 @end
This page took 0.043877 seconds and 5 git commands to generate.