forked from veryweblog/ichm
-
Notifications
You must be signed in to change notification settings - Fork 26
/
CHMDocument.m
1220 lines (921 loc) · 39.5 KB
/
CHMDocument.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
//
// CHMDocument.m
// ichm
//
// Created by Robin Lu on 7/16/08.
// Copyright __MyCompanyName__ 2008 . All rights reserved.
//
#import "CHMDocument.h"
#import "CHMWebViewController.h"
#import "CHMAppController.h"
#import "BookmarkController.h"
#import "CHMWebView.h"
#import "CHMBookmark.h"
#define MD_DEBUG 1
#if MD_DEBUG
#define MDLog(...) NSLog(__VA_ARGS__)
#else
#define MDLog(...)
#endif
#define CHM_SIDEBAR_WIDTH_MIN 130.0
#define CHM_SIDEBAR_WIDTH_DEFAULT 176.0
static NSString * const ICHMToolbarIdentifier = @"ICHM Toolbar Identifier";
static NSString * const HistoryToolbarItemIdentifier = @"History Item Identifier";
static NSString * const TextSizeToolbarItemIdentifier = @"Text Size Item Identifier";
static NSString * const SearchToolbarItemIdentifier = @"Search Item Identifier";
static NSString * const HomeToolbarItemIdentifier = @"Home Item Identifier";
static NSString * const SidebarToolbarItemIdentifier = @"Sidebar Item Identifier";
static NSString * const WebVewPreferenceIndentifier = @"iCHM WebView Preferences";
static NSString * const CHMDocumentSidebarWidthKey = @"Sidebar Width";
static NSString * const CHMDocumentTextSizeMultiplierKey = @"zoom factor";
static NSString * const CHMDocumentFilesInfoKey = @"files info";
static NSString * const CHMDocumentUpdatedAtKey = @"updated at";
static NSString * const CHMDocumentLastPathKey = @"last path";
static NSString * const CHMDocumentSearchTypeKey = @"search type";
static NSString * const CHMDocumentSearchTypeIndex = @"index";
static NSString * const CHMDocumentSearchTypeFile = @"file";
static BOOL firstDocument = YES;
@interface CHMConsole : NSObject {
}
- (void)log:(NSString*)string;
@end
@implementation CHMConsole
- (void)log:(NSString *)string {
NSLog(@"%@", string);
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)selector {
if (selector == @selector(log:)) {
return NO;
}
return YES;
}
+ (NSString *)webScriptNameForSelector:(SEL)selector {
if (@selector(log:)) {
return @"log";
}
return nil;
}
@end
@interface CHMDocument ()
- (void)removeHighlight;
- (void)highlightString:(NSString *)pattern;
// file preferences
- (void)setPreference:(id)object forFile:(NSString *)filename withKey:(NSString *)key;
- (id)getPreferenceforFile:(NSString *)filename withKey:(NSString *)key;
- (void)setupToolbar;
- (void)updateToolbarButtons;
- (void)setupTabBar;
- (void)loadJavascript;
- (void)runJavascript:(NSString *)script;
- (void)restoreSidebar;
- (IBAction)hideSidebar:(id)sender;
- (void)after_zoom;
- (NSTabViewItem *)createWebViewInTab:(id)sender;
- (CHMTableOfContents *)currentDataSource;
- (void)chm__loadURL:(NSURL *)URL;
- (void)loadLinkItem:(CHMLinkItem *)anItem;
- (IBAction)revealCurrentItemInOutlineView:(id)sender;
@end
@implementation CHMDocument
@synthesize filePath;
@synthesize searchMode;
@synthesize viewMode;
@synthesize documentFile;
@synthesize currentLinkItem;
@synthesize pendingBookmarkToLoad;
+ (void)initialize {
/* This `initialized` flag is used to guard against the rare cases where Cocoa bindings
may cause `+initialize` to be called twice: once for this class, and once for the isa-swizzled class:
`[NSKVONotifying_MDClassName initialize]`
*/
static BOOL initialized = NO;
@synchronized(self) {
if (initialized == NO) {
NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
[defaults setObject:[NSNumber numberWithFloat:CHM_SIDEBAR_WIDTH_DEFAULT] forKey:CHMDocumentSidebarWidthKey];
[defaults setObject:[NSNumber numberWithFloat:1.0] forKey:CHMDocumentTextSizeMultiplierKey];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
[[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:defaults];
initialized = YES;
}
}
}
- (id)init {
if ((self = [super init])) {
// Add your subclass-specific initialization here.
// If an error occurs here, send a [self release] message and return nil.
webViews = [[NSMutableArray alloc] init];
console = [[CHMConsole alloc] init];
sidebarWidth = [[NSUserDefaults standardUserDefaults] floatForKey:CHMDocumentSidebarWidthKey];
isSidebarRestored = NO;
searchMode = CHMDocumentFileSearchInFile;
viewMode = CHMDocumentViewTableOfContents;
searchResults = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc {
MDLog(@"%@ - [%@ %@]", [self displayName], NSStringFromClass([self class]), NSStringFromSelector(_cmd));
[filePath release];
[webViews release];
[console release];
documentFile.searchDelegate = nil;
[documentFile release];
[currentLinkItem release];
[searchResults release];
exporter.delegate = nil;
[exporter release];
[pendingBookmarkToLoad release];
[super dealloc];
}
#pragma mark - NSDocument
- (NSString *)windowNibName {
return @"CHMDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController {
MDLog(@"%@ - [%@ %@]", [self displayName], NSStringFromClass([self class]), NSStringFromSelector(_cmd));
[super windowControllerDidLoadNib:aController];
[aController.window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
[self setupTabBar];
[self addNewTab:self];
[exportProgressIndicator setUsesThreadedAnimation:YES];
[outlineView setAutoresizesOutlineColumn:NO];
if (documentFile.tableOfContents.linkItems.numberOfChildren == 0) {
[self hideSidebar:self];
}
[self setupToolbar];
[self restoreSidebar];
if (pendingBookmarkToLoad) {
NSURL *bookmarkURL = [NSURL URLWithString:pendingBookmarkToLoad.url];
CHMLinkItem *bookmarkLinkItem = [documentFile linkItemAtPath:bookmarkURL.path];
(bookmarkLinkItem ? [self loadLinkItem:bookmarkLinkItem] : [self goHome:self]);
self.pendingBookmarkToLoad = nil;
} else {
// go to last viewed page
NSString *lastPath = (NSString *)[self getPreferenceforFile:filePath withKey:CHMDocumentLastPathKey];
if (lastPath == nil) {
[self goHome:self];
} else {
// old prefs saved relative paths, we now use absolute paths; make sure to make any relative paths to absolute
if (![lastPath hasPrefix:@"/"]) lastPath = [@"/" stringByAppendingPathComponent:lastPath];
CHMLinkItem *lastItem = [documentFile linkItemAtPath:lastPath];
(lastItem ? [self loadLinkItem:lastItem] : [self goHome:self]);
}
}
// set search type and search menu
NSString *type = [self getPreferenceforFile:filePath withKey:CHMDocumentSearchTypeKey];
if (type && [type isEqualToString:CHMDocumentSearchTypeIndex]) {
self.searchMode = CHMDocumentFileSearchInIndex;
[[searchField cell] setPlaceholderString:NSLocalizedString(@"Search in Index", @"")];
}
// invoke search if query string provided in command line
if (firstDocument) {
NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
NSString *searchTerm = [[args stringForKey:@"search"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (searchTerm && searchTerm.length) {
[searchField setStringValue:searchTerm];
self.searchMode = CHMDocumentFileSearchInFile;
[self search:self];
firstDocument = NO;
}
}
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
return nil;
}
- (BOOL)readFromURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError **)outError {
MDLog(@"[%@ %@] url.path == %@, type == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), url.path, typeName);
[filePath release];
filePath = [[url path] retain];
documentFile = [[CHMDocumentFile alloc] initWithContentsOfFile:filePath error:outError];
documentFile.searchDelegate = self;
return (documentFile != nil);
}
- (void)chm__loadURL:(NSURL *)URL {
// MDLog(@"[%@ %@] URL == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), URL);
if (URL == nil) return;
NSURLRequest *req = [NSURLRequest requestWithURL:URL];
[[curWebView mainFrame] loadRequest:req];
}
- (void)loadURL:(NSURL *)URL {
// MDLog(@"[%@ %@] URL == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), URL);
if (URL == nil) return;
CHMLinkItem *linkItem = [documentFile linkItemAtPath:URL.path];
if (linkItem) [self loadLinkItem:linkItem];
}
- (void)loadLinkItem:(CHMLinkItem *)anItem {
// MDLog(@"[%@ %@] anItem == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), anItem);
if (anItem == nil) return;
self.currentLinkItem = anItem;
NSURL *URL = [NSURL chm__ITSSURLWithPath:anItem.path];
[self chm__loadURL:URL];
}
- (void)setPreference:(id)object forFile:(NSString *)filename withKey:(NSString *)key {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *filesInfoList = [NSMutableDictionary dictionaryWithDictionary:[defaults dictionaryForKey:CHMDocumentFilesInfoKey]];
NSMutableDictionary *fileInfo = [NSMutableDictionary dictionaryWithDictionary:[filesInfoList objectForKey:filename]];
[fileInfo setObject:object forKey:key];
[fileInfo setObject:[NSDate date] forKey:CHMDocumentUpdatedAtKey];
[filesInfoList setObject:fileInfo forKey:filename];
if ([filesInfoList count] > 20) {
NSDictionary *oldest = nil;
NSString *oldestKey = nil;
for (NSString *key in[filesInfoList allKeys]) {
NSDictionary *info = [filesInfoList objectForKey:key];
if (oldest == nil || [[oldest objectForKey:CHMDocumentUpdatedAtKey] compare:[info objectForKey:CHMDocumentUpdatedAtKey]] == NSOrderedDescending) {
oldest = info;
oldestKey = key;
}
}
[oldestKey retain];
if (oldestKey) [filesInfoList removeObjectForKey:oldestKey];
[oldestKey release];
}
[defaults setObject:filesInfoList forKey:CHMDocumentFilesInfoKey];
}
- (id)getPreferenceforFile:(NSString *)filename withKey:(NSString *)key {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *filesInfoList = [defaults dictionaryForKey:CHMDocumentFilesInfoKey];
if (filesInfoList == nil) {
return nil;
}
NSDictionary *fileInfo = [filesInfoList objectForKey:filename];
if (fileInfo == nil) {
return nil;
}
return [fileInfo objectForKey:key];
}
#pragma mark - Properties
- (NSString *)currentURL {
if (curWebView) {
return [curWebView mainFrameURL];
}
return nil;
}
- (NSString *)currentTitle {
if (curWebView) {
return [[docTabView selectedTabViewItem] label];
}
return nil;
}
#pragma mark - <WebFrameLoadDelegate>
- (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
NSLog(@"[%@ %@] error == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error);
}
- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
if (error.domain == NSURLErrorDomain && error.code != NSURLErrorCancelled) {
NSLog(@"[%@ %@] error == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error);
}
}
- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
if (frame == [sender mainFrame]) {
// MDLog(@"[%@ %@] title == \"%@\"", NSStringFromClass([self class]), NSStringFromSelector(_cmd), title);
}
}
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
// MDLog(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
// MDLog(@"[%@ %@] (frame == [sender mainFrame]) == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), (frame == [sender mainFrame] ? @"YES" : @"NO"));
NSURL *URL = [[[frame dataSource] request] URL];
CHMLinkItem *newCurrentItem = [documentFile linkItemAtPath:URL.path];
if (newCurrentItem) self.currentLinkItem = newCurrentItem;
[self updateToolbarButtons];
[self revealCurrentItemInOutlineView:nil];
NSTabViewItem *tabItem = [docTabView selectedTabViewItem];
NSString *name = currentLinkItem.name;
if (name.length == 0) name = [curWebView mainFrameTitle];
[tabItem setLabel:(name.length ? name : NSLocalizedString(@"(Untitled)", @"(Untitled)"))];
if (frame == [sender mainFrame]) {
[[curWebView windowScriptObject] setValue:console forKey:@"console"];
[self loadJavascript];
NSString *searchString = [searchField stringValue];
if (searchString.length) {
[self highlightString:searchString];
[self findNext:self];
}
}
// setup last path
[self setPreference:URL.path forFile:filePath withKey:CHMDocumentLastPathKey];
}
#pragma mark - Javascript
- (void)loadJavascript {
NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"highlight" ofType:@"js"];
[self runJavascript:[NSString stringWithContentsOfFile:scriptPath encoding:NSUTF8StringEncoding error:NULL]];
}
- (void)runJavascript:(NSString *)script {
[[curWebView windowScriptObject] evaluateWebScript:[NSString stringWithFormat:@"try{ %@; } catch(e){console.log(e.toString());}", script]];
}
#pragma mark - <WebPolicyDelegate>
- (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
if ([CHMITSSURLProtocol canInitWithRequest:request]) {
int navigationType = [[actionInformation objectForKey:WebActionNavigationTypeKey] intValue];
unsigned int modifier = [[actionInformation objectForKey:WebActionModifierFlagsKey] unsignedIntValue];
// link click
if (navigationType == WebNavigationTypeLinkClicked && modifier) {
[self addNewTab:self];
[[curWebView mainFrame] loadRequest:request];
[listener ignore];
return;
}
[listener use];
} else {
[[NSWorkspace sharedWorkspace] openURL:[request URL]];
[listener ignore];
}
}
- (void)webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id<WebPolicyDecisionListener>)listener {
// MDLog(@"[%@ %@] request == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), request);
if ([CHMITSSURLProtocol canInitWithRequest:request]) {
[listener use];
} else {
[[NSWorkspace sharedWorkspace] openURL:[request URL]];
[listener ignore];
}
}
#pragma mark - <WebResourceLoadDelegate>
- (NSURLRequest *)webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource {
// MDLog(@"[%@ %@] request == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), request);
if ([CHMITSSURLProtocol canInitWithRequest:request]) {
NSMutableURLRequest *specialURLRequest = [[request mutableCopy] autorelease];
[specialURLRequest setDocumentFile:documentFile];
[specialURLRequest setEncodingName:documentFile.currentEncodingName];
return specialURLRequest;
} else {
return request;
}
}
#pragma mark - <WebUIDelegate>
- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
WebView *wv = [(CHMWebViewController *)[[self createWebViewInTab:sender] identifier] webView];
[[wv mainFrame] loadRequest:request];
return wv;
}
- (void)webViewShow:(WebView *)sender {
for (NSTabViewItem *item in [docTabView tabViewItems]) {
CHMWebViewController *chmwv = [item identifier];
if ([chmwv webView] == sender) {
curWebView = sender;
[docTabView selectTabViewItem:item];
}
}
}
#pragma mark - IBActions
- (IBAction)changeTopic:(id)sender {
if (ignoreOutlineViewSelectionChanges) return;
NSInteger selectedRow = [outlineView selectedRow];
if (selectedRow >= 0) {
CHMLinkItem *topic = [outlineView itemAtRow:selectedRow];
[self loadLinkItem:topic];
}
}
- (IBAction)openInNewTab:(id)sender {
[self addNewTab:sender];
[self changeTopic:sender];
}
- (IBAction)goForward:(id)sender {
[curWebView goForward];
}
- (IBAction)goBack:(id)sender {
[curWebView goBack];
}
- (IBAction)goHome:(id)sender {
[self loadLinkItem:[documentFile linkItemAtPath:documentFile.homePath]];
}
- (IBAction)goHistory:(id)sender {
NSSegmentedCell *segCell = sender;
switch ([segCell selectedSegment]) {
case 0:
[self goBack:sender];
break;
case 1:
[self goForward:sender];
break;
default:
break;
}
}
- (IBAction)gotoNextPage:(id)sender {
NSInteger selectedRow = [outlineView selectedRow];
CHMLinkItem *topic = [outlineView itemAtRow:selectedRow];
CHMLinkItem *nextPage = [documentFile.tableOfContents pageAfterPage:topic];
if (nextPage) [self loadLinkItem:nextPage];
}
- (IBAction)gotoPrevPage:(id)sender {
NSInteger selectedRow = [outlineView selectedRow];
CHMLinkItem *topic = [outlineView itemAtRow:selectedRow];
CHMLinkItem *prevPage = [documentFile.tableOfContents pageBeforePage:topic];
if (prevPage) [self loadLinkItem:prevPage];
}
- (IBAction)revealCurrentItemInOutlineView:(id)sender {
// MDLog(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
if (!isSearching) {
NSArray *ancestors = [currentLinkItem ancestors];
for (CHMLinkItem *parent in ancestors) {
if (![outlineView isItemExpanded:parent]) [outlineView expandItem:parent];
}
}
NSInteger currentItemIndex = [outlineView rowForItem:currentLinkItem];
if (currentItemIndex == -1) return;
NSIndexSet *selectedRowIndexes = [outlineView selectedRowIndexes];
if (![selectedRowIndexes containsIndex:currentItemIndex]) {
ignoreOutlineViewSelectionChanges = YES;
[outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:currentItemIndex] byExtendingSelection:NO];
ignoreOutlineViewSelectionChanges = NO;
}
[outlineView scrollRowToVisible:currentItemIndex];
}
- (IBAction)zoomIn:(id)sender {
[curWebView makeTextLarger:sender];
[self after_zoom];
}
- (IBAction)zoom:(id)sender {
NSSegmentedCell *segCell = sender;
switch ([segCell selectedSegment]) {
case 0:
[self zoomOut:sender];
break;
case 1:
[self zoomIn:sender];
break;
default:
break;
}
}
- (IBAction)zoomOut:(id)sender {
[curWebView makeTextSmaller:sender];
[self after_zoom];
}
- (void)after_zoom {
[textSizeControl setEnabled:[curWebView canMakeTextSmaller] forSegment:0];
[textSizeControl setEnabled:[curWebView canMakeTextLarger] forSegment:1];
float zoomFactor = [curWebView textSizeMultiplier];
[[NSUserDefaults standardUserDefaults] setFloat:zoomFactor forKey:CHMDocumentTextSizeMultiplierKey];
}
- (IBAction)printDocument:(id)sender {
NSView *docView = [[[curWebView mainFrame] frameView] documentView];
NSPrintOperation *operation = [NSPrintOperation printOperationWithView:docView printInfo:[self printInfo]];
operation.showsPrintPanel = YES;
[self runModalPrintOperation:operation delegate:nil didRunSelector:NULL contextInfo:NULL];
}
- (void)updateToolbarButtons {
[historyControl setEnabled:[curWebView canGoBack] forSegment:0];
[historyControl setEnabled:[curWebView canGoForward] forSegment:1];
[textSizeControl setEnabled:[curWebView canMakeTextSmaller] forSegment:0];
[textSizeControl setEnabled:[curWebView canMakeTextLarger] forSegment:1];
}
#pragma mark - export to pdf
- (IBAction)exportToPDF:(id)sender {
/* create or get the shared instance of NSSavePanel */
NSSavePanel *savePanel = [NSSavePanel savePanel];
[savePanel setTitle:NSLocalizedString(@"Save as PDF", @"Save as PDF")];
[savePanel setAllowedFileTypes:[NSArray arrayWithObjects:@"pdf", nil]];
[savePanel setNameFieldStringValue:[[filePath lastPathComponent] stringByDeletingPathExtension]];
/* display the NSSavePanel */
NSInteger runResult = [savePanel runModal];
/* if successful, save file under designated name */
if (runResult == NSOKButton) {
NSURL *URL = [savePanel URL];
exporter = [[CHMExporter alloc] initWithDocument:self destinationURL:URL pageList:documentFile.tableOfContents.pageList];
exporter.delegate = self;
[exporter beginExport];
}
}
#pragma mark - <CHMExporterDelegate>
- (void)exporterDidBeginExporting:(CHMExporter *)anExporter {
// MDLog(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
[exportProgressIndicator setIndeterminate:NO];
[exportNoticeLabel setStringValue:NSLocalizedString(@"Save as PDF", @"Save as PDF")];
[NSApp beginSheet:exportProgressSheet modalForWindow:documentWindow modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
}
- (void)exporter:(CHMExporter *)anExporter didExportPage:(NSUInteger)page percentageComplete:(CGFloat)percentageComplete {
NSString *title = NSLocalizedString(@"Save as PDF", @"Save as PDF");
NSString *label = [NSString stringWithFormat:@"%@ : %lu %@", title, (unsigned long)page, NSLocalizedString(@"pages", @"pages")];
[exportNoticeLabel setStringValue:label];
[exportProgressIndicator setDoubleValue:percentageComplete];
}
- (void)exporterDidFinishExporting:(CHMExporter *)anExporter {
[NSApp endSheet:exportProgressSheet];
[exportProgressSheet orderOut:nil];
exporter.delegate = nil;
[exporter autorelease];
exporter = nil;
}
#pragma mark <CHMExporterDelegate>
#pragma mark - TabVew
- (void)setupTabBar {
[tabBar setTabView:docTabView];
[tabBar setPartnerView:docTabView];
[tabBar setStyleNamed:@"Unified"];
[tabBar setDelegate:self];
[tabBar setShowAddTabButton:YES];
[tabBar setSizeCellsToFit:YES];
[[tabBar addTabButton] setTarget:self];
[[tabBar addTabButton] setAction:@selector(addNewTab:)];
}
- (NSTabViewItem *)createWebViewInTab:(id)sender {
CHMWebViewController *chmWebViewController = [[[CHMWebViewController alloc] init] autorelease];
// init the webview
WebView *newView = [chmWebViewController webView];
[(CHMWebView *)newView setDocument:self];
[newView setPreferencesIdentifier:WebVewPreferenceIndentifier];
if ([webViews count] == 0) {
// set preference
WebPreferences *pref = [newView preferences];
[pref setJavaScriptEnabled:YES];
[pref setUserStyleSheetEnabled:YES];
[pref setJavaScriptCanOpenWindowsAutomatically:YES];
[pref setAutosaves:YES];
NSString *stylePath = [[NSBundle mainBundle] pathForResource:@"ichm" ofType:@"css"];
NSURL *styleURL = [[NSURL alloc] initFileURLWithPath:stylePath];
[pref setUserStyleSheetLocation:styleURL];
[styleURL release];
}
[newView setPolicyDelegate:self];
[newView setFrameLoadDelegate:self];
[newView setUIDelegate:self];
[newView setResourceLoadDelegate:self];
[newView setTextSizeMultiplier:[[NSUserDefaults standardUserDefaults] floatForKey:CHMDocumentTextSizeMultiplierKey]];
// NOTE: the tab view item retains the CHMWebViewController instance
// create new tab item
NSTabViewItem *newItem = [[[NSTabViewItem alloc] init] autorelease];
[newItem setView:[chmWebViewController view]];
[newItem setLabel:@"(Untitled)"];
[newItem setIdentifier:chmWebViewController];
// add to tab view
[docTabView addTabViewItem:newItem];
[webViews addObject:newView];
return newItem;
}
- (IBAction)addNewTab:(id)sender {
NSTabViewItem *item = [self createWebViewInTab:sender];
curWebView = [(CHMWebViewController *)[item identifier] webView];
[docTabView selectTabViewItem:item];
}
- (IBAction)closeTab:(id)sender {
if ([webViews count] > 1) {
NSTabViewItem *item = [docTabView selectedTabViewItem];
[item retain];
[docTabView removeTabViewItem:item];
[[tabBar delegate] tabView:docTabView didCloseTabViewItem:item];
[item release];
} else {
[self close];
}
}
- (void)tabView:(NSTabView *)tabView didCloseTabViewItem:(NSTabViewItem *)tabViewItem {
[webViews removeObject:[[tabViewItem identifier] webView]];
[self updateToolbarButtons];
}
- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem {
curWebView = [(CHMWebViewController *)[tabViewItem identifier] webView];
[self updateToolbarButtons];
}
- (IBAction)chmDocumentSelectNextTabViewItem:(id)sender {
NSInteger tabViewItemCount = docTabView.numberOfTabViewItems;
NSInteger currentTabViewItemIndex = [docTabView indexOfTabViewItem:[docTabView selectedTabViewItem]];
if (currentTabViewItemIndex == tabViewItemCount - 1) {
[docTabView selectFirstTabViewItem:sender];
} else {
[docTabView selectNextTabViewItem:sender];
}
}
- (IBAction)chmDocumentSelectPreviousTabViewItem:(id)sender {
NSInteger currentIndex = [docTabView indexOfTabViewItem:[docTabView selectedTabViewItem]];
if (currentIndex == 0) {
[docTabView selectLastTabViewItem:sender];
} else {
[docTabView selectPreviousTabViewItem:sender];
}
}
#pragma mark - Toolbar
- (void)setupToolbar {
NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:ICHMToolbarIdentifier] autorelease];
[toolbar setAllowsUserCustomization:YES];
[toolbar setAutosavesConfiguration:YES];
[toolbar setDelegate:self];
[documentWindow setToolbar:toolbar];
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
return [NSArray arrayWithObjects:HistoryToolbarItemIdentifier, TextSizeToolbarItemIdentifier, NSToolbarSeparatorItemIdentifier, NSToolbarPrintItemIdentifier,
NSToolbarSpaceItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier, SidebarToolbarItemIdentifier, SearchToolbarItemIdentifier, nil];
}
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
return [NSArray arrayWithObjects:HistoryToolbarItemIdentifier, HomeToolbarItemIdentifier, TextSizeToolbarItemIdentifier, NSToolbarPrintItemIdentifier, NSToolbarSeparatorItemIdentifier,
NSToolbarSpaceItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier, SidebarToolbarItemIdentifier, SearchToolbarItemIdentifier, nil];
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdent willBeInsertedIntoToolbar:(BOOL)willBeInserted {
NSToolbarItem *toolbarItem = nil;
if ([itemIdent isEqual:TextSizeToolbarItemIdentifier]) {
toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
[toolbarItem setLabel:NSLocalizedString(@"Zoom", @"Zoom")];
[toolbarItem setPaletteLabel:NSLocalizedString(@"Zoom", @"Zoom")];
[toolbarItem setToolTip:NSLocalizedString(@"Zoom", @"Zoom")];
[toolbarItem setView:[textSizeControl superview]];
[textSizeControl setEnabled:[curWebView canMakeTextLarger] forSegment:0];
[textSizeControl setEnabled:[curWebView canMakeTextSmaller] forSegment:1];
} else if ([itemIdent isEqual:HistoryToolbarItemIdentifier]) {
toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
[toolbarItem setLabel:NSLocalizedString(@"History", @"History")];
[toolbarItem setPaletteLabel:NSLocalizedString(@"History", @"History")];
[toolbarItem setToolTip:NSLocalizedString(@"Navigate in History", @"Navigate in History")];
[toolbarItem setView:[historyControl superview]];
[historyControl setEnabled:NO forSegment:0];
[historyControl setEnabled:NO forSegment:1];
} else if ([itemIdent isEqual:HomeToolbarItemIdentifier]) {
toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
[toolbarItem setLabel:NSLocalizedString(@"Home", @"Home")];
[toolbarItem setPaletteLabel:NSLocalizedString(@"Home", @"Home")];
[toolbarItem setToolTip:NSLocalizedString(@"Back to Home", @"Back to Home")];
[toolbarItem setView:[homeButton superview]];
} else if ([itemIdent isEqual:SidebarToolbarItemIdentifier]) {
toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
[toolbarItem setLabel:NSLocalizedString(@"Sidebar", @"Sidebar")];
[toolbarItem setPaletteLabel:NSLocalizedString(@"Sidebar", @"Sidebar")];
[toolbarItem setToolTip:NSLocalizedString(@"Toggle Sidebar", @"Toggle Sidebar")];
[toolbarItem setView:toggleSidebarButton];
} else if ([itemIdent isEqual:SearchToolbarItemIdentifier]) {
toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
[toolbarItem setLabel:NSLocalizedString(@"Search", @"Search")];
[toolbarItem setPaletteLabel:NSLocalizedString(@"Search", @"Search")];
[toolbarItem setToolTip:NSLocalizedString(@"Search", @"Search")];
[toolbarItem setView:searchField];
} else {
// itemIdent refered to a toolbar item that is not provide or supported by us or cocoa
// Returning nil will inform the toolbar this kind of item is not supported
toolbarItem = nil;
}
return toolbarItem;
}
#pragma mark - <CHMDocumentFileSearchDelegate>
- (void)documentFile:(CHMDocumentFile *)aDocumentFile didUpdateSearchResults:(NSArray *)aSearchResults {
MDLog(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
[searchResults setArray:aSearchResults];
[outlineView reloadData];
}
#pragma mark <CHMDocumentFileSearchDelegate>
#pragma mark - Search
- (IBAction)changeSearchMode:(id)sender {
NSInteger tag = [sender tag];
if (searchMode == tag) return;
self.searchMode = tag;
[[searchField cell] setPlaceholderString:(searchMode == CHMDocumentFileSearchInFile ? NSLocalizedString(@"Search in File", @"") : NSLocalizedString(@"Search in Index", @""))];
[self setPreference:(searchMode == CHMDocumentFileSearchInFile ? CHMDocumentSearchTypeFile : CHMDocumentSearchTypeIndex) forFile:filePath withKey:CHMDocumentSearchTypeKey];
if ([searchField stringValue].length) [self search:self];
}
- (IBAction)search:(id)sender {
#if MD_DEBUG
NSLog(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
#endif
NSString *searchString = [searchField stringValue];
if (searchString.length == 0) {
isSearching = NO;
[searchResults removeAllObjects];
[outlineView reloadData];
if (viewMode == CHMDocumentViewTableOfContents) {
[[[outlineView outlineTableColumn] headerCell] setStringValue:NSLocalizedString(@"Contents", @"Contents")];
[self revealCurrentItemInOutlineView:nil];
} else if (viewMode == CHMDocumentViewIndex) {
[[[outlineView outlineTableColumn] headerCell] setStringValue:NSLocalizedString(@"Index", @"Index")];
}
[self removeHighlight];
return;
}
isSearching = YES;
[searchResults removeAllObjects];
[[[outlineView outlineTableColumn] headerCell] setStringValue:NSLocalizedString(@"Search", @"Search")];
[outlineView deselectAll:nil];
[documentFile searchForString:searchString usingMode:searchMode];
}
- (IBAction)focusOnSearch:(id)sender {
[documentWindow makeFirstResponder:searchField];
}
#pragma mark - find panel
- (IBAction)showFindPanel:(id)sender {
return [(CHMWebViewController *)[[docTabView selectedTabViewItem] identifier] showFindPanel:sender];
}
- (IBAction)beginFind:(id)sender {
NSString *searchString = [[[[docTabView selectedTabViewItem] identifier] searchField] stringValue];
if (searchString.length == 0) {
[self removeHighlight];
return;
}
[self highlightString:searchString];
[self findNext:sender];
}
- (void)highlightString:(NSString *)pattern {
[self runJavascript:[NSString stringWithFormat:@"highlight(document.body, '%@')", pattern]];
}
- (void)removeHighlight {
[self runJavascript:@"removeHighlight();"];
}
- (IBAction)findNext:(id)sender {
[self runJavascript:[NSString stringWithFormat:@"scrollToHighlight(%d)", 1]];
}
- (IBAction)findPrev:(id)sender {
[self runJavascript:[NSString stringWithFormat:@"scrollToHighlight(%d)", -1]];
}
- (IBAction)findInFile:(id)sender {
NSSegmentedCell *segCell = sender;
if ([segCell selectedSegment] == 0) {
[self findPrev:sender];
} else {
[self findNext:sender];
}
}
- (IBAction)doneFind:(id)sender {
[(CHMWebViewController *)[[docTabView selectedTabViewItem] identifier] hideFindPanel:sender];
[self removeHighlight];
}
#pragma mark - text encoding
- (IBAction)changeEncoding:(id)sender {
MDLog(@"[%@ %@] sender == %@, tag == %ld", NSStringFromClass([self class]), NSStringFromSelector(_cmd), sender, (long)[sender tag]);
NSInteger tag = [sender tag];
id representedObject = [sender representedObject];
// get the path of the currentLinkItem
NSString *previousCurrentItemPath = [[currentLinkItem.path retain] autorelease];
if (tag == 0 && documentFile.customEncoding) {
// go back to default encoding
[documentFile setCustomEncoding:CHMDocumentFileDefaultStringEncoding customEncodingName:nil];
} else if ((tag && documentFile.customEncoding == 0) // set a custom encoding
|| (tag && tag != documentFile.customEncoding)) { // set a different custom encoding
NSString *customEncodingName = nil;
if ([representedObject isKindOfClass:[NSString class]]) {
customEncodingName = (NSString *)representedObject;
} else {
customEncodingName = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(tag));
}
[documentFile setCustomEncoding:tag customEncodingName:customEncodingName];
MDLog(@"[%@ %@] customEncoding == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), [NSString localizedNameOfStringEncoding:tag]);
MDLog(@"[%@ %@] customEncodingName == %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), customEncodingName);
} else {
return;
}
// Setting a new encoding will recreate the table of contents and CHMLinkItem tree, so our currentLinkItem is no longer valid; replace it with the new one at the same path.
self.currentLinkItem = [documentFile linkItemAtPath:previousCurrentItemPath];
for (WebView *webView in webViews) {