summaryrefslogtreecommitdiffstats
path: root/macosx/HBPreviewController.m
blob: d13254ddc11254c4ed5b063de136aae264436949 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
/* $Id: HBPreviewController.mm,v 1.11 2005/08/01 15:10:44 titer Exp $

 This file is part of the HandBrake source code.
 Homepage: <http://handbrake.fr/>.
 It may be used under the terms of the GNU General Public License. */

#import "HBPreviewController.h"
#import "HBPreviewGenerator.h"
#import "HBUtilities.h"
#import "Controller.h"
#import <QTKit/QTKit.h>

@implementation QTMovieView (HBQTMovieViewExtensions)

- (void) mouseMoved: (NSEvent *) theEvent
{
    [super mouseMoved:theEvent];
}

@end

@implementation QTMovie (HBQTMovieExtensions)

- (BOOL) isPlaying
{
    if ([self rate])
        return YES;
    else
        return NO;
}

- (NSString *) timecode
{
    QTTime time = [self currentTime];
    double timeInSeconds = (double)time.timeValue / time.timeScale;
	UInt16 seconds = fmod(timeInSeconds, 60.0);
	UInt16 minutes = fmod(timeInSeconds / 60.0, 60.0);
	UInt16 hours = timeInSeconds / (60.0 * 60.0);
	UInt16 milliseconds = (timeInSeconds - (int) timeInSeconds) * 1000;
	return [NSString stringWithFormat:@"%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds];
}

- (void) setCurrentTimeDouble: (double) value
{
	long timeScale = [[self attributeForKey:QTMovieTimeScaleAttribute] longValue];
	[self setCurrentTime:QTMakeTime(value * timeScale, timeScale)];
}

@end

#if MAC_OS_X_VERSION_MAX_ALLOWED < 1070
@interface NSWindow(HBExtensions)
@property (readonly) CGFloat backingScaleFactor;
@end
#endif

#define BORDER_SIZE 2.0
// make min width and height of preview window large enough for hud
#define MIN_WIDTH 460.0
#define MIN_HEIGHT 128.0

#define ANIMATION_DUR 0.2

typedef enum ViewMode : NSUInteger {
    ViewModePicturePreview,
    ViewModeEncoding,
    ViewModeMoviePreview
} ViewMode;

@interface HBPreviewController () <HBPreviewGeneratorDelegate>
{
    /* HUD boxes */
    IBOutlet NSBox           * fPictureControlBox;
    IBOutlet NSBox           * fEncodingControlBox;
    IBOutlet NSBox           * fMoviePlaybackControlBox;

    IBOutlet NSSlider        * fPictureSlider;
    IBOutlet NSTextField     * fInfoField;
    IBOutlet NSTextField     * fscaleInfoField;

    /* Full Screen Mode Toggle */
    IBOutlet NSButton               * fScaleToScreenToggleButton;

    /* Movie Previews */
    IBOutlet QTMovieView            * fMovieView;
    /* Playback Panel Controls */
    IBOutlet NSButton               * fPlayPauseButton;
    IBOutlet NSSlider               * fMovieScrubberSlider;
    IBOutlet NSTextField            * fMovieInfoField;

    IBOutlet NSProgressIndicator    * fMovieCreationProgressIndicator;
    IBOutlet NSTextField            * fPreviewMovieStatusField;

    /* Popup of choices for length of preview in seconds */
    IBOutlet NSPopUpButton          * fPreviewMovieLengthPopUp;
}

@property (nonatomic) CALayer *backLayer;
@property (nonatomic) CALayer *pictureLayer;

@property (nonatomic) CGFloat backingScaleFactor;

@property (nonatomic) ViewMode currentViewMode;
@property (nonatomic) BOOL scaleToScreen;

@property (nonatomic, retain) NSTimer *hudTimer;

@property (nonatomic, retain) HBPreviewGenerator *generator;
@property (nonatomic) NSUInteger pictureIndex;

@property (nonatomic, retain) QTMovie *movie;
@property (nonatomic, retain) NSTimer *movieTimer;

/* Pictures HUD actions */
- (IBAction) previewDurationPopUpChanged: (id) sender;
- (IBAction) pictureSliderChanged: (id) sender;
- (IBAction) showPictureSettings:(id)sender;
- (IBAction) toggleScaleToScreen:(id)sender;

- (IBAction) cancelCreateMoviePreview: (id) sender;
- (IBAction) createMoviePreview: (id) sender;

/* Movie HUD actions */
- (IBAction) showPicturesPreview: (id) sender;
- (IBAction) toggleMoviePreviewPlayPause: (id) sender;
- (IBAction) moviePlaybackGoToBeginning: (id) sender;
- (IBAction) moviePlaybackGoToEnd: (id) sender;
- (IBAction) previewScrubberChanged: (id) sender;

@end

@implementation HBPreviewController

- (id) init
{
	if (self = [super initWithWindowNibName:@"PicturePreview"])
	{
        // NSWindowController likes to lazily load its window. However since
        // this controller tries to set all sorts of outlets before the window
        // is displayed, we need it to load immediately. The correct way to do
        // this, according to the documentation, is simply to invoke the window
        // getter once.
        //
        // If/when we switch a lot of this stuff to bindings, this can probably
        // go away.
        [self window];

    }
	return self;
}

- (void) awakeFromNib
{
    [[self window] setDelegate:self];

    if( ![[self window] setFrameUsingName:@"Preview"] )
        [[self window] center];

    [self setWindowFrameAutosaveName:@"Preview"];
    [[self window] setExcludedFromWindowsMenu:YES];

    /* lets set the preview window to accept mouse moved events */
    [[self window] setAcceptsMouseMovedEvents:YES];

    /* we set the progress indicator to not use threaded animation
     * as it causes a conflict with the qtmovieview's controllerbar
     */
    [fMovieCreationProgressIndicator setUsesThreadedAnimation:NO];

	[fMovieView setHidden:YES];
    [fMovieView setDelegate:self];
    [fMovieView setControllerVisible:NO];

    /* we set the preview length popup in seconds */
    [fPreviewMovieLengthPopUp removeAllItems];
    [fPreviewMovieLengthPopUp addItemsWithTitles:@[@"15", @"30", @"45", @"60", @"90",
                                                   @"120", @"150", @"180", @"210", @"240"]];

    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"PreviewLength"])
        [fPreviewMovieLengthPopUp selectItemWithTitle:[[NSUserDefaults standardUserDefaults]
                                                       objectForKey:@"PreviewLength"]];
    if (![fPreviewMovieLengthPopUp selectedItem])
        /* currently hard set default to 15 seconds */
        [fPreviewMovieLengthPopUp selectItemAtIndex: 0];

    /* Setup our layers for core animation */
    [[[self window] contentView] setWantsLayer:YES];
    [fPictureControlBox setWantsLayer:YES];
    [fEncodingControlBox setWantsLayer:YES];
    [fMoviePlaybackControlBox setWantsLayer:YES];

    self.backLayer = [CALayer layer];
    [self.backLayer setBounds:CGRectMake(0.0, 0.0, MIN_WIDTH, MIN_HEIGHT)];
    [self.backLayer setPosition:CGPointMake([[[self window] contentView] frame].size.width /2,
                                            [[[self window] contentView] frame].size.height /2)];

    [self.backLayer setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
    CGColorRef white = CGColorCreateGenericRGB(1.0, 1.0, 1.0, 1.0);
    [self.backLayer setBackgroundColor: white];
    CFRelease(white);
    [self.backLayer setShadowOpacity:0.5f];
    [self.backLayer setShadowOffset:CGSizeMake(0, 0)];

    self.pictureLayer = [CALayer layer];
    [self.pictureLayer setBounds:CGRectMake(0.0, 0.0, MIN_WIDTH - (BORDER_SIZE * 2), MIN_HEIGHT - (BORDER_SIZE * 2))];
    [self.pictureLayer setPosition:CGPointMake([[[self window] contentView] frame].size.width /2,
                                               [[[self window] contentView] frame].size.height /2)];

    [self.pictureLayer setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];

    // Disable fade on contents change
    NSMutableDictionary *actions = [NSMutableDictionary
                                    dictionaryWithDictionary:[self.pictureLayer actions]];

    [actions setObject:[NSNull null] forKey:@"contents"];
    [self.pictureLayer setActions:actions];

    [[[[self window] contentView] layer] insertSublayer:self.backLayer below: [fMovieView layer]];
    [[[[self window] contentView] layer] insertSublayer:self.pictureLayer below: [fMovieView layer]];

    /* relocate our hud origins */
    NSPoint hudControlBoxOrigin = [fMoviePlaybackControlBox frame].origin;
    [fPictureControlBox setFrameOrigin:hudControlBoxOrigin];
    [fEncodingControlBox setFrameOrigin:hudControlBoxOrigin];
    [fMoviePlaybackControlBox setFrameOrigin:hudControlBoxOrigin];

    /* set the current scale factor */
    if( [[self window] respondsToSelector:@selector( backingScaleFactor )] )
        self.backingScaleFactor = [[self window] backingScaleFactor];
    else
        self.backingScaleFactor = 1.0;
}

- (void) setTitle: (hb_title_t *) title
{
    _title = title;

    [self.generator cancel];
    self.generator = nil;

    if (_title)
    {
        /* alloc and init a generator for the current title */
        self.generator = [[[HBPreviewGenerator alloc] initWithHandle:self.handle andTitle:self.title] autorelease];

        /* adjust the preview slider length */
        [fPictureSlider setMaxValue: self.generator.imagesCount - 1.0];
        [fPictureSlider setNumberOfTickMarks: self.generator.imagesCount];

        [self displayPreview];
    }
}

- (void) reload
{
    if (self.title)
    {
        // Purge the existing picture previews so they get recreated the next time
        // they are needed.
        [self.generator purgeImageCache];
        [self switchViewToMode:ViewModePicturePreview];
        [self displayPreview];
    }
}

- (void) showWindow: (id) sender
{
    if (self.currentViewMode == ViewModeMoviePreview)
    {
        [self startMovieTimer];
    }

    [super showWindow:sender];
}

- (void) windowWillClose: (NSNotification *) aNotification
{
    if (self.currentViewMode == ViewModeEncoding)
    {
        [self cancelCreateMoviePreview:self];
    }
    else if (self.currentViewMode == ViewModeMoviePreview)
    {
        [fMovieView pause:self];
        [self stopMovieTimer];
    }

    [self.generator purgeImageCache];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"PreviewWindowIsOpen"];
}

- (void) windowDidChangeBackingProperties: (NSNotification *) notification
{
    NSWindow *theWindow = (NSWindow *)[notification object];

    CGFloat newBackingScaleFactor = [theWindow backingScaleFactor];
    CGFloat oldBackingScaleFactor = [[[notification userInfo]
                                      objectForKey:@"NSBackingPropertyOldScaleFactorKey"]
                                     doubleValue];

    if (newBackingScaleFactor != oldBackingScaleFactor)
    {
        // Scale factor changed, update the preview window
        // to the new situation
        self.backingScaleFactor = newBackingScaleFactor;
        if (self.title)
            [self reload];
    }
}

/**
 * Given the size of the preview image to be shown, returns the best possible
 * size for the view.
 */
- (NSSize) optimalViewSizeForImageSize: (NSSize) imageSize
{
    CGFloat minWidth = MIN_WIDTH;
    CGFloat minHeight = MIN_HEIGHT;

    NSSize screenSize = [[[self window] screen] visibleFrame].size;
    CGFloat maxWidth = screenSize.width;
    CGFloat maxHeight = screenSize.height;

    NSSize resultSize = imageSize;
    CGFloat resultPar = resultSize.width / resultSize.height;

    //note, a mbp 15" at 1440 x 900 is a 1.6 ar
    CGFloat screenAspect = screenSize.width / screenSize.height;

    if ( resultSize.width > maxWidth || resultSize.height > maxHeight )
    {
    	// Source is larger than screen in one or more dimensions
        if ( resultPar > screenAspect )
        {
            // Source aspect wider than screen aspect, snap to max width and vary height
            resultSize.width = maxWidth;
            resultSize.height = (maxWidth / resultPar);
        }
        else
        {
            // Source aspect narrower than screen aspect, snap to max height vary width
            resultSize.height = maxHeight;
            resultSize.width = (maxHeight * resultPar);
        }
    }

    // If necessary, grow to minimum dimensions to ensure controls overlay is not obstructed
    if ( resultSize.width < minWidth )
        resultSize.width = minWidth;
    if ( resultSize.height < minHeight )
        resultSize.height = minHeight;

    return resultSize;
}

/**
 * Resizes the entire window to accomodate a view of a particular size.
 */
- (void) resizeWindowForViewSize: (NSSize) viewSize
{
    NSSize currentSize = [[[self window] contentView] frame].size;
    NSRect frame = [[self window] frame];

    // Calculate border around content region of the frame
    int borderX = frame.size.width - currentSize.width;
    int borderY = frame.size.height - currentSize.height;

    // Make sure the frame is smaller than the screen
    NSSize maxSize = [[[self window] screen] visibleFrame].size;

    /* if we are not Scale To Screen, put an 10% of visible screen on the window */
    if (self.scaleToScreen == NO)
    {
        maxSize.width = maxSize.width * 0.90;
        maxSize.height = maxSize.height * 0.90;
    }

    // Set the new frame size
    // Add the border to the new frame size so that the content region
    // of the frame is large enough to accomodate the preview image
    frame.size.width = viewSize.width + borderX;
    frame.size.height = viewSize.height + borderY;

    /* compare frame to max size of screen */
    if( frame.size.width > maxSize.width )
    {
        frame.size.width = maxSize.width;
    }
    if( frame.size.height > maxSize.height )
    {
        frame.size.height = maxSize.height;
    }

    /* Since upon launch we can open up the preview window if it was open
     * the last time we quit (and at the size it was) we want to make
     * sure that upon resize we do not have the window off the screen
     * So check the origin against the screen origin and adjust if
     * necessary.
     */
    NSSize screenSize = [[[self window] screen] visibleFrame].size;
    NSPoint screenOrigin = [[[self window] screen] visibleFrame].origin;

    /* our origin is off the screen to the left*/
    if (frame.origin.x < screenOrigin.x)
    {
        /* so shift our origin to the right */
        frame.origin.x = screenOrigin.x;
    }
    else if ((frame.origin.x + frame.size.width) > (screenOrigin.x + screenSize.width))
    {
        /* the right side of the preview is off the screen, so shift to the left */
        frame.origin.x = (screenOrigin.x + screenSize.width) - frame.size.width;
    }

    if (self.scaleToScreen == YES)
    {
        /* our origin is off the screen to the top*/
        if (frame.origin.y < screenOrigin.y)
        {
            /* so shift our origin to the bottom */
            frame.origin.y = screenOrigin.y;
        }
        else if ((frame.origin.y + frame.size.height) > (screenOrigin.y + screenSize.height))
        {
            /* the top side of the preview is off the screen, so shift to the bottom */
            frame.origin.y = (screenOrigin.y + screenSize.height) - frame.size.height;
        }
    }

    [[self window] setFrame:frame display:YES animate:YES];
}


/**
 * Enable/Disable an arbitrary number of UI elements.
 * @param boxes an array of UI elements
 * @param indexes a set of indexes of the elements in boxes to be enabled
 */
- (void) toggleBoxes: (NSArray *) boxes usingIndexes: (NSIndexSet *) indexes
{
    [NSAnimationContext beginGrouping];
    [[NSAnimationContext currentContext] setDuration:ANIMATION_DUR];

    [boxes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [[obj animator] setHidden:([indexes containsIndex:idx]) ? NO : YES];
    }];

    [NSAnimationContext endGrouping];
}

/**
 * Switch the preview controller to one of his view mode:
 * ViewModePicturePreview, ViewModeEncoding, ViewModeMoviePreview
 * This methods is the only way to change the mode, do not try otherwise.
 * @param mode ViewMode mode
 */
- (void) switchViewToMode: (ViewMode) mode
{
    switch (mode) {
        case ViewModePicturePreview:
        {
            if (self.currentViewMode == ViewModeEncoding)
            {
                [self.generator cancel];
                [self toggleBoxes:@[fPictureControlBox, fEncodingControlBox]
                     usingIndexes:[NSIndexSet indexSetWithIndex:0]];
                [fMovieCreationProgressIndicator stopAnimation:self];
            }
            else if (self.currentViewMode == ViewModeMoviePreview)
            {
                /* Stop playback and remove the observers */
                [fMovieView pause:self];
                [self stopMovieTimer];
                [self removeMovieObservers];

                [self toggleBoxes:@[fPictureControlBox, fMoviePlaybackControlBox, fMovieView]
                     usingIndexes:[NSIndexSet indexSetWithIndex:0]];

                /* Release the movie */
                [fMovieView setMovie:nil];
                self.movie = nil;
            }

            break;
        }

        case ViewModeEncoding:
        {
            [fMovieCreationProgressIndicator setDoubleValue:0];
            [fMovieCreationProgressIndicator startAnimation:self];
            [self toggleBoxes:@[fEncodingControlBox, fPictureControlBox, fMoviePlaybackControlBox]
                 usingIndexes:[NSIndexSet indexSetWithIndex:0]];

            break;
        }

        case ViewModeMoviePreview:
        {
            [self toggleBoxes:@[fMovieView, fMoviePlaybackControlBox, fEncodingControlBox, fPictureControlBox]
                 usingIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)]];

            [fMovieCreationProgressIndicator stopAnimation:self];
            [self initPreviewScrubberForMovie];
            [self startMovieTimer];

            /* Install movie notifications */
            [self addMovieObservers];
        }
            break;

        default:
            break;
    }

    self.currentViewMode = mode;
}

- (void) dealloc
{
    [_hudTimer invalidate];
    [_hudTimer release];

    [_movieTimer invalidate];
    [_movieTimer release];

    [_generator cancel];
    [_generator release];

    [self removeMovieObservers];

    [super dealloc];
}

#pragma mark -
#pragma mark Hud Control Overlay

- (void) mouseMoved: (NSEvent *) theEvent
{
    [super mouseMoved:theEvent];
    NSPoint mouseLoc = [theEvent locationInWindow];

    /* Test for mouse location to show/hide hud controls */
    if (self.currentViewMode != ViewModeEncoding && self.title)
    {
        /* Since we are not encoding, verify which control hud to show
         * or hide based on aMovie ( aMovie indicates we need movie controls )
         */
        NSBox *hudBoxToShow;
        if (self.currentViewMode == !ViewModeMoviePreview) // No movie loaded up
        {
            hudBoxToShow = fPictureControlBox;
        }
        else // We have a movie
        {
            hudBoxToShow = fMoviePlaybackControlBox;
        }

        if (NSPointInRect(mouseLoc, [hudBoxToShow frame]))
        {
            [[hudBoxToShow animator] setHidden: NO];
            [self stopHudTimer];
        }
		else if (NSPointInRect(mouseLoc, [[[self window] contentView] frame]))
        {
            [[hudBoxToShow animator] setHidden: NO];
            [self startHudTimer];
        }
        else
        {
            [[hudBoxToShow animator] setHidden: YES];
        }
	}
}

- (void) startHudTimer
{
	if (self.hudTimer)
    {
        [self.hudTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:8.0]];
	}
    else
    {
        self.hudTimer = [NSTimer scheduledTimerWithTimeInterval:8.0 target:self selector:@selector(hudTimerFired:)
                                                       userInfo:nil repeats:YES];
    }
}

- (void) stopHudTimer
{
    [self.hudTimer invalidate];
    self.hudTimer = nil;
}

- (void) hudTimerFired: (NSTimer *)theTimer
{
    /* Regardless which control box is active, after the timer
     * period we want either one to fade to hidden.
     */
    [[fPictureControlBox animator] setHidden: YES];
    [[fMoviePlaybackControlBox animator] setHidden: YES];
    [self stopHudTimer];
}

#pragma mark -
#pragma mark Still previews mode

/**
 * Adjusts the window to draw the current picture (fPicture) adjusting its size as
 * necessary to display as much of the picture as possible.
 */
- (void) displayPreview
{
    hb_title_t *title = self.title;

    NSImage *fPreviewImage = [self.generator imageAtIndex:self.pictureIndex shouldCache:YES];
    NSSize imageScaledSize = [fPreviewImage size];
    [self.pictureLayer setContents:fPreviewImage];

    NSSize displaySize = NSMakeSize( ( CGFloat )title->width, ( CGFloat )title->height );
    NSString *sizeInfoString;

    /* Set the picture size display fields below the Preview Picture*/
    int display_width;
    display_width = title->job->width * title->job->anamorphic.par_width / title->job->anamorphic.par_height;
    if (title->job->anamorphic.mode == HB_ANAMORPHIC_STRICT) // Original PAR Implementation
    {
        sizeInfoString = [NSString stringWithFormat:
                          @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d Strict",
                          title->width, title->height, title->job->width, title->job->height, display_width, title->job->height];
    }
    else if (title->job->anamorphic.mode == HB_ANAMORPHIC_LOOSE) // Loose Anamorphic
    {
        sizeInfoString = [NSString stringWithFormat:
                          @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d Loose",
                          title->width, title->height, title->job->width, title->job->height, display_width, title->job->height];
    }
    else if (title->job->anamorphic.mode == HB_ANAMORPHIC_CUSTOM) // Custom Anamorphic
    {
        sizeInfoString = [NSString stringWithFormat:
                          @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d Custom",
                          title->width, title->height, title->job->width, title->job->height, display_width, title->job->height];
    }
    else // No Anamorphic
    {
        sizeInfoString = [NSString stringWithFormat:
                          @"Source: %dx%d, Output: %dx%d",
                          title->width, title->height, title->job->width, title->job->height];
    }
    displaySize.width = display_width;
    displaySize.height = title->job->height;
    imageScaledSize.width = display_width;
    imageScaledSize.height = title->job->height;

    if (self.backingScaleFactor != 1.0)
    {
        // HiDPI mode usually display everything
        // with douple pixel count, but we don't
        // want to double the size of the video
        displaySize.height /= self.backingScaleFactor;
        displaySize.width /= self.backingScaleFactor;
        imageScaledSize.height /= self.backingScaleFactor;
        imageScaledSize.width /= self.backingScaleFactor;
    }

    // Get the optimal view size for the image
    NSSize viewSize = [self optimalViewSizeForImageSize:displaySize];
    viewSize.width += BORDER_SIZE * 2;
    viewSize.height += BORDER_SIZE * 2;

    NSSize windowSize;
    if (self.scaleToScreen == YES)
        // Scale the window to the max possible size
        windowSize = [[[self window] screen] visibleFrame].size;
    else
        // Scale the window to the image size
        windowSize = viewSize;

    [self resizeWindowForViewSize:windowSize];
    NSSize areaSize = [[[self window] contentView] frame].size;
    areaSize.width -= BORDER_SIZE * 2;
    areaSize.height -= BORDER_SIZE * 2;

    if (self.scaleToScreen == YES)
    {
        /* We are in Scale To Screen mode so, we have to get the ratio for height and width against the window
         *size so we can scale from there.
         */
        CGFloat pictureAspectRatio = imageScaledSize.width / imageScaledSize.height;
        CGFloat areaAspectRatio = areaSize.width / areaSize.height;

        if (pictureAspectRatio > areaAspectRatio)
        {
            viewSize.width = areaSize.width;
            viewSize.height = viewSize.width / pictureAspectRatio;
        }
        else
        {
            viewSize.height = areaSize.height;
            viewSize.width = viewSize.height * pictureAspectRatio;
        }
    }
    else
    {
        // If the image is larger then the window, scale the image
        viewSize = imageScaledSize;

        if (imageScaledSize.width > areaSize.width || imageScaledSize.height > areaSize.height)
        {
            CGFloat pictureAspectRatio = imageScaledSize.width / imageScaledSize.height;
            CGFloat areaAspectRatio = areaSize.width / areaSize.height;

            if (pictureAspectRatio > areaAspectRatio)
            {
                viewSize.width = areaSize.width;
                viewSize.height = viewSize.width / pictureAspectRatio;
            }
            else
            {
                viewSize.height = areaSize.height;
                viewSize.width = viewSize.height * pictureAspectRatio;
            }
        }
    }

    // Resize the CALayers
    [self.backLayer setBounds:CGRectMake(0, 0, viewSize.width + (BORDER_SIZE * 2), viewSize.height + (BORDER_SIZE * 2))];
    [self.pictureLayer setBounds:CGRectMake(0, 0, viewSize.width, viewSize.height)];

    NSString *scaleString;
    CGFloat scale = ( ( CGFloat )[self.pictureLayer frame].size.width) / ( ( CGFloat )imageScaledSize.width);
    if (scale * 100.0 != 100)
        scaleString = [NSString stringWithFormat:@" (%.0f%% actual size)", scale * 100.0];
    else
        scaleString = @"(Actual size)";

    if (_scaleToScreen == YES)
        scaleString = [scaleString stringByAppendingString:@" Scaled To Screen"];

    /* Set the info fields in the hud controller */
    [fInfoField setStringValue: [NSString stringWithFormat:
                                 @"%@", sizeInfoString]];

    [fscaleInfoField setStringValue: [NSString stringWithFormat:
                                      @"%@", scaleString]];

    /* Set the info field in the window title bar */
    [[self window] setTitle:[NSString stringWithFormat: @"Preview - %@ %@",sizeInfoString, scaleString]];
}

- (IBAction) previewDurationPopUpChanged: (id) sender
{
    [[NSUserDefaults standardUserDefaults] setObject:[fPreviewMovieLengthPopUp titleOfSelectedItem] forKey:@"PreviewLength"];
}

- (void) setDeinterlacePreview: (BOOL) deinterlacePreview
{
    _deinterlacePreview = deinterlacePreview;
    self.generator.deinterlace = deinterlacePreview;
}

- (IBAction) pictureSliderChanged: (id) sender
{
    if ((self.pictureIndex != [fPictureSlider intValue] || !sender) && self.title) {
        self.pictureIndex = [fPictureSlider intValue];
        [self displayPreview];
    }
}

- (IBAction) toggleScaleToScreen: (id) sender
{
    if (self.scaleToScreen == YES)
    {
        self.scaleToScreen = NO;
        /* make sure we are set to a still preview */
        [self displayPreview];
        [fScaleToScreenToggleButton setTitle:@"Scale To Screen"];
    }
    else
    {
        self.scaleToScreen = YES;
        /* make sure we are set to a still preview */
        [self displayPreview];
        [fScaleToScreenToggleButton setTitle:@"Actual Scale"];
    }
}

- (NSString *) pictureSizeInfoString
{
    return [fInfoField stringValue];
}

- (IBAction) showPictureSettings: (id) sender
{
    [self.delegate showPicturePanel:self];
}

#pragma mark -
#pragma mark Movie preview mode

- (void) updateProgress: (double) progress info: (NSString *) progressInfo {
    [fPreviewMovieStatusField setStringValue: progressInfo];

    [fMovieCreationProgressIndicator setIndeterminate: NO];
    [fMovieCreationProgressIndicator setDoubleValue: progress];
}

- (void) didCreateMovieAtURL: (NSURL *) fileURL
{
    /* Load the new movie into fMovieView */
    if (fileURL)
    {
		NSError *outError;
		NSDictionary *movieAttributes = @{QTMovieURLAttribute: fileURL,
                                          QTMovieAskUnresolvedDataRefsAttribute: @(NO),
                                          @"QTMovieOpenForPlaybackAttribute": @(YES),
                                          @"QTMovieOpenAsyncRequiredAttribute": @(NO),
                                          @"QTMovieOpenAsyncOKAttribute": @(NO),
                                          @"QTMovieIsSteppableAttribute": @(YES),
                                          QTMovieApertureModeAttribute: QTMovieApertureModeClean};

        QTMovie *movie = [[[QTMovie alloc] initWithAttributes:movieAttributes error:&outError] autorelease];

		if (!movie)
        {
            [HBUtilities writeToActivityLog: "showMoviePreview: Unable to open movie"];
            [self switchViewToMode:ViewModePicturePreview];
		}
        else
        {
            /* Scale the fMovieView to the picture player size */
            [fMovieView setFrameSize:[self.pictureLayer frame].size];
            [fMovieView setFrameOrigin:[self.pictureLayer frame].origin];

            [fMovieView setMovie:movie];
            [movie setDelegate:self];

            // get and enable subtitles
            NSArray *subtitlesArray = [movie tracksOfMediaType: @"sbtl"];
            if (subtitlesArray && [subtitlesArray count])
            {
                // enable the first tx3g subtitle track
                [[subtitlesArray objectAtIndex: 0] setEnabled: YES];
            }
            else
            {
                // Perian subtitles
                subtitlesArray = [movie tracksOfMediaType: QTMediaTypeVideo];
                if (subtitlesArray && ([subtitlesArray count] >= 2))
                {
                    // track 0 should be video, other video tracks should
                    // be subtitles; force-enable the first subs track
                    [[subtitlesArray objectAtIndex: 1] setEnabled: YES];
                }
            }

            // to actually play the movie
            self.movie = movie;

            [self switchViewToMode:ViewModeMoviePreview];

            [fMovieView play:movie];
        }
    }
}

- (IBAction) cancelCreateMoviePreview: (id) sender
{
    [self switchViewToMode:ViewModePicturePreview];
}

- (IBAction) createMoviePreview: (id) sender
{
    if (!self.generator)
        return;

    self.generator.delegate = self;
    [self.delegate prepareJobForPreview];
    [self.generator createMovieAsyncWithImageIndex:self.pictureIndex
                                       andDuration:[[fPreviewMovieLengthPopUp titleOfSelectedItem] intValue]];

    [self switchViewToMode:ViewModeEncoding];
}

- (IBAction) toggleMoviePreviewPlayPause: (id) sender
{
    /* make sure a movie is even loaded up */
    if (self.movie)
    {
        if ([self.movie isPlaying]) // we are playing
        {
            [fMovieView pause:self.movie];
            [fPlayPauseButton setState: NSOnState];
        }
        else // we are paused or stopped
        {
            [fMovieView play:self.movie];
            [fPlayPauseButton setState: NSOffState];
        }
    }
}

- (IBAction) moviePlaybackGoToBeginning: (id) sender
{
    [fMovieView gotoBeginning:self.movie];
}

- (IBAction) moviePlaybackGoToEnd: (id) sender
{
    [fMovieView gotoEnd:self.movie];
}

- (void) startMovieTimer
{
	if (!self.movieTimer)
    {
        self.movieTimer = [NSTimer scheduledTimerWithTimeInterval:0.09 target:self
                                                         selector:@selector(movieTimerFired:)
                                                         userInfo:nil repeats:YES];
    }
}

- (void) stopMovieTimer
{
    [self.movieTimer invalidate];
    self.movieTimer = nil;
}

- (void) movieTimerFired: (NSTimer *)theTimer
{
    if (self.movie != nil)
    {
        [self adjustPreviewScrubberForCurrentMovieTime];
        [fMovieInfoField setStringValue: [self.movie timecode]];
    }
}

- (IBAction) showPicturesPreview: (id) sender
{
    [self switchViewToMode:ViewModePicturePreview];
}

#pragma mark -
#pragma mark Movie Playback Scrubber

// Initialize the preview scrubber min/max to appropriate values for the current movie
- (void) initPreviewScrubberForMovie
{
    QTTime duration = [self.movie duration];
    CGFloat result = duration.timeValue / duration.timeScale;

    [fMovieScrubberSlider setMinValue:0.0];
    [fMovieScrubberSlider setMaxValue: result];
    [fMovieScrubberSlider setDoubleValue: 0.0];
}

- (void) adjustPreviewScrubberForCurrentMovieTime
{
    QTTime time = [self.movie currentTime];

    CGFloat result = (CGFloat)time.timeValue / (CGFloat)time.timeScale;;
    [fMovieScrubberSlider setDoubleValue:result];
}

- (IBAction) previewScrubberChanged: (id) sender
{
    [fMovieView pause:self.movie];
    [self.movie setCurrentTimeDouble:[fMovieScrubberSlider doubleValue]];
    [fMovieInfoField setStringValue: [self.movie timecode]];
}

#pragma mark -
#pragma mark Movie Notifications

- (void) addMovieObservers
{
    /* Notification for any time the movie rate changes */
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieRateDidChange:)
                                                 name:@"QTMovieRateDidChangeNotification"
                                               object:self.movie];
}

- (void) removeMovieObservers
{
    /*Notification for any time the movie rate changes */
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:@"QTMovieRateDidChangeNotification"
                                                  object:self.movie];
}

- (void) movieRateDidChange: (NSNotification *) notification
{
    if ([self.movie isPlaying])
        [fPlayPauseButton setState: NSOnState];
    else
        [fPlayPauseButton setState: NSOffState];
}

#pragma mark -
#pragma mark Keyboard and mouse wheel control

/* fMovieView Keyboard controls */
- (void) keyDown: (NSEvent *) event
{
    unichar key = [[event charactersIgnoringModifiers] characterAtIndex:0];
    QTMovie *movie = self.movie;

    if (movie)
    {
        if (key == 32)
        {
            if ([movie isPlaying])
                [fMovieView pause:movie];
            else
                [fMovieView play:movie];
        }
        else if (key == 'k')
            [fMovieView pause:movie];
        else if (key == 'l')
        {
            float rate = [movie rate];
            rate += 1.0f;
            [fMovieView play:movie];
            [movie setRate:rate];
        }
        else if (key == 'j')
        {
            float rate = [movie rate];
            rate -= 1.0f;
            [fMovieView play:movie];
            [movie setRate:rate];
        }
        else if ([event modifierFlags] & NSAlternateKeyMask && key == NSLeftArrowFunctionKey)
            [fMovieView gotoBeginning:self];
        else if ([event modifierFlags] & NSAlternateKeyMask && key == NSRightArrowFunctionKey)
            [fMovieView gotoEnd:self];
        else if (key == NSLeftArrowFunctionKey)
            [fMovieView stepBackward:self];
        else if (key == NSRightArrowFunctionKey)
            [fMovieView stepForward:self];
        else
            [super keyDown:event];
    }
    else if (self.currentViewMode != ViewModeEncoding)
    {
        if (key == NSLeftArrowFunctionKey)
        {
            [fPictureSlider setIntegerValue:self.pictureIndex > [fPictureSlider minValue] ? self.pictureIndex - 1 : self.pictureIndex];
            [self pictureSliderChanged:self];
        }
        else if (key == NSRightArrowFunctionKey)
        {
            [fPictureSlider setIntegerValue:self.pictureIndex < [fPictureSlider maxValue] ? self.pictureIndex + 1 : self.pictureIndex];
            [self pictureSliderChanged:self];
        }
        else
            [super keyDown:event];
    }
    else
        [super keyDown:event];
}

- (void) scrollWheel: (NSEvent *) theEvent
{
    if (self.currentViewMode != ViewModeEncoding)
    {
        if ([theEvent deltaY] < 0)
        {
            [fPictureSlider setIntegerValue:self.pictureIndex < [fPictureSlider maxValue] ? self.pictureIndex + 1 : self.pictureIndex];
            [self pictureSliderChanged:self];
        }
        else if ([theEvent deltaY] > 0)
        {
            [fPictureSlider setIntegerValue:self.pictureIndex > [fPictureSlider minValue] ? self.pictureIndex - 1 : self.pictureIndex];
            [self pictureSliderChanged:self];
        }
    }
}

@end