forked from R-macos/Mac-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RDataEditorTableView.m
499 lines (390 loc) · 15.1 KB
/
RDataEditorTableView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
* R.app : a Cocoa front end to: "R A Computer Language for Statistical Data Analysis"
*
* R.app Copyright notes:
* Copyright (C) 2004-11 The R Foundation
* written by Stefano M. Iacus and Simon Urbanek
*
*
* R Copyright notes:
* Copyright (C) 1995-1996 Robert Gentleman and Ross Ihaka
* Copyright (C) 1998-2001 The R Development Core Team
* Copyright (C) 2002-2004 The R Foundation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* A copy of the GNU General Public License is available via WWW at
* http://www.gnu.org/copyleft/gpl.html. You can also obtain it by
* writing to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA.
*
* RDataEditorTableView.m
*
* Created by Hans-J. Bibiko on 21/06/2011.
*
*/
#import "RDataEditorTableView.h"
#import "RGUI.h"
#import "../REditor.h"
#import "../NSArray_RAdditions.h"
@implementation RDataEditorTableView
/**
* Handles the general Copy action of selected rows as tab delimited data
*/
- (void)copy:(id)sender
{
NSString *tmp = nil;
switch([sender tag]) {
case 0:
tmp = ([self numberOfSelectedRows]) ? [self rowsAsTabStringWithHeaders:YES] : [self columnsAsTabStringWithHeaders:YES];
break;
case 1:
tmp = ([self numberOfSelectedRows]) ? [self rowsAsCsvStringWithHeaders:YES] : [self columnsAsCsvStringWithHeaders:YES];
break;
default:
NSBeep();
return;
}
if ( nil != tmp )
{
NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb declareTypes:[NSArray arrayWithObjects:
NSTabularTextPboardType,
NSStringPboardType,
nil]
owner:nil];
[pb setString:tmp forType:NSStringPboardType];
[pb setString:tmp forType:NSTabularTextPboardType];
}
}
- (NSUInteger)draggingSourceOperationMaskForLocal:(BOOL)isLocal
{
return ([[self selectedRowIndexes] count]) ? NSDragOperationCopy : NSDragOperationNone;
}
- (NSString *)rowsAsTabStringWithHeaders:(BOOL)withHeaders
{
if (![self numberOfSelectedRows]) return nil;
NSIndexSet *selectedRows = [self selectedRowIndexes];
NSUInteger i;
NSArray *columns = [self tableColumns];
NSUInteger numColumns = [columns count];
NSMutableString *result = [NSMutableString stringWithCapacity:2000];
// Add the table headers if requested to do so
if (withHeaders) {
for(i = 0; i < numColumns; i++ ){
if([result length])
[result appendString:@"\t"];
[result appendString:[[NSArrayObjectAtIndex([self tableColumns], i) headerCell] title]];
}
[result appendString:@"\n"];
}
// Loop through the rows, adding their descriptive contents
NSUInteger rowIndex = [selectedRows firstIndex];
NSArray *ds = [[self delegate] objectData];
while ( rowIndex != NSNotFound )
{
for ( i = 0; i < numColumns; i++ )
[result appendFormat:@"%@\t", NSArrayObjectAtIndex(NSArrayObjectAtIndex(ds, i), rowIndex)];
// Remove the trailing tab and add the linebreak
if ([result length])
[result deleteCharactersInRange:NSMakeRange([result length]-1, 1)];
[result appendString:@"\n"];
// Select the next row index
rowIndex = [selectedRows indexGreaterThanIndex:rowIndex];
}
// Remove the trailing line end
if ([result length]) {
[result deleteCharactersInRange:NSMakeRange([result length]-1, 1)];
}
return result;
}
- (NSString *)rowsAsCsvStringWithHeaders:(BOOL)withHeaders
{
if (![self numberOfSelectedRows]) return nil;
NSIndexSet *selectedRows = [self selectedRowIndexes];
NSUInteger i;
NSArray *columns = [self tableColumns];
NSUInteger numColumns = [columns count];
NSMutableString *result = [NSMutableString stringWithCapacity:2000];
// Add the table headers if requested to do so
if (withHeaders) {
for( i = 0; i < numColumns; i++ ){
if([result length])
[result appendString:@","];
[result appendFormat:@"\"%@\"", [[[NSArrayObjectAtIndex([self tableColumns], i) headerCell] title] stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]];
}
[result appendString:@"\n"];
}
// Loop through the rows, adding their descriptive contents
NSUInteger rowIndex = [selectedRows firstIndex];
NSArray *ds = [[self delegate] objectData];
NSArray *columnTypes = [[self delegate] objectColumnTypes];
NSInteger types[[columnTypes count]];
for( i = 0; i < numColumns; i++ )
types[i] = [NSArrayObjectAtIndex(columnTypes, i) intValue];
while ( rowIndex != NSNotFound )
{
for ( i = 0; i < numColumns; i++ )
if(types[i] == 1)
[result appendFormat:@"%@,", NSArrayObjectAtIndex(NSArrayObjectAtIndex(ds, i), rowIndex)];
else
[result appendFormat:@"\"%@\",", [NSArrayObjectAtIndex(NSArrayObjectAtIndex(ds, i), rowIndex) stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]];
// Remove the trailing comma and add the linebreak
if ([result length]){
[result deleteCharactersInRange:NSMakeRange([result length]-1, 1)];
}
[result appendString:@"\n"];
// Select the next row index
rowIndex = [selectedRows indexGreaterThanIndex:rowIndex];
}
// Remove the trailing line end
if ([result length]) {
[result deleteCharactersInRange:NSMakeRange([result length]-1, 1)];
}
return result;
}
- (NSString *)columnsAsTabStringWithHeaders:(BOOL)withHeaders
{
if (![self numberOfSelectedColumns]) return nil;
NSIndexSet *selectedCols = [self selectedColumnIndexes];
NSUInteger colIndex = [selectedCols firstIndex];
NSUInteger i;
NSMutableString *result = [NSMutableString stringWithCapacity:2000];
// Add the table headers if requested to do so
if (withHeaders) {
while ( colIndex != NSNotFound ) {
if([result length])
[result appendString:@"\t"];
[result appendString:[[NSArrayObjectAtIndex([self tableColumns], colIndex) headerCell] title]];
// Select the next column index
colIndex = [selectedCols indexGreaterThanIndex:colIndex];
}
[result appendString:@"\n"];
}
// Loop through the rows, adding their descriptive contents
NSArray *ds = [[self delegate] objectData];
for ( i = 0; i < [NSArrayObjectAtIndex(ds, 0) count]; i++ )
{
colIndex = [selectedCols firstIndex];
while ( colIndex != NSNotFound ) {
[result appendFormat:@"%@\t", NSArrayObjectAtIndex(NSArrayObjectAtIndex(ds, colIndex), i)];
// Select the next column index
colIndex = [selectedCols indexGreaterThanIndex:colIndex];
}
// Remove the trailing tab and add the linebreak
if ([result length])
[result deleteCharactersInRange:NSMakeRange([result length]-1, 1)];
[result appendString:@"\n"];
}
// Remove the trailing line end
if ([result length]) {
[result deleteCharactersInRange:NSMakeRange([result length]-1, 1)];
}
return result;
}
- (NSString *)columnsAsCsvStringWithHeaders:(BOOL)withHeaders
{
if (![self numberOfSelectedColumns]) return nil;
NSIndexSet *selectedCols = [self selectedColumnIndexes];
NSUInteger colIndex = [selectedCols firstIndex];
NSUInteger i;
NSMutableString *result = [NSMutableString stringWithCapacity:2000];
// Add the table headers if requested to do so
if (withHeaders) {
while ( colIndex != NSNotFound ) {
if([result length])
[result appendString:@","];
[result appendString:[NSString stringWithFormat:@"\"%@\"",[[[NSArrayObjectAtIndex([self tableColumns], colIndex) headerCell] title] stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]]];
// Select the next column index
colIndex = [selectedCols indexGreaterThanIndex:colIndex];
}
[result appendString:@"\n"];
}
// Loop through the rows, adding their descriptive contents
NSArray *ds = [[self delegate] objectData];
NSArray *columnTypes = [[self delegate] objectColumnTypes];
NSInteger types[[columnTypes count]];
for( i = 0; i < [columnTypes count]; i++ )
types[i] = [NSArrayObjectAtIndex(columnTypes, i) intValue];
for ( i = 0; i < [NSArrayObjectAtIndex(ds, 0) count]; i++ )
{
colIndex = [selectedCols firstIndex];
while ( colIndex != NSNotFound ) {
if(types[colIndex] == 1)
[result appendFormat:@"%@,", NSArrayObjectAtIndex(NSArrayObjectAtIndex(ds, colIndex), i)];
else
[result appendFormat:@"\"%@\",", [NSArrayObjectAtIndex(NSArrayObjectAtIndex(ds, colIndex), i) stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]];
// Select the next column index
colIndex = [selectedCols indexGreaterThanIndex:colIndex];
}
// Remove the trailing comma and add the linebreak
if ([result length]){
[result deleteCharactersInRange:NSMakeRange([result length]-1, 1)];
}
[result appendString:@"\n"];
}
// Remove the trailing line end
if ([result length]) {
[result deleteCharactersInRange:NSMakeRange([result length]-1, 1)];
}
return result;
}
- (CGFloat)widthForColumn:(NSInteger)columnIndex andHeaderName:(NSString*)colName
{
CGFloat columnBaseWidth;
NSString *contentString;
NSUInteger cellWidth, maxCellWidth, i;
NSRange linebreakRange;
double rowStep;
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]
forKey:NSFontAttributeName];
NSCharacterSet *newLineCharSet = [NSCharacterSet newlineCharacterSet];
NSInteger rowsToCheck = 100;
NSUInteger maxRows = [[self delegate] numberOfRowsInTableView:self];
// Check the number of rows available to check, sampling every n rows
if (maxRows < rowsToCheck)
rowStep = 1;
else
rowStep = floor(maxRows / rowsToCheck);
rowsToCheck = (rowsToCheck > maxRows) ? maxRows : rowsToCheck;
// Set a default padding for this column
columnBaseWidth = 32.0f;
// Iterate through the data store rows, checking widths
id ds = [[self delegate] objectData];
maxCellWidth = 0;
for (i = 0; i < maxRows; i += rowStep) {
contentString = NSArrayObjectAtIndex(NSArrayObjectAtIndex(ds, columnIndex), i);
if ([contentString length] > 500) {
contentString = [contentString substringToIndex:500];
}
// If any linebreaks are present, use only the visible part of the string
linebreakRange = [contentString rangeOfCharacterFromSet:newLineCharSet];
if (linebreakRange.location != NSNotFound) {
contentString = [contentString substringToIndex:linebreakRange.location];
}
// Calculate the width, using it if it's higher than the current stored width
cellWidth = [contentString sizeWithAttributes:stringAttributes].width;
if (cellWidth > maxCellWidth) maxCellWidth = cellWidth;
if (maxCellWidth > 400) {
maxCellWidth = 400;
break;
}
}
// Add the padding
maxCellWidth += columnBaseWidth;
// If the header width is wider than this expanded width, use it instead
if(colName) {
cellWidth = [colName sizeWithAttributes:[NSDictionary dictionaryWithObject:[NSFont labelFontOfSize:[NSFont smallSystemFontSize]] forKey:NSFontAttributeName]].width;
if (cellWidth + columnBaseWidth > maxCellWidth) maxCellWidth = cellWidth + columnBaseWidth;
if (maxCellWidth > 400) maxCellWidth = 400;
}
return maxCellWidth;
}
- (void)setFont:(NSFont *)font;
{
NSArray *tableColumns = [self tableColumns];
NSUInteger columnIndex = [tableColumns count];
while (columnIndex--)
{
[[(NSTableColumn *)[tableColumns objectAtIndex:columnIndex] dataCell] setFont:font];
}
}
- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command
{
SLog(@"RDataEditorTableView received selector %@", NSStringFromSelector(command));
NSInteger row, column;
row = [self editedRow];
column = [self editedColumn];
// Trap down arrow key
if ( [textView methodForSelector:command] == [textView methodForSelector:@selector(moveDown:)] )
{
NSInteger newRow = row+1;
if (newRow >= [[self delegate] numberOfRowsInTableView:self]) return YES; //check if we're already at the end of the list
[[control window] makeFirstResponder:control];
[self selectRowIndexes:[NSIndexSet indexSetWithIndex:newRow] byExtendingSelection:NO];
[self editColumn:column row:newRow withEvent:nil select:YES];
return YES;
}
// Trap up arrow key
else if ( [textView methodForSelector:command] == [textView methodForSelector:@selector(moveUp:)] )
{
if (row==0) return YES; //already at the beginning of the list
NSInteger newRow = row-1;
if (newRow>=[[self delegate] numberOfRowsInTableView:self]) return YES; // saveRowToTable could reload the table and change the number of rows
[[control window] makeFirstResponder:control];
[self selectRowIndexes:[NSIndexSet indexSetWithIndex:newRow] byExtendingSelection:NO];
[self editColumn:column row:newRow withEvent:nil select:YES];
return YES;
}
return NO;
}
- (void)keyDown:(NSEvent*)theEvent
{
long allFlags = (NSShiftKeyMask|NSControlKeyMask|NSAlternateKeyMask|NSCommandKeyMask);
long curFlags = ([theEvent modifierFlags] & allFlags);
// Check if user pressed ⌥ to allow composing of accented characters.
// e.g. for US keyboard "⌥u a" to insert ä
// or for non-US keyboards to allow to enter dead keys
// e.g. for German keyboard ` is a dead key, press space to enter `
if ((curFlags & allFlags) == NSAlternateKeyMask || [[theEvent characters] length] == 0) {
[super keyDown:theEvent];
return;
}
NSString *charactersIgnMod = [theEvent charactersIgnoringModifiers];
// ⇧⌥⌘C - add col as CHARACTER
if(((curFlags & allFlags) == (NSCommandKeyMask|NSAlternateKeyMask|NSShiftKeyMask)) && [charactersIgnMod isEqualToString:@"C"]) {
NSMenuItem *m = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
[m setTag:2];
[[self delegate] addCol:m];
[m release];
return;
}
// ⌥⌘C - add col as NUMERIC
if(((curFlags & allFlags) == (NSCommandKeyMask|NSAlternateKeyMask)) && [charactersIgnMod isEqualToString:@"c"]) {
NSMenuItem *m = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
[m setTag:1];
[[self delegate] addCol:m];
[m release];
return;
}
// ⌥⌘A - add row
if(((curFlags & allFlags) == (NSCommandKeyMask|NSAlternateKeyMask)) && [charactersIgnMod isEqualToString:@"a"]) {
[[self delegate] addRow:nil];
return;
}
// ^⌥⌘⎋ cancel editing without saving data
if(((curFlags & allFlags) == (NSControlKeyMask|NSAlternateKeyMask|NSCommandKeyMask)) && [theEvent keyCode] == 51) {
[[self delegate] cancelEditing:nil];
return;
}
// ⌘⌫ delete selected rows or columns according to selection
if((curFlags & allFlags) == NSCommandKeyMask && [theEvent keyCode] == 51) {
if([[self selectedColumnIndexes] count])
[[self delegate] remCols:nil];
else if([[self selectedRowIndexes] count])
[[self delegate] remRows:nil];
else
NSBeep();
return;
}
[super keyDown:theEvent];
}
- (BOOL)validateMenuItem:(NSMenuItem*)menuItem
{
if ([menuItem action] == @selector(copy:)) {
return ([self numberOfSelectedRows] > 0 || [self numberOfSelectedColumns] > 0);
}
if ([menuItem action] == @selector(remSelection:)) {
return ([[self selectedColumnIndexes] count] || [[self selectedRowIndexes] count]);
}
return YES;
}
@end