]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/preferences_widgets.cpp
c572e063967f4654d6d0ab0b20bba101b5ece3f5
[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.9 2003/11/02 22:16:32 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->ppsz_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_min != 0 || p_item->i_max != 0 )
73         {
74             p_control = new RangedIntConfigControl( p_item, parent );
75         }
76         else
77         {
78             p_control = new IntegerConfigControl( p_item, parent );
79         }
80         break;
81
82     case CONFIG_ITEM_KEY:
83         p_control = new KeyConfigControl( p_item, parent );
84         break;
85
86     case CONFIG_ITEM_FLOAT:
87         p_control = new FloatConfigControl( p_item, parent );
88         break;
89
90     case CONFIG_ITEM_BOOL:
91         p_control = new BoolConfigControl( p_item, parent );
92         break;
93
94     default:
95         break;
96     }
97
98     return p_control;
99 }
100
101 /*****************************************************************************
102  * ConfigControl implementation
103  *****************************************************************************/
104 ConfigControl::ConfigControl( module_config_t *p_item, wxWindow *parent )
105   : wxPanel( parent ), name( wxU(p_item->psz_name) ),
106     i_type( p_item->i_type ), b_advanced( p_item->b_advanced )
107 {
108     sizer = new wxBoxSizer( wxHORIZONTAL );
109 }
110
111 ConfigControl::~ConfigControl()
112 {
113 }
114
115 wxSizer *ConfigControl::Sizer()
116 {
117     return sizer;
118 }
119
120 wxString ConfigControl::GetName()
121 {
122     return name;
123 }
124
125 int ConfigControl::GetType()
126 {
127     return i_type;
128 }
129
130 vlc_bool_t ConfigControl::IsAdvanced()
131 {
132     return b_advanced;
133 }
134
135 /*****************************************************************************
136  * KeyConfigControl implementation
137  *****************************************************************************/
138 static wxString KeysList[] =
139 {
140     wxT("Unset"),
141     wxT("Left"),
142     wxT("Right"),
143     wxT("Up"),
144     wxT("Down"),
145     wxT("Space"),
146     wxT("Enter"),
147     wxT("F1"),
148     wxT("F2"),
149     wxT("F3"),
150     wxT("F4"),
151     wxT("F5"),
152     wxT("F6"),
153     wxT("F7"),
154     wxT("F8"),
155     wxT("F9"),
156     wxT("F10"),
157     wxT("F11"),
158     wxT("F12"),
159     wxT("Home"),
160     wxT("End"),
161     wxT("Menu"),
162     wxT("Esc"),
163     wxT("Page Up"),
164     wxT("Page Down"),
165     wxT("Tab"),
166     wxT("Backspace"),
167     wxT("a"),
168     wxT("b"),
169     wxT("c"),
170     wxT("d"),
171     wxT("e"),
172     wxT("f"),
173     wxT("g"),
174     wxT("h"),
175     wxT("i"),
176     wxT("j"),
177     wxT("k"),
178     wxT("l"),
179     wxT("m"),
180     wxT("n"),
181     wxT("o"),
182     wxT("p"),
183     wxT("q"),
184     wxT("r"),
185     wxT("s"),
186     wxT("t"),
187     wxT("u"),
188     wxT("v"),
189     wxT("w"),
190     wxT("x"),
191     wxT("y"),
192     wxT("z"),
193     wxT("+"),
194     wxT("="),
195     wxT("-"),
196     wxT(","),
197     wxT("."),
198     wxT("<"),
199     wxT(">"),
200     wxT("`"),
201     wxT("/"),
202     wxT(";"),
203     wxT("'"),
204     wxT("\\"),
205     wxT("["),
206     wxT("]"),
207     wxT("*")
208 };
209
210 KeyConfigControl::KeyConfigControl( module_config_t *p_item, wxWindow *parent )
211   : ConfigControl( p_item, parent )
212 {
213     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
214     alt = new wxCheckBox( this, -1, wxU(_("Alt")) );
215     alt->SetValue( p_item->i_value & KEY_MODIFIER_ALT );
216     ctrl = new wxCheckBox( this, -1, wxU(_("Ctrl")) );
217     ctrl->SetValue( p_item->i_value & KEY_MODIFIER_CTRL );
218     shift = new wxCheckBox( this, -1, wxU(_("Shift")) );
219     shift->SetValue( p_item->i_value & KEY_MODIFIER_SHIFT );
220     combo = new wxComboBox( this, -1, wxT(""), wxDefaultPosition,
221                             wxDefaultSize, WXSIZEOF(KeysList), KeysList,
222                             wxCB_READONLY );
223     for( unsigned int i = 0; i < WXSIZEOF(KeysList); i++ )
224     {
225         combo->SetClientData( i, (void*)keys[i].i_key_code );
226         if( keys[i].i_key_code ==
227             ( ((unsigned int)p_item->i_value) & ~KEY_MODIFIER ) )
228         {
229             combo->SetSelection( i );
230             combo->SetValue( wxU(_(keys[i].psz_key_string)) );
231         }
232     }
233
234     sizer->Add( label, 2, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
235     sizer->Add( alt,   1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
236     sizer->Add( ctrl,  1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
237     sizer->Add( shift, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
238     sizer->Add( combo, 2, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
239     sizer->Layout();
240     this->SetSizerAndFit( sizer );
241 }
242
243 KeyConfigControl::~KeyConfigControl()
244 {
245     ;
246 }
247
248 int KeyConfigControl::GetIntValue()
249 {
250     int result = 0;
251     if( alt->IsChecked() )
252     {
253         result |= KEY_MODIFIER_ALT;
254     }
255     if( ctrl->IsChecked() )
256     {
257         result |= KEY_MODIFIER_CTRL;
258     }
259     if( shift->IsChecked() )
260     {
261         result |= KEY_MODIFIER_SHIFT;
262     }
263     int selected = combo->GetSelection();
264     if( selected != -1 )
265     {
266         result |= (int)combo->GetClientData( selected );
267     }
268     return result;
269 }
270
271 /*****************************************************************************
272  * ModuleConfigControl implementation
273  *****************************************************************************/
274 ModuleConfigControl::ModuleConfigControl( vlc_object_t *p_this,
275                                           module_config_t *p_item,
276                                           wxWindow *parent )
277   : ConfigControl( p_item, parent )
278 {
279     vlc_list_t *p_list;
280     module_t *p_parser;
281
282     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
283     combo = new wxComboBox( this, -1, wxU(p_item->psz_value),
284                             wxDefaultPosition, wxDefaultSize,
285                             0, NULL, wxCB_READONLY | wxCB_SORT );
286
287     /* build a list of available modules */
288     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
289     combo->Append( wxU(_("Default")), (void *)NULL );
290     combo->SetSelection( 0 );
291     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
292     {
293         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
294
295         if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
296         {
297             combo->Append( wxU(p_parser->psz_longname),
298                            p_parser->psz_object_name );
299             if( p_item->psz_value && !strcmp(p_item->psz_value, 
300                                              p_parser->psz_object_name) )
301                 combo->SetValue( wxU(p_parser->psz_longname) );
302         }
303     }
304     vlc_list_release( p_list );
305
306     combo->SetToolTip( wxU(p_item->psz_longtext) );
307     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
308     sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
309     sizer->Layout();
310     this->SetSizerAndFit( sizer );
311 }
312
313 ModuleConfigControl::~ModuleConfigControl()
314 {
315     ;
316 }
317
318 wxString ModuleConfigControl::GetPszValue()
319 {
320     return wxU( (char *)combo->GetClientData( combo->GetSelection() ));
321 }
322
323 /*****************************************************************************
324  * StringConfigControl implementation
325  *****************************************************************************/
326 StringConfigControl::StringConfigControl( module_config_t *p_item,
327                                           wxWindow *parent )
328   : ConfigControl( p_item, parent )
329 {
330     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
331     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
332     textctrl = new wxTextCtrl( this, -1, 
333                                wxU(p_item->psz_value),
334                                wxDefaultPosition,
335                                wxDefaultSize,
336                                wxTE_PROCESS_ENTER);
337     textctrl->SetToolTip( wxU(p_item->psz_longtext) );
338     sizer->Add( textctrl, 1, wxALL, 5 );
339     sizer->Layout();
340     this->SetSizerAndFit( sizer );
341 }
342
343 StringConfigControl::~StringConfigControl()
344 {
345     ;
346 }
347
348 wxString StringConfigControl::GetPszValue()
349 {
350     return textctrl->GetValue();
351 }
352
353 /*****************************************************************************
354  * StringListConfigControl implementation
355  *****************************************************************************/
356 StringListConfigControl::StringListConfigControl( module_config_t *p_item,
357                                                   wxWindow *parent )
358   : ConfigControl( p_item, parent )
359 {
360     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
361     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
362     combo = new wxComboBox( this, -1, wxU(p_item->psz_value),
363                             wxDefaultPosition, wxDefaultSize,
364                             0, NULL, wxCB_READONLY );
365
366     /* build a list of available options */
367     for( int i_index = 0; p_item->ppsz_list[i_index];
368          i_index++ )
369     {
370         combo->Append( wxU(p_item->ppsz_list[i_index]) );
371         if( p_item->psz_value && !strcmp( p_item->psz_value,
372                                           p_item->ppsz_list[i_index] ) )
373             combo->SetSelection( i_index );
374     }
375
376     if( p_item->psz_value )
377         combo->SetValue( wxU(p_item->psz_value) );
378     combo->SetToolTip( wxU(p_item->psz_longtext) );
379     sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );    
380     sizer->Layout();
381     this->SetSizerAndFit( sizer );
382 }
383
384 StringListConfigControl::~StringListConfigControl()
385 {
386     ;
387 }
388
389 wxString StringListConfigControl::GetPszValue()
390 {
391     return combo->GetStringSelection();
392 }
393
394 /*****************************************************************************
395  * FileConfigControl implementation
396  *****************************************************************************/
397 FileConfigControl::FileConfigControl( module_config_t *p_item,
398                                       wxWindow *parent )
399   : ConfigControl( p_item, parent )
400 {
401     directory = p_item->i_type == CONFIG_ITEM_DIRECTORY;
402     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
403     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
404     textctrl = new wxTextCtrl( this, -1, 
405                                wxU(p_item->psz_value),
406                                wxDefaultPosition,
407                                wxDefaultSize,
408                                wxTE_PROCESS_ENTER);
409     textctrl->SetToolTip( wxU(p_item->psz_longtext) );
410     sizer->Add( textctrl, 1, wxALL, 5 );
411     browse = new wxButton( this, wxID_HIGHEST, wxU(_("Browse...")) );
412     sizer->Add( browse, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
413     sizer->Layout();
414     this->SetSizerAndFit( sizer );
415 }
416
417 BEGIN_EVENT_TABLE(FileConfigControl, wxPanel)
418     /* Button events */
419     EVT_BUTTON(wxID_HIGHEST, FileConfigControl::OnBrowse)
420 END_EVENT_TABLE()
421
422 void FileConfigControl::OnBrowse( wxCommandEvent& event )
423 {
424     if( directory )
425     {
426         wxDirDialog dialog( this, wxU(_("Choose Directory")) );
427
428         if( dialog.ShowModal() == wxID_OK )
429         {
430             textctrl->SetValue( dialog.GetPath() );      
431         }
432     }
433     else
434     {
435         wxFileDialog dialog( this, wxU(_("Choose File")),
436                              wxT(""), wxT(""), wxT("*.*"),
437 #if defined( __WXMSW__ )
438                              wxOPEN
439 #else
440                              wxOPEN | wxSAVE
441 #endif
442                            );
443     }
444 }
445
446 FileConfigControl::~FileConfigControl()
447 {
448     ;
449 }
450     
451 wxString FileConfigControl::GetPszValue()
452 {
453     return textctrl->GetValue();
454 }
455
456 /*****************************************************************************
457  * IntegerConfigControl implementation
458  *****************************************************************************/
459 IntegerConfigControl::IntegerConfigControl( module_config_t *p_item,
460                                             wxWindow *parent )
461   : ConfigControl( p_item, parent )
462 {
463     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
464     spin = new wxSpinCtrl( this, -1,
465                            wxString::Format(wxT("%d"),
466                                             p_item->i_value),
467                            wxDefaultPosition, wxDefaultSize,
468                            wxSP_ARROW_KEYS,
469                            -16000, 16000, p_item->i_value);
470     spin->SetToolTip( wxU(p_item->psz_longtext) );
471     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
472     sizer->Add( spin, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );    
473     sizer->Layout();
474     this->SetSizerAndFit( sizer );
475 }
476
477 IntegerConfigControl::~IntegerConfigControl()
478 {
479     ;
480 }
481
482 int IntegerConfigControl::GetIntValue()
483 {
484     return spin->GetValue();
485 }
486
487 /*****************************************************************************
488  * RangedIntConfigControl implementation
489  *****************************************************************************/
490 RangedIntConfigControl::RangedIntConfigControl( module_config_t *p_item,
491                                                 wxWindow *parent )
492   : ConfigControl( p_item, parent )
493 {
494     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
495     slider = new wxSlider( this, -1, p_item->i_value, p_item->i_min,
496                            p_item->i_max, wxDefaultPosition, wxDefaultSize,
497                            wxSL_LABELS | wxSL_HORIZONTAL );
498     slider->SetToolTip( wxU(p_item->psz_longtext) );
499     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
500     sizer->Add( slider, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );    
501     sizer->Layout();
502     this->SetSizerAndFit( sizer );
503 }
504
505 RangedIntConfigControl::~RangedIntConfigControl()
506 {
507     ;
508 }
509
510 int RangedIntConfigControl::GetIntValue()
511 {
512     return slider->GetValue();
513 }
514
515 /*****************************************************************************
516  * FloatConfigControl implementation
517  *****************************************************************************/
518 FloatConfigControl::FloatConfigControl( module_config_t *p_item,
519                                         wxWindow *parent )
520   : ConfigControl( p_item, parent )
521 {
522     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
523     textctrl = new wxTextCtrl( this, -1,
524                                wxString::Format(wxT("%f"),
525                                                 p_item->f_value),
526                                wxDefaultPosition, wxDefaultSize,
527                                wxTE_PROCESS_ENTER );
528     textctrl->SetToolTip( wxU(p_item->psz_longtext) );
529     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
530     sizer->Add( textctrl, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
531     sizer->Layout();
532     this->SetSizerAndFit( sizer );
533 }
534
535 FloatConfigControl::~FloatConfigControl()
536 {
537     ;
538 }
539
540 float FloatConfigControl::GetFloatValue()
541 {
542     float f_value;
543     if( (wxSscanf(textctrl->GetValue(), wxT("%f"), &f_value) == 1) )
544         return f_value;
545     else return 0.0;
546 }
547
548 /*****************************************************************************
549  * BoolConfigControl implementation
550  *****************************************************************************/
551 BoolConfigControl::BoolConfigControl( module_config_t *p_item,
552                                       wxWindow *parent )
553   : ConfigControl( p_item, parent )
554 {
555     checkbox = new wxCheckBox( this, -1, wxU(p_item->psz_text) );
556     if( p_item->i_value ) checkbox->SetValue(TRUE);
557     checkbox->SetToolTip( wxU(p_item->psz_longtext) );
558     sizer->Add( checkbox, 0, wxALL, 5 );
559     sizer->Layout();
560     this->SetSizerAndFit( sizer );
561 }
562
563 BoolConfigControl::~BoolConfigControl()
564 {
565     ;
566 }
567
568 int BoolConfigControl::GetIntValue()
569 {
570     if( checkbox->IsChecked() )
571     {
572         return 1;
573     }
574     else
575     {
576         return 0;
577     }
578 }