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