1 /*****************************************************************************
2 * preferences.cpp : WinCE gui plugin for VLC
3 *****************************************************************************
4 * Copyright (C) 2000-2004 the VideoLAN team
7 * Authors: Marodon Cedric <cedric_marodon@yahoo.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <string.h> /* strerror() */
31 #include <vlc_interface.h>
41 #include <vlc_config_cat.h>
43 #include "preferences_widgets.h"
45 #define GENERAL_ID 1242
46 #define PLUGIN_ID 1243
47 #define CAPABILITY_ID 1244
49 /*****************************************************************************
51 *****************************************************************************/
53 /* IDs for the controls and the menu commands */
62 /*****************************************************************************
63 * Classes declarations.
64 *****************************************************************************/
71 PrefsTreeCtrl( intf_thread_t *_p_intf, PrefsDialog *p_prefs_dialog,
72 HWND hwnd, HINSTANCE _hInst );
73 virtual ~PrefsTreeCtrl();
76 /*void CleanChanges();*/
78 void OnSelectTreeItem( LPNM_TREEVIEW pnmtv, HWND parent, HINSTANCE hInst );
80 ConfigTreeData *FindModuleConfig( ConfigTreeData *config_data );
85 intf_thread_t *p_intf;
86 PrefsDialog *p_prefs_dialog;
87 vlc_bool_t b_advanced;
89 HTREEITEM general_item;
90 HTREEITEM plugins_item;
98 PrefsPanel( HWND parent, HINSTANCE hInst, intf_thread_t *_p_intf,
99 PrefsDialog *, int i_object_id, char *, char * );
100 virtual ~PrefsPanel() {}
113 intf_thread_t *p_intf;
114 PrefsDialog *p_prefs_dialog;
116 vlc_bool_t b_advanced;
120 vector<ConfigControl *> config_array;
127 ConfigTreeData() { b_submodule = 0; panel = NULL; psz_section = NULL;
129 virtual ~ConfigTreeData() { if( panel ) delete panel;
130 if( psz_section) free(psz_section);
131 if( psz_help) free(psz_help); }
133 vlc_bool_t b_submodule;
141 /*****************************************************************************
143 *****************************************************************************/
144 PrefsDialog::PrefsDialog( intf_thread_t *p_intf, CBaseWindow *p_parent,
146 : CBaseWindow( p_intf, p_parent, h_inst )
148 /* Initializations */
152 /***********************************************************************
158 Processes messages sent to the main window.
160 ***********************************************************************/
161 LRESULT PrefsDialog::WndProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
170 shidi.dwMask = SHIDIM_FLAGS;
171 shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN |
172 SHIDIF_FULLSCREENNOMENUBAR;//SHIDIF_SIZEDLGFULLSCREEN;
174 SHInitDialog( &shidi );
177 memset( &mbi, 0, sizeof (SHMENUBARINFO) );
178 mbi.cbSize = sizeof (SHMENUBARINFO);
179 mbi.hwndParent = hwnd;
180 mbi.dwFlags = SHCMBF_EMPTYBAR;
181 mbi.hInstRes = hInst;
183 if( !SHCreateMenuBar(&mbi) )
185 MessageBox(hwnd, _T("SHCreateMenuBar Failed"), _T("Error"), MB_OK);
191 // Get the client area rect to put the panels in
192 GetClientRect(hwnd, &rcClient);
194 /* Create the buttons */
196 CreateWindow( _T("BUTTON"), _T("Advanced options"),
197 WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
198 5, 10, 15, 15, hwnd, NULL, hInst, NULL );
199 SendMessage( advanced_checkbox, BM_SETCHECK, BST_UNCHECKED, 0 );
201 advanced_label = CreateWindow( _T("STATIC"), _T("Advanced options"),
202 WS_CHILD | WS_VISIBLE | SS_LEFT,
203 5 + 15 + 5, 10, 110, 15,
204 hwnd, NULL, hInst, NULL);
206 if( config_GetInt( p_intf, "advanced" ) )
208 SendMessage( advanced_checkbox, BM_SETCHECK, BST_CHECKED, 0 );
209 /*dummy_event.SetInt(TRUE);
210 OnAdvanced( dummy_event );*/
213 reset_button = CreateWindow( _T("BUTTON"), _T("Reset All"),
214 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
215 rcClient.right - 5 - 80, 10 - 3, 80, 15 + 6,
216 hwnd, NULL, hInst, NULL );
218 /* Create the preferences tree control */
219 prefs_tree = new PrefsTreeCtrl( p_intf, this, hwnd, hInst );
221 UpdateWindow( hwnd );
225 EndDialog( hwnd, LOWORD( wp ) );
229 SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
233 if( LOWORD(wp) == IDOK )
236 EndDialog( hwnd, LOWORD( wp ) );
242 if( lp && prefs_tree &&
243 ((LPNMHDR)lp)->hwndFrom == prefs_tree->hwndTV &&
244 ((LPNMHDR)lp)->code == TVN_SELCHANGED )
246 prefs_tree->OnSelectTreeItem( (NM_TREEVIEW FAR *)(LPNMHDR)lp,
254 tvi.mask = TVIF_PARAM;
255 tvi.hItem = TreeView_GetSelection( prefs_tree->hwndTV );
256 if( !tvi.hItem ) break;
258 if( !TreeView_GetItem( prefs_tree->hwndTV, &tvi ) ) break;
260 ConfigTreeData *config_data =
261 prefs_tree->FindModuleConfig( (ConfigTreeData *)tvi.lParam );
262 if( config_data && hwnd == config_data->panel->config_window )
266 GetWindowRect( hwnd, &rc);
267 int newvalue = config_data->panel->oldvalue;
268 switch ( GET_WM_VSCROLL_CODE(wp,lp) )
270 case SB_BOTTOM : newvalue = 0; break;
271 case SB_TOP : newvalue = config_data->panel->maxvalue; break;
272 case SB_LINEDOWN : newvalue += 10; break;
273 case SB_PAGEDOWN : newvalue += rc.bottom - rc.top - 25; break; // wrong! one page is notebook actual length
274 case SB_LINEUP : newvalue -= 10; break;
275 case SB_PAGEUP : newvalue -= rc.bottom - rc.top - 25; break;
276 case SB_THUMBPOSITION:
277 case SB_THUMBTRACK : newvalue = GET_WM_VSCROLL_POS(wp,lp); break;
279 newvalue = max(0,min(config_data->panel->maxvalue,newvalue));
280 SetScrollPos( hwnd,SB_VERT,newvalue,TRUE);//SB_CTL si hwnd=hwndScrollBar, SB_VERT si window
281 dy = config_data->panel->oldvalue - newvalue;
283 ScrollWindowEx( hwnd, 0, dy, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN );
284 UpdateWindow ( hwnd);
286 config_data->panel->oldvalue = newvalue;
298 /*****************************************************************************
300 *****************************************************************************/
302 /*****************************************************************************
304 *****************************************************************************/
305 void PrefsDialog::OnOk( void )
307 prefs_tree->ApplyChanges();
308 config_SaveConfigFile( p_intf, NULL );
311 /*****************************************************************************
312 * PrefsTreeCtrl class definition.
313 *****************************************************************************/
314 PrefsTreeCtrl::PrefsTreeCtrl( intf_thread_t *_p_intf,
315 PrefsDialog *_p_prefs_dialog, HWND hwnd,
320 module_config_t *p_item;
323 INITCOMMONCONTROLSEX iccex;
326 TVINSERTSTRUCT tvins = {0};
329 size_t i_capability_count = 0;
330 size_t i_child_index;
332 HTREEITEM capability_item;
334 /* Initializations */
336 p_prefs_dialog = _p_prefs_dialog;
337 b_advanced = VLC_FALSE;
339 /* Create a tree view */
340 // Initialize the INITCOMMONCONTROLSEX structure.
341 iccex.dwSize = sizeof( INITCOMMONCONTROLSEX );
342 iccex.dwICC = ICC_TREEVIEW_CLASSES;
344 // Registers Statusbar control classes from the common control dll
345 InitCommonControlsEx( &iccex );
347 // Get the client area rect to put the tv in
348 GetClientRect(hwnd, &rcClient);
350 // Create the tree-view control.
351 hwndTV = CreateWindowEx( 0, WC_TREEVIEW, NULL,
352 WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES |
353 TVS_LINESATROOT | TVS_HASBUTTONS,
354 5, 10 + 2*(15 + 10) + 105 + 5, rcClient.right - 5 - 5, 6*15,
355 hwnd, NULL, hInst, NULL );
357 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
359 /* List the plugins */
360 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
361 if( !p_list ) return;
364 * Build a tree of the main options
366 ConfigTreeData *config_data = new ConfigTreeData;
367 config_data->i_object_id = GENERAL_ID;
368 config_data->psz_help = strdup("nothing");//strdup( GENERAL_HELP );
369 config_data->psz_section = strdup( GENERAL_TITLE );
370 tvi.pszText = _T("General settings");
371 tvi.cchTextMax = lstrlen(_T("General settings"));
372 tvi.lParam = (long)config_data;
374 tvins.hInsertAfter = TVI_FIRST;
375 tvins.hParent = TVI_ROOT;
377 // Add the item to the tree-view control.
378 hPrev = (HTREEITEM) TreeView_InsertItem( hwndTV, &tvins);
379 general_item = hPrev;
381 for( i_index = 0; i_index < p_list->i_count; i_index++ )
383 p_module = (module_t *)p_list->p_values[i_index].p_object;
384 if( !strcmp( p_module->psz_object_name, "main" ) )
387 if( i_index < p_list->i_count )
389 /* We found the main module */
391 /* Enumerate config categories and store a reference so we can
392 * generate their config panel them when it is asked by the user. */
393 p_item = p_module->p_config;
397 switch( p_item->i_type )
399 case CONFIG_HINT_CATEGORY:
400 ConfigTreeData *config_data = new ConfigTreeData;
401 config_data->psz_section = strdup( p_item->psz_text );
402 if( p_item->psz_longtext )
404 config_data->psz_help =
405 strdup( p_item->psz_longtext );
409 config_data->psz_help = NULL;
411 config_data->i_object_id = p_module->i_object_id;
413 /* Add the category to the tree */
414 // Set the text of the item.
415 tvi.pszText = _FROMMB(p_item->psz_text);
416 tvi.cchTextMax = _tcslen(tvi.pszText);
417 tvi.lParam = (long)config_data;
419 tvins.hInsertAfter = hPrev;
420 tvins.hParent = general_item; //level 3
422 // Add the item to the tree-view control.
423 hPrev = (HTREEITEM)TreeView_InsertItem( hwndTV, &tvins );
428 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
430 TreeView_SortChildren( hwndTV, general_item, 0 );
434 * Build a tree of all the plugins
436 config_data = new ConfigTreeData;
437 config_data->i_object_id = PLUGIN_ID;
438 config_data->psz_help = strdup("nothing");//strdup( PLUGIN_HELP );
439 config_data->psz_section = strdup("nothing");//strdup( PLUGIN_TITLE );
440 tvi.pszText = _T("Modules");
441 tvi.cchTextMax = lstrlen(_T("Modules"));
442 tvi.lParam = (long)config_data;
444 tvins.hInsertAfter = TVI_LAST;
445 tvins.hParent = TVI_ROOT;
447 // Add the item to the tree-view control.
448 hPrev = (HTREEITEM) TreeView_InsertItem( hwndTV, &tvins);
449 plugins_item = hPrev;
451 i_capability_count = 0;
452 for( i_index = 0; i_index < p_list->i_count; i_index++ )
456 p_module = (module_t *)p_list->p_values[i_index].p_object;
458 /* Exclude the main module */
459 if( !strcmp( p_module->psz_object_name, "main" ) )
462 /* Exclude empty plugins (submodules don't have config options, they
463 * are stored in the parent module) */
464 if( p_module->b_submodule )
465 p_item = ((module_t *)p_module->p_parent)->p_config;
467 p_item = p_module->p_config;
469 if( !p_item ) continue;
472 if( p_item->i_type & CONFIG_ITEM )
475 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
476 if( p_item->i_type == CONFIG_HINT_END ) continue;
478 /* Find the capability child item */
479 /*long cookie; size_t i_child_index;*/
480 capability_item = TreeView_GetChild( hwndTV, plugins_item );
481 while( capability_item != 0 )
483 TVITEM capability_tvi = {0};
487 capability_tvi.mask = TVIF_TEXT;
488 capability_tvi.pszText = psz_text;
489 capability_tvi.cchTextMax = 256;
490 capability_tvi.hItem = capability_item;
491 TreeView_GetItem( hwndTV, &capability_tvi );
492 if( !strcmp( _TOMB(capability_tvi.pszText),
493 p_module->psz_capability ) ) break;
496 TreeView_GetNextSibling( hwndTV, capability_item );
499 if( i_child_index == i_capability_count &&
500 p_module->psz_capability && *p_module->psz_capability )
502 /* We didn't find it, add it */
503 ConfigTreeData *config_data = new ConfigTreeData;
504 config_data->psz_section =
505 strdup( GetCapabilityHelp( p_module->psz_capability , 1 ) );
506 config_data->psz_help =
507 strdup( GetCapabilityHelp( p_module->psz_capability , 2 ) );
508 config_data->i_object_id = CAPABILITY_ID;
509 tvi.pszText = _FROMMB(p_module->psz_capability);
510 tvi.cchTextMax = _tcslen(tvi.pszText);
511 tvi.lParam = (long)config_data;
513 tvins.hInsertAfter = plugins_item;
514 tvins.hParent = plugins_item;// level 3
516 // Add the item to the tree-view control.
517 capability_item = (HTREEITEM) TreeView_InsertItem( hwndTV, &tvins);
519 i_capability_count++;
522 /* Add the plugin to the tree */
523 ConfigTreeData *config_data = new ConfigTreeData;
524 config_data->b_submodule = p_module->b_submodule;
525 config_data->i_object_id = p_module->b_submodule ?
526 ((module_t *)p_module->p_parent)->i_object_id :
527 p_module->i_object_id;
528 config_data->psz_help = NULL;
529 tvi.pszText = _FROMMB(p_module->psz_object_name);
530 tvi.cchTextMax = _tcslen(tvi.pszText);
531 tvi.lParam = (long)config_data;
533 tvins.hInsertAfter = capability_item;
534 tvins.hParent = capability_item;// level 4
536 // Add the item to the tree-view control.
537 TreeView_InsertItem( hwndTV, &tvins );
540 /* Sort all this mess */
541 /*long cookie; size_t i_child_index;*/
542 TreeView_SortChildren( hwndTV, plugins_item, 0 );
543 capability_item = TreeView_GetChild( hwndTV, plugins_item );
544 while( capability_item != 0 )
546 TreeView_SortChildren( hwndTV, capability_item, 0 );
547 capability_item = TreeView_GetNextSibling( hwndTV, capability_item );
550 /* Clean-up everything */
551 vlc_list_release( p_list );
553 TreeView_Expand( hwndTV, general_item, TVE_EXPANDPARTIAL |TVE_EXPAND );
556 PrefsTreeCtrl::~PrefsTreeCtrl()
560 void PrefsTreeCtrl::ApplyChanges()
562 /*long cookie, cookie2;*/
563 ConfigTreeData *config_data;
565 /* Apply changes to the main module */
566 HTREEITEM item = TreeView_GetChild( hwndTV, general_item );
570 tvi.mask = TVIF_PARAM;
572 TreeView_GetItem( hwndTV, &tvi );
573 config_data = (ConfigTreeData *)tvi.lParam;
574 if( config_data && config_data->panel )
576 config_data->panel->ApplyChanges();
579 item = TreeView_GetNextSibling( hwndTV, item );
582 /* Apply changes to the plugins */
583 item = TreeView_GetChild( hwndTV, plugins_item );
586 HTREEITEM item2 = TreeView_GetChild( hwndTV, item );
590 tvi.mask = TVIF_PARAM;
592 TreeView_GetItem( hwndTV, &tvi );
593 config_data = (ConfigTreeData *)tvi.lParam;
594 if( config_data && config_data->panel )
596 config_data->panel->ApplyChanges();
598 item2 = TreeView_GetNextSibling( hwndTV, item2 );
600 item = TreeView_GetNextSibling( hwndTV, item );
604 ConfigTreeData *PrefsTreeCtrl::FindModuleConfig( ConfigTreeData *config_data )
606 /* We need this complexity because submodules don't have their own config
607 * options. They use the parent module ones. */
609 if( !config_data || !config_data->b_submodule )
614 /*long cookie, cookie2;*/
615 ConfigTreeData *config_new;
616 HTREEITEM item = TreeView_GetChild( hwndTV, plugins_item );
619 HTREEITEM item2 = TreeView_GetChild( hwndTV, item );
623 tvi.mask = TVIF_PARAM;
625 TreeView_GetItem( hwndTV, &tvi );
626 config_new = (ConfigTreeData *)tvi.lParam;
627 if( config_new && !config_new->b_submodule &&
628 config_new->i_object_id == config_data->i_object_id )
632 item2 = TreeView_GetNextSibling( hwndTV, item2 );
634 item = TreeView_GetNextSibling( hwndTV, item );
641 void PrefsTreeCtrl::OnSelectTreeItem( LPNM_TREEVIEW pnmtv, HWND parent,
644 ConfigTreeData *config_data = NULL;
646 if( pnmtv->itemOld.hItem )
647 config_data = FindModuleConfig( (ConfigTreeData *)pnmtv->itemOld.lParam );
649 if( config_data && config_data->panel )
651 config_data->panel->Hide();
654 /* Don't use event.GetItem() because we also send fake events */
656 tvi.mask = TVIF_PARAM;
657 tvi.hItem = TreeView_GetSelection( hwndTV );
658 TreeView_GetItem( hwndTV, &tvi );
659 config_data = FindModuleConfig( (ConfigTreeData *)tvi.lParam );
662 if( !config_data->panel )
664 /* The panel hasn't been created yet. Let's do it. */
666 new PrefsPanel( parent, hInst, p_intf, p_prefs_dialog,
667 config_data->i_object_id,
668 config_data->psz_section,
669 config_data->psz_help );
673 config_data->panel->Show();
678 /*****************************************************************************
679 * PrefsPanel class definition.
680 *****************************************************************************/
681 PrefsPanel::PrefsPanel( HWND parent, HINSTANCE hInst, intf_thread_t *_p_intf,
682 PrefsDialog *_p_prefs_dialog,
683 int i_object_id, char *psz_section, char *psz_help )
685 module_config_t *p_item;
686 module_t *p_module = NULL;
688 /* Initializations */
690 p_prefs_dialog = _p_prefs_dialog;
692 b_advanced = VLC_TRUE;
694 if( i_object_id == PLUGIN_ID || i_object_id == GENERAL_ID ||
695 i_object_id == CAPABILITY_ID )
697 label = CreateWindow( _T("STATIC"), _FROMMB(psz_section),
698 WS_CHILD | WS_VISIBLE | SS_LEFT,
699 5, 10 + (15 + 10), 200, 15,
700 parent, NULL, hInst, NULL );
701 config_window = NULL;
705 /* Get a pointer to the module */
706 p_module = (module_t *)vlc_object_get( p_intf, i_object_id );
707 if( p_module->i_object_type != VLC_OBJECT_MODULE )
709 /* 0OOoo something went really bad */
713 /* Enumerate config options and add corresponding config boxes
714 * (submodules don't have config options, they are stored in the
716 if( p_module->b_submodule )
717 p_item = ((module_t *)p_module->p_parent)->p_config;
719 p_item = p_module->p_config;
721 /* Find the category if it has been specified */
722 if( psz_section && p_item->i_type == CONFIG_HINT_CATEGORY )
724 while( !(p_item->i_type == CONFIG_HINT_CATEGORY) ||
725 strcmp( psz_section, p_item->psz_text ) )
727 if( p_item->i_type == CONFIG_HINT_END ) break;
732 /* Add a head title to the panel */
733 label = CreateWindow( _T("STATIC"), _FROMMB(psz_section ?
734 p_item->psz_text : p_module->psz_longname),
735 WS_CHILD | WS_VISIBLE | SS_LEFT,
736 5, 10 + (15 + 10), 250, 15,
737 parent, NULL, hInst, NULL );
740 memset( &wc, 0, sizeof(wc) );
741 wc.style = CS_HREDRAW | CS_VREDRAW;
742 wc.lpfnWndProc = (WNDPROC) _p_prefs_dialog->BaseWndProc;
745 wc.hInstance = hInst;
748 wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
750 wc.lpszClassName = _T("PrefsPanelClass");
754 GetWindowRect( parent, &rc);
755 config_window = CreateWindow( _T("PrefsPanelClass"),
757 WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER,
758 5, 10 + 2*(15 + 10), rc.right - 5 - 7, 105,
759 parent, NULL, hInst, (void *) _p_prefs_dialog );
764 /* If a category has been specified, check we finished the job */
765 if( psz_section && p_item->i_type == CONFIG_HINT_CATEGORY &&
766 strcmp( psz_section, p_item->psz_text ) )
769 ConfigControl *control =
770 CreateConfigControl( VLC_OBJECT(p_intf),
771 p_item, config_window,
774 /* Don't add items that were not recognized */
775 if( control == NULL ) continue;
777 /* Add the config data to our array so we can keep a trace of it */
778 config_array.push_back( control );
780 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
782 GetWindowRect( config_window, &rc);
783 maxvalue = y_pos - (rc.bottom - rc.top) + 5;
785 SetScrollRange( config_window, SB_VERT, 0, maxvalue, TRUE );
789 void PrefsPanel::Hide()
791 ShowWindow( label, SW_HIDE );
792 if( config_window ) ShowWindow( config_window, SW_HIDE );
795 void PrefsPanel::Show()
797 ShowWindow( label, SW_SHOW );
798 if( config_window ) ShowWindow( config_window, SW_SHOW );
801 void PrefsPanel::ApplyChanges()
805 for( size_t i = 0; i < config_array.size(); i++ )
807 ConfigControl *control = config_array[i];
809 switch( control->GetType() )
811 case CONFIG_ITEM_STRING:
812 case CONFIG_ITEM_FILE:
813 case CONFIG_ITEM_DIRECTORY:
814 case CONFIG_ITEM_MODULE:
815 config_PutPsz( p_intf, control->GetName(),
816 control->GetPszValue() );
818 case CONFIG_ITEM_KEY:
819 /* So you don't need to restart to have the changes take effect */
820 val.i_int = control->GetIntValue();
821 var_Set( p_intf->p_libvlc, control->GetName(), val );
822 case CONFIG_ITEM_INTEGER:
823 case CONFIG_ITEM_BOOL:
824 config_PutInt( p_intf, control->GetName(),
825 control->GetIntValue() );
827 case CONFIG_ITEM_FLOAT:
828 config_PutFloat( p_intf, control->GetName(),
829 control->GetFloatValue() );