]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
*: remove useless code
[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 setRulersVisible: NO];
101     [o_prefs_view setDocumentView: o_empty_view];
102     [o_tree selectRow:0 byExtendingSelection:NO];
103 }
104
105 - (void)showPrefs
106 {
107     /* load our nib (if not already loaded) */
108     [NSBundle loadNibNamed:@"Preferences" owner:self];
109
110     [o_prefs_window center];
111     [o_prefs_window makeKeyAndOrderFront:self];
112 }
113
114 - (void)initStrings
115 {
116     [o_prefs_window setTitle: _NS("Preferences")];
117     [o_save_btn setTitle: _NS("Save")];
118     [o_cancel_btn setTitle: _NS("Cancel")];
119     [o_reset_btn setTitle: _NS("Reset All")];
120     [o_advanced_ckb setTitle: _NS("Advanced")];
121 }
122
123 - (IBAction)savePrefs: (id)sender
124 {
125     /* TODO: call savePrefs on Root item */
126     [[VLCTreeItem rootItem] applyChanges];
127     config_SaveConfigFile( p_intf, NULL );
128     [o_prefs_window orderOut:self];
129 }
130
131 - (IBAction)closePrefs: (id)sender
132 {
133     [o_prefs_window orderOut:self];
134 }
135
136 - (IBAction)resetAll: (id)sender
137 {
138     NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
139         _NS("Continue"), nil, o_prefs_window, self,
140         @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
141         _NS("Beware this will reset your VLC media player preferences.\n"
142             "Are you sure you want to continue?") );
143 }
144
145 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
146     contextInfo:(void *)o_context
147 {
148     if( i_return == NSAlertAlternateReturn )
149     {
150         config_ResetAll( p_intf );
151         [[o_tree itemAtRow:[o_tree selectedRow]]
152             showView:o_prefs_view advancedView:
153             ( [o_advanced_ckb state] == NSOnState ) ? VLC_TRUE : VLC_FALSE];
154     }
155 }
156
157 - (IBAction)advancedToggle: (id)sender
158 {
159     b_advanced = !b_advanced;
160     [o_advanced_ckb setState: b_advanced];
161     /* refresh the view of the current treeitem */
162     [[o_tree itemAtRow:[o_tree selectedRow]] showView:o_prefs_view advancedView:
163         ( [o_advanced_ckb state] == NSOnState ) ? VLC_TRUE : VLC_FALSE];
164 }
165
166 - (void)loadConfigTree
167 {
168 }
169
170 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
171 {
172 }
173
174 /* update the document view to the view of the selected tree item */
175 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
176 {
177     [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view
178         advancedView:( [o_advanced_ckb state] == NSOnState ) ?
179         VLC_TRUE : VLC_FALSE];
180 }
181
182 @end
183
184 @implementation VLCPrefs (NSTableDataSource)
185
186 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
187     return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] :
188                             [item numberOfChildren];
189 }
190
191 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
192 {
193     return (item == nil) ? YES : ( ([item numberOfChildren] != -1) && 
194                                    ([item numberOfChildren] != 0));
195 }
196
197 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
198     return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] :
199                             [item childAtIndex:index];
200 }
201
202 - (id)outlineView:(NSOutlineView *)outlineView
203     objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
204 {
205     return (item == nil) ? @"" : (id)[item getName];
206 }
207
208 @end
209
210 @implementation VLCTreeItem
211
212 static VLCTreeItem *o_root_item = nil;
213
214 #define IsALeafNode ((id)-1)
215
216 - (id)initWithName: (NSString *)o_item_name ID: (int)i_id
217     parent:(VLCTreeItem *)o_parent_item
218     children:(NSMutableArray *)o_children_array
219     whithCategory: (int) i_category
220 {
221     self = [super init];
222
223     if( self != nil )
224     {
225         o_name = [o_item_name copy];
226         i_object_id = i_id;
227         o_parent = o_parent_item;
228         o_children = o_children_array;
229         i_object_category = i_category;
230         o_subviews = nil;
231     }
232     return( self );
233 }
234
235 + (VLCTreeItem *)rootItem
236 {
237    if (o_root_item == nil)
238         o_root_item = [[VLCTreeItem alloc] initWithName:@"main" ID:0
239             parent:nil children:[[NSMutableArray alloc] initWithCapacity:10]
240             whithCategory: -1];
241    return o_root_item;
242 }
243
244 - (void)dealloc
245 {
246     if (o_children != IsALeafNode) [o_children release];
247     [o_name release];
248     [super dealloc];
249 }
250
251 /* Creates and returns the array of children
252  * Loads children incrementally */
253 - (NSArray *)children
254 {
255     if( o_children == IsALeafNode )
256         return o_children;
257     if( [ o_children count] == 0 )
258     {
259         intf_thread_t   *p_intf = VLCIntf;
260         vlc_list_t      *p_list;
261         module_t        *p_module = NULL;
262         module_config_t *p_item;
263         int             i_index;
264
265         /* List the modules */
266         p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
267         if( !p_list ) return nil;
268
269         if( [[self getName] isEqualToString: @"main"] )
270         {
271             /*
272             * Find the main module
273             */
274             for( i_index = 0; i_index < p_list->i_count; i_index++ )
275             {
276                 p_module = (module_t *)p_list->p_values[i_index].p_object;
277                 if( !strcmp( p_module->psz_object_name, "main" ) )
278                     break;
279             }
280             if( p_module == NULL )
281             {
282                 msg_Err( p_intf,
283                     "could not find the main module in our preferences" );
284                 return nil;
285             }
286             if( i_index < p_list->i_count )
287             {
288                 /* We found the main module */
289                 /* Enumerate config categories and store a reference so we can
290                  * generate their config panel them when it is asked by the user. */
291                 VLCTreeItem *p_last_category = NULL;
292                 p_item = p_module->p_config;
293                 o_children = [[NSMutableArray alloc] initWithCapacity:10];
294                 if( p_item ) do
295                 {
296                     NSString *o_child_name;
297                     switch( p_item->i_type )
298                     {
299                     case CONFIG_CATEGORY:
300                         o_child_name = [[VLCMain sharedInstance]
301     localizedString: config_CategoryNameGet(p_item->i_value ) ];
302                         p_last_category = [VLCTreeItem alloc];
303                         [o_children addObject:[p_last_category
304                             initWithName: o_child_name
305                             ID: p_item->i_value
306                             parent:self
307                             children:[[NSMutableArray alloc]
308                                 initWithCapacity:10]
309                             whithCategory: p_item - p_module->p_config]];
310                         break;
311                     case CONFIG_SUBCATEGORY:
312                         o_child_name = [[VLCMain sharedInstance]
313     localizedString: config_CategoryNameGet(p_item->i_value ) ];
314                         if( p_item->i_value != SUBCAT_VIDEO_GENERAL &&
315                             p_item->i_value != SUBCAT_AUDIO_GENERAL )
316                             [p_last_category->o_children
317                                 addObject:[[VLCTreeItem alloc]
318                                 initWithName: o_child_name
319                                 ID: p_item->i_value
320                                 parent:p_last_category
321                                 children:[[NSMutableArray alloc]
322                                     initWithCapacity:10]
323                                 whithCategory: p_item - p_module->p_config]];
324                         break;
325                     default:
326                         break;
327                     }
328                 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
329             }
330
331             /* Build a tree of the plugins */
332             /* Add the capabilities */
333             for( i_index = 0; i_index < p_list->i_count; i_index++ )
334             {
335                 p_module = (module_t *)p_list->p_values[i_index].p_object;
336
337                 /* Exclude the main module */
338                 if( !strcmp( p_module->psz_object_name, "main" ) )
339                     continue;
340
341                 /* Exclude empty plugins (submodules don't have config */
342                 /* options, they are stored in the parent module) */
343                 if( p_module->b_submodule )
344                     continue;
345                 else
346                     p_item = p_module->p_config;
347
348                 if( !p_item ) continue;
349                 int i_category = -1;
350                 int i_subcategory = -1;
351                 int i_options = 0;
352                 do
353                 {
354                     if( p_item->i_type == CONFIG_CATEGORY )
355                         i_category = p_item->i_value;
356                     else if( p_item->i_type == CONFIG_SUBCATEGORY )
357                         i_subcategory = p_item->i_value;
358
359                     if( p_item->i_type & CONFIG_ITEM )
360                         i_options ++;
361                     if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
362                         break;
363                 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
364                 if( !i_options ) continue;
365
366                 /* Find the right category item */
367
368                 long cookie;
369                 vlc_bool_t b_found = VLC_FALSE;
370                 unsigned int i;
371                 VLCTreeItem* p_category_item, * p_subcategory_item;
372                 for (i = 0 ; i < [o_children count] ; i++)
373                 {
374                     p_category_item = [o_children objectAtIndex: i];
375                     if( p_category_item->i_object_id == i_category )
376                     {
377                         b_found = VLC_TRUE;
378                         break;
379                     }
380                 }
381                 if( !b_found ) continue;
382
383                 /* Find subcategory item */
384                 b_found = VLC_FALSE;
385                 cookie = -1;
386                 for (i = 0 ; i < [p_category_item->o_children count] ; i++)
387                 {
388                     p_subcategory_item = [p_category_item->o_children
389                                             objectAtIndex: i];
390                     if( p_subcategory_item->i_object_id == i_subcategory )
391                     {
392                         b_found = VLC_TRUE;
393                         break;
394                     }
395                 }
396                 if( !b_found )
397                     p_subcategory_item = p_category_item;
398
399                 [p_subcategory_item->o_children addObject:[[VLCTreeItem alloc]
400                     initWithName:[[VLCMain sharedInstance]
401                         localizedString: p_module->psz_object_name ]
402                     ID: p_module->i_object_id
403                     parent:p_subcategory_item
404                     children:IsALeafNode
405                     whithCategory: -1]];
406             }
407         }
408         vlc_list_release( p_list );
409     }
410     return o_children;
411 }
412
413 - (int)getObjectID
414 {
415     return i_object_id;
416 }
417
418 - (NSString *)getName
419 {
420     return o_name;
421 }
422
423 - (VLCTreeItem *)childAtIndex:(int)i_index
424 {
425     return [[self children] objectAtIndex:i_index];
426 }
427
428 - (int)numberOfChildren {
429     id i_tmp = [self children];
430     return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
431 }
432
433 - (BOOL)hasPrefs:(NSString *)o_module_name
434 {
435     intf_thread_t *p_intf = VLCIntf;
436     module_t *p_parser;
437     vlc_list_t *p_list;
438     char *psz_module_name;
439     int i_index;
440
441     psz_module_name = (char *)[o_module_name UTF8String];
442
443     /* look for module */
444     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
445
446     for( i_index = 0; i_index < p_list->i_count; i_index++ )
447     {
448         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
449
450         if( !strcmp( p_parser->psz_object_name, psz_module_name ) )
451         {
452             BOOL b_has_prefs = p_parser->i_config_items != 0;
453             vlc_list_release( p_list );
454             return( b_has_prefs );
455         }
456     }
457
458     vlc_list_release( p_list );
459
460     return( NO );
461 }
462
463 - (NSView *)showView:(NSScrollView *)o_prefs_view
464     advancedView:(vlc_bool_t) b_advanced
465 {
466 fprintf( stderr, "[%s] showView\n", [o_name UTF8String] );
467     NSRect          s_vrc;
468     NSView          *o_view;
469
470     s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
471     o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
472     [o_view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
473
474 /* Create all subviews if it isn't already done because we cannot use 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 fprintf( stderr, "drawing subcategory %s\n", [o_name UTF8String] );
508                     break;
509                 case CONFIG_SECTION:
510 fprintf( stderr, "drawing section %s\n", p_item->psz_text );
511                     break;
512                 case CONFIG_CATEGORY:
513 fprintf( stderr, "drawing category %s\n", [o_name UTF8String] );
514                     break;
515                 case CONFIG_HINT_END:
516 fprintf( stderr, "end of (sub)category\n" );
517                     break;
518                 case CONFIG_HINT_USAGE:
519 fprintf( stderr, "skipping hint usage\n" );
520                     break;
521                 default:
522 fprintf( stderr, "%s (%d)", p_item->psz_name, p_item->i_type );
523                 {
524                     VLCConfigControl *o_control = nil;
525                     o_control = [VLCConfigControl newControl:p_item
526                                                   withView:o_view];
527                     if( o_control != nil )
528                     {
529                         [o_control setAutoresizingMask: NSViewMaxYMargin |
530                             NSViewWidthSizable];
531                         [o_subviews addObject: o_control];
532                     }
533 fprintf( stderr, "\n" );
534                 }
535                     break;
536                 }
537             } while( p_item++->i_type != CONFIG_HINT_END );
538
539             vlc_object_release( p_parser );
540         }
541         else
542         {
543             int i = 0;
544             int i_index;
545             p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
546             if( !p_list ) return o_view;
547
548             /*
549             * Find the main module
550             */
551             for( i_index = 0; i_index < p_list->i_count; i_index++ )
552             {
553                 p_parser = (module_t *)p_list->p_values[i_index].p_object;
554                 if( !strcmp( p_parser->psz_object_name, "main" ) )
555                     break;
556             }
557             if( p_parser == NULL )
558             {
559                 msg_Err( p_intf, "could not find the main module in our "
560                                     "preferences" );
561                 return o_view;
562             }
563             p_item = (p_parser->p_config + i_object_category);
564             if( ( p_item->i_type == CONFIG_CATEGORY ) &&
565               ( ( p_item->i_value == CAT_AUDIO )  ||
566                 ( p_item->i_value == CAT_VIDEO ) ) )
567                 p_item++;
568
569             do
570             {
571                 p_item++;
572                 if( !p_item )
573                 {
574                     msg_Err( p_intf, "null item found" );
575                     break;
576                 }
577                 switch(p_item->i_type)
578                 {
579                 case CONFIG_SUBCATEGORY:
580 fprintf( stderr, "drawing subcategory %s\n", [o_name UTF8String] );
581                     break;
582                 case CONFIG_SECTION:
583 fprintf( stderr, "drawing section %s\n", p_item->psz_text );
584                     break;
585                 case CONFIG_CATEGORY:
586 fprintf( stderr, "drawing category %s\n", [o_name UTF8String] );
587                     break;
588                 case CONFIG_HINT_END:
589 fprintf( stderr, "end of (sub)category\n" );
590                     break;
591                 case CONFIG_HINT_USAGE:
592 fprintf( stderr, "skipping hint usage\n" );
593                     break;
594                 default:
595 fprintf( stderr, "%s (%d)", p_item->psz_name, p_item->i_type );
596                 {
597                     VLCConfigControl *o_control = nil;
598                     o_control = [VLCConfigControl newControl:p_item
599                                                   withView:o_view];
600                     if( o_control != nil )
601                     {
602                         [o_control setAutoresizingMask: NSViewMaxYMargin |
603                                                         NSViewWidthSizable];
604                         [o_subviews addObject: o_control];
605                     }
606                     break;
607                 }
608                 }
609             } while ( ( p_item->i_type != CONFIG_HINT_END ) &&
610                       ( p_item->i_type != CONFIG_SUBCATEGORY ) );
611
612             vlc_object_release( p_parser );
613             vlc_list_release( p_list );
614         }
615     }
616
617     if( o_view != nil )
618     {
619         int i_lastItem = 0;
620         int i_yPos = -2;
621         unsigned int i;
622         for( i = 0 ; i < [o_subviews count] ; i++ )
623         {
624             int i_widget;
625             VLCConfigControl *o_widget = [o_subviews objectAtIndex:i];
626             if( ( [o_widget isAdvanced] ) && (! b_advanced) )
627                 continue;
628
629             i_widget = [o_widget getViewType];
630             i_yPos += [VLCConfigControl calcVerticalMargin:i_widget
631                 lastItem:i_lastItem];
632             [o_widget setYPos:i_yPos];
633             i_yPos += [o_widget frame].size.height;
634             i_lastItem = i_widget;
635             [o_view addSubview:o_widget];
636          }
637
638         [o_prefs_view setDocumentView:o_view];
639     }
640     return o_view;
641 }
642
643 - (void)applyChanges
644 {
645     unsigned int i;
646     if( o_subviews != nil )
647         //Item has been shown
648         for( i = 0 ; i < [o_subviews count] ; i++ )
649             [[o_subviews objectAtIndex:i] applyChanges];
650
651     if( o_children != IsALeafNode )
652         for( i = 0 ; i < [o_children count] ; i++ )
653             [[o_children objectAtIndex:i] applyChanges];
654 }
655
656 @end
657
658
659 @implementation VLCFlippedView
660
661 - (BOOL)isFlipped
662 {
663     return( YES );
664 }
665
666 @end