]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/preferences.cpp
b68916f475a2b14cd92ff17d018191590291150a
[vlc] / modules / gui / wxwindows / preferences.cpp
1 /*****************************************************************************
2  * preferences.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: preferences.cpp,v 1.38 2003/10/20 12:25:22 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/intf.h>
34
35 #include <vlc_help.h>
36
37 #include "wxwindows.h"
38 #include "preferences_widgets.h"
39 #include <wx/notebook.h>
40 #include <wx/textctrl.h>
41 #include <wx/combobox.h>
42 #include <wx/spinctrl.h>
43 #include <wx/statline.h>
44 #include <wx/treectrl.h>
45 #include <wx/clntdata.h>
46 #include <wx/dynarray.h>
47
48 #ifndef wxRB_SINGLE
49 #   define wxRB_SINGLE 0
50 #endif
51
52 #define GENERAL_ID 1242
53 #define PLUGIN_ID 1243
54 #define CAPABILITY_ID 1244
55
56 /*****************************************************************************
57  * Classes declarations.
58  *****************************************************************************/
59 class ConfigTreeData;
60 class PrefsTreeCtrl : public wxTreeCtrl
61 {
62 public:
63
64     PrefsTreeCtrl() { }
65     PrefsTreeCtrl( wxWindow *parent, intf_thread_t *_p_intf,
66                    PrefsDialog *p_prefs_dialog, wxBoxSizer *_p_sizer );
67     virtual ~PrefsTreeCtrl();
68
69     void ApplyChanges();
70     void CleanChanges();
71
72 private:
73     /* Event handlers (these functions should _not_ be virtual) */
74     void OnSelectTreeItem( wxTreeEvent& event );
75     void OnAdvanced( wxCommandEvent& event );
76
77     ConfigTreeData *FindModuleConfig( ConfigTreeData *config_data );
78
79     DECLARE_EVENT_TABLE()
80
81     intf_thread_t *p_intf;
82     PrefsDialog *p_prefs_dialog;
83     wxBoxSizer *p_sizer;
84     wxWindow *p_parent;
85     vlc_bool_t b_advanced;
86
87     wxTreeItemId root_item;
88     wxTreeItemId general_item;
89     wxTreeItemId plugins_item;
90 };
91
92 WX_DEFINE_ARRAY(ConfigControl *, ArrayOfConfigControls);
93
94 class PrefsPanel : public wxPanel
95 {
96 public:
97
98     PrefsPanel() { }
99     PrefsPanel( wxWindow *parent, intf_thread_t *_p_intf,
100                 PrefsDialog *, int i_object_id, char *, char * );
101     virtual ~PrefsPanel() {}
102
103     void ApplyChanges();
104     void SwitchAdvanced( vlc_bool_t );
105
106 private:
107     intf_thread_t *p_intf;
108     PrefsDialog *p_prefs_dialog;
109
110     vlc_bool_t b_advanced;
111
112     wxBoxSizer *config_sizer;
113     wxScrolledWindow *config_window;
114
115     ArrayOfConfigControls config_array;
116 };
117
118 class ConfigTreeData : public wxTreeItemData
119 {
120 public:
121
122     ConfigTreeData() { b_submodule = 0; panel = NULL; psz_section = NULL; }
123     virtual ~ConfigTreeData() { if( panel ) delete panel; }
124
125     vlc_bool_t b_submodule;
126
127     char *psz_help;
128
129     PrefsPanel *panel;
130     wxBoxSizer *sizer;
131     int i_object_id;
132     char *psz_section;
133 };
134
135 /*****************************************************************************
136  * Event Table.
137  *****************************************************************************/
138
139 /* IDs for the controls and the menu commands */
140 enum
141 {
142     Notebook_Event = wxID_HIGHEST,
143     MRL_Event,
144     ResetAll_Event,
145     Advanced_Event,
146 };
147
148 BEGIN_EVENT_TABLE(PrefsDialog, wxFrame)
149     /* Button events */
150     EVT_BUTTON(wxID_OK, PrefsDialog::OnOk)
151     EVT_BUTTON(wxID_CANCEL, PrefsDialog::OnCancel)
152     EVT_BUTTON(wxID_SAVE, PrefsDialog::OnSave)
153     EVT_BUTTON(ResetAll_Event, PrefsDialog::OnResetAll)
154     EVT_CHECKBOX(Advanced_Event, PrefsDialog::OnAdvanced)
155
156     /* Don't destroy the window when the user closes it */
157     EVT_CLOSE(PrefsDialog::OnCancel)
158 END_EVENT_TABLE()
159
160 // menu and control ids
161 enum
162 {
163     PrefsTree_Ctrl = wxID_HIGHEST
164 };
165
166 BEGIN_EVENT_TABLE(PrefsTreeCtrl, wxTreeCtrl)
167     EVT_TREE_SEL_CHANGED(PrefsTree_Ctrl, PrefsTreeCtrl::OnSelectTreeItem)
168     EVT_COMMAND(Advanced_Event, wxEVT_USER_FIRST, PrefsTreeCtrl::OnAdvanced)
169 END_EVENT_TABLE()
170
171 /*****************************************************************************
172  * Constructor.
173  *****************************************************************************/
174 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf, wxWindow *p_parent)
175   :  wxFrame( p_parent, -1, wxU(_("Preferences")), wxDefaultPosition,
176               wxSize(700,450), wxDEFAULT_FRAME_STYLE )
177 {
178     /* Initializations */
179     p_intf = _p_intf;
180     SetIcon( *p_intf->p_sys->p_icon );
181
182     /* Create a panel to put everything in */
183     wxPanel *panel = new wxPanel( this, -1 );
184     panel->SetAutoLayout( TRUE );
185
186     /* Create the preferences tree control */
187     wxBoxSizer *controls_sizer = new wxBoxSizer( wxHORIZONTAL );
188     prefs_tree =
189         new PrefsTreeCtrl( panel, p_intf, this, controls_sizer );
190
191     /* Separation */
192     wxStaticLine *static_line = new wxStaticLine( panel, wxID_OK );
193
194     /* Create the buttons */
195     wxButton *ok_button = new wxButton( panel, wxID_OK, wxU(_("OK")) );
196     ok_button->SetDefault();
197     wxButton *cancel_button = new wxButton( panel, wxID_CANCEL,
198                                             wxU(_("Cancel")) );
199     wxButton *save_button = new wxButton( panel, wxID_SAVE, wxU(_("Save")) );
200     wxButton *reset_button = new wxButton( panel, ResetAll_Event,
201                                            wxU(_("Reset All")) );
202
203     wxPanel *dummy_panel = new wxPanel( this, -1 );
204     wxCheckBox *advanced_checkbox =
205         new wxCheckBox( panel, Advanced_Event, wxU(_("Advanced options")) );
206
207     if( config_GetInt( p_intf, "advanced" ) )
208     {
209         advanced_checkbox->SetValue(TRUE);
210         wxCommandEvent dummy_event;
211         dummy_event.SetInt(TRUE);
212         OnAdvanced( dummy_event );
213     }
214
215     /* Place everything in sizers */
216     wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
217     button_sizer->Add( ok_button, 0, wxALL, 5 );
218     button_sizer->Add( cancel_button, 0, wxALL, 5 );
219     button_sizer->Add( save_button, 0, wxALL, 5 );
220     button_sizer->Add( reset_button, 0, wxALL, 5 );
221     button_sizer->Add( dummy_panel, 1, wxALL, 5 );
222     button_sizer->Add( advanced_checkbox, 0, wxALL | wxALIGN_RIGHT |
223                        wxALIGN_CENTER_VERTICAL, 0 );
224     button_sizer->Layout();
225
226     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
227     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
228     panel_sizer->Add( controls_sizer, 1, wxEXPAND | wxALL, 5 );
229     panel_sizer->Add( static_line, 0, wxEXPAND | wxALL, 5 );
230     panel_sizer->Add( button_sizer, 0, wxALIGN_LEFT | wxALIGN_BOTTOM |
231                       wxALL | wxEXPAND, 5 );
232     panel_sizer->Layout();
233     panel->SetSizer( panel_sizer );
234     main_sizer->Add( panel, 1, wxEXPAND, 0 );
235     main_sizer->Layout();
236     SetSizer( main_sizer );
237 }
238
239 PrefsDialog::~PrefsDialog()
240 {
241 }
242
243 /*****************************************************************************
244  * Private methods.
245  *****************************************************************************/
246
247
248 /*****************************************************************************
249  * Events methods.
250  *****************************************************************************/
251 void PrefsDialog::OnOk( wxCommandEvent& WXUNUSED(event) )
252 {
253     prefs_tree->ApplyChanges();
254     this->Hide();
255     prefs_tree->CleanChanges();
256 }
257
258 void PrefsDialog::OnCancel( wxCommandEvent& WXUNUSED(event) )
259 {
260     this->Hide();
261     prefs_tree->CleanChanges();
262 }
263
264 void PrefsDialog::OnSave( wxCommandEvent& WXUNUSED(event) )
265 {
266     prefs_tree->ApplyChanges();
267     config_SaveConfigFile( p_intf, NULL );
268     this->Hide();
269 }
270
271 void PrefsDialog::OnResetAll( wxCommandEvent& WXUNUSED(event) )
272 {
273     wxMessageDialog dlg( this,
274         wxU(_("Beware this will reset your VLC Media Player config file.\n"
275               "Are you sure you want to continue?")),
276         wxU(_("Reset config file")), wxYES_NO|wxNO_DEFAULT|wxCENTRE );
277
278     if ( dlg.ShowModal() == wxID_YES )
279     {
280         /* TODO: need to reset all the controls */
281         config_ResetAll( p_intf );
282         prefs_tree->CleanChanges();
283         config_SaveConfigFile( p_intf, NULL );
284     }
285 }
286
287 void PrefsDialog::OnAdvanced( wxCommandEvent& event )
288 {
289     wxCommandEvent newevent( wxEVT_USER_FIRST, Advanced_Event );
290     newevent.SetInt( event.GetInt() );
291
292     prefs_tree->AddPendingEvent( newevent );
293 }
294
295 /*****************************************************************************
296  * GetCapabilityHelp: Display the help for one capability.
297  *****************************************************************************/
298 static char * GetCapabilityHelp( char *psz_capability, int i_type)
299 {
300     if( psz_capability == NULL) return "";
301
302     if( !strcasecmp(psz_capability,"access") )
303         return i_type == 1 ? ACCESS_TITLE : ACCESS_HELP;
304     if( !strcasecmp(psz_capability,"audio filter") )
305         return i_type == 1 ? AUDIO_FILTER_TITLE : AUDIO_FILTER_HELP;
306     if( !strcasecmp(psz_capability,"audio output") )
307         return i_type == 1 ? AOUT_TITLE : AOUT_HELP;
308     if( !strcasecmp(psz_capability,"chroma") )
309         return i_type == 1 ? CHROMA_TITLE : CHROMA_HELP;
310     if( !strcasecmp(psz_capability,"decoder") )
311         return i_type == 1 ? DECODER_TITLE : DECODER_HELP;
312     if( !strcasecmp(psz_capability,"demux") )
313         return i_type == 1 ? DEMUX_TITLE : DEMUX_HELP;
314     if( !strcasecmp(psz_capability,"interface") )
315         return i_type == 1 ? INTERFACE_TITLE : INTERFACE_HELP;
316     if( !strcasecmp(psz_capability,"sout access") )
317         return i_type == 1 ? SOUT_TITLE : SOUT_HELP;
318     if( !strcasecmp(psz_capability,"subtitle demux") )
319         return i_type == 1 ? SUBTITLE_DEMUX_TITLE : SUBTITLE_DEMUX_HELP;
320     if( !strcasecmp(psz_capability,"text renderer") )
321         return i_type == 1 ? TEXT_TITLE : TEXT_HELP;
322     if( !strcasecmp(psz_capability,"video output") )
323         return i_type == 1 ? VOUT__TITLE : VOUT_HELP;
324     if( !strcasecmp(psz_capability,"video filter") )
325         return i_type == 1 ? VIDEO_FILTER_TITLE : VIDEO_FILTER_HELP;
326
327     return "";
328 }
329
330 /*****************************************************************************
331  * PrefsTreeCtrl class definition.
332  *****************************************************************************/
333 PrefsTreeCtrl::PrefsTreeCtrl( wxWindow *_p_parent, intf_thread_t *_p_intf,
334                               PrefsDialog *_p_prefs_dialog,
335                               wxBoxSizer *_p_sizer )
336   : wxTreeCtrl( _p_parent, PrefsTree_Ctrl, wxDefaultPosition, wxDefaultSize,
337                 wxTR_NO_LINES | wxTR_FULL_ROW_HIGHLIGHT |
338                 wxTR_LINES_AT_ROOT | wxTR_HIDE_ROOT |
339                 wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS | wxSUNKEN_BORDER )
340 {
341     vlc_list_t      *p_list;
342     module_t        *p_module;
343     module_config_t *p_item;
344     int i_index;
345
346     /* Initializations */
347     p_intf = _p_intf;
348     p_prefs_dialog = _p_prefs_dialog;
349     p_sizer = _p_sizer;
350     p_parent = _p_parent;
351     b_advanced = VLC_FALSE;
352
353     root_item = AddRoot( wxT("") );
354
355     /* List the plugins */
356     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
357     if( !p_list ) return;
358
359     /*
360      * Build a tree of the main options
361      */
362     ConfigTreeData *config_data = new ConfigTreeData;
363     config_data->psz_section = NULL;
364     config_data->i_object_id = GENERAL_ID;
365     config_data->psz_help = wraptext( GENERAL_HELP, WRAPCOUNT, ISUTF8 );
366     config_data->psz_section = strdup( GENERAL_TITLE );
367     general_item = AppendItem( root_item, wxU(_("General Settings")),
368                                 -1, -1, config_data );
369
370     for( i_index = 0; i_index < p_list->i_count; i_index++ )
371     {
372         p_module = (module_t *)p_list->p_values[i_index].p_object;
373         if( !strcmp( p_module->psz_object_name, "main" ) )
374             break;
375     }
376     if( i_index < p_list->i_count )
377     {
378         /* We found the main module */
379
380         /* Enumerate config categories and store a reference so we can
381          * generate their config panel them when it is asked by the user. */
382         p_item = p_module->p_config;
383
384         if( p_item ) do
385         {
386             switch( p_item->i_type )
387             {
388             case CONFIG_HINT_CATEGORY:
389                 ConfigTreeData *config_data = new ConfigTreeData;
390                 config_data->psz_section = strdup(p_item->psz_text);
391                 if( p_item->psz_longtext )
392                 {
393                     config_data->psz_help =
394                         wraptext( p_item->psz_longtext, WRAPCOUNT, ISUTF8 );
395                 }
396                 else
397                 {
398                     config_data->psz_help = NULL;
399                 }
400                 config_data->i_object_id = p_module->i_object_id;
401
402                 /* Add the category to the tree */
403                 AppendItem( general_item, wxU(p_item->psz_text),
404                             -1, -1, config_data );
405                 break;
406             }
407         }
408         while( p_item->i_type != CONFIG_HINT_END && p_item++ );
409
410         SortChildren( general_item );
411     }
412
413
414     /*
415      * Build a tree of all the plugins
416      */
417     config_data = new ConfigTreeData;
418     config_data->psz_section = NULL;
419     config_data->i_object_id = PLUGIN_ID;
420     config_data->psz_help = wraptext( PLUGIN_HELP, WRAPCOUNT, ISUTF8 );
421     config_data->psz_section = strdup( PLUGIN_TITLE );
422     plugins_item = AppendItem( root_item, wxU(_("Plugins")),
423                         -1,-1,config_data );
424
425     for( i_index = 0; i_index < p_list->i_count; i_index++ )
426     {
427         p_module = (module_t *)p_list->p_values[i_index].p_object;
428
429         /* Exclude the main module */
430         if( !strcmp( p_module->psz_object_name, "main" ) )
431             continue;
432
433         /* Exclude empty plugins (submodules don't have config options, they
434          * are stored in the parent module) */
435         if( p_module->b_submodule )
436             p_item = ((module_t *)p_module->p_parent)->p_config;
437         else
438             p_item = p_module->p_config;
439
440         if( !p_item ) continue;
441         do
442         {
443             if( p_item->i_type & CONFIG_ITEM )
444                 break;
445         }
446         while( p_item->i_type != CONFIG_HINT_END && p_item++ );
447         if( p_item->i_type == CONFIG_HINT_END ) continue;
448
449         /* Find the capability child item */
450         long cookie; size_t i_child_index;
451         wxTreeItemId capability_item = GetFirstChild( plugins_item, cookie);
452         for( i_child_index = 0;
453              i_child_index < GetChildrenCount( plugins_item, FALSE );
454              i_child_index++ )
455         {
456             if( !GetItemText(capability_item).Cmp(
457                     wxU(p_module->psz_capability ) ) )
458             {
459                 break;
460             }
461             capability_item = GetNextChild( plugins_item, cookie );
462         }
463
464         if( i_child_index == GetChildrenCount( plugins_item, FALSE ) &&
465             p_module->psz_capability && *p_module->psz_capability )
466         {
467             /* We didn't find it, add it */
468             ConfigTreeData *config_data = new ConfigTreeData;
469             config_data->psz_section =
470                 wraptext( GetCapabilityHelp( p_module->psz_capability , 1 ),
471                           WRAPCOUNT, ISUTF8 );
472             config_data->psz_help =
473                 wraptext( GetCapabilityHelp( p_module->psz_capability , 2 ),
474                           WRAPCOUNT, ISUTF8 );
475             config_data->i_object_id = CAPABILITY_ID;
476             capability_item = AppendItem( plugins_item,
477                                           wxU(p_module->psz_capability),
478                                           -1,-1,config_data );
479         }
480
481         /* Add the plugin to the tree */
482         ConfigTreeData *config_data = new ConfigTreeData;
483         config_data->b_submodule = p_module->b_submodule;
484         config_data->i_object_id = p_module->b_submodule ?
485             ((module_t *)p_module->p_parent)->i_object_id :
486             p_module->i_object_id;
487         config_data->psz_help = NULL;
488         AppendItem( capability_item, wxU(p_module->psz_object_name), -1, -1,
489                     config_data );
490     }
491
492     /* Sort all this mess */
493     long cookie; size_t i_child_index;
494     SortChildren( plugins_item );
495     wxTreeItemId capability_item = GetFirstChild( plugins_item, cookie);
496     for( i_child_index = 0;
497          i_child_index < GetChildrenCount( plugins_item, FALSE );
498          i_child_index++ )
499     {
500         capability_item = GetNextChild( plugins_item, cookie );
501         SortChildren( capability_item );
502     }
503
504     /* Clean-up everything */
505     vlc_list_release( p_list );
506
507     p_sizer->Add( this, 1, wxEXPAND | wxALL, 0 );
508     p_sizer->Layout();
509
510     /* Update Tree Ctrl */
511 #ifndef WIN32 /* Workaround a bug in win32 implementation */
512     SelectItem( GetFirstChild( root_item, cookie ) );
513 #endif
514
515     Expand( general_item );
516 }
517
518 PrefsTreeCtrl::~PrefsTreeCtrl()
519 {
520 }
521
522 void PrefsTreeCtrl::ApplyChanges()
523 {
524     long cookie, cookie2;
525     ConfigTreeData *config_data;
526
527     /* Apply changes to the main module */
528     wxTreeItemId item = GetFirstChild( general_item, cookie );
529     for( size_t i_child_index = 0;
530          i_child_index < GetChildrenCount( general_item, FALSE );
531          i_child_index++ )
532     {
533         config_data = (ConfigTreeData *)GetItemData( item );
534         if( config_data && config_data->panel )
535         {
536             config_data->panel->ApplyChanges();
537         }
538
539         item = GetNextChild( general_item, cookie );
540     }
541
542     /* Apply changes to the plugins */
543     item = GetFirstChild( plugins_item, cookie );
544     for( size_t i_child_index = 0;
545          i_child_index < GetChildrenCount( plugins_item, FALSE );
546          i_child_index++ )
547     {
548         wxTreeItemId item2 = GetFirstChild( item, cookie2 );
549         for( size_t i_child_index = 0;
550              i_child_index < GetChildrenCount( item, FALSE );
551              i_child_index++ )
552         {
553             config_data = (ConfigTreeData *)GetItemData( item2 );
554             if( config_data && config_data->panel )
555             {
556                 config_data->panel->ApplyChanges();
557             }
558
559             item2 = GetNextChild( item, cookie2 );
560         }
561
562         item = GetNextChild( plugins_item, cookie );
563     }
564 }
565
566 void PrefsTreeCtrl::CleanChanges()
567 {
568     long cookie, cookie2;
569     ConfigTreeData *config_data;
570
571     config_data = !GetSelection() ? NULL :
572         FindModuleConfig( (ConfigTreeData *)GetItemData( GetSelection() ) );
573     if( config_data  )
574     {
575         config_data->panel->Hide();
576         p_sizer->Remove( config_data->panel );
577     }
578
579     /* Clean changes for the main module */
580     wxTreeItemId item = GetFirstChild( general_item, cookie );
581     for( size_t i_child_index = 0;
582          i_child_index < GetChildrenCount( general_item, FALSE );
583          i_child_index++ )
584     {
585         config_data = (ConfigTreeData *)GetItemData( item );
586         if( config_data && config_data->panel )
587         {
588             delete config_data->panel;
589             config_data->panel = NULL;
590         }
591
592         item = GetNextChild( general_item, cookie );
593     }
594
595     /* Clean changes for the plugins */
596     item = GetFirstChild( plugins_item, cookie );
597     for( size_t i_child_index = 0;
598          i_child_index < GetChildrenCount( plugins_item, FALSE );
599          i_child_index++ )
600     {
601         wxTreeItemId item2 = GetFirstChild( item, cookie2 );
602         for( size_t i_child_index = 0;
603              i_child_index < GetChildrenCount( item, FALSE );
604              i_child_index++ )
605         {
606             config_data = (ConfigTreeData *)GetItemData( item2 );
607
608             if( config_data && config_data->panel )
609             {
610                 delete config_data->panel;
611                 config_data->panel = NULL;
612             }
613
614             item2 = GetNextChild( item, cookie2 );
615         }
616
617         item = GetNextChild( plugins_item, cookie );
618     }
619
620     if( GetSelection() )
621     {
622         wxTreeEvent event;
623         OnSelectTreeItem( event );
624     }
625 }
626
627 ConfigTreeData *PrefsTreeCtrl::FindModuleConfig( ConfigTreeData *config_data )
628 {
629     /* We need this complexity because submodules don't have their own config
630      * options. They use the parent module ones. */
631
632     if( !config_data || !config_data->b_submodule )
633     {
634         return config_data;
635     }
636
637     long cookie, cookie2;
638     ConfigTreeData *config_new;
639     wxTreeItemId item = GetFirstChild( plugins_item, cookie );
640     for( size_t i_child_index = 0;
641          i_child_index < GetChildrenCount( plugins_item, FALSE );
642          i_child_index++ )
643     {
644         wxTreeItemId item2 = GetFirstChild( item, cookie2 );
645         for( size_t i_child_index = 0;
646              i_child_index < GetChildrenCount( item, FALSE );
647              i_child_index++ )
648         {
649             config_new = (ConfigTreeData *)GetItemData( item2 );
650             if( config_new && !config_new->b_submodule &&
651                 config_new->i_object_id == config_data->i_object_id )
652             {
653                 return config_new;
654             }
655
656             item2 = GetNextChild( item, cookie2 );
657         }
658
659         item = GetNextChild( plugins_item, cookie );
660     }
661
662     /* Found nothing */
663     return NULL;
664 }
665
666 void PrefsTreeCtrl::OnSelectTreeItem( wxTreeEvent& event )
667 {
668     ConfigTreeData *config_data;
669
670     config_data = FindModuleConfig( (ConfigTreeData *)GetItemData(
671                                     event.GetOldItem() ) );
672     if( config_data && config_data->panel )
673     {
674         config_data->panel->Hide();
675         p_sizer->Remove( config_data->panel );
676     }
677
678     /* Don't use event.GetItem() because we also send fake events */
679     config_data = FindModuleConfig( (ConfigTreeData *)GetItemData(
680                                     GetSelection() ) );
681     if( config_data )
682     {
683         if( !config_data->panel )
684         {
685             /* The panel hasn't been created yet. Let's do it. */
686             config_data->panel =
687                 new PrefsPanel( p_parent, p_intf, p_prefs_dialog,
688                                 config_data->i_object_id,
689                                 config_data->psz_section,
690                                 config_data->psz_help );
691             config_data->panel->SwitchAdvanced( b_advanced );
692         }
693         else
694         {
695             config_data->panel->SwitchAdvanced( b_advanced );
696             config_data->panel->Show();
697         }
698
699         p_sizer->Add( config_data->panel, 3, wxEXPAND | wxALL, 0 );
700         p_sizer->Layout();
701     }
702 }
703
704 void PrefsTreeCtrl::OnAdvanced( wxCommandEvent& event )
705 {
706     b_advanced = event.GetInt();
707
708     ConfigTreeData *config_data = !GetSelection() ? NULL :
709         FindModuleConfig( (ConfigTreeData *)GetItemData( GetSelection() ) );
710     if( config_data  )
711     {
712         config_data->panel->Hide();
713         p_sizer->Remove( config_data->panel );
714     }
715
716     if( GetSelection() )
717     {
718         wxTreeEvent event;
719         OnSelectTreeItem( event );
720     }
721 }
722
723 /*****************************************************************************
724  * PrefsPanel class definition.
725  *****************************************************************************/
726 PrefsPanel::PrefsPanel( wxWindow* parent, intf_thread_t *_p_intf,
727                         PrefsDialog *_p_prefs_dialog,
728                         int i_object_id, char *psz_section, char *psz_help )
729   :  wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize )
730 {
731     module_config_t *p_item;
732
733     wxStaticText *label;
734     wxStaticText *help;
735     wxArrayString array;
736
737     module_t *p_module = NULL;
738     
739     /* Initializations */
740     p_intf = _p_intf;
741     p_prefs_dialog =_p_prefs_dialog,
742
743     b_advanced = VLC_TRUE;
744     SetAutoLayout( TRUE );
745
746     wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
747
748
749     if( i_object_id == PLUGIN_ID || i_object_id == GENERAL_ID ||
750         i_object_id == CAPABILITY_ID )
751     {
752         label = new wxStaticText( this, -1,wxU(_( psz_section )));
753         wxFont heading_font = label->GetFont();
754         heading_font.SetPointSize( heading_font.GetPointSize() + 5 );
755         label->SetFont( heading_font );
756         sizer->Add( label, 0, wxEXPAND | wxLEFT, 10 );
757         sizer->Add( new wxStaticLine( this, 0 ), 0,
758                     wxEXPAND | wxLEFT | wxRIGHT, 2 );
759
760         help = new wxStaticText( this, -1, wxU(_( psz_help ) ) );
761         sizer->Add( help ,0 ,wxEXPAND | wxALL, 5 );
762
763         config_sizer = NULL; config_window = NULL;
764     }
765     else
766     {
767         /* Get a pointer to the module */
768         p_module = (module_t *)vlc_object_get( p_intf,  i_object_id );
769         if( p_module->i_object_type != VLC_OBJECT_MODULE )
770         {
771             /* 0OOoo something went really bad */
772             return;
773         }
774
775         /* Enumerate config options and add corresponding config boxes
776          * (submodules don't have config options, they are stored in the
777          *  parent module) */
778         if( p_module->b_submodule )
779             p_item = ((module_t *)p_module->p_parent)->p_config;
780         else
781             p_item = p_module->p_config;
782
783         /* Find the category if it has been specified */
784         if( psz_section && p_item->i_type == CONFIG_HINT_CATEGORY )
785         {
786             while( !p_item->i_type == CONFIG_HINT_CATEGORY ||
787                    strcmp( psz_section, p_item->psz_text ) )
788             {
789                 if( p_item->i_type == CONFIG_HINT_END )
790                     break;
791                 p_item++;
792             }
793         }
794
795         /* Add a head title to the panel */
796         label = new wxStaticText( this, -1,
797                                   wxU(_(psz_section ? p_item->psz_text :
798                                   p_module->psz_longname )));
799         wxFont heading_font = label->GetFont();
800         heading_font.SetPointSize( heading_font.GetPointSize() + 5 );
801         label->SetFont( heading_font );
802         sizer->Add( label, 0, wxEXPAND | wxLEFT, 10 );
803         sizer->Add( new wxStaticLine( this, 0 ), 0,
804                     wxEXPAND | wxLEFT | wxRIGHT, 2 );
805
806         /* Now put all the config options into a scrolled window */
807         config_sizer = new wxBoxSizer( wxVERTICAL );
808         config_window = new wxScrolledWindow( this, -1, wxDefaultPosition,
809             wxDefaultSize, wxSTATIC_BORDER | wxHSCROLL | wxVSCROLL );
810         config_window->SetAutoLayout( TRUE );
811         config_window->SetScrollRate( 5, 5 );
812
813         if( p_item ) do
814         {
815             /* If a category has been specified, check we finished the job */
816             if( psz_section && p_item->i_type == CONFIG_HINT_CATEGORY &&
817                 strcmp( psz_section, p_item->psz_text ) )
818                 break;
819
820             ConfigControl *control =
821                 CreateConfigControl( VLC_OBJECT(p_intf),
822                                      p_item, config_window );
823
824             /* Don't add items that were not recognized */
825             if( control == NULL ) continue;
826
827             /* Add the config data to our array so we can keep a trace of it */
828             config_array.Add( control );
829
830             config_sizer->Add( control, 0, wxEXPAND | wxALL, 2 );
831         }
832         while( p_item->i_type != CONFIG_HINT_END && p_item++ );
833
834         config_sizer->Layout();
835         config_window->SetSizer( config_sizer );
836         sizer->Add( config_window, 1, wxEXPAND | wxALL, 5 );
837
838         /* And at last put a useful help string if available */
839         if( psz_help && *psz_help )
840         {
841             sizer->Add( new wxStaticLine( this, 0 ), 0,
842                         wxEXPAND | wxLEFT | wxRIGHT, 2 );
843             help = new wxStaticText( this, -1, wxU(_(psz_help)),
844                                      wxDefaultPosition, wxDefaultSize,
845                                      wxALIGN_LEFT,
846                                      wxT("") );
847             sizer->Add( help ,0 ,wxEXPAND | wxALL, 5 );
848         }
849
850     } 
851     sizer->Layout();
852     SetSizer( sizer );
853 }
854
855 void PrefsPanel::ApplyChanges()
856 {
857     for( size_t i = 0; i < config_array.GetCount(); i++ )
858     {
859         ConfigControl *control = config_array.Item(i);
860
861         switch( control->GetType() )
862         {
863         case CONFIG_ITEM_STRING:
864         case CONFIG_ITEM_FILE:
865         case CONFIG_ITEM_DIRECTORY:
866         case CONFIG_ITEM_MODULE:
867             config_PutPsz( p_intf, control->GetName().mb_str(),
868                            control->GetPszValue().mb_str() );
869             break;
870         case CONFIG_ITEM_INTEGER:
871         case CONFIG_ITEM_KEY:
872         case CONFIG_ITEM_BOOL:
873             config_PutInt( p_intf, control->GetName().mb_str(),
874                            control->GetIntValue() );
875             break;
876         case CONFIG_ITEM_FLOAT:
877             config_PutFloat( p_intf, control->GetName().mb_str(),
878                              control->GetFloatValue() );
879             break;
880         }
881     }
882 }
883
884 void PrefsPanel::SwitchAdvanced( vlc_bool_t b_new_advanced )
885 {
886     if( b_advanced == b_new_advanced ) return;
887
888     if( config_sizer && config_window )
889     {
890         b_advanced = b_new_advanced;
891
892         for( size_t i = 0; i < config_array.GetCount(); i++ )
893         {
894             ConfigControl *control = config_array.Item(i);
895             if( control->IsAdvanced() )
896             {
897                 control->Show( b_advanced );
898                 config_sizer->Show( control, b_advanced );
899             }
900         }
901
902         config_sizer->Layout();
903         config_window->FitInside();
904         config_window->Refresh();
905     }
906     return;     
907 }