]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/preferences_widgets.cpp
923b75e313cd797669972d7d3fa0a4fbc3dd2887
[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.3 2003/10/20 00:09:27 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  * ConfigControl implementation
43  *****************************************************************************/
44 ConfigControl::ConfigControl( wxWindow *parent ): wxPanel( parent )
45 {
46     sizer = new wxBoxSizer( wxHORIZONTAL );
47     i_value = 0;
48     f_value = 0;
49     psz_value = NULL;
50 }
51
52 ConfigControl::~ConfigControl()
53 {
54     if( psz_value ) free( psz_value );
55 }
56
57 wxSizer *ConfigControl::Sizer()
58 {
59     return sizer;
60 }
61
62 int ConfigControl::GetIntValue()
63 {
64     return i_value;
65 }
66
67 float ConfigControl::GetFloatValue()
68 {
69     return f_value;
70 }
71
72 wxString ConfigControl::GetPszValue()
73 {
74     return wxString(psz_value ? wxU(psz_value) : wxT(""));
75 }
76
77 /*****************************************************************************
78  * KeyConfigControl implementation
79  *****************************************************************************/
80 KeyConfigControl::KeyConfigControl( module_config_t *p_item, wxWindow *parent )
81   : ConfigControl( parent )
82 {
83     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
84     alt = new wxCheckBox( this, -1, wxU(_("Alt")) );
85     alt->SetValue( p_item->i_value & KEY_MODIFIER_ALT );
86     ctrl = new wxCheckBox( this, -1, wxU(_("Ctrl")) );
87     ctrl->SetValue( p_item->i_value & KEY_MODIFIER_CTRL );
88     shift = new wxCheckBox( this, -1, wxU(_("Shift")) );
89     shift->SetValue( p_item->i_value & KEY_MODIFIER_SHIFT );
90     combo = new wxComboBox( this, -1, wxU("f"), wxDefaultPosition,
91                             wxDefaultSize, 0, NULL,
92                             wxCB_READONLY | wxCB_SORT );
93     for( unsigned int i = 0; i < sizeof(keys)/sizeof(key_descriptor_s); i++ )
94     {
95         /* HPReg says casting the int to void * is fine */
96         combo->Append( wxU(_(keys[i].psz_key_string)),
97                        (void*)keys[i].i_key_code );
98         if( keys[i].i_key_code == ( p_item->i_value & ~KEY_MODIFIER ) )
99         {
100             combo->SetSelection( i );
101         }
102     }
103     sizer->Add( label, 2, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
104     sizer->Add( alt,   1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
105     sizer->Add( ctrl,  1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
106     sizer->Add( shift, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
107     sizer->Add( combo, 2, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
108     sizer->Layout();
109     this->SetSizerAndFit( sizer );
110 }
111
112 KeyConfigControl::~KeyConfigControl()
113 {
114     ;
115 }
116
117 int KeyConfigControl::GetIntValue()
118 {
119     int result = 0;
120     if( alt->IsChecked() )
121     {
122         result |= KEY_MODIFIER_ALT;
123     }
124     if( ctrl->IsChecked() )
125     {
126         result |= KEY_MODIFIER_CTRL;
127     }
128     if( shift->IsChecked() )
129     {
130         result |= KEY_MODIFIER_SHIFT;
131     }
132     int selected = combo->GetSelection();
133     if( selected != -1 )
134     {
135         result |= (int)combo->GetClientData( selected );
136     }
137     return result;
138 }
139
140 /*****************************************************************************
141  * ModuleConfigControl implementation
142  *****************************************************************************/
143 ModuleConfigControl::ModuleConfigControl( intf_thread_t *p_intf,
144                                           module_config_t *p_item,
145                                           wxWindow *parent )
146   : ConfigControl( parent )
147 {
148     vlc_list_t *p_list;
149     module_t *p_parser;
150     
151     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
152     combo = new wxComboBox( this, -1, wxU(p_item->psz_value),
153                             wxDefaultPosition, wxDefaultSize,
154                             0, NULL, wxCB_READONLY | wxCB_SORT );
155     
156     /* build a list of available modules */
157     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
158     combo->Append( wxU(_("Default")), (void *)NULL );
159     combo->SetSelection( 0 );
160     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
161     {
162         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
163         
164         if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
165         {
166             combo->Append( wxU(p_parser->psz_longname),
167                            p_parser->psz_object_name );
168             if( p_item->psz_value && !strcmp(p_item->psz_value, 
169                                              p_parser->psz_object_name) )
170                 combo->SetValue( wxU(p_parser->psz_longname) );
171         }
172     }
173     vlc_list_release( p_list );
174     
175     combo->SetToolTip( wxU(p_item->psz_longtext) );
176     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
177     sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
178     sizer->Layout();
179     this->SetSizerAndFit( sizer );
180 }
181
182 ModuleConfigControl::~ModuleConfigControl()
183 {
184     ;
185 }
186
187 wxString ModuleConfigControl::GetPszValue()
188 {
189     return combo->GetStringSelection();
190 }
191
192 /*****************************************************************************
193  * StringConfigControl implementation
194  *****************************************************************************/
195 StringConfigControl::StringConfigControl( module_config_t *p_item,
196                                           wxWindow *parent )
197   : ConfigControl( parent )
198 {
199     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
200     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
201     textctrl = new wxTextCtrl( this, -1, 
202                                wxU(p_item->psz_value),
203                                wxDefaultPosition,
204                                wxDefaultSize,
205                                wxTE_PROCESS_ENTER);
206     textctrl->SetToolTip( wxU(p_item->psz_longtext) );
207     sizer->Add( textctrl, 1, wxALL, 5 );
208     sizer->Layout();
209     this->SetSizerAndFit( sizer );
210 }
211
212 StringConfigControl::~StringConfigControl()
213 {
214     ;
215 }
216
217 wxString StringConfigControl::GetPszValue()
218 {
219     return textctrl->GetValue();
220 }
221
222 /*****************************************************************************
223  * StringListConfigControl implementation
224  *****************************************************************************/
225 StringListConfigControl::StringListConfigControl( module_config_t *p_item,
226                                                   wxWindow *parent )
227   : ConfigControl( parent )
228 {
229     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
230     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
231     combo = new wxComboBox( this, -1, wxU(p_item->psz_value),
232                             wxDefaultPosition, wxDefaultSize,
233                             0, NULL, wxCB_READONLY|wxCB_SORT );
234     
235     /* build a list of available options */
236     for( int i_index = 0; p_item->ppsz_list[i_index];
237          i_index++ )
238     {
239         combo->Append( wxU(p_item->ppsz_list[i_index]) );
240         if( p_item->psz_value && !strcmp( p_item->psz_value,
241                                           p_item->ppsz_list[i_index] ) )
242             combo->SetSelection( i_index );
243     }
244     
245     if( p_item->psz_value )
246         combo->SetValue( wxU(p_item->psz_value) );
247     combo->SetToolTip( wxU(p_item->psz_longtext) );
248     sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );    
249     sizer->Layout();
250     this->SetSizerAndFit( sizer );
251 }
252
253 StringListConfigControl::~StringListConfigControl()
254 {
255     ;
256 }
257
258 wxString StringListConfigControl::GetPszValue()
259 {
260     return combo->GetStringSelection();
261 }
262
263 /*****************************************************************************
264  * FileConfigControl implementation
265  *****************************************************************************/
266 FileConfigControl::FileConfigControl( module_config_t *p_item,
267                                       wxWindow *parent )
268   : ConfigControl( parent )
269 {
270     directory = p_item->i_type == CONFIG_ITEM_DIRECTORY;
271     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
272     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
273     textctrl = new wxTextCtrl( this, -1, 
274                                wxU(p_item->psz_value),
275                                wxDefaultPosition,
276                                wxDefaultSize,
277                                wxTE_PROCESS_ENTER);
278     textctrl->SetToolTip( wxU(p_item->psz_longtext) );
279     sizer->Add( textctrl, 1, wxALL, 5 );
280     browse = new wxButton( this, wxID_HIGHEST, wxU(_("Browse...")) );
281     sizer->Add( browse, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
282     sizer->Layout();
283     this->SetSizerAndFit( sizer );
284 }
285
286 BEGIN_EVENT_TABLE(FileConfigControl, wxPanel)
287     /* Button events */
288     EVT_BUTTON(wxID_HIGHEST, FileConfigControl::OnBrowse)
289 END_EVENT_TABLE()
290
291 void FileConfigControl::OnBrowse( wxCommandEvent& event )
292 {
293     if( directory )
294     {
295         wxDirDialog dialog( this, wxU(_("Choose Directory")) );
296
297         if( dialog.ShowModal() == wxID_OK )
298         {
299             textctrl->SetValue( dialog.GetPath() );      
300         }
301     }
302     else
303     {
304         wxFileDialog dialog( this, wxU(_("Choose File")),
305                              wxT(""), wxT(""), wxT("*.*"),
306 #if defined( __WXMSW__ )
307                              wxOPEN
308 #else
309                              wxOPEN | wxSAVE
310 #endif
311                            );
312     }
313 }
314
315 FileConfigControl::~FileConfigControl()
316 {
317     ;
318 }
319     
320 wxString FileConfigControl::GetPszValue()
321 {
322     return textctrl->GetValue();
323 }
324
325 /*****************************************************************************
326  * IntegerConfigControl implementation
327  *****************************************************************************/
328 IntegerConfigControl::IntegerConfigControl( module_config_t *p_item,
329                                             wxWindow *parent )
330   : ConfigControl( parent )
331 {
332     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
333     spin = new wxSpinCtrl( this, -1,
334                            wxString::Format(wxT("%d"),
335                                             p_item->i_value),
336                            wxDefaultPosition, wxDefaultSize,
337                            wxSP_ARROW_KEYS,
338                            -16000, 16000, p_item->i_value);
339     spin->SetToolTip( wxU(p_item->psz_longtext) );
340     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
341     sizer->Add( spin, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );    
342     sizer->Layout();
343     this->SetSizerAndFit( sizer );
344 }
345
346 IntegerConfigControl::~IntegerConfigControl()
347 {
348     ;
349 }
350
351 int IntegerConfigControl::GetIntValue()
352 {
353     return spin->GetValue();
354 }
355
356 /*****************************************************************************
357  * RangedIntConfigControl implementation
358  *****************************************************************************/
359 RangedIntConfigControl::RangedIntConfigControl( module_config_t *p_item,
360                                                 wxWindow *parent )
361   : ConfigControl( parent )
362 {
363     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
364     slider = new wxSlider( this, -1, p_item->i_value, p_item->i_min,
365                            p_item->i_max );
366     slider->SetToolTip( wxU(p_item->psz_longtext) );
367     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
368     sizer->Add( slider, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );    
369     sizer->Layout();
370     this->SetSizerAndFit( sizer );
371 }
372
373 RangedIntConfigControl::~RangedIntConfigControl()
374 {
375     ;
376 }
377
378 int RangedIntConfigControl::GetIntValue()
379 {
380     return slider->GetValue();
381 }
382
383 /*****************************************************************************
384  * FloatConfigControl implementation
385  *****************************************************************************/
386 FloatConfigControl::FloatConfigControl( module_config_t *p_item,
387                                         wxWindow *parent )
388   : ConfigControl( parent )
389 {
390     label = new wxStaticText(this, -1, wxU(p_item->psz_text));
391     textctrl = new wxTextCtrl( this, -1,
392                                wxString::Format(wxT("%f"),
393                                                 p_item->f_value),
394                                wxDefaultPosition, wxDefaultSize,
395                                wxTE_PROCESS_ENTER );
396     textctrl->SetToolTip( wxU(p_item->psz_longtext) );
397     sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
398     sizer->Add( textctrl, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
399     sizer->Layout();
400     this->SetSizerAndFit( sizer );
401 }
402
403 FloatConfigControl::~FloatConfigControl()
404 {
405     ;
406 }
407
408 float FloatConfigControl::GetFloatValue()
409 {
410     float f_value;
411     if( (wxSscanf(textctrl->GetValue(), wxT("%f"), &f_value) == 1) )
412         return f_value;
413     else return 0.0;
414 }
415
416 /*****************************************************************************
417  * BoolConfigControl implementation
418  *****************************************************************************/
419 BoolConfigControl::BoolConfigControl( module_config_t *p_item,
420                                       wxWindow *parent )
421   : ConfigControl( parent )
422 {
423     checkbox = new wxCheckBox( this, -1, wxU(p_item->psz_text) );
424     if( p_item->i_value ) checkbox->SetValue(TRUE);
425     checkbox->SetToolTip( wxU(p_item->psz_longtext) );
426     sizer->Add( checkbox, 0, wxALL, 5 );
427     sizer->Layout();
428     this->SetSizerAndFit( sizer );
429 }
430
431 BoolConfigControl::~BoolConfigControl()
432 {
433     ;
434 }
435
436 int BoolConfigControl::GetIntValue()
437 {
438     if( checkbox->IsChecked() )
439     {
440         return 1;
441     }
442     else
443     {
444         return 0;
445     }
446 }