menus.cpp 23.3 KB
Newer Older
gbazin's avatar
 
gbazin committed
1 2 3 4
/*****************************************************************************
 * menus.cpp : wxWindows plugin for vlc
 *****************************************************************************
 * Copyright (C) 2000-2001 VideoLAN
5
 * $Id: menus.cpp,v 1.22 2003/10/15 12:24:14 gbazin Exp $
gbazin's avatar
 
gbazin committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <errno.h>                                                 /* ENOMEM */
#include <string.h>                                            /* strerror() */
#include <stdio.h>

#include <vlc/vlc.h>
#include <vlc/intf.h>

#include "wxwindows.h"
36
#include <wx/listctrl.h>
gbazin's avatar
 
gbazin committed
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

class wxMenuItemExt: public wxMenuItem
{
public:
    /* Constructor */
    wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
                   const wxString& helpString, wxItemKind kind,
                   char *_psz_var, int _i_object_id, vlc_value_t _val,
                   int _i_val_type );

    virtual ~wxMenuItemExt();

    char *psz_var;
    int  i_val_type;
    int  i_object_id;
    vlc_value_t val;

private:

};

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

/* IDs for the controls and the menu commands */
enum
{
    /* menu items */
gbazin's avatar
 
gbazin committed
66
    MenuDummy_Event = wxID_HIGHEST + 1000,
67
    OpenFileSimple_Event = wxID_HIGHEST + 1100,
gbazin's avatar
 
gbazin committed
68 69 70 71 72 73 74
    OpenFile_Event,
    OpenDisc_Event,
    OpenNet_Event,
    FirstAutoGenerated_Event = wxID_HIGHEST + 1999,
    AudioMenu_Events = wxID_HIGHEST + 2000,
    VideoMenu_Events = wxID_HIGHEST + 3000,
    NavigMenu_Events = wxID_HIGHEST + 4000,
gbazin's avatar
 
gbazin committed
75 76
    MiscMenu_Events  = wxID_HIGHEST + 5000,
    PopupMenu_Events = wxID_HIGHEST + 6000
gbazin's avatar
 
gbazin committed
77 78 79 80 81 82
};

BEGIN_EVENT_TABLE(Menu, wxMenu)
END_EVENT_TABLE()

BEGIN_EVENT_TABLE(MenuEvtHandler, wxEvtHandler)
gbazin's avatar
 
gbazin committed
83 84 85 86
    EVT_MENU(OpenFileSimple_Event, MenuEvtHandler::OnShowDialog)
    EVT_MENU(OpenFile_Event, MenuEvtHandler::OnShowDialog)
    EVT_MENU(OpenDisc_Event, MenuEvtHandler::OnShowDialog)
    EVT_MENU(OpenNet_Event, MenuEvtHandler::OnShowDialog)
gbazin's avatar
 
gbazin committed
87 88 89
    EVT_MENU(-1, MenuEvtHandler::OnMenuEvent)
END_EVENT_TABLE()

gbazin's avatar
 
gbazin committed
90 91 92 93 94 95 96 97 98 99
wxMenu *OpenStreamMenu( intf_thread_t *p_intf )
{
    wxMenu *menu = new wxMenu;
    menu->Append( OpenFileSimple_Event, wxU(_("Simple &Open ...")) );
    menu->Append( OpenFile_Event, wxU(_("Open &File...")) );
    menu->Append( OpenDisc_Event, wxU(_("Open &Disc...")) );
    menu->Append( OpenNet_Event, wxU(_("Open &Network Stream...")) );
    return menu;
}

gbazin's avatar
 
gbazin committed
100
void PopupMenu( intf_thread_t *p_intf, wxWindow *p_parent,
gbazin's avatar
 
gbazin committed
101
                const wxPoint& pos )
gbazin's avatar
 
gbazin committed
102 103
{
    vlc_object_t *p_object;
gbazin's avatar
 
gbazin committed
104 105
    char *ppsz_varnames[29];
    int pi_objects[29];
gbazin's avatar
 
gbazin committed
106 107 108
    int i = 0;

    /* Initializations */
gbazin's avatar
 
gbazin committed
109
    memset( pi_objects, 0, 29 * sizeof(int) );
gbazin's avatar
 
gbazin committed
110 111 112 113 114

    /* Audio menu */
    ppsz_varnames[i++] = _("Audio menu");
    ppsz_varnames[i++] = NULL; /* Separator */

gbazin's avatar
 
gbazin committed
115
    p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
gbazin's avatar
 
gbazin committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
                                                FIND_ANYWHERE );
    if( p_object != NULL )
    {
        ppsz_varnames[i] = "audio-device";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "audio-channels";
        pi_objects[i++] = p_object->i_object_id;
        vlc_object_release( p_object );
    }

    /* Video menu */
    ppsz_varnames[i++] = NULL; /* Separator */
    ppsz_varnames[i++] = _("Video menu");
    ppsz_varnames[i++] = NULL; /* Separator */

gbazin's avatar
 
gbazin committed
131
    p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
gbazin's avatar
 
gbazin committed
132 133 134 135 136
                                                FIND_ANYWHERE );
    if( p_object != NULL )
    {
        ppsz_varnames[i] = "fullscreen";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
137 138
        ppsz_varnames[i] = "deinterlace";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
139 140 141 142
        ppsz_varnames[i] = "aspect-ratio";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "crop";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
143 144
        ppsz_varnames[i] = "directx-on-top";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
145 146 147 148 149 150 151 152
        vlc_object_release( p_object );
    }

    /* Input menu */
    ppsz_varnames[i++] = NULL; /* Separator */
    ppsz_varnames[i++] = _("Input menu");
    ppsz_varnames[i++] = NULL; /* Separator */

gbazin's avatar
 
gbazin committed
153
    p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
gbazin's avatar
 
gbazin committed
154 155 156 157 158 159 160
                                                FIND_ANYWHERE );
    if( p_object != NULL )
    {
        ppsz_varnames[i] = "title";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "chapter";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
161 162
        ppsz_varnames[i] = "program";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
163 164
        ppsz_varnames[i] = "navigation";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
165
        ppsz_varnames[i] = "dvd_menus";
gbazin's avatar
 
gbazin committed
166
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
167 168 169 170 171 172 173 174

        ppsz_varnames[i] = "video-es";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "audio-es";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "spu-es";
        pi_objects[i++] = p_object->i_object_id;

gbazin's avatar
 
gbazin committed
175 176 177
        vlc_object_release( p_object );
    }

gbazin's avatar
 
gbazin committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    /* Interface menu */
    ppsz_varnames[i++] = NULL; /* Separator */
    ppsz_varnames[i++] = _("Interface menu");
    ppsz_varnames[i++] = NULL; /* Separator */

    /* vlc_object_find is needed because of the dialogs provider case */
    p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INTF,
                                                FIND_PARENT );
    if( p_object != NULL )
    {
        ppsz_varnames[i] = "intf-switch";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "intf-add";
        pi_objects[i++] = p_object->i_object_id;

        vlc_object_release( p_object );
    }

gbazin's avatar
 
gbazin committed
196
    /* Build menu */
gbazin's avatar
 
gbazin committed
197
    Menu popupmenu( p_intf, p_parent, i,
gbazin's avatar
 
gbazin committed
198
                     ppsz_varnames, pi_objects, PopupMenu_Events );
gbazin's avatar
 
gbazin committed
199

gbazin's avatar
 
gbazin committed
200 201 202 203 204 205 206
#if 1
    /* Add static entries */
    popupmenu.AppendSeparator();
    popupmenu.Append( MenuDummy_Event, wxU("Open"),
                      OpenStreamMenu( p_intf ), wxT("") );
#endif

gbazin's avatar
 
gbazin committed
207 208 209
    p_intf->p_sys->p_popup_menu = &popupmenu;
    p_parent->PopupMenu( &popupmenu, pos.x, pos.y );
    p_intf->p_sys->p_popup_menu = NULL;
gbazin's avatar
 
gbazin committed
210 211
}

gbazin's avatar
 
gbazin committed
212
wxMenu *AudioMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
gbazin's avatar
 
gbazin committed
213 214
{
    vlc_object_t *p_object;
gbazin's avatar
 
gbazin committed
215 216
    char *ppsz_varnames[5];
    int pi_objects[5];
gbazin's avatar
 
gbazin committed
217 218 219
    int i = 0;

    /* Initializations */
gbazin's avatar
 
gbazin committed
220
    memset( pi_objects, 0, 5 * sizeof(int) );
gbazin's avatar
 
gbazin committed
221 222 223 224 225 226 227 228 229 230 231 232

    p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
                                                FIND_ANYWHERE );
    if( p_object != NULL )
    {
        ppsz_varnames[i] = "audio-device";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "audio-channels";
        pi_objects[i++] = p_object->i_object_id;
        vlc_object_release( p_object );
    }

gbazin's avatar
 
gbazin committed
233 234 235 236 237 238 239 240 241
    p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
                                                FIND_ANYWHERE );
    if( p_object != NULL )
    {
        ppsz_varnames[i] = "audio-es";
        pi_objects[i++] = p_object->i_object_id;
        vlc_object_release( p_object );
    }

gbazin's avatar
 
gbazin committed
242
    /* Build menu */
gbazin's avatar
 
gbazin committed
243
    return new Menu( _p_intf, p_parent, i,
gbazin's avatar
 
gbazin committed
244
                     ppsz_varnames, pi_objects, AudioMenu_Events );
gbazin's avatar
 
gbazin committed
245 246
}

gbazin's avatar
 
gbazin committed
247
wxMenu *VideoMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
gbazin's avatar
 
gbazin committed
248 249
{
    vlc_object_t *p_object;
gbazin's avatar
 
gbazin committed
250 251
    char *ppsz_varnames[6];
    int pi_objects[6];
gbazin's avatar
 
gbazin committed
252 253 254
    int i = 0;

    /* Initializations */
gbazin's avatar
 
gbazin committed
255
    memset( pi_objects, 0, 6 * sizeof(int) );
gbazin's avatar
 
gbazin committed
256 257 258 259 260 261 262

    p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
                                                FIND_ANYWHERE );
    if( p_object != NULL )
    {
        ppsz_varnames[i] = "fullscreen";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
263 264
        ppsz_varnames[i] = "deinterlace";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
265 266 267 268
        ppsz_varnames[i] = "aspect-ratio";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "crop";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
269 270
        ppsz_varnames[i] = "directx-on-top";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
271 272 273
        vlc_object_release( p_object );
    }

gbazin's avatar
 
gbazin committed
274 275 276 277 278 279 280 281 282 283 284
    p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
                                                FIND_ANYWHERE );
    if( p_object != NULL )
    {
        ppsz_varnames[i] = "video-es";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "spu-es";
        pi_objects[i++] = p_object->i_object_id;
        vlc_object_release( p_object );
    }

gbazin's avatar
 
gbazin committed
285
    /* Build menu */
gbazin's avatar
 
gbazin committed
286
    return new Menu( _p_intf, p_parent, i,
gbazin's avatar
 
gbazin committed
287
                     ppsz_varnames, pi_objects, VideoMenu_Events );
gbazin's avatar
 
gbazin committed
288 289
}

gbazin's avatar
 
gbazin committed
290
wxMenu *NavigMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
gbazin's avatar
 
gbazin committed
291 292
{
    vlc_object_t *p_object;
gbazin's avatar
 
gbazin committed
293 294
    char *ppsz_varnames[10];
    int pi_objects[10];
gbazin's avatar
 
gbazin committed
295 296 297 298 299 300 301 302 303 304 305 306 307
    int i = 0;

    /* Initializations */
    memset( pi_objects, 0, 4 * sizeof(int) );

    p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
                                                FIND_ANYWHERE );
    if( p_object != NULL )
    {
        ppsz_varnames[i] = "title";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "chapter";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
308 309
        ppsz_varnames[i] = "program";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
310 311
        ppsz_varnames[i] = "navigation";
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
312
        ppsz_varnames[i] = "dvd_menus";
gbazin's avatar
 
gbazin committed
313
        pi_objects[i++] = p_object->i_object_id;
gbazin's avatar
 
gbazin committed
314 315 316 317 318 319 320 321 322 323 324

        ppsz_varnames[i] = "prev-title";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "next-title";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "prev-chapter";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "next-chapter";
        pi_objects[i++] = p_object->i_object_id;

        vlc_object_release( p_object );
gbazin's avatar
 
gbazin committed
325 326 327
    }

    /* Build menu */
gbazin's avatar
 
gbazin committed
328
    return new Menu( _p_intf, p_parent, i,
gbazin's avatar
 
gbazin committed
329 330 331
                     ppsz_varnames, pi_objects, NavigMenu_Events );
}

gbazin's avatar
 
gbazin committed
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
wxMenu *MiscMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
{
    vlc_object_t *p_object;
    char *ppsz_varnames[10];
    int pi_objects[10];
    int i = 0;

    /* Initializations */
    memset( pi_objects, 0, 4 * sizeof(int) );

    p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INTF,
                                                FIND_PARENT );
    if( p_object != NULL )
    {
        ppsz_varnames[i] = "intf-switch";
        pi_objects[i++] = p_object->i_object_id;
        ppsz_varnames[i] = "intf-add";
        pi_objects[i++] = p_object->i_object_id;
        vlc_object_release( p_object );
    }

    /* Build menu */
    return new Menu( _p_intf, p_parent, i,
                     ppsz_varnames, pi_objects, MiscMenu_Events );
}

gbazin's avatar
 
gbazin committed
358 359 360
/*****************************************************************************
 * Constructor.
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
361
Menu::Menu( intf_thread_t *_p_intf, wxWindow *p_parent,
gbazin's avatar
 
gbazin committed
362
            int i_count, char **ppsz_varnames, int *pi_objects,
gbazin's avatar
 
gbazin committed
363
            int i_start_id ): wxMenu( )
gbazin's avatar
 
gbazin committed
364 365
{
    vlc_object_t *p_object;
366
    vlc_bool_t b_section_empty = VLC_FALSE;
gbazin's avatar
 
gbazin committed
367 368 369 370 371
    int i;

    /* Initializations */
    p_intf = _p_intf;

gbazin's avatar
 
gbazin committed
372
    i_item_id = i_start_id;
gbazin's avatar
 
gbazin committed
373 374 375 376 377

    for( i = 0; i < i_count; i++ )
    {
        if( !ppsz_varnames[i] )
        {
378 379 380 381 382 383
            if( b_section_empty )
            {
                Append( MenuDummy_Event + i, wxU(_("Empty")) );
                Enable( MenuDummy_Event + i, FALSE );
            }

gbazin's avatar
 
gbazin committed
384
            AppendSeparator();
385
            b_section_empty = VLC_TRUE;
gbazin's avatar
 
gbazin committed
386 387 388 389 390
            continue;
        }

        if( !pi_objects[i] )
        {
gbazin's avatar
 
gbazin committed
391
            Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
392
            b_section_empty = VLC_FALSE;
gbazin's avatar
 
gbazin committed
393 394 395 396 397 398
            continue;
        }

        p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
        if( p_object == NULL ) continue;

399
        b_section_empty = VLC_FALSE;
gbazin's avatar
 
gbazin committed
400 401 402 403
        CreateMenuItem( this, ppsz_varnames[i], p_object );
        vlc_object_release( p_object );
    }

gbazin's avatar
 
gbazin committed
404
    /* Special case for empty menus */
405
    if( GetMenuItemCount() == 0 || b_section_empty )
gbazin's avatar
 
gbazin committed
406
    {
407 408
        Append( MenuDummy_Event + i, wxU(_("Empty")) );
        Enable( MenuDummy_Event + i, FALSE );
gbazin's avatar
 
gbazin committed
409
    }
gbazin's avatar
 
gbazin committed
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 443 444 445 446
}

Menu::~Menu()
{
}

/*****************************************************************************
 * Private methods.
 *****************************************************************************/
void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
                           vlc_object_t *p_object )
{
    wxMenuItemExt *menuitem;
    vlc_value_t val, text;
    int i_type;

    /* Check the type of the object variable */
    i_type = var_Type( p_object, psz_var );

    switch( i_type & VLC_VAR_TYPE )
    {
    case VLC_VAR_VOID:
    case VLC_VAR_BOOL:
    case VLC_VAR_VARIABLE:
    case VLC_VAR_STRING:
    case VLC_VAR_INTEGER:
        break;
    default:
        /* Variable doesn't exist or isn't handled */
        return;
    }

    /* Make sure we want to display the variable */
    if( i_type & VLC_VAR_HASCHOICE )
    {
        var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
        if( val.i_int == 0 ) return;
gbazin's avatar
 
gbazin committed
447 448
        if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
            return;
gbazin's avatar
 
gbazin committed
449 450 451 452 453
    }

    /* Get the descriptive name of the variable */
    var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );

gbazin's avatar
 
gbazin committed
454 455
    var_Get( p_object, psz_var, &val );

gbazin's avatar
 
gbazin committed
456 457 458
    if( i_type & VLC_VAR_HASCHOICE )
    {
        menu->Append( MenuDummy_Event,
gbazin's avatar
 
gbazin committed
459
                      wxU(text.psz_string ? text.psz_string : psz_var),
gbazin's avatar
 
gbazin committed
460
                      CreateChoicesMenu( psz_var, p_object ),
gbazin's avatar
 
gbazin committed
461
                      wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
gbazin's avatar
 
gbazin committed
462 463 464 465 466 467 468 469 470 471

        if( text.psz_string ) free( text.psz_string );
        return;
    }


    switch( i_type & VLC_VAR_TYPE )
    {
    case VLC_VAR_VOID:
        menuitem = new wxMenuItemExt( menu, ++i_item_id,
gbazin's avatar
 
gbazin committed
472 473 474
                                      wxU(text.psz_string ?
                                        text.psz_string : psz_var),
                                      wxT(""), wxITEM_NORMAL, strdup(psz_var),
gbazin's avatar
 
gbazin committed
475 476 477 478 479
                                      p_object->i_object_id, val, i_type );
        menu->Append( menuitem );
        break;

    case VLC_VAR_BOOL:
gbazin's avatar
 
gbazin committed
480
        val.b_bool = !val.b_bool;
gbazin's avatar
 
gbazin committed
481
        menuitem = new wxMenuItemExt( menu, ++i_item_id,
gbazin's avatar
 
gbazin committed
482 483 484
                                      wxU(text.psz_string ?
                                        text.psz_string : psz_var),
                                      wxT(""), wxITEM_CHECK, strdup(psz_var),
gbazin's avatar
 
gbazin committed
485 486
                                      p_object->i_object_id, val, i_type );
        menu->Append( menuitem );
gbazin's avatar
 
gbazin committed
487
        Check( i_item_id, val.b_bool ? FALSE : TRUE );
gbazin's avatar
 
gbazin committed
488 489 490 491 492 493 494
        break;

    default:
        if( text.psz_string ) free( text.psz_string );
        return;
    }

gbazin's avatar
 
gbazin committed
495
    if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
gbazin's avatar
 
gbazin committed
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
    if( text.psz_string ) free( text.psz_string );
}

wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
{
    vlc_value_t val, val_list, text_list;
    int i_type, i;

    /* Check the type of the object variable */
    i_type = var_Type( p_object, psz_var );

    /* Make sure we want to display the variable */
    if( i_type & VLC_VAR_HASCHOICE )
    {
        var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
        if( val.i_int == 0 ) return NULL;
gbazin's avatar
 
gbazin committed
512 513
        if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
            return NULL;
gbazin's avatar
 
gbazin committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
    }
    else
    {
        return NULL;
    }

    switch( i_type & VLC_VAR_TYPE )
    {
    case VLC_VAR_VOID:
    case VLC_VAR_BOOL:
    case VLC_VAR_VARIABLE:
    case VLC_VAR_STRING:
    case VLC_VAR_INTEGER:
        break;
    default:
        /* Variable doesn't exist or isn't handled */
        return NULL;
    }

    if( var_Get( p_object, psz_var, &val ) < 0 )
    {
        return NULL;
    }

    if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
                    &val_list, &text_list ) < 0 )
    {
        if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
        return NULL;
    }

    wxMenu *menu = new wxMenu;
    for( i = 0; i < val_list.p_list->i_count; i++ )
    {
        vlc_value_t another_val;
        wxMenuItemExt *menuitem;

        switch( i_type & VLC_VAR_TYPE )
        {
        case VLC_VAR_VARIABLE:
          menu->Append( MenuDummy_Event,
gbazin's avatar
 
gbazin committed
555
                        wxU(text_list.p_list->p_values[i].psz_string ?
gbazin's avatar
 
gbazin committed
556
                        text_list.p_list->p_values[i].psz_string :
gbazin's avatar
 
gbazin committed
557
                        val_list.p_list->p_values[i].psz_string),
gbazin's avatar
 
gbazin committed
558 559
                        CreateChoicesMenu(
                            val_list.p_list->p_values[i].psz_string,
gbazin's avatar
 
gbazin committed
560
                            p_object ), wxT("") );
gbazin's avatar
 
gbazin committed
561 562 563 564 565 566
          break;

        case VLC_VAR_STRING:
          another_val.psz_string =
              strdup(val_list.p_list->p_values[i].psz_string);
          menuitem =
gbazin's avatar
 
gbazin committed
567
              new wxMenuItemExt( menu, ++i_item_id,
gbazin's avatar
 
gbazin committed
568
                                 wxU(text_list.p_list->p_values[i].psz_string ?
gbazin's avatar
 
gbazin committed
569
                                 text_list.p_list->p_values[i].psz_string :
gbazin's avatar
 
gbazin committed
570 571 572 573
                                 another_val.psz_string), wxT(""),
                                 i_type & VLC_VAR_ISCOMMAND ?
                                   wxITEM_NORMAL : wxITEM_RADIO,
                                 strdup(psz_var),
gbazin's avatar
 
gbazin committed
574 575 576 577 578 579 580 581 582 583 584
                                 p_object->i_object_id, another_val, i_type );

          menu->Append( menuitem );

          if( !strcmp( val.psz_string,
                       val_list.p_list->p_values[i].psz_string ) )
              menu->Check( i_item_id, TRUE );
          break;

        case VLC_VAR_INTEGER:
          menuitem =
gbazin's avatar
 
gbazin committed
585
              new wxMenuItemExt( menu, ++i_item_id,
gbazin's avatar
 
gbazin committed
586
                                 text_list.p_list->p_values[i].psz_string ?
gbazin's avatar
 
gbazin committed
587 588
                                 (wxString)wxU(
                                   text_list.p_list->p_values[i].psz_string) :
gbazin's avatar
 
gbazin committed
589
                                 wxString::Format(wxT("%d"),
gbazin's avatar
 
gbazin committed
590 591 592 593
                                 val_list.p_list->p_values[i].i_int), wxT(""),
                                 i_type & VLC_VAR_ISCOMMAND ?
                                   wxITEM_NORMAL : wxITEM_RADIO,
                                 strdup(psz_var),
gbazin's avatar
 
gbazin committed
594 595 596 597 598
                                 p_object->i_object_id,
                                 val_list.p_list->p_values[i], i_type );

          menu->Append( menuitem );

gbazin's avatar
 
gbazin committed
599
          if( val_list.p_list->p_values[i].i_int == val.i_int )
gbazin's avatar
 
gbazin committed
600 601 602 603 604 605 606 607 608
              menu->Check( i_item_id, TRUE );
          break;

        default:
          break;
        }
    }

    /* clean up everything */
gbazin's avatar
 
gbazin committed
609
    if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
gbazin's avatar
 
gbazin committed
610 611 612 613 614
    var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );

    return menu;
}

gbazin's avatar
 
gbazin committed
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
void Menu::OnShowDialog( wxCommandEvent& event )
{
    if( p_intf->p_sys->pf_show_dialog )
    {
        int i_id;

        switch( event.GetId() )
        {
        case OpenFileSimple_Event:
            i_id = INTF_DIALOG_FILE_SIMPLE;
            break;
        case OpenFile_Event:
            i_id = INTF_DIALOG_FILE;
            break;
        case OpenDisc_Event:
            i_id = INTF_DIALOG_DISC;
            break;
        case OpenNet_Event:
            i_id = INTF_DIALOG_NET;
            break;
        default:
            i_id = INTF_DIALOG_FILE;
            break;

        }

gbazin's avatar
 
gbazin committed
641
        p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
gbazin's avatar
 
gbazin committed
642 643 644
    }
}

gbazin's avatar
 
gbazin committed
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
/*****************************************************************************
 * A small helper class which intercepts all popup menu events
 *****************************************************************************/
MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
                                Interface *_p_main_interface )
{
    /* Initializations */
    p_intf = _p_intf;
    p_main_interface = _p_main_interface;
}

MenuEvtHandler::~MenuEvtHandler()
{
}

gbazin's avatar
 
gbazin committed
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
void MenuEvtHandler::OnShowDialog( wxCommandEvent& event )
{
    if( p_intf->p_sys->pf_show_dialog )
    {
        int i_id;

        switch( event.GetId() )
        {
        case OpenFileSimple_Event:
            i_id = INTF_DIALOG_FILE_SIMPLE;
            break;
        case OpenFile_Event:
            i_id = INTF_DIALOG_FILE;
            break;
        case OpenDisc_Event:
            i_id = INTF_DIALOG_DISC;
            break;
        case OpenNet_Event:
            i_id = INTF_DIALOG_NET;
            break;
        default:
            i_id = INTF_DIALOG_FILE;
            break;

        }

gbazin's avatar
 
gbazin committed
686
        p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
gbazin's avatar
 
gbazin committed
687 688 689
    }
}

gbazin's avatar
 
gbazin committed
690 691
void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
{
gbazin's avatar
 
gbazin committed
692
    wxMenuItem *p_menuitem = NULL;
gbazin's avatar
 
gbazin committed
693 694 695 696 697 698 699 700

    /* Check if this is an auto generated menu item */
    if( event.GetId() < FirstAutoGenerated_Event )
    {
        event.Skip();
        return;
    }

gbazin's avatar
 
gbazin committed
701 702
    if( !p_main_interface ||
        (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
gbazin's avatar
 
gbazin committed
703 704
        == NULL )
    {
gbazin's avatar
 
gbazin committed
705
        if( p_intf->p_sys->p_popup_menu )
gbazin's avatar
 
gbazin committed
706 707
        {
            p_menuitem = 
gbazin's avatar
 
gbazin committed
708
                p_intf->p_sys->p_popup_menu->FindItem( event.GetId() );
gbazin's avatar
 
gbazin committed
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
        }
    }

    if( p_menuitem )
    {
        wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
        vlc_object_t *p_object;

        p_object = (vlc_object_t *)vlc_object_get( p_intf,
                                       p_menuitemext->i_object_id );
        if( p_object == NULL ) return;

        var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );

        vlc_object_release( p_object );
    }
    else
        event.Skip();
}

/*****************************************************************************
 * A small helper class which encapsulate wxMenuitem with some other useful
 * things.
 *****************************************************************************/
wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
    const wxString& helpString, wxItemKind kind,
    char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
    wxMenuItem( parentMenu, id, text, helpString, kind )
{
    /* Initializations */
    psz_var = _psz_var;
    i_val_type = _i_val_type;
    i_object_id = _i_object_id;
    val = _val;
};

wxMenuItemExt::~wxMenuItemExt()
{
    if( psz_var ) free( psz_var );
    if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
        && val.psz_string ) free( val.psz_string );
};