1 /*****************************************************************************
2 * playlist_manager.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8 * Clément Stenac <zorglub@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/OR MODIFy
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include "playlist_manager.hpp"
30 #include "interface.hpp"
32 #include "bitmaps/type_unknown.xpm"
33 #include "bitmaps/type_afile.xpm"
34 #include "bitmaps/type_vfile.xpm"
35 #include "bitmaps/type_net.xpm"
36 #include "bitmaps/type_card.xpm"
37 #include "bitmaps/type_disc.xpm"
38 #include "bitmaps/type_cdda.xpm"
39 #include "bitmaps/type_directory.xpm"
40 #include "bitmaps/type_playlist.xpm"
41 #include "bitmaps/type_node.xpm"
43 #include <wx/dynarray.h>
44 #include <wx/imaglist.h>
48 /* Callback prototype */
49 static int PlaylistChanged( vlc_object_t *, const char *,
50 vlc_value_t, vlc_value_t, void * );
51 static int PlaylistNext( vlc_object_t *, const char *,
52 vlc_value_t, vlc_value_t, void * );
53 static int ItemChanged( vlc_object_t *, const char *,
54 vlc_value_t, vlc_value_t, void * );
55 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
56 vlc_value_t oval, vlc_value_t nval, void *param );
57 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
58 vlc_value_t oval, vlc_value_t nval, void *param );
60 /*****************************************************************************
62 *****************************************************************************/
64 /* IDs for the controls and the menu commands */
74 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
76 BEGIN_EVENT_TABLE(PlaylistManager, wxPanel)
77 /* Tree control events */
78 EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, PlaylistManager::OnActivateItem )
81 EVT_COMMAND(-1, wxEVT_PLAYLIST, PlaylistManager::OnPlaylistEvent)
84 /*****************************************************************************
86 ****************************************************************************/
87 class PlaylistItem : public wxTreeItemData
90 PlaylistItem( playlist_item_t *p_item ) : wxTreeItemData()
93 i_input_id = p_item->p_input->i_id;
98 friend class PlaylistManager;
101 /*****************************************************************************
103 *****************************************************************************/
104 PlaylistManager::PlaylistManager( intf_thread_t *_p_intf, wxWindow *p_parent ):
105 wxPanel( p_parent, -1, wxDefaultPosition, wxSize(0,0) )
107 /* Initializations */
109 b_need_update = VLC_FALSE;
110 i_items_to_append = 0;
111 i_cached_item_id = -1;
112 i_update_counter = 0;
114 p_playlist = (playlist_t *)
115 vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
116 if( p_playlist == NULL ) return;
118 var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
119 var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
120 var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
122 /* Create the tree */
123 treectrl = new wxTreeCtrl( this, TreeCtrl_Event,
124 wxDefaultPosition, wxDefaultSize,
125 wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
127 wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
128 wxTR_MULTIPLE | wxTR_EXTENDED );
130 /* Add everything to the panel */
131 sizer = new wxBoxSizer( wxHORIZONTAL );
133 sizer->Add( treectrl, 1, wxEXPAND );
137 /* Create image list */
138 wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
140 /* FIXME: absolutely needs to be in the right order FIXME */
141 p_images->Add( wxIcon( type_unknown_xpm ) );
142 p_images->Add( wxIcon( type_afile_xpm ) );
143 p_images->Add( wxIcon( type_vfile_xpm ) );
144 p_images->Add( wxIcon( type_directory_xpm ) );
145 p_images->Add( wxIcon( type_disc_xpm ) );
146 p_images->Add( wxIcon( type_cdda_xpm ) );
147 p_images->Add( wxIcon( type_card_xpm ) );
148 p_images->Add( wxIcon( type_net_xpm ) );
149 p_images->Add( wxIcon( type_playlist_xpm ) );
150 p_images->Add( wxIcon( type_node_xpm ) );
151 treectrl->AssignImageList( p_images );
153 /* Reduce font size */
154 wxFont font = treectrl->GetFont(); font.SetPointSize(9);
155 treectrl->SetFont( font );
157 #if wxUSE_DRAG_AND_DROP
158 /* Associate drop targets with the playlist */
159 SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
162 /* Update the playlist */
166 * We want to be notified of playlist changes
169 /* Some global changes happened -> Rebuild all */
170 var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
172 /* We went to the next item */
173 var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
175 /* One item has been updated */
176 var_AddCallback( p_playlist, "item-change", ItemChanged, this );
178 var_AddCallback( p_playlist, "item-append", ItemAppended, this );
179 var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
182 PlaylistManager::~PlaylistManager()
184 if( p_playlist == NULL ) return;
186 var_DelCallback( p_playlist, "item-change", ItemChanged, this );
187 var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
188 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
189 var_DelCallback( p_playlist, "item-append", ItemAppended, this );
190 var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
191 vlc_object_release( p_playlist );
194 /*****************************************************************************
195 * PlaylistChanged: callback triggered by the intf-change playlist variable
196 * We don't rebuild the playlist directly here because we don't want the
197 * caller to block for a too long time.
198 *****************************************************************************/
199 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
200 vlc_value_t oval, vlc_value_t nval, void *param )
202 PlaylistManager *p_playlist = (PlaylistManager *)param;
203 p_playlist->b_need_update = VLC_TRUE;
207 /*****************************************************************************
208 * Next: callback triggered by the playlist-current playlist variable
209 *****************************************************************************/
210 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
211 vlc_value_t oval, vlc_value_t nval, void *param )
213 PlaylistManager *p_playlist = (PlaylistManager *)param;
215 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
216 event.SetInt( oval.i_int );
217 p_playlist->AddPendingEvent( event );
218 event.SetInt( nval.i_int );
219 p_playlist->AddPendingEvent( event );
224 /*****************************************************************************
226 *****************************************************************************/
227 void PlaylistManager::CreateNode( playlist_item_t *p_node, wxTreeItemId parent)
230 treectrl->AppendItem( parent, wxL2U( p_node->p_input->psz_name ), -1, -1,
231 new PlaylistItem( p_node ) );
232 treectrl->SetItemImage( node, p_node->p_input->i_type );
234 UpdateNodeChildren( p_node, node );
237 void PlaylistManager::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
239 wxTreeItemIdValue cookie;
242 for( int i = 0; i < p_node->i_children ; i++ )
244 if( !i ) child = treectrl->GetFirstChild( node, cookie);
245 else child = treectrl->GetNextChild( node, cookie );
249 /* Not enough children */
250 CreateNode( p_node->pp_children[i], node );
251 /* Keep the tree pointer up to date */
252 child = treectrl->GetNextChild( node, cookie );
256 treectrl->SetItemImage( node, p_node->p_input->i_type );
260 void PlaylistManager::UpdateNodeChildren( playlist_item_t *p_node,
263 for( int i = 0; i< p_node->i_children ; i++ )
265 /* Append the item */
266 if( p_node->pp_children[i]->i_children == -1 )
269 treectrl->AppendItem( node,
270 wxL2U( p_node->pp_children[i]->p_input->psz_name ), -1,-1,
271 new PlaylistItem( p_node->pp_children[i]) );
273 UpdateTreeItem( item );
277 CreateNode( p_node->pp_children[i], node );
282 void PlaylistManager::UpdateTreeItem( wxTreeItemId item )
284 if( ! item.IsOk() ) return;
286 wxTreeItemData *p_data = treectrl->GetItemData( item );
287 if( !p_data ) return;
289 LockPlaylist( p_intf->p_sys, p_playlist );
290 playlist_item_t *p_item =
291 playlist_ItemGetById( p_playlist, ((PlaylistItem *)p_data)->i_id,
295 UnlockPlaylist( p_intf->p_sys, p_playlist );
300 wxString duration = wxU( "" );
302 const char *psz_artist = input_item_GetArtist( p_item->p_input );
308 char psz_duration[MSTRTIME_MAX_SIZE];
309 mtime_t dur = p_item->p_input->i_duration;
313 secstotimestr( psz_duration, dur/1000000 );
314 duration.Append( wxU( " ( " ) + wxString( wxU( psz_duration ) ) +
318 if( !strcmp( psz_artist, "" ) || p_item->p_input->b_fixed_name == VLC_TRUE )
320 msg = wxString( wxU( p_item->p_input->psz_name ) ) + duration;
324 msg = wxString(wxU( psz_artist )) + wxT(" - ") +
325 wxString(wxU(p_item->p_input->psz_name)) + duration;
327 treectrl->SetItemText( item , msg );
328 treectrl->SetItemImage( item, p_item->p_input->i_type );
330 if( p_playlist->status.p_item == p_item )
332 treectrl->SetItemBold( item, true );
333 while( treectrl->GetItemParent( item ).IsOk() )
335 item = treectrl->GetItemParent( item );
336 treectrl->Expand( item );
341 treectrl->SetItemBold( item, false );
343 UnlockPlaylist( p_intf->p_sys, p_playlist );
346 void PlaylistManager::AppendItem( wxCommandEvent& event )
348 playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
349 playlist_item_t *p_item = NULL;
350 wxTreeItemId item, node;
354 /* No need to do anything if the playlist is going to be rebuilt */
355 if( b_need_update ) return;
357 //if( p_add->i_view != i_current_view ) goto update;
359 node = FindItem( treectrl->GetRootItem(), p_add->i_node );
360 if( !node.IsOk() ) goto update;
362 p_item = playlist_ItemGetById( p_playlist, p_add->i_item, VLC_FALSE );
363 if( !p_item ) goto update;
365 item = FindItem( treectrl->GetRootItem(), p_add->i_item );
366 if( item.IsOk() ) goto update;
368 item = treectrl->AppendItem( node, wxL2U( p_item->p_input->psz_name ), -1,-1,
369 new PlaylistItem( p_item ) );
370 treectrl->SetItemImage( item, p_item->p_input->i_type );
372 if( item.IsOk() && p_item->i_children == -1 ) UpdateTreeItem( item );
378 void PlaylistManager::UpdateItem( int i )
380 if( i < 0 ) return; /* Sanity check */
382 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
383 if( item.IsOk() ) UpdateTreeItem( item );
386 void PlaylistManager::RemoveItem( int i )
388 if( i <= 0 ) return; /* Sanity check */
390 wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
393 treectrl->Delete( item );
395 /* Invalidate cache */
396 i_cached_item_id = -1;
400 /* This function is called on a regular basis */
401 void PlaylistManager::Update()
405 /* If the playlist isn't show there's no need to update it */
406 if( !IsShown() ) return;
408 if( this->b_need_update )
410 this->b_need_update = VLC_FALSE;
414 /* Updating the playing status every 0.5s is enough */
415 if( i_update_counter % 5 ) return;
418 /**********************************************************************
419 * Rebuild the playlist
420 **********************************************************************/
421 void PlaylistManager::Rebuild( vlc_bool_t b_root )
423 i_items_to_append = 0;
424 i_cached_item_id = -1;
426 treectrl->DeleteAllItems();
427 treectrl->AddRoot( wxU(_("root" )), -1, -1,
428 new PlaylistItem( p_playlist->p_root_category ) );
430 wxTreeItemId root = treectrl->GetRootItem();
431 UpdateNode( p_playlist->p_root_category, root );
434 /**********************************************************************
435 * Search functions (internal)
436 **********************************************************************/
438 /* Find a wxItem from a playlist id */
439 wxTreeItemId PlaylistManager::FindItem( wxTreeItemId root, int i_id )
441 wxTreeItemIdValue cookie;
442 PlaylistItem *p_wxcurrent;
443 wxTreeItemId dummy, search, item, child;
445 if( i_id < 0 ) return dummy;
446 if( i_cached_item_id == i_id ) return cached_item;
448 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
449 if( !p_wxcurrent ) return dummy;
451 if( p_wxcurrent->i_id == i_id )
453 i_cached_item_id = i_id;
458 item = treectrl->GetFirstChild( root, cookie );
461 p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
464 item = treectrl->GetNextChild( root, cookie );
468 if( p_wxcurrent->i_id == i_id )
470 i_cached_item_id = i_id;
475 if( treectrl->ItemHasChildren( item ) )
477 wxTreeItemId search = FindItem( item, i_id );
478 if( search.IsOk() ) return search;
481 item = treectrl->GetNextChild( root, cookie );
487 /********************************************************************
489 ********************************************************************/
490 void PlaylistManager::OnActivateItem( wxTreeEvent& event )
492 playlist_item_t *p_item, *p_node;
493 wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
494 PlaylistItem *p_wxitem = (PlaylistItem *)
495 treectrl->GetItemData( event.GetItem() );
497 if( !p_wxitem || !parent.IsOk() ) return;
499 PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
500 if( !p_wxparent ) return;
502 LockPlaylist( p_intf->p_sys, p_playlist );
503 p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id, VLC_TRUE );
504 p_node = playlist_ItemGetById( p_playlist, p_wxparent->i_id, VLC_TRUE );
505 if( !p_item || p_item->i_children >= 0 )
510 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE, p_node, p_item );
511 UnlockPlaylist( p_intf->p_sys, p_playlist );
514 void PlaylistManager::OnPlaylistEvent( wxCommandEvent& event )
516 switch( event.GetId() )
518 case UpdateItem_Event:
519 UpdateItem( event.GetInt() );
521 case AppendItem_Event:
524 case RemoveItem_Event:
525 RemoveItem( event.GetInt() );
530 /*****************************************************************************
531 * ItemChanged: callback triggered by the item-change playlist variable
532 *****************************************************************************/
533 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
534 vlc_value_t old_val, vlc_value_t new_val, void *param )
536 PlaylistManager *p_playlist = (PlaylistManager *)param;
538 wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
539 event.SetInt( new_val.i_int );
540 p_playlist->AddPendingEvent( event );
544 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
545 vlc_value_t old_val, vlc_value_t new_val, void *param )
547 PlaylistManager *p_playlist = (PlaylistManager *)param;
549 wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
550 event.SetInt( new_val.i_int );
551 p_playlist->AddPendingEvent( event );
556 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
557 vlc_value_t oval, vlc_value_t nval, void *param )
559 PlaylistManager *p_playlist = (PlaylistManager *)param;
561 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
562 memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
564 if( ++p_playlist->i_items_to_append >= 50 )
566 /* Too many items waiting to be added, it will be quicker to rebuild
567 * the whole playlist */
568 p_playlist->b_need_update = VLC_TRUE;
572 wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
573 event.SetClientData( (void *)p_add );
574 p_playlist->AddPendingEvent( event );