]> git.sesse.net Git - vlc/blob - modules/gui/macosx/extended.m
* port of the WX-advanded-GUI to OSX (refs #268)
[vlc] / modules / gui / macosx / extended.m
1 /*****************************************************************************
2  * extended.m: MacOS X Extended interface panel
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Felix Kühne <fkuehne@users.sf.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24
25 /*****************************************************************************
26  * Note: 
27  * the code used to bind with VLC's modules is heavily based upon 
28  * ../wxwidgets/extrapanel.cpp, written by Clément Stenac.
29  * the code used to insert/remove the view and resize/remove the other stuff
30  * was inspired by intf.m, written by Derk-Jan Hartman. 
31  * (both are members of the VideoLAN team) 
32  *****************************************************************************/
33
34
35 /*****************************************************************************
36  * Preamble
37  *****************************************************************************/
38
39 #import "extended.h"
40 #import "intf.h"
41 #import <vlc/vlc.h>
42 #import <vlc/aout.h>
43 #import <aout_internal.h>
44 #import <vlc/vout.h>
45 #import <vlc/intf.h>
46
47 /*****************************************************************************
48  * VLCExtended implementation
49  *****************************************************************************/
50
51 @implementation VLCExtended
52
53 static VLCExtended *_o_sharedInstance = nil;
54
55 + (VLCExtended *)sharedInstance
56 {
57     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
58 }
59
60 - (id)init
61 {
62     if (_o_sharedInstance) {
63         [self dealloc];
64     } else {
65         _o_sharedInstance = [super init];
66     }
67
68     return _o_sharedInstance;
69 }
70
71 - (void)initStrings
72 {
73     /* localise GUI-strings */
74     /* method is called from intf.m (in method showExtended) */
75 }
76
77 - (void)awakeFromNib
78 {
79     /* set the adjust-filter-sliders to the values from the prefs and enable
80      * them, if wanted */
81     char * psz_vfilters;
82     intf_thread_t * p_intf = VLCIntf;
83     psz_vfilters = config_GetPsz( p_intf, "vout-filter" );
84     if( psz_vfilters && strstr( psz_vfilters, "adjust" ) )
85     {
86         [o_ckb_enblAdjustImg setState: NSOnState];
87         [o_btn_rstrDefaults setEnabled: YES];
88         [o_sld_brightness setEnabled: YES];
89         [o_sld_contrast setEnabled: YES];
90         [o_sld_gamma setEnabled: YES];
91         [o_sld_hue setEnabled: YES];
92         [o_sld_saturation setEnabled: YES];
93     }
94     else
95     {
96         [o_ckb_enblAdjustImg setState: NSOffState];
97         [o_btn_rstrDefaults setEnabled: NO];
98         [o_sld_brightness setEnabled: NO];
99         [o_sld_contrast setEnabled: NO];
100         [o_sld_gamma setEnabled: NO];
101         [o_sld_hue setEnabled: NO];
102         [o_sld_saturation setEnabled: NO];
103     }
104     if( psz_vfilters ) free( psz_vfilters );
105
106     int i_value = config_GetInt( p_intf, "hue" );
107     if( i_value > 0 && i_value < 360 )
108     {
109         [o_sld_hue setIntValue: i_value];
110     }
111     
112     float f_value;
113     
114     f_value = config_GetFloat( p_intf, "saturation" );
115     if( f_value > 0 && f_value < 5 )
116     {
117         [o_sld_saturation setIntValue: (int)(100 * f_value) ];
118     }
119     
120     f_value = config_GetFloat( p_intf, "contrast" );
121     if( f_value > 0 && f_value < 4 )
122     {
123         [o_sld_contrast setIntValue: (int)(100 * f_value) ];
124     }
125     
126     f_value = config_GetFloat( p_intf, "brightness" );
127     if( f_value > 0 && f_value < 2 )
128     {
129         [o_sld_brightness setIntValue: (int)(100 * f_value) ];
130     }
131     
132     f_value = config_GetFloat( p_intf, "gamma" );
133     if( f_value > 0 && f_value < 10 )
134     {
135         [o_sld_gamma setIntValue: (int)(10 * f_value) ];
136     }
137     
138     
139     /* set the audio-filter-checkboxes to the values taken from the prefs */
140     char * psz_afilters;
141     psz_afilters = config_GetPsz( p_intf, "audio-filter" );
142     if( psz_afilters )
143     {
144         [o_ckb_hdphnVirt setState: (int)strstr( psz_afilters, "headphone" ) ];
145         [o_ckb_vlme_norm setState: (int)strstr( psz_afilters, "normvol" ) ];
146         free( psz_afilters );
147     }
148 }
149
150 - (void)showPanel
151 {
152     /* show the window */
153     [o_extended_window displayIfNeeded];
154     [o_extended_window makeKeyAndOrderFront:nil];
155 }
156
157 - (IBAction)adjImg_Enbl:(id)sender
158 {
159     /* en-/disable the sliders */
160     if ([o_ckb_enblAdjustImg state] == NSOnState)
161     {
162         [o_btn_rstrDefaults setEnabled: YES];
163         [o_sld_brightness setEnabled: YES];
164         [o_sld_contrast setEnabled: YES];
165         [o_sld_gamma setEnabled: YES];
166         [o_sld_hue setEnabled: YES];
167         [o_sld_saturation setEnabled: YES];
168         [self changeVFiltersString: "adjust" onOrOff: YES];
169     }else{
170         [o_btn_rstrDefaults setEnabled: NO];
171         [o_sld_brightness setEnabled: NO];
172         [o_sld_contrast setEnabled: NO];
173         [o_sld_gamma setEnabled: NO];
174         [o_sld_hue setEnabled: NO];
175         [o_sld_saturation setEnabled: NO];
176         [self changeVFiltersString: "adjust" onOrOff: NO];
177     }
178 }
179
180 - (IBAction)adjImg_rstrDefaults:(id)sender
181 {
182     /* reset the sliders */
183     [o_sld_brightness setIntValue: 100];
184     [o_sld_contrast setIntValue: 100];
185     [o_sld_gamma setIntValue: 10];
186     [o_sld_hue setIntValue: 0];
187     [o_sld_saturation setIntValue: 100];
188     
189     /* transmit the values */
190     [self adjImg_sliders: o_sld_brightness];
191     [self adjImg_sliders: o_sld_contrast];
192     [self adjImg_sliders: o_sld_gamma];
193     [self adjImg_sliders: o_sld_hue];
194     [self adjImg_sliders: o_sld_saturation];
195 }
196
197 - (IBAction)adjImg_sliders:(id)sender
198 {
199     /* read-out the sliders' values and apply them */
200     intf_thread_t * p_intf = VLCIntf;
201     vout_thread_t *p_vout = (vout_thread_t *)vlc_object_find(p_intf,
202                                  VLC_OBJECT_VOUT, FIND_ANYWHERE);
203     if( p_vout == NULL )
204     {
205         if (sender == o_sld_brightness)
206         {
207             config_PutFloat( p_intf , "brightness" , [o_sld_brightness floatValue] / 100);
208         } else if (sender == o_sld_contrast)
209         {
210             config_PutFloat( p_intf , "contrast" , [o_sld_contrast floatValue] / 100);
211         } else if (sender == o_sld_gamma)
212         {
213             config_PutFloat( p_intf , "gamma" , [o_sld_gamma floatValue] / 10);
214         } else if (sender == o_sld_hue)
215         {
216             config_PutInt( p_intf , "hue" , [o_sld_hue intValue]);
217         } else if (sender == o_sld_saturation)
218         {
219             config_PutFloat( p_intf , "saturation" , [o_sld_saturation floatValue] / 100);
220         }
221     } else {
222         vlc_value_t val;
223         if (sender == o_sld_brightness)
224         {
225             val.f_float = [o_sld_brightness floatValue] / 100;
226             var_Set( p_vout, "brightness", val );
227             config_PutFloat( p_intf , "brightness" , [o_sld_brightness floatValue] / 100);
228         } else if (sender == o_sld_contrast)
229         {
230             val.f_float = [o_sld_contrast floatValue] / 100;
231             var_Set( p_vout, "contrast", val );
232             config_PutFloat( p_intf , "contrast" , [o_sld_contrast floatValue] / 100);
233         } else if (sender == o_sld_gamma)
234         {
235             val.f_float = [o_sld_gamma floatValue] / 10;
236             var_Set( p_vout, "gamma", val );
237             config_PutFloat( p_intf , "gamma" , [o_sld_gamma floatValue] / 10);
238         } else if (sender == o_sld_hue)
239         {
240             val.i_int = [o_sld_hue intValue];
241             var_Set( p_vout, "hue", val );
242             config_PutInt( p_intf , "hue" , [o_sld_hue intValue]);
243         } else if (sender == o_sld_saturation)
244         {
245             val.f_float = [o_sld_saturation floatValue] / 100;
246             var_Set( p_vout, "saturation", val );
247             config_PutFloat( p_intf , "saturation" , [o_sld_saturation floatValue] / 100);
248         }
249     }
250 }
251
252 - (IBAction)audFtls_hdphnVirt:(id)sender
253 {
254     /* en-/disable headphone virtualisation */
255     if ([o_ckb_hdphnVirt state] == NSOnState)
256     {
257         [self changeAFiltersString: "headphone" onOrOff: YES ];
258     }else{
259         [self changeAFiltersString: "headphone" onOrOff: NO ];
260     }
261 }
262
263 - (IBAction)audFtls_maxLevelSld:(id)sender
264 {
265     /* read-out the slider's value and apply it */
266     intf_thread_t * p_intf = VLCIntf;
267     aout_instance_t * p_aout= (aout_instance_t *)vlc_object_find(p_intf,
268                                  VLC_OBJECT_AOUT, FIND_ANYWHERE);
269     if( p_aout != NULL )
270     {
271         var_SetFloat( p_aout, "norm-max-level", [o_sld_maxLevel floatValue] / 10 );
272         vlc_object_release( p_aout );
273     }
274     else
275     {
276         config_PutFloat( p_intf, "norm-max-level", [o_sld_maxLevel floatValue] /10 );
277     }
278 }
279
280 - (IBAction)audFtls_vlmeNorm:(id)sender
281 {
282     /* en-/disable volume normalisation */
283     if ([o_ckb_vlme_norm state] == NSOnState)
284     {
285         [self changeAFiltersString: "normvol" onOrOff: YES ];
286     }else{
287         [self changeAFiltersString: "normvol" onOrOff: NO ];
288     }
289 }
290
291 - (IBAction)extWin_exp_adjImg:(id)sender
292 {
293     /* expand or collapse adjImg */
294     NSRect o_win_rect = [o_extended_window frame];
295     NSRect o_box_audFlts_rect = [o_box_audFlts frame];
296     NSRect o_box_vidFlts_rect = [o_box_vidFlts frame];
297     NSRect o_box_adjImg_rect = [o_box_adjImg frame];
298     
299     if (o_adjImg_expanded)
300     {
301         /* move the window contents upwards (partially done through settings
302          * inside the nib) and resize the window */
303         o_win_rect.size.height = o_win_rect.size.height - 151;
304         o_win_rect.origin.y = [o_extended_window frame].origin.y + 151;
305         o_box_audFlts_rect.origin.y = o_box_audFlts_rect.origin.y + 151;
306         o_box_vidFlts_rect.origin.y = o_box_vidFlts_rect.origin.y + 151;
307         
308         /* remove the inserted view */
309         [o_adjustImg_view removeFromSuperviewWithoutNeedingDisplay];
310     }else{
311     
312         /* move the window contents downwards and resize the window */
313         o_win_rect.size.height = o_win_rect.size.height + 151;
314         o_win_rect.origin.y = [o_extended_window frame].origin.y - 151;
315         o_box_audFlts_rect.origin.y = o_box_audFlts_rect.origin.y - 151;
316         o_box_vidFlts_rect.origin.y = o_box_vidFlts_rect.origin.y - 151;
317     }
318     
319     [o_box_audFlts setFrameFromContentFrame: o_box_audFlts_rect];
320     [o_box_vidFlts setFrameFromContentFrame: o_box_vidFlts_rect];
321     [o_extended_window displayIfNeeded];
322     [o_extended_window setFrame: o_win_rect display:YES animate: YES];
323     
324     if (o_adjImg_expanded)
325     {
326         o_box_adjImg_rect.size.height = [o_box_adjImg frame].size.height - 151;
327         o_adjImg_expanded = NO;
328     } else {
329         /* insert view */
330         o_box_adjImg_rect.size.height = [o_box_adjImg frame].size.height + 151;
331         [o_adjustImg_view setFrame: NSMakeRect( 20, -10, 370, 161)];
332         [o_adjustImg_view setNeedsDisplay:YES];
333         [o_adjustImg_view setAutoresizesSubviews: YES];
334         [[o_box_adjImg contentView] addSubview: o_adjustImg_view];
335         o_adjImg_expanded = YES;
336     }
337     [o_box_adjImg setFrameFromContentFrame: o_box_adjImg_rect];
338 }
339
340 - (IBAction)extWin_exp_audFlts:(id)sender
341 {
342     /* expand or collapse audFlts */
343     NSRect o_win_rect = [o_extended_window frame];
344     NSRect o_box_audFlts_rect = [o_box_audFlts frame];
345     
346     if (o_audFlts_expanded)
347     {
348         /* move the window contents upwards (partially done through settings
349          * inside the nib) and resize the window */
350         o_win_rect.size.height = o_win_rect.size.height - 66;
351         o_win_rect.origin.y = [o_extended_window frame].origin.y + 66;
352         
353         /* remove the inserted view */
354         [o_audioFlts_view removeFromSuperviewWithoutNeedingDisplay];
355     }else{
356         /* move the window contents downwards and resize the window */
357         o_win_rect.size.height = o_win_rect.size.height + 66;
358         o_win_rect.origin.y = [o_extended_window frame].origin.y - 66;
359     }
360     [o_extended_window displayIfNeeded];
361     [o_extended_window setFrame: o_win_rect display:YES animate: YES];
362     
363     
364     if (o_audFlts_expanded)
365     {
366         o_box_audFlts_rect.size.height = [o_box_audFlts frame].size.height - 66;
367         o_audFlts_expanded = NO;
368     } else {
369         /* insert view */
370         o_box_audFlts_rect.size.height = [o_box_audFlts frame].size.height + 66;
371         [o_audioFlts_view setFrame: NSMakeRect( 20, -20, 370, 76)];
372         [o_audioFlts_view setNeedsDisplay:YES];
373         [o_audioFlts_view setAutoresizesSubviews: YES];
374         [[o_box_audFlts contentView] addSubview: o_audioFlts_view];
375         o_audFlts_expanded = YES;
376     }
377     [o_box_audFlts setFrameFromContentFrame: o_box_audFlts_rect];
378 }
379
380 - (IBAction)extWin_exp_vidFlts:(id)sender
381 {
382     /* expand or collapse vidFlts */
383     NSRect o_win_rect = [o_extended_window frame];
384     NSRect o_box_audFlts_rect = [o_box_audFlts frame];
385     NSRect o_box_vidFlts_rect = [o_box_vidFlts frame];
386     
387     if (o_vidFlts_expanded)
388     {
389         /* move the window contents upwards (partially done through settings
390          * inside the nib) and resize the window */
391         o_win_rect.size.height = o_win_rect.size.height - 134;
392         o_win_rect.origin.y = [o_extended_window frame].origin.y + 134;
393         o_box_audFlts_rect.origin.y = o_box_audFlts_rect.origin.y + 134;
394         
395         /* remove the inserted view */
396         [o_videoFilters_view removeFromSuperviewWithoutNeedingDisplay];
397     }else{
398     
399         /* move the window contents downwards and resize the window */
400         o_win_rect.size.height = o_win_rect.size.height + 134;
401         o_win_rect.origin.y = [o_extended_window frame].origin.y - 134;
402         o_box_audFlts_rect.origin.y = o_box_audFlts_rect.origin.y - 134;
403     }
404     
405     [o_box_audFlts setFrameFromContentFrame: o_box_audFlts_rect];
406     [o_extended_window displayIfNeeded];
407     [o_extended_window setFrame: o_win_rect display:YES animate: YES];
408     
409     if (o_vidFlts_expanded)
410     {
411         o_box_vidFlts_rect.size.height = [o_box_vidFlts frame].size.height - 134;
412         o_vidFlts_expanded = NO;
413     } else {
414         /* insert view */
415         o_box_vidFlts_rect.size.height = [o_box_vidFlts frame].size.height + 134;
416         [o_videoFilters_view setFrame: NSMakeRect( 20, -10, 370, 144)];
417         [o_videoFilters_view setNeedsDisplay:YES];
418         [o_videoFilters_view setAutoresizesSubviews: YES];
419         [[o_box_vidFlts contentView] addSubview: o_videoFilters_view];
420         o_vidFlts_expanded = YES;
421     }
422     [o_box_vidFlts setFrameFromContentFrame: o_box_vidFlts_rect];
423 }
424
425 - (IBAction)vidFlts:(id)sender
426 {
427     /* en-/disable video filters */
428 }
429
430 - (IBAction)vidFlts_mrInfo:(id)sender
431 {
432     /* show info sheet */
433     NSBeginInformationalAlertSheet(_NS("More information"), _NS("OK"), @"", @"", \
434         o_extended_window, nil, nil, nil, nil, _NS("Select the video effects " \
435         "filters to apply. You must restart the stream for these settings to " \
436         "take effect.\nTo configure the filters, go to the Preferences, and " \
437         "go to Modules/Video Filters. You can then configure each filter.\n" \
438         "If you want fine control over the filters ( to choose the order in " \
439         "which they are applied ), you need to enter manually a filters " \
440         "string (Preferences / Video / Filters)."));
441 }
442
443 - (void)changeVFiltersString:(char *)psz_name onOrOff:(BOOL)o_onOrOff 
444 {
445     /* copied from ../wxwidgets/extrapanel.cpp
446      * renamed to conform with Cocoa's rules */
447      
448     vout_thread_t *p_vout;
449     intf_thread_t * p_intf = VLCIntf;
450     
451     char *psz_parser, *psz_string;
452     psz_string = config_GetPsz( p_intf, "vout-filter" );
453     
454     if( !psz_string ) psz_string = strdup("");
455
456     psz_parser = strstr( psz_string, psz_name );
457
458     if( o_onOrOff )
459     {
460         if( !psz_parser )
461         {
462             psz_parser = psz_string;
463             asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
464                             psz_string, psz_name );
465             free( psz_parser );
466         }
467         else
468         {
469             return;
470         }
471     }
472     else
473     {
474         if( psz_parser )
475         {
476             memmove( psz_parser, psz_parser + strlen(psz_name) +
477                             (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
478                             strlen(psz_parser + strlen(psz_name)) + 1 );
479
480             /* Remove trailing : : */
481             if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
482             {
483                 *(psz_string+strlen(psz_string ) -1 ) = '\0';
484             }
485          }
486          else
487          {
488              free( psz_string );
489              return;
490          }
491     }
492     /* Vout is not kept, so put that in the config */
493     config_PutPsz( p_intf, "vout-filter", psz_string );
494
495     /* Try to set on the fly */
496     p_vout = (vout_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
497                                               FIND_ANYWHERE );
498     if( p_vout )
499     {
500         var_SetString( p_vout, "vout-filter", psz_string );
501         vlc_object_release( p_vout );
502     }
503
504     free( psz_string );
505 }
506
507
508 - (void)changeAFiltersString: (char *)psz_name onOrOff: (BOOL)o_onOrOff;
509 {
510     char *psz_parser, *psz_string;
511     intf_thread_t * p_intf = VLCIntf;
512     aout_instance_t * p_aout= (aout_instance_t *)vlc_object_find(p_intf,
513                                  VLC_OBJECT_AOUT, FIND_ANYWHERE);
514
515     if( p_aout )
516     {
517         psz_string = var_GetString( p_aout, "audio-filter" );
518     }
519     else
520     {
521         psz_string = config_GetPsz( p_intf, "audio-filter" );
522     }
523
524     if( !psz_string ) psz_string = strdup("");
525
526     psz_parser = strstr( psz_string, psz_name );
527
528     if( o_onOrOff )
529     {
530         if( !psz_parser )
531         {
532             psz_parser = psz_string;
533             asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
534                             psz_string, psz_name );
535             free( psz_parser );
536         }
537         else
538         {
539             return;
540         }
541     }
542     else
543     {
544         if( psz_parser )
545         {
546             memmove( psz_parser, psz_parser + strlen(psz_name) +
547                             (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
548                             strlen(psz_parser + strlen(psz_name)) + 1 );
549
550             if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
551             {
552                 *(psz_string+strlen(psz_string ) -1 ) = '\0';
553             }
554          }
555          else
556          {
557              free( psz_string );
558              return;
559          }
560     }
561
562     if( p_aout == NULL )
563     {
564         config_PutPsz( p_intf, "audio-filter", psz_string );
565     }
566     else
567     {
568         var_SetString( p_aout, "audio-filter", psz_string );
569         int i = 0;
570         while( i < p_aout->i_nb_inputs )
571         {
572             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
573             i = (i + 1);
574         }
575         vlc_object_release( p_aout );
576     }
577     free( psz_string );
578 }
579 @end