]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/preferences.cpp
de970f98a6e8aff51659d95694b1d31fa416a54e
[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.48 2004/02/26 00:23:04 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/intf.h>
34
35 #include <vlc_help.h>
36
37 #include "wxwindows.h"
38 #include "preferences_widgets.h"
39
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         SortChildren( capability_item );
463         capability_item = GetNextChild( plugins_item, cookie );
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 = NULL;
631
632     if( event.GetOldItem() )
633         config_data = FindModuleConfig( (ConfigTreeData *)GetItemData(
634                                         event.GetOldItem() ) );
635     if( config_data && config_data->panel )
636     {
637         config_data->panel->Hide();
638         p_sizer->Remove( config_data->panel );
639     }
640
641     /* Don't use event.GetItem() because we also send fake events */
642     config_data = FindModuleConfig( (ConfigTreeData *)GetItemData(
643                                     GetSelection() ) );
644     if( config_data )
645     {
646         if( !config_data->panel )
647         {
648             /* The panel hasn't been created yet. Let's do it. */
649             config_data->panel =
650                 new PrefsPanel( p_parent, p_intf, p_prefs_dialog,
651                                 config_data->i_object_id,
652                                 config_data->psz_section,
653                                 config_data->psz_help );
654             config_data->panel->SwitchAdvanced( b_advanced );
655         }
656         else
657         {
658             config_data->panel->SwitchAdvanced( b_advanced );
659             config_data->panel->Show();
660         }
661
662         p_sizer->Add( config_data->panel, 3, wxEXPAND | wxALL, 0 );
663         p_sizer->Layout();
664     }
665 }
666
667 void PrefsTreeCtrl::OnAdvanced( wxCommandEvent& event )
668 {
669     b_advanced = event.GetInt();
670
671     ConfigTreeData *config_data = !GetSelection() ? NULL :
672         FindModuleConfig( (ConfigTreeData *)GetItemData( GetSelection() ) );
673     if( config_data  )
674     {
675         config_data->panel->Hide();
676         p_sizer->Remove( config_data->panel );
677     }
678
679     if( GetSelection() )
680     {
681         wxTreeEvent event;
682         OnSelectTreeItem( event );
683     }
684 }
685
686 /*****************************************************************************
687  * PrefsPanel class definition.
688  *****************************************************************************/
689 PrefsPanel::PrefsPanel( wxWindow* parent, intf_thread_t *_p_intf,
690                         PrefsDialog *_p_prefs_dialog,
691                         int i_object_id, char *psz_section, char *psz_help )
692   :  wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize )
693 {
694     module_config_t *p_item;
695
696     wxStaticText *label;
697     wxStaticText *help;
698     wxArrayString array;
699
700     module_t *p_module = NULL;
701
702     /* Initializations */
703     p_intf = _p_intf;
704     p_prefs_dialog =_p_prefs_dialog,
705
706     b_advanced = VLC_TRUE;
707     SetAutoLayout( TRUE );
708
709     wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
710
711
712     if( i_object_id == PLUGIN_ID || i_object_id == GENERAL_ID ||
713         i_object_id == CAPABILITY_ID )
714     {
715         label = new wxStaticText( this, -1,wxU(_( psz_section )));
716         wxFont heading_font = label->GetFont();
717         heading_font.SetPointSize( heading_font.GetPointSize() + 5 );
718         label->SetFont( heading_font );
719         sizer->Add( label, 0, wxEXPAND | wxLEFT, 10 );
720         sizer->Add( new wxStaticLine( this, 0 ), 0,
721                     wxEXPAND | wxLEFT | wxRIGHT, 2 );
722
723         help = new wxStaticText( this, -1, wxU(_( psz_help ) ) );
724         sizer->Add( help ,0 ,wxEXPAND | wxALL, 5 );
725
726         config_sizer = NULL; config_window = NULL;
727     }
728     else
729     {
730         /* Get a pointer to the module */
731         p_module = (module_t *)vlc_object_get( p_intf,  i_object_id );
732         if( p_module->i_object_type != VLC_OBJECT_MODULE )
733         {
734             /* 0OOoo something went really bad */
735             return;
736         }
737
738         /* Enumerate config options and add corresponding config boxes
739          * (submodules don't have config options, they are stored in the
740          *  parent module) */
741         if( p_module->b_submodule )
742             p_item = ((module_t *)p_module->p_parent)->p_config;
743         else
744             p_item = p_module->p_config;
745
746         /* Find the category if it has been specified */
747         if( psz_section && p_item->i_type == CONFIG_HINT_CATEGORY )
748         {
749             while( !p_item->i_type == CONFIG_HINT_CATEGORY ||
750                    strcmp( psz_section, p_item->psz_text ) )
751             {
752                 if( p_item->i_type == CONFIG_HINT_END )
753                     break;
754                 p_item++;
755             }
756         }
757
758         /* Add a head title to the panel */
759         label = new wxStaticText( this, -1,
760                                   wxU(_(psz_section ? p_item->psz_text :
761                                   p_module->psz_longname )));
762         wxFont heading_font = label->GetFont();
763         heading_font.SetPointSize( heading_font.GetPointSize() + 5 );
764         label->SetFont( heading_font );
765         sizer->Add( label, 0, wxEXPAND | wxLEFT, 10 );
766         sizer->Add( new wxStaticLine( this, 0 ), 0,
767                     wxEXPAND | wxLEFT | wxRIGHT, 2 );
768
769         /* Now put all the config options into a scrolled window */
770         config_sizer = new wxBoxSizer( wxVERTICAL );
771         config_window = new wxScrolledWindow( this, -1, wxDefaultPosition,
772             wxDefaultSize, wxSTATIC_BORDER | wxHSCROLL | wxVSCROLL );
773         config_window->SetAutoLayout( TRUE );
774         config_window->SetScrollRate( 5, 5 );
775
776         if( p_item ) do
777         {
778             /* If a category has been specified, check we finished the job */
779             if( psz_section && p_item->i_type == CONFIG_HINT_CATEGORY &&
780                 strcmp( psz_section, p_item->psz_text ) )
781                 break;
782
783             ConfigControl *control =
784                 CreateConfigControl( VLC_OBJECT(p_intf),
785                                      p_item, config_window );
786
787             /* Don't add items that were not recognized */
788             if( control == NULL ) continue;
789
790             /* Add the config data to our array so we can keep a trace of it */
791             config_array.Add( control );
792
793             config_sizer->Add( control, 0, wxEXPAND | wxALL, 2 );
794         }
795         while( p_item->i_type != CONFIG_HINT_END && p_item++ );
796
797         config_sizer->Layout();
798         config_window->SetSizer( config_sizer );
799         sizer->Add( config_window, 1, wxEXPAND | wxALL, 5 );
800
801         /* And at last put a useful help string if available */
802         if( psz_help && *psz_help )
803         {
804             sizer->Add( new wxStaticLine( this, 0 ), 0,
805                         wxEXPAND | wxLEFT | wxRIGHT, 2 );
806             help = new wxStaticText( this, -1, wxU(_(psz_help)),
807                                      wxDefaultPosition, wxDefaultSize,
808                                      wxALIGN_LEFT,
809                                      wxT("") );
810             sizer->Add( help ,0 ,wxEXPAND | wxALL, 5 );
811         }
812
813     }
814     sizer->Layout();
815     SetSizer( sizer );
816 }
817
818 void PrefsPanel::ApplyChanges()
819 {
820     vlc_value_t val;
821
822     for( size_t i = 0; i < config_array.GetCount(); i++ )
823     {
824         ConfigControl *control = config_array.Item(i);
825
826         switch( control->GetType() )
827         {
828         case CONFIG_ITEM_STRING:
829         case CONFIG_ITEM_FILE:
830         case CONFIG_ITEM_DIRECTORY:
831         case CONFIG_ITEM_MODULE:
832             config_PutPsz( p_intf, control->GetName().mb_str(),
833                            control->GetPszValue().mb_str() );
834             break;
835         case CONFIG_ITEM_KEY:
836             /* So you don't need to restart to have the changes take effect */
837             val.i_int = control->GetIntValue();
838             var_Set( p_intf->p_vlc, control->GetName().mb_str(), val );
839         case CONFIG_ITEM_INTEGER:
840         case CONFIG_ITEM_BOOL:
841             config_PutInt( p_intf, control->GetName().mb_str(),
842                            control->GetIntValue() );
843             break;
844         case CONFIG_ITEM_FLOAT:
845             config_PutFloat( p_intf, control->GetName().mb_str(),
846                              control->GetFloatValue() );
847             break;
848         }
849     }
850 }
851
852 void PrefsPanel::SwitchAdvanced( vlc_bool_t b_new_advanced )
853 {
854     if( b_advanced == b_new_advanced ) return;
855
856     if( config_sizer && config_window )
857     {
858         b_advanced = b_new_advanced;
859
860         for( size_t i = 0; i < config_array.GetCount(); i++ )
861         {
862             ConfigControl *control = config_array.Item(i);
863             if( control->IsAdvanced() )
864             {
865                 control->Show( b_advanced );
866                 config_sizer->Show( control, b_advanced );
867             }
868         }
869
870         config_sizer->Layout();
871         config_window->FitInside();
872         config_window->Refresh();
873     }
874     return;
875 }