macosx/prefs.m: compile fix
[vlc.git] / modules / gui / macosx / prefs.m
1 /*****************************************************************************
2  * prefs.m: MacOS X module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2003 VideoLAN
5  * $Id: prefs.m,v 1.39 2004/02/02 08:50:41 titer Exp $
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Derk-Jan Hartman <hartman at videolan.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 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include "intf.h"
32 #include "prefs.h"
33 #include "misc.h"
34 #include <vlc_help.h>
35
36 #define ROOT_ID 1241
37 #define GENERAL_ID 1242
38 #define MODULE_ID 1243
39 #define CAPABILITY_ID 1244
40
41 /*****************************************************************************
42  * GetCapabilityHelp: Display the help for one capability.
43  *****************************************************************************/
44 static char * GetCapabilityHelp( char *psz_capability, int i_type)
45 {
46     if( psz_capability == NULL) return "";
47
48     if( !strcasecmp(psz_capability,"access") )
49         return i_type == 1 ? ACCESS_TITLE : ACCESS_HELP;
50     if( !strcasecmp(psz_capability,"audio filter") )
51         return i_type == 1 ? AUDIO_FILTER_TITLE : AUDIO_FILTER_HELP;
52     if( !strcasecmp(psz_capability,"audio output") )
53         return i_type == 1 ? AOUT_TITLE : AOUT_HELP;
54     if( !strcasecmp(psz_capability,"audio encoder") )
55         return i_type == 1 ? AOUT_ENC_TITLE : AOUT_ENC_HELP;
56     if( !strcasecmp(psz_capability,"chroma") )
57         return i_type == 1 ? CHROMA_TITLE : CHROMA_HELP;
58     if( !strcasecmp(psz_capability,"decoder") )
59         return i_type == 1 ? DECODER_TITLE : DECODER_HELP;
60     if( !strcasecmp(psz_capability,"demux") )
61         return i_type == 1 ? DEMUX_TITLE : DEMUX_HELP;
62     if( !strcasecmp(psz_capability,"interface") )
63         return i_type == 1 ? INTERFACE_TITLE : INTERFACE_HELP;
64     if( !strcasecmp(psz_capability,"sout access") )
65         return i_type == 1 ? SOUT_TITLE : SOUT_HELP;
66     if( !strcasecmp(psz_capability,"subtitle demux") )
67         return i_type == 1 ? SUBTITLE_DEMUX_TITLE : SUBTITLE_DEMUX_HELP;
68     if( !strcasecmp(psz_capability,"text renderer") )
69         return i_type == 1 ? TEXT_TITLE : TEXT_HELP;
70     if( !strcasecmp(psz_capability,"video output") )
71         return i_type == 1 ? VOUT__TITLE : VOUT_HELP;
72     if( !strcasecmp(psz_capability,"video filter") )
73         return i_type == 1 ? VIDEO_FILTER_TITLE : VIDEO_FILTER_HELP;
74
75     return " ";
76 }
77
78 /*****************************************************************************
79  * VLCPrefs implementation
80  *****************************************************************************/
81 @implementation VLCPrefs
82
83 - (id)init
84 {
85     self = [super init];
86
87     if( self != nil )
88     {
89         o_empty_view = [[[NSView alloc] init] retain];
90     }
91
92     return( self );
93 }
94
95 - (void)dealloc
96 {
97     [o_empty_view release];
98     [super dealloc];
99 }
100
101 - (void)awakeFromNib
102 {
103     p_intf = [NSApp getIntf];
104     b_advanced = config_GetInt( p_intf, "advanced" );
105
106     [self initStrings];
107     [o_advanced_ckb setState: b_advanced];
108     [o_prefs_view setBorderType: NSGrooveBorder];
109     [o_prefs_view setHasVerticalScroller: YES];
110     [o_prefs_view setDrawsBackground: NO];
111     [o_prefs_view setRulersVisible: NO];
112     [o_prefs_view setDocumentView: o_empty_view];
113     [o_tree selectRow:0 byExtendingSelection:NO];
114     [o_tree expandItem:[o_tree itemAtRow:0]];
115 }
116
117 - (void)initStrings
118 {
119     [o_prefs_window setTitle: _NS("Preferences")];
120     [o_save_btn setTitle: _NS("Save")];
121     [o_cancel_btn setTitle: _NS("Cancel")];
122     [o_reset_btn setTitle: _NS("Reset All")];
123     [o_advanced_ckb setTitle: _NS("Advanced")];
124 }
125
126 - (void)showPrefs
127 {
128     // show first tree item
129     [[o_prefs_view window] center];
130     [[o_prefs_view window] makeKeyAndOrderFront:self];
131 }
132
133 - (IBAction)savePrefs: (id)sender
134 {
135     // walk trough all treeitems and tell them all to save
136     config_SaveConfigFile( p_intf, NULL );
137     [o_prefs_window orderOut:self];
138 }
139
140 - (IBAction)closePrefs: (id)sender
141 {
142     [o_prefs_window orderOut:self];
143 }
144
145 - (IBAction)resetAll: (id)sender
146 {
147     NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"), _NS("Continue"), 
148         nil, o_prefs_window, self, @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
149         _NS("Beware this will reset your VLC media player preferences.\n"
150             "Are you sure you want to continue?") );
151 }
152
153 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return contextInfo:(void *)o_context
154 {
155     if( i_return == NSAlertAlternateReturn )
156     {
157         config_ResetAll( p_intf );
158         // show first config treeitem
159     }
160 }
161
162 - (IBAction)advancedToggle: (id)sender
163 {
164     b_advanced = !b_advanced;
165     [o_advanced_ckb setState: b_advanced];
166     // walk trough all treeitems and set advanced state
167 }
168
169 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
170 {
171     // a tree item will be shown
172 }
173
174 - (void)showViewForID:(int)i_id
175 {
176 }
177
178 @end
179
180 @implementation VLCPrefs (NSTableDataSource)
181
182 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
183     return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
184 }
185
186 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
187     return (item == nil) ? YES : ([item numberOfChildren] >= 0);
188 }
189
190 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
191     return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
192 }
193
194 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
195     return (item == nil) ? @"" : (id)[item name];
196 }
197
198 @end
199
200 @implementation VLCTreeItem
201
202 static VLCTreeItem *o_root_item = nil;
203
204 #define IsALeafNode ((id)-1)
205
206 - (id)initWithID: (int)i_id parent: (VLCTreeItem *)o_parent_item
207 {
208     self = [super init];
209
210     if( self != nil )
211     {
212         i_object_id = i_id;
213         o_parent = o_parent_item;
214     }
215     return( self );
216 }
217
218 + (VLCTreeItem *)rootItem {
219    if (o_root_item == nil) o_root_item = [[VLCTreeItem alloc] initWithID: ROOT_ID parent:nil];
220    return o_root_item;       
221 }
222
223 - (void)dealloc
224 {
225     if( psz_help ) free( psz_help );
226     if( psz_section ) free( psz_section );
227     if (o_name) [o_name release];
228     if (o_children != IsALeafNode) [o_children release];
229     [super dealloc];
230 }
231
232 /* Creates and returns the array of children
233  * Loads children incrementally */
234 - (NSArray *)children
235 {
236     if (o_children == NULL)
237     {
238         intf_thread_t   *p_intf = [NSApp getIntf];
239         vlc_list_t      *p_list = NULL;
240         module_t        *p_module = NULL;
241         module_config_t *p_item = NULL;
242         int             i_index;
243
244         /* List the modules */
245         p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
246         if( !p_list ) return nil;
247
248         if( [self objectID] == ROOT_ID )
249         {
250             /* Create the General Settings and Modules items */
251             o_children = [[NSMutableArray alloc] initWithCapacity:2];
252             o_name = @"root";
253             [o_children addObject:[[VLCTreeItem alloc] initWithID: GENERAL_ID parent:self]];
254             [o_children addObject:[[VLCTreeItem alloc] initWithID: MODULE_ID parent:self]];
255             [o_children retain];
256         }
257         else if( [self objectID] == GENERAL_ID )
258         {
259             /*
260              * Build a tree of the main options
261              */
262             o_name = [_NS("General Settings") copy];
263             psz_help = strdup( GENERAL_HELP );
264             psz_section = strdup( GENERAL_TITLE );
265             
266             for( i_index = 0; i_index < p_list->i_count; i_index++ )
267             {
268                 p_module = (module_t *)p_list->p_values[i_index].p_object;
269                 if( !strcmp( p_module->psz_object_name, "main" ) )
270                     break;
271             }
272             if( p_module == NULL )
273             {
274                 msg_Err( p_intf, "could not find the main module in our preferences" );
275                 return nil;
276             }
277             if( i_index < p_list->i_count )
278             {
279                 /* Enumerate config categories and store a reference so we can
280                  * generate their config panel them when it is asked by the user. */
281                 p_item = p_module->p_config;
282                 o_children = [[NSMutableArray alloc] initWithCapacity:10];
283
284                 if( p_item ) do
285                 {
286                     VLCTreeItem *o_now;
287
288                     switch( p_item->i_type )
289                     {
290                     case CONFIG_HINT_CATEGORY:
291                         o_now = [[VLCTreeItem alloc] initWithID: p_module->i_object_id parent:self];
292                         [o_now setName: [[NSApp localizedString: p_item->psz_text] retain]];
293                         [o_children addObject: o_now];
294                         break;
295                     }
296                 }
297                 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
298                 [o_children retain];
299                 //[o_children sortUsingSelector:@selector(caseInsensitiveCompare:)];
300             }
301         }
302         else if( [self objectID] == MODULE_ID )
303         {
304             int i_counter;
305             int i_total;
306             BOOL b_found;
307
308             /* Build a list of the capabilities */
309             o_name = [_NS("Modules") copy];
310             psz_help = strdup( PLUGIN_HELP );
311             psz_section = strdup( PLUGIN_TITLE );
312             
313             o_children = [[NSMutableArray alloc] initWithCapacity:10];
314             for( i_index = 0; i_index < p_list->i_count; i_index++ )
315             {
316                 p_module = (module_t *)p_list->p_values[i_index].p_object;
317                 
318                 /* Exclude the main module */
319                 if( !strcmp( p_module->psz_object_name, "main" ) )
320                     continue;
321
322                 /* Exclude empty modules */
323                 if( p_module->b_submodule )
324                     p_item = ((module_t *)p_module->p_parent)->p_config;
325                 else
326                     p_item = p_module->p_config;
327                 
328                 if( !p_item ) continue;
329                 do
330                 {
331                     if( p_item->i_type & CONFIG_ITEM )
332                         break;
333                 }
334                 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
335                 if( p_item->i_type == CONFIG_HINT_END ) continue;
336                 
337                 i_total = [o_children count];
338                 b_found = FALSE;
339                 
340                 for( i_counter = 0; i_counter < i_total; i_counter++ )
341                 {
342                     if( [[[o_children objectAtIndex: i_counter] name] isEqualToString:
343                         [NSApp localizedString: p_module->psz_capability]] )
344                     {
345                         b_found = TRUE;
346                         break;
347                     }
348                 }
349                 
350                 if( !b_found )
351                 {
352                     VLCTreeItem *o_now = [[VLCTreeItem alloc] initWithID: CAPABILITY_ID parent:self];
353                     [o_now setName: [NSApp localizedString: p_module->psz_capability]];
354                     [o_children addObject:o_now];
355                 }
356             }
357             [o_children retain];
358             //[o_children sortUsingSelector:@selector(caseInsensitiveCompare:)];
359         }
360         else if( [self objectID] == CAPABILITY_ID )
361         {
362             /* add the modules */
363             o_children = [[NSMutableArray alloc] initWithCapacity:3];
364             for( i_index = 0; i_index < p_list->i_count; i_index++ )
365             {
366                 p_module = (module_t *)p_list->p_values[i_index].p_object;
367                 
368                 /* Exclude the main module */
369                 if( !strcmp( p_module->psz_object_name, "main" ) )
370                     continue;
371
372                 /* Exclude empty modules */
373                 if( p_module->b_submodule )
374                     p_item = ((module_t *)p_module->p_parent)->p_config;
375                 else
376                     p_item = p_module->p_config;
377                 
378                 if( !p_item ) continue;
379                 do
380                 {
381                     if( p_item->i_type & CONFIG_ITEM )
382                         break;
383                 }
384                 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
385                 if( p_item->i_type == CONFIG_HINT_END ) continue;
386
387                 if( [[self name] isEqualToString:
388                     [NSApp localizedString: p_module->psz_capability]] )
389                 {
390                     VLCTreeItem *o_now = [[VLCTreeItem alloc] initWithID: p_module->i_object_id parent:self];
391                     psz_help = strdup(GetCapabilityHelp( p_module->psz_capability, 1));
392                     psz_section = strdup(GetCapabilityHelp( p_module->psz_capability, 2));
393                     [o_now setName: [[NSApp localizedString:p_module->psz_object_name] retain]];
394                     [o_children addObject:o_now];
395                 }
396             }
397             [o_children retain];
398         }
399         else
400         {
401             /* all the other stuff are leafs */
402             o_children = IsALeafNode;
403         }
404     }
405     return o_children;
406 }
407
408 - (int)objectID
409 {
410     return i_object_id;
411 }
412
413 - (NSString *)name
414 {
415     if( o_name ) return o_name;
416 }
417
418 - (void)setName:(NSString *)a_name;
419 {
420     if( o_name ) [o_name release];
421     o_name = [a_name copy];
422 }
423
424 - (VLCTreeItem *)childAtIndex:(int)i_index
425 {
426     return [[self children] objectAtIndex:i_index];
427 }
428
429 - (int)numberOfChildren
430 {
431     id i_tmp = [self children];
432     return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
433 }
434
435 @end