VLCTimeSelectionPanelController.m 3.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*****************************************************************************
 * TimeSelectionPanelController.m: Controller for time selection panel
 *****************************************************************************
 * Copyright (C) 2015 VideoLAN and authors
 * Author:       David Fuhrmann <david dot fuhrmann at googlemail dot com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#import "VLCTimeSelectionPanelController.h"

#import "misc.h"
25
#import "VLCStringUtility.h"
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

@interface VLCTimeSelectionPanelController()
{
    TimeSelectionCompletionHandler _completionHandler;
}

@end

@implementation VLCTimeSelectionPanelController

#pragma mark - object handling

- (id)init
{
    self = [super initWithWindowNibName:@"TimeSelectionPanel"];

    return self;
}


#pragma mark - UI handling

- (void)windowDidLoad
{
    [_cancelButton setTitle: _NS("Cancel")];
    [_okButton setTitle: _NS("OK")];
    [_secsLabel setStringValue: _NS("sec.")];
    [_goToLabel setStringValue: _NS("Jump to Time")];
    [_textField setFormatter:[[PositionFormatter alloc] init]];
}

- (IBAction)buttonPressed:(id)sender
{
    [self.window orderOut:sender];
    [NSApp endSheet: self.window];

    // calculate resulting time in secs:
    int64_t timeInSec = 0;
    NSString *string = [_textField stringValue];
    if ([[string componentsSeparatedByString: @":"] count] > 1 &&
        [[string componentsSeparatedByString: @":"] count] <= 3) {
        NSArray *ourTempArray = \
        [string componentsSeparatedByString: @":"];

        if ([[string componentsSeparatedByString: @":"] count] == 3) {
            timeInSec += ([[ourTempArray firstObject] intValue] *3600); //h
            timeInSec += ([[ourTempArray objectAtIndex:1] intValue] *60); //m
            timeInSec += [[ourTempArray objectAtIndex:2] intValue];        //s
        } else {
            timeInSec += ([[ourTempArray firstObject] intValue] *60); //m
            timeInSec += [[ourTempArray objectAtIndex:1] intValue]; //s
        }
    }
    else
        timeInSec = [string intValue];

    if (_completionHandler)
        _completionHandler(sender == _okButton ? NSOKButton : NSCancelButton, timeInSec);
}

- (void)runModalForWindow:(NSWindow *)window completionHandler:(TimeSelectionCompletionHandler)handler
{
    [self window];
    [_stepper setMaxValue:self.maxValue];

    _completionHandler = [handler copy];
    [NSApp beginSheet:self.window
       modalForWindow:window modalDelegate:self
       didEndSelector:nil
          contextInfo:nil];
}

@end