-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyWindowController.m
1708 lines (1389 loc) · 53.9 KB
/
MyWindowController.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
/*
* Copyright (C) 2012 Alasdair Morrison <[email protected]>
* This file is part of ProductivePDF.
* ProductivePDF is free software and comes with ABSOLUTELY NO WARRANTY.
* See LICENSE for details.
*/
#import "MyApplication.h"
#import "MyWindowController.h"
#define kPDFViewXDelta 0
#define kPDFViewYDelta 70
#define kURLLink 0
#define kDestinationLink 1
static NSString *ToolbarBackForward = @"Back Forward";
static NSString *ToolbarPreviousPage = @"Previous Page";
static NSString *ToolbarNextPage = @"Next Page";
static NSString *ToolbarPageNumber = @"Page Number";
static NSString *ToolbarViewMode = @"View Mode";
static NSString *ToolbarSearch = @"Search";
static NSString *ToolbarEditTest = @"EditTest";
static NSString *ToolbarToggleDrawer = @"ToggleDrawer";
@implementation MyWindowController
// =================================================================================================== MyWindowController
// -------------------------------------------------------------------------------------------------------- windowDidLoad
- (void) windowDidLoad
{
PDFDocument *pdfDoc;
NSRect visibleScreen;
NSSize pageSize;
float scaleFactor;
// Create PDFDocument.
pdfDoc = [[PDFDocument alloc] initWithURL: [NSURL fileURLWithPath: [[self document] fileName]]];
// Set document.
[_pdfView setDocument: pdfDoc];
[pdfDoc release];
// Default display mode.
[_pdfView setAutoScales: YES];
[_pdfView setDisplaysPageBreaks: NO];
// Get outline (if any).
_outline = [[[_pdfView document] outlineRoot] retain];
if (_outline)
{
if ([[_pdfView document] isLocked] == NO)
{
[_outlineView reloadData];
[_outlineView setAutoresizesOutlineColumn: NO];
// Expand items.
if ([_outlineView numberOfRows] == 1)
[_outlineView expandItem: [_outlineView itemAtRow: 0] expandChildren: NO];
[self updateOutlineSelection];
// Always open drawer if there is an outline and unencrypted PDF.
[[[[self window] drawers] objectAtIndex: 0] open];
}
}
// How big to create the window?
// Visible frame for main screen.
visibleScreen = [[NSScreen mainScreen] visibleFrame];
// Taking into account the toolbars, etc. in the UI.
visibleScreen.size.width -= kPDFViewXDelta;
visibleScreen.size.height -= kPDFViewYDelta;
// If continuous and multi-page, subtract space for a vertical scrollbar.
if ((([_pdfView displayMode] & 0x01) == 0x01) && ([[_pdfView document] pageCount] > 1))
visibleScreen.size.width -= [NSScroller scrollerWidth];
// Page size.
pageSize = [_pdfView rowSizeForPage: [_pdfView currentPage]];
// Determine limiting scale factor.
scaleFactor = visibleScreen.size.width / pageSize.width;
if (visibleScreen.size.height / pageSize.height < scaleFactor)
scaleFactor = visibleScreen.size.height / pageSize.height;
// Scale bounds.
pageSize.width = floorf(pageSize.width * scaleFactor);
pageSize.height = floorf(pageSize.height * scaleFactor);
// Set the window size.
[[self window] setContentSize: pageSize];
// Close the search results.
[self setSearchResultsViewHeight: 0];
// Create toolbar.
[self setupToolbarForWindow: [self window]];
// Internal notification.
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(newActiveAnnotation:)
name: @"newActiveAnnotation" object: _pdfView];
// Establish notifications for this document.
[self setupDocumentNotifications];
// State.
[self updateLinkTools];
}
// ------------------------------------------------------------------------------------------- setupDocumentNotifications
- (void) setupDocumentNotifications
{
// Find notifications.
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(startFind:)
name: PDFDocumentDidBeginFindNotification object: [_pdfView document]];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(endFind:)
name: PDFDocumentDidEndFindNotification object: [_pdfView document]];
// Document saving progress notifications.
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(documentBeginWrite:)
name: @"PDFDidBeginDocumentWrite" object: [_pdfView document]];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(documentEndWrite:)
name: @"PDFDidEndDocumentWrite" object: [_pdfView document]];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(documentEndPageWrite:)
name: @"PDFDidEndPageWrite" object: [_pdfView document]];
// Delegate.
[[_pdfView document] setDelegate: self];
}
// -------------------------------------------------------------------------------------------------------------- dealloc
- (void) dealloc
{
// No more notifications.
[[NSNotificationCenter defaultCenter] removeObserver: self];
// Remove back-forward toolbar item.
if (_toolbarBackForwardItem)
{
[_toolbarBackForwardItem release];
[_backForwardView release];
}
// Remove page number toolbar item.
if (_toolbarPageNumberItem)
{
[_toolbarPageNumberItem release];
[_pageNumberView release];
}
// Remove page number toolbar item.
if (_toolbarViewModeItem)
{
[_toolbarViewModeItem release];
[_viewModeView release];
}
// Remove search toolbar item.
if (_toolbarSearchFieldItem)
{
[_toolbarSearchFieldItem release];
[_searchFieldView release];
}
// Remove back-forward toolbar item.
if (_toolbarEditTestItem)
{
[_toolbarEditTestItem release];
[_editTestView release];
}
// Search clean-up.
[_searchResults release];
[_sampleStrings release];
// Release outline.
if (_outline)
[_outline release];
// Call super.
[super dealloc];
}
// -------------------------------------------------------------------------------------------------------------- pdfView
- (PDFView *) pdfView
{
return _pdfView;
}
// ------------------------------------------------------------------------------------------- setSearchResultsViewHeight
- (void) setSearchResultsViewHeight: (float) height
{
NSRect frameBounds;
float wasHeight;
NSArray *subViews;
// Get subviews of split view.
//subViews = [_splitView subviews];
// Get current height of search results view (view on top, view zero).
frameBounds = [[subViews objectAtIndex: 0] frame];
wasHeight = frameBounds.size.height;
// Set it's frame to reflect new height.
frameBounds.size.height = height;
frameBounds.origin.y += wasHeight - height;
// Adjust lower view (PDFView, view 1).
[[subViews objectAtIndex: 0] setFrame: frameBounds];
frameBounds = [[subViews objectAtIndex: 1] frame];
frameBounds.size.height += wasHeight - height;
[[subViews objectAtIndex: 1] setFrame: frameBounds];
// Do we need to call this? It doesn't seem to hurt.
//[_splitView adjustSubviews];
}
#pragma mark -------- NSTableView delegate methods
// ---------------------------------------------------------------------------------------------- numberOfRowsInTableView
- (int) numberOfRowsInTableView: (NSTableView *) aTableView
{
/*if (aTableView == _searchResultsTable)
return ([_searchResults count]);
else
*/return 0;
}
// ------------------------------------------------------------------------------ tableView:objectValueForTableColumn:row
- (id) tableView: (NSTableView *) aTableView objectValueForTableColumn: (NSTableColumn *) theColumn row: (int) rowIndex
{
/*if (aTableView == _searchResultsTable)
{
if ([[theColumn identifier] isEqualToString: @"page"])
return ([[[[_searchResults objectAtIndex: rowIndex] pages] objectAtIndex: 0] label]);
else if ([[theColumn identifier] isEqualToString: @"section"])
return ([[[_pdfView document] outlineItemForSelection: [_searchResults objectAtIndex: rowIndex]] label]);
else if ([[theColumn identifier] isEqualToString: @"text"])
return ([_sampleStrings objectAtIndex: rowIndex]);
else
return NULL;
}
else
{
return NULL;
}*/
}
// ------------------------------------------------------------------------------------------ tableViewSelectionDidChange
- (void) tableViewSelectionDidChange: (NSNotification *) notification
{
int rowIndex;
/*if ([notification object] == _searchResultsTable)
{
// What was selected. Skip out if the row has not changed.
rowIndex = [(NSTableView *)[notification object] selectedRow];
if (rowIndex >= 0)
{
[_pdfView setCurrentSelection: [_searchResults objectAtIndex: rowIndex]];
[_pdfView scrollSelectionToVisible: self];
}
}*/
}
#pragma mark -------- NSOutlineView methods
// ----------------------------------------------------------------------------------- outlineView:numberOfChildrenOfItem
- (int) outlineView: (NSOutlineView *) outlineView numberOfChildrenOfItem: (id) item
{
if (item == NULL)
{
if ((_outline) && ([[_pdfView document] isLocked] == NO))
return [_outline numberOfChildren];
else
return 0;
}
else
return [(PDFOutline *)item numberOfChildren];
}
// --------------------------------------------------------------------------------------------- outlineView:child:ofItem
- (id) outlineView: (NSOutlineView *) outlineView child: (int) index ofItem: (id) item
{
if (item == NULL)
{
if ((_outline) && ([[_pdfView document] isLocked] == NO))
return [[_outline childAtIndex: index] retain];
else
return NULL;
}
else
return [[(PDFOutline *)item childAtIndex: index] retain];
}
// ----------------------------------------------------------------------------------------- outlineView:isItemExpandable
- (BOOL) outlineView: (NSOutlineView *) outlineView isItemExpandable: (id) item
{
if (item == NULL)
{
if ((_outline) && ([[_pdfView document] isLocked] == NO))
return ([_outline numberOfChildren] > 0);
else
return NO;
}
else
return ([(PDFOutline *)item numberOfChildren] > 0);
}
// ------------------------------------------------------------------------- outlineView:objectValueForTableColumn:byItem
- (id) outlineView: (NSOutlineView *) outlineView objectValueForTableColumn: (NSTableColumn *) tableColumn
byItem: (id) item
{
return [(PDFOutline *)item label];
}
// ---------------------------------------------------------------------------------------- outlineViewSelectionDidChange
- (void) outlineViewSelectionDidChange: (NSNotification *) notification
{
// Get the destination associated with the search result list. Tell the PDFView to go there.
if (([notification object] == _outlineView) && (_ignoreNotification == NO))
[_pdfView goToDestination: [[_outlineView itemAtRow: [_outlineView selectedRow]] destination]];
}
// --------------------------------------------------------------------------------------------- outlineViewItemDidExpand
- (void) outlineViewItemDidExpand: (NSNotification *) notification
{
[self updateOutlineSelection];
}
// ------------------------------------------------------------------------------------------- outlineViewItemDidCollapse
- (void) outlineViewItemDidCollapse: (NSNotification *) notification
{
[self updateOutlineSelection];
}
// ----------------------------------------------------------------------------------------------- updateOutlineSelection
- (void) updateOutlineSelection
{
PDFOutline *outlineItem;
int pageIndex;
int numRows;
int i;
// Skip out if this PDF has no outline.
if (_outline == NULL)
return;
// Get index of current page.
pageIndex = [[_pdfView document] indexForPage: [_pdfView currentPage]];
// Test that the current selection is still valid.
outlineItem = (PDFOutline *)[_outlineView itemAtRow: [_outlineView selectedRow]];
if ([[_pdfView document] indexForPage: [[outlineItem destination] page]] == pageIndex)
return;
// Walk outline view looking for best firstpage number match.
numRows = [_outlineView numberOfRows];
for (i = 0; i < numRows; i++)
{
// Get the destination of the given row....
outlineItem = (PDFOutline *)[_outlineView itemAtRow: i];
if ([[_pdfView document] indexForPage: [[outlineItem destination] page]] == pageIndex)
{
_ignoreNotification = YES;
[_outlineView selectRow: i byExtendingSelection: NO];
_ignoreNotification = NO;
break;
}
else if ([[_pdfView document] indexForPage: [[outlineItem destination] page]] > pageIndex)
{
_ignoreNotification = YES;
if (i < 1)
[_outlineView selectRow: 0 byExtendingSelection: NO];
else
[_outlineView selectRow: i - 1 byExtendingSelection: NO];
_ignoreNotification = NO;
break;
}
}
}
#pragma mark -------- toolbar methods
// ------------------------------------------------------------------------------------------------ setupToolbarForWindow
// Create a new toolbar instance, and attach it to our document window.
- (void) setupToolbarForWindow: (NSWindow *) window
{
NSToolbar *toolbar;
// Allocate it.
toolbar = [[NSToolbar alloc] initWithIdentifier: @"ProductivePDF Toolbar"];
// Set up toolbar properties: Allow customization, give a default display mode, and
// remember state in user defaults.
[toolbar setAllowsUserCustomization: YES];
[toolbar setAutosavesConfiguration: YES];
[toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
// We are the delegate
[toolbar setDelegate: self];
// Attach the toolbar to the document window.
[window setToolbar: toolbar];
// Done.
[toolbar release];
}
// -------------------------------------------------------------- toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar
// Required delegate method. Given an item identifier, self method returns an
// item. The toolbar will use self method to obtain toolbar items that can be
// displayed in the customization sheet, or in the toolbar itself.
- (NSToolbarItem *) toolbar: (NSToolbar *) toolbar itemForItemIdentifier: (NSString *) itemIdent
willBeInsertedIntoToolbar: (BOOL) willBeInserted
{
NSToolbarItem *toolbarItem;
// Create a new toolbar item.
toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdent] autorelease];
if ([itemIdent isEqualToString: ToolbarBackForward])
{
// Set the text label to be displayed in the toolbar, customization palette and tooltip.
[toolbarItem setLabel: NSLocalizedString(@"Back/Forward", NULL)];
[toolbarItem setPaletteLabel: NSLocalizedString(@"Back/Forward", NULL)];
[toolbarItem setToolTip: NSLocalizedString(@"Go Back or Forward", NULL)];
// Set toolbar item view.
[_backForwardView retain];
[toolbarItem setView: _backForwardView];
[toolbarItem setMinSize: [_backForwardView frame].size];
[toolbarItem setMaxSize: [_backForwardView frame].size];
if (willBeInserted)
{
NSMenu *submenu = NULL;
NSMenuItem *submenuItem1 = NULL;
NSMenuItem *submenuItem2 = NULL;
NSMenuItem *menuFormRep = NULL;
// Create sub menu.
submenu = [[[NSMenu alloc] init] autorelease];
// Create Back menu item - add to sub menu.
submenuItem1 = [[[NSMenuItem alloc] initWithTitle: NSLocalizedString(@"Back", NULL)
action: @selector(doGoBack:) keyEquivalent: @""] autorelease];
[submenuItem1 setTarget: self];
[submenu addItem: submenuItem1];
// Create Forward menu item - add to sub menu.
submenuItem2 = [[[NSMenuItem alloc] initWithTitle: NSLocalizedString(@"Forward", NULL)
action: @selector(doGoForward:) keyEquivalent: @""] autorelease];
[submenuItem2 setTarget: self];
[submenu addItem: submenuItem2];
// Create menu form representation - set it.
menuFormRep = [[[NSMenuItem alloc] init] autorelease];
[menuFormRep setTitle: [toolbarItem label]];
[menuFormRep setSubmenu: submenu];
[toolbarItem setMenuFormRepresentation: menuFormRep];
}
}
else if ([itemIdent isEqualToString: ToolbarPreviousPage])
{
// Set the text label to be displayed in the toolbar, customization palette and tooltip.
[toolbarItem setLabel: NSLocalizedString(@"Previous", NULL)];
[toolbarItem setPaletteLabel: NSLocalizedString(@"Previous", NULL)];
[toolbarItem setToolTip: NSLocalizedString(@"Go To Previous Page", NULL)];
// Set image.
[toolbarItem setImage: [NSImage imageNamed: @"ToolbarPreviousPageImage"]];
// Tell the item what message to send when it is clicked.
[toolbarItem setTarget: self];
[toolbarItem setAction: @selector(doGoToPreviousPage:)];
}
else if ([itemIdent isEqualToString: ToolbarNextPage])
{
// Set the text label to be displayed in the toolbar and customization palette
[toolbarItem setLabel: NSLocalizedString(@"Next", NULL)];
[toolbarItem setPaletteLabel: NSLocalizedString(@"Next", NULL)];
// Set up a reasonable tooltip, and image.
[toolbarItem setToolTip: NSLocalizedString(@"Go To Next Page", NULL)];
[toolbarItem setImage: [NSImage imageNamed: @"ToolbarNextPageImage"]];
// Tell the item what message to send when it is clicked.
[toolbarItem setTarget: self];
[toolbarItem setAction: @selector(doGoToNextPage:)];
}
else if ([itemIdent isEqual: ToolbarPageNumber])
{
// Set up the standard properties .
[toolbarItem setLabel: NSLocalizedString(@"Page", NULL)];
[toolbarItem setPaletteLabel: NSLocalizedString(@"Page", NULL)];
[toolbarItem setToolTip: NSLocalizedString(@"Go To Page", NULL)];
// Set toolbar item view.
[_pageNumberView retain];
[toolbarItem setView: _pageNumberView];
[toolbarItem setMinSize: NSMakeSize(50, NSHeight([_pageNumberView frame]))];
[toolbarItem setMaxSize: NSMakeSize(56, NSHeight([_pageNumberView frame]))];
if (willBeInserted)
{
NSMenu *submenu = NULL;
NSMenuItem *submenuItem = NULL;
NSMenuItem *menuFormRep = NULL;
// Create sub menu.
submenu = [[[NSMenu alloc] init] autorelease];
// Create Page Dialog item.
submenuItem = [[[NSMenuItem alloc] initWithTitle: NSLocalizedString(@"Go To Page Panel", NULL)
action: @selector(doGoToPagePanel:) keyEquivalent: @""] autorelease];
[submenuItem setTarget: self];
[submenu addItem: submenuItem];
// Create menu form representation - set it.
menuFormRep = [[[NSMenuItem alloc] init] autorelease];
[menuFormRep setTitle: [toolbarItem label]];
[menuFormRep setSubmenu: submenu];
[toolbarItem setMenuFormRepresentation: menuFormRep];
}
else
{
toolbarItem = [[toolbarItem copy] autorelease];
[(NSTextField *)[toolbarItem view] setStringValue: @"--"];
}
}
else if ([itemIdent isEqualToString: ToolbarViewMode])
{
// Set the text label to be displayed in the toolbar, customization palette and tooltip.
[toolbarItem setLabel: NSLocalizedString(@"View Mode", NULL)];
[toolbarItem setPaletteLabel: NSLocalizedString(@"View Mode", NULL)];
[toolbarItem setToolTip: NSLocalizedString(@"Change the Viewing Mode", NULL)];
// Set toolbar item view.
[_viewModeView retain];
[toolbarItem setView: _viewModeView];
[toolbarItem setMinSize: [_viewModeView frame].size];
[toolbarItem setMaxSize: [_viewModeView frame].size];
}
else if ([itemIdent isEqual: ToolbarSearch])
{
// Set up the standard properties .
[toolbarItem setLabel: NSLocalizedString(@"Search", NULL)];
[toolbarItem setPaletteLabel: NSLocalizedString(@"Search", NULL)];
[toolbarItem setToolTip: NSLocalizedString(@"Search document", NULL)];
// Set toolbar item view.
[_searchFieldView retain];
[toolbarItem setView: _searchFieldView];
[toolbarItem setMinSize: NSMakeSize(128, NSHeight([_searchFieldView frame]))];
[toolbarItem setMaxSize: NSMakeSize(256, NSHeight([_searchFieldView frame]))];
if (willBeInserted)
{
NSMenu *submenu = NULL;
NSMenuItem *submenuItem = NULL;
NSMenuItem *menuFormRep = NULL;
// Create sub menu.
submenu = [[[NSMenu alloc] init] autorelease];
// Create Search panel item.
submenuItem = [[[NSMenuItem alloc] initWithTitle: NSLocalizedString(@"Search Panel", NULL)
action: @selector(doSearch:) keyEquivalent: @""] autorelease];
[submenuItem setTarget: self];
[submenu addItem: submenuItem];
// Create menu form representation - set it.
menuFormRep = [[[NSMenuItem alloc] init] autorelease];
[menuFormRep setTitle: [toolbarItem label]];
[menuFormRep setSubmenu: submenu];
[toolbarItem setMenuFormRepresentation: menuFormRep];
}
}
/*else if ([itemIdent isEqualToString: ToolbarEditTest])
{
// Set the text label to be displayed in the toolbar and customization palette
[toolbarItem setLabel: NSLocalizedString(@"Link Mode", NULL)];
[toolbarItem setPaletteLabel: NSLocalizedString(@"Link Mode", NULL)];
[toolbarItem setToolTip: NSLocalizedString(@"Switch between Edit and Test modes", NULL)];
// Use a custom view (a menu).
[_editTestView retain];
[toolbarItem setView: _editTestView];
[toolbarItem setMinSize: [[toolbarItem view] frame].size];
[toolbarItem setMaxSize: [[toolbarItem view] frame].size];
if (willBeInserted)
{
NSMenu *submenu = NULL;
NSMenuItem *submenuItem1 = NULL;
NSMenuItem *submenuItem2 = NULL;
NSMenuItem *menuFormRep = NULL;
// Tell the item what message to send when it is clicked.
[toolbarItem setTarget: self];
// Create sub menu.
submenu = [[[NSMenu alloc] init] autorelease];
// Create Media Box menu item - add to sub menu.
submenuItem1 = [[[NSMenuItem alloc] initWithTitle: NSLocalizedString(@"Edit Links", NULL)
action: @selector(doEditMode:) keyEquivalent: @""] autorelease];
[submenuItem1 setTarget: self];
[submenu addItem: submenuItem1];
// Create Crop Box menu item - add to sub menu.
submenuItem2 = [[[NSMenuItem alloc] initWithTitle: NSLocalizedString(@"Test Links", NULL)
action: @selector(doTestMode:) keyEquivalent: @""] autorelease];
[submenuItem2 setTarget: self];
[submenu addItem: submenuItem2];
// Create menu form representation - set it.
menuFormRep = [[[NSMenuItem alloc] init] autorelease];
[menuFormRep setTitle: [toolbarItem label]];
[menuFormRep setSubmenu: submenu];
[toolbarItem setMenuFormRepresentation: menuFormRep];
}
else
{
toolbarItem = [[toolbarItem copy] autorelease];
}
}*/
else if ([itemIdent isEqualToString: ToolbarToggleDrawer])
{
// Set the text label to be displayed in the toolbar and customization palette
[toolbarItem setLabel: NSLocalizedString(@"Outline", NULL)];
[toolbarItem setPaletteLabel: NSLocalizedString(@"Outline", NULL)];
// Set up a reasonable tooltip, and image.
[toolbarItem setToolTip: NSLocalizedString(@"Show or Hide Outline", NULL)];
[toolbarItem setImage: [NSImage imageNamed: @"ToolbarDrawerImage"]];
// Tell the item what message to send when it is clicked.
[toolbarItem setTarget: self];
[toolbarItem setAction: @selector(toggleDrawer:)];
}
else
{
// Not identified, not supported.
toolbarItem = NULL;
}
return toolbarItem;
}
// ---------------------------------------------------------------------------------------- toolbarDefaultItemIdentifiers
// Required delegate method. Returns the ordered list of items to be shown in
// the toolbar by default. If during the toolbar's initialization, no overriding
// values are found in the user defaults, or if the user chooses to revert to
// the default items self set will be used.
- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
{
return [NSArray arrayWithObjects:
ToolbarPreviousPage, ToolbarNextPage, ToolbarBackForward, ToolbarPageNumber,
NSToolbarSeparatorItemIdentifier,
ToolbarEditTest,
NSToolbarFlexibleSpaceItemIdentifier,
ToolbarSearch, ToolbarToggleDrawer,
NULL];
}
// ---------------------------------------------------------------------------------------- toolbarAllowedItemIdentifiers
// Required delegate method. Returns the list of all allowed items by identifier.
// By default, the toolbar does not assume any items are allowed, even the
// separator. So, every allowed item must be explicitly listed. The set of
// allowed items is used to construct the customization palette.
- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
{
return [NSArray arrayWithObjects:
ToolbarPreviousPage, ToolbarNextPage, ToolbarBackForward, ToolbarPageNumber, ToolbarViewMode,
ToolbarEditTest, ToolbarSearch, ToolbarToggleDrawer,
NSToolbarSeparatorItemIdentifier, NSToolbarSpaceItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier,
NSToolbarCustomizeToolbarItemIdentifier, NSToolbarPrintItemIdentifier,
NULL];
}
// --------------------------------------------------------------------------------------------------- toolbarWillAddItem
// Optional delegate method. Before an new item is added to the toolbar, self
// notification is posted self is the best place to notice a new item is going
// into the toolbar. For instance, if you need to cache a reference to the
// toolbar item or need to set up some initial state, self is the best place
// to do it. The notification object is the toolbar to which the item is being
// added. The item being added is found by referencing the @"item" key in the userInfo.
- (void) toolbarWillAddItem: (NSNotification *) theNotification
{
NSToolbarItem *addedItem;
// Toolbar item added.
addedItem = [[theNotification userInfo] objectForKey: @"item"];
// See if it is one we're interested in.
if ([[addedItem itemIdentifier] isEqualToString: ToolbarBackForward])
{
_toolbarBackForwardItem = [addedItem retain];
// Listen for these.
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateBackForwardState:)
name: PDFViewChangedHistoryNotification object: _pdfView];
// Update.
[self updateBackForwardState: NULL];
}
else if ([[addedItem itemIdentifier] isEqualToString: ToolbarPageNumber])
{
_toolbarPageNumberItem = [addedItem retain];
// Listen for these.
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updatePageNumberField:)
name: PDFViewPageChangedNotification object: _pdfView];
// Update.
[self updatePageNumberField: NULL];
}
else if ([[addedItem itemIdentifier] isEqualToString: ToolbarViewMode])
{
_toolbarViewModeItem = [addedItem retain];
// Update.
[self updateViewMode: NULL];
}
else if ([[addedItem itemIdentifier] isEqualToString: ToolbarSearch])
{
_toolbarSearchFieldItem = [addedItem retain];
}
else if ([[addedItem itemIdentifier] isEqualToString: ToolbarEditTest])
{
_toolbarEditTestItem = [addedItem retain];
}
}
// ------------------------------------------------------------------------------------------------- toolbarDidRemoveItem
- (void) toolbarDidRemoveItem: (NSNotification *) theNotification
{
NSToolbarItem *removedItem;
// Which item is going away?
removedItem = [[theNotification userInfo] objectForKey: @"item"];
if ([[removedItem itemIdentifier] isEqualToString: ToolbarBackForward])
{
// No longer listen.
[[NSNotificationCenter defaultCenter] removeObserver: self name: PDFViewChangedHistoryNotification
object: _pdfView];
// Release.
if (_toolbarBackForwardItem)
[_toolbarBackForwardItem release];
_toolbarBackForwardItem = NULL;
}
else if ([[removedItem itemIdentifier] isEqualToString: ToolbarPageNumber])
{
// No longer listen.
[[NSNotificationCenter defaultCenter] removeObserver: self name: PDFViewPageChangedNotification
object: _pdfView];
// Release.
if (_toolbarPageNumberItem)
[_toolbarPageNumberItem release];
_toolbarPageNumberItem = NULL;
}
else if ([[removedItem itemIdentifier] isEqualToString: ToolbarViewMode])
{
// Release.
if (_toolbarViewModeItem)
[_toolbarViewModeItem release];
_toolbarViewModeItem = NULL;
}
else if ([[removedItem itemIdentifier] isEqualToString: ToolbarSearch])
{
// Release.
if (_toolbarSearchFieldItem)
[_toolbarSearchFieldItem release];
_toolbarSearchFieldItem = NULL;
}
else if ([[removedItem itemIdentifier] isEqualToString: ToolbarBackForward])
{
// Release.
if (_toolbarEditTestItem)
[_toolbarEditTestItem release];
_toolbarEditTestItem = NULL;
}
}
// -------------------------------------------------------------------------------------------------- validateToolbarItem
// Optional method. Self message is sent to us since we are the target of some
// toolbar item actions (for example: of the save items action)
- (BOOL) validateToolbarItem: (NSToolbarItem *) toolbarItem
{
BOOL enable = YES;
if ([toolbarItem action] == @selector(doGoToPreviousPage:))
{
enable = [_pdfView canGoToPreviousPage];
}
else if ([toolbarItem action] == @selector(doGoToNextPage:))
{
enable = [_pdfView canGoToNextPage];
}
else if ([toolbarItem action] == @selector(doGoToPage:))
{
// Enabled if the current document is multipage.
enable = ([[_pdfView document] pageCount] > 1);
}
else if ([toolbarItem action] == @selector(toggleDrawer:))
{
// Enabled if we have an outline for this PDF.
enable = (_outline != NULL);
}
return enable;
}
// --------------------------------------------------------------------------------------------------------- saveDocument
- (void) saveDocument: (id) sender
{
[self writePDFToURL: NULL];
}
// ------------------------------------------------------------------------------------------------------- saveDocumentAs
- (void) saveDocumentAs: (id) sender
{
NSSavePanel *savePanel;
// Create save panel, require PDF.
savePanel = [NSSavePanel savePanel];
[savePanel setRequiredFileType: @"pdf"];
// Run save panel.
if ([savePanel runModal] == NSFileHandlingPanelOKButton)
[self writePDFToURL: [savePanel URL]];
}
// -------------------------------------------------------------------------------------------------------- writePDFToURL
- (void) writePDFToURL: (NSURL *) URL
{
// Currently in PDFKit, when a PDF is saved, the resulting PDF loses its outline.
if (_outline)
[_outline release];
_outline = NULL;
if (URL == NULL)
{
[[_pdfView document] writeToURL: [[_pdfView document] documentURL]];
}
else
{
[[_pdfView document] writeToURL: URL];
[_pdfView setDocument: [[[PDFDocument alloc] initWithURL: URL] autorelease]];
[self setupDocumentNotifications];
}
// Clear edited flag.
[[self window] setDocumentEdited: NO];
}
// -------------------------------------------------------------------------------------------------------- printDocument
- (void) printDocument: (id) sender
{
// Pass down to PDF view.
[_pdfView printDocument: sender];
}
#pragma mark -------- actions
// ------------------------------------------------------------------------------------------------------ doGoBackForward
- (void) doGoBackForward: (id) sender
{
// Segment with tag eqial to zero is the Back, otherwise Forward.
if ([[sender cell] tagForSegment: [sender selectedSegment]] == 0)
[_pdfView goBack: sender];
else
[_pdfView goForward: sender];
}
// ------------------------------------------------------------------------------------------------------------- doGoBack
- (void) doGoBack: (id) sender
{
[_pdfView goBack: sender];
}
// ---------------------------------------------------------------------------------------------------------- doGoForward
- (void) doGoForward: (id) sender
{
[_pdfView goForward: sender];
}
// ---------------------------------------------------------------------------------------------- refreshBackForwardState
- (void) updateBackForwardState: (NSNotification *) notification
{
// Update segemented control state.
[_backForwardView setEnabled: [_pdfView canGoBack] forSegment: 0];
[_backForwardView setEnabled: [_pdfView canGoForward] forSegment: 1];
}
// ------------------------------------------------------------------------------------------------------- doGoToNextPage
- (void) doGoToNextPage: (id) sender
{
[_pdfView goToNextPage: sender];
}
// --------------------------------------------------------------------------------------------------- doGoToPreviousPage
- (void) doGoToPreviousPage: (id) sender
{
[_pdfView goToPreviousPage: sender];
}
// ----------------------------------------------------------------------------------------------------------- doGoToPage
- (void) doGoToPage: (id) sender
{
int newPage;
// Make sure page number entered is valid.
newPage = [self getPageIndexFromLabel: [sender stringValue]];
if ((newPage < 1) || (newPage > [[_pdfView document] pageCount]))
{
// Error.
[self updatePageNumberField: NULL];
NSBeep();
}
else
{
// Go to that page.
[_pdfView goToPage: [[_pdfView document] pageAtIndex: newPage - 1]];
}
}
// ------------------------------------------------------------------------------------------------------ doGoToPagePanel
- (void) doGoToPagePanel: (id) sender
{
// Specify page range.
[_pageNumberPanelRange setStringValue: [NSString stringWithFormat:
NSLocalizedString(@"Go to page (%@ to %@):", NULL), [[[_pdfView document] pageAtIndex: 0] label],
[[[_pdfView document] pageAtIndex: [[_pdfView document] pageCount] - 1] label]]];
// Populate initially with current page label.
[_pageNumberPanelText setStringValue: [[_pdfView currentPage] label]];
// Bring up the page number panel as a sheet.
[NSApp beginSheet: _pageNumberPanel modalForWindow: [self window] modalDelegate: self
didEndSelector: @selector(goToPagePanelDidEnd: returnCode: contextInfo:) contextInfo: NULL];
}
// -------------------------------------------------------------------------------------------------- goToPagePanelDidEnd
- (void) goToPagePanelDidEnd: (NSWindow *) sheet returnCode: (int) returnCode contextInfo: (void *) contextInfo
{
// Close.
[_pageNumberPanel close];
// Make sure page number entered is valid.
if ((returnCode < 1) || (returnCode > [[_pdfView document] pageCount]))
{
// Zero may indicate user canceled, don't beep in that case.
if (returnCode != 0)
NSBeep();
return;
}
// Go to that page.