1 /*****************************************************************************
2 * preferences_widgets.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: preferences_widgets.cpp,v 1.12 2003/11/05 17:46:21 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Sigmund Augdal <sigmunau@idi.ntnu.no>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
29 #include <errno.h> /* ENOMEM */
30 #include <string.h> /* strerror() */
38 #include "wxwindows.h"
39 #include "preferences_widgets.h"
41 /*****************************************************************************
42 * CreateConfigControl wrapper
43 *****************************************************************************/
44 ConfigControl *CreateConfigControl( vlc_object_t *p_this,
45 module_config_t *p_item, wxWindow *parent )
47 ConfigControl *p_control = NULL;
49 switch( p_item->i_type )
51 case CONFIG_ITEM_MODULE:
52 p_control = new ModuleConfigControl( p_this, p_item, parent );
55 case CONFIG_ITEM_STRING:
58 p_control = new StringConfigControl( p_this, p_item, parent );
62 p_control = new StringListConfigControl( p_this, p_item, parent );
66 case CONFIG_ITEM_FILE:
67 case CONFIG_ITEM_DIRECTORY:
68 p_control = new FileConfigControl( p_this, p_item, parent );
71 case CONFIG_ITEM_INTEGER:
74 p_control = new IntegerListConfigControl( p_this, p_item, parent );
76 else if( p_item->i_min != 0 || p_item->i_max != 0 )
78 p_control = new RangedIntConfigControl( p_this, p_item, parent );
82 p_control = new IntegerConfigControl( p_this, p_item, parent );
87 p_control = new KeyConfigControl( p_this, p_item, parent );
90 case CONFIG_ITEM_FLOAT:
91 p_control = new FloatConfigControl( p_this, p_item, parent );
94 case CONFIG_ITEM_BOOL:
95 p_control = new BoolConfigControl( p_this, p_item, parent );
105 /*****************************************************************************
106 * ConfigControl implementation
107 *****************************************************************************/
108 ConfigControl::ConfigControl( vlc_object_t *_p_this,
109 module_config_t *p_item, wxWindow *parent )
110 : wxPanel( parent ), p_this( _p_this ), name( wxU(p_item->psz_name) ),
111 i_type( p_item->i_type ), b_advanced( p_item->b_advanced )
113 sizer = new wxBoxSizer( wxHORIZONTAL );
116 ConfigControl::~ConfigControl()
120 wxSizer *ConfigControl::Sizer()
125 wxString ConfigControl::GetName()
130 int ConfigControl::GetType()
135 vlc_bool_t ConfigControl::IsAdvanced()
140 /*****************************************************************************
141 * KeyConfigControl implementation
142 *****************************************************************************/
143 static wxString KeysList[] =
215 KeyConfigControl::KeyConfigControl( vlc_object_t *p_this,
216 module_config_t *p_item, wxWindow *parent )
217 : ConfigControl( p_this, p_item, parent )
219 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
220 alt = new wxCheckBox( this, -1, wxU(_("Alt")) );
221 alt->SetValue( p_item->i_value & KEY_MODIFIER_ALT );
222 ctrl = new wxCheckBox( this, -1, wxU(_("Ctrl")) );
223 ctrl->SetValue( p_item->i_value & KEY_MODIFIER_CTRL );
224 shift = new wxCheckBox( this, -1, wxU(_("Shift")) );
225 shift->SetValue( p_item->i_value & KEY_MODIFIER_SHIFT );
226 combo = new wxComboBox( this, -1, wxT(""), wxDefaultPosition,
227 wxDefaultSize, WXSIZEOF(KeysList), KeysList,
229 for( unsigned int i = 0; i < WXSIZEOF(KeysList); i++ )
231 combo->SetClientData( i, (void*)keys[i].i_key_code );
232 if( keys[i].i_key_code ==
233 ( ((unsigned int)p_item->i_value) & ~KEY_MODIFIER ) )
235 combo->SetSelection( i );
236 combo->SetValue( wxU(_(keys[i].psz_key_string)) );
240 sizer->Add( label, 2, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
241 sizer->Add( alt, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
242 sizer->Add( ctrl, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
243 sizer->Add( shift, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
244 sizer->Add( combo, 2, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
246 this->SetSizerAndFit( sizer );
249 KeyConfigControl::~KeyConfigControl()
254 int KeyConfigControl::GetIntValue()
257 if( alt->IsChecked() )
259 result |= KEY_MODIFIER_ALT;
261 if( ctrl->IsChecked() )
263 result |= KEY_MODIFIER_CTRL;
265 if( shift->IsChecked() )
267 result |= KEY_MODIFIER_SHIFT;
269 int selected = combo->GetSelection();
272 result |= (int)combo->GetClientData( selected );
277 /*****************************************************************************
278 * ModuleConfigControl implementation
279 *****************************************************************************/
280 ModuleConfigControl::ModuleConfigControl( vlc_object_t *p_this,
281 module_config_t *p_item,
283 : ConfigControl( p_this, p_item, parent )
288 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
289 combo = new wxComboBox( this, -1, wxU(p_item->psz_value),
290 wxDefaultPosition, wxDefaultSize,
291 0, NULL, wxCB_READONLY | wxCB_SORT );
293 /* build a list of available modules */
294 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
295 combo->Append( wxU(_("Default")), (void *)NULL );
296 combo->SetSelection( 0 );
297 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
299 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
301 if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
303 combo->Append( wxU(p_parser->psz_longname),
304 p_parser->psz_object_name );
305 if( p_item->psz_value && !strcmp(p_item->psz_value,
306 p_parser->psz_object_name) )
307 combo->SetValue( wxU(p_parser->psz_longname) );
310 vlc_list_release( p_list );
312 combo->SetToolTip( wxU(p_item->psz_longtext) );
313 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
314 sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
316 this->SetSizerAndFit( sizer );
319 ModuleConfigControl::~ModuleConfigControl()
324 wxString ModuleConfigControl::GetPszValue()
326 return wxU( (char *)combo->GetClientData( combo->GetSelection() ));
329 /*****************************************************************************
330 * StringConfigControl implementation
331 *****************************************************************************/
332 StringConfigControl::StringConfigControl( vlc_object_t *p_this,
333 module_config_t *p_item,
335 : ConfigControl( p_this, p_item, parent )
337 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
338 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
339 textctrl = new wxTextCtrl( this, -1,
340 wxU(p_item->psz_value),
344 textctrl->SetToolTip( wxU(p_item->psz_longtext) );
345 sizer->Add( textctrl, 1, wxALL, 5 );
347 this->SetSizerAndFit( sizer );
350 StringConfigControl::~StringConfigControl()
355 wxString StringConfigControl::GetPszValue()
357 return textctrl->GetValue();
360 /*****************************************************************************
361 * StringListConfigControl implementation
362 *****************************************************************************/
363 StringListConfigControl::StringListConfigControl( vlc_object_t *p_this,
364 module_config_t *p_item,
366 : ConfigControl( p_this, p_item, parent ), psz_name( NULL )
368 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
369 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
370 combo = new wxComboBox( this, -1, wxT(""),
371 wxDefaultPosition, wxDefaultSize,
372 0, NULL, wxCB_READONLY );
373 UpdateCombo( p_item );
375 combo->SetToolTip( wxU(p_item->psz_longtext) );
376 sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
378 if( p_item->pf_list_update )
381 new wxButton( this, wxID_HIGHEST, wxU(_("Refresh")) );
382 sizer->Add( refresh, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
384 psz_name = strdup( p_item->psz_name );
385 pf_list_update = p_item->pf_list_update;
389 this->SetSizerAndFit( sizer );
392 StringListConfigControl::~StringListConfigControl()
394 if( psz_name ) free( psz_name );
397 void StringListConfigControl::UpdateCombo( module_config_t *p_item )
399 /* build a list of available options */
400 for( int i_index = 0; i_index < p_item->i_list; i_index++ )
402 combo->Append( ( p_item->ppsz_list_text &&
403 p_item->ppsz_list_text[i_index] ) ?
404 wxU(p_item->ppsz_list_text[i_index]) :
405 wxU(p_item->ppsz_list[i_index]) );
406 combo->SetClientData( i_index, (void *)p_item->ppsz_list[i_index] );
407 if( ( p_item->psz_value &&
408 !strcmp( p_item->psz_value, p_item->ppsz_list[i_index] ) ) ||
409 ( !p_item->psz_value && !*p_item->ppsz_list[i_index] ) )
411 combo->SetSelection( i_index );
412 combo->SetValue( ( p_item->ppsz_list_text &&
413 p_item->ppsz_list_text[i_index] ) ?
414 wxU(p_item->ppsz_list_text[i_index]) :
415 wxU(p_item->ppsz_list[i_index]) );
420 BEGIN_EVENT_TABLE(StringListConfigControl, wxPanel)
422 EVT_BUTTON(wxID_HIGHEST, StringListConfigControl::OnRefresh)
425 void StringListConfigControl::OnRefresh( wxCommandEvent& event )
430 module_config_t *p_item;
432 pf_list_update( p_this, psz_name, val, val, 0 );
433 p_item = config_FindConfig( p_this, psz_name );
436 UpdateCombo( p_item );
440 wxString StringListConfigControl::GetPszValue()
442 int selected = combo->GetSelection();
445 return (char *)combo->GetClientData( selected );
450 /*****************************************************************************
451 * FileConfigControl implementation
452 *****************************************************************************/
453 FileConfigControl::FileConfigControl( vlc_object_t *p_this,
454 module_config_t *p_item,
456 : ConfigControl( p_this, p_item, parent )
458 directory = p_item->i_type == CONFIG_ITEM_DIRECTORY;
459 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
460 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
461 textctrl = new wxTextCtrl( this, -1,
462 wxU(p_item->psz_value),
466 textctrl->SetToolTip( wxU(p_item->psz_longtext) );
467 sizer->Add( textctrl, 1, wxALL, 5 );
468 browse = new wxButton( this, wxID_HIGHEST, wxU(_("Browse...")) );
469 sizer->Add( browse, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
471 this->SetSizerAndFit( sizer );
474 BEGIN_EVENT_TABLE(FileConfigControl, wxPanel)
476 EVT_BUTTON(wxID_HIGHEST, FileConfigControl::OnBrowse)
479 void FileConfigControl::OnBrowse( wxCommandEvent& event )
483 wxDirDialog dialog( this, wxU(_("Choose Directory")) );
485 if( dialog.ShowModal() == wxID_OK )
487 textctrl->SetValue( dialog.GetPath() );
492 wxFileDialog dialog( this, wxU(_("Choose File")),
493 wxT(""), wxT(""), wxT("*.*"),
494 #if defined( __WXMSW__ )
503 FileConfigControl::~FileConfigControl()
508 wxString FileConfigControl::GetPszValue()
510 return textctrl->GetValue();
513 /*****************************************************************************
514 * IntegerConfigControl implementation
515 *****************************************************************************/
516 IntegerConfigControl::IntegerConfigControl( vlc_object_t *p_this,
517 module_config_t *p_item,
519 : ConfigControl( p_this, p_item, parent )
521 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
522 spin = new wxSpinCtrl( this, -1,
523 wxString::Format(wxT("%d"),
525 wxDefaultPosition, wxDefaultSize,
527 -16000, 16000, p_item->i_value);
528 spin->SetToolTip( wxU(p_item->psz_longtext) );
529 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
530 sizer->Add( spin, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
532 this->SetSizerAndFit( sizer );
535 IntegerConfigControl::~IntegerConfigControl()
540 int IntegerConfigControl::GetIntValue()
542 return spin->GetValue();
545 /*****************************************************************************
546 * IntegerListConfigControl implementation
547 *****************************************************************************/
548 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *p_this,
549 module_config_t *p_item,
551 : ConfigControl( p_this, p_item, parent ), psz_name( NULL )
553 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
554 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
555 combo = new wxComboBox( this, -1, wxT(""),
556 wxDefaultPosition, wxDefaultSize,
557 0, NULL, wxCB_READONLY );
559 UpdateCombo( p_item );
561 combo->SetToolTip( wxU(p_item->psz_longtext) );
562 sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
564 if( p_item->pf_list_update )
567 new wxButton( this, wxID_HIGHEST, wxU(_("Refresh")) );
568 sizer->Add( refresh, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
570 psz_name = strdup( p_item->psz_name );
571 pf_list_update = p_item->pf_list_update;
575 this->SetSizerAndFit( sizer );
578 IntegerListConfigControl::~IntegerListConfigControl()
580 if( psz_name ) free( psz_name );
583 void IntegerListConfigControl::UpdateCombo( module_config_t *p_item )
585 /* build a list of available options */
586 for( int i_index = 0; i_index < p_item->i_list; i_index++ )
588 combo->Append( ( p_item->ppsz_list_text &&
589 p_item->ppsz_list_text[i_index] ) ?
590 wxU(p_item->ppsz_list_text[i_index]) :
591 wxString::Format(wxT("%i"),
592 p_item->pi_list[i_index]) );
593 combo->SetClientData( i_index, (void *)p_item->pi_list[i_index] );
594 if( p_item->i_value == p_item->pi_list[i_index] )
596 combo->SetSelection( i_index );
597 combo->SetValue( ( p_item->ppsz_list_text &&
598 p_item->ppsz_list_text[i_index] ) ?
599 wxU(p_item->ppsz_list_text[i_index]) :
600 wxString::Format(wxT("%i"),
601 p_item->pi_list[i_index]) );
606 BEGIN_EVENT_TABLE(IntegerListConfigControl, wxPanel)
608 EVT_BUTTON(wxID_HIGHEST, IntegerListConfigControl::OnRefresh)
611 void IntegerListConfigControl::OnRefresh( wxCommandEvent& event )
616 module_config_t *p_item;
618 pf_list_update( p_this, psz_name, val, val, 0 );
619 p_item = config_FindConfig( p_this, psz_name );
622 UpdateCombo( p_item );
626 int IntegerListConfigControl::GetIntValue()
628 int selected = combo->GetSelection();
631 return (int)combo->GetClientData( selected );
636 /*****************************************************************************
637 * RangedIntConfigControl implementation
638 *****************************************************************************/
639 RangedIntConfigControl::RangedIntConfigControl( vlc_object_t *p_this,
640 module_config_t *p_item,
642 : ConfigControl( p_this, p_item, parent )
644 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
645 slider = new wxSlider( this, -1, p_item->i_value, p_item->i_min,
646 p_item->i_max, wxDefaultPosition, wxDefaultSize,
647 wxSL_LABELS | wxSL_HORIZONTAL );
648 slider->SetToolTip( wxU(p_item->psz_longtext) );
649 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
650 sizer->Add( slider, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
652 this->SetSizerAndFit( sizer );
655 RangedIntConfigControl::~RangedIntConfigControl()
660 int RangedIntConfigControl::GetIntValue()
662 return slider->GetValue();
665 /*****************************************************************************
666 * FloatConfigControl implementation
667 *****************************************************************************/
668 FloatConfigControl::FloatConfigControl( vlc_object_t *p_this,
669 module_config_t *p_item,
671 : ConfigControl( p_this, p_item, parent )
673 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
674 textctrl = new wxTextCtrl( this, -1,
675 wxString::Format(wxT("%f"),
677 wxDefaultPosition, wxDefaultSize,
678 wxTE_PROCESS_ENTER );
679 textctrl->SetToolTip( wxU(p_item->psz_longtext) );
680 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
681 sizer->Add( textctrl, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
683 this->SetSizerAndFit( sizer );
686 FloatConfigControl::~FloatConfigControl()
691 float FloatConfigControl::GetFloatValue()
694 if( (wxSscanf(textctrl->GetValue(), wxT("%f"), &f_value) == 1) )
699 /*****************************************************************************
700 * BoolConfigControl implementation
701 *****************************************************************************/
702 BoolConfigControl::BoolConfigControl( vlc_object_t *p_this,
703 module_config_t *p_item,
705 : ConfigControl( p_this, p_item, parent )
707 checkbox = new wxCheckBox( this, -1, wxU(p_item->psz_text) );
708 if( p_item->i_value ) checkbox->SetValue(TRUE);
709 checkbox->SetToolTip( wxU(p_item->psz_longtext) );
710 sizer->Add( checkbox, 0, wxALL, 5 );
712 this->SetSizerAndFit( sizer );
715 BoolConfigControl::~BoolConfigControl()
720 int BoolConfigControl::GetIntValue()
722 if( checkbox->IsChecked() )