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