]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/playlist_manager.cpp
* modules/gui/wxwidgets: start of a playlist_manager widget.
[vlc] / modules / gui / wxwidgets / playlist_manager.cpp
1 /*****************************************************************************
2  * playlist_manager.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *          Clément Stenac <zorglub@videolan.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
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.
15  *
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.
20  *
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "playlist_manager.hpp"
30 #include "interface.hpp"
31
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"
42
43 #include <wx/dynarray.h>
44 #include <wx/imaglist.h>
45
46 namespace wxvlc {
47 /* Callback prototype */
48 static int PlaylistChanged( vlc_object_t *, const char *,
49                             vlc_value_t, vlc_value_t, void * );
50 static int PlaylistNext( vlc_object_t *, const char *,
51                          vlc_value_t, vlc_value_t, void * );
52 static int ItemChanged( vlc_object_t *, const char *,
53                         vlc_value_t, vlc_value_t, void * );
54 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
55                       vlc_value_t oval, vlc_value_t nval, void *param );
56 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
57                       vlc_value_t oval, vlc_value_t nval, void *param );
58
59 /*****************************************************************************
60  * Event Table.
61  *****************************************************************************/
62
63 /* IDs for the controls and the menu commands */
64 enum
65 {
66     TreeCtrl_Event,
67
68     UpdateItem_Event,
69     AppendItem_Event,
70     RemoveItem_Event,
71 };
72
73 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
74
75 BEGIN_EVENT_TABLE(PlaylistManager, wxPanel)
76     /* Tree control events */
77     EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, PlaylistManager::OnActivateItem )
78
79     /* Custom events */
80     EVT_COMMAND(-1, wxEVT_PLAYLIST, PlaylistManager::OnPlaylistEvent)
81 END_EVENT_TABLE()
82
83 /*****************************************************************************
84  * PlaylistItem class
85  ****************************************************************************/
86 class PlaylistItem : public wxTreeItemData
87 {
88 public:
89     PlaylistItem( playlist_item_t *p_item ) : i_id(p_item->input.i_id) {}
90     int i_id;
91 };
92
93 /*****************************************************************************
94  * Constructor.
95  *****************************************************************************/
96 PlaylistManager::PlaylistManager( intf_thread_t *_p_intf, wxWindow *p_parent ):
97     wxPanel( p_parent, -1, wxDefaultPosition, wxSize(500,300) )
98 {
99     /* Initializations */
100     p_intf = _p_intf;
101     b_need_update = VLC_FALSE;
102     i_items_to_append = 0;
103     i_cached_item_id = -1;
104     i_update_counter = 0;
105
106     p_playlist = (playlist_t *)
107         vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
108     if( p_playlist == NULL ) return;
109
110     var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
111     var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
112     var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
113
114     /* Create the tree */
115     treectrl = new wxTreeCtrl( this, TreeCtrl_Event,
116                                wxDefaultPosition, wxDefaultSize,
117                                wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
118                                wxTR_NO_LINES |
119                                wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
120                                wxTR_MULTIPLE | wxTR_EXTENDED );
121
122     /* Add everything to the panel */
123     sizer = new wxBoxSizer( wxHORIZONTAL );
124     SetSizer( sizer );
125     sizer->Add( treectrl, 1, wxEXPAND );
126
127     /* Create image list */
128     wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
129
130     /* FIXME: absolutely needs to be in the right order FIXME */
131     p_images->Add( wxIcon( type_unknown_xpm ) );
132     p_images->Add( wxIcon( type_afile_xpm ) );
133     p_images->Add( wxIcon( type_vfile_xpm ) );
134     p_images->Add( wxIcon( type_directory_xpm ) );
135     p_images->Add( wxIcon( type_disc_xpm ) );
136     p_images->Add( wxIcon( type_cdda_xpm ) );
137     p_images->Add( wxIcon( type_card_xpm ) );
138     p_images->Add( wxIcon( type_net_xpm ) );
139     p_images->Add( wxIcon( type_playlist_xpm ) );
140     p_images->Add( wxIcon( type_node_xpm ) );
141     treectrl->AssignImageList( p_images );
142
143     /* Reduce font size */
144     wxFont font = treectrl->GetFont(); font.SetPointSize(9);
145     treectrl->SetFont( font );
146
147 #if wxUSE_DRAG_AND_DROP
148     /* Associate drop targets with the playlist */
149     SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
150 #endif
151
152     /* Update the playlist */
153     Rebuild( VLC_TRUE );
154
155     /*
156      * We want to be notified of playlist changes
157      */
158
159     /* Some global changes happened -> Rebuild all */
160     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
161
162     /* We went to the next item */
163     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
164
165     /* One item has been updated */
166     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
167
168     var_AddCallback( p_playlist, "item-append", ItemAppended, this );
169     var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
170 }
171
172 PlaylistManager::~PlaylistManager()
173 {
174     if( p_playlist == NULL ) return;
175
176     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
177     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
178     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
179     var_DelCallback( p_playlist, "item-append", ItemAppended, this );
180     var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
181     vlc_object_release( p_playlist );
182 }
183
184 /*****************************************************************************
185  * PlaylistChanged: callback triggered by the intf-change playlist variable
186  *  We don't rebuild the playlist directly here because we don't want the
187  *  caller to block for a too long time.
188  *****************************************************************************/
189 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
190                             vlc_value_t oval, vlc_value_t nval, void *param )
191 {
192     PlaylistManager *p_playlist = (PlaylistManager *)param;
193     p_playlist->b_need_update = VLC_TRUE;
194     return VLC_SUCCESS;
195 }
196
197 /*****************************************************************************
198  * Next: callback triggered by the playlist-current playlist variable
199  *****************************************************************************/
200 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
201                          vlc_value_t oval, vlc_value_t nval, void *param )
202 {
203     PlaylistManager *p_playlist = (PlaylistManager *)param;
204
205     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
206     event.SetInt( oval.i_int );
207     p_playlist->AddPendingEvent( event );
208     event.SetInt( nval.i_int );
209     p_playlist->AddPendingEvent( event );
210
211     return VLC_SUCCESS;
212 }
213
214 /*****************************************************************************
215  * Update functions
216  *****************************************************************************/
217 void PlaylistManager::CreateNode( playlist_item_t *p_node, wxTreeItemId parent)
218 {
219     wxTreeItemId node =
220         treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ), -1, -1,
221                               new PlaylistItem( p_node ) );
222     treectrl->SetItemImage( node, p_node->input.i_type );
223
224     UpdateNodeChildren( p_node, node );
225 }
226
227 void PlaylistManager::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
228 {
229     wxTreeItemIdValue cookie;
230     wxTreeItemId child;
231
232     for( int i = 0; i < p_node->i_children ; i++ )
233     {
234         if( !i ) child = treectrl->GetFirstChild( node, cookie);
235         else child = treectrl->GetNextChild( node, cookie );
236
237         if( !child.IsOk() )
238         {
239             /* Not enough children */
240             CreateNode( p_node->pp_children[i], node );
241             /* Keep the tree pointer up to date */
242             child = treectrl->GetNextChild( node, cookie );
243         }
244     }
245
246     treectrl->SetItemImage( node, p_node->input.i_type );
247
248 }
249
250 void PlaylistManager::UpdateNodeChildren( playlist_item_t *p_node,
251                                           wxTreeItemId node )
252 {
253     for( int i = 0; i< p_node->i_children ; i++ )
254     {
255         /* Append the item */
256         if( p_node->pp_children[i]->i_children == -1 )
257         {
258             wxTreeItemId item =
259                 treectrl->AppendItem( node,
260                     wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
261                            new PlaylistItem( p_node->pp_children[i]) );
262
263             UpdateTreeItem( item );
264         }
265         else
266         {
267             CreateNode( p_node->pp_children[i], node );
268         }
269     }
270 }
271
272 void PlaylistManager::UpdateTreeItem( wxTreeItemId item )
273 {
274     if( ! item.IsOk() ) return;
275
276     wxTreeItemData *p_data = treectrl->GetItemData( item );
277     if( !p_data ) return;
278
279     LockPlaylist( p_intf->p_sys, p_playlist );
280     playlist_item_t *p_item =
281         playlist_ItemGetById( p_playlist, ((PlaylistItem *)p_data)->i_id );
282     if( !p_item )
283     {
284         UnlockPlaylist( p_intf->p_sys, p_playlist );
285         return;
286     }
287
288     wxString msg;
289     wxString duration = wxU( "" );
290     char *psz_author =
291         vlc_input_item_GetInfo( &p_item->input,
292                                 _("Meta-information"), _("Artist"));
293     if( !psz_author )
294     {
295         UnlockPlaylist( p_intf->p_sys, p_playlist );
296         return;
297     }
298
299     char psz_duration[MSTRTIME_MAX_SIZE];
300     mtime_t dur = p_item->input.i_duration;
301
302     if( dur != -1 )
303     {
304         secstotimestr( psz_duration, dur/1000000 );
305         duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
306                          wxU( " )" ) );
307     }
308
309     if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
310     {
311         msg = wxString( wxU( p_item->input.psz_name ) ) + duration;
312     }
313     else
314     {
315         msg = wxString(wxU( psz_author )) + wxT(" - ") +
316                     wxString(wxU(p_item->input.psz_name)) + duration;
317     }
318     free( psz_author );
319     treectrl->SetItemText( item , msg );
320     treectrl->SetItemImage( item, p_item->input.i_type );
321
322     if( p_playlist->status.p_item == p_item )
323     {
324         treectrl->SetItemBold( item, true );
325         while( treectrl->GetItemParent( item ).IsOk() )
326         {
327             item = treectrl->GetItemParent( item );
328             treectrl->Expand( item );
329         }
330     }
331     else
332     {
333         treectrl->SetItemBold( item, false );
334     }
335     UnlockPlaylist( p_intf->p_sys, p_playlist );
336 }
337
338 void PlaylistManager::AppendItem( wxCommandEvent& event )
339 {
340     playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
341     playlist_item_t *p_item = NULL;
342     wxTreeItemId item, node;
343
344     i_items_to_append--;
345
346     /* No need to do anything if the playlist is going to be rebuilt */
347     if( b_need_update ) return;
348
349     //if( p_add->i_view != i_current_view ) goto update;
350
351     node = FindItem( treectrl->GetRootItem(), p_add->i_node );
352     if( !node.IsOk() ) goto update;
353
354     p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
355     if( !p_item ) goto update;
356
357     item = FindItem( treectrl->GetRootItem(), p_add->i_item );
358     if( item.IsOk() ) goto update;
359
360     item = treectrl->AppendItem( node, wxL2U( p_item->input.psz_name ), -1,-1,
361                                  new PlaylistItem( p_item ) );
362     treectrl->SetItemImage( item, p_item->input.i_type );
363
364     if( item.IsOk() && p_item->i_children == -1 ) UpdateTreeItem( item );
365
366 update:
367     return;
368 }
369
370 void PlaylistManager::UpdateItem( int i )
371 {
372     if( i < 0 ) return; /* Sanity check */
373
374     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
375     if( item.IsOk() ) UpdateTreeItem( item );
376 }
377
378 void PlaylistManager::RemoveItem( int i )
379 {
380     if( i <= 0 ) return; /* Sanity check */
381
382     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
383     if( item.IsOk() )
384     {
385         treectrl->Delete( item );
386
387         /* Invalidate cache */
388         i_cached_item_id = -1;
389     }
390 }
391
392 /* This function is called on a regular basis */
393 void PlaylistManager::Update()
394 {
395     i_update_counter++;
396
397     /* If the playlist isn't show there's no need to update it */
398     if( !IsShown() ) return;
399
400     if( this->b_need_update )
401     {
402         this->b_need_update = VLC_FALSE;
403         Rebuild( VLC_TRUE );
404     }
405
406     /* Updating the playing status every 0.5s is enough */
407     if( i_update_counter % 5 ) return;
408 }
409
410 /**********************************************************************
411  * Rebuild the playlist
412  **********************************************************************/
413 void PlaylistManager::Rebuild( vlc_bool_t b_root )
414 {
415     playlist_view_t *p_view;
416
417     i_items_to_append = 0;
418     i_cached_item_id = -1;
419
420     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
421
422     treectrl->DeleteAllItems();
423     treectrl->AddRoot( wxU(_("root" )), -1, -1,
424                        new PlaylistItem( p_view->p_root ) );
425
426     wxTreeItemId root = treectrl->GetRootItem();
427     UpdateNode( p_view->p_root, root );
428 }
429
430 /**********************************************************************
431  * Search functions (internal)
432  **********************************************************************/
433
434 /* Find a wxItem from a playlist id */
435 wxTreeItemId PlaylistManager::FindItem( wxTreeItemId root, int i_id )
436 {
437     wxTreeItemIdValue cookie;
438     PlaylistItem *p_wxcurrent;
439     wxTreeItemId dummy, search, item, child;
440
441     if( i_id < 0 ) return dummy;
442     if( i_cached_item_id == i_id ) return cached_item;
443
444     p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
445     if( !p_wxcurrent ) return dummy;
446
447     if( p_wxcurrent->i_id == i_id )
448     {
449         i_cached_item_id = i_id;
450         cached_item = root;
451         return root;
452     }
453
454     item = treectrl->GetFirstChild( root, cookie );
455     while( item.IsOk() )
456     {
457         p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
458         if( !p_wxcurrent )
459         {
460             item = treectrl->GetNextChild( root, cookie );
461             continue;
462         }
463
464         if( p_wxcurrent->i_id == i_id )
465         {
466             i_cached_item_id = i_id;
467             cached_item = item;
468             return item;
469         }
470
471         if( treectrl->ItemHasChildren( item ) )
472         {
473             wxTreeItemId search = FindItem( item, i_id );
474             if( search.IsOk() ) return search;
475         }
476
477         item = treectrl->GetNextChild( root, cookie );
478     }
479
480     return dummy;
481 }
482
483 /********************************************************************
484  * Events
485  ********************************************************************/
486 void PlaylistManager::OnActivateItem( wxTreeEvent& event )
487 {
488     playlist_item_t *p_item, *p_node;
489     wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
490     PlaylistItem *p_wxitem = (PlaylistItem *)
491         treectrl->GetItemData( event.GetItem() );
492
493     if( !p_wxitem || !parent.IsOk() ) return;
494
495     PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
496     if( !p_wxparent ) return;
497
498     LockPlaylist( p_intf->p_sys, p_playlist );
499     p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
500     p_node = playlist_ItemGetById( p_playlist, p_wxparent->i_id );
501     if( !p_item || p_item->i_children >= 0 )
502     {
503         p_node = p_item;
504         p_item = NULL;
505     }
506
507     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_CATEGORY,
508                       p_node, p_item );
509     UnlockPlaylist( p_intf->p_sys, p_playlist );
510 }
511
512 void PlaylistManager::OnPlaylistEvent( wxCommandEvent& event )
513 {
514     switch( event.GetId() )
515     {
516     case UpdateItem_Event:
517         UpdateItem( event.GetInt() );
518         break;
519     case AppendItem_Event:
520         AppendItem( event );
521         break;
522     case RemoveItem_Event:
523         RemoveItem( event.GetInt() );
524         break;
525     }
526 }
527
528 /*****************************************************************************
529  * ItemChanged: callback triggered by the item-change playlist variable
530  *****************************************************************************/
531 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
532                         vlc_value_t old_val, vlc_value_t new_val, void *param )
533 {
534     PlaylistManager *p_playlist = (PlaylistManager *)param;
535
536     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
537     event.SetInt( new_val.i_int );
538     p_playlist->AddPendingEvent( event );
539
540     return VLC_SUCCESS;
541 }
542 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
543                         vlc_value_t old_val, vlc_value_t new_val, void *param )
544 {
545     PlaylistManager *p_playlist = (PlaylistManager *)param;
546
547     wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
548     event.SetInt( new_val.i_int );
549     p_playlist->AddPendingEvent( event );
550
551     return VLC_SUCCESS;
552 }
553
554 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
555                          vlc_value_t oval, vlc_value_t nval, void *param )
556 {
557     PlaylistManager *p_playlist = (PlaylistManager *)param;
558
559     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
560     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
561
562     if( p_playlist->i_items_to_append++ > 50 )
563     {
564         /* Too many items waiting to be added, it will be quicker to rebuild
565          * the whole playlist */
566         p_playlist->b_need_update = VLC_TRUE;
567         return VLC_SUCCESS;
568     }
569
570     wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
571     event.SetClientData( (void *)p_add );
572     p_playlist->AddPendingEvent( event );
573
574     return VLC_SUCCESS;
575 }
576 }