]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/preferences.cpp
Fix a bug with preferences
[vlc] / modules / gui / wxwindows / preferences.cpp
1 /*****************************************************************************
2  * preferences.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id$
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_config_cat.h>
36
37 #include "wxwindows.h"
38 #include "preferences_widgets.h"
39
40 #include <wx/combobox.h>
41 #include <wx/statline.h>
42 #include <wx/clntdata.h>
43 #include <wx/dynarray.h>
44
45 #ifndef wxRB_SINGLE
46 #   define wxRB_SINGLE 0
47 #endif
48
49 #define TYPE_CATEGORY 0
50 #define TYPE_CATSUBCAT 1  /* Category with embedded subcategory */
51 #define TYPE_SUBCATEGORY 2
52 #define TYPE_MODULE 3
53
54 /*****************************************************************************
55  * Classes declarations.
56  *****************************************************************************/
57 class ConfigTreeData;
58 class PrefsTreeCtrl : public wxTreeCtrl
59 {
60 public:
61
62     PrefsTreeCtrl() { }
63     PrefsTreeCtrl( wxWindow *parent, intf_thread_t *_p_intf,
64                    PrefsDialog *p_prefs_dialog, wxBoxSizer *_p_sizer );
65     virtual ~PrefsTreeCtrl();
66
67     void ApplyChanges();
68     void CleanChanges();
69
70 private:
71     /* Event handlers (these functions should _not_ be virtual) */
72     void OnSelectTreeItem( wxTreeEvent& event );
73     void OnAdvanced( wxCommandEvent& event );
74
75     ConfigTreeData *FindModuleConfig( ConfigTreeData *config_data );
76
77     DECLARE_EVENT_TABLE()
78
79     intf_thread_t *p_intf;
80     PrefsDialog *p_prefs_dialog;
81     wxBoxSizer *p_sizer;
82     wxWindow *p_parent;
83     vlc_bool_t b_advanced;
84
85     wxTreeItemId root_item;
86     wxTreeItemId plugins_item;
87 };
88
89 WX_DEFINE_ARRAY(ConfigControl *, ArrayOfConfigControls);
90
91 class PrefsPanel : public wxPanel
92 {
93 public:
94
95     PrefsPanel() { }
96     PrefsPanel( wxWindow *parent, intf_thread_t *_p_intf,
97                 PrefsDialog *, ConfigTreeData* );
98     virtual ~PrefsPanel() {}
99
100     void ApplyChanges();
101     void SwitchAdvanced( vlc_bool_t );
102
103 private:
104     intf_thread_t *p_intf;
105     PrefsDialog *p_prefs_dialog;
106
107     vlc_bool_t b_advanced;
108
109     wxStaticText *hidden_text;
110     wxBoxSizer *config_sizer;
111     wxScrolledWindow *config_window;
112
113     ArrayOfConfigControls config_array;
114 };
115
116 class ConfigTreeData : public wxTreeItemData
117 {
118 public:
119
120     ConfigTreeData() { b_submodule = 0; panel = NULL; psz_name = NULL;
121                        psz_help = NULL; }
122     virtual ~ConfigTreeData() {
123                                  if( panel ) delete panel;
124                                  if( psz_name ) free( psz_name );
125                                  if( psz_help ) free( psz_help );
126                               };
127
128     vlc_bool_t b_submodule;
129
130     PrefsPanel *panel;
131     wxBoxSizer *sizer;
132
133     int i_object_id;
134     int i_subcat_id;
135     int i_type;
136     char *psz_name;
137     char *psz_help;
138 };
139
140 /*****************************************************************************
141  * Event Table.
142  *****************************************************************************/
143
144 /* IDs for the controls and the menu commands */
145 enum
146 {
147     Notebook_Event = wxID_HIGHEST,
148     MRL_Event,
149     ResetAll_Event,
150     Advanced_Event,
151 };
152
153 BEGIN_EVENT_TABLE(PrefsDialog, wxFrame)
154     /* Button events */
155     EVT_BUTTON(wxID_OK, PrefsDialog::OnOk)
156     EVT_BUTTON(wxID_CANCEL, PrefsDialog::OnCancel)
157     EVT_BUTTON(wxID_SAVE, PrefsDialog::OnSave)
158     EVT_BUTTON(ResetAll_Event, PrefsDialog::OnResetAll)
159     EVT_CHECKBOX(Advanced_Event, PrefsDialog::OnAdvanced)
160
161     /* Don't destroy the window when the user closes it */
162     EVT_CLOSE(PrefsDialog::OnCancel)
163 END_EVENT_TABLE()
164
165 // menu and control ids
166 enum
167 {
168     PrefsTree_Ctrl = wxID_HIGHEST
169 };
170
171 BEGIN_EVENT_TABLE(PrefsTreeCtrl, wxTreeCtrl)
172     EVT_TREE_SEL_CHANGED(PrefsTree_Ctrl, PrefsTreeCtrl::OnSelectTreeItem)
173     EVT_COMMAND(Advanced_Event, wxEVT_USER_FIRST, PrefsTreeCtrl::OnAdvanced)
174 END_EVENT_TABLE()
175
176 /*****************************************************************************
177  * Constructor.
178  *****************************************************************************/
179 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf, wxWindow *p_parent)
180   :  wxFrame( p_parent, -1, wxU(_("Preferences")), wxDefaultPosition,
181               wxSize(700,450), wxDEFAULT_FRAME_STYLE )
182 {
183     /* Initializations */
184     p_intf = _p_intf;
185     SetIcon( *p_intf->p_sys->p_icon );
186
187     /* Create a panel to put everything in */
188     wxPanel *panel = new wxPanel( this, -1 );
189     panel->SetAutoLayout( TRUE );
190
191     /* Create the preferences tree control */
192     wxBoxSizer *controls_sizer = new wxBoxSizer( wxHORIZONTAL );
193     prefs_tree =
194         new PrefsTreeCtrl( panel, p_intf, this, controls_sizer );
195
196     /* Separation */
197     wxStaticLine *static_line = new wxStaticLine( panel, wxID_OK );
198
199     /* Create the buttons */
200     wxButton *ok_button = new wxButton( panel, wxID_OK, wxU(_("OK")) );
201     ok_button->SetDefault();
202     wxButton *cancel_button = new wxButton( panel, wxID_CANCEL,
203                                             wxU(_("Cancel")) );
204     wxButton *save_button = new wxButton( panel, wxID_SAVE, wxU(_("Save")) );
205     wxButton *reset_button = new wxButton( panel, ResetAll_Event,
206                                            wxU(_("Reset All")) );
207
208     wxPanel *dummy_panel = new wxPanel( this, -1 );
209     wxCheckBox *advanced_checkbox =
210         new wxCheckBox( panel, Advanced_Event, wxU(_("Advanced options")) );
211
212     if( config_GetInt( p_intf, "advanced" ) )
213     {
214         advanced_checkbox->SetValue(TRUE);
215         wxCommandEvent dummy_event;
216         dummy_event.SetInt(TRUE);
217         OnAdvanced( dummy_event );
218     }
219
220     /* Place everything in sizers */
221     wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
222     button_sizer->Add( ok_button, 0, wxALL, 5 );
223     button_sizer->Add( cancel_button, 0, wxALL, 5 );
224     button_sizer->Add( save_button, 0, wxALL, 5 );
225     button_sizer->Add( reset_button, 0, wxALL, 5 );
226     button_sizer->Add( dummy_panel, 1, wxALL, 5 );
227     button_sizer->Add( advanced_checkbox, 0, wxALL | wxALIGN_RIGHT |
228                        wxALIGN_CENTER_VERTICAL, 0 );
229     button_sizer->Layout();
230
231     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
232     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
233     panel_sizer->Add( controls_sizer, 1, wxEXPAND | wxALL, 5 );
234     panel_sizer->Add( static_line, 0, wxEXPAND | wxALL, 5 );
235     panel_sizer->Add( button_sizer, 0, wxALIGN_LEFT | wxALIGN_BOTTOM |
236                       wxALL | wxEXPAND, 5 );
237     panel_sizer->Layout();
238     panel->SetSizer( panel_sizer );
239     main_sizer->Add( panel, 1, wxEXPAND, 0 );
240     main_sizer->Layout();
241     SetSizer( main_sizer );
242 }
243
244 PrefsDialog::~PrefsDialog()
245 {
246 }
247
248 /*****************************************************************************
249  * Private methods.
250  *****************************************************************************/
251
252
253 /*****************************************************************************
254  * Events methods.
255  *****************************************************************************/
256 void PrefsDialog::OnOk( wxCommandEvent& WXUNUSED(event) )
257 {
258     prefs_tree->ApplyChanges();
259     this->Hide();
260     prefs_tree->CleanChanges();
261 }
262
263 void PrefsDialog::OnCancel( wxCommandEvent& WXUNUSED(event) )
264 {
265     this->Hide();
266     prefs_tree->CleanChanges();
267 }
268
269 void PrefsDialog::OnSave( wxCommandEvent& WXUNUSED(event) )
270 {
271     prefs_tree->ApplyChanges();
272     config_SaveConfigFile( p_intf, NULL );
273     this->Hide();
274 }
275
276 void PrefsDialog::OnResetAll( wxCommandEvent& WXUNUSED(event) )
277 {
278     wxMessageDialog dlg( this,
279         wxU(_("Beware this will reset your VLC media player preferences.\n"
280               "Are you sure you want to continue?")),
281         wxU(_("Reset Preferences")), wxYES_NO|wxNO_DEFAULT|wxCENTRE );
282
283     if ( dlg.ShowModal() == wxID_YES )
284     {
285         /* TODO: need to reset all the controls */
286         config_ResetAll( p_intf );
287         prefs_tree->CleanChanges();
288         config_SaveConfigFile( p_intf, NULL );
289     }
290 }
291
292 void PrefsDialog::OnAdvanced( wxCommandEvent& event )
293 {
294     wxCommandEvent newevent( wxEVT_USER_FIRST, Advanced_Event );
295     newevent.SetInt( event.GetInt() );
296
297     prefs_tree->AddPendingEvent( newevent );
298 }
299
300 /*****************************************************************************
301  * PrefsTreeCtrl class definition.
302  *****************************************************************************/
303 PrefsTreeCtrl::PrefsTreeCtrl( wxWindow *_p_parent, intf_thread_t *_p_intf,
304                               PrefsDialog *_p_prefs_dialog,
305                               wxBoxSizer *_p_sizer )
306   : wxTreeCtrl( _p_parent, PrefsTree_Ctrl, wxDefaultPosition, wxSize(200,-1),
307                 wxTR_NO_LINES | wxTR_FULL_ROW_HIGHLIGHT |
308                 wxTR_LINES_AT_ROOT | wxTR_HIDE_ROOT |
309                 wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS | wxSUNKEN_BORDER )
310 {
311     vlc_list_t      *p_list = NULL;;
312     module_t        *p_module;
313     module_config_t *p_item;
314     int i_index;
315
316     /* Initializations */
317     p_intf = _p_intf;
318     p_prefs_dialog = _p_prefs_dialog;
319     p_sizer = _p_sizer;
320     p_parent = _p_parent;
321     b_advanced = VLC_FALSE;
322
323     root_item = AddRoot( wxT("") );
324
325     /* List the plugins */
326     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
327     if( !p_list ) return;
328
329     /* Build the categories list */
330     for( i_index = 0; i_index < p_list->i_count; i_index++ )
331     {
332         p_module = (module_t *)p_list->p_values[i_index].p_object;
333         if( !strcmp( p_module->psz_object_name, "main" ) )
334             break;
335     }
336     if( i_index < p_list->i_count )
337     {
338         wxTreeItemId current_item;
339         char *psz_help;
340         /* We found the main module */
341
342         /* Enumerate config categories and store a reference so we can
343          * generate their config panel them when it is asked by the user. */
344         p_item = p_module->p_config;
345
346         if( p_item ) do
347         {
348             ConfigTreeData *config_data;
349             switch( p_item->i_type )
350             {
351             case CONFIG_CATEGORY:
352                 config_data = new ConfigTreeData;
353                 config_data->psz_name = strdup( config_CategoryNameGet(
354                                                             p_item->i_value ) );
355                 psz_help = config_CategoryHelpGet( p_item->i_value );
356                 if( psz_help )
357                 {
358                     config_data->psz_help = wraptext( strdup( psz_help ),
359                                                       72 , ISUTF8 );
360                 }
361                 else
362                 {
363                     config_data->psz_help = NULL;
364                 }
365                 config_data->i_type = TYPE_CATEGORY;
366                 config_data->i_object_id = p_item->i_value;
367
368                 /* Add the category to the tree */
369                 current_item = AppendItem( root_item,
370                                            wxU( config_data->psz_name ),
371                                            -1, -1, config_data );
372                 break;
373             case CONFIG_SUBCATEGORY:
374                 if( p_item->i_value == SUBCAT_VIDEO_GENERAL ||
375                     p_item->i_value == SUBCAT_AUDIO_GENERAL )
376                 {
377                     ConfigTreeData *cd = (ConfigTreeData *)
378                                             GetItemData( current_item );
379                     cd->i_type = TYPE_CATSUBCAT;
380                     cd->i_subcat_id = p_item->i_value;
381                     if( cd->psz_name ) free( cd->psz_name );
382                     cd->psz_name = strdup(  config_CategoryNameGet(
383                                                       p_item->i_value ) );
384                     if( cd->psz_help ) free( cd->psz_help );
385                     char *psz_help = config_CategoryHelpGet( p_item->i_value );
386                     if( psz_help )
387                     {
388                         cd->psz_help = wraptext( strdup( psz_help ),72 ,
389                                                  ISUTF8 );
390                     }
391                     else
392                     {
393                         cd->psz_help = NULL;
394                     }
395                     continue;
396                 }
397
398                 config_data = new ConfigTreeData;
399
400                 config_data->psz_name = strdup(  config_CategoryNameGet(
401                                                            p_item->i_value ) );
402                 psz_help = config_CategoryHelpGet( p_item->i_value );
403                 if( psz_help )
404                 {
405                     config_data->psz_help = wraptext( strdup( psz_help ) ,
406                                                       72 , ISUTF8 );
407                 }
408                 else
409                 {
410                     config_data->psz_help = NULL;
411                 }
412                 config_data->i_type = TYPE_SUBCATEGORY;
413                 config_data->i_object_id = p_item->i_value;
414                 AppendItem( current_item, wxU( config_data->psz_name ),
415                             -1, -1, config_data );
416                 break;
417             }
418         }
419         while( p_item->i_type != CONFIG_HINT_END && p_item++ );
420
421     }
422
423
424     /*
425      * Build a tree of all the plugins
426      */
427     for( i_index = 0; i_index < p_list->i_count; i_index++ )
428     {
429         int i_category = -1;
430         int i_subcategory = -1;
431         int i_options = 0;
432
433         p_module = (module_t *)p_list->p_values[i_index].p_object;
434
435         /* Exclude the main module */
436         if( !strcmp( p_module->psz_object_name, "main" ) )
437             continue;
438
439         /* Exclude empty plugins (submodules don't have config options, they
440          * are stored in the parent module) */
441         if( p_module->b_submodule )
442               continue;
443 //            p_item = ((module_t *)p_module->p_parent)->p_config;
444         else
445             p_item = p_module->p_config;
446
447
448         if( !p_item ) continue;
449         do
450         {
451             if( p_item->i_type == CONFIG_CATEGORY )
452             {
453                 i_category = p_item->i_value;
454             }
455             else if( p_item->i_type == CONFIG_SUBCATEGORY )
456             {
457                 i_subcategory = p_item->i_value;
458             }
459             if( p_item->i_type & CONFIG_ITEM )
460                 i_options ++;
461             if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
462             {
463                 break;
464             }
465         }
466         while( p_item->i_type != CONFIG_HINT_END && p_item++ );
467
468         if( !i_options ) continue;
469
470         /* Find the right category item */
471         long cookie;
472         vlc_bool_t b_found = VLC_FALSE;
473         wxTreeItemId category_item = GetFirstChild( root_item , cookie);
474         while( category_item.IsOk() )
475         {
476             ConfigTreeData *config_data =
477                     (ConfigTreeData *)GetItemData( category_item );
478             if( config_data->i_object_id == i_category )
479             {
480                 b_found = VLC_TRUE;
481                 break;
482             }
483             category_item = GetNextChild( root_item, cookie );
484         }
485
486         if( !b_found ) continue;
487
488         /* Find subcategory item */
489         b_found = VLC_FALSE;
490         cookie = -1;
491         wxTreeItemId subcategory_item = GetFirstChild( category_item, cookie );
492         while( subcategory_item.IsOk() )
493         {
494             ConfigTreeData *config_data =
495                     (ConfigTreeData *)GetItemData( subcategory_item );
496             if( config_data->i_object_id == i_subcategory )
497             {
498                 b_found = VLC_TRUE;
499                 break;
500             }
501             subcategory_item = GetNextChild( category_item, cookie );
502         }
503         if( !b_found )
504         {
505             subcategory_item = category_item;
506         }
507
508         /* Add the plugin to the tree */
509         ConfigTreeData *config_data = new ConfigTreeData;
510         config_data->b_submodule = p_module->b_submodule;
511         config_data->i_type = TYPE_MODULE;
512         config_data->i_object_id = p_module->b_submodule ?
513             ((module_t *)p_module->p_parent)->i_object_id :
514             p_module->i_object_id;
515         config_data->psz_help = NULL;
516
517         AppendItem( subcategory_item, wxU( p_module->psz_shortname ?
518                        p_module->psz_shortname : p_module->psz_object_name )
519                                     , -1, -1,
520                     config_data );
521     }
522
523     /* Sort all this mess */
524     long cookie; size_t i_child_index;
525     wxTreeItemId capability_item = GetFirstChild( root_item, cookie);
526     for( i_child_index = 0;
527          i_child_index < GetChildrenCount( plugins_item, FALSE );
528          i_child_index++ )
529     {
530         SortChildren( capability_item );
531         capability_item = GetNextChild( plugins_item, cookie );
532     }
533
534     /* Clean-up everything */
535     vlc_list_release( p_list );
536
537     p_sizer->Add( this, 1, wxEXPAND | wxALL, 0 );
538     p_sizer->Layout();
539
540     /* Update Tree Ctrl */
541 #ifndef WIN32 /* Workaround a bug in win32 implementation */
542     SelectItem( GetFirstChild( root_item, cookie ) );
543 #endif
544
545     Expand( root_item );
546 }
547
548 PrefsTreeCtrl::~PrefsTreeCtrl()
549 {
550 }
551
552 void PrefsTreeCtrl::ApplyChanges()
553 {
554     long cookie, cookie2, cookie3;
555     ConfigTreeData *config_data;
556
557     wxTreeItemId category = GetFirstChild( root_item, cookie );
558     while( category.IsOk() )
559     {
560         wxTreeItemId subcategory = GetFirstChild( category, cookie2 );
561         while( subcategory.IsOk() )
562         {
563             wxTreeItemId module = GetFirstChild( subcategory, cookie3 );
564             while( module.IsOk() )
565             {
566                 config_data = (ConfigTreeData *)GetItemData( module );
567                 if( config_data && config_data->panel )
568                 {
569                     config_data->panel->ApplyChanges();
570                 }
571                 module = GetNextChild( subcategory, cookie3 );
572             }
573             config_data = (ConfigTreeData *)GetItemData( subcategory );
574             if( config_data && config_data->panel )
575             {
576                 config_data->panel->ApplyChanges();
577             }
578             subcategory = GetNextChild( category, cookie2 );
579         }
580         config_data = (ConfigTreeData *)GetItemData( category );
581         if( config_data && config_data->panel )
582         {
583             config_data->panel->ApplyChanges();
584         }
585         category = GetNextChild( root_item, cookie );
586     }
587 }
588
589 void PrefsTreeCtrl::CleanChanges()
590 {
591     long cookie, cookie2, cookie3;
592     ConfigTreeData *config_data;
593
594     config_data = !GetSelection() ? NULL :
595         FindModuleConfig( (ConfigTreeData *)GetItemData( GetSelection() ) );
596     if( config_data  )
597     {
598         config_data->panel->Hide();
599         p_sizer->Remove( config_data->panel );
600     }
601
602     wxTreeItemId category = GetFirstChild( root_item, cookie );
603     while( category.IsOk() )
604     {
605         wxTreeItemId subcategory = GetFirstChild( category, cookie2 );
606         while( subcategory.IsOk() )
607         {
608             wxTreeItemId module = GetFirstChild( subcategory, cookie3 );
609             while( module.IsOk() )
610             {
611                 config_data = (ConfigTreeData *)GetItemData( module );
612                 if( config_data && config_data->panel )
613                 {
614                     delete config_data->panel;
615                     config_data->panel = NULL;
616                 }
617                 module = GetNextChild( subcategory, cookie3 );
618             }
619             config_data = (ConfigTreeData *)GetItemData( subcategory );
620             if( config_data && config_data->panel )
621             {
622                 delete config_data->panel;
623                 config_data->panel = NULL;
624             }
625             subcategory = GetNextChild( category, cookie2 );
626         }
627         config_data = (ConfigTreeData *)GetItemData( category );
628         if( config_data && config_data->panel )
629         {
630             delete config_data->panel;
631             config_data->panel = NULL;
632         }
633         category = GetNextChild( root_item, cookie );
634     }
635
636     if( GetSelection() )
637     {
638         wxTreeEvent event;
639         OnSelectTreeItem( event );
640     }
641 }
642
643 ConfigTreeData *PrefsTreeCtrl::FindModuleConfig( ConfigTreeData *config_data )
644 {
645     /* We need this complexity because submodules don't have their own config
646      * options. They use the parent module ones. */
647
648     if( !config_data || !config_data->b_submodule )
649     {
650         return config_data;
651     }
652
653     long cookie, cookie2, cookie3;
654     ConfigTreeData *config_new;
655     wxTreeItemId category = GetFirstChild( root_item, cookie );
656     while( category.IsOk() )
657     {
658         wxTreeItemId subcategory = GetFirstChild( category, cookie2 );
659         while( subcategory.IsOk() )
660         {
661             wxTreeItemId module = GetFirstChild( subcategory, cookie3 );
662             while( module.IsOk() )
663             {
664                 config_new = (ConfigTreeData *)GetItemData( module );
665                 if( config_new && !config_new->b_submodule &&
666                     config_new->i_object_id == config_data->i_object_id )
667                 {
668                     return config_new;
669                 }
670                 module = GetNextChild( subcategory, cookie3 );
671             }
672             subcategory = GetNextChild( category, cookie2 );
673         }
674         category = GetNextChild( root_item, cookie );
675     }
676
677     /* Found nothing */
678     return NULL;
679 }
680
681 void PrefsTreeCtrl::OnSelectTreeItem( wxTreeEvent& event )
682 {
683     ConfigTreeData *config_data = NULL;
684
685     if( event.GetOldItem() )
686         config_data = FindModuleConfig( (ConfigTreeData *)GetItemData(
687                                         event.GetOldItem() ) );
688     if( config_data && config_data->panel )
689     {
690         config_data->panel->Hide();
691         p_sizer->Remove( config_data->panel );
692     }
693
694     /* Don't use event.GetItem() because we also send fake events */
695     config_data = FindModuleConfig( (ConfigTreeData *)GetItemData(
696                                     GetSelection() ) );
697     if( config_data )
698     {
699         if( !config_data->panel )
700         {
701             /* The panel hasn't been created yet. Let's do it. */
702             config_data->panel =
703                 new PrefsPanel( p_parent, p_intf, p_prefs_dialog,
704                                 config_data );
705             config_data->panel->SwitchAdvanced( b_advanced );
706         }
707         else
708         {
709             config_data->panel->SwitchAdvanced( b_advanced );
710             config_data->panel->Show();
711         }
712
713         p_sizer->Add( config_data->panel, 3, wxEXPAND | wxALL, 0 );
714         p_sizer->Layout();
715     }
716 }
717
718 void PrefsTreeCtrl::OnAdvanced( wxCommandEvent& event )
719 {
720     b_advanced = event.GetInt();
721
722     ConfigTreeData *config_data = !GetSelection() ? NULL :
723         FindModuleConfig( (ConfigTreeData *)GetItemData( GetSelection() ) );
724     if( config_data  )
725     {
726         config_data->panel->Hide();
727         p_sizer->Remove( config_data->panel );
728     }
729
730     if( GetSelection() )
731     {
732         wxTreeEvent event;
733         OnSelectTreeItem( event );
734     }
735 }
736
737 /*****************************************************************************
738  * PrefsPanel class definition.
739  *****************************************************************************/
740 PrefsPanel::PrefsPanel( wxWindow* parent, intf_thread_t *_p_intf,
741                         PrefsDialog *_p_prefs_dialog,
742                         ConfigTreeData *config_data )
743   :  wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize )
744 {
745     module_config_t *p_item;
746     vlc_list_t *p_list = NULL;;
747
748     wxStaticText *label;
749     wxStaticText *help;
750     wxArrayString array;
751
752     module_t *p_module = NULL;
753
754     /* Initializations */
755     p_intf = _p_intf;
756     p_prefs_dialog =_p_prefs_dialog,
757
758     b_advanced = VLC_TRUE;
759     SetAutoLayout( TRUE );
760     Hide();
761
762     wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
763
764
765     if( config_data->i_type == TYPE_CATEGORY )
766     {
767         label = new wxStaticText( this, -1,wxU(_( config_data->psz_name )));
768         wxFont heading_font = label->GetFont();
769         heading_font.SetPointSize( heading_font.GetPointSize() + 5 );
770         label->SetFont( heading_font );
771         sizer->Add( label, 0, wxEXPAND | wxLEFT, 10 );
772         sizer->Add( new wxStaticLine( this, 0 ), 0,
773                     wxEXPAND | wxLEFT | wxRIGHT, 2 );
774
775         hidden_text = NULL;
776         help = new wxStaticText( this, -1, wxU(_( config_data->psz_help ) ) );
777         sizer->Add( help ,0 ,wxEXPAND | wxALL, 5 );
778
779         config_sizer = NULL; config_window = NULL;
780     }
781     else
782     {
783         /* Get a pointer to the module */
784         if( config_data->i_type == TYPE_MODULE )
785         {
786             p_module = (module_t *)vlc_object_get( p_intf,
787                                                    config_data->i_object_id );
788         }
789         else
790         {
791             /* List the plugins */
792             int i_index;
793             vlc_bool_t b_found = VLC_FALSE;
794             p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
795             if( !p_list ) return;
796
797             for( i_index = 0; i_index < p_list->i_count; i_index++ )
798             {
799                 p_module = (module_t *)p_list->p_values[i_index].p_object;
800                 if( !strcmp( p_module->psz_object_name, "main" ) )
801                 {
802                     b_found = VLC_TRUE;
803                     break;
804                 }
805             }
806             if( !p_module && !b_found )
807             {
808                 msg_Warn( p_intf, "ohoh, unable to find main module" );
809                 return;
810             }
811         }
812
813         if( p_module->i_object_type != VLC_OBJECT_MODULE )
814         {
815             /* 0OOoo something went really bad */
816             return;
817         }
818
819         /* Enumerate config options and add corresponding config boxes
820          * (submodules don't have config options, they are stored in the
821          *  parent module) */
822         if( p_module->b_submodule )
823             p_item = ((module_t *)p_module->p_parent)->p_config;
824         else
825             p_item = p_module->p_config;
826
827         /* Find the category if it has been specified */
828         if( config_data->i_type == TYPE_SUBCATEGORY ||
829             config_data->i_type == TYPE_CATSUBCAT )
830         {
831             do
832             {
833                 if( p_item->i_type == CONFIG_SUBCATEGORY &&
834                     ( config_data->i_type == TYPE_SUBCATEGORY &&
835                       p_item->i_value == config_data->i_object_id ) ||
836                     ( config_data->i_type == TYPE_CATSUBCAT &&
837                       p_item->i_value == config_data->i_subcat_id ) )
838                 {
839                     break;
840                 }
841                 if( p_item->i_type == CONFIG_HINT_END )
842                     break;
843             } while( p_item++ );
844         }
845
846         /* Add a head title to the panel */
847         char *psz_head;
848         if( config_data->i_type == TYPE_SUBCATEGORY ||
849             config_data->i_type == TYPE_CATSUBCAT )
850         {
851             psz_head = config_data->psz_name;
852             p_item++;
853         }
854         else
855         {
856             psz_head = p_module->psz_longname;
857         }
858
859         label = new wxStaticText( this, -1,
860                       wxU(_( psz_head ? psz_head : _("Unknown") ) ) );
861         wxFont heading_font = label->GetFont();
862         heading_font.SetPointSize( heading_font.GetPointSize() + 5 );
863         label->SetFont( heading_font );
864         sizer->Add( label, 0, wxEXPAND | wxLEFT, 10 );
865         sizer->Add( new wxStaticLine( this, 0 ), 0,
866                     wxEXPAND | wxLEFT | wxRIGHT, 2 );
867
868         /* Now put all the config options into a scrolled window */
869         config_sizer = new wxBoxSizer( wxVERTICAL );
870         config_window = new wxScrolledWindow( this, -1, wxDefaultPosition,
871             wxDefaultSize, wxBORDER_NONE | wxHSCROLL | wxVSCROLL );
872         config_window->SetAutoLayout( TRUE );
873         config_window->SetScrollRate( 5, 5 );
874
875         if( p_item ) do
876         {
877             /* If a category has been specified, check we finished the job */
878             if( ( ( config_data->i_type == TYPE_SUBCATEGORY &&
879                     p_item->i_value != config_data->i_object_id ) ||
880                   ( config_data->i_type == TYPE_CATSUBCAT  &&
881                     p_item->i_value != config_data->i_subcat_id ) ) &&
882                 (p_item->i_type == CONFIG_CATEGORY ||
883                   p_item->i_type == CONFIG_SUBCATEGORY ) )
884                 break;
885
886             ConfigControl *control =
887                 CreateConfigControl( VLC_OBJECT(p_intf),
888                                      p_item, config_window );
889
890             /* Don't add items that were not recognized */
891             if( control == NULL ) continue;
892
893             /* Add the config data to our array so we can keep a trace of it */
894             config_array.Add( control );
895
896             config_sizer->Add( control, 0, wxEXPAND | wxALL, 2 );
897         }
898         while( !( p_item->i_type == CONFIG_HINT_END ||
899                ( ( config_data->i_type == TYPE_SUBCATEGORY ||
900                    config_data->i_type == TYPE_CATSUBCAT ) &&
901                  ( p_item->i_type == CONFIG_CATEGORY ||
902                    p_item->i_type == CONFIG_SUBCATEGORY ) ) ) && p_item++ );
903
904
905         config_sizer->Layout();
906         config_window->SetSizer( config_sizer );
907         sizer->Add( config_window, 1, wxEXPAND | wxALL, 5 );
908         hidden_text = new wxStaticText( this, -1,
909                         wxU( _( "Some options are available but hidden. " \
910                                 "Check \"Advanced options\" to see them." ) ) );
911         sizer->Add( hidden_text );
912
913         /* And at last put a useful help string if available */
914         if( config_data->psz_help && *config_data->psz_help )
915         {
916             sizer->Add( new wxStaticLine( this, 0 ), 0,
917                         wxEXPAND | wxLEFT | wxRIGHT, 2 );
918             help = new wxStaticText( this, -1, wxU(_(config_data->psz_help)),
919                                      wxDefaultPosition, wxDefaultSize,
920                                      wxALIGN_LEFT,
921                                      wxT("") );
922             sizer->Add( help ,0 ,wxEXPAND | wxALL, 5 );
923         }
924
925         if( config_data->i_type == TYPE_MODULE )
926         {
927             vlc_object_release( p_module );
928         }
929         else
930         {
931             vlc_list_release( p_list );
932         }
933     }
934     sizer->Layout();
935     SetSizer( sizer );
936     Show();
937 }
938
939 void PrefsPanel::ApplyChanges()
940 {
941     vlc_value_t val;
942
943     for( size_t i = 0; i < config_array.GetCount(); i++ )
944     {
945         ConfigControl *control = config_array.Item(i);
946
947         switch( control->GetType() )
948         {
949         case CONFIG_ITEM_STRING:
950         case CONFIG_ITEM_FILE:
951         case CONFIG_ITEM_DIRECTORY:
952         case CONFIG_ITEM_MODULE:
953         case CONFIG_ITEM_MODULE_LIST:
954         case CONFIG_ITEM_MODULE_LIST_CAT:
955             config_PutPsz( p_intf, control->GetName().mb_str(),
956                            control->GetPszValue().mb_str() );
957             break;
958         case CONFIG_ITEM_KEY:
959             /* So you don't need to restart to have the changes take effect */
960             val.i_int = control->GetIntValue();
961             var_Set( p_intf->p_vlc, control->GetName().mb_str(), val );
962         case CONFIG_ITEM_INTEGER:
963         case CONFIG_ITEM_BOOL:
964             config_PutInt( p_intf, control->GetName().mb_str(),
965                            control->GetIntValue() );
966             break;
967         case CONFIG_ITEM_FLOAT:
968             config_PutFloat( p_intf, control->GetName().mb_str(),
969                              control->GetFloatValue() );
970             break;
971         }
972     }
973 }
974
975 void PrefsPanel::SwitchAdvanced( vlc_bool_t b_new_advanced )
976 {
977     bool hidden = false;
978
979     if( b_advanced == b_new_advanced ) 
980     {
981         goto hide;
982     }
983
984     if( config_sizer && config_window )
985     {
986         b_advanced = b_new_advanced;
987
988         for( size_t i = 0; i < config_array.GetCount(); i++ )
989         {
990             ConfigControl *control = config_array.Item(i);
991             if( control->IsAdvanced() )
992             {
993                 if( !b_advanced ) hidden = true;
994                 control->Show( b_advanced );
995                 config_sizer->Show( control, b_advanced );
996             }
997         }
998
999         config_sizer->Layout();
1000         config_window->FitInside();
1001         config_window->Refresh();
1002     }
1003 hide:
1004     if( hidden && hidden_text )
1005     {
1006         hidden_text->Show( true );
1007         config_sizer->Show( hidden_text, true );
1008     }
1009     else if ( hidden_text )
1010     {
1011         hidden_text->Show( false );
1012         config_sizer->Show( hidden_text, false );
1013     }
1014     return;
1015 }