]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
* Use psz_shortname instead of object name in the Preferences tree of OS X prefs.
[vlc] / modules / gui / macosx / prefs.m
1 /*****************************************************************************
2  * prefs.m: MacOS X module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2005 the VideoLAN team
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         [o_prefs_view setDocumentView: o_empty_view];
150         config_ResetAll( p_intf );
151         [[VLCTreeItem rootItem] resetView];
152         [[o_tree itemAtRow:[o_tree selectedRow]]
153             showView:o_prefs_view advancedView:
154             ( [o_advanced_ckb state] == NSOnState ) ? VLC_TRUE : VLC_FALSE];
155     }
156 }
157
158 - (IBAction)advancedToggle: (id)sender
159 {
160     b_advanced = !b_advanced;
161     [o_advanced_ckb setState: b_advanced];
162     /* refresh the view of the current treeitem */
163     [[o_tree itemAtRow:[o_tree selectedRow]] showView:o_prefs_view advancedView:
164         ( [o_advanced_ckb state] == NSOnState ) ? VLC_TRUE : VLC_FALSE];
165 }
166
167 - (void)loadConfigTree
168 {
169 }
170
171 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
172 {
173 }
174
175 /* update the document view to the view of the selected tree item */
176 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
177 {
178     [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view
179         advancedView:( [o_advanced_ckb state] == NSOnState ) ?
180         VLC_TRUE : VLC_FALSE];
181 }
182
183 @end
184
185 @implementation VLCPrefs (NSTableDataSource)
186
187 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
188     return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] :
189                             [item numberOfChildren];
190 }
191
192 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
193 {
194     return (item == nil) ? YES : ( ([item numberOfChildren] != -1) && 
195                                    ([item numberOfChildren] != 0));
196 }
197
198 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
199     return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] :
200                             [item childAtIndex:index];
201 }
202
203 - (id)outlineView:(NSOutlineView *)outlineView
204     objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
205 {
206     return (item == nil) ? @"" : (id)[item getName];
207 }
208
209 @end
210
211 @implementation VLCTreeItem
212
213 static VLCTreeItem *o_root_item = nil;
214
215 #define IsALeafNode ((id)-1)
216
217 - (id)initWithName: (NSString *)o_item_name ID: (int)i_id
218     parent:(VLCTreeItem *)o_parent_item
219     children:(NSMutableArray *)o_children_array
220     whithCategory: (int) i_category
221 {
222     self = [super init];
223
224     if( self != nil )
225     {
226         o_name = [o_item_name copy];
227         i_object_id = i_id;
228         o_parent = o_parent_item;
229         o_children = o_children_array;
230         i_object_category = i_category;
231         o_subviews = nil;
232     }
233     return( self );
234 }
235
236 + (VLCTreeItem *)rootItem
237 {
238    if (o_root_item == nil)
239         o_root_item = [[VLCTreeItem alloc] initWithName:@"main" ID:0
240             parent:nil children:[[NSMutableArray alloc] initWithCapacity:10]
241             whithCategory: -1];
242    return o_root_item;
243 }
244
245 - (void)dealloc
246 {
247     if (o_children != IsALeafNode) [o_children release];
248     [o_name release];
249     [super dealloc];
250 }
251
252 /* Creates and returns the array of children
253  * Loads children incrementally */
254 - (NSArray *)children
255 {
256     if( o_children == IsALeafNode )
257         return o_children;
258     if( [ o_children count] == 0 )
259     {
260         intf_thread_t   *p_intf = VLCIntf;
261         vlc_list_t      *p_list;
262         module_t        *p_module = NULL;
263         module_config_t *p_item;
264         int             i_index;
265
266         /* List the modules */
267         p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
268         if( !p_list ) return nil;
269
270         if( [[self getName] isEqualToString: @"main"] )
271         {
272             /*
273             * Find the main module
274             */
275             for( i_index = 0; i_index < p_list->i_count; i_index++ )
276             {
277                 p_module = (module_t *)p_list->p_values[i_index].p_object;
278                 if( !strcmp( p_module->psz_object_name, "main" ) )
279                     break;
280             }
281             if( p_module == NULL )
282             {
283                 msg_Err( p_intf,
284                     "could not find the main module in our preferences" );
285                 return nil;
286             }
287             if( i_index < p_list->i_count )
288             {
289                 /* We found the main module */
290                 /* Enumerate config categories and store a reference so we can
291                  * generate their config panel them when it is asked by the user. */
292                 VLCTreeItem *p_last_category = NULL;
293                 p_item = p_module->p_config;
294                 o_children = [[NSMutableArray alloc] initWithCapacity:10];
295                 if( p_item ) do
296                 {
297                     NSString *o_child_name;
298                     switch( p_item->i_type )
299                     {
300                     case CONFIG_CATEGORY:
301                         o_child_name = [[VLCMain sharedInstance]
302     localizedString: config_CategoryNameGet(p_item->i_value ) ];
303                         p_last_category = [VLCTreeItem alloc];
304                         [o_children addObject:[p_last_category
305                             initWithName: o_child_name
306                             ID: p_item->i_value
307                             parent:self
308                             children:[[NSMutableArray alloc]
309                                 initWithCapacity:10]
310                             whithCategory: p_item - p_module->p_config]];
311                         break;
312                     case CONFIG_SUBCATEGORY:
313                         o_child_name = [[VLCMain sharedInstance]
314     localizedString: config_CategoryNameGet(p_item->i_value ) ];
315                         if( p_item->i_value != SUBCAT_PLAYLIST_GENERAL &&
316                             p_item->i_value != SUBCAT_VIDEO_GENERAL &&
317                             p_item->i_value != SUBCAT_AUDIO_GENERAL )
318                             [p_last_category->o_children
319                                 addObject:[[VLCTreeItem alloc]
320                                 initWithName: o_child_name
321                                 ID: p_item->i_value
322                                 parent:p_last_category
323                                 children:[[NSMutableArray alloc]
324                                     initWithCapacity:10]
325                                 whithCategory: p_item - p_module->p_config]];
326                         break;
327                     default:
328                         break;
329                     }
330                 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
331             }
332
333             /* Build a tree of the plugins */
334             /* Add the capabilities */
335             for( i_index = 0; i_index < p_list->i_count; i_index++ )
336             {
337                 p_module = (module_t *)p_list->p_values[i_index].p_object;
338
339                 /* Exclude the main module */
340                 if( !strcmp( p_module->psz_object_name, "main" ) )
341                     continue;
342
343                 /* Exclude empty plugins (submodules don't have config */
344                 /* options, they are stored in the parent module) */
345                 if( p_module->b_submodule )
346                     continue;
347                 else
348                     p_item = p_module->p_config;
349
350                 if( !p_item ) continue;
351                 int i_category = -1;
352                 int i_subcategory = -1;
353                 int i_options = 0;
354                 do
355                 {
356                     if( p_item->i_type == CONFIG_CATEGORY )
357                         i_category = p_item->i_value;
358                     else if( p_item->i_type == CONFIG_SUBCATEGORY )
359                         i_subcategory = p_item->i_value;
360
361                     if( p_item->i_type & CONFIG_ITEM )
362                         i_options ++;
363                     if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
364                         break;
365                 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
366                 if( !i_options ) continue;
367
368                 /* Find the right category item */
369
370                 long cookie;
371                 vlc_bool_t b_found = VLC_FALSE;
372                 unsigned int i;
373                 VLCTreeItem* p_category_item, * p_subcategory_item;
374                 for (i = 0 ; i < [o_children count] ; i++)
375                 {
376                     p_category_item = [o_children objectAtIndex: i];
377                     if( p_category_item->i_object_id == i_category )
378                     {
379                         b_found = VLC_TRUE;
380                         break;
381                     }
382                 }
383                 if( !b_found ) continue;
384
385                 /* Find subcategory item */
386                 b_found = VLC_FALSE;
387                 cookie = -1;
388                 for (i = 0 ; i < [p_category_item->o_children count] ; i++)
389                 {
390                     p_subcategory_item = [p_category_item->o_children
391                                             objectAtIndex: i];
392                     if( p_subcategory_item->i_object_id == i_subcategory )
393                     {
394                         b_found = VLC_TRUE;
395                         break;
396                     }
397                 }
398                 if( !b_found )
399                     p_subcategory_item = p_category_item;
400
401                 [p_subcategory_item->o_children addObject:[[VLCTreeItem alloc]
402                     initWithName:[[VLCMain sharedInstance]
403                         localizedString: p_module->psz_shortname ?
404                         p_module->psz_shortname : p_module->psz_object_name ]
405                     ID: p_module->i_object_id
406                     parent:p_subcategory_item
407                     children:IsALeafNode
408                     whithCategory: -1]];
409             }
410         }
411         vlc_list_release( p_list );
412     }
413     return o_children;
414 }
415
416 - (int)getObjectID
417 {
418     return i_object_id;
419 }
420
421 - (NSString *)getName
422 {
423     return o_name;
424 }
425
426 - (VLCTreeItem *)childAtIndex:(int)i_index
427 {
428     return [[self children] objectAtIndex:i_index];
429 }
430
431 - (int)numberOfChildren {
432     id i_tmp = [self children];
433     return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
434 }
435
436 - (BOOL)hasPrefs:(NSString *)o_module_name
437 {
438     intf_thread_t *p_intf = VLCIntf;
439     module_t *p_parser;
440     vlc_list_t *p_list;
441     char *psz_module_name;
442     int i_index;
443
444     psz_module_name = (char *)[o_module_name UTF8String];
445
446     /* look for module */
447     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
448
449     for( i_index = 0; i_index < p_list->i_count; i_index++ )
450     {
451         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
452
453         if( !strcmp( p_parser->psz_object_name, psz_module_name ) )
454         {
455             BOOL b_has_prefs = p_parser->i_config_items != 0;
456             vlc_list_release( p_list );
457             return( b_has_prefs );
458         }
459     }
460
461     vlc_list_release( p_list );
462
463     return( NO );
464 }
465
466 - (NSView *)showView:(NSScrollView *)o_prefs_view
467     advancedView:(vlc_bool_t) b_advanced
468 {
469     NSRect          s_vrc;
470     NSView          *o_view;
471
472     s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
473     o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
474     [o_view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin |
475                                     NSViewMaxYMargin];
476
477 /* Create all subviews if it isn't already done because we cannot use */
478 /* setHiden for MacOS < 10.3*/
479     if( o_subviews == nil )
480     {
481         intf_thread_t   *p_intf = VLCIntf;
482         vlc_list_t      *p_list;
483         module_t        *p_parser = NULL;
484         module_config_t *p_item;
485
486         o_subviews = [[NSMutableArray alloc] initWithCapacity:10];
487         /* Get a pointer to the module */
488         if( i_object_category == -1 )
489         {
490             p_parser = (module_t *) vlc_object_get( p_intf, i_object_id );
491             if( !p_parser || p_parser->i_object_type != VLC_OBJECT_MODULE )
492             {
493                 /* 0OOoo something went really bad */
494                 return nil;
495             }
496             p_item = p_parser->p_config;
497             int i = 0;
498
499             p_item = p_parser->p_config + 1;
500
501             do
502             {
503                 if( !p_item )
504                 {
505                     msg_Err( p_intf, "null item found" );
506                     break;
507                 }
508                 switch(p_item->i_type)
509                 {
510                 case CONFIG_SUBCATEGORY:
511                     break;
512                 case CONFIG_SECTION:
513                     break;
514                 case CONFIG_CATEGORY:
515                     break;
516                 case CONFIG_HINT_END:
517                     break;
518                 case CONFIG_HINT_USAGE:
519                     break;
520                 default:
521                 {
522                     VLCConfigControl *o_control = nil;
523                     o_control = [VLCConfigControl newControl:p_item
524                                                   withView:o_view];
525                     if( o_control != nil )
526                     {
527                         [o_control setAutoresizingMask: NSViewMaxYMargin |
528                             NSViewWidthSizable];
529                         [o_subviews addObject: o_control];
530                     }
531                 }
532                     break;
533                 }
534             } while( p_item++->i_type != CONFIG_HINT_END );
535
536             vlc_object_release( p_parser );
537         }
538         else
539         {
540             int i = 0;
541             int i_index;
542             p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
543             if( !p_list ) return o_view;
544
545             /*
546             * Find the main module
547             */
548             for( i_index = 0; i_index < p_list->i_count; i_index++ )
549             {
550                 p_parser = (module_t *)p_list->p_values[i_index].p_object;
551                 if( !strcmp( p_parser->psz_object_name, "main" ) )
552                     break;
553             }
554             if( p_parser == NULL )
555             {
556                 msg_Err( p_intf, "could not find the main module in our "
557                                     "preferences" );
558                 return o_view;
559             }
560             p_item = (p_parser->p_config + i_object_category);
561             if( ( p_item->i_type == CONFIG_CATEGORY ) &&
562               ( ( p_item->i_value == CAT_PLAYLIST )  ||
563                 ( p_item->i_value == CAT_AUDIO )  ||
564                 ( p_item->i_value == CAT_VIDEO ) ) )
565                 p_item++;
566
567             do
568             {
569                 p_item++;
570                 if( !p_item )
571                 {
572                     msg_Err( p_intf, "null item found" );
573                     break;
574                 }
575                 switch(p_item->i_type)
576                 {
577                 case CONFIG_SUBCATEGORY:
578                     break;
579                 case CONFIG_SECTION:
580                     break;
581                 case CONFIG_CATEGORY:
582                     break;
583                 case CONFIG_HINT_END:
584                     break;
585                 case CONFIG_HINT_USAGE:
586                     break;
587                 default:
588                 {
589                     VLCConfigControl *o_control = nil;
590                     o_control = [VLCConfigControl newControl:p_item
591                                                   withView:o_view];
592                     if( o_control != nil )
593                     {
594                         [o_control setAutoresizingMask: NSViewMaxYMargin |
595                                                         NSViewWidthSizable];
596                         [o_subviews addObject: o_control];
597                     }
598                     break;
599                 }
600                 }
601             } while ( ( p_item->i_type != CONFIG_HINT_END ) &&
602                       ( p_item->i_type != CONFIG_SUBCATEGORY ) );
603
604             vlc_list_release( p_list );
605         }
606     }
607
608     if( o_view != nil )
609     {
610         int i_lastItem = 0;
611         int i_yPos = -2;
612         int i_max_label = 0;
613         int i_show_advanced = 0;
614
615         NSEnumerator *enumerator = [o_subviews objectEnumerator];
616         VLCConfigControl *o_widget;
617         NSRect o_frame;
618         
619         while( ( o_widget = [enumerator nextObject] ) )
620             if( ( [o_widget isAdvanced] ) && (! b_advanced) )
621                 continue;
622             else if( i_max_label < [o_widget getLabelSize] )
623                 i_max_label = [o_widget getLabelSize];
624
625         enumerator = [o_subviews objectEnumerator];
626         while( ( o_widget = [enumerator nextObject] ) )
627         {
628             int i_widget;
629             if( ( [o_widget isAdvanced] ) && (! b_advanced) )
630             {
631                 i_show_advanced++;
632                 continue;
633             }
634
635             i_widget = [o_widget getViewType];
636             i_yPos += [VLCConfigControl calcVerticalMargin:i_widget
637                 lastItem:i_lastItem];
638             [o_widget setYPos:i_yPos];
639             o_frame = [o_widget frame];
640             o_frame.size.width = [o_view frame].size.width -
641                                     LEFTMARGIN - RIGHTMARGIN;
642             [o_widget setFrame:o_frame];
643             [o_widget alignWithXPosition: i_max_label];
644             i_yPos += [o_widget frame].size.height;
645             i_lastItem = i_widget;
646             [o_view addSubview:o_widget];
647          }
648         if( i_show_advanced != 0 )
649         {
650             /* We add the advanced notice... */
651             NSRect s_rc = [o_view frame];
652             NSTextField *o_label;
653             s_rc.size.height = 17;
654             s_rc.origin.x = LEFTMARGIN;
655             s_rc.origin.y = i_yPos += [VLCConfigControl
656                                         calcVerticalMargin:CONFIG_ITEM_STRING
657                                         lastItem:i_lastItem];
658             o_label = [[[NSTextField alloc] initWithFrame: s_rc] retain];
659             [o_label setDrawsBackground: NO];
660             [o_label setBordered: NO];
661             [o_label setEditable: NO];
662             [o_label setSelectable: NO];
663             [o_label setStringValue: _NS("Some options are available but " \
664                                 "hidden. Check \"Advanced\" to see them.")];
665             [o_label setFont:[NSFont systemFontOfSize:10]];
666             [o_label sizeToFit];
667             [o_view addSubview:o_label];
668             i_yPos += [o_label frame].size.height;
669         }
670         o_frame = [o_view frame];
671         o_frame.size.height = i_yPos;
672         [o_view setFrame:o_frame];
673         [o_prefs_view setDocumentView:o_view];
674
675     }
676     return o_view;
677 }
678
679 - (void)applyChanges
680 {
681     unsigned int i;
682     if( o_subviews != nil )
683         //Item has been shown
684         for( i = 0 ; i < [o_subviews count] ; i++ )
685             [[o_subviews objectAtIndex:i] applyChanges];
686
687     if( o_children != IsALeafNode )
688         for( i = 0 ; i < [o_children count] ; i++ )
689             [[o_children objectAtIndex:i] applyChanges];
690 }
691
692 - (void)resetView
693 {
694     unsigned int i;
695     if( o_subviews != nil )
696     {
697         //Item has been shown
698         [o_subviews release];
699         o_subviews = nil;
700     }
701
702     if( o_children != IsALeafNode )
703         for( i = 0 ; i < [o_children count] ; i++ )
704             [[o_children objectAtIndex:i] resetView];
705 }
706
707 @end
708
709
710 @implementation VLCFlippedView
711
712 - (BOOL)isFlipped
713 {
714     return( YES );
715 }
716
717 @end