forked from DanteBortone/episodic-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBDetailOutlineView.m
593 lines (344 loc) · 17.4 KB
/
DBDetailOutlineView.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
Episodic Notes: A personal wiki, note taking app for Mac OSX. Create notes through any application on your computer and keep them in one place.
Copyright (C) 2014. Dante Bortone
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 3 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.
*/
//---------------------------------------------
#import "DBDetailOutlineView.h"
//---------------------------------------------
#import "DBDetailOutlineViewDelegate.h"
#import "DBDetailOutlineViewController.h"
#import "DBTopicOutlineViewController.h"
#import "DBControllerOfOutlineViews.h"
#import "NoteTaker_AppDelegate.h"
#import "NSTreeController_Extensions.h"
#import "DBRelatedOutlineViewController.h"
#import "DBDetailViewController.h"
#import "DBViewObject.h"
#import "DBDetail.h"
#import "NSIndexPath_Extensions.h"
#import "DBTableCellTextView.h"
#import "DBTextViewCell.h"
#import "DBHyperlinkEditor.h"
#import "DBTableCellFileView.h"
//#import "DBTableCellNoteView.h"
#import "DBCalendarController.h"
#import "DBDetailController.h"
//---------------------------------------------
@implementation DBDetailOutlineView
#define kOutlineCellWidth 11
#define kOutlineMinLeftMargin 6
@synthesize selectRowIndexesEnabled;
// -------------------------------------------------------------------------------
// awakeFromNib
// -------------------------------------------------------------------------------
- (void) awakeFromNib
{
[super awakeFromNib];
[self setDoubleAction:@selector(doubleClicked:)];
NSTableColumn * noteColumn = [self tableColumnWithIdentifier:@"Note"];
[[noteColumn headerCell] setImage:[NSImage imageNamed:@"TableHeaderNote"]];
NSTableColumn * fileColumn = [self tableColumnWithIdentifier:@"File"];
[[fileColumn headerCell] setImage:[NSImage imageNamed:@"TableHeaderFile"]];
NSTableColumn * checkColumn = [self tableColumnWithIdentifier:@"Check"];
[[checkColumn headerCell] setImage:[NSImage imageNamed:@"SmallCheck"]];
NSTableColumn * imageColumn = [self tableColumnWithIdentifier:@"Image"];
[[imageColumn headerCell] setImage:[NSImage imageNamed:@"16x16 camera"]];
// for wiki word overlays
NSTrackingArea *tracker = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingEnabledDuringMouseDrag|NSTrackingMouseMoved|NSTrackingActiveInActiveApp owner:self userInfo:nil];
//NSTrackingMouseEnteredAndExited
scrollView = [self enclosingScrollView];
dragInProgress = NO;
[self addTrackingArea:tracker];
}
// -------------------------------------------------------------------------------
// draggingSession: movedToPoint:
// -------------------------------------------------------------------------------
-(void) draggingSession:(NSDraggingSession *)session movedToPoint:(NSPoint)screenPoint{
//NSLog(@"draggingSession:movedToPoint");
// if details were dropped into a topic header then the items will be dropped inside the topic itself ( not as a node in the view ) and return NSDragOperationNone so the folder doesn't open but we don't want it to animate a slide back to the original location
[session setAnimatesToStartingPositionsOnCancelOrFail:self.animatesToStartingPositionsOnCancelOrFail];
}
// -------------------------------------------------------------------------------
// noteHeightOfRowsWithIndexesChanged:
// -------------------------------------------------------------------------------
- (void) noteHeightOfRowsWithIndexesChanged:(NSIndexSet *)indexSet{
//NSLog(@"noteHeightOfRowsWithIndexesChanged");
[super noteHeightOfRowsWithIndexesChanged:indexSet];
}
/*
-(NSRect)frameOfCellAtColumn:(NSInteger)column row:(NSInteger)row
{
NSRect superFrame = [super frameOfCellAtColumn:column row:row];
return superFrame;
}
*/
// -------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------
// THIS is how view-based TableViews select
-(void)selectRowIndexes:(NSIndexSet *)indexes byExtendingSelection:(BOOL)extend{
//NSLog(@"selectRowIndexes:byExtendingSelection");
if ([selectRowIndexesEnabled boolValue]) {
[super selectRowIndexes:indexes byExtendingSelection:extend];
} else {
// this is set to yes by mouseDown
indexes = [NSIndexSet indexSet];
[super selectRowIndexes:indexes byExtendingSelection:NO];
}
}
// -------------------------------------------------------------------------------
// mouseDragged:
// -------------------------------------------------------------------------------
-(void) mouseDragged:(NSEvent *)theEvent
{
// this only works for me if it's called by the datasource
// mousedown has to call [super mouseDown] before drag can be detected
// NSLog(@"Mouse dragged");
dragInProgress = YES;
}
// -------------------------------------------------------------------------------
// mouseUp:
// -------------------------------------------------------------------------------
//-(void) mouseUp:(NSEvent *)theEvent
//{
// only works for on the outline not the row
//NSLog(@"Mouse up");
//}
// -------------------------------------------------------------------------------
// selectedRowFromEvent:
// -------------------------------------------------------------------------------
//Tries to select row and returns
// 1 if row was selected
// 0 if row was already selected
// and only selects this row if more than one row was selected previously
// NULL if the row could not be selected
-(NSNumber *)selectedRowFromEvent:(NSEvent *)theEvent
{
//NSLog(@"selectedRowFromEvent --------------");
self.selectRowIndexesEnabled = [NSNumber numberWithBool:YES];
NSPoint tablePoint = [self convertPoint:theEvent.locationInWindow fromView:nil];
//NSLog(@"event location: %f, %f", tablePoint.x, tablePoint.y);
NSInteger rowNum = [self rowAtPoint:tablePoint];
//NSLog(@"rowNum: %li", rowNum);
//NSInteger colNum = [self columnAtPoint:tablePoint];
//NSIndexPath * selectionPath = self.controller.tree.selectionIndexPath;
NSArray * selectionPaths = self.controller.tree.selectionIndexPaths;
if (rowNum >-1 ) { //valid row clicked
//Boolean clickedRowWasPreviouslySelected = ([clickedItemPath compare:selectionPath] == NSOrderedSame); // aside: if NULL is first it will always return NSOrderedSame)
NSIndexPath * clickedItemPath = [[self itemAtRow:rowNum] indexPath];
Boolean clickedRowWasPreviouslySelected = NO;
for (NSIndexPath * selectionPath in selectionPaths){
// aside: if NULL is first it will always return NSOrderedSame)
if ([clickedItemPath compare:selectionPath] == NSOrderedSame) clickedRowWasPreviouslySelected = YES;
};
NSUInteger flags = [theEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask;
if ( ! clickedRowWasPreviouslySelected ){
// does nothing if the shift key is down so that multiple rows can be selected
//if( flags != NSShiftKeyMask ){
//NSLog(@"no shift key");
//[self.controller.tree setSelectionIndexPath:clickedItemPath]; // prevents selection of multiple rows
//}
return [NSNumber numberWithInt:1]; // row was newly selected
} else {
// if it was already selected in a group then let's limit it to just one so that right clicks can select the correct item
if (selectionPaths.count > 1) {
// holding down shift allows multiple rows selected
//if((flags != NSShiftKeyMask) && (flags == NSControlKeyMask)){ //right click
if(flags == NSControlKeyMask){ //right click
//NSLog(@"no shift key");
[self.controller.tree setSelectionIndexPath:clickedItemPath]; // prevents selection of multiple rows
}
}
return [NSNumber numberWithInt:0]; // row was already selected
}
} else {
//NSLog(@"select none");
[self.controller.tree selectNone];
return nil; // couldn't select row
}
}
// -------------------------------------------------------------------------------
// checkTextViewCellforEndEditing
// -------------------------------------------------------------------------------
// end editing in textViewCell
// see if a row is selected
// see if that row's textViewCell is being edited
-(void)checkTextViewCellforEndEditing
{
//NSLog(@"assignTopic called: checkTextViewCellforEndEditing");
NSInteger selectedRow = [self selectedRow];
//NSLog(@"selected row: %li",selectedRow);
if (selectedRow > -1 ){
NSInteger column = [self columnWithIdentifier:@"Detail"];
//NSLog(@"column: %li", column);
DBTableCellTextView * tableCell = [self viewAtColumn:column row:selectedRow makeIfNecessary:NO];
if (tableCell) {
//NSLog(@"tableCell has a value");
} //else {
//NSLog(@"tableCell is null");
//}
DBTextViewCell * textViewCell = [tableCell textView];
if (textViewCell.editingText) {
//NSLog(@"view was editing");
[textViewCell textDidEndEditing:NULL];
} //else {
//NSLog(@"view was NOT editing");
//}
}
}
/* moved to super
//assumes a row is selected
-(id)tableCellViewFromEvent:(NSEvent *) theEvent{
NSPoint tablePoint = [self convertPoint:theEvent.locationInWindow fromView:nil];
NSInteger rowNum = [self rowAtPoint:tablePoint];
NSInteger colNum = [self columnAtPoint:tablePoint];
return [self viewAtColumn:colNum row:rowNum makeIfNecessary:NO];
}
*/
// -------------------------------------------------------------------------------
// mouseDown:
// -------------------------------------------------------------------------------
-(void)mouseDown:(NSEvent *)theEvent
{
//NSLog(@"Mouse down");
NSNumber *rowSelected = [self selectedRowFromEvent:theEvent]; // selects row; YES = newly selected; NO was already selected; NULL couldn't select a row
BOOL openWikiLink = false;
if (rowSelected != NULL){ // means a valid row is selected
id tableCellView = [self tableCellViewFromEvent:theEvent];
NSString * link;
Boolean textViewClicked = [tableCellView isKindOfClass:[DBTableCellTextView class]]; // all DBTableCellTextViews have a textView
Boolean doubleClicked;
if ([theEvent clickCount]>=2){
doubleClicked = YES;
} else {
doubleClicked = NO;
}
//first check for a link
// then check for a double click
// then send the mouse even on
// if a drag didn't start, then
// select the row if it wasn't previously selected
DBTextViewCell *textViewCell;
NSInteger characterIndexOfTextView;
if (textViewClicked) { //check a link was clicked on
textViewCell = [(DBTableCellTextView*)tableCellView textView];
characterIndexOfTextView = [textViewCell characterIndexForInsertionAtPoint:[textViewCell convertPoint:theEvent.locationInWindow fromView:nil ]];
//if the user clicks on the empty space to the right of the word, characterIndexOfTextView returns an index that is one too large
if (characterIndexOfTextView < textViewCell.textStorage.length) {
//NSLog(@"accepted characterIndex: %li",characterIndexOfTextView);
link = [[textViewCell textStorage] attribute:@"wikiWordURL" atIndex:characterIndexOfTextView effectiveRange:NULL];
//NSLog(@"DBDetailOutlineView: %@", link);
if (link) {
if ([link isEqualToString:@"model"]) {
//NSLog(@"using model to link...");
NSString * viewIdentifier = [tableCellView identifier];
DBDetail * detail = [tableCellView objectValue];
if ([viewIdentifier isEqualToString:@"AssociatedCellView"]) {
[appDelegate.calendarController assignDateTopicFromDate: detail.dateAssociated throughLink:YES];
} else if ([viewIdentifier isEqualToString:@"CreatedCellView"]){
[appDelegate.calendarController assignDateTopicFromDate: detail.dateCreated throughLink:YES];
} else if ([viewIdentifier isEqualToString:@"ModifiedCellView"]){
[appDelegate.calendarController assignDateTopicFromDate: detail.dateModified throughLink:YES];
}
} else {
//NSLog(@"open link");
openWikiLink = true;
//[ appDelegate.hyperlinkEditor openWikiLink:link ];
}
} else if (doubleClicked) {
// select everything
[appDelegate.mainWindow makeFirstResponder:textViewCell];
[textViewCell setSelectedRange:NSMakeRange(0, textViewCell.textStorage.length)];
}
}
}
if (!doubleClicked){
// everything before [super mouseDown] is at mouse down
// calling here allows us to prevent the direct entry to edit mode
// but allows the drag detection from the datasource
[super mouseDown:theEvent];//has to be after so previous row is deselected
// everything after [super mouseDown] is at mouse up
if (!dragInProgress) {
//NSLog(@"no drag");
// link here!!
if (openWikiLink) {
[ appDelegate.hyperlinkEditor openWikiLink:link ];
} else {
if (![rowSelected boolValue]){ // row was already selected. insert cursor
if (textViewClicked) {
[appDelegate.mainWindow makeFirstResponder:textViewCell];
[textViewCell setSelectedRange:NSMakeRange(0, textViewCell.textStorage.length)];
}
}
}
} else { // drag IS in progress
//if there is a drag in progress we need to reset this variable
dragInProgress = NO;
}
} // (!doubleClicked)
} // (rowSelected != NULL)
}
// -------------------------------------------------------------------------------
// setSortDescriptors:
// -------------------------------------------------------------------------------
//disable reodering when clicking on header
-(void) setSortDescriptors:(NSArray *)array
{
//NSLog(@"DBDetailOutlineView:setSortDescriptors");
}
// -------------------------------------------------------------------------------
// mouseMoved
// -------------------------------------------------------------------------------
-(void)mouseMoved:(NSEvent *)theEvent
{
// NSLog(@"mouseMoved");
// don't want to see the IBeam unless the textView that the cursor over is the first responder
NSPoint tablePoint = [self convertPoint:theEvent.locationInWindow fromView:nil];
NSInteger rowNum = [self rowAtPoint:tablePoint];
NSInteger colNum = [self columnAtPoint:tablePoint];
if (rowNum > -1) {
id viewCursorIsOver = [self viewAtColumn:colNum row:rowNum makeIfNecessary:NO];
if ([viewCursorIsOver isKindOfClass:[DBTableCellTextView class]]) {
if ( [ appDelegate.mainWindow firstResponder ] != (DBTextViewCell*)[viewCursorIsOver textView] ) {
if ([NSCursor currentCursor] == [NSCursor IBeamCursor]) {
[[NSCursor arrowCursor]set];
}
}
}
}
}
// -------------------------------------------------------------------------------
// becomeFirstResponder
// -------------------------------------------------------------------------------
- (BOOL)becomeFirstResponder
{
[self.controller.mainDetailViewController becomeActiveViewController];
[self.controller.mainDetailViewController.controllerOfOutlineViews.topicOutlineViewController.tree selectNone];
NSArrayController *recentTopicController = self.controller.mainDetailViewController.controllerOfOutlineViews.recentTopicController;
[recentTopicController removeSelectionIndexes:[recentTopicController selectionIndexes]];
return [super becomeFirstResponder];//important this is last
}
// -------------------------------------------------------------------------------
// reloadData
// -------------------------------------------------------------------------------
- (void)reloadData
{
[super reloadData];
NSUInteger row;
for (row = 0 ; row < [self numberOfRows] ; row++) {
NSTreeNode *item = [self itemAtRow:row];
if ([[[item representedObject] valueForKey:@"isExpanded"] boolValue])
[self expandItem:item];
}
}
// -------------------------------------------------------------------------------
// createDefaultItemWithString:
// -------------------------------------------------------------------------------
-(void) createDefaultItemWithString:(NSString *)string
{
//NSLog(@"create default item for detail outline view passing key: %@", string);
[ appDelegate.detailController insertBulletWithString: string ];
}
@end