]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
Backport of [11415]
[vlc] / modules / gui / macosx / prefs.m
1 /*****************************************************************************
2  * prefs.m: MacOS X module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /* VLCPrefs manages the main preferences dialog 
26    the class is related to wxwindows intf, PrefsPanel */
27 /* VLCTreeItem should contain:
28    - the children of the treeitem
29    - the associated prefs widgets
30    - the documentview with all the prefs widgets in it
31    - a saveChanges action
32    - a revertChanges action
33    - an advanced action (to hide/show advanced options)
34    - a redraw view action
35    - the children action should generate a list of the treeitems children (to be used by VLCPrefs datasource)
36
37    The class is sort of a mix of wxwindows intfs, PrefsTreeCtrl and ConfigTreeData
38 */
39 /* VLCConfigControl are subclassed NSView's containing and managing individual config items
40    the classes are VERY closely related to wxwindows ConfigControls */
41
42 /*****************************************************************************
43  * Preamble
44  *****************************************************************************/
45 #include <stdlib.h>                                      /* malloc(), free() */
46 #include <sys/param.h>                                    /* for MAXPATHLEN */
47 #include <string.h>
48
49 #include <vlc/vlc.h>
50 #include <vlc_config_cat.h>
51
52 #include "intf.h"
53 #include "prefs.h"
54 #include "prefs_widgets.h"
55 #include "vlc_keys.h"
56
57 /*****************************************************************************
58  * VLCPrefs implementation
59  *****************************************************************************/
60 @implementation VLCPrefs
61
62 static VLCPrefs *_o_sharedMainInstance = nil;
63
64 + (VLCPrefs *)sharedInstance
65 {
66     return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
67 }
68
69 - (id)init
70 {
71     if( _o_sharedMainInstance ) {
72         [self dealloc];
73     }
74     else
75     {
76         _o_sharedMainInstance = [super init];
77         p_intf = VLCIntf;
78         o_empty_view = [[NSView alloc] init];
79     }
80
81     return _o_sharedMainInstance;
82 }
83
84 - (void)dealloc
85 {
86     [o_empty_view release];
87     [super dealloc];
88 }
89
90 - (void)awakeFromNib
91 {
92     p_intf = VLCIntf;
93     b_advanced = config_GetInt( p_intf, "advanced" );
94
95     [self initStrings];
96     [o_advanced_ckb setState: b_advanced];
97     [o_prefs_view setBorderType: NSGrooveBorder];
98     [o_prefs_view setHasVerticalScroller: YES];
99     [o_prefs_view setDrawsBackground: NO];
100     [o_prefs_view setDocumentView: o_empty_view];
101     [o_tree selectRow:0 byExtendingSelection:NO];
102 }
103
104 - (void)showPrefs
105 {
106     /* load our nib (if not already loaded) */
107     [NSBundle loadNibNamed:@"Preferences" owner:self];
108
109     [o_prefs_window center];
110     [o_prefs_window makeKeyAndOrderFront:self];
111 }
112
113 - (void)initStrings
114 {
115     [o_prefs_window setTitle: _NS("Preferences")];
116     [o_save_btn setTitle: _NS("Save")];
117     [o_cancel_btn setTitle: _NS("Cancel")];
118     [o_reset_btn setTitle: _NS("Reset All")];
119     [o_advanced_ckb setTitle: _NS("Advanced")];
120 }
121
122 - (IBAction)savePrefs: (id)sender
123 {
124     /* TODO: call savePrefs on Root item */
125     [[VLCTreeItem rootItem] applyChanges];
126     config_SaveConfigFile( p_intf, NULL );
127     [o_prefs_window orderOut:self];
128 }
129
130 - (IBAction)closePrefs: (id)sender
131 {
132     [o_prefs_window orderOut:self];
133 }
134
135 - (IBAction)resetAll: (id)sender
136 {
137     NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
138         _NS("Continue"), nil, o_prefs_window, self,
139         @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
140         _NS("Beware this will reset your VLC media player preferences.\n"
141             "Are you sure you want to continue?") );
142 }
143
144 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
145     contextInfo:(void *)o_context
146 {
147     if( i_return == NSAlertAlternateReturn )
148     {
149         config_ResetAll( p_intf );
150         [[o_tree itemAtRow:[o_tree selectedRow]]
151             showView:o_prefs_view advancedView:
152             ( [o_advanced_ckb state] == NSOnState ) ? VLC_TRUE : VLC_FALSE];
153     }
154 }
155
156 - (IBAction)advancedToggle: (id)sender
157 {
158     b_advanced = !b_advanced;
159     [o_advanced_ckb setState: b_advanced];
160     /* refresh the view of the current treeitem */
161     [[o_tree itemAtRow:[o_tree selectedRow]] showView:o_prefs_view advancedView:
162         ( [o_advanced_ckb state] == NSOnState ) ? VLC_TRUE : VLC_FALSE];
163 }
164
165 - (void)loadConfigTree
166 {
167 }
168
169 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
170 {
171 }
172
173 /* update the document view to the view of the selected tree item */
174 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
175 {
176     [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view
177         advancedView:( [o_advanced_ckb state] == NSOnState ) ?
178         VLC_TRUE : VLC_FALSE];
179 }
180
181 @end
182
183 @implementation VLCPrefs (NSTableDataSource)
184
185 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
186     return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] :
187                             [item numberOfChildren];
188 }
189
190 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
191 {
192     return (item == nil) ? YES : ( ([item numberOfChildren] != -1) && 
193                                    ([item numberOfChildren] != 0));
194 }
195
196 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
197     return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] :
198                             [item childAtIndex:index];
199 }
200
201 - (id)outlineView:(NSOutlineView *)outlineView
202     objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
203 {
204     return (item == nil) ? @"" : (id)[item getName];
205 }
206
207 @end
208
209 @implementation VLCTreeItem
210
211 static VLCTreeItem *o_root_item = nil;
212
213 #define IsALeafNode ((id)-1)
214
215 - (id)initWithName: (NSString *)o_item_name ID: (int)i_id
216     parent:(VLCTreeItem *)o_parent_item
217     children:(NSMutableArray *)o_children_array
218     whithCategory: (int) i_category
219 {
220     self = [super init];
221
222     if( self != nil )
223     {
224         o_name = [o_item_name copy];
225         i_object_id = i_id;
226         o_parent = o_parent_item;
227         o_children = o_children_array;
228         i_object_category = i_category;
229         o_subviews = nil;
230     }
231     return( self );
232 }
233
234 + (VLCTreeItem *)rootItem
235 {
236    if (o_root_item == nil)
237         o_root_item = [[VLCTreeItem alloc] initWithName:@"main" ID:0
238             parent:nil children:[[NSMutableArray alloc] initWithCapacity:10]
239             whithCategory: -1];
240    return o_root_item;
241 }
242
243 - (void)dealloc
244 {
245     if (o_children != IsALeafNode) [o_children release];
246     [o_name release];
247     [super dealloc];
248 }
249
250 /* Creates and returns the array of children
251  * Loads children incrementally */
252 - (NSArray *)children
253 {
254     if( o_children == IsALeafNode )
255         return o_children;
256     if( [ o_children count] == 0 )
257     {
258         intf_thread_t   *p_intf = VLCIntf;
259         vlc_list_t      *p_list;
260         module_t        *p_module = NULL;
261         module_config_t *p_item;
262         int             i_index;
263
264         /* List the modules */
265         p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
266         if( !p_list ) return nil;
267
268         if( [[self getName] isEqualToString: @"main"] )
269         {
270             /*
271             * Find the main module
272             */
273             for( i_index = 0; i_index < p_list->i_count; i_index++ )
274             {
275                 p_module = (module_t *)p_list->p_values[i_index].p_object;
276                 if( !strcmp( p_module->psz_object_name, "main" ) )
277                     break;
278             }
279             if( p_module == NULL )
280             {
281                 msg_Err( p_intf,
282                     "could not find the main module in our preferences" );
283                 return nil;
284             }
285             if( i_index < p_list->i_count )
286             {
287                 /* We found the main module */
288                 /* Enumerate config categories and store a reference so we can
289                  * generate their config panel them when it is asked by the user. */
290                 VLCTreeItem *p_last_category = NULL;
291                 p_item = p_module->p_config;
292                 o_children = [[NSMutableArray alloc] initWithCapacity:10];
293                 if( p_item ) do
294                 {
295                     NSString *o_child_name;
296                     switch( p_item->i_type )
297                     {
298                     case CONFIG_CATEGORY:
299                         o_child_name = [[VLCMain sharedInstance]
300     localizedString: config_CategoryNameGet(p_item->i_value ) ];
301                         p_last_category = [VLCTreeItem alloc];
302                         [o_children addObject:[p_last_category
303                             initWithName: o_child_name
304                             ID: p_item->i_value
305                             parent:self
306                             children:[[NSMutableArray alloc]
307                                 initWithCapacity:10]
308                             whithCategory: p_item - p_module->p_config]];
309                         break;
310                     case CONFIG_SUBCATEGORY:
311                         o_child_name = [[VLCMain sharedInstance]
312     localizedString: config_CategoryNameGet(p_item->i_value ) ];
313                         if( p_item->i_value != SUBCAT_VIDEO_GENERAL &&
314                             p_item->i_value != SUBCAT_AUDIO_GENERAL )
315                             [p_last_category->o_children
316                                 addObject:[[VLCTreeItem alloc]
317                                 initWithName: o_child_name
318                                 ID: p_item->i_value
319                                 parent:p_last_category
320                                 children:[[NSMutableArray alloc]
321                                     initWithCapacity:10]
322                                 whithCategory: p_item - p_module->p_config]];
323                         break;
324                     default:
325                         break;
326                     }
327                 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
328             }
329
330             /* Build a tree of the plugins */
331             /* Add the capabilities */
332             for( i_index = 0; i_index < p_list->i_count; i_index++ )
333             {
334                 p_module = (module_t *)p_list->p_values[i_index].p_object;
335
336                 /* Exclude the main module */
337                 if( !strcmp( p_module->psz_object_name, "main" ) )
338                     continue;
339
340                 /* Exclude empty plugins (submodules don't have config */
341                 /* options, they are stored in the parent module) */
342                 if( p_module->b_submodule )
343                     continue;
344                 else
345                     p_item = p_module->p_config;
346
347                 if( !p_item ) continue;
348                 int i_category = -1;
349                 int i_subcategory = -1;
350                 int i_options = 0;
351                 do
352                 {
353                     if( p_item->i_type == CONFIG_CATEGORY )
354                         i_category = p_item->i_value;
355                     else if( p_item->i_type == CONFIG_SUBCATEGORY )
356                         i_subcategory = p_item->i_value;
357
358                     if( p_item->i_type & CONFIG_ITEM )
359                         i_options ++;
360                     if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
361                         break;
362                 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
363                 if( !i_options ) continue;
364
365                 /* Find the right category item */
366
367                 long cookie;
368                 vlc_bool_t b_found = VLC_FALSE;
369                 unsigned int i;
370                 VLCTreeItem* p_category_item, * p_subcategory_item;
371                 for (i = 0 ; i < [o_children count] ; i++)
372                 {
373                     p_category_item = [o_children objectAtIndex: i];
374                     if( p_category_item->i_object_id == i_category )
375                     {
376                         b_found = VLC_TRUE;
377                         break;
378                     }
379                 }
380                 if( !b_found ) continue;
381
382                 /* Find subcategory item */
383                 b_found = VLC_FALSE;
384                 cookie = -1;
385                 for (i = 0 ; i < [p_category_item->o_children count] ; i++)
386                 {
387                     p_subcategory_item = [p_category_item->o_children
388                                             objectAtIndex: i];
389                     if( p_subcategory_item->i_object_id == i_subcategory )
390                     {
391                         b_found = VLC_TRUE;
392                         break;
393                     }
394                 }
395                 if( !b_found )
396                     p_subcategory_item = p_category_item;
397
398                 [p_subcategory_item->o_children addObject:[[VLCTreeItem alloc]
399                     initWithName:[[VLCMain sharedInstance]
400                         localizedString: p_module->psz_object_name ]
401                     ID: p_module->i_object_id
402                     parent:p_subcategory_item
403                     children:IsALeafNode
404                     whithCategory: -1]];
405             }
406         }
407         vlc_list_release( p_list );
408     }
409     return o_children;
410 }
411
412 - (int)getObjectID
413 {
414     return i_object_id;
415 }
416
417 - (NSString *)getName
418 {
419     return o_name;
420 }
421
422 - (VLCTreeItem *)childAtIndex:(int)i_index
423 {
424     return [[self children] objectAtIndex:i_index];
425 }
426
427 - (int)numberOfChildren {
428     id i_tmp = [self children];
429     return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
430 }
431
432 - (BOOL)hasPrefs:(NSString *)o_module_name
433 {
434     intf_thread_t *p_intf = VLCIntf;
435     module_t *p_parser;
436     vlc_list_t *p_list;
437     char *psz_module_name;
438     int i_index;
439
440     psz_module_name = (char *)[o_module_name UTF8String];
441
442     /* look for module */
443     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
444
445     for( i_index = 0; i_index < p_list->i_count; i_index++ )
446     {
447         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
448
449         if( !strcmp( p_parser->psz_object_name, psz_module_name ) )
450         {
451             BOOL b_has_prefs = p_parser->i_config_items != 0;
452             vlc_list_release( p_list );
453             return( b_has_prefs );
454         }
455     }
456
457     vlc_list_release( p_list );
458
459     return( NO );
460 }
461
462 - (NSView *)showView:(NSScrollView *)o_prefs_view
463     advancedView:(vlc_bool_t) b_advanced
464 {
465     NSRect          s_vrc;
466     NSView          *o_view;
467
468     s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
469     o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
470     [o_view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin |
471                                     NSViewMaxYMargin];
472
473 /* Create all subviews if it isn't already done because we cannot use */
474 /* setHiden for MacOS < 10.3*/
475     if( o_subviews == nil )
476     {
477         intf_thread_t   *p_intf = VLCIntf;
478         vlc_list_t      *p_list;
479         module_t        *p_parser = NULL;
480         module_config_t *p_item;
481
482         o_subviews = [[NSMutableArray alloc] initWithCapacity:10];
483         /* Get a pointer to the module */
484         if( i_object_category == -1 )
485         {
486             p_parser = (module_t *) vlc_object_get( p_intf, i_object_id );
487             if( !p_parser || p_parser->i_object_type != VLC_OBJECT_MODULE )
488             {
489                 /* 0OOoo something went really bad */
490                 return nil;
491             }
492             p_item = p_parser->p_config;
493             int i = 0;
494
495             p_item = p_parser->p_config + 1;
496
497             do
498             {
499                 if( !p_item )
500                 {
501                     msg_Err( p_intf, "null item found" );
502                     break;
503                 }
504                 switch(p_item->i_type)
505                 {
506                 case CONFIG_SUBCATEGORY:
507                     break;
508                 case CONFIG_SECTION:
509                     break;
510                 case CONFIG_CATEGORY:
511                     break;
512                 case CONFIG_HINT_END:
513                     break;
514                 case CONFIG_HINT_USAGE:
515                     break;
516                 default:
517                 {
518                     VLCConfigControl *o_control = nil;
519                     o_control = [VLCConfigControl newControl:p_item
520                                                   withView:o_view];
521                     if( o_control != nil )
522                     {
523                         [o_control setAutoresizingMask: NSViewMaxYMargin |
524                             NSViewWidthSizable];
525                         [o_subviews addObject: o_control];
526                     }
527                 }
528                     break;
529                 }
530             } while( p_item++->i_type != CONFIG_HINT_END );
531
532             vlc_object_release( p_parser );
533         }
534         else
535         {
536             int i = 0;
537             int i_index;
538             p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
539             if( !p_list ) return o_view;
540
541             /*
542             * Find the main module
543             */
544             for( i_index = 0; i_index < p_list->i_count; i_index++ )
545             {
546                 p_parser = (module_t *)p_list->p_values[i_index].p_object;
547                 if( !strcmp( p_parser->psz_object_name, "main" ) )
548                     break;
549             }
550             if( p_parser == NULL )
551             {
552                 msg_Err( p_intf, "could not find the main module in our "
553                                     "preferences" );
554                 return o_view;
555             }
556             p_item = (p_parser->p_config + i_object_category);
557             if( ( p_item->i_type == CONFIG_CATEGORY ) &&
558               ( ( p_item->i_value == CAT_AUDIO )  ||
559                 ( p_item->i_value == CAT_VIDEO ) ) )
560                 p_item++;
561
562             do
563             {
564                 p_item++;
565                 if( !p_item )
566                 {
567                     msg_Err( p_intf, "null item found" );
568                     break;
569                 }
570                 switch(p_item->i_type)
571                 {
572                 case CONFIG_SUBCATEGORY:
573                     break;
574                 case CONFIG_SECTION:
575                     break;
576                 case CONFIG_CATEGORY:
577                     break;
578                 case CONFIG_HINT_END:
579                     break;
580                 case CONFIG_HINT_USAGE:
581                     break;
582                 default:
583                 {
584                     VLCConfigControl *o_control = nil;
585                     o_control = [VLCConfigControl newControl:p_item
586                                                   withView:o_view];
587                     if( o_control != nil )
588                     {
589                         [o_control setAutoresizingMask: NSViewMaxYMargin |
590                                                         NSViewWidthSizable];
591                         [o_subviews addObject: o_control];
592                     }
593                     break;
594                 }
595                 }
596             } while ( ( p_item->i_type != CONFIG_HINT_END ) &&
597                       ( p_item->i_type != CONFIG_SUBCATEGORY ) );
598
599             vlc_list_release( p_list );
600         }
601     }
602
603     if( o_view != nil )
604     {
605         int i_lastItem = 0;
606         int i_yPos = -2;
607         int i_max_label = 0;
608         int i_show_advanced = 0;
609
610         NSEnumerator *enumerator = [o_subviews objectEnumerator];
611         VLCConfigControl *o_widget;
612         NSRect o_frame;
613         
614         while( ( o_widget = [enumerator nextObject] ) )
615             if( ( [o_widget isAdvanced] ) && (! b_advanced) )
616                 continue;
617             else if( i_max_label < [o_widget getLabelSize] )
618                 i_max_label = [o_widget getLabelSize];
619
620         enumerator = [o_subviews objectEnumerator];
621         while( ( o_widget = [enumerator nextObject] ) )
622         {
623             int i_widget;
624             if( ( [o_widget isAdvanced] ) && (! b_advanced) )
625             {
626                 i_show_advanced++;
627                 continue;
628             }
629
630             i_widget = [o_widget getViewType];
631             i_yPos += [VLCConfigControl calcVerticalMargin:i_widget
632                 lastItem:i_lastItem];
633             [o_widget setYPos:i_yPos];
634             o_frame = [o_widget frame];
635             o_frame.size.width = [o_view frame].size.width -
636                                     LEFTMARGIN - RIGHTMARGIN;
637             [o_widget setFrame:o_frame];
638             [o_widget alignWithXPosition: i_max_label];
639             i_yPos += [o_widget frame].size.height;
640             i_lastItem = i_widget;
641             [o_view addSubview:o_widget];
642          }
643         if( i_show_advanced != 0 )
644         {
645             /* We add the advanced notice... */
646             NSRect s_rc = [o_view frame];
647             NSTextField *o_label;
648             s_rc.size.height = 17;
649             s_rc.origin.x = LEFTMARGIN;
650             s_rc.origin.y = i_yPos += [VLCConfigControl
651                                         calcVerticalMargin:CONFIG_ITEM_STRING
652                                         lastItem:i_lastItem];
653             o_label = [[[NSTextField alloc] initWithFrame: s_rc] retain];
654             [o_label setDrawsBackground: NO];
655             [o_label setBordered: NO];
656             [o_label setEditable: NO];
657             [o_label setSelectable: NO];
658             [o_label setStringValue: _NS("Some options are available but " \
659                                 "hidden. Check \"Advanced\" to see them.")];
660             [o_label setFont:[NSFont systemFontOfSize:10]];
661             [o_label sizeToFit];
662             [o_view addSubview:o_label];
663             i_yPos += [o_label frame].size.height;
664         }
665         o_frame = [o_view frame];
666         o_frame.size.height = i_yPos;
667         [o_view setFrame:o_frame];
668         [o_prefs_view setDocumentView:o_view];
669
670     }
671     return o_view;
672 }
673
674 - (void)applyChanges
675 {
676     unsigned int i;
677     if( o_subviews != nil )
678         //Item has been shown
679         for( i = 0 ; i < [o_subviews count] ; i++ )
680             [[o_subviews objectAtIndex:i] applyChanges];
681
682     if( o_children != IsALeafNode )
683         for( i = 0 ; i < [o_children count] ; i++ )
684             [[o_children objectAtIndex:i] applyChanges];
685 }
686
687 @end
688
689
690 @implementation VLCFlippedView
691
692 - (BOOL)isFlipped
693 {
694     return( YES );
695 }
696
697 @end