]> git.sesse.net Git - vlc/blob - modules/gui/win32/preferences.cpp
3c1713ae738519d5b7988fe79f4216a80e757251
[vlc] / modules / gui / win32 / preferences.cpp
1 /*****************************************************************************\r
2  * preferences.cpp: the "Preferences" dialog box\r
3  *****************************************************************************\r
4  * Copyright (C) 2002-2003 VideoLAN\r
5  *\r
6  * Authors: Olivier Teuliere <ipkiss@via.ecp.fr>\r
7  *          Boris Dores <babal@via.ecp.fr>\r
8  *\r
9  * This program is free software; you can redistribute it and/or modify\r
10  * it under the terms of the GNU General Public License as published by\r
11  * the Free Software Foundation; either version 2 of the License, or\r
12  * (at your option) any later version.\r
13  *\r
14  * This program is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU General Public License for more details.\r
18  *\r
19  * You should have received a copy of the GNU General Public License\r
20  * along with this program; if not, write to the Free Software\r
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.\r
22  *****************************************************************************/\r
23 \r
24 #include <vcl.h>\r
25 #pragma hdrstop\r
26 \r
27 #include <stdlib.h>                                      /* malloc(), free() */\r
28 #include <string.h>                                                /* strcmp */\r
29 \r
30 #include <vlc/vlc.h>\r
31 #include <vlc/intf.h>\r
32 \r
33 #include "preferences.h"\r
34 #include "win32_common.h"\r
35 \r
36 //---------------------------------------------------------------------------\r
37 //#pragma package(smart_init)\r
38 #pragma link "CSPIN"\r
39 #pragma resource "*.dfm"\r
40 \r
41 /****************************************************************************\r
42  * A THintWindow with a limited width\r
43  ****************************************************************************/\r
44 void __fastcall TNarrowHintWindow::ActivateHint( const Windows::TRect &Rect,\r
45     const System::AnsiString AHint )\r
46 {\r
47     TRect NarrowRect = CalcHintRect( 300, AHint, NULL );\r
48     NarrowRect.Left = Rect.Left;\r
49     NarrowRect.Top = Rect.Top;\r
50     NarrowRect.Right += Rect.Left;\r
51     NarrowRect.Bottom += Rect.Top;\r
52     THintWindow::ActivateHint( NarrowRect, AHint );\r
53 }\r
54 \r
55 \r
56 /****************************************************************************\r
57  * Just a wrapper to embed an AnsiString into a TObject\r
58  ****************************************************************************/\r
59 __fastcall TObjectString::TObjectString( char * String )\r
60 {\r
61     FString = AnsiString( String );\r
62 }\r
63 //---------------------------------------------------------------------------\r
64 AnsiString __fastcall TObjectString::String()\r
65 {\r
66     return FString;\r
67 }\r
68 \r
69 \r
70 /****************************************************************************\r
71  * A TCheckListBox that automatically disposes any TObject\r
72  * associated with the string items\r
73  ****************************************************************************/\r
74 __fastcall TCleanCheckListBox::~TCleanCheckListBox()\r
75 {\r
76     for( int i = 0 ; i < Items->Count ; i++ )\r
77     {\r
78         if( Items->Objects[i] != NULL )\r
79             delete Items->Objects[i];\r
80     }\r
81 }\r
82 \r
83 \r
84 /****************************************************************************\r
85  * Functions to help components creation\r
86  ****************************************************************************/\r
87 __fastcall TPanelPref::TPanelPref( TComponent* Owner,\r
88     module_config_t *_p_config, intf_thread_t *_p_intf ) : TPanel( Owner )\r
89 {\r
90     p_intf = _p_intf;\r
91     p_config = _p_config;\r
92     BevelInner = bvNone;\r
93     BevelOuter = bvNone;\r
94     BorderStyle = bsNone;\r
95 }\r
96 //---------------------------------------------------------------------------\r
97 TCleanCheckListBox * __fastcall TPanelPref::CreateCleanCheckListBox(\r
98     TWinControl *Parent, int Left, int Width, int Top, int Height )\r
99 {\r
100     TCleanCheckListBox *CleanCheckListBox = new TCleanCheckListBox( Parent );\r
101     CleanCheckListBox->Parent = Parent;\r
102     CleanCheckListBox->Left = Left;\r
103     CleanCheckListBox->Width = Width;\r
104     CleanCheckListBox->Top = Top;\r
105     CleanCheckListBox->Height = Height;\r
106     return CleanCheckListBox;\r
107 }\r
108 //---------------------------------------------------------------------------\r
109 TButton * __fastcall TPanelPref::CreateButton( TWinControl *Parent,\r
110             int Left, int Width, int Top, int Height, AnsiString Caption )\r
111 {\r
112     TButton *Button = new TButton( Parent );\r
113     Button->Parent = Parent;\r
114     Button->Left = Left;\r
115     Button->Width = Width;\r
116     Button->Top = Top;\r
117     Button->Height = Height;\r
118     Button->Caption = Caption;\r
119     return Button;\r
120 }\r
121 //---------------------------------------------------------------------------\r
122 TCheckBox * __fastcall TPanelPref::CreateCheckBox( TWinControl *Parent,\r
123             int Left, int Width, int Top, int Height, AnsiString Caption )\r
124 {\r
125     TCheckBox *CheckBox = new TCheckBox( Parent );\r
126     CheckBox->Parent = Parent;\r
127     CheckBox->Left = Left;\r
128     CheckBox->Width = Width;\r
129     CheckBox->Top = Top;\r
130     CheckBox->Height = Height;\r
131     CheckBox->Caption = Caption;\r
132     return CheckBox;\r
133 }\r
134 //---------------------------------------------------------------------------\r
135 TLabel * __fastcall TPanelPref::CreateLabel( TWinControl *Parent,\r
136             int Left, int Width, int Top, int Height, AnsiString Caption,\r
137             bool WordWrap )\r
138 {\r
139     TLabel *Label = new TLabel( Parent );\r
140     Label->Parent = Parent;\r
141     Label->Caption = Caption;\r
142     Label->Left = Left;\r
143     Label->Width = Width;\r
144     Label->Top = Top;\r
145     Label->Height = Height;\r
146     Label->WordWrap = WordWrap;\r
147     return Label;\r
148 }\r
149 //---------------------------------------------------------------------------\r
150 TEdit * __fastcall TPanelPref::CreateEdit( TWinControl *Parent,\r
151             int Left, int Width, int Top, int Height, AnsiString Text )\r
152 {\r
153     TEdit *Edit = new TEdit( Parent );\r
154     Edit->Parent = Parent;\r
155     Edit->Left = Left;\r
156     Edit->Width = Width;\r
157     Edit->Top = Top;\r
158     Edit->Height = Height;\r
159     Edit->Text = Text;\r
160     return Edit;\r
161 }\r
162 //---------------------------------------------------------------------------\r
163 TCSpinEdit * __fastcall TPanelPref::CreateSpinEdit( TWinControl *Parent,\r
164             int Left, int Width, int Top, int Height,\r
165             long Min, long Max, long Value )\r
166 {\r
167     TCSpinEdit *SpinEdit = new TCSpinEdit( Parent );\r
168     SpinEdit->Parent = Parent;\r
169     SpinEdit->Left = Left;\r
170     SpinEdit->Width = Width;\r
171     SpinEdit->Top = Top;\r
172     SpinEdit->Height = Height;\r
173     SpinEdit->MinValue = Min;\r
174     SpinEdit->MaxValue = Max;\r
175     SpinEdit->Value = Value;\r
176     return SpinEdit;\r
177 }\r
178 //---------------------------------------------------------------------------\r
179 \r
180 #define LIBWIN32_PREFSIZE_VPAD                4\r
181 #define LIBWIN32_PREFSIZE_HPAD                4\r
182 #define LIBWIN32_PREFSIZE_LEFT                16\r
183 #define LIBWIN32_PREFSIZE_EDIT_LEFT           (LIBWIN32_PREFSIZE_LEFT+32)\r
184 #define LIBWIN32_PREFSIZE_WIDTH               375\r
185 #define LIBWIN32_PREFSIZE_EDIT_WIDTH          (LIBWIN32_PREFSIZE_WIDTH-32)\r
186 #define LIBWIN32_PREFSIZE_BUTTON_WIDTH        150\r
187 #define LIBWIN32_PREFSIZE_SPINEDIT_WIDTH      100\r
188 #define LIBWIN32_PREFSIZE_RIGHT               (LIBWIN32_PREFSIZE_LEFT+LIBWIN32_PREFSIZE_WIDTH)\r
189 #define LIBWIN32_PREFSIZE_BUTTON_HEIGHT       25\r
190 #define LIBWIN32_PREFSIZE_LABEL_HEIGHT        26\r
191 #define LIBWIN32_PREFSIZE_CHECKLISTBOX_HEIGHT 120\r
192 #define LIBWIN32_PREFSIZE_EDIT_HEIGHT         21\r
193 #define LIBWIN32_PREFSIZE_CHECKBOX_HEIGHT     17\r
194 #define LIBWIN32_PREFSIZE_SPINEDIT_HEIGHT     21\r
195 \r
196 /****************************************************************************\r
197  * Panel for module management\r
198  ****************************************************************************/\r
199 __fastcall TPanelPlugin::TPanelPlugin( TComponent* Owner,\r
200         module_config_t *p_config, intf_thread_t *_p_intf,\r
201         bool b_multi_plugins ) : TPanelPref( Owner, p_config, _p_intf )\r
202 {\r
203     this->b_multi_plugins = b_multi_plugins;\r
204 \r
205     /* init configure button */\r
206     ButtonConfig = CreateButton( this,\r
207             LIBWIN32_PREFSIZE_RIGHT - LIBWIN32_PREFSIZE_BUTTON_WIDTH,\r
208             LIBWIN32_PREFSIZE_BUTTON_WIDTH,\r
209             LIBWIN32_PREFSIZE_VPAD,\r
210             LIBWIN32_PREFSIZE_BUTTON_HEIGHT,\r
211             "Configure..." );\r
212     ButtonConfig->Enabled = false;\r
213     ButtonConfig->OnClick = ButtonConfigClick;\r
214 \r
215     /* init label */\r
216     AnsiString Text = AnsiString( p_config->psz_text ) + ":";\r
217     Label = CreateLabel( this,\r
218             LIBWIN32_PREFSIZE_LEFT,\r
219             LIBWIN32_PREFSIZE_RIGHT - LIBWIN32_PREFSIZE_BUTTON_WIDTH\r
220              - LIBWIN32_PREFSIZE_HPAD,\r
221             LIBWIN32_PREFSIZE_VPAD,\r
222             LIBWIN32_PREFSIZE_LABEL_HEIGHT,\r
223             Text.c_str(), true );\r
224 \r
225     /* vertical alignement */\r
226     if ( ButtonConfig->Height > Label->Height )\r
227         Label->Top += ( ButtonConfig->Height - Label->Height ) / 2;\r
228     else\r
229         ButtonConfig->Top += ( Label->Height - ButtonConfig->Height ) / 2;\r
230 \r
231     /* init checklistbox */\r
232     CleanCheckListBox = CreateCleanCheckListBox( this,\r
233             LIBWIN32_PREFSIZE_EDIT_LEFT,\r
234             LIBWIN32_PREFSIZE_EDIT_WIDTH,\r
235             max( Label->Top + Label->Height , ButtonConfig->Top\r
236                  + ButtonConfig->Height ) + LIBWIN32_PREFSIZE_VPAD,\r
237                  LIBWIN32_PREFSIZE_CHECKLISTBOX_HEIGHT );\r
238     CleanCheckListBox->OnClick = CheckListBoxClick;\r
239     CleanCheckListBox->OnClickCheck = CheckListBoxClickCheck;\r
240     CleanCheckListBox->Hint = p_config->psz_longtext;\r
241     CleanCheckListBox->ShowHint = true;\r
242 \r
243     /* init up and down buttons */\r
244     if ( b_multi_plugins )\r
245     {\r
246         ButtonUp = CreateButton ( this, LIBWIN32_PREFSIZE_LEFT,\r
247                 CleanCheckListBox->Left - LIBWIN32_PREFSIZE_HPAD\r
248                     - LIBWIN32_PREFSIZE_LEFT,\r
249                 CleanCheckListBox->Top + ( CleanCheckListBox->Height\r
250                     - 2*LIBWIN32_PREFSIZE_BUTTON_HEIGHT ) / 3,\r
251                 LIBWIN32_PREFSIZE_BUTTON_HEIGHT,\r
252                 "+" );\r
253         ButtonUp->Enabled = false;\r
254         ButtonUp->OnClick = ButtonUpClick;\r
255         ButtonUp->Hint = "Raise the plugin priority";\r
256         ButtonUp->ShowHint = true;\r
257 \r
258         ButtonDown = CreateButton ( this, LIBWIN32_PREFSIZE_LEFT,\r
259                 CleanCheckListBox->Left - LIBWIN32_PREFSIZE_HPAD\r
260                     - LIBWIN32_PREFSIZE_LEFT,\r
261                 CleanCheckListBox->Top + ( CleanCheckListBox->Height\r
262                     - 2*LIBWIN32_PREFSIZE_BUTTON_HEIGHT ) * 2 / 3\r
263                     + LIBWIN32_PREFSIZE_BUTTON_HEIGHT,\r
264                 LIBWIN32_PREFSIZE_BUTTON_HEIGHT,\r
265                 "-" );\r
266         ButtonDown->Enabled = false;\r
267         ButtonDown->OnClick = ButtonDownClick;\r
268         ButtonDown->Hint = "Decrease the plugin priority";\r
269         ButtonDown->ShowHint = true;\r
270     }\r
271 \r
272     /* panel height */\r
273     Height = CleanCheckListBox->Top + CleanCheckListBox->Height\r
274             + LIBWIN32_PREFSIZE_VPAD;\r
275 };\r
276 //---------------------------------------------------------------------------\r
277 void __fastcall TPanelPlugin::CheckListBoxClick( TObject *Sender )\r
278 {\r
279     module_t *p_parser;\r
280     vlc_list_t list;\r
281     int i_index;\r
282 \r
283     /* check that the click is valid (we are on an item, and the click\r
284      * started on an item */\r
285     if( CleanCheckListBox->ItemIndex == -1 )\r
286     {\r
287         ButtonUp->Enabled = false;\r
288         ButtonDown->Enabled = false;\r
289         return;\r
290     }\r
291 \r
292     AnsiString Name = ((TObjectString*)CleanCheckListBox->Items->\r
293         Objects[CleanCheckListBox->ItemIndex])->String().c_str();\r
294     if( Name == "" )\r
295         return;\r
296 \r
297     /* enable up and down buttons */\r
298     if ( b_multi_plugins )\r
299     {\r
300         if ( CleanCheckListBox->ItemIndex != -1 )\r
301         {\r
302             if ( CleanCheckListBox->ItemIndex == 0 )\r
303                 ButtonUp->Enabled = false; else ButtonUp->Enabled = true;\r
304             if ( CleanCheckListBox->ItemIndex\r
305                 == CleanCheckListBox->Items->Count - 1 )\r
306                 ButtonDown->Enabled = false; else ButtonDown->Enabled = true;\r
307         }\r
308     }\r
309 \r
310     /* look for module 'Name' */\r
311     list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );\r
312 \r
313     for( i_index = 0; i_index < list.i_count; i_index++ )\r
314     {\r
315         p_parser = (module_t *)list.p_values[i_index].p_object ;\r
316 \r
317         if( strcmp( p_parser->psz_object_name, Name.c_str() ) == 0 )\r
318         {\r
319             ModuleSelected = p_parser;\r
320             ButtonConfig->Enabled =\r
321                 p_parser->i_config_items ? true : false;\r
322 \r
323             break;\r
324         }\r
325     }\r
326 }\r
327 //---------------------------------------------------------------------------\r
328 void __fastcall TPanelPlugin::CheckListBoxClickCheck( TObject *Sender )\r
329 {\r
330     if ( ! b_multi_plugins )\r
331     {\r
332         /* one item maximum must be checked */\r
333         if( CleanCheckListBox->Checked[CleanCheckListBox->ItemIndex] )\r
334         {\r
335             for( int item = 0; item < CleanCheckListBox->Items->Count; item++ )\r
336             {\r
337                 if( item != CleanCheckListBox->ItemIndex )\r
338                 {\r
339                     CleanCheckListBox->Checked[item] = false;\r
340                 }\r
341             }\r
342         }\r
343     }\r
344 }\r
345 //---------------------------------------------------------------------------\r
346 void __fastcall TPanelPlugin::ButtonConfigClick( TObject *Sender )\r
347 {\r
348     p_intf->p_sys->p_window->\r
349                         CreatePreferences( ModuleSelected->psz_object_name );\r
350 }\r
351 //---------------------------------------------------------------------------\r
352 void __fastcall TPanelPlugin::ButtonUpClick( TObject *Sender )\r
353 {\r
354     if( CleanCheckListBox->ItemIndex != -1 && CleanCheckListBox->ItemIndex > 0 )\r
355     {\r
356         int Pos = CleanCheckListBox->ItemIndex;\r
357         CleanCheckListBox->Items->Move ( Pos , Pos - 1 );\r
358         CleanCheckListBox->ItemIndex = Pos - 1;\r
359         CheckListBoxClick ( Sender );\r
360     }\r
361 }\r
362 //---------------------------------------------------------------------------\r
363 void __fastcall TPanelPlugin::ButtonDownClick( TObject *Sender )\r
364 {\r
365     if( CleanCheckListBox->ItemIndex != -1\r
366         && CleanCheckListBox->ItemIndex < CleanCheckListBox->Items->Count - 1 )\r
367     {\r
368         int Pos = CleanCheckListBox->ItemIndex;\r
369         CleanCheckListBox->Items->Move ( Pos , Pos + 1 );\r
370         CleanCheckListBox->ItemIndex = Pos + 1;\r
371         CheckListBoxClick ( Sender );\r
372     }\r
373 }\r
374 //---------------------------------------------------------------------------\r
375 void __fastcall TPanelPlugin::SetValue ( AnsiString Values )\r
376 {\r
377     int TopChecked = 0;\r
378     while ( Values.Length() != 0 )\r
379     {\r
380         AnsiString Value;\r
381 \r
382         int NextValue = Values.Pos ( "," );\r
383         if ( NextValue == 0 )\r
384         {\r
385             Value = Values.Trim();\r
386             Values = "";\r
387         }\r
388         else\r
389         {\r
390             Value = Values.SubString(1,NextValue-1).Trim();\r
391             Values = Values.SubString ( NextValue + 1\r
392                     , Values.Length() - NextValue );\r
393         }\r
394 \r
395         if ( Value.Length() > 0 )\r
396         {\r
397             for ( int i = TopChecked; i < CleanCheckListBox->Items->Count; i++ )\r
398             {\r
399                 if ( ((TObjectString*)CleanCheckListBox->Items->Objects[i])\r
400                         ->String() == Value )\r
401                 {\r
402                     CleanCheckListBox->Checked[i] = true;\r
403                     CleanCheckListBox->Items->Move ( i , TopChecked );\r
404                     TopChecked++;\r
405                 }\r
406             }\r
407         }\r
408     }\r
409 }\r
410 //---------------------------------------------------------------------------\r
411 void __fastcall TPanelPlugin::UpdateChanges()\r
412 {\r
413     AnsiString Name = "";\r
414 \r
415     /* find the selected plugin (if any) */\r
416     for( int item = 0; item < CleanCheckListBox->Items->Count; item++ )\r
417     {\r
418         if( CleanCheckListBox->Checked[item] )\r
419         {\r
420             if ( Name.Length() == 0 )\r
421             {\r
422                 Name = ((TObjectString*)CleanCheckListBox->Items->Objects[item])\r
423                        ->String();\r
424             }\r
425             else\r
426             {\r
427                 Name = Name + ","\r
428                      + ((TObjectString*)CleanCheckListBox->Items->Objects[item])\r
429                        ->String();\r
430             }\r
431         }\r
432     }\r
433 \r
434     config_PutPsz( p_intf, p_config->psz_name,\r
435                    Name.Length() ? Name.c_str() : NULL );\r
436 }\r
437 \r
438 \r
439 /****************************************************************************\r
440  * Panel for string management\r
441  ****************************************************************************/\r
442 __fastcall TPanelString::TPanelString( TComponent* Owner,\r
443         module_config_t *p_config, intf_thread_t *_p_intf )\r
444         : TPanelPref( Owner, p_config, _p_intf )\r
445 {\r
446     /* init description label */\r
447     AnsiString Text = AnsiString ( p_config->psz_text ) + ":";\r
448     Label = CreateLabel( this,\r
449             LIBWIN32_PREFSIZE_LEFT,\r
450             LIBWIN32_PREFSIZE_WIDTH,\r
451             LIBWIN32_PREFSIZE_VPAD,\r
452             LIBWIN32_PREFSIZE_LABEL_HEIGHT,\r
453             Text.c_str(), true );\r
454 \r
455     /* init edit */\r
456     Edit = CreateEdit( this,\r
457             LIBWIN32_PREFSIZE_EDIT_LEFT,\r
458             LIBWIN32_PREFSIZE_EDIT_WIDTH,\r
459             LIBWIN32_PREFSIZE_VPAD + Label->Height + LIBWIN32_PREFSIZE_VPAD,\r
460             LIBWIN32_PREFSIZE_EDIT_HEIGHT, "" );\r
461     vlc_mutex_lock( p_config->p_lock );\r
462     Edit->Text = p_config->psz_value ? p_config->psz_value : "";\r
463     vlc_mutex_unlock( p_config->p_lock );\r
464     Edit->Hint = p_config->psz_longtext;\r
465     Edit->ShowHint = true;\r
466 \r
467     /* panel height */\r
468     Height = LIBWIN32_PREFSIZE_VPAD + Label->Height + LIBWIN32_PREFSIZE_VPAD\r
469             + Edit->Height + LIBWIN32_PREFSIZE_VPAD;\r
470 };\r
471 //---------------------------------------------------------------------------\r
472 void __fastcall TPanelString::UpdateChanges()\r
473 {\r
474     config_PutPsz( p_intf, p_config->psz_name,\r
475                    Edit->Text.Length() ? Edit->Text.c_str() : NULL );\r
476 }\r
477 \r
478 \r
479 /****************************************************************************\r
480  * Panel for integer management\r
481  ****************************************************************************/\r
482 __fastcall TPanelInteger::TPanelInteger( TComponent* Owner,\r
483         module_config_t *p_config, intf_thread_t *_p_intf )\r
484         : TPanelPref( Owner, p_config, _p_intf )\r
485 {\r
486     /* init description label */\r
487     AnsiString Text = AnsiString ( p_config->psz_text ) + ":";\r
488     Label = CreateLabel( this,\r
489             LIBWIN32_PREFSIZE_LEFT,\r
490             LIBWIN32_PREFSIZE_WIDTH - LIBWIN32_PREFSIZE_SPINEDIT_WIDTH\r
491              - LIBWIN32_PREFSIZE_HPAD,\r
492             LIBWIN32_PREFSIZE_VPAD,\r
493             LIBWIN32_PREFSIZE_LABEL_HEIGHT, Text.c_str(), true );\r
494 \r
495     /* init spinedit */\r
496     SpinEdit = CreateSpinEdit( this,\r
497             LIBWIN32_PREFSIZE_RIGHT - LIBWIN32_PREFSIZE_SPINEDIT_WIDTH,\r
498             LIBWIN32_PREFSIZE_SPINEDIT_WIDTH,\r
499             LIBWIN32_PREFSIZE_VPAD,\r
500             LIBWIN32_PREFSIZE_SPINEDIT_HEIGHT,\r
501             -1, 100000, p_config->i_value );\r
502     SpinEdit->Hint = p_config->psz_longtext;\r
503     SpinEdit->ShowHint = true;\r
504 \r
505     /* vertical alignement and panel height */\r
506     if ( SpinEdit->Height > Label->Height )\r
507     {\r
508         Label->Top += ( SpinEdit->Height - Label->Height ) / 2;\r
509         Height = SpinEdit->Top + SpinEdit->Height + LIBWIN32_PREFSIZE_VPAD;\r
510     }\r
511     else\r
512     {\r
513         SpinEdit->Top += ( Label->Height - SpinEdit->Height ) / 2;\r
514         Height = Label->Top + Label->Height + LIBWIN32_PREFSIZE_VPAD;\r
515     }\r
516 };\r
517 //---------------------------------------------------------------------------\r
518 void __fastcall TPanelInteger::UpdateChanges()\r
519 {\r
520     /* Warning: we're casting from long to int */\r
521     config_PutInt( p_intf, p_config->psz_name, (int)SpinEdit->Value );\r
522 }\r
523 \r
524 \r
525 /****************************************************************************\r
526  * Panel for float management\r
527  ****************************************************************************/\r
528 __fastcall TPanelFloat::TPanelFloat( TComponent* Owner,\r
529         module_config_t *p_config, intf_thread_t *_p_intf )\r
530         : TPanelPref( Owner, p_config, _p_intf )\r
531 {\r
532 #define MAX_FLOAT_CHARS 20\r
533     /* init description label */\r
534     AnsiString Text = AnsiString( p_config->psz_text ) + ":";\r
535     Label = CreateLabel( this,\r
536             LIBWIN32_PREFSIZE_LEFT,\r
537             LIBWIN32_PREFSIZE_WIDTH,\r
538             LIBWIN32_PREFSIZE_VPAD,\r
539             LIBWIN32_PREFSIZE_LABEL_HEIGHT,\r
540             Text.c_str(), true );\r
541 \r
542     /* init edit */\r
543     char *psz_value = (char *)malloc( MAX_FLOAT_CHARS );\r
544     snprintf( psz_value, MAX_FLOAT_CHARS, "%f", p_config->f_value );\r
545     /* we use the spinedit size, to be similar with the integers */\r
546     Edit = CreateEdit( this,\r
547             LIBWIN32_PREFSIZE_RIGHT - LIBWIN32_PREFSIZE_SPINEDIT_WIDTH,\r
548             LIBWIN32_PREFSIZE_SPINEDIT_WIDTH,\r
549             LIBWIN32_PREFSIZE_VPAD,\r
550             LIBWIN32_PREFSIZE_SPINEDIT_HEIGHT,\r
551             psz_value );\r
552     free( psz_value );\r
553     Edit->Hint = p_config->psz_longtext;\r
554     Edit->ShowHint = true;\r
555 \r
556     /* vertical alignement and panel height */\r
557     if ( Edit->Height > Label->Height )\r
558     {\r
559         Label->Top += ( Edit->Height - Label->Height ) / 2;\r
560         Height = Edit->Top + Edit->Height + LIBWIN32_PREFSIZE_VPAD;\r
561     }\r
562     else\r
563     {\r
564         Edit->Top += ( Label->Height - Edit->Height ) / 2;\r
565         Height = Label->Top + Label->Height + LIBWIN32_PREFSIZE_VPAD;\r
566     }\r
567 \r
568 #undef MAX_FLOAT_CHARS\r
569 };\r
570 //---------------------------------------------------------------------------\r
571 void __fastcall TPanelFloat::UpdateChanges()\r
572 {\r
573     /* Warning: we're casting from double to float */\r
574     config_PutFloat( p_intf, p_config->psz_name, atof( Edit->Text.c_str() ) );\r
575 }\r
576 \r
577 \r
578 /****************************************************************************\r
579  * Panel for boolean management\r
580  ****************************************************************************/\r
581 __fastcall TPanelBool::TPanelBool( TComponent* Owner,\r
582         module_config_t *p_config, intf_thread_t *_p_intf )\r
583         : TPanelPref( Owner, p_config, _p_intf )\r
584 {\r
585     /* init checkbox */\r
586     CheckBox = CreateCheckBox( this,\r
587             LIBWIN32_PREFSIZE_LEFT,\r
588             LIBWIN32_PREFSIZE_WIDTH,\r
589             LIBWIN32_PREFSIZE_VPAD,\r
590             LIBWIN32_PREFSIZE_CHECKBOX_HEIGHT, p_config->psz_text );\r
591     CheckBox->Checked = p_config->i_value;\r
592     CheckBox->Hint = p_config->psz_longtext;\r
593     CheckBox->ShowHint = true;\r
594 \r
595     /* panel height */\r
596     Height = LIBWIN32_PREFSIZE_VPAD + CheckBox->Height + LIBWIN32_PREFSIZE_VPAD;\r
597 };\r
598 //---------------------------------------------------------------------------\r
599 void __fastcall TPanelBool::UpdateChanges()\r
600 {\r
601     config_PutInt( p_intf, p_config->psz_name, CheckBox->Checked ? 1 : 0 );\r
602 }\r
603 \r
604 \r
605 /****************************************************************************\r
606  * Callbacks for the dialog\r
607  ****************************************************************************/\r
608 __fastcall TPreferencesDlg::TPreferencesDlg( TComponent* Owner,\r
609     intf_thread_t *_p_intf ) : TForm( Owner )\r
610 {\r
611     p_intf = _p_intf;\r
612     Icon = p_intf->p_sys->p_window->Icon;\r
613     Application->HintHidePause = 0x1000000;\r
614     HintWindowClass = __classid ( TNarrowHintWindow );\r
615     /* prevent the form from being resized horizontally */\r
616     Constraints->MinWidth = Width;\r
617     Constraints->MaxWidth = Width;\r
618 }\r
619 //---------------------------------------------------------------------------\r
620 void __fastcall TPreferencesDlg::FormClose( TObject *Sender,\r
621       TCloseAction &Action )\r
622 {\r
623     Action = caHide;\r
624 }\r
625 \r
626 \r
627 /****************************************************************************\r
628  * CreateConfigDialog: dynamically creates the configuration dialog\r
629  * box from all the configuration data provided by the selected module.\r
630  ****************************************************************************/\r
631 #define ADD_PANEL                               \\r
632 {                                               \\r
633             Panel = new TPanel( this );         \\r
634             Panel->Parent = ScrollBox;          \\r
635             Panel->Caption = "";                \\r
636             Panel->BevelOuter = bvNone;         \\r
637             Panel->Height = 12;                 \\r
638 }\r
639 \r
640 void __fastcall TPreferencesDlg::CreateConfigDialog( char *psz_module_name )\r
641 {\r
642     module_t           *p_parser;\r
643     vlc_list_t          list;\r
644     int                 i_index;\r
645 \r
646     module_config_t    *p_item;\r
647     int                 i_pages, i_ctrl;\r
648 \r
649     TTabSheet          *TabSheet;\r
650     TScrollBox         *ScrollBox = NULL;\r
651     TPanel             *Panel;\r
652     TPanelPlugin       *PanelPlugin;\r
653     TPanelString       *PanelString;\r
654     TPanelInteger      *PanelInteger;\r
655     TPanelFloat        *PanelFloat;\r
656     TPanelBool         *PanelBool;\r
657 \r
658     /* Look for the selected module */\r
659     list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );\r
660 \r
661     for( i_index = 0; i_index < list.i_count; i_index++ )\r
662     {\r
663         p_parser = (module_t *)list.p_values[i_index].p_object ;\r
664 \r
665         if( psz_module_name\r
666              && !strcmp( psz_module_name, p_parser->psz_object_name ) )\r
667         {\r
668             break;\r
669         }\r
670     }\r
671     if( !p_parser || i_index == list.i_count )\r
672     {\r
673         vlc_list_release( &list );\r
674         return;\r
675     }\r
676 \r
677     /*\r
678      * We found it, now we can start building its configuration interface\r
679      */\r
680 \r
681     /* Enumerate config options and add corresponding config boxes */\r
682     p_item = p_parser->p_config;\r
683     if( p_item ) do\r
684     {\r
685         switch( p_item->i_type )\r
686         {\r
687         case CONFIG_HINT_CATEGORY:\r
688 \r
689             /* create a new tabsheet. */\r
690             TabSheet = new TTabSheet( this );\r
691             TabSheet->PageControl = PageControlPref;\r
692             TabSheet->Caption = p_item->psz_text;\r
693             TabSheet->Visible = true;\r
694 \r
695             /* pack a scrollbox into the tabsheet */\r
696             ScrollBox = new TScrollBox( this );\r
697             ScrollBox->Parent = TabSheet;\r
698             ScrollBox->Align = alClient;\r
699             ScrollBox->BorderStyle = bsNone;\r
700             ScrollBox->HorzScrollBar->Tracking = true;\r
701             ScrollBox->VertScrollBar->Tracking = true;\r
702 \r
703             /* add a panel as top margin */\r
704             ADD_PANEL;\r
705 \r
706             break;\r
707 \r
708         case CONFIG_ITEM_MODULE:\r
709 \r
710             /* add new panel for the config option */\r
711             PanelPlugin = new TPanelPlugin( this, p_item, p_intf, true );\r
712             PanelPlugin->Parent = ScrollBox;\r
713 \r
714             /* Look for valid modules */\r
715             for( i_index = 0; i_index < list.i_count; i_index++ )\r
716             {\r
717                 p_parser = (module_t *)list.p_values[i_index].p_object ;\r
718 \r
719                 if( !strcmp( p_parser->psz_capability, p_item->psz_type ) )\r
720                 {\r
721                     AnsiString ModuleDesc;\r
722                     if ( p_parser->psz_longname != NULL ) {\r
723                         ModuleDesc = AnsiString( p_parser->psz_longname ) +\r
724                             " (" + AnsiString( p_parser->psz_object_name ) +\r
725                             ")";\r
726                     }\r
727                     else\r
728                         ModuleDesc = AnsiString( p_parser->psz_object_name );\r
729 \r
730                     PanelPlugin->CleanCheckListBox->Items->AddObject(\r
731                         ModuleDesc.c_str(),\r
732                         new TObjectString( p_parser->psz_object_name ) );\r
733                 }\r
734             }\r
735 \r
736             /* check relevant boxes */\r
737             PanelPlugin->SetValue ( AnsiString ( p_item->psz_value ) );\r
738 \r
739             break;\r
740 \r
741         case CONFIG_ITEM_FILE:\r
742 \r
743         case CONFIG_ITEM_STRING:\r
744 \r
745             /* add new panel for the config option */\r
746             PanelString = new TPanelString( this, p_item, p_intf );\r
747             PanelString->Parent = ScrollBox;\r
748 \r
749             break;\r
750 \r
751         case CONFIG_ITEM_INTEGER:\r
752 \r
753             /* add new panel for the config option */\r
754             PanelInteger = new TPanelInteger( this, p_item, p_intf );\r
755             PanelInteger->Parent = ScrollBox;\r
756 \r
757             break;\r
758 \r
759         case CONFIG_ITEM_FLOAT:\r
760 \r
761             /* add new panel for the config option */\r
762             PanelFloat = new TPanelFloat( this, p_item, p_intf );\r
763             PanelFloat->Parent = ScrollBox;\r
764 \r
765             break;\r
766 \r
767         case CONFIG_ITEM_BOOL:\r
768 \r
769             /* add new panel for the config option */\r
770             PanelBool = new TPanelBool( this, p_item, p_intf );\r
771             PanelBool->Parent = ScrollBox;\r
772 \r
773             break;\r
774         default:\r
775             msg_Warn( p_intf, "unknown config type: %i", p_item->i_type );\r
776             break;\r
777         }\r
778 \r
779         p_item++;\r
780     }\r
781     while( p_item->i_type != CONFIG_HINT_END );\r
782 \r
783     /* Reorder panels inside the tabsheets */\r
784     for( i_pages = 0; i_pages < PageControlPref->PageCount; i_pages++ )\r
785     {\r
786         /* get scrollbox from the tabsheet */\r
787         ScrollBox = (TScrollBox *)PageControlPref->Pages[i_pages]->Controls[0];\r
788 \r
789         /* add a panel as bottom margin */\r
790         ADD_PANEL;\r
791 \r
792         for( i_ctrl = ScrollBox->ControlCount - 1; i_ctrl >= 0 ; i_ctrl-- )\r
793         {\r
794             ScrollBox->Controls[i_ctrl]->Align = alTop;\r
795         }\r
796     }\r
797 \r
798     vlc_list_release( &list );\r
799 \r
800     /* set active tabsheet\r
801      * FIXME: i don't know why, but both lines are necessary */\r
802     PageControlPref->ActivePageIndex = 1;\r
803     PageControlPref->ActivePageIndex = 0;\r
804 }\r
805 #undef ADD_PANEL\r
806 //---------------------------------------------------------------------------\r
807 void __fastcall TPreferencesDlg::ButtonOkClick( TObject *Sender )\r
808 {\r
809     ButtonApplyClick( Sender );\r
810     Hide();\r
811 }\r
812 //---------------------------------------------------------------------------\r
813 void __fastcall TPreferencesDlg::ButtonApplyClick( TObject *Sender )\r
814 {\r
815     TScrollBox *ScrollBox;\r
816     TPanelPref *Panel;\r
817     int i, j;\r
818 \r
819     for( i = 0; i < PageControlPref->PageCount; i++ )\r
820     {\r
821         /* get scrollbox from the tabsheet */\r
822         ScrollBox = (TScrollBox *)PageControlPref->Pages[i]->Controls[0];\r
823 \r
824         for( j = 0; j < ScrollBox->ControlCount ; j++ )\r
825         {\r
826             /* skip the panels */\r
827             if( ScrollBox->Controls[j]->InheritsFrom( __classid( TPanelPref ) ) )\r
828             {\r
829                 Panel = (TPanelPref *)ScrollBox->Controls[j];\r
830                 Panel->UpdateChanges();\r
831             }\r
832         }\r
833     }\r
834 }\r
835 //---------------------------------------------------------------------------\r
836 void __fastcall TPreferencesDlg::ButtonSaveClick( TObject *Sender )\r
837 {\r
838     ButtonApplyClick( Sender );\r
839     config_SaveConfigFile( p_intf, NULL );\r
840 }\r
841 //---------------------------------------------------------------------------\r
842 void __fastcall TPreferencesDlg::ButtonCancelClick( TObject *Sender )\r
843 {\r
844     Hide();\r
845 }\r
846 //---------------------------------------------------------------------------\r