]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/preferences_widgets.cpp
* include/configuration.h: some small re-work of the config declaration macros.
[vlc] / modules / gui / wxwindows / preferences_widgets.cpp
1 /*****************************************************************************
2  * preferences_widgets.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: preferences_widgets.cpp,v 1.10 2003/11/05 00:39:16 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Sigmund Augdal <sigmunau@idi.ntnu.no>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <string.h>                                            /* strerror() */
31 #include <stdio.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc/intf.h>
35
36 #include <vlc_help.h>
37
38 #include "wxwindows.h"
39 #include "preferences_widgets.h"
40
41 /*****************************************************************************
42  * CreateConfigControl wrapper
43  *****************************************************************************/
44 ConfigControl *CreateConfigControl( vlc_object_t *p_this,
45                                     module_config_t *p_item, wxWindow *parent )
46 {
47     ConfigControl *p_control = NULL;
48
49     switch( p_item->i_type )
50     {
51     case CONFIG_ITEM_MODULE:
52         p_control = new ModuleConfigControl( p_this, p_item, parent );
53         break;
54
55     case CONFIG_ITEM_STRING:
56         if( !p_item->i_list )
57         {
58             p_control = new StringConfigControl( p_item, parent );
59         }
60         else
61         {
62             p_control = new StringListConfigControl( p_item, parent );
63         }
64         break;
65
66     case CONFIG_ITEM_FILE:
67     case CONFIG_ITEM_DIRECTORY:
68         p_control = new FileConfigControl( p_item, parent );
69         break;
70
71     case CONFIG_ITEM_INTEGER:
72         if( p_item->i_list )
73         {
74             p_control = new IntegerListConfigControl( p_item, parent );
75         }
76         else if( p_item->i_min != 0 || p_item->i_max != 0 )
77         {
78             p_control = new RangedIntConfigControl( p_item, parent );
79         }
80         else
81         {
82             p_control = new IntegerConfigControl( p_item, parent );
83         }
84         break;
85
86     case CONFIG_ITEM_KEY:
87         p_control = new KeyConfigControl( p_item, parent );
88         break;
89
90     case CONFIG_ITEM_FLOAT:
91         p_control = new FloatConfigControl( p_item, parent );
92         break;
93
94     case CONFIG_ITEM_BOOL:
95         p_control = new BoolConfigControl( p_item, parent );
96         break;
97
98     default:
99         break;
100     }
101
102     return p_control;
103 }
104
105 /*****************************************************************************
106  * ConfigControl implementation
107  *****************************************************************************/
108 ConfigControl::ConfigControl( module_config_t *p_item, wxWindow *parent )
109   : wxPanel( parent ), name( wxU(p_item->psz_name) ),
110     i_type( p_item->i_type ), b_advanced( p_item->b_advanced )
111 {
112     sizer = new wxBoxSizer( wxHORIZONTAL );
113 }
114
115 ConfigControl::~ConfigControl()
116 {
117 }
118
119 wxSizer *ConfigControl::Sizer()
120 {
121     return sizer;
122 }
123
124 wxString ConfigControl::GetName()
125 {
126     return name;
127 }
128
129 int ConfigControl::GetType()
130 {
131     return i_type;
132 }
133
134 vlc_bool_t ConfigControl::IsAdvanced()
135 {
136     return b_advanced;
137 }
138
139 /*****************************************************************************
140  * KeyConfigControl implementation
141  *****************************************************************************/
142 static wxString KeysList[] =
143 {
144     wxT("Unset"),
145     wxT("Left"),
146     wxT("Right"),
147     wxT("Up"),
148     wxT("Down"),
149     wxT("Space"),
150     wxT("Enter"),
151     wxT("F1"),
152     wxT("F2"),
153     wxT("F3"),
154     wxT("F4"),
155     wxT("F5"),
156     wxT("F6"),
157     wxT("F7"),
158     wxT("F8"),
159     wxT("F9"),
160     wxT("F10"),
161     wxT("F11"),
162     wxT("F12"),
163     wxT("Home"),
164     wxT("End"),
165     wxT("Menu"),
166     wxT("Esc"),
167     wxT("Page Up"),
168     wxT("Page Down"),
169     wxT("Tab"),
170     wxT("Backspace"),
171     wxT("a"),
172     wxT("b"),
173     wxT("c"),
174     wxT("d"),
175     wxT("e"),
176     wxT("f"),
177     wxT("g"),
178     wxT("h"),
179     wxT("i"),
180     wxT("j"),
181     wxT("k"),
182     wxT("l"),
183     wxT("m"),
184     wxT("n"),
185     wxT("o"),
186     wxT("p"),
187     wxT("q"),
188     wxT("r"),
189     wxT("s"),
190     wxT("t"),
191     wxT("u"),
192     wxT("v"),
193     wxT("w"),
194     wxT("x"),
195     wxT("y"),
196     wxT("z"),
197     wxT("+"),
198     wxT("="),
199     wxT("-"),
200     wxT(","),
201     wxT("."),
202     wxT("<"),
203     wxT(">"),
204     wxT("`"),
205     wxT("/"),
206     wxT(";"),
207     wxT("'"),
208     wxT("\\"),
209     wxT("["),
210     wxT("]"),
211     wxT("*")
212 };
213
214 KeyConfigControl::KeyConfigControl( module_config_t *p_item, wxWindow *parent )
215   : ConfigControl( p_item, parent )
216 {
217     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
218     alt = new wxCheckBox( this, -1, wxU(_("Alt")) );
219     alt->SetValue( p_item->i_value & KEY_MODIFIER_ALT );
220     ctrl = new wxCheckBox( this, -1, wxU(_("Ctrl")) );
221     ctrl->SetValue( p_item->i_value & KEY_MODIFIER_CTRL );
222     shift = new wxCheckBox( this, -1, wxU(_("Shift")) );
223     shift->SetValue( p_item->i_value & KEY_MODIFIER_SHIFT );
224     combo = new wxComboBox( this, -1, wxT(""), wxDefaultPosition,
225                             wxDefaultSize, WXSIZEOF(KeysList), KeysList,
226                             wxCB_READONLY );
227     for( unsigned int i = 0; i < WXSIZEOF(KeysList); i++ )
228     {
229         combo->SetClientData( i, (void*)keys[i].i_key_code );
230         if( keys[i].i_key_code ==
231             ( ((unsigned int)p_item->i_value) & ~KEY_MODIFIER ) )
232         {
233             combo->SetSelection( i );
234             combo->SetValue( wxU(_(keys[i].psz_key_string)) );
235         }
236     }
237
238     sizer->Add( label, 2, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
239     sizer->Add( alt,   1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
240     sizer->Add( ctrl,  1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
241     sizer->Add( shift, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
242     sizer->Add( combo, 2, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
243     sizer->Layout();
244     this->SetSizerAndFit( sizer );
245 }
246
247 KeyConfigControl::~KeyConfigControl()
248 {
249     ;
250 }
251
252 int KeyConfigControl::GetIntValue()
253 {
254     int result = 0;
255     if( alt->IsChecked() )
256     {
257         result |= KEY_MODIFIER_ALT;
258     }
259     if( ctrl->IsChecked() )
260     {
261         result |= KEY_MODIFIER_CTRL;
262     }
263     if( shift->IsChecked() )
264     {
265         result |= KEY_MODIFIER_SHIFT;
266     }
267     int selected = combo->GetSelection();
268     if( selected != -1 )
269     {
270         result |= (int)combo->GetClientData( selected );
271     }
272     return result;
273 }
274
275 /*****************************************************************************
276  * ModuleConfigControl implementation
277  *****************************************************************************/
278 ModuleConfigControl::ModuleConfigControl( vlc_object_t *p_this,
279                                           module_config_t *p_item,
280                                           wxWindow *parent )
281   : ConfigControl( p_item, parent )
282 {
283     vlc_list_t *p_list;
284     module_t *p_parser;
285
286     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
287     combo = new wxComboBox( this, -1, wxU(p_item->psz_value),
288                             wxDefaultPosition, wxDefaultSize,
289                             0, NULL, wxCB_READONLY | wxCB_SORT );
290
291     /* build a list of available modules */
292     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
293     combo->Append( wxU(_("Default")), (void *)NULL );
294     combo->SetSelection( 0 );
295     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
296     {
297         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
298
299         if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
300         {
301             combo->Append( wxU(p_parser->psz_longname),
302                            p_parser->psz_object_name );
303             if( p_item->psz_value && !strcmp(p_item->psz_value, 
304                                              p_parser->psz_object_name) )
305                 combo->SetValue( wxU(p_parser->psz_longname) );
306         }
307     }
308     vlc_list_release( p_list );
309
310     combo->SetToolTip( wxU(p_item->psz_longtext) );
311     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
312     sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
313     sizer->Layout();
314     this->SetSizerAndFit( sizer );
315 }
316
317 ModuleConfigControl::~ModuleConfigControl()
318 {
319     ;
320 }
321
322 wxString ModuleConfigControl::GetPszValue()
323 {
324     return wxU( (char *)combo->GetClientData( combo->GetSelection() ));
325 }
326
327 /*****************************************************************************
328  * StringConfigControl implementation
329  *****************************************************************************/
330 StringConfigControl::StringConfigControl( module_config_t *p_item,
331                                           wxWindow *parent )
332   : ConfigControl( p_item, parent )
333 {
334     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
335     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
336     textctrl = new wxTextCtrl( this, -1, 
337                                wxU(p_item->psz_value),
338                                wxDefaultPosition,
339                                wxDefaultSize,
340                                wxTE_PROCESS_ENTER);
341     textctrl->SetToolTip( wxU(p_item->psz_longtext) );
342     sizer->Add( textctrl, 1, wxALL, 5 );
343     sizer->Layout();
344     this->SetSizerAndFit( sizer );
345 }
346
347 StringConfigControl::~StringConfigControl()
348 {
349     ;
350 }
351
352 wxString StringConfigControl::GetPszValue()
353 {
354     return textctrl->GetValue();
355 }
356
357 /*****************************************************************************
358  * StringListConfigControl implementation
359  *****************************************************************************/
360 StringListConfigControl::StringListConfigControl( module_config_t *p_item,
361                                                   wxWindow *parent )
362   : ConfigControl( p_item, parent )
363 {
364     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
365     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
366     combo = new wxComboBox( this, -1, wxT(""),
367                             wxDefaultPosition, wxDefaultSize,
368                             0, NULL, wxCB_READONLY );
369
370     /* build a list of available options */
371     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
372     {
373         combo->Append( ( p_item->ppsz_list_text &&
374                          p_item->ppsz_list_text[i_index] ) ?
375                        wxU(p_item->ppsz_list_text[i_index]) :
376                        wxU(p_item->ppsz_list[i_index]) );
377         combo->SetClientData( i_index, (void *)p_item->ppsz_list[i_index] );
378         if( p_item->psz_value && !strcmp( p_item->psz_value,
379                                           p_item->ppsz_list[i_index] ) )
380         {
381             combo->SetSelection( i_index );
382             combo->SetValue( ( p_item->ppsz_list_text &&
383                                p_item->ppsz_list_text[i_index] ) ?
384                              wxU(p_item->ppsz_list_text[i_index]) :
385                              wxU(p_item->ppsz_list[i_index]) );
386         }
387     }
388
389     combo->SetToolTip( wxU(p_item->psz_longtext) );
390     sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );    
391     sizer->Layout();
392     this->SetSizerAndFit( sizer );
393 }
394
395 StringListConfigControl::~StringListConfigControl()
396 {
397     ;
398 }
399
400 wxString StringListConfigControl::GetPszValue()
401 {
402     int selected = combo->GetSelection();
403     if( selected != -1 )
404     {
405         return (char *)combo->GetClientData( selected );
406     }
407     return wxString();
408 }
409
410 /*****************************************************************************
411  * FileConfigControl implementation
412  *****************************************************************************/
413 FileConfigControl::FileConfigControl( module_config_t *p_item,
414                                       wxWindow *parent )
415   : ConfigControl( p_item, parent )
416 {
417     directory = p_item->i_type == CONFIG_ITEM_DIRECTORY;
418     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
419     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
420     textctrl = new wxTextCtrl( this, -1, 
421                                wxU(p_item->psz_value),
422                                wxDefaultPosition,
423                                wxDefaultSize,
424                                wxTE_PROCESS_ENTER);
425     textctrl->SetToolTip( wxU(p_item->psz_longtext) );
426     sizer->Add( textctrl, 1, wxALL, 5 );
427     browse = new wxButton( this, wxID_HIGHEST, wxU(_("Browse...")) );
428     sizer->Add( browse, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
429     sizer->Layout();
430     this->SetSizerAndFit( sizer );
431 }
432
433 BEGIN_EVENT_TABLE(FileConfigControl, wxPanel)
434     /* Button events */
435     EVT_BUTTON(wxID_HIGHEST, FileConfigControl::OnBrowse)
436 END_EVENT_TABLE()
437
438 void FileConfigControl::OnBrowse( wxCommandEvent& event )
439 {
440     if( directory )
441     {
442         wxDirDialog dialog( this, wxU(_("Choose Directory")) );
443
444         if( dialog.ShowModal() == wxID_OK )
445         {
446             textctrl->SetValue( dialog.GetPath() );      
447         }
448     }
449     else
450     {
451         wxFileDialog dialog( this, wxU(_("Choose File")),
452                              wxT(""), wxT(""), wxT("*.*"),
453 #if defined( __WXMSW__ )
454                              wxOPEN
455 #else
456                              wxOPEN | wxSAVE
457 #endif
458                            );
459     }
460 }
461
462 FileConfigControl::~FileConfigControl()
463 {
464     ;
465 }
466     
467 wxString FileConfigControl::GetPszValue()
468 {
469     return textctrl->GetValue();
470 }
471
472 /*****************************************************************************
473  * IntegerConfigControl implementation
474  *****************************************************************************/
475 IntegerConfigControl::IntegerConfigControl( module_config_t *p_item,
476                                             wxWindow *parent )
477   : ConfigControl( p_item, parent )
478 {
479     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
480     spin = new wxSpinCtrl( this, -1,
481                            wxString::Format(wxT("%d"),
482                                             p_item->i_value),
483                            wxDefaultPosition, wxDefaultSize,
484                            wxSP_ARROW_KEYS,
485                            -16000, 16000, p_item->i_value);
486     spin->SetToolTip( wxU(p_item->psz_longtext) );
487     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
488     sizer->Add( spin, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );    
489     sizer->Layout();
490     this->SetSizerAndFit( sizer );
491 }
492
493 IntegerConfigControl::~IntegerConfigControl()
494 {
495     ;
496 }
497
498 int IntegerConfigControl::GetIntValue()
499 {
500     return spin->GetValue();
501 }
502
503 /*****************************************************************************
504  * IntegerListConfigControl implementation
505  *****************************************************************************/
506 IntegerListConfigControl::IntegerListConfigControl( module_config_t *p_item,
507                                                     wxWindow *parent )
508   : ConfigControl( p_item, parent )
509 {
510     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
511     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
512     combo = new wxComboBox( this, -1, wxT(""),
513                             wxDefaultPosition, wxDefaultSize,
514                             0, NULL, wxCB_READONLY );
515
516     /* build a list of available options */
517     for( int i_index = 0; i_index < p_item->i_list; i_index++ )
518     {
519         combo->Append( ( p_item->ppsz_list_text &&
520                          p_item->ppsz_list_text[i_index] ) ?
521                        wxU(p_item->ppsz_list_text[i_index]) :
522                        wxString::Format(wxT("%i"),
523                                         p_item->pi_list[i_index]) );
524         combo->SetClientData( i_index, (void *)p_item->pi_list[i_index] );
525         if( p_item->i_value == p_item->pi_list[i_index] )
526         {
527             combo->SetSelection( i_index );
528             combo->SetValue( ( p_item->ppsz_list_text &&
529                                p_item->ppsz_list_text[i_index] ) ?
530                              wxU(p_item->ppsz_list_text[i_index]) :
531                              wxString::Format(wxT("%i"),
532                                               p_item->pi_list[i_index]) );
533         }
534     }
535
536     combo->SetToolTip( wxU(p_item->psz_longtext) );
537     sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );    
538     sizer->Layout();
539     this->SetSizerAndFit( sizer );
540 }
541
542 IntegerListConfigControl::~IntegerListConfigControl()
543 {
544     ;
545 }
546
547 int IntegerListConfigControl::GetIntValue()
548 {
549     int selected = combo->GetSelection();
550     if( selected != -1 )
551     {
552         return (int)combo->GetClientData( selected );
553     }
554     return -1;
555 }
556
557 /*****************************************************************************
558  * RangedIntConfigControl implementation
559  *****************************************************************************/
560 RangedIntConfigControl::RangedIntConfigControl( module_config_t *p_item,
561                                                 wxWindow *parent )
562   : ConfigControl( p_item, parent )
563 {
564     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
565     slider = new wxSlider( this, -1, p_item->i_value, p_item->i_min,
566                            p_item->i_max, wxDefaultPosition, wxDefaultSize,
567                            wxSL_LABELS | wxSL_HORIZONTAL );
568     slider->SetToolTip( wxU(p_item->psz_longtext) );
569     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
570     sizer->Add( slider, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );    
571     sizer->Layout();
572     this->SetSizerAndFit( sizer );
573 }
574
575 RangedIntConfigControl::~RangedIntConfigControl()
576 {
577     ;
578 }
579
580 int RangedIntConfigControl::GetIntValue()
581 {
582     return slider->GetValue();
583 }
584
585 /*****************************************************************************
586  * FloatConfigControl implementation
587  *****************************************************************************/
588 FloatConfigControl::FloatConfigControl( module_config_t *p_item,
589                                         wxWindow *parent )
590   : ConfigControl( p_item, parent )
591 {
592     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
593     textctrl = new wxTextCtrl( this, -1,
594                                wxString::Format(wxT("%f"),
595                                                 p_item->f_value),
596                                wxDefaultPosition, wxDefaultSize,
597                                wxTE_PROCESS_ENTER );
598     textctrl->SetToolTip( wxU(p_item->psz_longtext) );
599     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
600     sizer->Add( textctrl, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
601     sizer->Layout();
602     this->SetSizerAndFit( sizer );
603 }
604
605 FloatConfigControl::~FloatConfigControl()
606 {
607     ;
608 }
609
610 float FloatConfigControl::GetFloatValue()
611 {
612     float f_value;
613     if( (wxSscanf(textctrl->GetValue(), wxT("%f"), &f_value) == 1) )
614         return f_value;
615     else return 0.0;
616 }
617
618 /*****************************************************************************
619  * BoolConfigControl implementation
620  *****************************************************************************/
621 BoolConfigControl::BoolConfigControl( module_config_t *p_item,
622                                       wxWindow *parent )
623   : ConfigControl( p_item, parent )
624 {
625     checkbox = new wxCheckBox( this, -1, wxU(p_item->psz_text) );
626     if( p_item->i_value ) checkbox->SetValue(TRUE);
627     checkbox->SetToolTip( wxU(p_item->psz_longtext) );
628     sizer->Add( checkbox, 0, wxALL, 5 );
629     sizer->Layout();
630     this->SetSizerAndFit( sizer );
631 }
632
633 BoolConfigControl::~BoolConfigControl()
634 {
635     ;
636 }
637
638 int BoolConfigControl::GetIntValue()
639 {
640     if( checkbox->IsChecked() )
641     {
642         return 1;
643     }
644     else
645     {
646         return 0;
647     }
648 }