1 /*****************************************************************************
\r
2 * preferences.h: 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 <videolan/vlc.h>
\r
31 #include "interface.h"
\r
33 #include "preferences.h"
\r
34 #include "win32_common.h"
\r
36 //---------------------------------------------------------------------------
\r
37 //#pragma package(smart_init)
\r
38 #pragma resource "*.dfm"
\r
40 extern struct intf_thread_s *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 TUpDown * __fastcall TGroupBoxPref::CreateUpDown( TWinControl *Parent,
\r
121 int Min, int Max, int Position, bool Thousands )
\r
123 TUpDown *UpDown = new TUpDown( Parent );
\r
124 UpDown->Parent = Parent;
\r
127 UpDown->Position = Position;
\r
128 UpDown->Thousands = Thousands;
\r
131 //---------------------------------------------------------------------------
\r
132 void __fastcall TGroupBoxPref::UpdateChanges()
\r
137 /****************************************************************************
\r
138 * GroupBox for module management
\r
139 ****************************************************************************/
\r
140 __fastcall TGroupBoxPlugin::TGroupBoxPlugin( TComponent* Owner,
\r
141 module_config_t *p_config ) : TGroupBoxPref( Owner, p_config )
\r
143 /* init listview */
\r
144 ListView = CreateListView( this, 16, 164, 24, 160, vsReport );
\r
145 ListView->ReadOnly = true;
\r
146 ListView->Columns->Add();
\r
147 ListView->Columns->Items[0]->Width = 160;
\r
148 ListView->Columns->Items[0]->Caption = "Name";//p_config->psz_text;
\r
149 ListView->OnSelectItem = ListViewSelectItem;
\r
151 /* init description label */
\r
152 LabelDesc = CreateLabel( this, 230, 225, 50, 52,
\r
153 p_config->psz_longtext, true );
\r
155 /* init hint label */
\r
156 LabelHint = CreateLabel( this, 230, 225, 135, 13, "", false );
\r
158 /* init configure button */
\r
159 ButtonConfig = CreateButton( this, 16, 70, 192, 25, "Configure" );
\r
160 ButtonConfig->Enabled = false;
\r
161 ButtonConfig->OnClick = ButtonConfigClick;
\r
163 /* init select button */
\r
164 ButtonSelect = CreateButton( this, 110, 70, 192, 25, "Select" );
\r
165 ButtonSelect->OnClick = ButtonSelectClick;
\r
167 /* init 'Selected' label */
\r
168 LabelSelected = CreateLabel( this, 230, 45, 198, 13, "Selected", false );
\r
170 /* init 'Selected' edit */
\r
171 Edit = CreateEdit( this, 280, 164, 194, 21, "" );
\r
172 vlc_mutex_lock( p_config->p_lock );
\r
173 Edit->Text = p_config->psz_value ? p_config->psz_value : "";
\r
174 vlc_mutex_unlock( p_config->p_lock );
\r
178 //---------------------------------------------------------------------------
\r
179 void __fastcall TGroupBoxPlugin::ListViewSelectItem( TObject *Sender,
\r
180 TListItem *Item, bool Selected )
\r
182 module_t *p_module;
\r
185 Name = Item->Caption;
\r
188 /* look for module 'Name' */
\r
189 for( p_module = p_module_bank->first ;
\r
191 p_module = p_module->next )
\r
193 if( strcmp( p_module->psz_name, Name.c_str() ) == 0 )
\r
195 ModuleSelected = p_module;
\r
196 LabelHint->Caption = p_module->psz_longname ?
\r
197 p_module->psz_longname : "";
\r
198 ButtonConfig->Enabled = p_module->i_config_items ? true : false;
\r
205 //---------------------------------------------------------------------------
\r
206 void __fastcall TGroupBoxPlugin::ButtonSelectClick( TObject *Sender )
\r
208 if( !ModuleSelected ) return;
\r
209 Edit->Text = ModuleSelected->psz_name;
\r
211 //---------------------------------------------------------------------------
\r
212 void __fastcall TGroupBoxPlugin::ButtonConfigClick( TObject *Sender )
\r
214 p_intfGlobal->p_sys->p_window->CreatePreferences( ModuleSelected->psz_name );
\r
216 //---------------------------------------------------------------------------
\r
217 void __fastcall TGroupBoxPlugin::UpdateChanges()
\r
219 /* XXX: Necessary, since c_str() returns only a temporary pointer... */
\r
220 free( p_config->psz_value );
\r
221 p_config->psz_value = (char *)malloc( Edit->Text.Length() + 1 );
\r
222 strcpy( p_config->psz_value, Edit->Text.c_str() );
\r
226 /****************************************************************************
\r
227 * GroupBox for string management
\r
228 ****************************************************************************/
\r
229 __fastcall TGroupBoxString::TGroupBoxString( TComponent* Owner,
\r
230 module_config_t *p_config ) : TGroupBoxPref( Owner, p_config )
\r
232 /* init description label */
\r
233 LabelDesc = CreateLabel( this, 230, 225, 24, 26,
\r
234 p_config->psz_longtext, true );
\r
237 Edit = CreateEdit( this, 16, 164, 24, 21, "" );
\r
238 vlc_mutex_lock( p_config->p_lock );
\r
239 Edit->Text = p_config->psz_value ? p_config->psz_value : "";
\r
240 vlc_mutex_unlock( p_config->p_lock );
\r
242 /* vertical alignment */
\r
243 Height = LabelDesc->Height + 24;
\r
244 LabelDesc->Top = Top + ( Height - LabelDesc->Height ) / 2 + 4;
\r
245 Edit->Top = Top + ( Height - Edit->Height ) / 2 + 4;
\r
247 //---------------------------------------------------------------------------
\r
248 void __fastcall TGroupBoxString::UpdateChanges()
\r
250 /* XXX: Necessary, since c_str() returns only a temporary pointer... */
\r
251 free( p_config->psz_value );
\r
252 p_config->psz_value = (char *)malloc( Edit->Text.Length() + 1 );
\r
253 strcpy( p_config->psz_value, Edit->Text.c_str() );
\r
257 /****************************************************************************
\r
258 * GroupBox for integer management
\r
259 ****************************************************************************/
\r
260 __fastcall TGroupBoxInteger::TGroupBoxInteger( TComponent* Owner,
\r
261 module_config_t *p_config ) : TGroupBoxPref( Owner, p_config )
\r
263 /* init description label */
\r
264 LabelDesc = CreateLabel( this, 230, 225, 19, 26,
\r
265 p_config->psz_longtext, true );
\r
268 Edit = CreateEdit( this, 16, 148, 24, 21, "" );
\r
271 UpDown = CreateUpDown( this, -1, 32767, p_config->i_value, false );
\r
272 UpDown->Associate = Edit;
\r
274 /* vertical alignment */
\r
275 Height = LabelDesc->Height + 24;
\r
276 LabelDesc->Top = Top + ( Height - LabelDesc->Height ) / 2 + 4;
\r
277 Edit->Top = Top + ( Height - Edit->Height ) / 2 + 4;
\r
279 //---------------------------------------------------------------------------
\r
280 void __fastcall TGroupBoxInteger::UpdateChanges()
\r
282 p_config->i_value = StrToInt( Edit->Text );
\r
286 /****************************************************************************
\r
287 * GroupBox for boolean management
\r
288 ****************************************************************************/
\r
289 __fastcall TGroupBoxBool::TGroupBoxBool( TComponent* Owner,
\r
290 module_config_t *p_config ) : TGroupBoxPref( Owner, p_config )
\r
292 /* init description label */
\r
293 LabelDesc = CreateLabel( this, 230, 225, 19, 26,
\r
294 p_config->psz_longtext, true );
\r
296 /* init checkbox */
\r
297 CheckBox = CreateCheckBox( this, 16, 184, 28, 17, p_config->psz_text );
\r
298 CheckBox->Checked = p_config->i_value;
\r
300 /* vertical alignment */
\r
301 Height = LabelDesc->Height + 24;
\r
302 LabelDesc->Top = Top + ( Height - LabelDesc->Height ) / 2 + 4;
\r
303 CheckBox->Top = Top + ( Height - CheckBox->Height ) / 2 + 4;
\r
305 //---------------------------------------------------------------------------
\r
306 void __fastcall TGroupBoxBool::UpdateChanges()
\r
308 p_config->i_value = CheckBox->Checked ? 1 : 0;
\r
312 /****************************************************************************
\r
313 * Callbacks for the dialog
\r
314 ****************************************************************************/
\r
315 //---------------------------------------------------------------------------
\r
316 __fastcall TPreferencesDlg::TPreferencesDlg( TComponent* Owner )
\r
319 Icon = p_intfGlobal->p_sys->p_window->Icon;
\r
321 //---------------------------------------------------------------------------
\r
322 void __fastcall TPreferencesDlg::FormClose( TObject *Sender,
\r
323 TCloseAction &Action )
\r
327 //---------------------------------------------------------------------------
\r
328 void __fastcall TPreferencesDlg::FormShow( TObject *Sender )
\r
331 p_intfGlobal->p_sys->p_window->MenuPreferences->Checked = true;
\r
332 p_intfGlobal->p_sys->p_window->PopupPreferences->Checked = true;
\r
335 //---------------------------------------------------------------------------
\r
336 void __fastcall TPreferencesDlg::FormHide( TObject *Sender )
\r
339 p_intfGlobal->p_sys->p_window->MenuPreferences->Checked = false;
\r
340 p_intfGlobal->p_sys->p_window->PopupPreferences->Checked = false;
\r
345 /****************************************************************************
\r
346 * CreateConfigDialog: dynamically creates the configuration dialog
\r
347 * box from all the configuration data provided by the selected module.
\r
348 ****************************************************************************/
\r
349 #define ADD_PANEL \
\r
351 Panel = new TPanel( this ); \
\r
352 Panel->Parent = ScrollBox; \
\r
353 Panel->Caption = ""; \
\r
354 Panel->BevelOuter = bvNone; \
\r
355 Panel->Height = 12; \
\r
358 void __fastcall TPreferencesDlg::CreateConfigDialog( char *psz_module_name )
\r
360 module_t *p_module, *p_module_plugins;
\r
361 module_config_t *p_item;
\r
362 int i_pages, i_ctrl;
\r
364 TTabSheet *TabSheet;
\r
365 TScrollBox *ScrollBox;
\r
367 TGroupBoxPlugin *GroupBoxPlugin;
\r
368 TGroupBoxString *GroupBoxString;
\r
369 TGroupBoxInteger *GroupBoxInteger;
\r
370 TGroupBoxBool *GroupBoxBool;
\r
371 TListItem *ListItem;
\r
373 /* Look for the selected module */
\r
374 for( p_module = p_module_bank->first ; p_module != NULL ;
\r
375 p_module = p_module->next )
\r
377 if( psz_module_name && !strcmp( psz_module_name, p_module->psz_name ) )
\r
380 if( !p_module ) return;
\r
383 * We found it, now we can start building its configuration interface
\r
386 /* Enumerate config options and add corresponding config boxes */
\r
387 p_item = p_module->p_config;
\r
390 switch( p_item->i_type )
\r
392 case MODULE_CONFIG_HINT_CATEGORY:
\r
394 /* create a new tabsheet. */
\r
395 TabSheet = new TTabSheet( this );
\r
396 TabSheet->PageControl = PageControlPref;
\r
397 TabSheet->Caption = p_item->psz_text;
\r
398 TabSheet->Visible = true;
\r
400 /* pack a scrollbox into the tabsheet */
\r
401 ScrollBox = new TScrollBox( this );
\r
402 ScrollBox->Parent = TabSheet;
\r
403 ScrollBox->Align = alClient;
\r
404 ScrollBox->BorderStyle = bsNone;
\r
405 ScrollBox->HorzScrollBar->Tracking = true;
\r
406 ScrollBox->VertScrollBar->Tracking = true;
\r
410 case MODULE_CONFIG_ITEM_MODULE:
\r
412 /* add new groupbox for the config option */
\r
413 GroupBoxPlugin = new TGroupBoxPlugin( this, p_item );
\r
414 GroupBoxPlugin->Parent = ScrollBox;
\r
416 /* add panel as separator */
\r
419 /* build a list of available plugins */
\r
420 for( p_module_plugins = p_module_bank->first ;
\r
421 p_module_plugins != NULL ;
\r
422 p_module_plugins = p_module_plugins->next )
\r
424 if( p_module_plugins->i_capabilities &
\r
425 ( 1 << p_item->i_value ) )
\r
427 ListItem = GroupBoxPlugin->ListView->Items->Add();
\r
428 ListItem->Caption = p_module_plugins->psz_name;
\r
434 case MODULE_CONFIG_ITEM_FILE:
\r
436 case MODULE_CONFIG_ITEM_STRING:
\r
438 /* add new groupbox for the config option */
\r
439 GroupBoxString = new TGroupBoxString( this, p_item );
\r
440 GroupBoxString->Parent = ScrollBox;
\r
442 /* add panel as separator */
\r
447 case MODULE_CONFIG_ITEM_INTEGER:
\r
449 /* add new groupbox for the config option */
\r
450 GroupBoxInteger = new TGroupBoxInteger( this, p_item );
\r
451 GroupBoxInteger->Parent = ScrollBox;
\r
453 /* add panel as separator */
\r
458 case MODULE_CONFIG_ITEM_BOOL:
\r
460 /* add new groupbox for the config option */
\r
461 GroupBoxBool = new TGroupBoxBool( this, p_item );
\r
462 GroupBoxBool->Parent = ScrollBox;
\r
464 /* add panel as separator */
\r
472 while( p_item->i_type != MODULE_CONFIG_HINT_END );
\r
474 /* Reorder groupboxes inside the tabsheets */
\r
475 for( i_pages = 0; i_pages < PageControlPref->PageCount; i_pages++ )
\r
477 /* get scrollbox from the tabsheet */
\r
478 ScrollBox = (TScrollBox *)PageControlPref->Pages[i_pages]->Controls[0];
\r
480 for( i_ctrl = ScrollBox->ControlCount - 1; i_ctrl >= 0 ; i_ctrl-- )
\r
482 ScrollBox->Controls[i_ctrl]->Align = alTop;
\r
486 /* set active tabsheet
\r
487 * FIXME: i don't know why, but both lines are necessary */
\r
488 PageControlPref->ActivePageIndex = 1;
\r
489 PageControlPref->ActivePageIndex = 0;
\r
492 //---------------------------------------------------------------------------
\r
493 void __fastcall TPreferencesDlg::ButtonOkClick( TObject *Sender )
\r
495 ButtonApplyClick( Sender );
\r
498 //---------------------------------------------------------------------------
\r
499 void __fastcall TPreferencesDlg::ButtonApplyClick( TObject *Sender )
\r
501 TScrollBox *ScrollBox;
\r
502 TGroupBoxPref *GroupBox;
\r
505 for( i = 0; i < PageControlPref->PageCount; i++ )
\r
507 /* get scrollbox from the tabsheet */
\r
508 ScrollBox = (TScrollBox *)PageControlPref->Pages[i]->Controls[0];
\r
510 for( j = 0; j < ScrollBox->ControlCount ; j++ )
\r
512 /* skip the panels */
\r
513 if( ScrollBox->Controls[j]->InheritsFrom( __classid( TGroupBoxPref ) ) )
\r
515 GroupBox = (TGroupBoxPref *)ScrollBox->Controls[j];
\r
516 GroupBox->UpdateChanges();
\r
517 SaveValue( GroupBox->p_config );
\r
522 //---------------------------------------------------------------------------
\r
523 void __fastcall TPreferencesDlg::ButtonSaveClick( TObject *Sender )
\r
525 ButtonApplyClick( Sender );
\r
526 config_SaveConfigFile( NULL );
\r
528 //---------------------------------------------------------------------------
\r
529 void __fastcall TPreferencesDlg::ButtonCancelClick( TObject *Sender )
\r
533 //---------------------------------------------------------------------------
\r
534 void __fastcall TPreferencesDlg::SaveValue( module_config_t *p_config )
\r
536 switch( p_config->i_type )
\r
538 case MODULE_CONFIG_ITEM_STRING:
\r
539 case MODULE_CONFIG_ITEM_FILE:
\r
540 case MODULE_CONFIG_ITEM_MODULE:
\r
541 config_PutPszVariable( p_config->psz_name,
\r
542 *p_config->psz_value ? p_config->psz_value : NULL );
\r
544 case MODULE_CONFIG_ITEM_INTEGER:
\r
545 case MODULE_CONFIG_ITEM_BOOL:
\r
546 config_PutIntVariable( p_config->psz_name, p_config->i_value );
\r
550 //---------------------------------------------------------------------------
\r