]> Dogcows Code - chaz/thecheat/blob - BetterTableView.m
update contact information and project URL
[chaz/thecheat] / BetterTableView.m
1
2 /*
3 * The Cheat - The legendary universal game trainer for Mac OS X.
4 * http://www.brokenzipper.com/trac/wiki/TheCheat
5 *
6 * Copyright (c) 2003-2011, Charles McGarvey et al.
7 *
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.
10 */
11
12 #import "BetterTableView.h"
13
14
15 @interface BetterTableView ( PrivateAPI )
16
17 - (NSString *)_copyString;
18
19 - (IBAction)copy:(id)sender;
20 - (IBAction)paste:(id)sender;
21 - (IBAction)cut:(id)sender;
22 - (IBAction)delete:(id)sender;
23
24 @end
25
26
27 @implementation BetterTableView
28
29
30 - (id)init
31 {
32 if ( self = [super init] ) {
33 _canCopyPaste = YES;
34 _canDelete = YES;
35 }
36 return self;
37 }
38
39 - (id)initWithCoder:(NSCoder *)coder
40 {
41 if ( self = [super initWithCoder:coder] ) {
42 _canCopyPaste = YES;
43 _canDelete = YES;
44 }
45 return self;
46 }
47
48
49 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
50 {
51 SEL selector = [menuItem action];
52
53 if ( selector == @selector(selectAll:) ) {
54 return YES;
55 }
56 // support copy/paste
57 if ( _canCopyPaste ) {
58 if ( selector == @selector(paste:) ) {
59 id delegate = [self delegate];
60 NSPasteboard *pb = [NSPasteboard generalPasteboard];
61 NSString *type = nil;
62 // allow the delegate specify the type of data
63 if ( [delegate respondsToSelector:@selector(tableViewPasteboardType:)] ) {
64 type = [delegate tableViewPasteboardType:self];
65 }
66 if ( type && [pb availableTypeFromArray:[NSArray arrayWithObject:type]] ) {
67 return YES;
68 }
69 }
70 if ( [self selectedRow] != -1 ) {
71 if ( selector == @selector(copy:) ) {
72 return YES;
73 }
74 else if ( selector == @selector(cut:) || selector == @selector(delete:) ) {
75 return _canDelete;
76 }
77 }
78 }
79 return NO;
80 }
81
82 - (IBAction)copy:(id)sender
83 {
84 if ( _canCopyPaste && [self selectedRow] != -1 ) {
85 id delegate = [self delegate];
86 NSPasteboard *pb = [NSPasteboard generalPasteboard];
87 NSString *type = nil;
88 // allow the delegate specify the type of data
89 if ( [delegate respondsToSelector:@selector(tableViewPasteboardType:)] ) {
90 type = [delegate tableViewPasteboardType:self];
91 }
92 if ( type ) {
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];
97 }
98 }
99 else {
100 [pb declareTypes:[NSArray arrayWithObjects:NSStringPboardType, nil] owner:self];
101 }
102 [pb setString:[self _copyString] forType:NSStringPboardType];
103 }
104 }
105
106 - (IBAction)paste:(id)sender
107 {
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];
115 }
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]];
120 }
121 }
122 }
123 }
124
125 - (IBAction)cut:(id)sender
126 {
127 [self copy:sender];
128 [self delete:sender];
129 }
130
131 - (IBAction)delete:(id)sender
132 {
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]];
137 }
138 }
139 }
140
141
142 - (void)keyDown:(NSEvent *)theEvent
143 {
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
148 [self delete:nil];
149 return;
150 }
151 else if ( keyCode == 0x24 || keyCode == 0x4C ) {
152 id delegate = [self delegate];
153 if ( [delegate respondsToSelector:@selector(tableViewDidReceiveEnterKey:)] ) {
154 if ( [delegate tableViewDidReceiveEnterKey:self] ) {
155 return;
156 }
157 }
158 }
159 else if ( keyCode == 0x31 ) {
160 // space key
161 id delegate = [self delegate];
162 if ( [delegate respondsToSelector:@selector(tableViewDidReceiveSpaceKey:)] ) {
163 if ( [delegate tableViewDidReceiveSpaceKey:self] ) {
164 return;
165 }
166 }
167 }
168 [super keyDown:theEvent];
169 }
170
171
172 // NEW STUFF HERE
173
174 - (BOOL)canDelete
175 {
176 return _canDelete;
177 }
178
179 - (void)setCanDelete:(BOOL)flag
180 {
181 _canDelete = flag;
182 }
183
184
185 - (BOOL)canCopyPaste
186 {
187 return _canCopyPaste;
188 }
189
190 - (void)setCanCopyPaste:(BOOL)flag
191 {
192 _canCopyPaste = flag;
193 }
194
195
196 - (NSArray *)selectedRows
197 {
198 return [[self selectedRowEnumerator] allObjects];
199 }
200
201
202 // PRIVATE METHODS
203
204 - (NSString *)_copyString
205 {
206 NSArray *rows, *columns;
207 int i, j, top, columnCount;
208 NSMutableString *string;
209 id delegate = [self delegate];
210
211 // check delegate
212 if ( ![delegate respondsToSelector:@selector(tableView:objectValueForTableColumn:row:)] ) {
213 return @"";
214 }
215
216 string = [[NSMutableString alloc] init];
217
218 columns = [self tableColumns];
219 columnCount = [self numberOfColumns];
220
221 // loop thru all selected cells and put the text into the string
222 rows = [self selectedRows];
223 top = [rows count];
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];
229 }
230 if ( i + 1 != top ) {
231 [string appendString:@"\n"];
232 }
233 }
234 return [string autorelease];
235 }
236
237
238 @end
This page took 0.038706 seconds and 4 git commands to generate.