forked from arauchfuss/Simple-Comic
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTSSTSessionWindowController.m
1739 lines (1416 loc) · 49.3 KB
/
TSSTSessionWindowController.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) 2006-2009 Dancing Tortoise Software
Created by Alexander Rauchfuss
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
TSSTSessionWindowController.m
*/
#import <XADMaster/XADArchive.h>
#import "UKXattrMetadataStore.h"
#import "SimpleComicAppDelegate.h"
#import "TSSTSessionWindowController.h"
#import "TSSTPageView.h"
#import "TSSTSortDescriptor.h"
#import "TSSTImageUtilities.h"
#import "TSSTPage.h"
#import "TSSTManagedGroup.h"
#import "TSSTInfoWindow.h"
#import "TSSTThumbnailView.h"
#import "TSSTManagedSession.h"
#import "DTPolishedProgressBar.h"
#import "DTWindowCategory.h"
@implementation TSSTSessionWindowController
@synthesize pageTurn, pageNames, pageSortDescriptor;
/*!
Turns all of the toolbar images in to templates so that they look consistent with
othe Apple generated icons.
*/
+ (void)initialize
{
NSImage * segmentImage = [NSImage imageNamed: @"org_size"];
[segmentImage setTemplate: YES];
segmentImage = [NSImage imageNamed: @"Loupe"];
[segmentImage setTemplate: YES];
segmentImage = [NSImage imageNamed: @"rotate_l"];
[segmentImage setTemplate: YES];
segmentImage = [NSImage imageNamed: @"rotate_r"];
[segmentImage setTemplate: YES];
segmentImage = [NSImage imageNamed: @"win_scale"];
[segmentImage setTemplate: YES];
segmentImage = [NSImage imageNamed: @"hor_scale"];
[segmentImage setTemplate: YES];
segmentImage = [NSImage imageNamed: @"one_page"];
[segmentImage setTemplate: YES];
segmentImage = [NSImage imageNamed: @"two_page"];
[segmentImage setTemplate: YES];
segmentImage = [NSImage imageNamed: @"rl_order"];
[segmentImage setTemplate: YES];
segmentImage = [NSImage imageNamed: @"lr_order"];
[segmentImage setTemplate: YES];
segmentImage = [NSImage imageNamed: @"equal"];
[segmentImage setTemplate: YES];
segmentImage = [NSImage imageNamed: @"thumbnails"];
[segmentImage setTemplate: YES];
segmentImage = [NSImage imageNamed: @"extract"];
[segmentImage setTemplate: YES];
}
/*!
*/
- (id)initWithSession:(TSSTManagedSession *)aSession
{
self = [super init];
if (self != nil)
{
pageTurn = 0;
pageSelectionInProgress = None;
mouseMovedTimer = nil;
// closing = NO;
session = [aSession retain];
BOOL cascade = [session valueForKey: @"position"] ? NO : YES;
[self setShouldCascadeWindows: cascade];
/* Make sure that the session does not start out in fullscreen, nor with the loupe enabled. */
[session setValue: @NO forKey: @"loupe"];
/* Images are sorted by group and then image name. */
TSSTSortDescriptor * fileNameSort = [[TSSTSortDescriptor alloc] initWithKey: @"imagePath" ascending: YES];
TSSTSortDescriptor * archivePathSort = [[TSSTSortDescriptor alloc] initWithKey: @"group.path" ascending: YES];
self.pageSortDescriptor = @[archivePathSort, fileNameSort];
[fileNameSort release];
[archivePathSort release];
}
return self;
}
- (NSString *)windowNibName
{
return @"TSSTSessionWindow";
}
/* Sets up all of the observers and bindings. */
- (void)awakeFromNib
{
/* This needs to be set as the window subclass that the expose window
uses has mouse events turned off by default */
[exposeBezel setIgnoresMouseEvents: NO];
[exposeBezel setFloatingPanel: YES];
[exposeBezel setWindowController: self];
[[self window] setAcceptsMouseMovedEvents: YES];
[pageController setSelectionIndex: [[session valueForKey: @"selection"] intValue]];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults addObserver: self forKeyPath: TSSTConstrainScale options: 0 context: nil];
[defaults addObserver: self forKeyPath: TSSTStatusbarVisible options: 0 context: nil];
[defaults addObserver: self forKeyPath: TSSTScrollersVisible options: 0 context: nil];
[defaults addObserver: self forKeyPath: TSSTBackgroundColor options: 0 context: nil];
[defaults addObserver: self forKeyPath: TSSTLoupeDiameter options: 0 context: nil];
[defaults addObserver: self forKeyPath: TSSTLoupePower options: 0 context: nil];
[session addObserver: self forKeyPath: TSSTPageOrder options: 0 context: nil];
[session addObserver: self forKeyPath: TSSTPageScaleOptions options: 0 context: nil];
[session addObserver: self forKeyPath: TSSTTwoPageSpread options: 0 context: nil];
[session addObserver: self forKeyPath: @"loupe" options: 0 context: nil];
[session bind: @"selection" toObject: pageController withKeyPath: @"selectionIndex" options: nil];
[pageScrollView setPostsFrameChangedNotifications: YES];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resizeView) name: NSViewFrameDidChangeNotification object: pageScrollView];
[pageController addObserver: self forKeyPath: @"selectionIndex" options: 0 context: nil];
[pageController addObserver: self forKeyPath: @"arrangedObjects.@count" options: 0 context: nil];
[progressBar addObserver: self forKeyPath: @"currentValue" options: 0 context: nil];
[progressBar bind: @"currentValue" toObject: pageController withKeyPath: @"selectionIndex" options: nil];
[progressBar bind: @"maxValue" toObject: pageController withKeyPath: @"arrangedObjects.@count" options: nil];
[progressBar bind: @"leftToRight" toObject: session withKeyPath: TSSTPageOrder options: nil];
[pageView bind: TSSTViewRotation toObject: session withKeyPath: TSSTViewRotation options: nil];
NSTrackingArea * newArea = [[NSTrackingArea alloc] initWithRect: [progressBar progressRect]
options: NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow | NSTrackingActiveInActiveApp
owner: self
userInfo: @{@"purpose": @"normalProgress"}];
[progressBar addTrackingArea: newArea];
[newArea release];
[jumpField setDelegate: self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMouseDragged:) name:@"SCMouseDragNotification" object:nil];
[self restoreSession];
}
- (void)dealloc
{
[(TSSTThumbnailView *)exposeView setDataSource: nil];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObserver: self forKeyPath: TSSTStatusbarVisible];
[defaults removeObserver: self forKeyPath: TSSTScrollersVisible];
[defaults removeObserver: self forKeyPath: TSSTBackgroundColor];
[defaults removeObserver: self forKeyPath: TSSTConstrainScale];
[defaults removeObserver: self forKeyPath: TSSTLoupeDiameter];
[defaults removeObserver: self forKeyPath: TSSTLoupePower];
[pageController removeObserver: self forKeyPath: @"selectionIndex"];
[pageController removeObserver: self forKeyPath: @"arrangedObjects.@count"];
[[NSNotificationCenter defaultCenter] removeObserver: self];
[progressBar removeObserver: self forKeyPath: @"currentValue"];
[progressBar unbind: @"currentValue"];
[progressBar unbind: @"maxValue"];
[progressBar unbind: @"leftToRight"];
[pageView setSessionController: nil];
[pageSortDescriptor release];
[pageNames release];
[session release];
[super dealloc];
}
/* Observes changes to the page controller. Changes are reflected by the
page view. */
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if([[pageController arrangedObjects] count] <= 0)
{
[self close];
// [[NSNotificationCenter defaultCenter] postNotificationName: TSSTSessionEndNotification object: self];
return;
}
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
if([keyPath isEqualToString: TSSTScrollersVisible])
{
[self scaleToWindow];
}
else if([keyPath isEqualToString: @"currentValue"])
{
if(object == progressBar)
{
[pageController setSelectionIndex: [progressBar currentValue]];
}
}
else if([keyPath isEqualToString: @"arrangedObjects.@count"])
{
[NSThread detachNewThreadSelector: @selector(processThumbs) toTarget: exposeView withObject: nil];
[self changeViewImages];
}
else if([keyPath isEqualToString: TSSTPageOrder])
{
[defaults setValue: [session valueForKey: TSSTPageOrder] forKey: TSSTPageOrder];
[(TSSTThumbnailView *)exposeView setNeedsDisplay: YES];
[(TSSTThumbnailView *)exposeView buildTrackingRects];
[self changeViewImages];
}
else if([keyPath isEqualToString: TSSTPageScaleOptions])
{
[defaults setValue: [session valueForKey: TSSTPageScaleOptions] forKey: TSSTPageScaleOptions];
[self scaleToWindow];
}
else if([keyPath isEqualToString: TSSTTwoPageSpread])
{
[defaults setValue: [session valueForKey: TSSTTwoPageSpread] forKey: TSSTTwoPageSpread];
[self changeViewImages];
}
else if([keyPath isEqualToString: TSSTBackgroundColor])
{
NSColor * color = [NSUnarchiver unarchiveObjectWithData: [defaults valueForKey: TSSTBackgroundColor]];
[pageScrollView setBackgroundColor: color];
}
else if([keyPath isEqualToString: TSSTStatusbarVisible])
{
[self adjustStatusBar];
}
else if([keyPath isEqualToString: TSSTLoupeDiameter])
{
int loupeDiameter = [[defaults valueForKey: TSSTLoupeDiameter] intValue];
[loupeWindow resizeToDiameter: loupeDiameter];
}
else if([keyPath isEqualToString: @"loupe"])
{
[self refreshLoupePanel];
}
else if([keyPath isEqualToString: TSSTLoupePower])
{
[self refreshLoupePanel];
}
else
{
[self changeViewImages];
}
}
#pragma mark -
#pragma mark Progress Bar
- (NSImage *)imageForPageAtIndex:(int)index
{
return [[pageController arrangedObjects][index] valueForKey: @"thumbnail"];
}
- (NSString *)nameForPageAtIndex:(int)index
{
return [[pageController arrangedObjects][index] valueForKey: @"name"];
}
#pragma mark -
#pragma mark Event handling
- (void)mouseMoved:(NSEvent *)theEvent
{
NSRect progressRect;
NSPoint windowLocation = [theEvent locationInWindow];
progressRect = [progressBar convertRect: [progressBar progressRect] toView: nil];
if(NSMouseInRect(windowLocation, progressRect, [progressBar isFlipped]))
{
[self infoPanelSetupAtPoint: windowLocation];
}
[self refreshLoupePanel];
}
- (void)mouseEntered:(NSEvent *)theEvent
{
NSString * purpose = [(NSDictionary *)[theEvent userData] valueForKey: @"purpose"];
if([purpose isEqualToString: @"normalProgress"])
{
[self infoPanelSetupAtPoint: [theEvent locationInWindow]];
[[self window] addChildWindow: infoWindow ordered: NSWindowAbove];
}
}
- (void)mouseExited:(NSEvent *)theEvent
{
if([theEvent trackingArea])
{
[[infoWindow parentWindow] removeChildWindow: infoWindow];
[infoWindow orderOut: self];
}
}
/* Handles mouse drag notifications relayed from progressbar */
- (void)handleMouseDragged:(NSNotification*)notification {
[infoWindow orderOut:self];
}
- (void)refreshLoupePanel
{
BOOL loupe = [[session valueForKey: @"loupe"] boolValue];
NSPoint mouse = [NSEvent mouseLocation];
NSRect point = NSMakeRect(mouse.x, mouse.y, 0, 0);
NSPoint localPoint = [pageView convertPoint: [[self window] convertRectFromScreen: point].origin fromView: nil];
NSPoint scrollPoint = [pageScrollView convertPoint: [[self window] convertRectFromScreen: point].origin fromView: nil];
if(NSMouseInRect(scrollPoint, [pageScrollView bounds], [pageScrollView isFlipped])
&& loupe
&& [[self window] isKeyWindow]
&& pageSelectionInProgress == None)
{
if(![loupeWindow isVisible])
{
[[self window] addChildWindow: loupeWindow ordered: NSWindowAbove];
[NSCursor hide];
}
NSRect zoomRect = [zoomView frame];
[loupeWindow centerAtPoint: mouse];
zoomRect.origin = localPoint;
[zoomView setImage: [pageView imageInRect: zoomRect]];
}
else
{
if([loupeWindow isVisible])
{
[[loupeWindow parentWindow] removeChildWindow: loupeWindow];
[loupeWindow orderOut: self];
}
[NSCursor unhide];
}
}
- (void)infoPanelSetupAtPoint:(NSPoint)point
{
NSPoint cursorPoint;
int index;
DTPolishedProgressBar * bar;
bar = progressBar;
[[infoWindow contentView] setBordered: NO];
point.y = (NSMaxY([bar frame]) - 6);
cursorPoint = [bar convertPoint: point fromView: nil];
index = [bar indexForPoint: cursorPoint];
NSImage * thumb = [self imageForPageAtIndex: index];
NSSize thumbSize = sizeConstrainedByDimension([thumb size], 128);
[infoPicture setFrameSize: thumbSize];
[infoPicture setImage: thumb];
NSRect area = NSMakeRect(point.x, point.y, 0, 0);
cursorPoint = [[self window] convertRectToScreen: area].origin;
[infoWindow caretAtPoint: cursorPoint size: NSMakeSize(thumbSize.width, thumbSize.height)
withLimitLeft: NSMinX([[bar window] frame])
right: NSMaxX([[bar window] frame])];
}
#pragma mark -
#pragma mark Actions
- (IBAction)changeTwoPage:(id)sender
{
BOOL spread = ![[session valueForKey: TSSTTwoPageSpread] boolValue];
[session setValue: @(spread) forKey: TSSTTwoPageSpread];
}
- (IBAction)changePageOrder:(id)sender
{
BOOL pageOrder = ![[session valueForKey: TSSTPageOrder] boolValue];
[session setValue: @(pageOrder) forKey: TSSTPageOrder];
}
- (IBAction)changeScaling:(id)sender
{
int scaleType = [sender tag] % 400;
[session setValue: @(scaleType) forKey: TSSTPageScaleOptions];
}
- (IBAction)turnPage:(id)sender
{
int segmentTag = [[sender cell] tagForSegment: [sender selectedSegment]];
if(segmentTag == 701)
{
[self pageLeft: self];
}
else if(segmentTag == 702)
{
[self pageRight: self];
}
}
/*! Method flips the page to the right calling nextPage or previousPage
depending on the prefered page ordering.
*/
- (IBAction)pageRight:(id)sender
{
[self setPageTurn: 2];
if([[session valueForKey: TSSTPageOrder] boolValue])
{
[self nextPage];
}
else
{
[self previousPage];
}
}
/*! Method flips the page to the left calling nextPage or previousPage
depending on the prefered page ordering.
*/
- (IBAction)pageLeft:(id)sender
{
[self setPageTurn: 1];
if([[session valueForKey: TSSTPageOrder] boolValue])
{
[self previousPage];
}
else
{
[self nextPage];
}
}
- (IBAction)shiftPageRight:(id)sender
{
if([[session valueForKey: TSSTPageOrder] boolValue])
{
[pageController selectNext: sender];
}
else
{
[pageController selectPrevious: sender];
}
}
- (IBAction)shiftPageLeft:(id)sender
{
if([[session valueForKey: TSSTPageOrder] boolValue])
{
[pageController selectPrevious: sender];
}
else
{
[pageController selectNext: sender];
}
}
- (IBAction)skipRight:(id)sender
{
int index;
if([[session valueForKey: TSSTPageOrder] boolValue])
{
index = ([pageController selectionIndex] + 10);
index = index < [[pageController content] count] ? index : [[pageController content] count] - 1;
}
else
{
index = ([pageController selectionIndex] - 10);
index = index > 0 ? index : 0;
}
[pageController setSelectionIndex: index];
}
- (IBAction)skipLeft:(id)sender
{
int index;
if(![[session valueForKey: TSSTPageOrder] boolValue])
{
index = ([pageController selectionIndex] + 10);
index = index < [[pageController content] count] ? index : [[pageController content] count] - 1;
}
else
{
index = ([pageController selectionIndex] - 10);
index = index > 0 ? index : 0;
}
[pageController setSelectionIndex: index];
}
- (IBAction)firstPage:(id)sender
{
[pageController setSelectionIndex: 0];
}
- (IBAction)lastPage:(id)sender
{
[pageController setSelectionIndex: [[pageController content] count] - 1];
}
/* Zoom method for the zoom segmented control. Each segment has its own tag. */
- (IBAction)zoom:(id)sender
{
int segmentTag = [[sender cell] tagForSegment: [sender selectedSegment]];
if(segmentTag == 801)
{
[self zoomIn: self];
}
else if(segmentTag == 802)
{
[self zoomOut: self];
}
else if(segmentTag == 803)
{
[self zoomReset: self];
}
}
- (IBAction)zoomIn:(id)sender
{
int scalingOption = [[session valueForKey: TSSTPageScaleOptions] intValue];
float previousZoom = [[session valueForKey: TSSTZoomLevel] floatValue];
if(scalingOption != 0)
{
previousZoom = NSWidth([pageView imageBounds]) / [pageView combinedImageSizeForZoom: 1].width;
}
previousZoom += 0.1;
[session setValue: @(previousZoom) forKey: TSSTZoomLevel];
[session setValue: @0 forKey: TSSTPageScaleOptions];
[pageView resizeView];
[self refreshLoupePanel];
}
- (IBAction)zoomOut:(id)sender
{
int scalingOption = [[session valueForKey: TSSTPageScaleOptions] intValue];
float previousZoom = [[session valueForKey: TSSTZoomLevel] floatValue];
if(scalingOption != 0)
{
previousZoom = NSWidth([pageView imageBounds]) / [pageView combinedImageSizeForZoom: 1].width;
}
previousZoom -= 0.1;
previousZoom = previousZoom < 0.1 ? 0.1 : previousZoom;
[session setValue: @(previousZoom) forKey: TSSTZoomLevel];
[session setValue: @0 forKey: TSSTPageScaleOptions];
[pageView resizeView];
[self refreshLoupePanel];
}
- (IBAction)zoomReset:(id)sender
{
[session setValue: @0 forKey: TSSTPageScaleOptions];
[session setValue: @1.0f forKey: TSSTZoomLevel];
[pageView resizeView];
[self refreshLoupePanel];
}
- (IBAction)rotate:(id)sender
{
int segmentTag = [[sender cell] tagForSegment: [sender selectedSegment]];
if(segmentTag == 901)
{
[self rotateLeft: self];
}
else if(segmentTag == 902)
{
[self rotateRight: self];
}
}
- (IBAction)rotateRight:(id)sender
{
int currentRotation = [[session valueForKey: TSSTViewRotation] intValue];
currentRotation = currentRotation + 1 > 3 ? 0 : currentRotation + 1;
[session setValue: @(currentRotation) forKey: TSSTViewRotation];
[self resizeWindow];
[self refreshLoupePanel];
}
- (IBAction)rotateLeft:(id)sender
{
int currentRotation = [[session valueForKey: TSSTViewRotation] intValue];
currentRotation = currentRotation - 1 < 0 ? 3 : currentRotation - 1;
[session setValue: @(currentRotation) forKey: TSSTViewRotation];
[self resizeWindow];
[self refreshLoupePanel];
}
- (IBAction)noRotation:(id)sender
{
[session setValue: @0 forKey: TSSTViewRotation];
[self resizeWindow];
[self refreshLoupePanel];
}
- (IBAction)toggleLoupe:(id)sender
{
BOOL loupe = [[session valueForKey: @"loupe"] boolValue];
loupe = !loupe;
[session setValue: @(loupe) forKey: @"loupe"];
}
- (IBAction)togglePageExpose:(id)sender
{
if([exposeBezel isVisible])
{
[[thumbnailPanel parentWindow] removeChildWindow: thumbnailPanel];
[thumbnailPanel orderOut: self];
[exposeBezel orderOut: self];
[[self window] makeKeyAndOrderFront: self];
[[self window] makeFirstResponder: pageView];
}
else
{
[NSCursor unhide];
[(TSSTThumbnailView *)exposeView buildTrackingRects];
[exposeBezel setFrame: [[[self window] screen] frame] display: NO];
[exposeBezel makeKeyAndOrderFront: self];
[NSThread detachNewThreadSelector: @selector(processThumbs) toTarget: exposeView withObject: nil];
}
}
- (IBAction)launchJumpPanel:(id)sender
{
[jumpField setIntValue: [pageController selectionIndex] + 1];
[self.window beginSheet: jumpPanel completionHandler:^(NSModalResponse returnCode) { }];
}
- (IBAction)cancelJumpPanel:(id)sender
{
[self.window endSheet: jumpPanel returnCode: NSModalResponseAbort];
}
- (IBAction)goToPage:(id)sender
{
if([jumpField integerValue] != NSNotFound)
{
int index = [jumpField intValue] < 1 ? 0 : [jumpField intValue] - 1;
[pageController setSelectionIndex: index];
}
[self.window endSheet: jumpPanel returnCode: NSModalResponseContinue];
}
- (IBAction)removePages:(id)sender
{
pageSelectionInProgress = Delete;
[self changeViewForSelection];
}
/* Method that allows the user to select an icon for comic archives.
Calls pageView and verifies that the images selected are from an
archive. */
- (IBAction)setArchiveIcon:(id)sender
{
pageSelectionInProgress = Icon;
[self changeViewForSelection];
}
/* Saves the selected page to a user specified location. */
- (IBAction)extractPage:(id)sender
{
pageSelectionInProgress = Extract;
[self changeViewForSelection];
}
- (BOOL)pageSelectionCanCrop
{
return (pageSelectionInProgress == Icon);
}
/* Used by all of the page selection methods to make both pages visible. Also adds a small
gutter around the images for cropping. */
- (void)changeViewForSelection
{
savedZoom = [[session valueForKey: TSSTZoomLevel] floatValue];
[pageScrollView setHasVerticalScroller: NO];
[pageScrollView setHasHorizontalScroller: NO];
[self refreshLoupePanel];
NSSize imageSize = [pageView combinedImageSizeForZoom: 1];
NSSize scrollerBounds = [[pageView enclosingScrollView] bounds].size;
scrollerBounds.height -= 20;
scrollerBounds.width -= 20;
float factor;
if(imageSize.width / imageSize.height > scrollerBounds.width / scrollerBounds.height)
{
factor = scrollerBounds.width / imageSize.width;
}
else
{
factor = scrollerBounds.height / imageSize.height;
}
[session setValue: @(factor) forKey: TSSTZoomLevel];
[pageView resizeView];
}
- (BOOL)canSelectPageIndex:(NSInteger)selection
{
int index = [pageController selectionIndex];
index += selection;
TSSTPage * selectedPage = [pageController arrangedObjects][index];
TSSTManagedGroup * selectedGroup = [selectedPage valueForKey: @"group"];
/* Makes sure that the group is both an archive and not nested */
if([selectedGroup class] == [TSSTManagedArchive class] &&
selectedGroup == [selectedGroup topLevelGroup] &&
![[selectedPage valueForKey: @"text"] boolValue])
{
return YES;
}
return NO;
}
- (BOOL)pageSelectionInProgress
{
return (pageSelectionInProgress != None);
}
- (void)cancelPageSelection
{
[session setValue: @(savedZoom) forKey: TSSTZoomLevel];
pageSelectionInProgress = None;
[self scaleToWindow];
}
- (void)selectedPage:(NSInteger)selection withCropRect:(NSRect)cropRect
{
switch (pageSelectionInProgress)
{
case Icon:
[self setIconWithSelection: selection andCropRect: cropRect];
break;
case Delete:
[self deletePageWithSelection: selection];
break;
case Extract:
[self extractPageWithSelection: selection];
break;
default:
break;
}
[session setValue: @(savedZoom) forKey: TSSTZoomLevel];
pageSelectionInProgress = None;
[self scaleToWindow];
}
- (void)deletePageWithSelection:(NSInteger)selection
{
if(selection != -1)
{
int index = [pageController selectionIndex];
index += selection;
TSSTPage * selectedPage = [pageController arrangedObjects][index];
[pageController removeObject: selectedPage];
[[self managedObjectContext] deleteObject: selectedPage];
}
}
- (void)extractPageWithSelection:(NSInteger)selection
{
/* selectpage returns prompts the user for which page they wish to use.
If there is only one page or the user selects the first page 0 is returned,
otherwise 1. */
if(selection != -1)
{
int index = [pageController selectionIndex];
index += selection;
TSSTPage * selectedPage = [pageController arrangedObjects][index];
NSSavePanel * savePanel = [NSSavePanel savePanel];
[savePanel setTitle: @"Extract Page"];
[savePanel setPrompt: @"Extract"];
[savePanel setNameFieldStringValue:[selectedPage name]];
if(NSModalResponseOK == [savePanel runModal])
{
[[selectedPage pageData] writeToFile: [[savePanel URL] path] atomically: YES];
}
}
}
- (void)setIconWithSelection:(NSInteger)selection andCropRect:(NSRect)cropRect
{
if(selection != -1)
{
int index = [pageController selectionIndex];
index += selection;
TSSTPage * selectedPage = [pageController arrangedObjects][index];
TSSTManagedGroup * selectedGroup = [selectedPage valueForKey: @"group"];
/* Makes sure that the group is both an archive and not nested */
if([selectedGroup class] == [TSSTManagedArchive class] &&
selectedGroup == [selectedGroup topLevelGroup] &&
![[selectedPage valueForKey: @"text"] boolValue])
{
NSString * archivePath = [[selectedGroup valueForKey: @"path"] stringByStandardizingPath];
if([(TSSTManagedArchive *)selectedGroup quicklookCompatible])
{
int coverIndex = [[selectedPage valueForKey: @"index"] intValue];
XADPath * coverName = [(XADArchive *)[selectedGroup instance] rawNameOfEntry: coverIndex];
[UKXattrMetadataStore setString: [coverName stringWithEncoding: NSNonLossyASCIIStringEncoding]
forKey: @"QCCoverName"
atPath: archivePath
traverseLink: NO];
[UKXattrMetadataStore setString: NSStringFromRect(cropRect)
forKey: @"QCCoverRect"
atPath: archivePath
traverseLink: NO];
[NSTask launchedTaskWithLaunchPath: @"/usr/bin/touch"
arguments: @[archivePath]];
}
else
{
NSRect drawRect = NSMakeRect(0, 0, 496, 496);
NSImage * iconImage = [[NSImage alloc] initWithSize: drawRect.size];
cropRect.size = NSEqualSizes(cropRect.size, NSZeroSize) ? NSMakeSize([[selectedPage valueForKey: @"width"] floatValue], [[selectedPage valueForKey: @"height"] floatValue]) : cropRect.size;
drawRect = rectWithSizeCenteredInRect( cropRect.size, drawRect);
[iconImage lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
[[selectedPage pageImage] drawInRect: drawRect fromRect: cropRect operation: NSCompositeSourceOver fraction: 1];
[iconImage unlockFocus];
NSImage * shadowImage = [[NSImage alloc] initWithSize: NSMakeSize(512, 512)];
NSShadow * thumbShadow = [NSShadow new];
[thumbShadow setShadowOffset: NSMakeSize(0.0, -8.0)];
[thumbShadow setShadowBlurRadius: 25.0];
[thumbShadow setShadowColor: [NSColor colorWithCalibratedWhite: 0.2 alpha: 1.0]];
[shadowImage lockFocus];
[thumbShadow set];
[iconImage drawInRect: NSMakeRect(16, 16, 496, 496) fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1];
[shadowImage unlockFocus];
[[NSWorkspace sharedWorkspace] setIcon: shadowImage forFile: archivePath options: 0];
[thumbShadow release];
[iconImage release];
[shadowImage release];
}
}
}
[session setValue: @(savedZoom) forKey: TSSTZoomLevel];
}
#pragma mark -
#pragma mark Convenience Methods
- (void)hideCursor
{
mouseMovedTimer = nil;
if([[self window] isFullscreen])
{
[NSCursor setHiddenUntilMouseMoves: YES];
}
}
/* When a session is launched this method is called. It checks to see if the
session was a saved session or one that is brand new. If it was a saved
session then all of the saved session information is passed to the window
and view. */
- (void)restoreSession
{
[self changeViewImages];
[self scaleToWindow];
[self adjustStatusBar];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
int loupeDiameter = [[defaults valueForKey: TSSTLoupeDiameter] intValue];
[loupeWindow setFrame:NSMakeRect(0,0, loupeDiameter, loupeDiameter) display: NO];
NSColor * color = [NSUnarchiver unarchiveObjectWithData: [defaults valueForKey: TSSTBackgroundColor]];
[pageScrollView setBackgroundColor: color];
[pageView setRotation: [[session valueForKey: TSSTViewRotation] intValue]];
NSValue * positionValue;
NSData * posData = [session valueForKey: @"position"];
if(posData)
{
positionValue = [NSUnarchiver unarchiveObjectWithData: posData];
[[self window] setFrame: [positionValue rectValue] display: NO];
NSData * scrollData = [session valueForKey: TSSTScrollPosition];
if(scrollData)
{