-
Notifications
You must be signed in to change notification settings - Fork 30
/
EntryCellController.m
2092 lines (1678 loc) · 66.4 KB
/
EntryCellController.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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// EntryCellController.m
// Journler
//
// Created by Philip Dow on 10/25/06.
// Copyright 2006 Sprouted, Philip Dow. All rights reserved.EntryTextAutoCorrectSpelling
//
/*
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the
distribution.
* Neither the name of the author nor the names of its contributors may be used to endorse or
promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Basically, you can use the code in your free, commercial, private and public projects
// as long as you include the above notice and attribute the code to Philip Dow / Sprouted
// If you use this code in an app send me a note. I'd love to know how the code is used.
// Please also note that this copyright does not supersede any other copyrights applicable to
// open source code used herein. While explicit credit has been given in the Journler about box,
// it may be lacking in some instances in the source code. I will remedy this in future commits,
// and if you notice any please point them out.
#import "EntryCellController.h"
#import "JournlerApplicationDelegate.h"
#import "JournlerJournal.h"
#import "JournlerEntry.h"
#import "JournlerCollection.h"
#import "JournlerResource.h"
#import "LinksOnlyNSTextView.h"
//#import "JUtility.h"
#import "Definitions.h"
#import "PDStylesBar.h"
#import "WebViewController.h"
#import <SproutedUtilities/SproutedUtilities.h>
#import <SproutedInterface/SproutedInterface.h>
#import "NSURL+JournlerAdditions.h"
#import "NSAlert+JournlerAdditions.h"
#import "NSString+JournlerAdditions.h"
#import "NSString+JournlerUtilities.h"
@implementation EntryCellController
/*
static NSDictionary * StatusAttributes()
{
static NSDictionary *statusAttributes = nil;
if ( statusAttributes == nil )
{
NSShadow *textShadow = [[[NSShadow alloc] init] autorelease];
[textShadow setShadowColor:[NSColor colorWithCalibratedWhite:0.96 alpha:0.8]];
[textShadow setShadowOffset:NSMakeSize(0,-1)];
NSColor *black = [NSColor blackColor];
NSFont *font = [NSFont controlContentFontOfSize:11];
NSParagraphStyle *paragraphStyle = [NSParagraphStyle defaultParagraphStyleWithLineBreakMode:NSLineBreakByTruncatingTail];
statusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
textShadow, NSShadowAttributeName,
paragraphStyle, NSParagraphStyleAttributeName,
font, NSFontAttributeName,
black, NSForegroundColorAttributeName, nil];
}
return statusAttributes;
}
*/
- (id) init
{
if ( self = [super init] )
{
// initialization
headerHidden = YES;
footerHidden = YES;
selectedEntries = [[NSArray alloc] init];
// smart quotes
openQuote = YES;
static unichar kOpenSmartQuote = 0x201C; // 0x201C; //0x0093;
static unichar kCloseSmartQuote = 0x201D; // 0x201D; // 0x0094;
openSmartQuote = [[NSString alloc] initWithCharacters:(const unichar[]){kOpenSmartQuote} length:1];
closeSmartQuote = [[NSString alloc] initWithCharacters:(const unichar[]){kCloseSmartQuote} length:1];
textBackgroundColor = [[NSColor whiteColor] retain];
headerBackgroundColor = [[NSColor whiteColor] retain];
headerLabelColor = [[NSColor colorWithCalibratedWhite:0.75 alpha:1.0] retain];
headerTextColor = [[NSColor colorWithCalibratedWhite:0.0 alpha:1.0] retain];
// the interface
[NSBundle loadNibNamed:@"EntryCell" owner:self];
}
return self;
}
- (void) awakeFromNib
{
// create the text view by hand
[self installTextSystem];
NSInteger statusBorders[4] = {1,0,0,0};
[statusBar setBordered:YES];
[statusBar setBorders:statusBorders];
[self setHeaderIsWhite:YES];
[headerView setBorders:(int[]){1,0,0,0}];
[headerView setGradientStartColor:[NSColor whiteColor]];
[headerView setGradientEndColor:[NSColor whiteColor]];
NSInteger contentBorders[4] = {0,0,0,0};
[contentView setBorders:contentBorders];
[self setHeaderHidden:NO];
[self setFooterHidden:NO];
[textView setDelegate:self];
[[textView textStorage] setDelegate:self];
//[textView setContinuouslyPostsSelectionNotification:YES];
[[scalePop cell] setArrowPosition:NSPopUpNoArrow];
[[marginPop cell] setArrowPosition:NSPopUpNoArrow];
// set the scale on the text view - depends on fullscreen status
NSInteger scale = ( [self respondsToSelector:@selector(textViewIsInFullscreenMode:)] && [self textViewIsInFullscreenMode:textView]
? [[NSUserDefaults standardUserDefaults] integerForKey:@"EntryTextFullscreenZoom"]
: [[NSUserDefaults standardUserDefaults] integerForKey:@"EntryTextDefaultZoom"] );
NSMenuItem *scaleItem = [[scalePop menu] itemWithTag:scale];
if ( scaleItem != nil )
{
[scalePop selectItem:scaleItem];
[[scaleItem target] performSelector:[scaleItem action] withObject:scaleItem];
}
// set the margin on the text view - depends on fullscreen status
NSInteger margin = ( [self respondsToSelector:@selector(textViewIsInFullscreenMode:)] && [self textViewIsInFullscreenMode:textView]
? [[NSUserDefaults standardUserDefaults] integerForKey:@"EntryTextHorizontalInsetFullscreen"]
: [[NSUserDefaults standardUserDefaults] integerForKey:@"EntryTextHorizontalInset"] );
NSMenuItem *marginItem = [[marginPop menu] itemWithTag:margin];
if ( marginItem != nil )
{
[marginPop selectItem:marginItem];
[[marginItem target] performSelector:[marginItem action] withObject:marginItem];
}
// the styles bar
stylesBar = [[PDStylesBar allocWithZone:[self zone]] initWithTextView:textView];
// the header
NSInteger borders[4] = {1,0,0,0};
[headerView setBorders:borders];
[headerView setBordered:YES];
// and make sure none of my headers fields draw a focus ring
[[titleField cell] setFocusRingType:NSFocusRingTypeNone];
[[tagsField cell] setFocusRingType:NSFocusRingTypeNone];
[[categoryField cell] setFocusRingType:NSFocusRingTypeNone];
[[dateField cell] setFocusRingType:NSFocusRingTypeNone];
[tagsField setDrawsBackground:NO];
// tags cell
[tagsField setBezeled:NO];
[tagsField setBordered:NO];
[tagsField setEditable:NO];
// set the formatter on the date cell
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateField setFormatter:dateFormatter];
// header background color
[self bind:@"headerBackgroundColor"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.HeaderBackgroundColor"
options:[NSDictionary dictionaryWithObjectsAndKeys:
@"NSUnarchiveFromData", NSValueTransformerNameBindingOption,
[NSColor whiteColor], NSNullPlaceholderBindingOption, nil]];
// content background color
[self bind:@"textBackgroundColor"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.EntryBackgroundColor"
options:[NSDictionary dictionaryWithObjectsAndKeys:
@"NSUnarchiveFromData", NSValueTransformerNameBindingOption,
[NSColor whiteColor], NSNullPlaceholderBindingOption, nil]];
// header label color
[self bind:@"headerLabelColor"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.HeaderLabelColor"
options:[NSDictionary dictionaryWithObjectsAndKeys:
@"NSUnarchiveFromData", NSValueTransformerNameBindingOption,
[NSColor darkGrayColor], NSNullPlaceholderBindingOption, nil]];
// header text color
[self bind:@"headerTextColor"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.HeaderTextColor"
options:[NSDictionary dictionaryWithObjectsAndKeys:
@"NSUnarchiveFromData", NSValueTransformerNameBindingOption,
[NSColor blackColor], NSNullPlaceholderBindingOption, nil]];
// smart quotes bound to user defaults on leopard
if ( [textView respondsToSelector:@selector(isAutomaticQuoteSubstitutionEnabled)] )
[textView bind:@"automaticQuoteSubstitutionEnabled"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.EntryTextUseSmartQuotes"
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:NSNullPlaceholderBindingOption]];
if ( [textView respondsToSelector:@selector(isAutomaticLinkDetectionEnabled)] )
[textView bind:@"automaticLinkDetectionEnabled"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.EntryTextRecognizeURLs"
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:NSNullPlaceholderBindingOption]];
}
- (void) dealloc
{
#ifdef __DEBUG__
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
// no more notifications
[[NSNotificationCenter defaultCenter] removeObserver:self];
// release the nib objects
[contentView release];
[headerView release];
[statusBar release];
// release the local objects
[stylesBar release];
[selectedEntry release];
[selectedEntries release];
[openSmartQuote release];
[closeSmartQuote release];
[objectController release];
[draggedResource release];
[super dealloc];
}
#pragma mark -
- (void) installTextSystem
{
// scroll view bounds: 1,1,480,509
// container bounds: 0,0,482,511 (contentView)
// everything enabled except hidden and allows document background color change
// attributedString is bound to ownerController selection selectedEntry.attributedContent w/ continuously updates value
// editable is bound to ownerController selection selectedEntry NSIsNotNil transformer
KBWordCountingTextStorage *textStorage = [[KBWordCountingTextStorage alloc] init];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
//[[NSNotificationCenter defaultCenter] addObserver:self
// selector:@selector(wordCountDidChange:)
// name:KBTextStorageStatisticsDidChangeNotification
// object:textStorage];
[textStorage addLayoutManager:layoutManager];
[layoutManager release];
NSRect theFrame = [contentView frame];
theFrame.origin.x = 1;
theFrame.origin.y = 1;
theFrame.size.width -= 2;
theFrame.size.height -= 2;
NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:theFrame];
[scrollView setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)];
[scrollView setHasVerticalScroller:YES];
[scrollView setHasHorizontalScroller:NO];
[[scrollView contentView] setAutoresizesSubviews:YES];
[[scrollView contentView] setBackgroundColor:[NSColor controlColor]];
if (NSInterfaceStyleForKey(NSInterfaceStyleDefault, scrollView) == NSWindows95InterfaceStyle) {
[scrollView setBorderType:NSBezelBorder];
}
[scrollView setBackgroundColor:[NSColor whiteColor]];
[scrollView setDrawsBackground:YES];
NSSize size = [scrollView contentSize];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(size.width,FLT_MAX)];
[textContainer setWidthTracksTextView:YES];
[textContainer setHeightTracksTextView:NO]; /* Not really necessary */
[layoutManager addTextContainer:textContainer];
[textContainer release];
textView = [[LinksOnlyNSTextView alloc] initWithFrame:NSMakeRect(0.0, 0.0, size.width, size.height) textContainer:textContainer];
[textView setUsesFontPanel:YES];
[textView setUsesFindPanel:YES];
[textView setAllowsUndo:YES];
[textView setAllowsDocumentBackgroundColorChange:NO];
[textView setContinuousSpellCheckingEnabled:YES];
[textView setImportsGraphics:YES];
[textView setRichText:YES];
[textView setHorizontallyResizable:NO]; /* Not really necessary */
[textView setVerticallyResizable:YES];
[textView setAutoresizingMask:NSViewWidthSizable];
[textView setMinSize:size]; /* Not really necessary; will be adjusted by the autoresizing... */
[textView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)]; /* Will be adjusted by the autoresizing... */
[textView setBackgroundColor:[NSColor whiteColor]];
[textView setDrawsBackground:YES];
// bindings
[textView bind:@"attributedString" toObject:objectController withKeyPath:@"selection.selectedEntry.attributedContent"
options:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSAllowsEditingMultipleValuesSelectionBindingOption,
[NSNumber numberWithBool:YES], NSConditionallySetsEditableBindingOption,
[NSNumber numberWithBool:YES], NSContinuouslyUpdatesValueBindingOption,
[NSNumber numberWithBool:YES], NSRaisesForNotApplicableKeysBindingOption, nil]];
[textView bind:@"editable" toObject:objectController withKeyPath:@"selection.selectedEntry"
options:[NSDictionary dictionaryWithObjectsAndKeys:@"NSIsNotNil", NSValueTransformerNameBindingOption, nil]];
[scrollView setDocumentView:textView];
[contentView addSubview:scrollView];
[textView doSetup];
[textView release];
[scrollView release];
}
#pragma mark -
- (NSView*) contentView {
return contentView;
}
- (NSView*) headerView {
return headerView;
}
- (LinksOnlyNSTextView*)textView {
return textView;
}
- (NSTextField*) titleField {
return titleField;
}
#pragma mark -
- (JournlerEntry*) selectedEntry
{
return selectedEntry;
}
- (void) setSelectedEntry:(JournlerEntry*)anEntry
{
if ( ![selectedEntry isEqual:anEntry] )
{
[selectedEntry release];
selectedEntry = [anEntry retain];
// pass the entry to the text view
[textView setSelectedRange:NSMakeRange(0,0)];
[textView setEntry:anEntry];
[textView setSelectedRange:NSMakeRange(0,0)];
[textView scrollRangeToVisible:NSMakeRange(0,0)];
// set the default style if the entry's length is 0
if ( [[anEntry valueForKey:@"attributedContent"] length] == 0 )
[textView applyDefaultStyleAndRuler];
[[textView window] invalidateCursorRectsForView:textView];
}
}
- (NSArray*) selectedEntries
{
return selectedEntries;
}
- (void) setSelectedEntries:(NSArray*)anArray
{
loadingEntries = YES;
if ( selectedEntries != anArray )
{
[selectedEntries release];
selectedEntries = [anArray retain];
// determine the single, selected entry
if ( [selectedEntries count] == 1 )
[self setSelectedEntry:[selectedEntries objectAtIndex:0]];
else
{
[self setSelectedEntry:nil];
}
}
loadingEntries = NO;
}
- (JournlerJournal*) journal
{
return journal;
}
- (void) setJournal:(JournlerJournal*) aJournal
{
if ( journal != aJournal )
{
[journal release];
journal = [aJournal retain];
}
}
- (id) delegate
{
return delegate;
}
- (void) setDelegate:(id)anObject
{
delegate = anObject;
}
#pragma mark -
- (void) setFullScreen:(BOOL)isFullScreen
{
// pass the message to the text view to set the inset
[textView setFullScreen:isFullScreen];
// reset our scale value
NSInteger scale;
if ( isFullScreen == YES )
scale = [[NSUserDefaults standardUserDefaults] integerForKey:@"EntryTextFullscreenZoom"];
else
scale = [[NSUserDefaults standardUserDefaults] integerForKey:@"EntryTextDefaultZoom"];
NSMenuItem *scaleItem = [[scalePop menu] itemWithTag:scale];
if ( scaleItem != nil )
{
[scalePop selectItem:scaleItem];
[[scaleItem target] performSelector:[scaleItem action] withObject:scaleItem];
}
// set the margin on the text view - depends on fullscreen status
NSInteger margin = ( isFullScreen
? [[NSUserDefaults standardUserDefaults] integerForKey:@"EntryTextHorizontalInsetFullscreen"]
: [[NSUserDefaults standardUserDefaults] integerForKey:@"EntryTextHorizontalInset"] );
NSMenuItem *marginItem = [[marginPop menu] itemWithTag:margin];
if ( marginItem != nil )
{
[marginPop selectItem:marginItem];
[[marginItem target] performSelector:[marginItem action] withObject:marginItem];
}
}
#pragma mark -
- (BOOL) headerHidden
{
return headerHidden;
}
- (void) setHeaderHidden:(BOOL)hidden
{
static NSInteger kHeaderHeight = 80;
if ( headerHidden != hidden )
{
headerHidden = hidden;
NSRect textFrame = [[textView enclosingScrollView] frame];
NSRect stylesFrame = [[stylesBar view] frame];
if ( headerHidden == YES )
{
textFrame.size.height += kHeaderHeight;
stylesFrame.origin.y += kHeaderHeight;
[headerView retain];
[headerView removeFromSuperview];
NSInteger contentBorders[4] = {1,0,0,0};
[contentView setBorders:contentBorders];
}
else
{
NSRect contentFrame = [contentView frame];
NSRect headerFrame = NSMakeRect( 1, contentFrame.size.height - kHeaderHeight, contentFrame.size.width - 2, kHeaderHeight);
textFrame.size.height -= kHeaderHeight;
stylesFrame.origin.y -= kHeaderHeight;
[headerView retain];
[headerView removeFromSuperview];
[headerView setFrame:headerFrame];
[contentView addSubview:headerView];
NSInteger contentBorders[4] = {0,0,0,0};
[contentView setBorders:contentBorders];
}
[[textView enclosingScrollView] setFrame:textFrame];
[[stylesBar view] setFrame:stylesFrame];
// adjust the styles bar
/*
if ( [self stylesBarVisible] )
{
NSRect scrollFrame = [[textView enclosingScrollView] frame];
NSRect stylesFrame = [[stylesBar view] frame];
scrollFrame.size.height -= stylesFrame.size.height;
stylesFrame.size.width = scrollFrame.size.width;
stylesFrame.origin.x = scrollFrame.origin.x;
stylesFrame.origin.y = scrollFrame.origin.y + scrollFrame.size.height;
[[stylesBar view] setFrame:stylesFrame];
[[textView enclosingScrollView] setFrame:scrollFrame];
}
*/
[contentView setNeedsDisplay:YES];
}
}
- (BOOL) footerHidden
{
return footerHidden;
}
- (void) setFooterHidden:(BOOL)hidden
{
if ( footerHidden != hidden )
{
footerHidden = hidden;
NSRect footerFrame;
NSRect textFrame = [[textView enclosingScrollView] frame];
[statusBar retain];
[statusBar removeFromSuperview];
if ( footerHidden )
{
textFrame.origin.y-=20;
textFrame.size.height+=20;
}
else
{
textFrame.origin.y+=20;
textFrame.size.height-=20;
footerFrame = NSMakeRect(textFrame.origin.x, textFrame.origin.y-21, textFrame.size.width, 20);
[statusBar setFrame:footerFrame];
[contentView addSubview:statusBar];
// update the live word count to get the latest measurement
if ( [self selectedEntry] != nil )
[self updateLiveCount];
}
[[textView enclosingScrollView] setFrame:textFrame];
[contentView setNeedsDisplay:YES];
}
}
- (BOOL) rulerVisible
{
return [textView isRulerVisible];
}
- (void) setRulerVisible:(BOOL)visible
{
//store this value in defaults
[[NSUserDefaults standardUserDefaults] setBool:visible forKey:@"EntryTextShowRuler"];
// assuming the presence of the ruler is bound to the styles bar
[self setStylesBarVisible:visible];
// determine the header borders
[self _determineHeaderBorders];
}
- (BOOL) stylesBarVisible
{
return stylesBarVisible;
}
- (void) setStylesBarVisible:(BOOL)visible
{
if ( stylesBarVisible != visible )
{
stylesBarVisible = visible;
NSRect scrollFrame = [[textView enclosingScrollView] frame];
NSRect stylesFrame = [[stylesBar view] frame];
if ( stylesBarVisible )
{
scrollFrame.size.height -= stylesFrame.size.height;
stylesFrame.size.width = scrollFrame.size.width;
stylesFrame.origin.x = scrollFrame.origin.x;
stylesFrame.origin.y = scrollFrame.origin.y + scrollFrame.size.height;
[[stylesBar view] setFrame:stylesFrame];
[contentView addSubview:[stylesBar view]];
}
else
{
scrollFrame.size.height += stylesFrame.size.height;
[[stylesBar view] removeFromSuperview];
}
[[textView enclosingScrollView] setFrame:scrollFrame];
[contentView setNeedsDisplay:YES];
}
}
#pragma mark -
- (NSColor*) headerBackgroundColor
{
return headerBackgroundColor;
}
- (void) setHeaderBackgroundColor:(NSColor*)aColor
{
if ( headerBackgroundColor != aColor )
{
[headerBackgroundColor release];
headerBackgroundColor = [aColor retain];
if ( headerBackgroundColor != nil )
{
NSColor *grayscaleColor = [headerBackgroundColor colorUsingColorSpace:[NSColorSpace genericGrayColorSpace]];
if ( [grayscaleColor isEqual:[NSColor whiteColor]] )
{
[self setHeaderIsWhite:YES];
//[headerView setBorders:(int[]){1,0,0,0}];
[headerView setGradientStartColor:[NSColor whiteColor]];
[headerView setGradientEndColor:[NSColor whiteColor]];
[self _determineHeaderBorders];
}
else
{
[self setHeaderIsWhite:NO];
//[headerView setBorders:(int[]){1,0,1,0}];
[headerView setGradientStartColor:[[headerBackgroundColor highlightWithLevel:0.2] colorWithAlphaComponent:0.6]];
[headerView setGradientEndColor:[[headerBackgroundColor shadowWithLevel:0.2] colorWithAlphaComponent:0.6]];
[self _determineHeaderBorders];
}
[headerView setNeedsDisplay:YES];
}
}
}
- (NSColor*) textBackgroundColor
{
return textBackgroundColor;
}
- (void) setTextBackgroundColor:(NSColor*)aColor
{
if ( textBackgroundColor != aColor )
{
[textBackgroundColor release];
textBackgroundColor = [aColor retain];
if ( textBackgroundColor != nil )
{
[textView setBackgroundColor:textBackgroundColor];
}
}
}
- (NSColor*) headerLabelColor
{
return headerLabelColor;
}
- (void) setHeaderLabelColor:(NSColor*)aColor
{
if ( headerLabelColor != aColor )
{
[headerLabelColor release];
headerLabelColor = [aColor retain];
if ( headerLabelColor == nil )
headerLabelColor = [[NSColor colorWithCalibratedWhite:0.75 alpha:1.0] retain];
}
}
- (NSColor*) headerTextColor
{
return headerTextColor;
}
- (void) setHeaderTextColor:(NSColor*)aColor
{
if ( headerTextColor != aColor )
{
[headerTextColor release];
headerTextColor = [aColor retain];
if ( headerTextColor == nil )
headerTextColor = [[NSColor colorWithCalibratedWhite:0.00 alpha:1.0] retain];
}
}
- (BOOL) headerIsWhite
{
return headerIsWhite;
}
- (void) setHeaderIsWhite:(BOOL)isWhite
{
headerIsWhite = isWhite;
}
#pragma mark -
-(void) _determineHeaderBorders
{
// depends on a white background and a visible ruler
if ( [self rulerVisible] )
{
[headerView setBorders:(int[]){1,0,1,0}];
}
else
{
if ( [self headerIsWhite] )
[headerView setBorders:(int[]){1,0,0,0}];
else
[headerView setBorders:(int[]){1,0,1,0}];
}
}
- (BOOL) commitEditing
{
return [objectController commitEditing];
}
- (IBAction) performFindPanelAction:(id)sender
{
[[[self contentView] window] makeFirstResponder:textView];
[textView performFindPanelAction:sender];
}
- (BOOL) highlightString:(NSString*)aString
{
if ( aString == nil || [aString length] == 0 )
return NO;
NSArray *components = [aString componentsSeparatedByString:@" "];
NSMutableArray *allRanges = [NSMutableArray array];
BOOL schonScrolled = NO;
for ( NSString *aComponent in components )
{
// get the range of the string and highlight it
NSArray *ranges = [[textView string] jn_rangesOfString:aComponent options:NSCaseInsensitiveSearch range:NSMakeRange(0,[[textView string] length])];
if ( ranges != nil && [ranges count] != 0 )
{
// put the term on the find clipboard, then highlight everywhere
if ( !schonScrolled )
{
if ( [aComponent length] != 0 )
{
NSPasteboard *findBoard = [NSPasteboard pasteboardWithName:NSFindPboard];
[findBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[findBoard setString:aComponent forType:NSStringPboardType];
}
schonScrolled = YES;
}
// store the ranges
[allRanges addObjectsFromArray:ranges];
}
}
if ( [allRanges count] > 0 )
{
// select the ranges
[textView setSelectedRanges:allRanges];
// scroll the first range to visible
[textView scrollRangeToVisible:[[allRanges objectAtIndex:0] rangeValue]];
return YES;
}
else
{
return NO;
}
}
- (void) appropriateFirstResponder:(NSWindow*)window
{
[window makeFirstResponder:textView];
}
- (void) appropriateFirstResponderForNewEntry:(NSWindow*)window
{
// fork the first repsonder based on preferece
if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"QuickEntryFocusesTitle"] && [self headerHidden] == NO )
[window makeFirstResponder:titleField];
else
[window makeFirstResponder:textView];
}
- (void) establishKeyViews:(NSView*)previous nextKeyView:(NSView*)next
{
[previous setNextKeyView:textView];
[textView setNextKeyView:titleField];
[titleField setNextKeyView:categoryField];
[categoryField setNextKeyView:tagsField];
[tagsField setNextKeyView:next];
}
- (void) ownerWillClose
{
[textView ownerWillClose:nil];
[objectController unbind:@"contentObject"];
[objectController setContent:nil];
[self unbind:@"headerBackgroundColor"];
[self unbind:@"textBackgroundColor"];
[self unbind:@"headerLabelColor"];
[self unbind:@"headerTextColor"];
}
- (IBAction) printDocument:(id)sender
{
//#warning implmenet
}
- (IBAction) setMargin:(id)sender
{
[self _setMargin:[sender tag]];
}
- (void) _setMargin:(NSInteger)margin
{
[[NSUserDefaults standardUserDefaults] setInteger:margin forKey:([textView inFullScreen] ? @"EntryTextHorizontalInsetFullscreen" : @"EntryTextHorizontalInset" )];
}
- (IBAction) scaleText:(id)sender
{
NSInteger scale = [sender tag];
[[NSUserDefaults standardUserDefaults] setInteger:scale forKey:([textView inFullScreen] ? @"EntryTextFullscreenZoom" : @"EntryTextDefaultZoom" )];
[textView scaleText:sender];
}
- (void) servicesMenuAppendSelection:(NSPasteboard*)pboard desiredType:(NSString*)type
{
if ( [self selectedEntry] == nil )
{
// ensure an entry is available before appending any data to it
if ( [[self delegate] respondsToSelector:@selector(entryCellController:newDefaultEntry:)] &&
![[self delegate] entryCellController:self newDefaultEntry:nil] )
return;
}
[textView setSelectedRange:NSMakeRange([[textView string] length],0)];
[textView insertText:@"\n\n"];
[textView readSelectionFromPasteboard:pboard type:type];
[textView insertText:@"\n\n"];
}
#pragma mark -
#pragma mark TextView Delegation
- (void)textViewDidChangeSelection:(NSNotification *)aNotification
{
if ( [aNotification object] == textView && [self footerHidden] == NO
&& [[NSUserDefaults standardUserDefaults] boolForKey:@"EntryTextShowWordCount"] )
{
#ifdef __DEBUG__
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
[self updateLiveCount];
}
}
- (void) textView:(LinksOnlyNSTextView*)aTextView rulerToggling:(NSNotification*)aNotification
{
[self setRulerVisible:[textView isRulerVisible]];
}
- (BOOL) textView:(LinksOnlyNSTextView*)aTextView newDefaultEntry:(NSNotification*)aNotification
{
if ( [[self delegate] respondsToSelector:@selector(entryCellController:newDefaultEntry:)] )
return [[self delegate] entryCellController:self newDefaultEntry:aNotification];
else
return NO;
}
- (BOOL) textViewIsInFullscreenMode:(LinksOnlyNSTextView*)aTextView
{
if ( aTextView != textView )
return NO;
// pass it up the chain if the chain respects it, otherwise definitely not fullscreen
if ( [[self delegate] respondsToSelector:@selector(textViewIsInFullscreenMode:)] )
return [[self delegate] textViewIsInFullscreenMode:aTextView];
else
return NO;
}
- (void)textView:(NSTextView *)aTextView clickedOnCell:(id <NSTextAttachmentCell>)cell inRect:(NSRect)cellFrame atIndex:(NSUInteger)charIndex
{
// checks for a checkbox and reverses the value and picture
if ( aTextView != textView )
return;
NSString *actualPreferred = [[[cell attachment] fileWrapper] preferredFilename];
if ( !( [actualPreferred isEqualToString:@"PDCheckboxChecked.png"] || [actualPreferred isEqualToString:@"PDCheckboxUnchecked.png"] ) )
return;
NSString *preferredName;
NSImage *tempImage;
if ( [actualPreferred isEqualToString:@"PDCheckboxUnchecked.png"] )
{
tempImage = [NSImage imageNamed:@"checkboxchecked.tif"];
preferredName = @"PDCheckboxChecked.png";
}
else
{
tempImage = [NSImage imageNamed:@"checkboxunchecked.tif"];
preferredName = @"PDCheckboxUnchecked.png";
}
NSBitmapImageRep *bitmapRep = [[[NSBitmapImageRep alloc] initWithData:[tempImage TIFFRepresentation]] autorelease];
NSFileWrapper *newWrapper = [[[NSFileWrapper alloc]
initRegularFileWithContents:[bitmapRep representationUsingType:NSPNGFileType properties:nil]] autorelease];
[newWrapper setPreferredFilename:preferredName];
NSTextAttachment *newAttachment = [[[NSTextAttachment alloc] initWithFileWrapper:newWrapper] autorelease];
if ( ![aTextView shouldChangeTextInRange:NSMakeRange(charIndex,1)
replacementString:[NSString stringWithCharacters: (const unichar[]){NSAttachmentCharacter} length:1]] )
{
NSBeep();
return;
}
[[aTextView textStorage] beginEditing];