1 /*****************************************************************************
2 * preferences_widgets.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2004 VideoLAN
5 * $Id: preferences_widgets.cpp,v 1.23 2004/01/29 17:04:01 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 ),
111 pf_update_callback( NULL ), p_update_data( NULL ),
112 name( wxU(p_item->psz_name) ), i_type( p_item->i_type ),
113 b_advanced( p_item->b_advanced )
116 sizer = new wxBoxSizer( wxHORIZONTAL );
119 ConfigControl::~ConfigControl()
123 wxSizer *ConfigControl::Sizer()
128 wxString ConfigControl::GetName()
133 int ConfigControl::GetType()
138 vlc_bool_t ConfigControl::IsAdvanced()
143 void ConfigControl::SetUpdateCallback( void (*p_callback)( void * ),
146 pf_update_callback = p_callback;
147 p_update_data = p_data;
150 void ConfigControl::OnUpdate( wxCommandEvent& WXUNUSED(event) )
152 if( pf_update_callback )
154 pf_update_callback( p_update_data );
158 /*****************************************************************************
159 * KeyConfigControl implementation
160 *****************************************************************************/
161 static wxString KeysList[] =
233 KeyConfigControl::KeyConfigControl( vlc_object_t *p_this,
234 module_config_t *p_item, wxWindow *parent )
235 : ConfigControl( p_this, p_item, parent )
237 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
238 alt = new wxCheckBox( this, -1, wxU(_("Alt")) );
239 alt->SetValue( p_item->i_value & KEY_MODIFIER_ALT );
240 ctrl = new wxCheckBox( this, -1, wxU(_("Ctrl")) );
241 ctrl->SetValue( p_item->i_value & KEY_MODIFIER_CTRL );
242 shift = new wxCheckBox( this, -1, wxU(_("Shift")) );
243 shift->SetValue( p_item->i_value & KEY_MODIFIER_SHIFT );
244 combo = new wxComboBox( this, -1, wxT(""), wxDefaultPosition,
245 wxDefaultSize, WXSIZEOF(KeysList), KeysList,
247 for( unsigned int i = 0; i < WXSIZEOF(KeysList); i++ )
249 combo->SetClientData( i, (void*)vlc_keys[i].i_key_code );
250 if( (unsigned int)vlc_keys[i].i_key_code ==
251 ( ((unsigned int)p_item->i_value) & ~KEY_MODIFIER ) )
253 combo->SetSelection( i );
254 combo->SetValue( wxU(_(vlc_keys[i].psz_key_string)) );
258 sizer->Add( label, 2, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
259 sizer->Add( alt, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
260 sizer->Add( ctrl, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
261 sizer->Add( shift, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
262 sizer->Add( combo, 2, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
264 this->SetSizerAndFit( sizer );
267 KeyConfigControl::~KeyConfigControl()
272 int KeyConfigControl::GetIntValue()
275 if( alt->IsChecked() )
277 result |= KEY_MODIFIER_ALT;
279 if( ctrl->IsChecked() )
281 result |= KEY_MODIFIER_CTRL;
283 if( shift->IsChecked() )
285 result |= KEY_MODIFIER_SHIFT;
287 int selected = combo->GetSelection();
290 result |= (int)combo->GetClientData( selected );
295 /*****************************************************************************
296 * ModuleConfigControl implementation
297 *****************************************************************************/
298 ModuleConfigControl::ModuleConfigControl( vlc_object_t *p_this,
299 module_config_t *p_item,
301 : ConfigControl( p_this, p_item, parent )
306 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
307 combo = new wxComboBox( this, -1, wxL2U(p_item->psz_value),
308 wxDefaultPosition, wxDefaultSize,
309 0, NULL, wxCB_READONLY | wxCB_SORT );
311 /* build a list of available modules */
312 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
313 combo->Append( wxU(_("Default")), (void *)NULL );
314 combo->SetSelection( 0 );
315 for( int i_index = 0; i_index < p_list->i_count; i_index++ )
317 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
319 if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )
321 combo->Append( wxU(p_parser->psz_longname),
322 p_parser->psz_object_name );
323 if( p_item->psz_value && !strcmp(p_item->psz_value,
324 p_parser->psz_object_name) )
325 combo->SetValue( wxU(p_parser->psz_longname) );
328 vlc_list_release( p_list );
330 combo->SetToolTip( wxU(p_item->psz_longtext) );
331 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
332 sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
334 this->SetSizerAndFit( sizer );
337 ModuleConfigControl::~ModuleConfigControl()
342 wxString ModuleConfigControl::GetPszValue()
344 return wxU( (char *)combo->GetClientData( combo->GetSelection() ));
347 /*****************************************************************************
348 * StringConfigControl implementation
349 *****************************************************************************/
350 StringConfigControl::StringConfigControl( vlc_object_t *p_this,
351 module_config_t *p_item,
353 : ConfigControl( p_this, p_item, parent )
355 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
356 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
357 textctrl = new wxTextCtrl( this, -1,
358 wxL2U(p_item->psz_value),
362 textctrl->SetToolTip( wxU(p_item->psz_longtext) );
363 sizer->Add( textctrl, 1, wxALL, 5 );
365 this->SetSizerAndFit( sizer );
368 StringConfigControl::~StringConfigControl()
373 wxString StringConfigControl::GetPszValue()
375 return textctrl->GetValue();
378 BEGIN_EVENT_TABLE(StringConfigControl, wxPanel)
380 EVT_TEXT(-1, StringConfigControl::OnUpdate)
383 /*****************************************************************************
384 * StringListConfigControl implementation
385 *****************************************************************************/
386 StringListConfigControl::StringListConfigControl( vlc_object_t *p_this,
387 module_config_t *p_item,
389 : ConfigControl( p_this, p_item, parent )
391 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
392 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
393 combo = new wxComboBox( this, -1, wxT(""),
394 wxDefaultPosition, wxDefaultSize,
395 0, NULL, wxCB_READONLY );
396 UpdateCombo( p_item );
398 combo->SetToolTip( wxU(p_item->psz_longtext) );
399 sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
401 for( int i = 0; i < p_item->i_action; i++ )
404 new wxButton( this, wxID_HIGHEST+i,
405 wxU(p_item->ppsz_action_text[i]) );
406 sizer->Add( button, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
410 this->SetSizerAndFit( sizer );
413 StringListConfigControl::~StringListConfigControl()
417 void StringListConfigControl::UpdateCombo( module_config_t *p_item )
419 /* build a list of available options */
420 for( int i_index = 0; i_index < p_item->i_list; i_index++ )
422 combo->Append( ( p_item->ppsz_list_text &&
423 p_item->ppsz_list_text[i_index] ) ?
424 wxU(p_item->ppsz_list_text[i_index]) :
425 wxL2U(p_item->ppsz_list[i_index]) );
426 combo->SetClientData( i_index, (void *)p_item->ppsz_list[i_index] );
427 if( ( p_item->psz_value &&
428 !strcmp( p_item->psz_value, p_item->ppsz_list[i_index] ) ) ||
429 ( !p_item->psz_value && !*p_item->ppsz_list[i_index] ) )
431 combo->SetSelection( i_index );
432 combo->SetValue( ( p_item->ppsz_list_text &&
433 p_item->ppsz_list_text[i_index] ) ?
434 wxU(p_item->ppsz_list_text[i_index]) :
435 wxL2U(p_item->ppsz_list[i_index]) );
440 BEGIN_EVENT_TABLE(StringListConfigControl, wxPanel)
442 EVT_BUTTON(-1, StringListConfigControl::OnAction)
445 EVT_TEXT(-1, StringListConfigControl::OnUpdate)
448 void StringListConfigControl::OnAction( wxCommandEvent& event )
450 int i_action = event.GetId() - wxID_HIGHEST;
452 module_config_t *p_item = config_FindConfig( p_this, GetName().mb_str() );
453 if( !p_item ) return;
455 if( i_action < 0 || i_action >= p_item->i_action ) return;
458 wxString value = GetPszValue();
459 (const char *)val.psz_string = value.mb_str();
460 p_item->ppf_action[i_action]( p_this, GetName().mb_str(), val, val, 0 );
462 if( p_item->b_dirty )
465 UpdateCombo( p_item );
466 p_item->b_dirty = VLC_FALSE;
470 wxString StringListConfigControl::GetPszValue()
472 int selected = combo->GetSelection();
475 return wxL2U((char *)combo->GetClientData( selected ));
480 /*****************************************************************************
481 * FileConfigControl implementation
482 *****************************************************************************/
483 FileConfigControl::FileConfigControl( vlc_object_t *p_this,
484 module_config_t *p_item,
486 : ConfigControl( p_this, p_item, parent )
488 directory = p_item->i_type == CONFIG_ITEM_DIRECTORY;
489 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
490 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
491 textctrl = new wxTextCtrl( this, -1,
492 wxL2U(p_item->psz_value),
496 textctrl->SetToolTip( wxU(p_item->psz_longtext) );
497 sizer->Add( textctrl, 1, wxALL, 5 );
498 browse = new wxButton( this, wxID_HIGHEST, wxU(_("Browse...")) );
499 sizer->Add( browse, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
501 this->SetSizerAndFit( sizer );
504 BEGIN_EVENT_TABLE(FileConfigControl, wxPanel)
506 EVT_BUTTON(wxID_HIGHEST, FileConfigControl::OnBrowse)
509 void FileConfigControl::OnBrowse( wxCommandEvent& event )
513 wxDirDialog dialog( this, wxU(_("Choose directory")) );
515 if( dialog.ShowModal() == wxID_OK )
517 textctrl->SetValue( dialog.GetPath() );
522 wxFileDialog dialog( this, wxU(_("Choose file")),
523 wxT(""), wxT(""), wxT("*.*"),
524 #if defined( __WXMSW__ )
530 if( dialog.ShowModal() == wxID_OK )
532 textctrl->SetValue( dialog.GetPath() );
537 FileConfigControl::~FileConfigControl()
542 wxString FileConfigControl::GetPszValue()
544 return textctrl->GetValue();
547 /*****************************************************************************
548 * IntegerConfigControl implementation
549 *****************************************************************************/
550 IntegerConfigControl::IntegerConfigControl( vlc_object_t *p_this,
551 module_config_t *p_item,
553 : ConfigControl( p_this, p_item, parent )
555 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
556 spin = new wxSpinCtrl( this, -1,
557 wxString::Format(wxT("%d"),
559 wxDefaultPosition, wxDefaultSize,
561 -10000000, 10000000, p_item->i_value);
562 spin->SetToolTip( wxU(p_item->psz_longtext) );
563 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
564 sizer->Add( spin, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
566 this->SetSizerAndFit( sizer );
569 IntegerConfigControl::~IntegerConfigControl()
574 int IntegerConfigControl::GetIntValue()
576 return spin->GetValue();
579 /*****************************************************************************
580 * IntegerListConfigControl implementation
581 *****************************************************************************/
582 IntegerListConfigControl::IntegerListConfigControl( vlc_object_t *p_this,
583 module_config_t *p_item,
585 : ConfigControl( p_this, p_item, parent )
587 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
588 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
589 combo = new wxComboBox( this, -1, wxT(""),
590 wxDefaultPosition, wxDefaultSize,
591 0, NULL, wxCB_READONLY );
593 UpdateCombo( p_item );
595 combo->SetToolTip( wxU(p_item->psz_longtext) );
596 sizer->Add( combo, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
599 this->SetSizerAndFit( sizer );
602 IntegerListConfigControl::~IntegerListConfigControl()
606 void IntegerListConfigControl::UpdateCombo( module_config_t *p_item )
608 /* build a list of available options */
609 for( int i_index = 0; i_index < p_item->i_list; i_index++ )
611 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i_index] )
613 combo->Append( wxU(p_item->ppsz_list_text[i_index]) );
617 combo->Append( wxString::Format(wxT("%i"),
618 p_item->pi_list[i_index]) );
620 combo->SetClientData( i_index, (void *)p_item->pi_list[i_index] );
621 if( p_item->i_value == p_item->pi_list[i_index] )
623 combo->SetSelection( i_index );
624 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i_index] )
626 combo->SetValue( wxU(p_item->ppsz_list_text[i_index]) );
630 combo->SetValue( wxString::Format(wxT("%i"),
631 p_item->pi_list[i_index]) );
637 BEGIN_EVENT_TABLE(IntegerListConfigControl, wxPanel)
639 EVT_BUTTON(-1, IntegerListConfigControl::OnAction)
642 void IntegerListConfigControl::OnAction( wxCommandEvent& event )
644 int i_action = event.GetId() - wxID_HIGHEST;
646 module_config_t *p_item;
647 p_item = config_FindConfig( p_this, GetName().mb_str() );
648 if( !p_item ) return;
650 if( i_action < 0 || i_action >= p_item->i_action ) return;
653 val.i_int = GetIntValue();
654 p_item->ppf_action[i_action]( p_this, GetName().mb_str(), val, val, 0 );
656 if( p_item->b_dirty )
659 UpdateCombo( p_item );
660 p_item->b_dirty = VLC_FALSE;
664 int IntegerListConfigControl::GetIntValue()
666 int selected = combo->GetSelection();
669 return (int)combo->GetClientData( selected );
674 /*****************************************************************************
675 * RangedIntConfigControl implementation
676 *****************************************************************************/
677 RangedIntConfigControl::RangedIntConfigControl( vlc_object_t *p_this,
678 module_config_t *p_item,
680 : ConfigControl( p_this, p_item, parent )
682 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
683 slider = new wxSlider( this, -1, p_item->i_value, p_item->i_min,
684 p_item->i_max, wxDefaultPosition, wxDefaultSize,
685 wxSL_LABELS | wxSL_HORIZONTAL );
686 slider->SetToolTip( wxU(p_item->psz_longtext) );
687 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
688 sizer->Add( slider, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
690 this->SetSizerAndFit( sizer );
693 RangedIntConfigControl::~RangedIntConfigControl()
698 int RangedIntConfigControl::GetIntValue()
700 return slider->GetValue();
703 /*****************************************************************************
704 * FloatConfigControl implementation
705 *****************************************************************************/
706 FloatConfigControl::FloatConfigControl( vlc_object_t *p_this,
707 module_config_t *p_item,
709 : ConfigControl( p_this, p_item, parent )
711 label = new wxStaticText(this, -1, wxU(p_item->psz_text));
712 textctrl = new wxTextCtrl( this, -1,
713 wxString::Format(wxT("%f"),
715 wxDefaultPosition, wxDefaultSize,
716 wxTE_PROCESS_ENTER );
717 textctrl->SetToolTip( wxU(p_item->psz_longtext) );
718 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
719 sizer->Add( textctrl, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
721 this->SetSizerAndFit( sizer );
724 FloatConfigControl::~FloatConfigControl()
729 float FloatConfigControl::GetFloatValue()
732 if( (wxSscanf(textctrl->GetValue(), wxT("%f"), &f_value) == 1) )
737 /*****************************************************************************
738 * BoolConfigControl implementation
739 *****************************************************************************/
740 BoolConfigControl::BoolConfigControl( vlc_object_t *p_this,
741 module_config_t *p_item,
743 : ConfigControl( p_this, p_item, parent )
745 checkbox = new wxCheckBox( this, -1, wxU(p_item->psz_text) );
746 if( p_item->i_value ) checkbox->SetValue(TRUE);
747 checkbox->SetToolTip( wxU(p_item->psz_longtext) );
748 sizer->Add( checkbox, 0, wxALL, 5 );
750 this->SetSizerAndFit( sizer );
753 BoolConfigControl::~BoolConfigControl()
758 int BoolConfigControl::GetIntValue()
760 if( checkbox->IsChecked() )