]> git.sesse.net Git - vlc/blob - modules/gui/macosx/controls.m
macosx: add custom numberformatter to goto time field to only allow digits and :
[vlc] / modules / gui / macosx / controls.m
1 /*****************************************************************************
2  * controls.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2013 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *          Benjamin Pracht <bigben at videolan doit org>
9  *          Felix Paul Kühne <fkuehne at videolan dot org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <sys/param.h>                                    /* for MAXPATHLEN */
31 #include <string.h>
32
33 #import "intf.h"
34 #import "VideoView.h"
35 #import "open.h"
36 #import "controls.h"
37 #import "playlist.h"
38 #import "MainMenu.h"
39 #import "CoreInteraction.h"
40 #import "misc.h"
41 #import <vlc_keys.h>
42
43 #pragma mark -
44 /*****************************************************************************
45  * VLCControls implementation
46  *****************************************************************************/
47 @implementation VLCControls
48
49 @synthesize jumpTimeValue;
50
51 - (void)awakeFromNib
52 {
53     [o_specificTime_mi setTitle: _NS("Jump To Time")];
54     [o_specificTime_cancel_btn setTitle: _NS("Cancel")];
55     [o_specificTime_ok_btn setTitle: _NS("OK")];
56     [o_specificTime_sec_lbl setStringValue: _NS("sec.")];
57     [o_specificTime_goTo_lbl setStringValue: _NS("Jump to time")];
58
59     [o_specificTime_enter_fld setFormatter:[[[PositionFormatter alloc] init] autorelease]];
60 }
61
62 - (void)dealloc
63 {
64     [[NSNotificationCenter defaultCenter] removeObserver: self];
65
66     [super dealloc];
67 }
68
69 - (IBAction)play:(id)sender
70 {
71     [[VLCCoreInteraction sharedInstance] playOrPause];
72 }
73
74 - (IBAction)stop:(id)sender
75 {
76     [[VLCCoreInteraction sharedInstance] stop];
77 }
78
79 - (IBAction)prev:(id)sender
80 {
81     [[VLCCoreInteraction sharedInstance] previous];
82 }
83
84 - (IBAction)next:(id)sender
85 {
86     [[VLCCoreInteraction sharedInstance] next];
87 }
88
89 - (IBAction)random:(id)sender
90 {
91     [[VLCCoreInteraction sharedInstance] shuffle];
92 }
93
94 - (IBAction)repeat:(id)sender
95 {
96     vlc_value_t val;
97     intf_thread_t * p_intf = VLCIntf;
98     playlist_t * p_playlist = pl_Get(p_intf);
99
100     var_Get(p_playlist, "repeat", &val);
101     if (! val.b_bool)
102         [[VLCCoreInteraction sharedInstance] repeatOne];
103     else
104         [[VLCCoreInteraction sharedInstance] repeatOff];
105 }
106
107 - (IBAction)loop:(id)sender
108 {
109     vlc_value_t val;
110     intf_thread_t * p_intf = VLCIntf;
111     playlist_t * p_playlist = pl_Get(p_intf);
112
113     var_Get(p_playlist, "loop", &val);
114     if (! val.b_bool)
115         [[VLCCoreInteraction sharedInstance] repeatAll];
116     else
117         [[VLCCoreInteraction sharedInstance] repeatOff];
118 }
119
120 - (IBAction)quitAfterPlayback:(id)sender
121 {
122     vlc_value_t val;
123     playlist_t * p_playlist = pl_Get(VLCIntf);
124     var_ToggleBool(p_playlist, "play-and-exit");
125 }
126
127 - (IBAction)forward:(id)sender
128 {
129     [[VLCCoreInteraction sharedInstance] forward];
130 }
131
132 - (IBAction)backward:(id)sender
133 {
134     [[VLCCoreInteraction sharedInstance] backward];
135 }
136
137 - (IBAction)volumeUp:(id)sender
138 {
139     [[VLCCoreInteraction sharedInstance] volumeUp];
140 }
141
142 - (IBAction)volumeDown:(id)sender
143 {
144     [[VLCCoreInteraction sharedInstance] volumeDown];
145 }
146
147 - (IBAction)mute:(id)sender
148 {
149     [[VLCCoreInteraction sharedInstance] setMute: YES];
150 }
151
152 - (IBAction)volumeSliderUpdated:(id)sender
153 {
154     [[VLCCoreInteraction sharedInstance] setVolume: [sender intValue]];
155 }
156
157 - (IBAction)showPosition: (id)sender
158 {
159     input_thread_t * p_input = pl_CurrentInput(VLCIntf);
160     if (p_input != NULL) {
161         vout_thread_t *p_vout = input_GetVout(p_input);
162         if (p_vout != NULL) {
163             var_SetInteger(VLCIntf->p_libvlc, "key-action", ACTIONID_POSITION);
164             vlc_object_release(p_vout);
165         }
166         vlc_object_release(p_input);
167     }
168 }
169
170 - (IBAction)lockVideosAspectRatio:(id)sender
171 {
172     [[VLCCoreInteraction sharedInstance] setAspectRatioIsLocked: ![sender state]];
173     [sender setState: [[VLCCoreInteraction sharedInstance] aspectRatioIsLocked]];
174 }
175
176 - (BOOL)keyEvent:(NSEvent *)o_event
177 {
178     BOOL eventHandled = NO;
179     NSString * characters = [o_event charactersIgnoringModifiers];
180     if ([characters length] > 0) {
181         unichar key = [characters characterAtIndex: 0];
182
183         if (key) {
184             input_thread_t * p_input = pl_CurrentInput(VLCIntf);
185             if (p_input != NULL) {
186                 vout_thread_t *p_vout = input_GetVout(p_input);
187
188                 if (p_vout != NULL) {
189                     /* Escape */
190                     if (key == (unichar) 0x1b) {
191                         if (var_GetBool(p_vout, "fullscreen")) {
192                             [[VLCCoreInteraction sharedInstance] toggleFullscreen];
193                             eventHandled = YES;
194                         }
195                     }
196                     else if (key == ' ') {
197                         [self play:self];
198                         eventHandled = YES;
199                     }
200                     vlc_object_release(p_vout);
201                 }
202                 vlc_object_release(p_input);
203             }
204         }
205     }
206     return eventHandled;
207 }
208
209 - (IBAction)goToSpecificTime:(id)sender
210 {
211     if (sender == o_specificTime_cancel_btn)
212     {
213         [NSApp endSheet: o_specificTime_win];
214         [o_specificTime_win close];
215     } else if (sender == o_specificTime_ok_btn) {
216         input_thread_t * p_input = pl_CurrentInput(VLCIntf);
217         if (p_input) {
218             int64_t timeInSec = 0;
219             NSString * fieldContent = [o_specificTime_enter_fld stringValue];
220             if ([[fieldContent componentsSeparatedByString: @":"] count] > 1 &&
221                [[fieldContent componentsSeparatedByString: @":"] count] <= 3) {
222                 NSArray * ourTempArray = \
223                     [fieldContent componentsSeparatedByString: @":"];
224
225                 if ([[fieldContent componentsSeparatedByString: @":"] count] == 3) {
226                     timeInSec += ([ourTempArray[0] intValue] * 3600); //h
227                     timeInSec += ([ourTempArray[1] intValue] * 60); //m
228                     timeInSec += [ourTempArray[2] intValue];        //s
229                 } else {
230                     timeInSec += ([ourTempArray[0] intValue] * 60); //m
231                     timeInSec += [ourTempArray[1] intValue]; //s
232                 }
233             }
234             else
235                 timeInSec = [fieldContent intValue];
236
237             input_Control(p_input, INPUT_SET_TIME, (int64_t)(timeInSec * 1000000));
238             vlc_object_release(p_input);
239         }
240
241         [NSApp endSheet: o_specificTime_win];
242         [o_specificTime_win close];
243     } else {
244         input_thread_t * p_input = pl_CurrentInput(VLCIntf);
245         if (p_input) {
246             /* we can obviously only do that if an input is available */
247             vlc_value_t pos, length;
248             var_Get(p_input, "length", &length);
249             [o_specificTime_stepper setMaxValue: (length.i_time / 1000000)];
250             var_Get(p_input, "time", &pos);
251             [self setJumpTimeValue: (pos.i_time / 1000000)];
252             [NSApp beginSheet: o_specificTime_win modalForWindow: \
253                 [NSApp mainWindow] modalDelegate: self didEndSelector: nil \
254                 contextInfo: nil];
255             [o_specificTime_win makeKeyWindow];
256             vlc_object_release(p_input);
257         }
258     }
259 }
260
261 @end
262
263 @implementation VLCControls (NSMenuValidation)
264
265 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
266 {
267     return [[VLCMainMenu sharedInstance] validateMenuItem:o_mi];
268 }
269
270 @end