playlist.cpp 53.3 KB
Newer Older
1
/*****************************************************************************
gbazin's avatar
 
gbazin committed
2
 * playlist.cpp : wxWindows plugin for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2000-2005 the VideoLAN team
zorglub's avatar
zorglub committed
5
 * $Id$
6
 *
7 8
 * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
 *          Clément Stenac <zorglub@videolan.org>
9
 *
10
 * This program is free software; you can redistribute it and/OR MODIFy
11 12 13
 * 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.
gbazin's avatar
 
gbazin committed
14
 *
15 16 17 18 19 20 21
 * 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
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29
#include "dialogs/playlist.hpp"
#include "dialogs/iteminfo.hpp"
30

zorglub's avatar
zorglub committed
31
#include "bitmaps/shuffle.xpm"
32 33 34
#include "bitmaps/repeat.xpm"
#include "bitmaps/loop.xpm"

35
#include "bitmaps/type_unknown.xpm"
zorglub's avatar
zorglub committed
36 37
#include "bitmaps/type_afile.xpm"
#include "bitmaps/type_vfile.xpm"
38 39 40
#include "bitmaps/type_net.xpm"
#include "bitmaps/type_card.xpm"
#include "bitmaps/type_disc.xpm"
zorglub's avatar
zorglub committed
41
#include "bitmaps/type_cdda.xpm"
42 43
#include "bitmaps/type_directory.xpm"
#include "bitmaps/type_playlist.xpm"
zorglub's avatar
zorglub committed
44
#include "bitmaps/type_node.xpm"
45 46

#include <wx/dynarray.h>
47
#include <wx/imaglist.h>
zorglub's avatar
zorglub committed
48

49
#include <vlc_meta.h>
zorglub's avatar
zorglub committed
50
#include "vlc_charset.h"
51

zorglub's avatar
zorglub committed
52
#define HELP_SHUFFLE N_( "Shuffle" )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
53 54
#define HELP_LOOP N_( "Repeat All" )
#define HELP_REPEAT N_( "Repeat One" )
zorglub's avatar
zorglub committed
55

56
namespace wxvlc {
gbazin's avatar
 
gbazin committed
57
/* Callback prototype */
58 59 60 61 62 63
static int PlaylistChanged( vlc_object_t *, const char *,
                            vlc_value_t, vlc_value_t, void * );
static int PlaylistNext( vlc_object_t *, const char *,
                         vlc_value_t, vlc_value_t, void * );
static int ItemChanged( vlc_object_t *, const char *,
                        vlc_value_t, vlc_value_t, void * );
64
static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
65
                         vlc_value_t oval, vlc_value_t nval, void *param );
66
static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
67
                        vlc_value_t oval, vlc_value_t nval, void *param );
68

69 70 71 72 73 74 75 76
/*****************************************************************************
 * Event Table.
 *****************************************************************************/

/* IDs for the controls and the menu commands */
enum
{
    /* menu items */
gbazin's avatar
 
gbazin committed
77
    AddFile_Event = 1,
zorglub's avatar
zorglub committed
78
    AddDir_Event,
gbazin's avatar
 
gbazin committed
79
    AddMRL_Event,
80
    Close_Event,
gbazin's avatar
 
gbazin committed
81 82
    Open_Event,
    Save_Event,
83

zorglub's avatar
zorglub committed
84 85
    SortTitle_Event,
    RSortTitle_Event,
zorglub's avatar
zorglub committed
86
    Randomize_Event,
zorglub's avatar
zorglub committed
87

88
    DeleteSelection_Event,
89 90
    Random_Event,
    Loop_Event,
91
    Repeat_Event,
92

zorglub's avatar
zorglub committed
93
    PopupPlay_Event,
94
    PopupPlayThis_Event,
95
    PopupPreparse_Event,
96
    PopupSort_Event,
zorglub's avatar
zorglub committed
97 98
    PopupDel_Event,
    PopupInfo_Event,
99
    PopupAddNode_Event,
zorglub's avatar
zorglub committed
100

zorglub's avatar
zorglub committed
101 102 103
    SearchText_Event,
    Search_Event,

104
    /* controls */
105
    Source_Event,
106
    TreeCtrl_Event,
zorglub's avatar
zorglub committed
107 108

    Browse_Event,  /* For export playlist */
109 110

    /* custom events */
111
    UpdateItem_Event,
112
    AppendItem_Event,
113
    RemoveItem_Event,
114

115 116
    MenuDummy_Event = wxID_HIGHEST + 999,

117
    FirstView_Event = wxID_HIGHEST + 1000,
118
    CategoryView_Event, OneLevelView_Event,
119 120 121 122
    LastView_Event = wxID_HIGHEST + 1100,

    FirstSD_Event = wxID_HIGHEST + 2000,
    LastSD_Event = wxID_HIGHEST + 2100,
123 124
};

125 126
DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );

127
BEGIN_EVENT_TABLE(Playlist, wxFrame)
128 129
    EVT_SIZE(Playlist::OnSize)

130
    /* Menu events */
gbazin's avatar
 
gbazin committed
131
    EVT_MENU(AddFile_Event, Playlist::OnAddFile)
zorglub's avatar
zorglub committed
132
    EVT_MENU(AddDir_Event, Playlist::OnAddDir)
gbazin's avatar
 
gbazin committed
133
    EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
134
    EVT_MENU(Close_Event, Playlist::OnMenuClose)
gbazin's avatar
 
gbazin committed
135 136
    EVT_MENU(Open_Event, Playlist::OnOpen)
    EVT_MENU(Save_Event, Playlist::OnSave)
zorglub's avatar
zorglub committed
137 138 139 140

    EVT_MENU(SortTitle_Event, Playlist::OnSort)
    EVT_MENU(RSortTitle_Event, Playlist::OnSort)

zorglub's avatar
zorglub committed
141 142
    EVT_MENU(Randomize_Event, Playlist::OnSort)

143
    EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
144

145 146 147
    EVT_MENU_OPEN( Playlist::OnMenuOpen )
    EVT_MENU( -1, Playlist::OnMenuEvent )

148 149 150
    EVT_TOOL(Random_Event, Playlist::OnRandom)
    EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
    EVT_TOOL(Loop_Event, Playlist::OnLoop)
151

zorglub's avatar
zorglub committed
152 153
    /* Popup events */
    EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
154
    EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
155
    EVT_MENU( PopupPreparse_Event, Playlist::OnPopupPreparse)
156
    EVT_MENU( PopupSort_Event, Playlist::OnPopupSort)
zorglub's avatar
zorglub committed
157 158
    EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
    EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
159
    EVT_MENU( PopupAddNode_Event, Playlist::OnPopupAddNode)
zorglub's avatar
zorglub committed
160

161 162
    /* Source selector */
    EVT_LIST_ITEM_SELECTED( Source_Event, Playlist::OnSourceSelected )
163 164
    /* Tree control events */
    EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
165
    EVT_TREE_KEY_DOWN( -1, Playlist::OnKeyDown )
166 167
    EVT_TREE_BEGIN_DRAG( TreeCtrl_Event, Playlist::OnDragItemBegin )
    EVT_TREE_END_DRAG( TreeCtrl_Event, Playlist::OnDragItemEnd )
168 169

    EVT_CONTEXT_MENU( Playlist::OnPopup )
170 171

    /* Button events */
zorglub's avatar
zorglub committed
172
    EVT_BUTTON( Search_Event, Playlist::OnSearch)
gbazin's avatar
 
gbazin committed
173
    EVT_BUTTON( Save_Event, Playlist::OnSave)
zorglub's avatar
zorglub committed
174

175 176
    /*EVT_TEXT( SearchText_Event, Playlist::OnSearchTextChange )*/
    EVT_TEXT_ENTER( SearchText_Event, Playlist::OnSearch )
zorglub's avatar
zorglub committed
177

178 179 180
    /* Custom events */
    EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)

181 182 183 184 185
    /* Special events : we don't want to destroy the window when the user
     * clicks on (X) */
    EVT_CLOSE(Playlist::OnClose)
END_EVENT_TABLE()

186 187 188 189 190 191
/*****************************************************************************
 * PlaylistItem class
 ****************************************************************************/
class PlaylistItem : public wxTreeItemData
{
public:
192
    PlaylistItem( playlist_item_t *p_item ) : wxTreeItemData()
193
    {
194 195
        i_id = p_item->i_id;
        i_input_id = p_item->p_input->i_id;
196 197
    }
protected:
198
    int i_input_id;
199
    int i_id;
200
friend class Playlist;
201
friend class PlaylistFileDropTarget;
202
};
zorglub's avatar
zorglub committed
203

204 205 206
/*****************************************************************************
 * Constructor.
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
207 208
Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
    wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
209
             wxSize(500,300), wxDEFAULT_FRAME_STYLE )
210
{
gbazin's avatar
 
gbazin committed
211 212
    vlc_value_t val;

213 214
    /* Initializations */
    p_intf = _p_intf;
215
    pp_sds = NULL;
gbazin's avatar
 
gbazin committed
216
    i_update_counter = 0;
zorglub's avatar
zorglub committed
217
    i_sort_mode = MODE_NONE;
gbazin's avatar
 
gbazin committed
218
    b_need_update = VLC_FALSE;
219
    i_items_to_append = 0;
220 221
    p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                                FIND_ANYWHERE );
222
    if( p_playlist == NULL ) return;
223

gbazin's avatar
 
gbazin committed
224
    SetIcon( *p_intf->p_sys->p_icon );
225

226
    p_view_menu = NULL;
227
    p_sd_menu = SDMenu();
228

229
    p_current_viewroot = p_playlist->p_root_category;
230
    p_current_treeroot = NULL;
231

232 233
    i_title_sorted = 0;
    i_group_sorted = 0;
zorglub's avatar
zorglub committed
234
    i_duration_sorted = 0;
235

hartman's avatar
hartman committed
236 237 238
    var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
239

240 241
    /* Create our "Manage" menu */
    wxMenu *manage_menu = new wxMenu;
zorglub's avatar
zorglub committed
242 243
    manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
    manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
zorglub's avatar
zorglub committed
244
    manage_menu->Append( AddMRL_Event, wxU(_("&Add URL...")) );
zorglub's avatar
zorglub committed
245
    manage_menu->AppendSeparator();
246
    manage_menu->Append( MenuDummy_Event, wxU(_("Services Discovery")),
247 248
                         p_sd_menu );
    manage_menu->AppendSeparator();
gbazin's avatar
 
gbazin committed
249 250
    manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
    manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
251
    manage_menu->AppendSeparator();
gbazin's avatar
 
gbazin committed
252
    manage_menu->Append( Close_Event, wxU(_("&Close")) );
253

zorglub's avatar
zorglub committed
254 255
    /* Create our "Sort" menu */
    wxMenu *sort_menu = new wxMenu;
256 257
    sort_menu->Append( SortTitle_Event, wxU(_("Sort by &Title")) );
    sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse Sort by Title")) );
zorglub's avatar
zorglub committed
258
    sort_menu->AppendSeparator();
zorglub's avatar
zorglub committed
259
    sort_menu->Append( Randomize_Event, wxU(_("&Shuffle")) );
zorglub's avatar
zorglub committed
260

261 262
    /* Create our "Selection" menu */
    wxMenu *selection_menu = new wxMenu;
hartman's avatar
hartman committed
263
    selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
264

265 266
    /* Create our "View" menu */
    ViewMenu();
zorglub's avatar
zorglub committed
267

268
    /* Append the freshly created menus to the menu bar */
269
    wxMenuBar *menubar = new wxMenuBar();
gbazin's avatar
 
gbazin committed
270
    menubar->Append( manage_menu, wxU(_("&Manage")) );
zorglub's avatar
zorglub committed
271
    menubar->Append( sort_menu, wxU(_("S&ort")) );
gbazin's avatar
 
gbazin committed
272
    menubar->Append( selection_menu, wxU(_("&Selection")) );
273
    menubar->Append( p_view_menu, wxU(_("&View items") ) );
274 275 276 277

    /* Attach the menu bar to the frame */
    SetMenuBar( menubar );

zorglub's avatar
zorglub committed
278
    /* Create the popup menu */
279 280
    node_popup = new wxMenu;
    node_popup->Append( PopupPlay_Event, wxU(_("Play")) );
281
    node_popup->Append( PopupPlayThis_Event, wxU(_("Play this Branch")) );
282
    node_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
283
    node_popup->Append( PopupSort_Event, wxU(_("Sort this Branch")) );
284 285
    node_popup->Append( PopupDel_Event, wxU(_("Delete")) );
    node_popup->Append( PopupInfo_Event, wxU(_("Info")) );
286
    node_popup->Append( PopupAddNode_Event, wxU(_("Add Node")) );
287 288 289 290 291 292

    item_popup = new wxMenu;
    item_popup->Append( PopupPlay_Event, wxU(_("Play")) );
    item_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
    item_popup->Append( PopupDel_Event, wxU(_("Delete")) );
    item_popup->Append( PopupInfo_Event, wxU(_("Info")) );
zorglub's avatar
zorglub committed
293

gbazin's avatar
 
gbazin committed
294 295 296 297
    /* Create a panel to put everything in */
    wxPanel *playlist_panel = new wxPanel( this, -1 );
    playlist_panel->SetAutoLayout( TRUE );

298 299
    /* Create the toolbar */
    wxToolBar *toolbar =
300
        CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT );
301 302 303 304 305

    /* Create the random tool */
    toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
                       wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
                       wxU(_(HELP_SHUFFLE) ) );
gbazin's avatar
 
gbazin committed
306
    var_Get( p_intf, "random", &val );
307
    toolbar->ToggleTool( Random_Event, val.b_bool );
308

309 310 311 312
    /* Create the Loop tool */
    toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
                      wxBitmap( loop_xpm), wxITEM_CHECK,
                      wxU(_(HELP_LOOP )  ) );
313
    var_Get( p_intf, "loop", &val );
314
    toolbar->ToggleTool( Loop_Event, val.b_bool );
315

316
    /* Create the Repeat one checkbox */
317 318 319
    toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
                      wxBitmap( repeat_xpm), wxITEM_CHECK,
                      wxU(_(HELP_REPEAT )  ) );
320
    var_Get( p_intf, "repeat", &val );
321
    toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
322

zorglub's avatar
zorglub committed
323
    /* Create the Search Textbox */
324 325 326
    search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
                                  wxDefaultPosition, wxSize(100, -1),
                                  wxTE_PROCESS_ENTER);
zorglub's avatar
zorglub committed
327 328

    /* Create the search button */
329
    search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
330

331 332
    toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
                         wxSize(16, 16), wxBORDER_NONE ) );
333
    toolbar->AddControl( search_text );
334 335
    toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
                         wxSize(5, 5), wxBORDER_NONE ) );
336
    toolbar->AddControl( search_button );
337
    search_button->SetDefault();
338
    toolbar->Realize();
zorglub's avatar
zorglub committed
339

340
    /* Create teh source selector */
341 342 343
    source_sel = new wxListView( playlist_panel, Source_Event,
                                 wxDefaultPosition, wxDefaultSize,
                                 wxLC_AUTOARRANGE|wxLC_SINGLE_SEL );
344

345 346
    /* Create the tree */
    treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
347
                               wxDefaultPosition, wxDefaultSize,
348 349 350 351 352 353
                               wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
                               wxTR_NO_LINES |
                               wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
                               wxTR_MULTIPLE | wxTR_EXTENDED );

    /* Create image list */
354
    wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
zorglub's avatar
zorglub committed
355 356 357

    /* FIXME: absolutely needs to be in the right order FIXME */
    p_images->Add( wxIcon( type_unknown_xpm ) );
zorglub's avatar
zorglub committed
358 359
    p_images->Add( wxIcon( type_afile_xpm ) );
    p_images->Add( wxIcon( type_vfile_xpm ) );
360
    p_images->Add( wxIcon( type_directory_xpm ) );
zorglub's avatar
zorglub committed
361
    p_images->Add( wxIcon( type_disc_xpm ) );
zorglub's avatar
zorglub committed
362
    p_images->Add( wxIcon( type_cdda_xpm ) );
363
    p_images->Add( wxIcon( type_card_xpm ) );
zorglub's avatar
zorglub committed
364 365
    p_images->Add( wxIcon( type_net_xpm ) );
    p_images->Add( wxIcon( type_playlist_xpm ) );
zorglub's avatar
zorglub committed
366
    p_images->Add( wxIcon( type_node_xpm ) );
367 368
    treectrl->AssignImageList( p_images );

369
    /* Reduce font size */
370
    wxFont font= treectrl->GetFont();
371
    font.SetPointSize(9);
372
    treectrl->SetFont( font );
zorglub's avatar
zorglub committed
373

374 375
    wxBoxSizer *panel_sizer = new wxBoxSizer( wxHORIZONTAL );
    panel_sizer->Add( source_sel, 0, wxALL | wxEXPAND, 5 );
376
    panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
gbazin's avatar
 
gbazin committed
377
    panel_sizer->Layout();
zorglub's avatar
zorglub committed
378

gbazin's avatar
 
gbazin committed
379
    playlist_panel->SetSizerAndFit( panel_sizer );
380

zorglub's avatar
zorglub committed
381 382 383 384
    int pi_widths[1] =  { -1 };
    statusbar = CreateStatusBar( 1 );
    statusbar->SetStatusWidths( 1, pi_widths );

385
#if wxUSE_DRAG_AND_DROP
gbazin's avatar
 
gbazin committed
386
    /* Associate drop targets with the playlist */
387
    SetDropTarget( new PlaylistFileDropTarget( this ) );
388 389
    menubar->SetDropTarget( new PlaylistFileDropTarget( this ) );
    toolbar->SetDropTarget( new PlaylistFileDropTarget( this ) );
gbazin's avatar
 
gbazin committed
390
#endif
gbazin's avatar
 
gbazin committed
391

392
    i_saved_id = -1;
393
    i_saved_input_id = -1;
zorglub's avatar
zorglub committed
394 395

    /* Some global changes happened -> Rebuild all */
gbazin's avatar
 
gbazin committed
396
    var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
zorglub's avatar
zorglub committed
397 398 399 400 401 402 403

    /* We went to the next item */
    var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );

    /* One item has been updated */
    var_AddCallback( p_playlist, "item-change", ItemChanged, this );

404
    var_AddCallback( p_playlist, "item-append", ItemAppended, this );
405
    var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
406

407
    /* Update the playlist */
408
    p_current_treeroot = p_playlist->p_local_category;
409
    Rebuild( VLC_TRUE );
410 411 412 413
}

Playlist::~Playlist()
{
414 415 416 417 418 419
    if( pp_sds != NULL )
    {
        char **pp_sd = pp_sds;
        for( ; *pp_sd; pp_sd++ ) free( *pp_sd );
        free( pp_sds );
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
420

421
    if( p_playlist == NULL ) return;
gbazin's avatar
 
gbazin committed
422

hartman's avatar
hartman committed
423 424
    var_DelCallback( p_playlist, "item-change", ItemChanged, this );
    var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
gbazin's avatar
 
gbazin committed
425
    var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
426
    var_DelCallback( p_playlist, "item-append", ItemAppended, this );
427
    var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
gbazin's avatar
 
gbazin committed
428
    vlc_object_release( p_playlist );
429 430
}

zorglub's avatar
zorglub committed
431
/**********************************************************************
zorglub's avatar
zorglub committed
432
 * Update functions
zorglub's avatar
zorglub committed
433
 **********************************************************************/
zorglub's avatar
zorglub committed
434 435

/* Update a node */
436
void Playlist::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
437
{
438
    wxTreeItemIdValue cookie;
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    wxTreeItemId child;
    for( int i = 0; i< p_node->i_children ; i++ )
    {
        if( i == 0 )
        {
            child = treectrl->GetFirstChild( node, cookie);
        }
        else
        {
            child = treectrl->GetNextChild( node, cookie );
        }

        if( !child.IsOk() )
        {
            /* Not enough children */
454
            CreateNode( p_node->pp_children[i], node );
455 456 457 458
            /* Keep the tree pointer up to date */
            child = treectrl->GetNextChild( node, cookie );
        }
    }
459
    treectrl->SetItemImage( node, p_node->p_input->i_type );
460 461

}
zorglub's avatar
zorglub committed
462

463
/* Creates the node p_node as last child of parent */
464
void Playlist::CreateNode( playlist_item_t *p_node, wxTreeItemId parent )
465
{
466
    wxTreeItemId node =
467
        treectrl->AppendItem( parent, wxL2U( p_node->p_input->psz_name ),
468
                              -1,-1, new PlaylistItem( p_node ) );
469
    treectrl->SetItemImage( node, p_node->p_input->i_type );
470

471
    UpdateNodeChildren( p_node, node );
zorglub's avatar
zorglub committed
472 473
}

zorglub's avatar
zorglub committed
474
/* Update all children (recursively) of this node */
475
void Playlist::UpdateNodeChildren( playlist_item_t *p_node,
zorglub's avatar
zorglub committed
476 477
                                   wxTreeItemId node )
{
478 479 480 481 482
    for( int i = 0; i< p_node->i_children ; i++ )
    {
        /* Append the item */
        if( p_node->pp_children[i]->i_children == -1 )
        {
483 484 485 486 487
            if( !(p_node->pp_children[i]->i_flags & PLAYLIST_DBL_FLAG) )
            {
                wxTreeItemId item =
                    treectrl->AppendItem( node,
                    wxL2U( p_node->pp_children[i]->p_input->psz_name ), -1,-1,
488
                           new PlaylistItem( p_node->pp_children[i]) );
489

490 491
                UpdateTreeItem( item );
            }
492 493 494
        }
        else
        {
495
            CreateNode( p_node->pp_children[i], node );
496 497 498 499
        }
    }
}

zorglub's avatar
zorglub committed
500
/* Update an item in the tree */
501
void Playlist::UpdateTreeItem( wxTreeItemId item )
zorglub's avatar
zorglub committed
502
{
503
    LockPlaylist( p_intf->p_sys, p_playlist );
504 505
    if( ! item.IsOk() ) return;

506 507
    wxTreeItemData *p_data = treectrl->GetItemData( item );
    if( !p_data ) return;
dionoea's avatar
dionoea committed
508

509
    playlist_item_t *p_item = playlist_ItemGetById( p_playlist,
510
                                    ((PlaylistItem *)p_data)->i_id, VLC_TRUE );
511 512 513 514 515
    if( !p_item )
    {
        UnlockPlaylist( p_intf->p_sys, p_playlist );
        return;
    }
516

zorglub's avatar
zorglub committed
517
    wxString msg;
518
    wxString duration = wxU( "" );
519

520 521
    char *psz_artist = input_item_GetArtist( p_item->p_input );
    char *psz_name = input_item_GetName( p_item->p_input );
522

zorglub's avatar
zorglub committed
523
    char psz_duration[MSTRTIME_MAX_SIZE];
524
    mtime_t dur = input_item_GetDuration( p_item->p_input );
525

zorglub's avatar
zorglub committed
526
    if( dur != -1 )
527
    {
zorglub's avatar
zorglub committed
528
        secstotimestr( psz_duration, dur/1000000 );
529
        duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
530
                         wxU( " )" ) );
531
    }
532

533
    if( !psz_artist || !strcmp( psz_artist, "" ) || p_item->p_input->b_fixed_name == VLC_TRUE )
534
    {
535
        msg = wxString( wxU( psz_name ) ) + duration;
536
    }
zorglub's avatar
zorglub committed
537 538
    else
    {
zorglub's avatar
zorglub committed
539
        msg = wxString(wxU( psz_artist )) + wxT(" - ") +
540
              wxString(wxU(psz_name)) + duration;
zorglub's avatar
zorglub committed
541
    }
542 543
    if( psz_artist )
        free( psz_artist );
544
    free( psz_name );
zorglub's avatar
zorglub committed
545
    treectrl->SetItemText( item , msg );
546
    treectrl->SetItemImage( item, p_item->p_input->i_type );
547

zorglub's avatar
zorglub committed
548
    if( p_playlist->status.p_item == p_item )
549
    {
550
        treectrl->SetItemBold( item, true );
551 552 553
        while( treectrl->GetItemParent( item ).IsOk() )
        {
            item = treectrl->GetItemParent( item );
554 555 556
            if( ! (item == treectrl->GetRootItem() &&
                treectrl->HasFlag( wxTR_HIDE_ROOT ) ) )
                treectrl->Expand( item );
557
        }
zorglub's avatar
zorglub committed
558 559 560 561
    }
    else
    {
        treectrl->SetItemBold( item, false );
562
    }
563
    UnlockPlaylist( p_intf->p_sys, p_playlist );
564 565
}

566
/* Process a AppendIt em request */
567 568 569
void Playlist::AppendItem( wxCommandEvent& event )
{
    playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
zorglub's avatar
zorglub committed
570
    playlist_item_t *p_item = NULL;
571
    wxTreeItemId item, node;
572

573
    i_items_to_append--;
574

575 576 577
    /* No need to do anything if the playlist is going to be rebuilt */
    if( b_need_update ) return;

zorglub's avatar
zorglub committed
578
    node = FindItem( treectrl->GetRootItem(), p_add->i_node );
579
    if( !node.IsOk() ) goto update;
580

581
    p_item = playlist_ItemGetById( p_playlist, p_add->i_item, VLC_TRUE );
582
    if( !p_item ) goto update;
583
    if( (p_item->i_flags & PLAYLIST_DBL_FLAG ) ) goto update;
584 585 586

    item = FindItem( treectrl->GetRootItem(), p_add->i_item );
    if( item.IsOk() ) goto update;
zorglub's avatar
zorglub committed
587

zorglub's avatar
zorglub committed
588
    item = treectrl->AppendItem( node,
589
                                 wxL2U( p_item->p_input->psz_name ), -1,-1,
zorglub's avatar
zorglub committed
590
                                 new PlaylistItem( p_item ) );
591
    treectrl->SetItemImage( item, p_item->p_input->i_type );
592

zorglub's avatar
zorglub committed
593
    if( item.IsOk() && p_item->i_children == -1 )
594
    {
595
        UpdateTreeItem( item );
596
    }
zorglub's avatar
zorglub committed
597

zorglub's avatar
zorglub committed
598 599
update:
    int i_count = CountItems( treectrl->GetRootItem());
600 601
    statusbar->SetStatusText( wxString::Format( wxU(_(
                                  "%i items in playlist" ) ), i_count ) );
zorglub's avatar
zorglub committed
602
    return;
603 604
}

zorglub's avatar
zorglub committed
605
/* Process a updateitem request */
zorglub's avatar
zorglub committed
606 607
void Playlist::UpdateItem( int i )
{
gbazin's avatar
 
gbazin committed
608
    if( i < 0 ) return; /* Sanity check */
609
    wxTreeItemId item = FindItemByInput( treectrl->GetRootItem(), i );
zorglub's avatar
zorglub committed
610 611
    if( item.IsOk() )
    {
612
        UpdateTreeItem( item );
zorglub's avatar
zorglub committed
613
    }
614 615
}

616 617 618
void Playlist::RemoveItem( int i )
{
    if( i <= 0 ) return; /* Sanity check */
619
    if( i == i_saved_id ) i_saved_id = -1;
620

621 622 623 624 625
    /* Hack: always invalidate input item cache */
    i_saved_input_id = -1;

    /// \todo Check if it is in the source selector */

626 627 628 629 630
    wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );

    if( item.IsOk() )
    {
        treectrl->Delete( item );
631
    }
632
}
633 634


zorglub's avatar
zorglub committed
635
/**********************************************************************
636
 * Search functions (internal)
zorglub's avatar
zorglub committed
637
 **********************************************************************/
zorglub's avatar
zorglub committed
638

639 640
/* Find a wxItem from a playlist id */
wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
641 642 643 644 645 646 647 648 649 650
{
    return FindItemInner( root, i_id, false );
}

wxTreeItemId Playlist::FindItemByInput( wxTreeItemId root, int i_input_id )
{
    return FindItemInner( root, i_input_id, true );
}

wxTreeItemId Playlist::FindItemInner( wxTreeItemId root, int i_id, bool b_byinput )
651
{
652
    wxTreeItemIdValue cookie;
653 654 655 656 657 658 659 660 661
    PlaylistItem *p_wxcurrent;
    wxTreeItemId search;
    wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
    wxTreeItemId child;

    p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );

    if( i_id < 0 )
    {
662
        wxTreeItemId dummy; dummy.Unset(); return dummy;
663
    }
664 665 666
    if( b_byinput && i_saved_input_id == i_id )
        return saved_input_tree_item;
    if( !b_byinput && i_saved_id == i_id)
667
        return saved_tree_item;
668 669

    if( !p_wxcurrent )
670
    {
671
        wxTreeItemId dummy; dummy.Unset(); return dummy;
dionoea's avatar
dionoea committed
672
    }
673

674
    if( !b_byinput && p_wxcurrent->i_id == i_id  )
675
    {
676 677
        i_saved_id = i_id;
        saved_tree_item = root;
678
        return root;
679
    }
680 681 682 683 684 685
    if( b_byinput && p_wxcurrent->i_input_id == i_id )
    {
        i_saved_input_id = i_id;
        saved_input_tree_item = root;
        return root;
    }
686

zorglub's avatar
zorglub committed
687
    while( item.IsOk() )
688
    {