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