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