]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/dialogs/preferences.cpp
cb99743f0c275b5dff70d354e61026594460502a
[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         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         p_item = p_module->p_config;
371         p_end = p_item + p_module->confsize;
372
373         if( p_item ) do
374         {
375             ConfigTreeData *config_data;
376             switch( p_item->i_type )
377             {
378             case CONFIG_CATEGORY:
379                 config_data = new ConfigTreeData;
380                 if( p_item->value.i == -1 )   break; // Don't display it
381                 config_data->psz_name = strdup( config_CategoryNameGet(
382                                                             p_item->value.i ) );
383                 psz_help = config_CategoryHelpGet( p_item->value.i );
384                 if( psz_help )
385                 {
386                     config_data->psz_help = wraptext( strdup( psz_help ), 72 );
387                 }
388                 else
389                 {
390                     config_data->psz_help = NULL;
391                 }
392                 config_data->i_type = TYPE_CATEGORY;
393                 config_data->i_object_id = p_item->value.i;
394
395                 /* Add the category to the tree */
396                 switch( p_item->value.i )
397                 {
398                     case CAT_AUDIO:
399                         i_image = 0; break;
400                     case CAT_VIDEO:
401                         i_image = 1; break;
402                     case CAT_INPUT:
403                         i_image = 2; break;
404                     case CAT_SOUT:
405                         i_image = 3; break;
406                     case CAT_ADVANCED:
407                         i_image = 4; break;
408                     case CAT_PLAYLIST:
409                         i_image = 5; break;
410                     case CAT_INTERFACE:
411                         i_image = 6; break;
412                 }
413                 current_item = AppendItem( root_item,
414                                            wxU( _(config_data->psz_name) ),
415                                            i_image, -1, config_data );
416
417                 break;
418             case CONFIG_SUBCATEGORY:
419                 if( p_item->value.i == -1 ) break; // Don't display it
420                 /* Special case: move the "general" subcategories to their
421                  * parent category */
422                 if( p_item->value.i == SUBCAT_VIDEO_GENERAL ||
423                     p_item->value.i == SUBCAT_ADVANCED_MISC ||
424                     p_item->value.i == SUBCAT_INPUT_GENERAL ||
425                     p_item->value.i == SUBCAT_INTERFACE_GENERAL ||
426                     p_item->value.i == SUBCAT_SOUT_GENERAL||
427                     p_item->value.i == SUBCAT_PLAYLIST_GENERAL||
428                     p_item->value.i == SUBCAT_AUDIO_GENERAL )
429                 {
430                     ConfigTreeData *cd = (ConfigTreeData *)
431                                             GetItemData( current_item );
432                     cd->i_type = TYPE_CATSUBCAT;
433                     cd->i_subcat_id = p_item->value.i;
434                     if( cd->psz_name ) free( cd->psz_name );
435                     cd->psz_name = strdup(  config_CategoryNameGet(
436                                                       p_item->value.i ) );
437                     if( cd->psz_help ) free( cd->psz_help );
438                     char *psz_help = config_CategoryHelpGet( p_item->value.i );
439                     if( psz_help )
440                     {
441                         cd->psz_help = wraptext( strdup( psz_help ), 72 );
442                     }
443                     else
444                     {
445                         cd->psz_help = NULL;
446                     }
447                     continue;
448                 }
449
450                 config_data = new ConfigTreeData;
451
452                 config_data->psz_name = strdup(  config_CategoryNameGet(
453                                                            p_item->value.i ) );
454                 psz_help = config_CategoryHelpGet( p_item->value.i );
455                 if( psz_help )
456                 {
457                     config_data->psz_help = wraptext( strdup( psz_help ), 72 );
458                 }
459                 else
460                 {
461                     config_data->psz_help = NULL;
462                 }
463                 config_data->i_type = TYPE_SUBCATEGORY;
464                 config_data->i_object_id = p_item->value.i;
465                 /* WXMSW doesn't know image -1 ... FIXME */
466                 #ifdef __WXMSW__
467                 switch( p_item->value.i / 100 )
468                 {
469                     case CAT_AUDIO:
470                         i_image = 0; break;
471                     case CAT_VIDEO:
472                         i_image = 1; break;
473                     case CAT_INPUT:
474                         i_image = 2; break;
475                     case CAT_SOUT:
476                         i_image = 3; break;
477                     case CAT_ADVANCED:
478                         i_image = 4; break;
479                     case CAT_PLAYLIST:
480                         i_image = 5; break;
481                     case CAT_INTERFACE:
482                         i_image = 6; break;
483                 }
484                 #else
485                 i_image = -1;
486                 #endif
487                 AppendItem( current_item, wxU( _(config_data->psz_name) ),
488                             i_image, -1, config_data );
489                 break;
490             }
491         }
492         while( p_item < p_end && p_item++ );
493
494     }
495
496
497     /*
498      * Build a tree of all the plugins
499      */
500     for( i_index = 0; i_index < p_list->i_count; i_index++ )
501     {
502         int i_category = -1;
503         int i_subcategory = -1;
504         int i_options = 0;
505
506         p_module = (module_t *)p_list->p_values[i_index].p_object;
507
508         /* Exclude the main module */
509         if( !strcmp( p_module->psz_object_name, "main" ) )
510             continue;
511
512         /* Exclude empty plugins (submodules don't have config options, they
513          * are stored in the parent module) */
514         if( p_module->b_submodule )
515               continue;
516 //            p_item = ((module_t *)p_module->p_parent)->p_config;
517         else
518             p_item = p_module->p_config;
519             p_end = p_item + p_module->confsize;
520
521         if( !p_item ) continue;
522         do
523         {
524             if( p_item->i_type == CONFIG_CATEGORY )
525             {
526                 i_category = p_item->value.i;
527             }
528             else if( p_item->i_type == CONFIG_SUBCATEGORY )
529             {
530                 i_subcategory = p_item->value.i;
531             }
532             if( p_item->i_type & CONFIG_ITEM )
533                 i_options ++;
534             if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
535             {
536                 break;
537             }
538         }
539         while( p_item < p_end && p_item++ );
540
541         if( !i_options ) continue;
542
543         /* Find the right category item */
544         wxTreeItemIdValue cookie;
545         vlc_bool_t b_found = VLC_FALSE;
546
547         wxTreeItemId category_item = GetFirstChild( root_item , cookie);
548         while( category_item.IsOk() )
549         {
550             ConfigTreeData *config_data =
551                     (ConfigTreeData *)GetItemData( category_item );
552             if( config_data->i_object_id == i_category )
553             {
554                 b_found = VLC_TRUE;
555                 break;
556             }
557             category_item = GetNextChild( root_item, cookie );
558         }
559
560         if( !b_found ) continue;
561
562         /* Find subcategory item */
563         b_found = VLC_FALSE;
564         //cookie = -1;
565         wxTreeItemId subcategory_item = GetFirstChild( category_item, cookie );
566         while( subcategory_item.IsOk() )
567         {
568             ConfigTreeData *config_data =
569                     (ConfigTreeData *)GetItemData( subcategory_item );
570             if( config_data->i_object_id == i_subcategory )
571             {
572                 b_found = VLC_TRUE;
573                 break;
574             }
575             subcategory_item = GetNextChild( category_item, cookie );
576         }
577         if( !b_found )
578         {
579             subcategory_item = category_item;
580         }
581
582         /* Add the plugin to the tree */
583         ConfigTreeData *config_data = new ConfigTreeData;
584         config_data->b_submodule = p_module->b_submodule;
585         config_data->i_type = TYPE_MODULE;
586         config_data->i_object_id = p_module->b_submodule ?
587             ((module_t *)p_module->p_parent)->i_object_id :
588             p_module->i_object_id;
589         config_data->psz_help = NULL;
590
591         /* WXMSW doesn't know image -1 ... FIXME */
592         #ifdef __WXMSW__
593         switch( i_subcategory / 100 )
594         {
595             case CAT_AUDIO:
596                 i_image = 0; break;
597             case CAT_VIDEO:
598                 i_image = 1; break;
599             case CAT_INPUT:
600                 i_image = 2; break;
601             case CAT_SOUT:
602                 i_image = 3; break;
603             case CAT_ADVANCED:
604                 i_image = 4; break;
605             case CAT_PLAYLIST:
606                 i_image = 5; break;
607             case CAT_INTERFACE:
608                 i_image = 6; break;
609         }
610         #else
611         i_image = -1;
612         #endif
613         AppendItem( subcategory_item, wxU( p_module->psz_shortname ?
614                        p_module->psz_shortname : p_module->psz_object_name )
615                                     , i_image, -1,
616                     config_data );
617     }
618
619     /* Sort all this mess */
620     wxTreeItemIdValue cookie;
621     size_t i_child_index;
622     wxTreeItemId capability_item = GetFirstChild( root_item, cookie);
623     for( i_child_index = 0;
624          (capability_item.IsOk() &&
625           //(i_child_index < GetChildrenCount( plugins_item, FALSE )));
626           (i_child_index < GetChildrenCount( root_item, FALSE )));
627          i_child_index++ )
628     {
629         SortChildren( capability_item );
630         //capability_item = GetNextChild( plugins_item, cookie );
631         capability_item = GetNextChild( root_item, cookie );
632     }
633
634     /* Clean-up everything */
635     vlc_list_release( p_list );
636
637     p_sizer->Add( this, 1, wxEXPAND | wxALL, 0 );
638     p_sizer->Layout();
639
640     p_current = NULL;
641
642     /* Update Tree Ctrl */
643 #ifndef WIN32 /* Workaround a bug in win32 implementation */
644     SelectItem( GetFirstChild( root_item, cookie ) );
645 #endif
646
647     //cannot expand hidden root item
648     //Expand( root_item );
649 }
650
651 PrefsTreeCtrl::~PrefsTreeCtrl(){
652 }
653
654 void PrefsTreeCtrl::ApplyChanges()
655 {
656     wxTreeItemIdValue cookie, cookie2, cookie3;
657     ConfigTreeData *config_data;
658
659     wxTreeItemId category = GetFirstChild( root_item, cookie );
660     while( category.IsOk() )
661     {
662         wxTreeItemId subcategory = GetFirstChild( category, cookie2 );
663         while( subcategory.IsOk() )
664         {
665             wxTreeItemId module = GetFirstChild( subcategory, cookie3 );
666             while( module.IsOk() )
667             {
668                 config_data = (ConfigTreeData *)GetItemData( module );
669                 if( config_data && config_data->panel )
670                 {
671                     config_data->panel->ApplyChanges();
672                 }
673                 module = GetNextChild( subcategory, cookie3 );
674             }
675             config_data = (ConfigTreeData *)GetItemData( subcategory );
676             if( config_data && config_data->panel )
677             {
678                 config_data->panel->ApplyChanges();
679             }
680             subcategory = GetNextChild( category, cookie2 );
681         }
682         config_data = (ConfigTreeData *)GetItemData( category );
683         if( config_data && config_data->panel )
684         {
685             config_data->panel->ApplyChanges();
686         }
687         category = GetNextChild( root_item, cookie );
688     }
689 }
690
691 void PrefsTreeCtrl::CleanChanges()
692 {
693     wxTreeItemIdValue cookie, cookie2, cookie3;
694     ConfigTreeData *config_data;
695
696     config_data = !GetSelection() ? NULL :
697         FindModuleConfig( (ConfigTreeData *)GetItemData( GetSelection() ) );
698     if( config_data  )
699     {
700         config_data->panel->Hide();
701 #if (wxCHECK_VERSION(2,5,0))
702         p_sizer->Detach( config_data->panel );
703 #else
704         p_sizer->Remove( config_data->panel );
705 #endif
706         p_current = NULL;
707     }
708
709     wxTreeItemId category = GetFirstChild( root_item, cookie );
710     while( category.IsOk() )
711     {
712         wxTreeItemId subcategory = GetFirstChild( category, cookie2 );
713         while( subcategory.IsOk() )
714         {
715             wxTreeItemId module = GetFirstChild( subcategory, cookie3 );
716             while( module.IsOk() )
717             {
718                 config_data = (ConfigTreeData *)GetItemData( module );
719                 if( config_data && config_data->panel )
720                 {
721                     delete config_data->panel;
722                     config_data->panel = NULL;
723                 }
724                 module = GetNextChild( subcategory, cookie3 );
725             }
726             config_data = (ConfigTreeData *)GetItemData( subcategory );
727             if( config_data && config_data->panel )
728             {
729                 delete config_data->panel;
730                 config_data->panel = NULL;
731             }
732             subcategory = GetNextChild( category, cookie2 );
733         }
734         config_data = (ConfigTreeData *)GetItemData( category );
735         if( config_data && config_data->panel )
736         {
737             delete config_data->panel;
738             config_data->panel = NULL;
739         }
740         category = GetNextChild( root_item, cookie );
741     }
742
743     if( GetSelection() )
744     {
745         wxTreeEvent event;
746         OnSelectTreeItem( event );
747     }
748 }
749
750 ConfigTreeData *PrefsTreeCtrl::FindModuleConfig( ConfigTreeData *config_data )
751 {
752     /* We need this complexity because submodules don't have their own config
753      * options. They use the parent module ones. */
754
755     if( !config_data || !config_data->b_submodule )
756     {
757         return config_data;
758     }
759
760     wxTreeItemIdValue cookie, cookie2, cookie3;
761     ConfigTreeData *config_new;
762     wxTreeItemId category = GetFirstChild( root_item, cookie );
763     while( category.IsOk() )
764     {
765         wxTreeItemId subcategory = GetFirstChild( category, cookie2 );
766         while( subcategory.IsOk() )
767         {
768             wxTreeItemId module = GetFirstChild( subcategory, cookie3 );
769             while( module.IsOk() )
770             {
771                 config_new = (ConfigTreeData *)GetItemData( module );
772                 if( config_new && !config_new->b_submodule &&
773                     config_new->i_object_id == config_data->i_object_id )
774                 {
775                     return config_new;
776                 }
777                 module = GetNextChild( subcategory, cookie3 );
778             }
779             subcategory = GetNextChild( category, cookie2 );
780         }
781         category = GetNextChild( root_item, cookie );
782     }
783
784     /* Found nothing */
785     return NULL;
786 }
787
788 void PrefsTreeCtrl::OnSelectTreeItem( wxTreeEvent& event )
789 {
790     ConfigTreeData *config_data = NULL;
791
792     if( p_current )
793     {
794         p_current->Hide();
795 #if (wxCHECK_VERSION(2,5,0))
796         p_sizer->Detach( p_current  );
797 #else
798         p_sizer->Remove( p_current );
799 #endif
800         p_current = NULL;
801     }
802
803     /* Don't use event.GetItem() because we also send fake events */
804     config_data = FindModuleConfig( (ConfigTreeData *)GetItemData(
805                                     GetSelection() ) );
806     if( config_data )
807     {
808         if( !config_data->panel )
809         {
810             /* The panel hasn't been created yet. Let's do it. */
811             config_data->panel =
812                 new PrefsPanel( p_parent, p_intf, p_prefs_dialog,
813                                 config_data );
814             config_data->panel->SwitchAdvanced( b_advanced );
815         }
816         else
817         {
818             config_data->panel->SwitchAdvanced( b_advanced );
819             config_data->panel->Show();
820         }
821
822         p_current = config_data->panel;
823
824         p_sizer->Add( config_data->panel, 3, wxEXPAND | wxALL, 0 );
825         p_sizer->Layout();
826     }
827 }
828
829 void PrefsTreeCtrl::OnAdvanced( wxCommandEvent& event )
830 {
831     b_advanced = event.GetInt();
832
833     ConfigTreeData *config_data = !GetSelection() ? NULL :
834         FindModuleConfig( (ConfigTreeData *)GetItemData( GetSelection() ) );
835     if( config_data  )
836     {
837         config_data->panel->Hide();
838 #if (wxCHECK_VERSION(2,5,0))
839         p_sizer->Detach( config_data->panel );
840 #else
841         p_sizer->Remove( config_data->panel );
842 #endif
843         p_current = NULL;
844     }
845
846     if( GetSelection() )
847     {
848         wxTreeEvent event;
849         OnSelectTreeItem( event );
850     }
851 }
852
853 /*****************************************************************************
854  * PrefsPanel class definition.
855  *****************************************************************************/
856 PrefsPanel::PrefsPanel( wxWindow* parent, intf_thread_t *_p_intf,
857                         PrefsDialog *_p_prefs_dialog,
858                         ConfigTreeData *config_data )
859   :  wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize )
860 {
861     module_config_t *p_item, *p_end;
862     vlc_list_t *p_list = NULL;;
863
864     wxStaticText *label;
865     wxStaticText *help;
866     wxArrayString array;
867
868     module_t *p_module = NULL;
869
870     /* Initializations */
871     p_intf = _p_intf;
872     p_prefs_dialog =_p_prefs_dialog,
873
874     b_advanced = VLC_TRUE;
875     SetAutoLayout( TRUE );
876     Hide();
877
878     wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
879
880
881     if( config_data->i_type == TYPE_CATEGORY )
882     {
883         label = new wxStaticText( this, -1,wxU(_( config_data->psz_name )));
884         wxFont heading_font = label->GetFont();
885         heading_font.SetPointSize( heading_font.GetPointSize() + 5 );
886         label->SetFont( heading_font );
887         sizer->Add( label, 0, wxEXPAND | wxLEFT, 10 );
888         sizer->Add( new wxStaticLine( this, 0 ), 0,
889                     wxEXPAND | wxLEFT | wxRIGHT, 2 );
890
891         hidden_text = NULL;
892         help = new wxStaticText( this, -1, wxU(_( config_data->psz_help ) ) );
893         sizer->Add( help ,0 ,wxEXPAND | wxALL, 5 );
894
895         config_sizer = NULL; config_window = NULL;
896     }
897     else
898     {
899         /* Get a pointer to the module */
900         if( config_data->i_type == TYPE_MODULE )
901         {
902             p_module = (module_t *)vlc_object_get( p_intf,
903                                                    config_data->i_object_id );
904         }
905         else
906         {
907             /* List the plugins */
908             int i_index;
909             vlc_bool_t b_found = VLC_FALSE;
910             p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
911             if( !p_list ) return;
912
913             for( i_index = 0; i_index < p_list->i_count; i_index++ )
914             {
915                 p_module = (module_t *)p_list->p_values[i_index].p_object;
916                 if( !strcmp( p_module->psz_object_name, "main" ) )
917                 {
918                     b_found = VLC_TRUE;
919                     break;
920                 }
921             }
922             if( !p_module && !b_found )
923             {
924                 msg_Warn( p_intf, "unable to create preferences "
925                                   "(main module not found)" );
926                 return;
927             }
928         }
929
930         if( p_module->i_object_type != VLC_OBJECT_MODULE )
931         {
932             /* 0OOoo something went really bad */
933             return;
934         }
935
936         /* Enumerate config options and add corresponding config boxes
937          * (submodules don't have config options, they are stored in the
938          *  parent module) */
939         if( p_module->b_submodule )
940             p_item = ((module_t *)p_module->p_parent)->p_config;
941         else
942             p_item = p_module->p_config;
943
944         p_end = p_item + p_module->confsize;
945
946         /* Find the category if it has been specified */
947         if( config_data->i_type == TYPE_SUBCATEGORY ||
948             config_data->i_type == TYPE_CATSUBCAT )
949         {
950             do
951             {
952                 if( p_item->i_type == CONFIG_SUBCATEGORY &&
953                     ( config_data->i_type == TYPE_SUBCATEGORY &&
954                       p_item->value.i == config_data->i_object_id ) ||
955                     ( config_data->i_type == TYPE_CATSUBCAT &&
956                       p_item->value.i == config_data->i_subcat_id ) )
957                 {
958                     break;
959                 }
960                 if( p_item < p_end )
961                     break;
962             } while( p_item++ );
963         }
964
965         /* Add a head title to the panel */
966         char *psz_head;
967         if( config_data->i_type == TYPE_SUBCATEGORY ||
968             config_data->i_type == TYPE_CATSUBCAT )
969         {
970             psz_head = config_data->psz_name;
971             p_item++;
972         }
973         else
974         {
975             psz_head = p_module->psz_longname;
976         }
977
978         label = new wxStaticText( this, -1,
979                       wxU(_( psz_head ? psz_head : _("Unknown") ) ) );
980         wxFont heading_font = label->GetFont();
981         heading_font.SetPointSize( heading_font.GetPointSize() + 5 );
982         label->SetFont( heading_font );
983         sizer->Add( label, 0, wxEXPAND | wxLEFT, 10 );
984         sizer->Add( new wxStaticLine( this, 0 ), 0,
985                     wxEXPAND | wxLEFT | wxRIGHT, 2 );
986
987         /* Now put all the config options into a scrolled window */
988         config_sizer = new wxBoxSizer( wxVERTICAL );
989         config_window = new wxScrolledWindow( this, -1, wxDefaultPosition,
990             wxDefaultSize, wxBORDER_NONE | wxHSCROLL | wxVSCROLL );
991         config_window->SetAutoLayout( TRUE );
992         config_window->SetScrollRate( 5, 5 );
993
994         if( p_item ) do
995         {
996             /* If a category has been specified, check we finished the job */
997             if( ( ( config_data->i_type == TYPE_SUBCATEGORY &&
998                     p_item->value.i != config_data->i_object_id ) ||
999                   ( config_data->i_type == TYPE_CATSUBCAT  &&
1000                     p_item->value.i != config_data->i_subcat_id ) ) &&
1001                 (p_item->i_type == CONFIG_CATEGORY ||
1002                   p_item->i_type == CONFIG_SUBCATEGORY ) )
1003                 break;
1004
1005             if( p_item->b_internal == VLC_TRUE ) continue;
1006
1007             ConfigControl *control =
1008                 CreateConfigControl( VLC_OBJECT(p_intf),
1009                                      p_item, config_window );
1010
1011             /* Don't add items that were not recognized */
1012             if( control == NULL ) continue;
1013
1014             /* Add the config data to our array so we can keep a trace of it */
1015             config_array.Add( control );
1016
1017             config_sizer->Add( control, 0, wxEXPAND | wxALL, 2 );
1018         }
1019         while( !( p_item < p_end )||
1020                ( ( config_data->i_type == TYPE_SUBCATEGORY ||
1021                    config_data->i_type == TYPE_CATSUBCAT ) &&
1022                  ( p_item->i_type == CONFIG_CATEGORY ||
1023                    p_item->i_type == CONFIG_SUBCATEGORY ) ) && p_item++ );
1024
1025
1026         config_sizer->Layout();
1027         config_window->SetSizer( config_sizer );
1028         sizer->Add( config_window, 1, wxEXPAND | wxALL, 5 );
1029         hidden_text = new wxStaticText( this, -1,
1030                         wxU( _( "Some options are available but hidden. " \
1031                                 "Check \"Advanced options\" to see them." ) ) );
1032         sizer->Add( hidden_text );
1033
1034         /* And at last put a useful help string if available */
1035         if( config_data->psz_help && *config_data->psz_help )
1036         {
1037             sizer->Add( new wxStaticLine( this, 0 ), 0,
1038                         wxEXPAND | wxLEFT | wxRIGHT, 2 );
1039             help = new wxStaticText( this, -1, wxU(_(config_data->psz_help)),
1040                                      wxDefaultPosition, wxDefaultSize,
1041                                      wxALIGN_LEFT,
1042                                      wxT("") );
1043             sizer->Add( help ,0 ,wxEXPAND | wxALL, 5 );
1044         }
1045
1046         if( config_data->i_type == TYPE_MODULE )
1047         {
1048             vlc_object_release( p_module );
1049         }
1050         else
1051         {
1052             vlc_list_release( p_list );
1053         }
1054     }
1055     sizer->Layout();
1056     SetSizer( sizer );
1057     Show();
1058 }
1059
1060 void PrefsPanel::ApplyChanges()
1061 {
1062     vlc_value_t val;
1063
1064     for( size_t i = 0; i < config_array.GetCount(); i++ )
1065     {
1066         ConfigControl *control = config_array.Item(i);
1067
1068         switch( control->GetType() )
1069         {
1070         case CONFIG_ITEM_STRING:
1071         case CONFIG_ITEM_FILE:
1072         case CONFIG_ITEM_DIRECTORY:
1073         case CONFIG_ITEM_MODULE:
1074         case CONFIG_ITEM_MODULE_CAT:
1075         case CONFIG_ITEM_MODULE_LIST:
1076         case CONFIG_ITEM_MODULE_LIST_CAT:
1077             config_PutPsz( p_intf, control->GetName().mb_str(),
1078                            control->GetPszValue().mb_str() );
1079             break;
1080         case CONFIG_ITEM_KEY:
1081             /* So you don't need to restart to have the changes take effect */
1082             val.i_int = control->GetIntValue();
1083             var_Set( p_intf->p_libvlc, control->GetName().mb_str(), val );
1084         case CONFIG_ITEM_INTEGER:
1085         case CONFIG_ITEM_BOOL:
1086             config_PutInt( p_intf, control->GetName().mb_str(),
1087                            control->GetIntValue() );
1088             break;
1089         case CONFIG_ITEM_FLOAT:
1090             config_PutFloat( p_intf, control->GetName().mb_str(),
1091                              control->GetFloatValue() );
1092             break;
1093         }
1094     }
1095 }
1096
1097 void PrefsPanel::SwitchAdvanced( vlc_bool_t b_new_advanced )
1098 {
1099     bool hidden = false;
1100
1101     if( b_advanced == b_new_advanced )
1102     {
1103         goto hide;
1104     }
1105
1106     if( config_sizer && config_window )
1107     {
1108         b_advanced = b_new_advanced;
1109
1110         for( size_t i = 0; i < config_array.GetCount(); i++ )
1111         {
1112             ConfigControl *control = config_array.Item(i);
1113             if( control->IsAdvanced() )
1114             {
1115                 if( !b_advanced ) hidden = true;
1116                 control->Show( b_advanced );
1117                 config_sizer->Show( control, b_advanced );
1118             }
1119         }
1120
1121         config_sizer->Layout();
1122         config_window->FitInside();
1123         config_window->Refresh();
1124     }
1125 hide:
1126     if( hidden && hidden_text )
1127     {
1128         hidden_text->Show( true );
1129         config_sizer->Show( hidden_text, true );
1130     }
1131     else if ( hidden_text )
1132     {
1133         hidden_text->Show( false );
1134         config_sizer->Show( hidden_text, false );
1135     }
1136     return;
1137 }