preferences.cpp 26.7 KB
Newer Older
1 2 3
/*****************************************************************************
 * preferences.cpp : WinCE gui plugin for VLC
 *****************************************************************************
4
 * Copyright (C) 2000-2004 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * $Id$
 *
 * Authors: Marodon Cedric <cedric_marodon@yahoo.fr>
 *          Gildas Bazin <gbazin@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29 30
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>                                            /* strerror() */
#include <stdio.h>
31 32 33
#include <vlc/vlc.h>
#include <vlc/intf.h>

34
#include "wince.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

#include <winuser.h>
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <commdlg.h>

#include <vlc_config_cat.h>

#include "preferences_widgets.h"

#define GENERAL_ID 1242
#define PLUGIN_ID 1243
#define CAPABILITY_ID 1244

/*****************************************************************************
 * Event Table.
 *****************************************************************************/

/* IDs for the controls and the menu commands */
enum
{
    Notebook_Event,
    MRL_Event,
    ResetAll_Event,
    Advanced_Event
};

/*****************************************************************************
 * Classes declarations.
 *****************************************************************************/
class ConfigTreeData;
class PrefsTreeCtrl
{
public:

    PrefsTreeCtrl() { }
72 73
    PrefsTreeCtrl( intf_thread_t *_p_intf, PrefsDialog *p_prefs_dialog,
                   HWND hwnd, HINSTANCE _hInst );
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    virtual ~PrefsTreeCtrl();

    void ApplyChanges();
    /*void CleanChanges();*/

    void OnSelectTreeItem( LPNM_TREEVIEW pnmtv, HWND parent, HINSTANCE hInst );
        
    ConfigTreeData *FindModuleConfig( ConfigTreeData *config_data );

    HWND hwndTV;

private:
    intf_thread_t *p_intf;
    PrefsDialog *p_prefs_dialog;
    vlc_bool_t b_advanced;

    HTREEITEM general_item;
    HTREEITEM plugins_item;
};

class PrefsPanel
{
public:

    PrefsPanel() { }
    PrefsPanel( HWND parent, HINSTANCE hInst, intf_thread_t *_p_intf,
                PrefsDialog *, int i_object_id, char *, char * );
    virtual ~PrefsPanel() {}

    void Hide();
    void Show();

    HWND config_window;

    int oldvalue;
    int maxvalue;

    void ApplyChanges();

private:
    intf_thread_t *p_intf;
    PrefsDialog *p_prefs_dialog;

    vlc_bool_t b_advanced;

    HWND label;

    vector<ConfigControl *> config_array;
};

class ConfigTreeData
{
public:

    ConfigTreeData() { b_submodule = 0; panel = NULL; psz_section = NULL;
                       psz_help = NULL; }
    virtual ~ConfigTreeData() { if( panel ) delete panel;
                                if( psz_section) free(psz_section);
                                if( psz_help) free(psz_help); }

    vlc_bool_t b_submodule;

    PrefsPanel *panel;
    int i_object_id;
    char *psz_section;
    char *psz_help;
};

/*****************************************************************************
 * Constructor.
 *****************************************************************************/
145 146 147
PrefsDialog::PrefsDialog( intf_thread_t *p_intf, CBaseWindow *p_parent,
                          HINSTANCE h_inst )
  :  CBaseWindow( p_intf, p_parent, h_inst )
148 149 150 151 152 153 154 155 156 157 158 159
{
    /* Initializations */
    prefs_tree = NULL;
}

/***********************************************************************

FUNCTION: 
  WndProc

PURPOSE: 
  Processes messages sent to the main window.
160

161
***********************************************************************/
162
LRESULT PrefsDialog::WndProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
163 164 165 166 167 168 169
{
    SHINITDLGINFO shidi;
    SHMENUBARINFO mbi;
    RECT rcClient;

    switch( msg )
    {
170
    case WM_INITDIALOG:
171 172 173 174 175 176 177 178 179 180
        shidi.dwMask = SHIDIM_FLAGS;
        shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN |
            SHIDIF_FULLSCREENNOMENUBAR;//SHIDIF_SIZEDLGFULLSCREEN;
        shidi.hDlg = hwnd;
        SHInitDialog( &shidi );

        //Create the menubar
        memset( &mbi, 0, sizeof (SHMENUBARINFO) );
        mbi.cbSize     = sizeof (SHMENUBARINFO);
        mbi.hwndParent = hwnd;
181
        mbi.dwFlags    = SHCMBF_EMPTYBAR;
182 183 184 185
        mbi.hInstRes   = hInst;

        if( !SHCreateMenuBar(&mbi) )
        {
186
            MessageBox(hwnd, _T("SHCreateMenuBar Failed"), _T("Error"), MB_OK);
187 188 189 190 191 192 193 194
            //return -1;
        }

        hwndCB = mbi.hwndMB;

        // Get the client area rect to put the panels in
        GetClientRect(hwnd, &rcClient);

195
        /* Create the buttons */            
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        advanced_checkbox =
            CreateWindow( _T("BUTTON"), _T("Advanced options"),
                        WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
                        5, 10, 15, 15, hwnd, NULL, hInst, NULL );
        SendMessage( advanced_checkbox, BM_SETCHECK, BST_UNCHECKED, 0 );

        advanced_label = CreateWindow( _T("STATIC"), _T("Advanced options"),
                        WS_CHILD | WS_VISIBLE | SS_LEFT,
                        5 + 15 + 5, 10, 110, 15,
                        hwnd, NULL, hInst, NULL);

        if( config_GetInt( p_intf, "advanced" ) )
        {
            SendMessage( advanced_checkbox, BM_SETCHECK, BST_CHECKED, 0 );
            /*dummy_event.SetInt(TRUE);
              OnAdvanced( dummy_event );*/
        }

        reset_button = CreateWindow( _T("BUTTON"), _T("Reset All"),
                        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                        rcClient.right - 5 - 80, 10 - 3, 80, 15 + 6,
                        hwnd, NULL, hInst, NULL );

        /* Create the preferences tree control */
        prefs_tree = new PrefsTreeCtrl( p_intf, this, hwnd, hInst );

        UpdateWindow( hwnd );
223 224 225 226 227
        break;

    case WM_CLOSE:
        EndDialog( hwnd, LOWORD( wp ) );
        break;
228

229 230 231 232
    case WM_SETFOCUS:
        SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
        break;

233 234 235 236 237 238
    case WM_COMMAND:
        if( LOWORD(wp) == IDOK )
        {
            OnOk();
            EndDialog( hwnd, LOWORD( wp ) );
        }
239
        break;
240 241 242

    case WM_NOTIFY:

243 244 245
        if( lp && prefs_tree &&
            ((LPNMHDR)lp)->hwndFrom == prefs_tree->hwndTV &&
            ((LPNMHDR)lp)->code == TVN_SELCHANGED )
246 247 248 249
        {
            prefs_tree->OnSelectTreeItem( (NM_TREEVIEW FAR *)(LPNMHDR)lp,
                                          hwnd, hInst );
        }
250
        break;
251 252 253 254 255 256

    case WM_VSCROLL:
    {
        TVITEM tvi = {0};
        tvi.mask = TVIF_PARAM;
        tvi.hItem = TreeView_GetSelection( prefs_tree->hwndTV );
257 258 259 260
	if( !tvi.hItem ) break;

        if( !TreeView_GetItem( prefs_tree->hwndTV, &tvi ) ) break;

261 262
        ConfigTreeData *config_data =
            prefs_tree->FindModuleConfig( (ConfigTreeData *)tvi.lParam );
263
        if( config_data && hwnd == config_data->panel->config_window ) 
264 265 266 267 268 269 270 271 272 273
        {
            int dy;
            RECT rc;
            GetWindowRect( hwnd, &rc);
            int newvalue = config_data->panel->oldvalue;
            switch ( GET_WM_VSCROLL_CODE(wp,lp) ) 
            {
            case SB_BOTTOM       : newvalue = 0; break;
            case SB_TOP          : newvalue = config_data->panel->maxvalue; break;
            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 276 277 278 279 280 281 282 283 284 285 286 287 288
            case SB_LINEUP       : newvalue -= 10; break;
            case SB_PAGEUP       : newvalue -= rc.bottom - rc.top - 25; break;
            case SB_THUMBPOSITION:
            case SB_THUMBTRACK   : newvalue = GET_WM_VSCROLL_POS(wp,lp); break;
            }
            newvalue = max(0,min(config_data->panel->maxvalue,newvalue));
            SetScrollPos( hwnd,SB_VERT,newvalue,TRUE);//SB_CTL si hwnd=hwndScrollBar, SB_VERT si window
            dy = config_data->panel->oldvalue - newvalue;

            ScrollWindowEx( hwnd, 0, dy, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN );
            UpdateWindow ( hwnd);

            config_data->panel->oldvalue = newvalue;                                
        }
289
        break;
290 291 292
    }

    default:
293
        break;
294 295
    }

296
    return FALSE;
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
}

/*****************************************************************************
 * Private methods.
 *****************************************************************************/

/*****************************************************************************
 * Events methods.
 *****************************************************************************/
void PrefsDialog::OnOk( void )
{
    prefs_tree->ApplyChanges();
    config_SaveConfigFile( p_intf, NULL );
}

/*****************************************************************************
 * PrefsTreeCtrl class definition.
 *****************************************************************************/
PrefsTreeCtrl::PrefsTreeCtrl( intf_thread_t *_p_intf,
                              PrefsDialog *_p_prefs_dialog, HWND hwnd,
                              HINSTANCE hInst )
{
    vlc_list_t      *p_list;
    module_t        *p_module;
    module_config_t *p_item;
    int i_index;

    INITCOMMONCONTROLSEX iccex;
    RECT rcClient;
    TVITEM tvi = {0}; 
    TVINSERTSTRUCT tvins = {0}; 
    HTREEITEM hPrev;

    size_t i_capability_count = 0;
    size_t i_child_index;

    HTREEITEM capability_item;

    /* Initializations */
    p_intf = _p_intf;
    p_prefs_dialog = _p_prefs_dialog;
    b_advanced = VLC_FALSE;

    /* Create a tree view */
    // Initialize the INITCOMMONCONTROLSEX structure.
    iccex.dwSize = sizeof( INITCOMMONCONTROLSEX );
    iccex.dwICC = ICC_TREEVIEW_CLASSES;

    // Registers Statusbar control classes from the common control dll
    InitCommonControlsEx( &iccex );

    // Get the client area rect to put the tv in
    GetClientRect(hwnd, &rcClient);

    // Create the tree-view control.
    hwndTV = CreateWindowEx( 0, WC_TREEVIEW, NULL,
        WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES |
        TVS_LINESATROOT | TVS_HASBUTTONS,
        5, 10 + 2*(15 + 10) + 105 + 5, rcClient.right - 5 - 5, 6*15,
        hwnd, NULL, hInst, NULL );

358
    tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
359 360 361 362 363 364 365 366 367 368 369 370 371 372

    /* List the plugins */
    p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
    if( !p_list ) return;

    /*
     * Build a tree of the main options
     */
    ConfigTreeData *config_data = new ConfigTreeData;
    config_data->i_object_id = GENERAL_ID;
    config_data->psz_help = strdup("nothing");//strdup( GENERAL_HELP );
    config_data->psz_section = strdup( GENERAL_TITLE );
    tvi.pszText = _T("General settings");
    tvi.cchTextMax = lstrlen(_T("General settings"));
373
    tvi.lParam = (long)config_data;
374
    tvins.item = tvi;
375 376
    tvins.hInsertAfter = TVI_FIRST;
    tvins.hParent = TVI_ROOT;
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442

    // Add the item to the tree-view control.
    hPrev = (HTREEITEM) TreeView_InsertItem( hwndTV, &tvins);
    general_item = hPrev;

    for( i_index = 0; i_index < p_list->i_count; i_index++ )
    {
        p_module = (module_t *)p_list->p_values[i_index].p_object;
        if( !strcmp( p_module->psz_object_name, "main" ) )
            break;
    }
    if( i_index < p_list->i_count )
    {
        /* We found the main module */

        /* Enumerate config categories and store a reference so we can
         * generate their config panel them when it is asked by the user. */
        p_item = p_module->p_config;

        if( p_item ) do
        {
            switch( p_item->i_type )
            {
            case CONFIG_HINT_CATEGORY:
                ConfigTreeData *config_data = new ConfigTreeData;
                config_data->psz_section = strdup( p_item->psz_text );
                if( p_item->psz_longtext )
                {
                    config_data->psz_help =
                        strdup( p_item->psz_longtext );
                }
                else
                {
                    config_data->psz_help = NULL;
                }
                config_data->i_object_id = p_module->i_object_id;

                /* Add the category to the tree */
                // Set the text of the item. 
                tvi.pszText = _FROMMB(p_item->psz_text); 
                tvi.cchTextMax = _tcslen(tvi.pszText);
                tvi.lParam = (long)config_data;
                tvins.item = tvi;
                tvins.hInsertAfter = hPrev; 
                tvins.hParent = general_item; //level 3
    
                // Add the item to the tree-view control. 
                hPrev = (HTREEITEM)TreeView_InsertItem( hwndTV, &tvins );

                break;
            }
        }
        while( p_item->i_type != CONFIG_HINT_END && p_item++ );

        TreeView_SortChildren( hwndTV, general_item, 0 );
    }
        
    /*
     * Build a tree of all the plugins
     */
    config_data = new ConfigTreeData;
    config_data->i_object_id = PLUGIN_ID;
    config_data->psz_help = strdup("nothing");//strdup( PLUGIN_HELP );
    config_data->psz_section = strdup("nothing");//strdup( PLUGIN_TITLE );
    tvi.pszText = _T("Modules");
    tvi.cchTextMax = lstrlen(_T("Modules"));
443
    tvi.lParam = (long)config_data;
444
    tvins.item = tvi;
445 446
    tvins.hInsertAfter = TVI_LAST;
    tvins.hParent = TVI_ROOT;
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

    // Add the item to the tree-view control.
    hPrev = (HTREEITEM) TreeView_InsertItem( hwndTV, &tvins);
    plugins_item = hPrev;

    i_capability_count = 0;
    for( i_index = 0; i_index < p_list->i_count; i_index++ )
    {
        i_child_index = 0;

        p_module = (module_t *)p_list->p_values[i_index].p_object;

        /* Exclude the main module */
        if( !strcmp( p_module->psz_object_name, "main" ) )
            continue;

        /* Exclude empty plugins (submodules don't have config options, they
         * are stored in the parent module) */
        if( p_module->b_submodule )
            p_item = ((module_t *)p_module->p_parent)->p_config;
        else
            p_item = p_module->p_config;

        if( !p_item ) continue;
        do
        {
            if( p_item->i_type & CONFIG_ITEM )
                break;
        }
        while( p_item->i_type != CONFIG_HINT_END && p_item++ );
        if( p_item->i_type == CONFIG_HINT_END ) continue;

        /* Find the capability child item */
        /*long cookie; size_t i_child_index;*/
        capability_item = TreeView_GetChild( hwndTV, plugins_item );
        while( capability_item != 0 )
        {
            TVITEM capability_tvi = {0};
485
            TCHAR psz_text[256];
486 487 488
            i_child_index++;

            capability_tvi.mask = TVIF_TEXT;
489 490
            capability_tvi.pszText = psz_text;
            capability_tvi.cchTextMax = 256;
491 492
            capability_tvi.hItem = capability_item;
            TreeView_GetItem( hwndTV, &capability_tvi );
493 494 495 496 497
            if( !strcmp( _TOMB(capability_tvi.pszText),
                         p_module->psz_capability ) ) break;
 
            capability_item =
                TreeView_GetNextSibling( hwndTV, capability_item );
498 499 500 501 502 503 504 505 506 507 508 509 510 511
        }

        if( i_child_index == i_capability_count &&
            p_module->psz_capability && *p_module->psz_capability )
        {
            /* We didn't find it, add it */
            ConfigTreeData *config_data = new ConfigTreeData;
            config_data->psz_section =
                strdup( GetCapabilityHelp( p_module->psz_capability , 1 ) );
            config_data->psz_help =
                strdup( GetCapabilityHelp( p_module->psz_capability , 2 ) );
            config_data->i_object_id = CAPABILITY_ID;
            tvi.pszText = _FROMMB(p_module->psz_capability);
            tvi.cchTextMax = _tcslen(tvi.pszText);
512
            tvi.lParam = (long)config_data;
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
            tvins.item = tvi;
            tvins.hInsertAfter = plugins_item; 
            tvins.hParent = plugins_item;// level 3

            // Add the item to the tree-view control. 
            capability_item = (HTREEITEM) TreeView_InsertItem( hwndTV, &tvins);

            i_capability_count++;
        }

        /* Add the plugin to the tree */
        ConfigTreeData *config_data = new ConfigTreeData;
        config_data->b_submodule = p_module->b_submodule;
        config_data->i_object_id = p_module->b_submodule ?
            ((module_t *)p_module->p_parent)->i_object_id :
            p_module->i_object_id;
        config_data->psz_help = NULL;
        tvi.pszText = _FROMMB(p_module->psz_object_name);
        tvi.cchTextMax = _tcslen(tvi.pszText);
532
        tvi.lParam = (long)config_data;
533 534 535 536 537
        tvins.item = tvi;
        tvins.hInsertAfter = capability_item; 
        tvins.hParent = capability_item;// level 4

        // Add the item to the tree-view control. 
538
        TreeView_InsertItem( hwndTV, &tvins );
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
    }

    /* Sort all this mess */
    /*long cookie; size_t i_child_index;*/
    TreeView_SortChildren( hwndTV, plugins_item, 0 );
    capability_item = TreeView_GetChild( hwndTV, plugins_item );
    while( capability_item != 0 )
    {
        TreeView_SortChildren( hwndTV, capability_item, 0 );
        capability_item = TreeView_GetNextSibling( hwndTV, capability_item );
    }

    /* Clean-up everything */
    vlc_list_release( p_list );

    TreeView_Expand( hwndTV, general_item, TVE_EXPANDPARTIAL |TVE_EXPAND );
}

PrefsTreeCtrl::~PrefsTreeCtrl()
{
}

void PrefsTreeCtrl::ApplyChanges()
{
    /*long cookie, cookie2;*/
    ConfigTreeData *config_data;

    /* Apply changes to the main module */
    HTREEITEM item = TreeView_GetChild( hwndTV, general_item );
    while( item != 0 )
    {
        TVITEM tvi = {0};
        tvi.mask = TVIF_PARAM;
        tvi.hItem = item;
        TreeView_GetItem( hwndTV, &tvi );
        config_data = (ConfigTreeData *)tvi.lParam;
        if( config_data && config_data->panel )
        {
            config_data->panel->ApplyChanges();
        }

        item = TreeView_GetNextSibling( hwndTV, item );
    }

    /* Apply changes to the plugins */
    item = TreeView_GetChild( hwndTV, plugins_item );
    while( item != 0 )
    {
        HTREEITEM item2 = TreeView_GetChild( hwndTV, item );
        while( item2 != 0 )
        {       
            TVITEM tvi = {0};
            tvi.mask = TVIF_PARAM;
            tvi.hItem = item2;
            TreeView_GetItem( hwndTV, &tvi );
            config_data = (ConfigTreeData *)tvi.lParam;
            if( config_data && config_data->panel )
            {
                config_data->panel->ApplyChanges();
            }
            item2 = TreeView_GetNextSibling( hwndTV, item2 );
        }
        item = TreeView_GetNextSibling( hwndTV, item );
    }
}

ConfigTreeData *PrefsTreeCtrl::FindModuleConfig( ConfigTreeData *config_data )
{
    /* We need this complexity because submodules don't have their own config
     * options. They use the parent module ones. */

    if( !config_data || !config_data->b_submodule )
    {
        return config_data;
    }

    /*long cookie, cookie2;*/
    ConfigTreeData *config_new;
    HTREEITEM item = TreeView_GetChild( hwndTV, plugins_item );
    while( item != 0 )
    {
        HTREEITEM item2 = TreeView_GetChild( hwndTV, item );
        while( item2 != 0 )
        {       
            TVITEM tvi = {0};
            tvi.mask = TVIF_PARAM;
            tvi.hItem = item2;
            TreeView_GetItem( hwndTV, &tvi );
            config_new = (ConfigTreeData *)tvi.lParam;
            if( config_new && !config_new->b_submodule &&
                config_new->i_object_id == config_data->i_object_id )
            {
                return config_new;
            }
            item2 = TreeView_GetNextSibling( hwndTV, item2 );
        }
        item = TreeView_GetNextSibling( hwndTV, item );
    }

    /* Found nothing */
    return NULL;
}

void PrefsTreeCtrl::OnSelectTreeItem( LPNM_TREEVIEW pnmtv, HWND parent,
                                      HINSTANCE hInst )
{
    ConfigTreeData *config_data = NULL;

    if( pnmtv->itemOld.hItem )
        config_data = FindModuleConfig( (ConfigTreeData *)pnmtv->itemOld.lParam );

    if( config_data && config_data->panel )
    {
        config_data->panel->Hide();
    }

    /* Don't use event.GetItem() because we also send fake events */
    TVITEM tvi = {0};
    tvi.mask = TVIF_PARAM;
    tvi.hItem = TreeView_GetSelection( hwndTV );
    TreeView_GetItem( hwndTV, &tvi );
    config_data = FindModuleConfig( (ConfigTreeData *)tvi.lParam );
    if( config_data )
    {
        if( !config_data->panel )
        {
            /* The panel hasn't been created yet. Let's do it. */
            config_data->panel =
                new PrefsPanel( parent, hInst, p_intf, p_prefs_dialog,
                                config_data->i_object_id,
                                config_data->psz_section,
                                config_data->psz_help );
        }
        else
        {
            config_data->panel->Show();
        }
    }
}

/*****************************************************************************
 * PrefsPanel class definition.
 *****************************************************************************/
PrefsPanel::PrefsPanel( HWND parent, HINSTANCE hInst, intf_thread_t *_p_intf,
                        PrefsDialog *_p_prefs_dialog,
                        int i_object_id, char *psz_section, char *psz_help )
{
    module_config_t *p_item;
    module_t *p_module = NULL;

    /* Initializations */
    p_intf = _p_intf;
    p_prefs_dialog = _p_prefs_dialog;

    b_advanced = VLC_TRUE;

    if( i_object_id == PLUGIN_ID || i_object_id == GENERAL_ID ||
        i_object_id == CAPABILITY_ID )
    {
        label = CreateWindow( _T("STATIC"), _FROMMB(psz_section),
                              WS_CHILD | WS_VISIBLE | SS_LEFT,
                              5, 10 + (15 + 10), 200, 15,
                              parent, NULL, hInst, NULL );
        config_window = NULL;
    }
    else
    {
        /* Get a pointer to the module */
        p_module = (module_t *)vlc_object_get( p_intf,  i_object_id );
        if( p_module->i_object_type != VLC_OBJECT_MODULE )
        {
            /* 0OOoo something went really bad */
            return;
        }

        /* Enumerate config options and add corresponding config boxes
         * (submodules don't have config options, they are stored in the
         *  parent module) */
        if( p_module->b_submodule )
            p_item = ((module_t *)p_module->p_parent)->p_config;
        else
            p_item = p_module->p_config;

        /* Find the category if it has been specified */
        if( psz_section && p_item->i_type == CONFIG_HINT_CATEGORY )
        {
725
            while( !(p_item->i_type == CONFIG_HINT_CATEGORY) ||
726 727
                   strcmp( psz_section, p_item->psz_text ) )
            {
728
                if( p_item->i_type == CONFIG_HINT_END ) break;
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
                p_item++;
            }
        }

        /* Add a head title to the panel */
        label = CreateWindow( _T("STATIC"), _FROMMB(psz_section ?
                        p_item->psz_text : p_module->psz_longname),
                        WS_CHILD | WS_VISIBLE | SS_LEFT,
                        5, 10 + (15 + 10), 250, 15,
                        parent, NULL, hInst, NULL );

        WNDCLASS wc;
        memset( &wc, 0, sizeof(wc) );
        wc.style          = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc    = (WNDPROC) _p_prefs_dialog->BaseWndProc;
        wc.cbClsExtra     = 0;
        wc.cbWndExtra     = 0;
        wc.hInstance      = hInst;
        wc.hIcon          = 0;
        wc.hCursor        = 0;
        wc.hbrBackground  = (HBRUSH) GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName   = 0;
        wc.lpszClassName  = _T("PrefsPanelClass");
        RegisterClass(&wc);

        RECT rc;
        GetWindowRect( parent, &rc);
        config_window = CreateWindow( _T("PrefsPanelClass"),
                        _T("config_window"),
                        WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER,
                        5, 10 + 2*(15 + 10), rc.right - 5 - 7, 105,
                        parent, NULL, hInst, (void *) _p_prefs_dialog );

        int y_pos = 5;
        if( p_item ) do
        {
            /* If a category has been specified, check we finished the job */
            if( psz_section && p_item->i_type == CONFIG_HINT_CATEGORY &&
                strcmp( psz_section, p_item->psz_text ) )
                break;

            ConfigControl *control =
                CreateConfigControl( VLC_OBJECT(p_intf),
                                     p_item, config_window,
                                     hInst, &y_pos );

            /* Don't add items that were not recognized */
            if( control == NULL ) continue;

            /* Add the config data to our array so we can keep a trace of it */
            config_array.push_back( control );
        }
        while( p_item->i_type != CONFIG_HINT_END && p_item++ );
                
        GetWindowRect( config_window, &rc);
        maxvalue = y_pos - (rc.bottom - rc.top) + 5;
        oldvalue = 0;
        SetScrollRange( config_window, SB_VERT, 0, maxvalue, TRUE );
    }
}

void PrefsPanel::Hide()
{
    ShowWindow( label, SW_HIDE );
    if( config_window ) ShowWindow( config_window, SW_HIDE );
}

void PrefsPanel::Show()
{
    ShowWindow( label, SW_SHOW );
    if( config_window ) ShowWindow( config_window, SW_SHOW );
}

void PrefsPanel::ApplyChanges()
{
    vlc_value_t val;

    for( size_t i = 0; i < config_array.size(); i++ )
    {
        ConfigControl *control = config_array[i];

        switch( control->GetType() )
        {
        case CONFIG_ITEM_STRING:
        case CONFIG_ITEM_FILE:
        case CONFIG_ITEM_DIRECTORY:
        case CONFIG_ITEM_MODULE:
            config_PutPsz( p_intf, control->GetName(),
                           control->GetPszValue() );
            break;
        case CONFIG_ITEM_KEY:
            /* So you don't need to restart to have the changes take effect */
            val.i_int = control->GetIntValue();
            var_Set( p_intf->p_vlc, control->GetName(), val );
        case CONFIG_ITEM_INTEGER:
        case CONFIG_ITEM_BOOL:
            config_PutInt( p_intf, control->GetName(),
                           control->GetIntValue() );
            break;
        case CONFIG_ITEM_FLOAT:
            config_PutFloat( p_intf, control->GetName(),
                             control->GetFloatValue() );
            break;
        }
    }
}