-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathVignettesController.m
224 lines (197 loc) · 6.6 KB
/
VignettesController.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
#import "VignettesController.h"
#import "REngine/REngine.h"
#import "REngine/RSEXP.h"
#import "RController.h"
@implementation VignettesController
static VignettesController *vignettesSharedController = nil;
+ (VignettesController*) sharedController
{
return vignettesSharedController;
}
- (IBAction)openVignette:(id)sender
{
int sr = [tableView selectedRow];
if (sr>=0) {
NSString *cmd = [NSString stringWithFormat:@"open \"%@/%@/doc/%@.pdf\"",
[dataSource objectAtColumn:@"path" row:sr],
[dataSource objectAtColumn:@"package" row:sr],
[dataSource objectAtColumn:@"vignette" row:sr]];
SLog(@"VignettesController.openVignette executing: %@", cmd);
system([cmd UTF8String]);
}
}
- (IBAction)openVignetteSource:(id)sender
{
int sr = [tableView selectedRow];
if (sr>=0) {
[[REngine mainEngine] executeString:[NSString stringWithFormat:@"edit(vignette(\"%@\", package=\"%@\"))",
[dataSource objectAtColumn:@"vignette" row:sr],
[dataSource objectAtColumn:@"package" row:sr]
]];
}
}
- (void) runFilter: (NSString*) filterString
{
SLog(@"VignettesController.reRunFilter (search string is %@)",filterString?filterString:@"<none>");
NSIndexSet *preIx = [tableView selectedRowIndexes];
NSMutableIndexSet *postIx = [[NSMutableIndexSet alloc] init];
NSMutableIndexSet *absSelIx = [[NSMutableIndexSet alloc] init];
NSUInteger i=0;
if ([preIx count]>0) { // save selection in absolute index positions
i = [preIx firstIndex];
do {
if (!filter || i<filterlen)
[absSelIx addIndex:filter?filter[i]:i];
i = [preIx indexGreaterThanIndex:i];
} while (i != NSNotFound);
i=0;
}
if (filter) {
filterlen=0;
if (filter) free(filter);
filter=0;
}
if (filterString && [filterString length]>0) {
filterlen=0;
while (i<[dataSource count]) {
if ([(NSString*)[dataSource objectAtColumn:@"vignette" index:i] rangeOfString:filterString options:NSCaseInsensitiveSearch].location!=NSNotFound ||
[(NSString*)[dataSource objectAtColumn:@"description" index:i] rangeOfString:filterString options:NSCaseInsensitiveSearch].location!=NSNotFound ||
[(NSString*)[dataSource objectAtColumn:@"package" index:i] rangeOfString:filterString options:NSCaseInsensitiveSearch].location!=NSNotFound
) filterlen++;
i++;
}
SLog(@" - found %d matches", filterlen);
filter=(int*)malloc(sizeof(int)*(filterlen+1));
i=0; filterlen=0;
while (i<[dataSource count]) {
if ([(NSString*)[dataSource objectAtColumn:@"vignette" index:i] rangeOfString:filterString options:NSCaseInsensitiveSearch].location!=NSNotFound ||
[(NSString*)[dataSource objectAtColumn:@"description" index:i] rangeOfString:filterString options:NSCaseInsensitiveSearch].location!=NSNotFound ||
[(NSString*)[dataSource objectAtColumn:@"package" index:i] rangeOfString:filterString options:NSCaseInsensitiveSearch].location!=NSNotFound
) {
if ([absSelIx containsIndex:i])
[postIx addIndex:filterlen];
filter[filterlen++]=i;
}
i++;
}
} else [postIx addIndexes:absSelIx];
if (filter)
[dataSource setFilter:filter length:filterlen];
else
[dataSource resetFilter];
[tableView reloadData];
[tableView selectRowIndexes:postIx byExtendingSelection:NO];
[absSelIx release];
[postIx release];
}
- (IBAction)search:(id)sender
{
if (dataSource)
[self runFilter: [filterField stringValue]];
}
- (void) reload
{
SLog(@"VignettesController.reload");
[dataSource reset];
RSEXP *x = [[REngine mainEngine] evaluateString:@"vignette()$results"];
SLog(@" result = %@", x);
if (x) {
RSEXP *dim = [x attr: @"dim"];
if (dim) {
if ([dim length]!=2) {
SLog(@"VignettesController.reload: result is not 2-dimnsional");
} else {
int *dimi = [dim intArray];
if (dimi[1]<4) {
SLog(@"VignettesController.reload: not enough columns");
} else {
int tl = [x length];
NSString **a = [x strings];
// SLog(@"tl=%d, a=%x", tl, a);
if (a) {
NSArray *aPkg = [[NSArray alloc] initWithObjects:a count:dimi[0]];
NSArray *aPath= [[NSArray alloc] initWithObjects:a+dimi[0] count:dimi[0]];
NSArray *aFile= [[NSArray alloc] initWithObjects:a+dimi[0]*2 count:dimi[0]];
NSArray *aDesc= [[NSArray alloc] initWithObjects:a+dimi[0]*3 count:dimi[0]];
[dataSource addColumn:aPkg withName:@"package"];
[dataSource addColumn:aFile withName:@"vignette"];
[dataSource addColumn:aDesc withName:@"description"];
[dataSource addColumn:aPath withName:@"path"];
[tableView reloadData];
int i=0;
while (i<tl) [a[i++] release];
free(a);
}
}
}
[dim release];
}
[x release];
}
needReload = NO;
}
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
int sr = [tableView selectedRow];
[openButton setEnabled: (sr!=-1)];
[openSourceButton setEnabled: (sr!=-1)];
if (sr!=-1) {
NSString *pdfFile = [NSString stringWithFormat:@"%@/%@/doc/%@.pdf",
[dataSource objectAtColumn:@"path" row:sr],
[dataSource objectAtColumn:@"package" row:sr],
[dataSource objectAtColumn:@"vignette" row:sr]];
SLog(@"VignettesController.tableViewSelectionDidChange: previewing %@", pdfFile);
if ([[NSFileManager defaultManager] fileExistsAtPath:pdfFile]) {
// [pdfView loadFromPath: pdfFile];
PDFDocument *pdfDoc = [[[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:pdfFile]] autorelease];
[thePDFView setDocument:pdfDoc];
// [pdfDrawer open];
} else {
// [pdfDrawer close];
NSBeep();
[openButton setEnabled:NO];
}
} else [pdfDrawer close];
}
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return NO;
}
- (void) showVigenttes
{
if (needReload)
[self reload];
[window makeKeyAndOrderFront:self];
}
- (void) awakeFromNib
{
SLog(@"VignettesController.awakeFromNib");
filter=0;
filterlen=0;
dataSource=[[SortableDataSource alloc] init];
[tableView setDataSource: dataSource];
[tableView setDoubleAction:@selector(openVignette:)];
[tableView setTarget:self];
// deferred loading of vignettes
//[self reload];
needReload = YES;
[openButton setEnabled: ([tableView selectedRow]!=-1)];
[openSourceButton setEnabled: ([tableView selectedRow]!=-1)];
vignettesSharedController = self;
}
- (IBAction)executeSelection:(id)sender
{
PDFSelection *dr = [thePDFView currentSelection];
if (dr) { /* we don't do line-exec since we don't get the text outside the selection */
NSString *stx = [dr string];
[[RController sharedController] sendInput:stx];
}
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
if ([menuItem action] == @selector(executeSelection:)) {
return ([thePDFView currentSelection] == nil) ? NO : YES;
}
return YES;
}
@end