]> git.sesse.net Git - vlc/blob - modules/gui/wince/preferences.cpp
* modules/gui/wince: some more cleanup.
[vlc] / modules / gui / wince / preferences.cpp
1 /*****************************************************************************
2  * preferences.cpp : WinCE gui plugin for VLC
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Marodon Cedric <cedric_marodon@yahoo.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31 #include <vlc/vlc.h>
32 #include <vlc/intf.h>
33
34 #include "wince.h"
35
36 #include <winuser.h>
37 #include <windows.h>
38 #include <windowsx.h>
39 #include <commctrl.h>
40 #include <commdlg.h>
41
42 #include <vlc_config_cat.h>
43
44 #include "preferences_widgets.h"
45
46 #define GENERAL_ID 1242
47 #define PLUGIN_ID 1243
48 #define CAPABILITY_ID 1244
49
50 /*****************************************************************************
51  * Event Table.
52  *****************************************************************************/
53
54 /* IDs for the controls and the menu commands */
55 enum
56 {
57     Notebook_Event,
58     MRL_Event,
59     ResetAll_Event,
60     Advanced_Event
61 };
62
63 /*****************************************************************************
64  * Classes declarations.
65  *****************************************************************************/
66 class ConfigTreeData;
67 class PrefsTreeCtrl
68 {
69 public:
70
71     PrefsTreeCtrl() { }
72     PrefsTreeCtrl( intf_thread_t *_p_intf, PrefsDialog *p_prefs_dialog,
73                    HWND hwnd, HINSTANCE _hInst );
74     virtual ~PrefsTreeCtrl();
75
76     void ApplyChanges();
77     /*void CleanChanges();*/
78
79     void OnSelectTreeItem( LPNM_TREEVIEW pnmtv, HWND parent, HINSTANCE hInst );
80         
81     ConfigTreeData *FindModuleConfig( ConfigTreeData *config_data );
82
83     HWND hwndTV;
84
85 private:
86     intf_thread_t *p_intf;
87     PrefsDialog *p_prefs_dialog;
88     vlc_bool_t b_advanced;
89
90     HTREEITEM root_item;
91     HTREEITEM general_item;
92     HTREEITEM plugins_item;
93 };
94
95 class PrefsPanel
96 {
97 public:
98
99     PrefsPanel() { }
100     PrefsPanel( HWND parent, HINSTANCE hInst, intf_thread_t *_p_intf,
101                 PrefsDialog *, int i_object_id, char *, char * );
102     virtual ~PrefsPanel() {}
103
104     void Hide();
105     void Show();
106
107     HWND config_window;
108
109     int oldvalue;
110     int maxvalue;
111
112     void ApplyChanges();
113
114 private:
115     intf_thread_t *p_intf;
116     PrefsDialog *p_prefs_dialog;
117
118     vlc_bool_t b_advanced;
119
120     HWND label;
121
122     vector<ConfigControl *> config_array;
123 };
124
125 class ConfigTreeData
126 {
127 public:
128
129     ConfigTreeData() { b_submodule = 0; panel = NULL; psz_section = NULL;
130                        psz_help = NULL; }
131     virtual ~ConfigTreeData() { if( panel ) delete panel;
132                                 if( psz_section) free(psz_section);
133                                 if( psz_help) free(psz_help); }
134
135     vlc_bool_t b_submodule;
136
137     PrefsPanel *panel;
138     int i_object_id;
139     char *psz_section;
140     char *psz_help;
141 };
142
143 /*****************************************************************************
144  * Constructor.
145  *****************************************************************************/
146 PrefsDialog::PrefsDialog( intf_thread_t *_p_intf, HINSTANCE _hInst )
147 {
148     /* Initializations */
149     p_intf = _p_intf;
150     hInst = _hInst;
151     prefs_tree = NULL;
152 }
153
154 /***********************************************************************
155
156 FUNCTION: 
157   WndProc
158
159 PURPOSE: 
160   Processes messages sent to the main window.
161
162 ***********************************************************************/
163 LRESULT PrefsDialog::WndProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
164 {
165     SHINITDLGINFO shidi;
166     SHMENUBARINFO mbi;
167     RECT rcClient;
168
169     switch( msg )
170     {
171     case WM_INITDIALOG:
172         shidi.dwMask = SHIDIM_FLAGS;
173         shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN |
174             SHIDIF_FULLSCREENNOMENUBAR;//SHIDIF_SIZEDLGFULLSCREEN;
175         shidi.hDlg = hwnd;
176         SHInitDialog( &shidi );
177
178         //Create the menubar
179         memset( &mbi, 0, sizeof (SHMENUBARINFO) );
180         mbi.cbSize     = sizeof (SHMENUBARINFO);
181         mbi.hwndParent = hwnd;
182         mbi.dwFlags    = SHCMBF_EMPTYBAR;
183         mbi.hInstRes   = hInst;
184
185         if( !SHCreateMenuBar(&mbi) )
186         {
187             MessageBox(hwnd, _T("SHCreateMenuBar Failed"), _T("Error"), MB_OK);
188             //return -1;
189         }
190
191         hwndCB = mbi.hwndMB;
192
193         // Get the client area rect to put the panels in
194         GetClientRect(hwnd, &rcClient);
195
196         /* Create the buttons */            
197         advanced_checkbox =
198             CreateWindow( _T("BUTTON"), _T("Advanced options"),
199                         WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
200                         5, 10, 15, 15, hwnd, NULL, hInst, NULL );
201         SendMessage( advanced_checkbox, BM_SETCHECK, BST_UNCHECKED, 0 );
202
203         advanced_label = CreateWindow( _T("STATIC"), _T("Advanced options"),
204                         WS_CHILD | WS_VISIBLE | SS_LEFT,
205                         5 + 15 + 5, 10, 110, 15,
206                         hwnd, NULL, hInst, NULL);
207
208         if( config_GetInt( p_intf, "advanced" ) )
209         {
210             SendMessage( advanced_checkbox, BM_SETCHECK, BST_CHECKED, 0 );
211             /*dummy_event.SetInt(TRUE);
212               OnAdvanced( dummy_event );*/
213         }
214
215         reset_button = CreateWindow( _T("BUTTON"), _T("Reset All"),
216                         WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
217                         rcClient.right - 5 - 80, 10 - 3, 80, 15 + 6,
218                         hwnd, NULL, hInst, NULL );
219
220         /* Create the preferences tree control */
221         prefs_tree = new PrefsTreeCtrl( p_intf, this, hwnd, hInst );
222
223         UpdateWindow( hwnd );
224         break;
225
226     case WM_CLOSE:
227         EndDialog( hwnd, LOWORD( wp ) );
228         break;
229
230     case WM_COMMAND:
231         if( LOWORD(wp) == IDOK )
232         {
233             OnOk();
234             EndDialog( hwnd, LOWORD( wp ) );
235         }
236         break;
237
238     case WM_NOTIFY:
239
240         if( lp && prefs_tree &&
241             ((LPNMHDR)lp)->hwndFrom == prefs_tree->hwndTV &&
242             ((LPNMHDR)lp)->code == TVN_SELCHANGED )
243         {
244             prefs_tree->OnSelectTreeItem( (NM_TREEVIEW FAR *)(LPNMHDR)lp,
245                                           hwnd, hInst );
246         }
247         break;
248
249     case WM_VSCROLL:
250     {
251         TVITEM tvi = {0};
252         tvi.mask = TVIF_PARAM;
253         tvi.hItem = TreeView_GetSelection( prefs_tree->hwndTV );
254         TreeView_GetItem( prefs_tree->hwndTV, &tvi );
255         ConfigTreeData *config_data =
256             prefs_tree->FindModuleConfig( (ConfigTreeData *)tvi.lParam );
257         if ( hwnd == config_data->panel->config_window ) 
258         {
259             int dy;
260             RECT rc;
261             GetWindowRect( hwnd, &rc);
262             int newvalue = config_data->panel->oldvalue;
263             switch ( GET_WM_VSCROLL_CODE(wp,lp) ) 
264             {
265             case SB_BOTTOM       : newvalue = 0; break;
266             case SB_TOP          : newvalue = config_data->panel->maxvalue; break;
267             case SB_LINEDOWN     : newvalue += 10; break;
268             case SB_PAGEDOWN     : newvalue += rc.bottom - rc.top - 25; break; // faux ! une page c'est la longueur réelle de notebook
269             case SB_LINEUP       : newvalue -= 10; break;
270             case SB_PAGEUP       : newvalue -= rc.bottom - rc.top - 25; break;
271             case SB_THUMBPOSITION:
272             case SB_THUMBTRACK   : newvalue = GET_WM_VSCROLL_POS(wp,lp); break;
273             }
274             newvalue = max(0,min(config_data->panel->maxvalue,newvalue));
275             SetScrollPos( hwnd,SB_VERT,newvalue,TRUE);//SB_CTL si hwnd=hwndScrollBar, SB_VERT si window
276             dy = config_data->panel->oldvalue - newvalue;
277
278             ScrollWindowEx( hwnd, 0, dy, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN );
279             UpdateWindow ( hwnd);
280
281             config_data->panel->oldvalue = newvalue;                                
282         }
283         break;
284     }
285
286     default:
287         break;
288     }
289
290     return FALSE;
291 }
292
293 /*****************************************************************************
294  * Private methods.
295  *****************************************************************************/
296
297 /*****************************************************************************
298  * Events methods.
299  *****************************************************************************/
300 void PrefsDialog::OnOk( void )
301 {
302     prefs_tree->ApplyChanges();
303     config_SaveConfigFile( p_intf, NULL );
304 }
305
306 /*****************************************************************************
307  * PrefsTreeCtrl class definition.
308  *****************************************************************************/
309 PrefsTreeCtrl::PrefsTreeCtrl( intf_thread_t *_p_intf,
310                               PrefsDialog *_p_prefs_dialog, HWND hwnd,
311                               HINSTANCE hInst )
312 {
313     vlc_list_t      *p_list;
314     module_t        *p_module;
315     module_config_t *p_item;
316     int i_index;
317
318     INITCOMMONCONTROLSEX iccex;
319     RECT rcClient;
320     TVITEM tvi = {0}; 
321     TVINSERTSTRUCT tvins = {0}; 
322     HTREEITEM hPrev;
323
324     size_t i_capability_count = 0;
325     size_t i_child_index;
326
327     HTREEITEM capability_item;
328
329     /* Initializations */
330     p_intf = _p_intf;
331     p_prefs_dialog = _p_prefs_dialog;
332     b_advanced = VLC_FALSE;
333
334     /* Create a tree view */
335     // Initialize the INITCOMMONCONTROLSEX structure.
336     iccex.dwSize = sizeof( INITCOMMONCONTROLSEX );
337     iccex.dwICC = ICC_TREEVIEW_CLASSES;
338
339     // Registers Statusbar control classes from the common control dll
340     InitCommonControlsEx( &iccex );
341
342     // Get the client area rect to put the tv in
343     GetClientRect(hwnd, &rcClient);
344
345     // Create the tree-view control.
346     hwndTV = CreateWindowEx( 0, WC_TREEVIEW, NULL,
347         WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES |
348         TVS_LINESATROOT | TVS_HASBUTTONS,
349         5, 10 + 2*(15 + 10) + 105 + 5, rcClient.right - 5 - 5, 6*15,
350         hwnd, NULL, hInst, NULL );
351
352     tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM; 
353     tvi.pszText = _T("root");
354     tvi.cchTextMax = lstrlen(_T("root"));
355     tvi.lParam = (LPARAM) 1; // root level
356     tvins.item = tvi;
357     tvins.hInsertAfter = TVI_FIRST; 
358     tvins.hParent = TVI_ROOT; 
359
360     // Add the item to the tree-view control. 
361     hPrev = (HTREEITEM) TreeView_InsertItem( hwndTV, &tvins );
362     root_item = hPrev;
363
364     /* List the plugins */
365     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
366     if( !p_list ) return;
367
368     /*
369      * Build a tree of the main options
370      */
371     ConfigTreeData *config_data = new ConfigTreeData;
372     config_data->i_object_id = GENERAL_ID;
373     config_data->psz_help = strdup("nothing");//strdup( GENERAL_HELP );
374     config_data->psz_section = strdup( GENERAL_TITLE );
375     tvi.pszText = _T("General settings");
376     tvi.cchTextMax = lstrlen(_T("General settings"));
377     tvi.lParam = (long) config_data;
378     tvins.item = tvi;
379     tvins.hInsertAfter = hPrev;
380     tvins.hParent = root_item; //level 2
381
382     // Add the item to the tree-view control.
383     hPrev = (HTREEITEM) TreeView_InsertItem( hwndTV, &tvins);
384     general_item = hPrev;
385
386     for( i_index = 0; i_index < p_list->i_count; i_index++ )
387     {
388         p_module = (module_t *)p_list->p_values[i_index].p_object;
389         if( !strcmp( p_module->psz_object_name, "main" ) )
390             break;
391     }
392     if( i_index < p_list->i_count )
393     {
394         /* We found the main module */
395
396         /* Enumerate config categories and store a reference so we can
397          * generate their config panel them when it is asked by the user. */
398         p_item = p_module->p_config;
399
400         if( p_item ) do
401         {
402             switch( p_item->i_type )
403             {
404             case CONFIG_HINT_CATEGORY:
405                 ConfigTreeData *config_data = new ConfigTreeData;
406                 config_data->psz_section = strdup( p_item->psz_text );
407                 if( p_item->psz_longtext )
408                 {
409                     config_data->psz_help =
410                         strdup( p_item->psz_longtext );
411                 }
412                 else
413                 {
414                     config_data->psz_help = NULL;
415                 }
416                 config_data->i_object_id = p_module->i_object_id;
417
418                 /* Add the category to the tree */
419                 // Set the text of the item. 
420                 tvi.pszText = _FROMMB(p_item->psz_text); 
421                 tvi.cchTextMax = _tcslen(tvi.pszText);
422                 tvi.lParam = (long)config_data;
423                 tvins.item = tvi;
424                 tvins.hInsertAfter = hPrev; 
425                 tvins.hParent = general_item; //level 3
426     
427                 // Add the item to the tree-view control. 
428                 hPrev = (HTREEITEM)TreeView_InsertItem( hwndTV, &tvins );
429
430                 break;
431             }
432         }
433         while( p_item->i_type != CONFIG_HINT_END && p_item++ );
434
435         TreeView_SortChildren( hwndTV, general_item, 0 );
436     }
437         
438     /*
439      * Build a tree of all the plugins
440      */
441     config_data = new ConfigTreeData;
442     config_data->i_object_id = PLUGIN_ID;
443     config_data->psz_help = strdup("nothing");//strdup( PLUGIN_HELP );
444     config_data->psz_section = strdup("nothing");//strdup( PLUGIN_TITLE );
445     tvi.pszText = _T("Modules");
446     tvi.cchTextMax = lstrlen(_T("Modules"));
447     tvi.lParam = (long) config_data;
448     tvins.item = tvi;
449     tvins.hInsertAfter = hPrev;
450     tvins.hParent = root_item;// level 2
451
452     // Add the item to the tree-view control.
453     hPrev = (HTREEITEM) TreeView_InsertItem( hwndTV, &tvins);
454     plugins_item = hPrev;
455
456     i_capability_count = 0;
457     for( i_index = 0; i_index < p_list->i_count; i_index++ )
458     {
459         i_child_index = 0;
460
461         p_module = (module_t *)p_list->p_values[i_index].p_object;
462
463         /* Exclude the main module */
464         if( !strcmp( p_module->psz_object_name, "main" ) )
465             continue;
466
467         /* Exclude empty plugins (submodules don't have config options, they
468          * are stored in the parent module) */
469         if( p_module->b_submodule )
470             p_item = ((module_t *)p_module->p_parent)->p_config;
471         else
472             p_item = p_module->p_config;
473
474         if( !p_item ) continue;
475         do
476         {
477             if( p_item->i_type & CONFIG_ITEM )
478                 break;
479         }
480         while( p_item->i_type != CONFIG_HINT_END && p_item++ );
481         if( p_item->i_type == CONFIG_HINT_END ) continue;
482
483         /* Find the capability child item */
484         /*long cookie; size_t i_child_index;*/
485         capability_item = TreeView_GetChild( hwndTV, plugins_item );
486         while( capability_item != 0 )
487         {
488             TVITEM capability_tvi = {0};
489             TCHAR psz_text[256];
490             i_child_index++;
491
492             capability_tvi.mask = TVIF_TEXT;
493             capability_tvi.pszText = psz_text;
494             capability_tvi.cchTextMax = 256;
495             capability_tvi.hItem = capability_item;
496             TreeView_GetItem( hwndTV, &capability_tvi );
497             if( !strcmp( _TOMB(capability_tvi.pszText),
498                          p_module->psz_capability ) ) break;
499  
500             capability_item =
501                 TreeView_GetNextSibling( hwndTV, capability_item );
502         }
503
504         if( i_child_index == i_capability_count &&
505             p_module->psz_capability && *p_module->psz_capability )
506         {
507             /* We didn't find it, add it */
508             ConfigTreeData *config_data = new ConfigTreeData;
509             config_data->psz_section =
510                 strdup( GetCapabilityHelp( p_module->psz_capability , 1 ) );
511             config_data->psz_help =
512                 strdup( GetCapabilityHelp( p_module->psz_capability , 2 ) );
513             config_data->i_object_id = CAPABILITY_ID;
514             tvi.pszText = _FROMMB(p_module->psz_capability);
515             tvi.cchTextMax = _tcslen(tvi.pszText);
516             tvi.lParam = (long) config_data;
517             tvins.item = tvi;
518             tvins.hInsertAfter = plugins_item; 
519             tvins.hParent = plugins_item;// level 3
520
521             // Add the item to the tree-view control. 
522             capability_item = (HTREEITEM) TreeView_InsertItem( hwndTV, &tvins);
523
524             i_capability_count++;
525         }
526
527         /* Add the plugin to the tree */
528         ConfigTreeData *config_data = new ConfigTreeData;
529         config_data->b_submodule = p_module->b_submodule;
530         config_data->i_object_id = p_module->b_submodule ?
531             ((module_t *)p_module->p_parent)->i_object_id :
532             p_module->i_object_id;
533         config_data->psz_help = NULL;
534         tvi.pszText = _FROMMB(p_module->psz_object_name);
535         tvi.cchTextMax = _tcslen(tvi.pszText);
536         tvi.lParam = (long) config_data;
537         tvins.item = tvi;
538         tvins.hInsertAfter = capability_item; 
539         tvins.hParent = capability_item;// level 4
540
541         // Add the item to the tree-view control. 
542         TreeView_InsertItem( hwndTV, &tvins );
543     }
544
545     /* Sort all this mess */
546     /*long cookie; size_t i_child_index;*/
547     TreeView_SortChildren( hwndTV, plugins_item, 0 );
548     capability_item = TreeView_GetChild( hwndTV, plugins_item );
549     while( capability_item != 0 )
550     {
551         TreeView_SortChildren( hwndTV, capability_item, 0 );
552         capability_item = TreeView_GetNextSibling( hwndTV, capability_item );
553     }
554
555     /* Clean-up everything */
556     vlc_list_release( p_list );
557
558     TreeView_Expand( hwndTV, root_item, TVE_EXPANDPARTIAL |TVE_EXPAND );
559     TreeView_Expand( hwndTV, general_item, TVE_EXPANDPARTIAL |TVE_EXPAND );
560 }
561
562 PrefsTreeCtrl::~PrefsTreeCtrl()
563 {
564 }
565
566 void PrefsTreeCtrl::ApplyChanges()
567 {
568     /*long cookie, cookie2;*/
569     ConfigTreeData *config_data;
570
571     /* Apply changes to the main module */
572     HTREEITEM item = TreeView_GetChild( hwndTV, general_item );
573     while( item != 0 )
574     {
575         TVITEM tvi = {0};
576         tvi.mask = TVIF_PARAM;
577         tvi.hItem = item;
578         TreeView_GetItem( hwndTV, &tvi );
579         config_data = (ConfigTreeData *)tvi.lParam;
580         if( config_data && config_data->panel )
581         {
582             config_data->panel->ApplyChanges();
583         }
584
585         item = TreeView_GetNextSibling( hwndTV, item );
586     }
587
588     /* Apply changes to the plugins */
589     item = TreeView_GetChild( hwndTV, plugins_item );
590     while( item != 0 )
591     {
592         HTREEITEM item2 = TreeView_GetChild( hwndTV, item );
593         while( item2 != 0 )
594         {       
595             TVITEM tvi = {0};
596             tvi.mask = TVIF_PARAM;
597             tvi.hItem = item2;
598             TreeView_GetItem( hwndTV, &tvi );
599             config_data = (ConfigTreeData *)tvi.lParam;
600             if( config_data && config_data->panel )
601             {
602                 config_data->panel->ApplyChanges();
603             }
604             item2 = TreeView_GetNextSibling( hwndTV, item2 );
605         }
606         item = TreeView_GetNextSibling( hwndTV, item );
607     }
608 }
609
610 ConfigTreeData *PrefsTreeCtrl::FindModuleConfig( ConfigTreeData *config_data )
611 {
612     /* We need this complexity because submodules don't have their own config
613      * options. They use the parent module ones. */
614
615     if( !config_data || !config_data->b_submodule )
616     {
617         return config_data;
618     }
619
620     /*long cookie, cookie2;*/
621     ConfigTreeData *config_new;
622     HTREEITEM item = TreeView_GetChild( hwndTV, plugins_item );
623     while( item != 0 )
624     {
625         HTREEITEM item2 = TreeView_GetChild( hwndTV, item );
626         while( item2 != 0 )
627         {       
628             TVITEM tvi = {0};
629             tvi.mask = TVIF_PARAM;
630             tvi.hItem = item2;
631             TreeView_GetItem( hwndTV, &tvi );
632             config_new = (ConfigTreeData *)tvi.lParam;
633             if( config_new && !config_new->b_submodule &&
634                 config_new->i_object_id == config_data->i_object_id )
635             {
636                 return config_new;
637             }
638             item2 = TreeView_GetNextSibling( hwndTV, item2 );
639         }
640         item = TreeView_GetNextSibling( hwndTV, item );
641     }
642
643     /* Found nothing */
644     return NULL;
645 }
646
647 void PrefsTreeCtrl::OnSelectTreeItem( LPNM_TREEVIEW pnmtv, HWND parent,
648                                       HINSTANCE hInst )
649 {
650     ConfigTreeData *config_data = NULL;
651
652     if( pnmtv->itemOld.hItem )
653         config_data = FindModuleConfig( (ConfigTreeData *)pnmtv->itemOld.lParam );
654
655     if( config_data && config_data->panel )
656     {
657         config_data->panel->Hide();
658     }
659
660     /* Don't use event.GetItem() because we also send fake events */
661     TVITEM tvi = {0};
662     tvi.mask = TVIF_PARAM;
663     tvi.hItem = TreeView_GetSelection( hwndTV );
664     TreeView_GetItem( hwndTV, &tvi );
665     config_data = FindModuleConfig( (ConfigTreeData *)tvi.lParam );
666     if( config_data )
667     {
668         if( !config_data->panel )
669         {
670             /* The panel hasn't been created yet. Let's do it. */
671             config_data->panel =
672                 new PrefsPanel( parent, hInst, p_intf, p_prefs_dialog,
673                                 config_data->i_object_id,
674                                 config_data->psz_section,
675                                 config_data->psz_help );
676         }
677         else
678         {
679             config_data->panel->Show();
680         }
681     }
682 }
683
684 /*****************************************************************************
685  * PrefsPanel class definition.
686  *****************************************************************************/
687 PrefsPanel::PrefsPanel( HWND parent, HINSTANCE hInst, intf_thread_t *_p_intf,
688                         PrefsDialog *_p_prefs_dialog,
689                         int i_object_id, char *psz_section, char *psz_help )
690 {
691     module_config_t *p_item;
692     module_t *p_module = NULL;
693
694     /* Initializations */
695     p_intf = _p_intf;
696     p_prefs_dialog = _p_prefs_dialog;
697
698     b_advanced = VLC_TRUE;
699
700     if( i_object_id == PLUGIN_ID || i_object_id == GENERAL_ID ||
701         i_object_id == CAPABILITY_ID )
702     {
703         label = CreateWindow( _T("STATIC"), _FROMMB(psz_section),
704                               WS_CHILD | WS_VISIBLE | SS_LEFT,
705                               5, 10 + (15 + 10), 200, 15,
706                               parent, NULL, hInst, NULL );
707         config_window = NULL;
708     }
709     else
710     {
711         /* Get a pointer to the module */
712         p_module = (module_t *)vlc_object_get( p_intf,  i_object_id );
713         if( p_module->i_object_type != VLC_OBJECT_MODULE )
714         {
715             /* 0OOoo something went really bad */
716             return;
717         }
718
719         /* Enumerate config options and add corresponding config boxes
720          * (submodules don't have config options, they are stored in the
721          *  parent module) */
722         if( p_module->b_submodule )
723             p_item = ((module_t *)p_module->p_parent)->p_config;
724         else
725             p_item = p_module->p_config;
726
727         /* Find the category if it has been specified */
728         if( psz_section && p_item->i_type == CONFIG_HINT_CATEGORY )
729         {
730             while( !p_item->i_type == CONFIG_HINT_CATEGORY ||
731                    strcmp( psz_section, p_item->psz_text ) )
732             {
733                 if( p_item->i_type == CONFIG_HINT_END )
734                     break;
735                 p_item++;
736             }
737         }
738
739         /* Add a head title to the panel */
740         label = CreateWindow( _T("STATIC"), _FROMMB(psz_section ?
741                         p_item->psz_text : p_module->psz_longname),
742                         WS_CHILD | WS_VISIBLE | SS_LEFT,
743                         5, 10 + (15 + 10), 250, 15,
744                         parent, NULL, hInst, NULL );
745
746         WNDCLASS wc;
747         memset( &wc, 0, sizeof(wc) );
748         wc.style          = CS_HREDRAW | CS_VREDRAW;
749         wc.lpfnWndProc    = (WNDPROC) _p_prefs_dialog->BaseWndProc;
750         wc.cbClsExtra     = 0;
751         wc.cbWndExtra     = 0;
752         wc.hInstance      = hInst;
753         wc.hIcon          = 0;
754         wc.hCursor        = 0;
755         wc.hbrBackground  = (HBRUSH) GetStockObject(WHITE_BRUSH);
756         wc.lpszMenuName   = 0;
757         wc.lpszClassName  = _T("PrefsPanelClass");
758         RegisterClass(&wc);
759
760         RECT rc;
761         GetWindowRect( parent, &rc);
762         config_window = CreateWindow( _T("PrefsPanelClass"),
763                         _T("config_window"),
764                         WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER,
765                         5, 10 + 2*(15 + 10), rc.right - 5 - 7, 105,
766                         parent, NULL, hInst, (void *) _p_prefs_dialog );
767
768         int y_pos = 5;
769         if( p_item ) do
770         {
771             /* If a category has been specified, check we finished the job */
772             if( psz_section && p_item->i_type == CONFIG_HINT_CATEGORY &&
773                 strcmp( psz_section, p_item->psz_text ) )
774                 break;
775
776             ConfigControl *control =
777                 CreateConfigControl( VLC_OBJECT(p_intf),
778                                      p_item, config_window,
779                                      hInst, &y_pos );
780
781             /* Don't add items that were not recognized */
782             if( control == NULL ) continue;
783
784             /* Add the config data to our array so we can keep a trace of it */
785             config_array.push_back( control );
786         }
787         while( p_item->i_type != CONFIG_HINT_END && p_item++ );
788                 
789         GetWindowRect( config_window, &rc);
790         maxvalue = y_pos - (rc.bottom - rc.top) + 5;
791         oldvalue = 0;
792         SetScrollRange( config_window, SB_VERT, 0, maxvalue, TRUE );
793     }
794 }
795
796 void PrefsPanel::Hide()
797 {
798     ShowWindow( label, SW_HIDE );
799     if( config_window ) ShowWindow( config_window, SW_HIDE );
800 }
801
802 void PrefsPanel::Show()
803 {
804     ShowWindow( label, SW_SHOW );
805     if( config_window ) ShowWindow( config_window, SW_SHOW );
806 }
807
808 void PrefsPanel::ApplyChanges()
809 {
810     vlc_value_t val;
811
812     for( size_t i = 0; i < config_array.size(); i++ )
813     {
814         ConfigControl *control = config_array[i];
815
816         switch( control->GetType() )
817         {
818         case CONFIG_ITEM_STRING:
819         case CONFIG_ITEM_FILE:
820         case CONFIG_ITEM_DIRECTORY:
821         case CONFIG_ITEM_MODULE:
822             config_PutPsz( p_intf, control->GetName(),
823                            control->GetPszValue() );
824             break;
825         case CONFIG_ITEM_KEY:
826             /* So you don't need to restart to have the changes take effect */
827             val.i_int = control->GetIntValue();
828             var_Set( p_intf->p_vlc, control->GetName(), val );
829         case CONFIG_ITEM_INTEGER:
830         case CONFIG_ITEM_BOOL:
831             config_PutInt( p_intf, control->GetName(),
832                            control->GetIntValue() );
833             break;
834         case CONFIG_ITEM_FLOAT:
835             config_PutFloat( p_intf, control->GetName(),
836                              control->GetFloatValue() );
837             break;
838         }
839     }
840 }