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