1 /*****************************************************************************
\r
2 * preferences.cpp: the "Preferences" dialog box
\r
3 *****************************************************************************
\r
4 * Copyright (C) 2002 VideoLAN
\r
6 * Authors: Olivier Teuliere <ipkiss@via.ecp.fr>
\r
8 * This program is free software; you can redistribute it and/or modify
\r
9 * it under the terms of the GNU General Public License as published by
\r
10 * the Free Software Foundation; either version 2 of the License, or
\r
11 * (at your option) any later version.
\r
13 * This program is distributed in the hope that it will be useful,
\r
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
16 * GNU General Public License for more details.
\r
18 * You should have received a copy of the GNU General Public License
\r
19 * along with this program; if not, write to the Free Software
\r
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
\r
21 *****************************************************************************/
\r
26 #include <stdlib.h> /* malloc(), free() */
\r
27 #include <string.h> /* strcmp */
\r
29 #include <vlc/vlc.h>
\r
30 #include <vlc/intf.h>
\r
32 #include "preferences.h"
\r
33 #include "win32_common.h"
\r
35 //---------------------------------------------------------------------------
\r
36 //#pragma package(smart_init)
\r
37 #pragma link "CSPIN"
\r
38 #pragma resource "*.dfm"
\r
40 extern intf_thread_t *p_intfGlobal;
\r
43 /****************************************************************************
\r
44 * Functions to help components creation
\r
45 ****************************************************************************/
\r
46 __fastcall TGroupBoxPref::TGroupBoxPref( TComponent* Owner,
\r
47 module_config_t *p_config_arg ) : TGroupBox( Owner )
\r
49 p_config = p_config_arg;
\r
50 Caption = p_config->psz_text;
\r
52 //---------------------------------------------------------------------------
\r
53 TListView * __fastcall TGroupBoxPref::CreateListView( TWinControl *Parent,
\r
54 int Left, int Width, int Top, int Height, TViewStyle ViewStyle )
\r
56 TListView *ListView = new TListView( Parent );
\r
57 ListView->Parent = Parent;
\r
58 ListView->ViewStyle = ViewStyle;
\r
59 ListView->Left = Left;
\r
60 ListView->Width = Width;
\r
61 ListView->Top = Top;
\r
62 ListView->Height = Height;
\r
65 //---------------------------------------------------------------------------
\r
66 TButton * __fastcall TGroupBoxPref::CreateButton( TWinControl *Parent,
\r
67 int Left, int Width, int Top, int Height, AnsiString Caption )
\r
69 TButton *Button = new TButton( Parent );
\r
70 Button->Parent = Parent;
\r
71 Button->Left = Left;
\r
72 Button->Width = Width;
\r
74 Button->Height = Height;
\r
75 Button->Caption = Caption;
\r
78 //---------------------------------------------------------------------------
\r
79 TCheckBox * __fastcall TGroupBoxPref::CreateCheckBox( TWinControl *Parent,
\r
80 int Left, int Width, int Top, int Height, AnsiString Caption )
\r
82 TCheckBox *CheckBox = new TCheckBox( Parent );
\r
83 CheckBox->Parent = Parent;
\r
84 CheckBox->Left = Left;
\r
85 CheckBox->Width = Width;
\r
86 CheckBox->Top = Top;
\r
87 CheckBox->Height = Height;
\r
88 CheckBox->Caption = Caption;
\r
91 //---------------------------------------------------------------------------
\r
92 TLabel * __fastcall TGroupBoxPref::CreateLabel( TWinControl *Parent,
\r
93 int Left, int Width, int Top, int Height, AnsiString Caption,
\r
96 TLabel *Label = new TLabel( Parent );
\r
97 Label->Parent = Parent;
\r
98 Label->Caption = Caption;
\r
100 Label->Width = Width;
\r
102 Label->Height = Height;
\r
103 Label->WordWrap = WordWrap;
\r
106 //---------------------------------------------------------------------------
\r
107 TEdit * __fastcall TGroupBoxPref::CreateEdit( TWinControl *Parent,
\r
108 int Left, int Width, int Top, int Height, AnsiString Text )
\r
110 TEdit *Edit = new TEdit( Parent );
\r
111 Edit->Parent = Parent;
\r
113 Edit->Width = Width;
\r
115 Edit->Height = Height;
\r
119 //---------------------------------------------------------------------------
\r
120 TCSpinEdit * __fastcall TGroupBoxPref::CreateSpinEdit( TWinControl *Parent,
\r
121 int Left, int Width, int Top, int Height,
\r
122 long Min, long Max, long Value )
\r
124 TCSpinEdit *SpinEdit = new TCSpinEdit( Parent );
\r
125 SpinEdit->Parent = Parent;
\r
126 SpinEdit->Left = Left;
\r
127 SpinEdit->Width = Width;
\r
128 SpinEdit->Top = Top;
\r
129 SpinEdit->Height = Height;
\r
130 SpinEdit->MinValue = Min;
\r
131 SpinEdit->MaxValue = Max;
\r
132 SpinEdit->Value = Value;
\r
135 //---------------------------------------------------------------------------
\r
136 void __fastcall TGroupBoxPref::UpdateChanges()
\r
141 /****************************************************************************
\r
142 * GroupBox for module management
\r
143 ****************************************************************************/
\r
144 __fastcall TGroupBoxPlugin::TGroupBoxPlugin( TComponent* Owner,
\r
145 module_config_t *p_config ) : TGroupBoxPref( Owner, p_config )
\r
147 /* init listview */
\r
148 ListView = CreateListView( this, 16, 164, 24, 160, vsReport );
\r
149 ListView->ReadOnly = true;
\r
150 ListView->Columns->Add();
\r
151 ListView->Columns->Items[0]->Width = 160;
\r
152 ListView->Columns->Items[0]->Caption = "Name";//p_config->psz_text;
\r
153 ListView->OnSelectItem = ListViewSelectItem;
\r
155 /* init description label */
\r
156 LabelDesc = CreateLabel( this, 230, 225, 50, 52,
\r
157 p_config->psz_longtext, true );
\r
159 /* init hint label */
\r
160 LabelHint = CreateLabel( this, 230, 225, 135, 13, "", false );
\r
162 /* init configure button */
\r
163 ButtonConfig = CreateButton( this, 16, 70, 192, 25, "Configure" );
\r
164 ButtonConfig->Enabled = false;
\r
165 ButtonConfig->OnClick = ButtonConfigClick;
\r
167 /* init select button */
\r
168 ButtonSelect = CreateButton( this, 110, 70, 192, 25, "Select" );
\r
169 ButtonSelect->OnClick = ButtonSelectClick;
\r
171 /* init 'Selected' label */
\r
172 LabelSelected = CreateLabel( this, 230, 45, 198, 13, "Selected", false );
\r
174 /* init 'Selected' edit */
\r
175 Edit = CreateEdit( this, 280, 164, 194, 21, "" );
\r
176 vlc_mutex_lock( p_config->p_lock );
\r
177 Edit->Text = p_config->psz_value ? p_config->psz_value : "";
\r
178 vlc_mutex_unlock( p_config->p_lock );
\r
182 //---------------------------------------------------------------------------
\r
183 void __fastcall TGroupBoxPlugin::ListViewSelectItem( TObject *Sender,
\r
184 TListItem *Item, bool Selected )
\r
186 module_t *p_module;
\r
189 Name = Item->Caption;
\r
192 /* look for module 'Name' */
\r
193 for( p_module = p_intfGlobal->p_vlc->p_module_bank->first ;
\r
195 p_module = p_module->next )
\r
197 if( strcmp( p_module->psz_object_name, Name.c_str() ) == 0 )
\r
199 ModuleSelected = p_module;
\r
200 LabelHint->Caption = p_module->psz_longname ?
\r
201 p_module->psz_longname : "";
\r
202 ButtonConfig->Enabled = p_module->i_config_items ? true : false;
\r
209 //---------------------------------------------------------------------------
\r
210 void __fastcall TGroupBoxPlugin::ButtonSelectClick( TObject *Sender )
\r
212 if( !ModuleSelected ) return;
\r
213 Edit->Text = ModuleSelected->psz_object_name;
\r
215 //---------------------------------------------------------------------------
\r
216 void __fastcall TGroupBoxPlugin::ButtonConfigClick( TObject *Sender )
\r
218 p_intfGlobal->p_sys->p_window->
\r
219 CreatePreferences( ModuleSelected->psz_object_name );
\r
221 //---------------------------------------------------------------------------
\r
222 void __fastcall TGroupBoxPlugin::UpdateChanges()
\r
224 /* XXX: Necessary, since c_str() returns only a temporary pointer... */
\r
225 free( p_config->psz_value );
\r
226 p_config->psz_value = (char *)malloc( Edit->Text.Length() + 1 );
\r
227 strcpy( p_config->psz_value, Edit->Text.c_str() );
\r
231 /****************************************************************************
\r
232 * GroupBox for string management
\r
233 ****************************************************************************/
\r
234 __fastcall TGroupBoxString::TGroupBoxString( TComponent* Owner,
\r
235 module_config_t *p_config ) : TGroupBoxPref( Owner, p_config )
\r
237 /* init description label */
\r
238 LabelDesc = CreateLabel( this, 230, 225, 24, 26,
\r
239 p_config->psz_longtext, true );
\r
242 Edit = CreateEdit( this, 16, 164, 24, 21, "" );
\r
243 vlc_mutex_lock( p_config->p_lock );
\r
244 Edit->Text = p_config->psz_value ? p_config->psz_value : "";
\r
245 vlc_mutex_unlock( p_config->p_lock );
\r
247 /* vertical alignment */
\r
248 Height = LabelDesc->Height + 24;
\r
249 LabelDesc->Top = Top + ( Height - LabelDesc->Height ) / 2 + 4;
\r
250 Edit->Top = Top + ( Height - Edit->Height ) / 2 + 4;
\r
252 //---------------------------------------------------------------------------
\r
253 void __fastcall TGroupBoxString::UpdateChanges()
\r
255 /* XXX: Necessary, since c_str() returns only a temporary pointer... */
\r
256 free( p_config->psz_value );
\r
257 p_config->psz_value = (char *)malloc( Edit->Text.Length() + 1 );
\r
258 strcpy( p_config->psz_value, Edit->Text.c_str() );
\r
262 /****************************************************************************
\r
263 * GroupBox for integer management
\r
264 ****************************************************************************/
\r
265 __fastcall TGroupBoxInteger::TGroupBoxInteger( TComponent* Owner,
\r
266 module_config_t *p_config ) : TGroupBoxPref( Owner, p_config )
\r
268 /* init description label */
\r
269 LabelDesc = CreateLabel( this, 230, 225, 19, 26,
\r
270 p_config->psz_longtext, true );
\r
272 /* init spinedit */
\r
273 SpinEdit = CreateSpinEdit( this, 16, 164, 24, 21,
\r
274 -1, 100000, p_config->i_value );
\r
276 /* vertical alignment */
\r
277 Height = LabelDesc->Height + 24;
\r
278 LabelDesc->Top = Top + ( Height - LabelDesc->Height ) / 2 + 4;
\r
279 SpinEdit->Top = Top + ( Height - SpinEdit->Height ) / 2 + 4;
\r
281 //---------------------------------------------------------------------------
\r
282 void __fastcall TGroupBoxInteger::UpdateChanges()
\r
284 /* Warning: we're casting from long to int */
\r
285 p_config->i_value = (int)SpinEdit->Value;
\r
289 /****************************************************************************
\r
290 * GroupBox for boolean management
\r
291 ****************************************************************************/
\r
292 __fastcall TGroupBoxBool::TGroupBoxBool( TComponent* Owner,
\r
293 module_config_t *p_config ) : TGroupBoxPref( Owner, p_config )
\r
295 /* init description label */
\r
296 LabelDesc = CreateLabel( this, 230, 225, 19, 26,
\r
297 p_config->psz_longtext, true );
\r
299 /* init checkbox */
\r
300 CheckBox = CreateCheckBox( this, 16, 184, 28, 17, p_config->psz_text );
\r
301 CheckBox->Checked = p_config->i_value;
\r
303 /* vertical alignment */
\r
304 Height = LabelDesc->Height + 24;
\r
305 LabelDesc->Top = Top + ( Height - LabelDesc->Height ) / 2 + 4;
\r
306 CheckBox->Top = Top + ( Height - CheckBox->Height ) / 2 + 4;
\r
308 //---------------------------------------------------------------------------
\r
309 void __fastcall TGroupBoxBool::UpdateChanges()
\r
311 p_config->i_value = CheckBox->Checked ? 1 : 0;
\r
315 /****************************************************************************
\r
316 * Callbacks for the dialog
\r
317 ****************************************************************************/
\r
318 //---------------------------------------------------------------------------
\r
319 __fastcall TPreferencesDlg::TPreferencesDlg( TComponent* Owner )
\r
322 Icon = p_intfGlobal->p_sys->p_window->Icon;
\r
324 //---------------------------------------------------------------------------
\r
325 void __fastcall TPreferencesDlg::FormClose( TObject *Sender,
\r
326 TCloseAction &Action )
\r
330 //---------------------------------------------------------------------------
\r
331 void __fastcall TPreferencesDlg::FormShow( TObject *Sender )
\r
334 p_intfGlobal->p_sys->p_window->MenuPreferences->Checked = true;
\r
335 p_intfGlobal->p_sys->p_window->PopupPreferences->Checked = true;
\r
338 //---------------------------------------------------------------------------
\r
339 void __fastcall TPreferencesDlg::FormHide( TObject *Sender )
\r
342 p_intfGlobal->p_sys->p_window->MenuPreferences->Checked = false;
\r
343 p_intfGlobal->p_sys->p_window->PopupPreferences->Checked = false;
\r
348 /****************************************************************************
\r
349 * CreateConfigDialog: dynamically creates the configuration dialog
\r
350 * box from all the configuration data provided by the selected module.
\r
351 ****************************************************************************/
\r
352 #define ADD_PANEL \
\r
354 Panel = new TPanel( this ); \
\r
355 Panel->Parent = ScrollBox; \
\r
356 Panel->Caption = ""; \
\r
357 Panel->BevelOuter = bvNone; \
\r
358 Panel->Height = 12; \
\r
361 void __fastcall TPreferencesDlg::CreateConfigDialog( char *psz_module_name )
\r
363 module_t *p_module;
\r
364 module_t **pp_parser;
\r
365 vlc_list_t *p_list;
\r
367 module_config_t *p_item;
\r
368 int i_pages, i_ctrl;
\r
370 TTabSheet *TabSheet;
\r
371 TScrollBox *ScrollBox;
\r
373 TGroupBoxPlugin *GroupBoxPlugin;
\r
374 TGroupBoxString *GroupBoxString;
\r
375 TGroupBoxInteger *GroupBoxInteger;
\r
376 TGroupBoxBool *GroupBoxBool;
\r
377 TListItem *ListItem;
\r
379 /* Look for the selected module */
\r
380 for( p_module = p_intfGlobal->p_vlc->p_module_bank->first ; p_module != NULL ;
\r
381 p_module = p_module->next )
\r
383 if( psz_module_name
\r
384 && !strcmp( psz_module_name, p_module->psz_object_name ) )
\r
389 if( !p_module ) return;
\r
392 * We found it, now we can start building its configuration interface
\r
395 /* Enumerate config options and add corresponding config boxes */
\r
396 p_item = p_module->p_config;
\r
399 switch( p_item->i_type )
\r
401 case CONFIG_HINT_CATEGORY:
\r
403 /* create a new tabsheet. */
\r
404 TabSheet = new TTabSheet( this );
\r
405 TabSheet->PageControl = PageControlPref;
\r
406 TabSheet->Caption = p_item->psz_text;
\r
407 TabSheet->Visible = true;
\r
409 /* pack a scrollbox into the tabsheet */
\r
410 ScrollBox = new TScrollBox( this );
\r
411 ScrollBox->Parent = TabSheet;
\r
412 ScrollBox->Align = alClient;
\r
413 ScrollBox->BorderStyle = bsNone;
\r
414 ScrollBox->HorzScrollBar->Tracking = true;
\r
415 ScrollBox->VertScrollBar->Tracking = true;
\r
419 case CONFIG_ITEM_MODULE:
\r
421 /* add new groupbox for the config option */
\r
422 GroupBoxPlugin = new TGroupBoxPlugin( this, p_item );
\r
423 GroupBoxPlugin->Parent = ScrollBox;
\r
425 /* add panel as separator */
\r
428 /* Look for valid modules */
\r
429 p_list = vlc_list_find( p_intfGlobal, VLC_OBJECT_MODULE, FIND_ANYWHERE );
\r
430 pp_parser = (module_t **)p_list->pp_objects;
\r
432 for( ; *pp_parser ; pp_parser++ )
\r
434 if( !strcmp( (*pp_parser)->psz_capability, p_item->psz_type ) )
\r
436 ListItem = GroupBoxPlugin->ListView->Items->Add();
\r
437 ListItem->Caption = (*pp_parser)->psz_object_name;
\r
441 vlc_list_release( p_list );
\r
445 case CONFIG_ITEM_FILE:
\r
447 case CONFIG_ITEM_STRING:
\r
449 /* add new groupbox for the config option */
\r
450 GroupBoxString = new TGroupBoxString( this, p_item );
\r
451 GroupBoxString->Parent = ScrollBox;
\r
453 /* add panel as separator */
\r
458 case CONFIG_ITEM_INTEGER:
\r
460 /* add new groupbox for the config option */
\r
461 GroupBoxInteger = new TGroupBoxInteger( this, p_item );
\r
462 GroupBoxInteger->Parent = ScrollBox;
\r
464 /* add panel as separator */
\r
469 case CONFIG_ITEM_BOOL:
\r
471 /* add new groupbox for the config option */
\r
472 GroupBoxBool = new TGroupBoxBool( this, p_item );
\r
473 GroupBoxBool->Parent = ScrollBox;
\r
475 /* add panel as separator */
\r
483 while( p_item->i_type != CONFIG_HINT_END );
\r
485 /* Reorder groupboxes inside the tabsheets */
\r
486 for( i_pages = 0; i_pages < PageControlPref->PageCount; i_pages++ )
\r
488 /* get scrollbox from the tabsheet */
\r
489 ScrollBox = (TScrollBox *)PageControlPref->Pages[i_pages]->Controls[0];
\r
491 for( i_ctrl = ScrollBox->ControlCount - 1; i_ctrl >= 0 ; i_ctrl-- )
\r
493 ScrollBox->Controls[i_ctrl]->Align = alTop;
\r
497 /* set active tabsheet
\r
498 * FIXME: i don't know why, but both lines are necessary */
\r
499 PageControlPref->ActivePageIndex = 1;
\r
500 PageControlPref->ActivePageIndex = 0;
\r
503 //---------------------------------------------------------------------------
\r
504 void __fastcall TPreferencesDlg::ButtonOkClick( TObject *Sender )
\r
506 ButtonApplyClick( Sender );
\r
509 //---------------------------------------------------------------------------
\r
510 void __fastcall TPreferencesDlg::ButtonApplyClick( TObject *Sender )
\r
512 TScrollBox *ScrollBox;
\r
513 TGroupBoxPref *GroupBox;
\r
516 for( i = 0; i < PageControlPref->PageCount; i++ )
\r
518 /* get scrollbox from the tabsheet */
\r
519 ScrollBox = (TScrollBox *)PageControlPref->Pages[i]->Controls[0];
\r
521 for( j = 0; j < ScrollBox->ControlCount ; j++ )
\r
523 /* skip the panels */
\r
524 if( ScrollBox->Controls[j]->InheritsFrom( __classid( TGroupBoxPref ) ) )
\r
526 GroupBox = (TGroupBoxPref *)ScrollBox->Controls[j];
\r
527 GroupBox->UpdateChanges();
\r
528 SaveValue( GroupBox->p_config );
\r
533 //---------------------------------------------------------------------------
\r
534 void __fastcall TPreferencesDlg::ButtonSaveClick( TObject *Sender )
\r
536 ButtonApplyClick( Sender );
\r
537 config_SaveConfigFile( p_intfGlobal, NULL );
\r
539 //---------------------------------------------------------------------------
\r
540 void __fastcall TPreferencesDlg::ButtonCancelClick( TObject *Sender )
\r
544 //---------------------------------------------------------------------------
\r
545 void __fastcall TPreferencesDlg::SaveValue( module_config_t *p_config )
\r
547 switch( p_config->i_type )
\r
549 case CONFIG_ITEM_STRING:
\r
550 case CONFIG_ITEM_FILE:
\r
551 case CONFIG_ITEM_MODULE:
\r
552 config_PutPsz( p_intfGlobal, p_config->psz_name,
\r
553 *p_config->psz_value ? p_config->psz_value : NULL );
\r
555 case CONFIG_ITEM_INTEGER:
\r
556 case CONFIG_ITEM_BOOL:
\r
557 config_PutInt( p_intfGlobal, p_config->psz_name,
\r
558 p_config->i_value );
\r
562 //---------------------------------------------------------------------------
\r