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