]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/playlist_manager.cpp
* modules/gui/wxwidgets: display and resizing of the embedded playlist should behave...
[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(0,0) )
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     sizer->Layout();
127     sizer->Fit( this );
128
129     /* Create image list */
130     wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
131
132     /* FIXME: absolutely needs to be in the right order FIXME */
133     p_images->Add( wxIcon( type_unknown_xpm ) );
134     p_images->Add( wxIcon( type_afile_xpm ) );
135     p_images->Add( wxIcon( type_vfile_xpm ) );
136     p_images->Add( wxIcon( type_directory_xpm ) );
137     p_images->Add( wxIcon( type_disc_xpm ) );
138     p_images->Add( wxIcon( type_cdda_xpm ) );
139     p_images->Add( wxIcon( type_card_xpm ) );
140     p_images->Add( wxIcon( type_net_xpm ) );
141     p_images->Add( wxIcon( type_playlist_xpm ) );
142     p_images->Add( wxIcon( type_node_xpm ) );
143     treectrl->AssignImageList( p_images );
144
145     /* Reduce font size */
146     wxFont font = treectrl->GetFont(); font.SetPointSize(9);
147     treectrl->SetFont( font );
148
149 #if wxUSE_DRAG_AND_DROP
150     /* Associate drop targets with the playlist */
151     SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
152 #endif
153
154     /* Update the playlist */
155     Rebuild( VLC_TRUE );
156
157     /*
158      * We want to be notified of playlist changes
159      */
160
161     /* Some global changes happened -> Rebuild all */
162     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
163
164     /* We went to the next item */
165     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
166
167     /* One item has been updated */
168     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
169
170     var_AddCallback( p_playlist, "item-append", ItemAppended, this );
171     var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
172 }
173
174 PlaylistManager::~PlaylistManager()
175 {
176     if( p_playlist == NULL ) return;
177
178     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
179     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
180     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
181     var_DelCallback( p_playlist, "item-append", ItemAppended, this );
182     var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
183     vlc_object_release( p_playlist );
184 }
185
186 /*****************************************************************************
187  * PlaylistChanged: callback triggered by the intf-change playlist variable
188  *  We don't rebuild the playlist directly here because we don't want the
189  *  caller to block for a too long time.
190  *****************************************************************************/
191 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
192                             vlc_value_t oval, vlc_value_t nval, void *param )
193 {
194     PlaylistManager *p_playlist = (PlaylistManager *)param;
195     p_playlist->b_need_update = VLC_TRUE;
196     return VLC_SUCCESS;
197 }
198
199 /*****************************************************************************
200  * Next: callback triggered by the playlist-current playlist variable
201  *****************************************************************************/
202 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
203                          vlc_value_t oval, vlc_value_t nval, void *param )
204 {
205     PlaylistManager *p_playlist = (PlaylistManager *)param;
206
207     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
208     event.SetInt( oval.i_int );
209     p_playlist->AddPendingEvent( event );
210     event.SetInt( nval.i_int );
211     p_playlist->AddPendingEvent( event );
212
213     return VLC_SUCCESS;
214 }
215
216 /*****************************************************************************
217  * Update functions
218  *****************************************************************************/
219 void PlaylistManager::CreateNode( playlist_item_t *p_node, wxTreeItemId parent)
220 {
221     wxTreeItemId node =
222         treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ), -1, -1,
223                               new PlaylistItem( p_node ) );
224     treectrl->SetItemImage( node, p_node->input.i_type );
225
226     UpdateNodeChildren( p_node, node );
227 }
228
229 void PlaylistManager::UpdateNode( playlist_item_t *p_node, wxTreeItemId node )
230 {
231     wxTreeItemIdValue cookie;
232     wxTreeItemId child;
233
234     for( int i = 0; i < p_node->i_children ; i++ )
235     {
236         if( !i ) child = treectrl->GetFirstChild( node, cookie);
237         else child = treectrl->GetNextChild( node, cookie );
238
239         if( !child.IsOk() )
240         {
241             /* Not enough children */
242             CreateNode( p_node->pp_children[i], node );
243             /* Keep the tree pointer up to date */
244             child = treectrl->GetNextChild( node, cookie );
245         }
246     }
247
248     treectrl->SetItemImage( node, p_node->input.i_type );
249
250 }
251
252 void PlaylistManager::UpdateNodeChildren( playlist_item_t *p_node,
253                                           wxTreeItemId node )
254 {
255     for( int i = 0; i< p_node->i_children ; i++ )
256     {
257         /* Append the item */
258         if( p_node->pp_children[i]->i_children == -1 )
259         {
260             wxTreeItemId item =
261                 treectrl->AppendItem( node,
262                     wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
263                            new PlaylistItem( p_node->pp_children[i]) );
264
265             UpdateTreeItem( item );
266         }
267         else
268         {
269             CreateNode( p_node->pp_children[i], node );
270         }
271     }
272 }
273
274 void PlaylistManager::UpdateTreeItem( wxTreeItemId item )
275 {
276     if( ! item.IsOk() ) return;
277
278     wxTreeItemData *p_data = treectrl->GetItemData( item );
279     if( !p_data ) return;
280
281     LockPlaylist( p_intf->p_sys, p_playlist );
282     playlist_item_t *p_item =
283         playlist_ItemGetById( p_playlist, ((PlaylistItem *)p_data)->i_id );
284     if( !p_item )
285     {
286         UnlockPlaylist( p_intf->p_sys, p_playlist );
287         return;
288     }
289
290     wxString msg;
291     wxString duration = wxU( "" );
292     char *psz_author =
293         vlc_input_item_GetInfo( &p_item->input,
294                                 _("Meta-information"), _("Artist"));
295     if( !psz_author )
296     {
297         UnlockPlaylist( p_intf->p_sys, p_playlist );
298         return;
299     }
300
301     char psz_duration[MSTRTIME_MAX_SIZE];
302     mtime_t dur = p_item->input.i_duration;
303
304     if( dur != -1 )
305     {
306         secstotimestr( psz_duration, dur/1000000 );
307         duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
308                          wxU( " )" ) );
309     }
310
311     if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
312     {
313         msg = wxString( wxU( p_item->input.psz_name ) ) + duration;
314     }
315     else
316     {
317         msg = wxString(wxU( psz_author )) + wxT(" - ") +
318                     wxString(wxU(p_item->input.psz_name)) + duration;
319     }
320     free( psz_author );
321     treectrl->SetItemText( item , msg );
322     treectrl->SetItemImage( item, p_item->input.i_type );
323
324     if( p_playlist->status.p_item == p_item )
325     {
326         treectrl->SetItemBold( item, true );
327         while( treectrl->GetItemParent( item ).IsOk() )
328         {
329             item = treectrl->GetItemParent( item );
330             treectrl->Expand( item );
331         }
332     }
333     else
334     {
335         treectrl->SetItemBold( item, false );
336     }
337     UnlockPlaylist( p_intf->p_sys, p_playlist );
338 }
339
340 void PlaylistManager::AppendItem( wxCommandEvent& event )
341 {
342     playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
343     playlist_item_t *p_item = NULL;
344     wxTreeItemId item, node;
345
346     i_items_to_append--;
347
348     /* No need to do anything if the playlist is going to be rebuilt */
349     if( b_need_update ) return;
350
351     //if( p_add->i_view != i_current_view ) goto update;
352
353     node = FindItem( treectrl->GetRootItem(), p_add->i_node );
354     if( !node.IsOk() ) goto update;
355
356     p_item = playlist_ItemGetById( p_playlist, p_add->i_item );
357     if( !p_item ) goto update;
358
359     item = FindItem( treectrl->GetRootItem(), p_add->i_item );
360     if( item.IsOk() ) goto update;
361
362     item = treectrl->AppendItem( node, wxL2U( p_item->input.psz_name ), -1,-1,
363                                  new PlaylistItem( p_item ) );
364     treectrl->SetItemImage( item, p_item->input.i_type );
365
366     if( item.IsOk() && p_item->i_children == -1 ) UpdateTreeItem( item );
367
368 update:
369     return;
370 }
371
372 void PlaylistManager::UpdateItem( int i )
373 {
374     if( i < 0 ) return; /* Sanity check */
375
376     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
377     if( item.IsOk() ) UpdateTreeItem( item );
378 }
379
380 void PlaylistManager::RemoveItem( int i )
381 {
382     if( i <= 0 ) return; /* Sanity check */
383
384     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
385     if( item.IsOk() )
386     {
387         treectrl->Delete( item );
388
389         /* Invalidate cache */
390         i_cached_item_id = -1;
391     }
392 }
393
394 /* This function is called on a regular basis */
395 void PlaylistManager::Update()
396 {
397     i_update_counter++;
398
399     /* If the playlist isn't show there's no need to update it */
400     if( !IsShown() ) return;
401
402     if( this->b_need_update )
403     {
404         this->b_need_update = VLC_FALSE;
405         Rebuild( VLC_TRUE );
406     }
407
408     /* Updating the playing status every 0.5s is enough */
409     if( i_update_counter % 5 ) return;
410 }
411
412 /**********************************************************************
413  * Rebuild the playlist
414  **********************************************************************/
415 void PlaylistManager::Rebuild( vlc_bool_t b_root )
416 {
417     playlist_view_t *p_view;
418
419     i_items_to_append = 0;
420     i_cached_item_id = -1;
421
422     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
423
424     treectrl->DeleteAllItems();
425     treectrl->AddRoot( wxU(_("root" )), -1, -1,
426                        new PlaylistItem( p_view->p_root ) );
427
428     wxTreeItemId root = treectrl->GetRootItem();
429     UpdateNode( p_view->p_root, root );
430 }
431
432 /**********************************************************************
433  * Search functions (internal)
434  **********************************************************************/
435
436 /* Find a wxItem from a playlist id */
437 wxTreeItemId PlaylistManager::FindItem( wxTreeItemId root, int i_id )
438 {
439     wxTreeItemIdValue cookie;
440     PlaylistItem *p_wxcurrent;
441     wxTreeItemId dummy, search, item, child;
442
443     if( i_id < 0 ) return dummy;
444     if( i_cached_item_id == i_id ) return cached_item;
445
446     p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
447     if( !p_wxcurrent ) return dummy;
448
449     if( p_wxcurrent->i_id == i_id )
450     {
451         i_cached_item_id = i_id;
452         cached_item = root;
453         return root;
454     }
455
456     item = treectrl->GetFirstChild( root, cookie );
457     while( item.IsOk() )
458     {
459         p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
460         if( !p_wxcurrent )
461         {
462             item = treectrl->GetNextChild( root, cookie );
463             continue;
464         }
465
466         if( p_wxcurrent->i_id == i_id )
467         {
468             i_cached_item_id = i_id;
469             cached_item = item;
470             return item;
471         }
472
473         if( treectrl->ItemHasChildren( item ) )
474         {
475             wxTreeItemId search = FindItem( item, i_id );
476             if( search.IsOk() ) return search;
477         }
478
479         item = treectrl->GetNextChild( root, cookie );
480     }
481
482     return dummy;
483 }
484
485 /********************************************************************
486  * Events
487  ********************************************************************/
488 void PlaylistManager::OnActivateItem( wxTreeEvent& event )
489 {
490     playlist_item_t *p_item, *p_node;
491     wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
492     PlaylistItem *p_wxitem = (PlaylistItem *)
493         treectrl->GetItemData( event.GetItem() );
494
495     if( !p_wxitem || !parent.IsOk() ) return;
496
497     PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
498     if( !p_wxparent ) return;
499
500     LockPlaylist( p_intf->p_sys, p_playlist );
501     p_item = playlist_ItemGetById( p_playlist, p_wxitem->i_id );
502     p_node = playlist_ItemGetById( p_playlist, p_wxparent->i_id );
503     if( !p_item || p_item->i_children >= 0 )
504     {
505         p_node = p_item;
506         p_item = NULL;
507     }
508
509     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VIEW_CATEGORY,
510                       p_node, p_item );
511     UnlockPlaylist( p_intf->p_sys, p_playlist );
512 }
513
514 void PlaylistManager::OnPlaylistEvent( wxCommandEvent& event )
515 {
516     switch( event.GetId() )
517     {
518     case UpdateItem_Event:
519         UpdateItem( event.GetInt() );
520         break;
521     case AppendItem_Event:
522         AppendItem( event );
523         break;
524     case RemoveItem_Event:
525         RemoveItem( event.GetInt() );
526         break;
527     }
528 }
529
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 )
535 {
536     PlaylistManager *p_playlist = (PlaylistManager *)param;
537
538     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
539     event.SetInt( new_val.i_int );
540     p_playlist->AddPendingEvent( event );
541
542     return VLC_SUCCESS;
543 }
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 )
546 {
547     PlaylistManager *p_playlist = (PlaylistManager *)param;
548
549     wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
550     event.SetInt( new_val.i_int );
551     p_playlist->AddPendingEvent( event );
552
553     return VLC_SUCCESS;
554 }
555
556 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
557                          vlc_value_t oval, vlc_value_t nval, void *param )
558 {
559     PlaylistManager *p_playlist = (PlaylistManager *)param;
560
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 ) );
563
564     if( p_playlist->i_items_to_append++ > 50 )
565     {
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;
569         return VLC_SUCCESS;
570     }
571
572     wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
573     event.SetClientData( (void *)p_add );
574     p_playlist->AddPendingEvent( event );
575
576     return VLC_SUCCESS;
577 }
578 }