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 <stdlib.h> /* malloc(), free() */
29 #include <string.h> /* strerror() */
32 #include <vlc_interface.h>
42 #include <vlc_config_cat.h>
44 #include "preferences_widgets.h"
46 #define GENERAL_ID 1242
47 #define PLUGIN_ID 1243
48 #define CAPABILITY_ID 1244
50 /*****************************************************************************
52 *****************************************************************************/
54 /* IDs for the controls and the menu commands */
63 /*****************************************************************************
64 * Classes declarations.
65 *****************************************************************************/
72 PrefsTreeCtrl( intf_thread_t *_p_intf, PrefsDialog *p_prefs_dialog,
73 HWND hwnd, HINSTANCE _hInst );
74 virtual ~PrefsTreeCtrl();
77 /*void CleanChanges();*/
79 void OnSelectTreeItem( LPNM_TREEVIEW pnmtv, HWND parent, HINSTANCE hInst );
81 ConfigTreeData *FindModuleConfig( ConfigTreeData *config_data );
86 intf_thread_t *p_intf;
87 PrefsDialog *p_prefs_dialog;
88 vlc_bool_t b_advanced;
90 HTREEITEM general_item;
91 HTREEITEM plugins_item;
99 PrefsPanel( HWND parent, HINSTANCE hInst, intf_thread_t *_p_intf,
100 PrefsDialog *, int i_object_id, char *, char * );
101 virtual ~PrefsPanel() {}
114 intf_thread_t *p_intf;
115 PrefsDialog *p_prefs_dialog;
117 vlc_bool_t b_advanced;
121 vector<ConfigControl *> config_array;
128 ConfigTreeData() { b_submodule = 0; panel = NULL; psz_section = NULL;
130 virtual ~ConfigTreeData() { if( panel ) delete panel;
131 if( psz_section) free(psz_section);
132 if( psz_help) free(psz_help); }
134 vlc_bool_t b_submodule;
142 /*****************************************************************************
144 *****************************************************************************/
145 PrefsDialog::PrefsDialog( intf_thread_t *p_intf, CBaseWindow *p_parent,
147 : CBaseWindow( p_intf, p_parent, h_inst )
149 /* Initializations */
153 /***********************************************************************
159 Processes messages sent to the main window.
161 ***********************************************************************/
162 LRESULT PrefsDialog::WndProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
171 shidi.dwMask = SHIDIM_FLAGS;
172 shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN |
173 SHIDIF_FULLSCREENNOMENUBAR;//SHIDIF_SIZEDLGFULLSCREEN;
175 SHInitDialog( &shidi );
178 memset( &mbi, 0, sizeof (SHMENUBARINFO) );
179 mbi.cbSize = sizeof (SHMENUBARINFO);
180 mbi.hwndParent = hwnd;
181 mbi.dwFlags = SHCMBF_EMPTYBAR;
182 mbi.hInstRes = hInst;
184 if( !SHCreateMenuBar(&mbi) )
186 MessageBox(hwnd, _T("SHCreateMenuBar Failed"), _T("Error"), MB_OK);
192 // Get the client area rect to put the panels in
193 GetClientRect(hwnd, &rcClient);
195 /* Create the buttons */
197 CreateWindow( _T("BUTTON"), _T("Advanced options"),
198 WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
199 5, 10, 15, 15, hwnd, NULL, hInst, NULL );
200 SendMessage( advanced_checkbox, BM_SETCHECK, BST_UNCHECKED, 0 );
202 advanced_label = CreateWindow( _T("STATIC"), _T("Advanced options"),
203 WS_CHILD | WS_VISIBLE | SS_LEFT,
204 5 + 15 + 5, 10, 110, 15,
205 hwnd, NULL, hInst, NULL);
207 if( config_GetInt( p_intf, "advanced" ) )
209 SendMessage( advanced_checkbox, BM_SETCHECK, BST_CHECKED, 0 );
210 /*dummy_event.SetInt(TRUE);
211 OnAdvanced( dummy_event );*/
214 reset_button = CreateWindow( _T("BUTTON"), _T("Reset All"),
215 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
216 rcClient.right - 5 - 80, 10 - 3, 80, 15 + 6,
217 hwnd, NULL, hInst, NULL );
219 /* Create the preferences tree control */
220 prefs_tree = new PrefsTreeCtrl( p_intf, this, hwnd, hInst );
222 UpdateWindow( hwnd );
226 EndDialog( hwnd, LOWORD( wp ) );
230 SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
234 if( LOWORD(wp) == IDOK )
237 EndDialog( hwnd, LOWORD( wp ) );
243 if( lp && prefs_tree &&
244 ((LPNMHDR)lp)->hwndFrom == prefs_tree->hwndTV &&
245 ((LPNMHDR)lp)->code == TVN_SELCHANGED )
247 prefs_tree->OnSelectTreeItem( (NM_TREEVIEW FAR *)(LPNMHDR)lp,
255 tvi.mask = TVIF_PARAM;
256 tvi.hItem = TreeView_GetSelection( prefs_tree->hwndTV );
257 if( !tvi.hItem ) break;
259 if( !TreeView_GetItem( prefs_tree->hwndTV, &tvi ) ) break;
261 ConfigTreeData *config_data =
262 prefs_tree->FindModuleConfig( (ConfigTreeData *)tvi.lParam );
263 if( config_data && hwnd == config_data->panel->config_window )
267 GetWindowRect( hwnd, &rc);
268 int newvalue = config_data->panel->oldvalue;
269 switch ( GET_WM_VSCROLL_CODE(wp,lp) )
271 case SB_BOTTOM : newvalue = 0; break;
272 case SB_TOP : newvalue = config_data->panel->maxvalue; break;
273 case SB_LINEDOWN : newvalue += 10; break;
274 case SB_PAGEDOWN : newvalue += rc.bottom - rc.top - 25; break; // wrong! one page is notebook actual length
275 case SB_LINEUP : newvalue -= 10; break;
276 case SB_PAGEUP : newvalue -= rc.bottom - rc.top - 25; break;
277 case SB_THUMBPOSITION:
278 case SB_THUMBTRACK : newvalue = GET_WM_VSCROLL_POS(wp,lp); break;
280 newvalue = max(0,min(config_data->panel->maxvalue,newvalue));
281 SetScrollPos( hwnd,SB_VERT,newvalue,TRUE);//SB_CTL si hwnd=hwndScrollBar, SB_VERT si window
282 dy = config_data->panel->oldvalue - newvalue;
284 ScrollWindowEx( hwnd, 0, dy, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN );
285 UpdateWindow ( hwnd);
287 config_data->panel->oldvalue = newvalue;
299 /*****************************************************************************
301 *****************************************************************************/
303 /*****************************************************************************
305 *****************************************************************************/
306 void PrefsDialog::OnOk( void )
308 prefs_tree->ApplyChanges();
309 config_SaveConfigFile( p_intf, NULL );
312 /*****************************************************************************
313 * PrefsTreeCtrl class definition.
314 *****************************************************************************/
315 PrefsTreeCtrl::PrefsTreeCtrl( intf_thread_t *_p_intf,
316 PrefsDialog *_p_prefs_dialog, HWND hwnd,
321 module_config_t *p_item;
324 INITCOMMONCONTROLSEX iccex;
327 TVINSERTSTRUCT tvins = {0};
330 size_t i_capability_count = 0;
331 size_t i_child_index;
333 HTREEITEM capability_item;
335 /* Initializations */
337 p_prefs_dialog = _p_prefs_dialog;
338 b_advanced = VLC_FALSE;
340 /* Create a tree view */
341 // Initialize the INITCOMMONCONTROLSEX structure.
342 iccex.dwSize = sizeof( INITCOMMONCONTROLSEX );
343 iccex.dwICC = ICC_TREEVIEW_CLASSES;
345 // Registers Statusbar control classes from the common control dll
346 InitCommonControlsEx( &iccex );
348 // Get the client area rect to put the tv in
349 GetClientRect(hwnd, &rcClient);
351 // Create the tree-view control.
352 hwndTV = CreateWindowEx( 0, WC_TREEVIEW, NULL,
353 WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES |
354 TVS_LINESATROOT | TVS_HASBUTTONS,
355 5, 10 + 2*(15 + 10) + 105 + 5, rcClient.right - 5 - 5, 6*15,
356 hwnd, NULL, hInst, NULL );
358 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
360 /* List the plugins */
361 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
362 if( !p_list ) return;
365 * Build a tree of the main options
367 ConfigTreeData *config_data = new ConfigTreeData;
368 config_data->i_object_id = GENERAL_ID;
369 config_data->psz_help = strdup("nothing");//strdup( GENERAL_HELP );
370 config_data->psz_section = strdup( GENERAL_TITLE );
371 tvi.pszText = _T("General settings");
372 tvi.cchTextMax = lstrlen(_T("General settings"));
373 tvi.lParam = (long)config_data;
375 tvins.hInsertAfter = TVI_FIRST;
376 tvins.hParent = TVI_ROOT;
378 // Add the item to the tree-view control.
379 hPrev = (HTREEITEM) TreeView_InsertItem( hwndTV, &tvins);
380 general_item = hPrev;
382 for( i_index = 0; i_index < p_list->i_count; i_index++ )
384 p_module = (module_t *)p_list->p_values[i_index].p_object;
385 if( !strcmp( p_module->psz_object_name, "main" ) )
388 if( i_index < p_list->i_count )
390 /* We found the main module */
392 /* Enumerate config categories and store a reference so we can
393 * generate their config panel them when it is asked by the user. */
394 p_item = p_module->p_config;
398 switch( p_item->i_type )
400 case CONFIG_HINT_CATEGORY:
401 ConfigTreeData *config_data = new ConfigTreeData;
402 config_data->psz_section = strdup( p_item->psz_text );
403 if( p_item->psz_longtext )
405 config_data->psz_help =
406 strdup( p_item->psz_longtext );
410 config_data->psz_help = NULL;
412 config_data->i_object_id = p_module->i_object_id;
414 /* Add the category to the tree */
415 // Set the text of the item.
416 tvi.pszText = _FROMMB(p_item->psz_text);
417 tvi.cchTextMax = _tcslen(tvi.pszText);
418 tvi.lParam = (long)config_data;
420 tvins.hInsertAfter = hPrev;
421 tvins.hParent = general_item; //level 3
423 // Add the item to the tree-view control.
424 hPrev = (HTREEITEM)TreeView_InsertItem( hwndTV, &tvins );
429 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
431 TreeView_SortChildren( hwndTV, general_item, 0 );
435 * Build a tree of all the plugins
437 config_data = new ConfigTreeData;
438 config_data->i_object_id = PLUGIN_ID;
439 config_data->psz_help = strdup("nothing");//strdup( PLUGIN_HELP );
440 config_data->psz_section = strdup("nothing");//strdup( PLUGIN_TITLE );
441 tvi.pszText = _T("Modules");
442 tvi.cchTextMax = lstrlen(_T("Modules"));
443 tvi.lParam = (long)config_data;
445 tvins.hInsertAfter = TVI_LAST;
446 tvins.hParent = TVI_ROOT;
448 // Add the item to the tree-view control.
449 hPrev = (HTREEITEM) TreeView_InsertItem( hwndTV, &tvins);
450 plugins_item = hPrev;
452 i_capability_count = 0;
453 for( i_index = 0; i_index < p_list->i_count; i_index++ )
457 p_module = (module_t *)p_list->p_values[i_index].p_object;
459 /* Exclude the main module */
460 if( !strcmp( p_module->psz_object_name, "main" ) )
463 /* Exclude empty plugins (submodules don't have config options, they
464 * are stored in the parent module) */
465 if( p_module->b_submodule )
466 p_item = ((module_t *)p_module->p_parent)->p_config;
468 p_item = p_module->p_config;
470 if( !p_item ) continue;
473 if( p_item->i_type & CONFIG_ITEM )
476 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
477 if( p_item->i_type == CONFIG_HINT_END ) continue;
479 /* Find the capability child item */
480 /*long cookie; size_t i_child_index;*/
481 capability_item = TreeView_GetChild( hwndTV, plugins_item );
482 while( capability_item != 0 )
484 TVITEM capability_tvi = {0};
488 capability_tvi.mask = TVIF_TEXT;
489 capability_tvi.pszText = psz_text;
490 capability_tvi.cchTextMax = 256;
491 capability_tvi.hItem = capability_item;
492 TreeView_GetItem( hwndTV, &capability_tvi );
493 if( !strcmp( _TOMB(capability_tvi.pszText),
494 p_module->psz_capability ) ) break;
497 TreeView_GetNextSibling( hwndTV, capability_item );
500 if( i_child_index == i_capability_count &&
501 p_module->psz_capability && *p_module->psz_capability )
503 /* We didn't find it, add it */
504 ConfigTreeData *config_data = new ConfigTreeData;
505 config_data->psz_section =
506 strdup( GetCapabilityHelp( p_module->psz_capability , 1 ) );
507 config_data->psz_help =
508 strdup( GetCapabilityHelp( p_module->psz_capability , 2 ) );
509 config_data->i_object_id = CAPABILITY_ID;
510 tvi.pszText = _FROMMB(p_module->psz_capability);
511 tvi.cchTextMax = _tcslen(tvi.pszText);
512 tvi.lParam = (long)config_data;
514 tvins.hInsertAfter = plugins_item;
515 tvins.hParent = plugins_item;// level 3
517 // Add the item to the tree-view control.
518 capability_item = (HTREEITEM) TreeView_InsertItem( hwndTV, &tvins);
520 i_capability_count++;
523 /* Add the plugin to the tree */
524 ConfigTreeData *config_data = new ConfigTreeData;
525 config_data->b_submodule = p_module->b_submodule;
526 config_data->i_object_id = p_module->b_submodule ?
527 ((module_t *)p_module->p_parent)->i_object_id :
528 p_module->i_object_id;
529 config_data->psz_help = NULL;
530 tvi.pszText = _FROMMB(p_module->psz_object_name);
531 tvi.cchTextMax = _tcslen(tvi.pszText);
532 tvi.lParam = (long)config_data;
534 tvins.hInsertAfter = capability_item;
535 tvins.hParent = capability_item;// level 4
537 // Add the item to the tree-view control.
538 TreeView_InsertItem( hwndTV, &tvins );
541 /* Sort all this mess */
542 /*long cookie; size_t i_child_index;*/
543 TreeView_SortChildren( hwndTV, plugins_item, 0 );
544 capability_item = TreeView_GetChild( hwndTV, plugins_item );
545 while( capability_item != 0 )
547 TreeView_SortChildren( hwndTV, capability_item, 0 );
548 capability_item = TreeView_GetNextSibling( hwndTV, capability_item );
551 /* Clean-up everything */
552 vlc_list_release( p_list );
554 TreeView_Expand( hwndTV, general_item, TVE_EXPANDPARTIAL |TVE_EXPAND );
557 PrefsTreeCtrl::~PrefsTreeCtrl()
561 void PrefsTreeCtrl::ApplyChanges()
563 /*long cookie, cookie2;*/
564 ConfigTreeData *config_data;
566 /* Apply changes to the main module */
567 HTREEITEM item = TreeView_GetChild( hwndTV, general_item );
571 tvi.mask = TVIF_PARAM;
573 TreeView_GetItem( hwndTV, &tvi );
574 config_data = (ConfigTreeData *)tvi.lParam;
575 if( config_data && config_data->panel )
577 config_data->panel->ApplyChanges();
580 item = TreeView_GetNextSibling( hwndTV, item );
583 /* Apply changes to the plugins */
584 item = TreeView_GetChild( hwndTV, plugins_item );
587 HTREEITEM item2 = TreeView_GetChild( hwndTV, item );
591 tvi.mask = TVIF_PARAM;
593 TreeView_GetItem( hwndTV, &tvi );
594 config_data = (ConfigTreeData *)tvi.lParam;
595 if( config_data && config_data->panel )
597 config_data->panel->ApplyChanges();
599 item2 = TreeView_GetNextSibling( hwndTV, item2 );
601 item = TreeView_GetNextSibling( hwndTV, item );
605 ConfigTreeData *PrefsTreeCtrl::FindModuleConfig( ConfigTreeData *config_data )
607 /* We need this complexity because submodules don't have their own config
608 * options. They use the parent module ones. */
610 if( !config_data || !config_data->b_submodule )
615 /*long cookie, cookie2;*/
616 ConfigTreeData *config_new;
617 HTREEITEM item = TreeView_GetChild( hwndTV, plugins_item );
620 HTREEITEM item2 = TreeView_GetChild( hwndTV, item );
624 tvi.mask = TVIF_PARAM;
626 TreeView_GetItem( hwndTV, &tvi );
627 config_new = (ConfigTreeData *)tvi.lParam;
628 if( config_new && !config_new->b_submodule &&
629 config_new->i_object_id == config_data->i_object_id )
633 item2 = TreeView_GetNextSibling( hwndTV, item2 );
635 item = TreeView_GetNextSibling( hwndTV, item );
642 void PrefsTreeCtrl::OnSelectTreeItem( LPNM_TREEVIEW pnmtv, HWND parent,
645 ConfigTreeData *config_data = NULL;
647 if( pnmtv->itemOld.hItem )
648 config_data = FindModuleConfig( (ConfigTreeData *)pnmtv->itemOld.lParam );
650 if( config_data && config_data->panel )
652 config_data->panel->Hide();
655 /* Don't use event.GetItem() because we also send fake events */
657 tvi.mask = TVIF_PARAM;
658 tvi.hItem = TreeView_GetSelection( hwndTV );
659 TreeView_GetItem( hwndTV, &tvi );
660 config_data = FindModuleConfig( (ConfigTreeData *)tvi.lParam );
663 if( !config_data->panel )
665 /* The panel hasn't been created yet. Let's do it. */
667 new PrefsPanel( parent, hInst, p_intf, p_prefs_dialog,
668 config_data->i_object_id,
669 config_data->psz_section,
670 config_data->psz_help );
674 config_data->panel->Show();
679 /*****************************************************************************
680 * PrefsPanel class definition.
681 *****************************************************************************/
682 PrefsPanel::PrefsPanel( HWND parent, HINSTANCE hInst, intf_thread_t *_p_intf,
683 PrefsDialog *_p_prefs_dialog,
684 int i_object_id, char *psz_section, char *psz_help )
686 module_config_t *p_item;
687 module_t *p_module = NULL;
689 /* Initializations */
691 p_prefs_dialog = _p_prefs_dialog;
693 b_advanced = VLC_TRUE;
695 if( i_object_id == PLUGIN_ID || i_object_id == GENERAL_ID ||
696 i_object_id == CAPABILITY_ID )
698 label = CreateWindow( _T("STATIC"), _FROMMB(psz_section),
699 WS_CHILD | WS_VISIBLE | SS_LEFT,
700 5, 10 + (15 + 10), 200, 15,
701 parent, NULL, hInst, NULL );
702 config_window = NULL;
706 /* Get a pointer to the module */
707 p_module = (module_t *)vlc_object_get( p_intf, i_object_id );
708 if( p_module->i_object_type != VLC_OBJECT_MODULE )
710 /* 0OOoo something went really bad */
714 /* Enumerate config options and add corresponding config boxes
715 * (submodules don't have config options, they are stored in the
717 if( p_module->b_submodule )
718 p_item = ((module_t *)p_module->p_parent)->p_config;
720 p_item = p_module->p_config;
722 /* Find the category if it has been specified */
723 if( psz_section && p_item->i_type == CONFIG_HINT_CATEGORY )
725 while( !(p_item->i_type == CONFIG_HINT_CATEGORY) ||
726 strcmp( psz_section, p_item->psz_text ) )
728 if( p_item->i_type == CONFIG_HINT_END ) break;
733 /* Add a head title to the panel */
734 label = CreateWindow( _T("STATIC"), _FROMMB(psz_section ?
735 p_item->psz_text : p_module->psz_longname),
736 WS_CHILD | WS_VISIBLE | SS_LEFT,
737 5, 10 + (15 + 10), 250, 15,
738 parent, NULL, hInst, NULL );
741 memset( &wc, 0, sizeof(wc) );
742 wc.style = CS_HREDRAW | CS_VREDRAW;
743 wc.lpfnWndProc = (WNDPROC) _p_prefs_dialog->BaseWndProc;
746 wc.hInstance = hInst;
749 wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
751 wc.lpszClassName = _T("PrefsPanelClass");
755 GetWindowRect( parent, &rc);
756 config_window = CreateWindow( _T("PrefsPanelClass"),
758 WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER,
759 5, 10 + 2*(15 + 10), rc.right - 5 - 7, 105,
760 parent, NULL, hInst, (void *) _p_prefs_dialog );
765 /* If a category has been specified, check we finished the job */
766 if( psz_section && p_item->i_type == CONFIG_HINT_CATEGORY &&
767 strcmp( psz_section, p_item->psz_text ) )
770 ConfigControl *control =
771 CreateConfigControl( VLC_OBJECT(p_intf),
772 p_item, config_window,
775 /* Don't add items that were not recognized */
776 if( control == NULL ) continue;
778 /* Add the config data to our array so we can keep a trace of it */
779 config_array.push_back( control );
781 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
783 GetWindowRect( config_window, &rc);
784 maxvalue = y_pos - (rc.bottom - rc.top) + 5;
786 SetScrollRange( config_window, SB_VERT, 0, maxvalue, TRUE );
790 void PrefsPanel::Hide()
792 ShowWindow( label, SW_HIDE );
793 if( config_window ) ShowWindow( config_window, SW_HIDE );
796 void PrefsPanel::Show()
798 ShowWindow( label, SW_SHOW );
799 if( config_window ) ShowWindow( config_window, SW_SHOW );
802 void PrefsPanel::ApplyChanges()
806 for( size_t i = 0; i < config_array.size(); i++ )
808 ConfigControl *control = config_array[i];
810 switch( control->GetType() )
812 case CONFIG_ITEM_STRING:
813 case CONFIG_ITEM_FILE:
814 case CONFIG_ITEM_DIRECTORY:
815 case CONFIG_ITEM_MODULE:
816 config_PutPsz( p_intf, control->GetName(),
817 control->GetPszValue() );
819 case CONFIG_ITEM_KEY:
820 /* So you don't need to restart to have the changes take effect */
821 val.i_int = control->GetIntValue();
822 var_Set( p_intf->p_libvlc, control->GetName(), val );
823 case CONFIG_ITEM_INTEGER:
824 case CONFIG_ITEM_BOOL:
825 config_PutInt( p_intf, control->GetName(),
826 control->GetIntValue() );
828 case CONFIG_ITEM_FLOAT:
829 config_PutFloat( p_intf, control->GetName(),
830 control->GetFloatValue() );