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