]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/preferences_widgets.cpp
* modules/video_output/x11/xcommon.c: hotkeys handling cleanup and support for ctrl...
[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.6 2003/10/28 21:59:13 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 KeyConfigControl::KeyConfigControl( module_config_t *p_item, wxWindow *parent )
139   : ConfigControl( p_item, parent )
140 {
141     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
142     alt = new wxCheckBox( this, -1, wxU(_("Alt")) );
143     alt->SetValue( p_item->i_value & KEY_MODIFIER_ALT );
144     ctrl = new wxCheckBox( this, -1, wxU(_("Ctrl")) );
145     ctrl->SetValue( p_item->i_value & KEY_MODIFIER_CTRL );
146     shift = new wxCheckBox( this, -1, wxU(_("Shift")) );
147     shift->SetValue( p_item->i_value & KEY_MODIFIER_SHIFT );
148     combo = new wxComboBox( this, -1, wxU("f"), wxDefaultPosition,
149                             wxDefaultSize, 0, NULL,
150                             wxCB_READONLY | wxCB_SORT );
151     for( unsigned int i = 0; i < sizeof(keys)/sizeof(key_descriptor_s); i++ )
152     {
153         /* HPReg says casting the int to void * is fine */
154         combo->Append( wxU(_(keys[i].psz_key_string)),
155                        (void*)keys[i].i_key_code );
156         if( keys[i].i_key_code == ( ((unsigned int)p_item->i_value) & ~KEY_MODIFIER ) )
157         {
158             combo->SetSelection( i );
159             combo->SetValue( wxU(_(keys[i].psz_key_string)) );
160         }
161     }
162
163     sizer->Add( label, 2, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
164     sizer->Add( alt,   1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
165     sizer->Add( ctrl,  1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
166     sizer->Add( shift, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
167     sizer->Add( combo, 2, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
168     sizer->Layout();
169     this->SetSizerAndFit( sizer );
170 }
171
172 KeyConfigControl::~KeyConfigControl()
173 {
174     ;
175 }
176
177 int KeyConfigControl::GetIntValue()
178 {
179     int result = 0;
180     if( alt->IsChecked() )
181     {
182         result |= KEY_MODIFIER_ALT;
183     }
184     if( ctrl->IsChecked() )
185     {
186         result |= KEY_MODIFIER_CTRL;
187     }
188     if( shift->IsChecked() )
189     {
190         result |= KEY_MODIFIER_SHIFT;
191     }
192     int selected = combo->GetSelection();
193     if( selected != -1 )
194     {
195         result |= (int)combo->GetClientData( selected );
196     }
197     return result;
198 }
199
200 /*****************************************************************************
201  * ModuleConfigControl implementation
202  *****************************************************************************/
203 ModuleConfigControl::ModuleConfigControl( vlc_object_t *p_this,
204                                           module_config_t *p_item,
205                                           wxWindow *parent )
206   : ConfigControl( p_item, parent )
207 {
208     vlc_list_t *p_list;
209     module_t *p_parser;
210
211     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
212     combo = new wxComboBox( this, -1, wxU(p_item->psz_value),
213                             wxDefaultPosition, wxDefaultSize,
214                             0, NULL, wxCB_READONLY | wxCB_SORT );
215
216     /* build a list of available modules */
217     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
218     combo->Append( wxU(_("Default")), (void *)NULL );
219     combo->SetSelection( 0 );
220     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
221     {
222         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
223
224         if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
225         {
226             combo->Append( wxU(p_parser->psz_longname),
227                            p_parser->psz_object_name );
228             if( p_item->psz_value && !strcmp(p_item->psz_value, 
229                                              p_parser->psz_object_name) )
230                 combo->SetValue( wxU(p_parser->psz_longname) );
231         }
232     }
233     vlc_list_release( p_list );
234
235     combo->SetToolTip( wxU(p_item->psz_longtext) );
236     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
237     sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
238     sizer->Layout();
239     this->SetSizerAndFit( sizer );
240 }
241
242 ModuleConfigControl::~ModuleConfigControl()
243 {
244     ;
245 }
246
247 wxString ModuleConfigControl::GetPszValue()
248 {
249     return wxU( (char *)combo->GetClientData( combo->GetSelection() ));
250 }
251
252 /*****************************************************************************
253  * StringConfigControl implementation
254  *****************************************************************************/
255 StringConfigControl::StringConfigControl( module_config_t *p_item,
256                                           wxWindow *parent )
257   : ConfigControl( p_item, parent )
258 {
259     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
260     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
261     textctrl = new wxTextCtrl( this, -1, 
262                                wxU(p_item->psz_value),
263                                wxDefaultPosition,
264                                wxDefaultSize,
265                                wxTE_PROCESS_ENTER);
266     textctrl->SetToolTip( wxU(p_item->psz_longtext) );
267     sizer->Add( textctrl, 1, wxALL, 5 );
268     sizer->Layout();
269     this->SetSizerAndFit( sizer );
270 }
271
272 StringConfigControl::~StringConfigControl()
273 {
274     ;
275 }
276
277 wxString StringConfigControl::GetPszValue()
278 {
279     return textctrl->GetValue();
280 }
281
282 /*****************************************************************************
283  * StringListConfigControl implementation
284  *****************************************************************************/
285 StringListConfigControl::StringListConfigControl( module_config_t *p_item,
286                                                   wxWindow *parent )
287   : ConfigControl( p_item, parent )
288 {
289     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
290     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
291     combo = new wxComboBox( this, -1, wxU(p_item->psz_value),
292                             wxDefaultPosition, wxDefaultSize,
293                             0, NULL, wxCB_READONLY|wxCB_SORT );
294
295     /* build a list of available options */
296     for( int i_index = 0; p_item->ppsz_list[i_index];
297          i_index++ )
298     {
299         combo->Append( wxU(p_item->ppsz_list[i_index]) );
300         if( p_item->psz_value && !strcmp( p_item->psz_value,
301                                           p_item->ppsz_list[i_index] ) )
302             combo->SetSelection( i_index );
303     }
304
305     if( p_item->psz_value )
306         combo->SetValue( wxU(p_item->psz_value) );
307     combo->SetToolTip( wxU(p_item->psz_longtext) );
308     sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );    
309     sizer->Layout();
310     this->SetSizerAndFit( sizer );
311 }
312
313 StringListConfigControl::~StringListConfigControl()
314 {
315     ;
316 }
317
318 wxString StringListConfigControl::GetPszValue()
319 {
320     return combo->GetStringSelection();
321 }
322
323 /*****************************************************************************
324  * FileConfigControl implementation
325  *****************************************************************************/
326 FileConfigControl::FileConfigControl( module_config_t *p_item,
327                                       wxWindow *parent )
328   : ConfigControl( p_item, parent )
329 {
330     directory = p_item->i_type == CONFIG_ITEM_DIRECTORY;
331     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
332     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
333     textctrl = new wxTextCtrl( this, -1, 
334                                wxU(p_item->psz_value),
335                                wxDefaultPosition,
336                                wxDefaultSize,
337                                wxTE_PROCESS_ENTER);
338     textctrl->SetToolTip( wxU(p_item->psz_longtext) );
339     sizer->Add( textctrl, 1, wxALL, 5 );
340     browse = new wxButton( this, wxID_HIGHEST, wxU(_("Browse...")) );
341     sizer->Add( browse, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
342     sizer->Layout();
343     this->SetSizerAndFit( sizer );
344 }
345
346 BEGIN_EVENT_TABLE(FileConfigControl, wxPanel)
347     /* Button events */
348     EVT_BUTTON(wxID_HIGHEST, FileConfigControl::OnBrowse)
349 END_EVENT_TABLE()
350
351 void FileConfigControl::OnBrowse( wxCommandEvent& event )
352 {
353     if( directory )
354     {
355         wxDirDialog dialog( this, wxU(_("Choose Directory")) );
356
357         if( dialog.ShowModal() == wxID_OK )
358         {
359             textctrl->SetValue( dialog.GetPath() );      
360         }
361     }
362     else
363     {
364         wxFileDialog dialog( this, wxU(_("Choose File")),
365                              wxT(""), wxT(""), wxT("*.*"),
366 #if defined( __WXMSW__ )
367                              wxOPEN
368 #else
369                              wxOPEN | wxSAVE
370 #endif
371                            );
372     }
373 }
374
375 FileConfigControl::~FileConfigControl()
376 {
377     ;
378 }
379     
380 wxString FileConfigControl::GetPszValue()
381 {
382     return textctrl->GetValue();
383 }
384
385 /*****************************************************************************
386  * IntegerConfigControl implementation
387  *****************************************************************************/
388 IntegerConfigControl::IntegerConfigControl( module_config_t *p_item,
389                                             wxWindow *parent )
390   : ConfigControl( p_item, parent )
391 {
392     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
393     spin = new wxSpinCtrl( this, -1,
394                            wxString::Format(wxT("%d"),
395                                             p_item->i_value),
396                            wxDefaultPosition, wxDefaultSize,
397                            wxSP_ARROW_KEYS,
398                            -16000, 16000, p_item->i_value);
399     spin->SetToolTip( wxU(p_item->psz_longtext) );
400     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
401     sizer->Add( spin, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );    
402     sizer->Layout();
403     this->SetSizerAndFit( sizer );
404 }
405
406 IntegerConfigControl::~IntegerConfigControl()
407 {
408     ;
409 }
410
411 int IntegerConfigControl::GetIntValue()
412 {
413     return spin->GetValue();
414 }
415
416 /*****************************************************************************
417  * RangedIntConfigControl implementation
418  *****************************************************************************/
419 RangedIntConfigControl::RangedIntConfigControl( module_config_t *p_item,
420                                                 wxWindow *parent )
421   : ConfigControl( p_item, parent )
422 {
423     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
424     slider = new wxSlider( this, -1, p_item->i_value, p_item->i_min,
425                            p_item->i_max );
426     slider->SetToolTip( wxU(p_item->psz_longtext) );
427     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
428     sizer->Add( slider, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );    
429     sizer->Layout();
430     this->SetSizerAndFit( sizer );
431 }
432
433 RangedIntConfigControl::~RangedIntConfigControl()
434 {
435     ;
436 }
437
438 int RangedIntConfigControl::GetIntValue()
439 {
440     return slider->GetValue();
441 }
442
443 /*****************************************************************************
444  * FloatConfigControl implementation
445  *****************************************************************************/
446 FloatConfigControl::FloatConfigControl( module_config_t *p_item,
447                                         wxWindow *parent )
448   : ConfigControl( p_item, parent )
449 {
450     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
451     textctrl = new wxTextCtrl( this, -1,
452                                wxString::Format(wxT("%f"),
453                                                 p_item->f_value),
454                                wxDefaultPosition, wxDefaultSize,
455                                wxTE_PROCESS_ENTER );
456     textctrl->SetToolTip( wxU(p_item->psz_longtext) );
457     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
458     sizer->Add( textctrl, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
459     sizer->Layout();
460     this->SetSizerAndFit( sizer );
461 }
462
463 FloatConfigControl::~FloatConfigControl()
464 {
465     ;
466 }
467
468 float FloatConfigControl::GetFloatValue()
469 {
470     float f_value;
471     if( (wxSscanf(textctrl->GetValue(), wxT("%f"), &f_value) == 1) )
472         return f_value;
473     else return 0.0;
474 }
475
476 /*****************************************************************************
477  * BoolConfigControl implementation
478  *****************************************************************************/
479 BoolConfigControl::BoolConfigControl( module_config_t *p_item,
480                                       wxWindow *parent )
481   : ConfigControl( p_item, parent )
482 {
483     checkbox = new wxCheckBox( this, -1, wxU(p_item->psz_text) );
484     if( p_item->i_value ) checkbox->SetValue(TRUE);
485     checkbox->SetToolTip( wxU(p_item->psz_longtext) );
486     sizer->Add( checkbox, 0, wxALL, 5 );
487     sizer->Layout();
488     this->SetSizerAndFit( sizer );
489 }
490
491 BoolConfigControl::~BoolConfigControl()
492 {
493     ;
494 }
495
496 int BoolConfigControl::GetIntValue()
497 {
498     if( checkbox->IsChecked() )
499     {
500         return 1;
501     }
502     else
503     {
504         return 0;
505     }
506 }