]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/playlist.cpp
Reduce the global verbosity
[vlc] / modules / gui / wxwindows / playlist.cpp
1 /*****************************************************************************
2  * playlist.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *          Clément Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/OR MODIFy
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/intf.h>
30
31 #include "wxwindows.h"
32
33 #include "bitmaps/shuffle.xpm"
34 #include "bitmaps/repeat.xpm"
35 #include "bitmaps/loop.xpm"
36
37 #include "bitmaps/type_unknown.xpm"
38 #include "bitmaps/type_afile.xpm"
39 #include "bitmaps/type_vfile.xpm"
40 #include "bitmaps/type_net.xpm"
41 #include "bitmaps/type_card.xpm"
42 #include "bitmaps/type_disc.xpm"
43 #include "bitmaps/type_cdda.xpm"
44 #include "bitmaps/type_directory.xpm"
45 #include "bitmaps/type_playlist.xpm"
46 #include "bitmaps/type_node.xpm"
47
48 #include <wx/dynarray.h>
49 #include <wx/imaglist.h>
50
51 #define HELP_SHUFFLE N_( "Shuffle" )
52 #define HELP_LOOP N_( "Repeat All" )
53 #define HELP_REPEAT N_( "Repeat One" )
54
55 /* Callback prototype */
56 static int PlaylistChanged( vlc_object_t *, const char *,
57                             vlc_value_t, vlc_value_t, void * );
58 static int PlaylistNext( vlc_object_t *, const char *,
59                          vlc_value_t, vlc_value_t, void * );
60 static int ItemChanged( vlc_object_t *, const char *,
61                         vlc_value_t, vlc_value_t, void * );
62 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
63                       vlc_value_t oval, vlc_value_t nval, void *param );
64 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
65                       vlc_value_t oval, vlc_value_t nval, void *param );
66
67 /*****************************************************************************
68  * Event Table.
69  *****************************************************************************/
70
71 /* IDs for the controls and the menu commands */
72 enum
73 {
74     /* menu items */
75     AddFile_Event = 1,
76     AddDir_Event,
77     AddMRL_Event,
78     Close_Event,
79     Open_Event,
80     Save_Event,
81
82     SortTitle_Event,
83     RSortTitle_Event,
84     Randomize_Event,
85
86     InvertSelection_Event,
87     DeleteSelection_Event,
88     Random_Event,
89     Loop_Event,
90     Repeat_Event,
91     SelectAll_Event,
92
93     PopupPlay_Event,
94     PopupPlayThis_Event,
95     PopupPreparse_Event,
96     PopupSort_Event,
97     PopupDel_Event,
98     PopupInfo_Event,
99
100     SearchText_Event,
101     Search_Event,
102
103     /* controls */
104     TreeCtrl_Event,
105
106     Browse_Event,  /* For export playlist */
107
108     /* custom events */
109     UpdateItem_Event,
110     AppendItem_Event,
111     RemoveItem_Event,
112
113     MenuDummy_Event = wxID_HIGHEST + 999,
114
115     FirstView_Event = wxID_HIGHEST + 1000,
116     LastView_Event = wxID_HIGHEST + 1100,
117
118     FirstSD_Event = wxID_HIGHEST + 2000,
119     LastSD_Event = wxID_HIGHEST + 2100,
120 };
121
122 DEFINE_LOCAL_EVENT_TYPE( wxEVT_PLAYLIST );
123
124 BEGIN_EVENT_TABLE(Playlist, wxFrame)
125     EVT_SIZE(Playlist::OnSize)
126
127     /* Menu events */
128     EVT_MENU(AddFile_Event, Playlist::OnAddFile)
129     EVT_MENU(AddDir_Event, Playlist::OnAddDir)
130     EVT_MENU(AddMRL_Event, Playlist::OnAddMRL)
131     EVT_MENU(Close_Event, Playlist::OnClose)
132     EVT_MENU(Open_Event, Playlist::OnOpen)
133     EVT_MENU(Save_Event, Playlist::OnSave)
134
135     EVT_MENU(SortTitle_Event, Playlist::OnSort)
136     EVT_MENU(RSortTitle_Event, Playlist::OnSort)
137
138     EVT_MENU(Randomize_Event, Playlist::OnSort)
139
140     EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
141     EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
142     EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
143
144     EVT_MENU_OPEN( Playlist::OnMenuOpen )
145     EVT_MENU( -1, Playlist::OnMenuEvent )
146
147     EVT_TOOL(Random_Event, Playlist::OnRandom)
148     EVT_TOOL(Repeat_Event, Playlist::OnRepeat)
149     EVT_TOOL(Loop_Event, Playlist::OnLoop)
150
151     /* Popup events */
152     EVT_MENU( PopupPlay_Event, Playlist::OnPopupPlay)
153     EVT_MENU( PopupPlayThis_Event, Playlist::OnPopupPlay)
154     EVT_MENU( PopupPreparse_Event, Playlist::OnPopupPreparse)
155     EVT_MENU( PopupSort_Event, Playlist::OnPopupSort)
156     EVT_MENU( PopupDel_Event, Playlist::OnPopupDel)
157     EVT_MENU( PopupInfo_Event, Playlist::OnPopupInfo)
158
159     /* Tree control events */
160     EVT_TREE_ITEM_ACTIVATED( TreeCtrl_Event, Playlist::OnActivateItem )
161
162     EVT_CONTEXT_MENU( Playlist::OnPopup )
163
164     /* Button events */
165     EVT_BUTTON( Search_Event, Playlist::OnSearch)
166     EVT_BUTTON( Save_Event, Playlist::OnSave)
167
168     EVT_TEXT(SearchText_Event, Playlist::OnSearchTextChange)
169
170     /* Custom events */
171     EVT_COMMAND(-1, wxEVT_PLAYLIST, Playlist::OnPlaylistEvent)
172
173     /* Special events : we don't want to destroy the window when the user
174      * clicks on (X) */
175     EVT_CLOSE(Playlist::OnClose)
176 END_EVENT_TABLE()
177
178 /*****************************************************************************
179  * PlaylistItem class
180  ****************************************************************************/
181 class PlaylistItem : public wxTreeItemData
182 {
183 public:
184     PlaylistItem( playlist_item_t *_p_item ) : wxTreeItemData()
185     {
186         p_item = _p_item;
187         i_id = p_item->input.i_id;
188     }
189 protected:
190     playlist_item_t *p_item;
191     int i_id;
192 friend class Playlist;
193 };
194
195 /*****************************************************************************
196  * Constructor.
197  *****************************************************************************/
198 Playlist::Playlist( intf_thread_t *_p_intf, wxWindow *p_parent ):
199     wxFrame( p_parent, -1, wxU(_("Playlist")), wxDefaultPosition,
200              wxSize(500,300), wxDEFAULT_FRAME_STYLE )
201 {
202     vlc_value_t val;
203
204     /* Initializations */
205     p_intf = _p_intf;
206     i_update_counter = 0;
207     i_sort_mode = MODE_NONE;
208     b_need_update = VLC_FALSE;
209     SetIcon( *p_intf->p_sys->p_icon );
210
211     p_view_menu = NULL;
212     p_sd_menu = SDMenu();
213
214     i_current_view = VIEW_SIMPLE;
215     b_changed_view = VLC_FALSE;
216
217     i_title_sorted = 0;
218     i_group_sorted = 0;
219     i_duration_sorted = 0;
220
221     var_Create( p_intf, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
222     var_Create( p_intf, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
223     var_Create( p_intf, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );;
224
225     /* Create our "Manage" menu */
226     wxMenu *manage_menu = new wxMenu;
227     manage_menu->Append( AddFile_Event, wxU(_("&Simple Add File...")) );
228     manage_menu->Append( AddDir_Event, wxU(_("Add &Directory...")) );
229     manage_menu->Append( AddMRL_Event, wxU(_("&Add MRL...")) );
230     manage_menu->AppendSeparator();
231     manage_menu->Append( MenuDummy_Event, wxU(_("Services discovery")),
232                          p_sd_menu );
233     manage_menu->AppendSeparator();
234     manage_menu->Append( Open_Event, wxU(_("&Open Playlist...")) );
235     manage_menu->Append( Save_Event, wxU(_("&Save Playlist...")) );
236     manage_menu->AppendSeparator();
237     manage_menu->Append( Close_Event, wxU(_("&Close")) );
238
239     /* Create our "Sort" menu */
240     wxMenu *sort_menu = new wxMenu;
241     sort_menu->Append( SortTitle_Event, wxU(_("Sort by &title")) );
242     sort_menu->Append( RSortTitle_Event, wxU(_("&Reverse sort by title")) );
243     sort_menu->AppendSeparator();
244     sort_menu->Append( Randomize_Event, wxU(_("&Shuffle Playlist")) );
245
246     /* Create our "Selection" menu */
247     wxMenu *selection_menu = new wxMenu;
248     selection_menu->Append( InvertSelection_Event, wxU(_("&Invert")) );
249     selection_menu->Append( DeleteSelection_Event, wxU(_("D&elete")) );
250     selection_menu->Append( SelectAll_Event, wxU(_("&Select All")) );
251
252     /* Create our "View" menu */
253     ViewMenu();
254
255     /* Append the freshly created menus to the menu bar */
256     wxMenuBar *menubar = new wxMenuBar();
257     menubar->Append( manage_menu, wxU(_("&Manage")) );
258     menubar->Append( sort_menu, wxU(_("S&ort")) );
259     menubar->Append( selection_menu, wxU(_("&Selection")) );
260     menubar->Append( p_view_menu, wxU(_("&View items") ) );
261
262     /* Attach the menu bar to the frame */
263     SetMenuBar( menubar );
264
265     /* Create the popup menu */
266     node_popup = new wxMenu;
267     node_popup->Append( PopupPlay_Event, wxU(_("Play")) );
268     node_popup->Append( PopupPlayThis_Event, wxU(_("Play this branch")) );
269     node_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
270     node_popup->Append( PopupSort_Event, wxU(_("Sort this branch")) );
271     node_popup->Append( PopupDel_Event, wxU(_("Delete")) );
272     node_popup->Append( PopupInfo_Event, wxU(_("Info")) );
273
274     item_popup = new wxMenu;
275     item_popup->Append( PopupPlay_Event, wxU(_("Play")) );
276     item_popup->Append( PopupPreparse_Event, wxU(_("Preparse")) );
277     item_popup->Append( PopupDel_Event, wxU(_("Delete")) );
278     item_popup->Append( PopupInfo_Event, wxU(_("Info")) );
279
280     /* Create a panel to put everything in */
281     wxPanel *playlist_panel = new wxPanel( this, -1 );
282     playlist_panel->SetAutoLayout( TRUE );
283
284     /* Create the toolbar */
285     wxToolBar *toolbar =
286         CreateToolBar( wxTB_HORIZONTAL | wxTB_FLAT );
287
288     /* Create the random tool */
289     toolbar->AddTool( Random_Event, wxT(""), wxBitmap(shuffle_on_xpm),
290                        wxBitmap(shuffle_on_xpm), wxITEM_CHECK,
291                        wxU(_(HELP_SHUFFLE) ) );
292     var_Get( p_intf, "random", &val );
293     toolbar->ToggleTool( Random_Event, val.b_bool );
294
295     /* Create the Loop tool */
296     toolbar->AddTool( Loop_Event, wxT(""), wxBitmap( loop_xpm),
297                       wxBitmap( loop_xpm), wxITEM_CHECK,
298                       wxU(_(HELP_LOOP )  ) );
299     var_Get( p_intf, "loop", &val );
300     toolbar->ToggleTool( Loop_Event, val.b_bool );
301
302     /* Create the Repeat one checkbox */
303     toolbar->AddTool( Repeat_Event, wxT(""), wxBitmap( repeat_xpm),
304                       wxBitmap( repeat_xpm), wxITEM_CHECK,
305                       wxU(_(HELP_REPEAT )  ) );
306     var_Get( p_intf, "repeat", &val );
307     toolbar->ToggleTool( Repeat_Event, val.b_bool ) ;
308
309     /* Create the Search Textbox */
310     search_text = new wxTextCtrl( toolbar, SearchText_Event, wxT(""),
311                                   wxDefaultPosition, wxSize(100, -1),
312                                   wxTE_PROCESS_ENTER);
313
314     /* Create the search button */
315     search_button = new wxButton( toolbar , Search_Event, wxU(_("Search")) );
316
317     toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
318                          wxSize(16, 16), wxBORDER_NONE ) );
319     toolbar->AddControl( search_text );
320     toolbar->AddControl( new wxControl( toolbar, -1, wxDefaultPosition,
321                          wxSize(5, 5), wxBORDER_NONE ) );
322     toolbar->AddControl( search_button );
323     search_button->SetDefault();
324     toolbar->Realize();
325
326     /* Create the tree */
327     treectrl = new wxTreeCtrl( playlist_panel, TreeCtrl_Event,
328                                wxDefaultPosition, wxDefaultSize,
329                                wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT|
330                                wxTR_NO_LINES |
331                                wxTR_HAS_BUTTONS | wxTR_TWIST_BUTTONS |
332                                wxTR_MULTIPLE | wxTR_EXTENDED );
333
334     /* Create image list */
335     wxImageList *p_images = new wxImageList( 16 , 16, TRUE );
336
337     /* FIXME: absolutely needs to be in the right order FIXME */
338     p_images->Add( wxIcon( type_unknown_xpm ) );
339     p_images->Add( wxIcon( type_afile_xpm ) );
340     p_images->Add( wxIcon( type_vfile_xpm ) );
341     p_images->Add( wxIcon( type_directory_xpm ) );
342     p_images->Add( wxIcon( type_disc_xpm ) );
343     p_images->Add( wxIcon( type_cdda_xpm ) );
344     p_images->Add( wxIcon( type_card_xpm ) );
345     p_images->Add( wxIcon( type_net_xpm ) );
346     p_images->Add( wxIcon( type_playlist_xpm ) );
347     p_images->Add( wxIcon( type_node_xpm ) );
348     treectrl->AssignImageList( p_images );
349
350     treectrl->AddRoot( wxU(_("root" )), -1, -1, NULL );
351
352     /* Reduce font size */
353     wxFont font= treectrl->GetFont();
354     font.SetPointSize(9);
355     treectrl->SetFont( font );
356
357     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
358     panel_sizer->Add( treectrl, 1, wxEXPAND | wxALL, 5 );
359     panel_sizer->Layout();
360
361     playlist_panel->SetSizerAndFit( panel_sizer );
362
363     int pi_widths[1] =  { -1 };
364     statusbar = CreateStatusBar( 1 );
365     statusbar->SetStatusWidths( 1, pi_widths );
366
367 #if wxUSE_DRAG_AND_DROP
368     /* Associate drop targets with the playlist */
369     SetDropTarget( new DragAndDrop( p_intf, VLC_TRUE ) );
370 #endif
371
372     playlist_t *p_playlist =
373         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
374                                        FIND_ANYWHERE );
375     if( p_playlist == NULL )
376     {
377         return;
378     }
379
380     p_saved_item = NULL;
381
382
383     /* We want to be noticed of playlist changes */
384
385     /* Some global changes happened -> Rebuild all */
386     var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
387
388     /* We went to the next item */
389     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
390
391     /* One item has been updated */
392     var_AddCallback( p_playlist, "item-change", ItemChanged, this );
393
394     var_AddCallback( p_playlist, "item-append", ItemAppended, this );
395     var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
396
397     /* Update the playlist */
398     Rebuild( VLC_TRUE );
399
400     vlc_object_release( p_playlist );
401 }
402
403 Playlist::~Playlist()
404 {
405     playlist_t *p_playlist =
406         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
407                                        FIND_ANYWHERE );
408     if( p_playlist == NULL )
409     {
410         return;
411     }
412
413     var_DelCallback( p_playlist, "item-change", ItemChanged, this );
414     var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
415     var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
416     var_DelCallback( p_playlist, "item-append", ItemAppended, this );
417     var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
418     vlc_object_release( p_playlist );
419 }
420
421 /**********************************************************************
422  * Update functions
423  **********************************************************************/
424
425 /* Update a node */
426 void Playlist::UpdateNode( playlist_t *p_playlist, playlist_item_t *p_node,
427                            wxTreeItemId node )
428 {
429     long cookie;
430     wxTreeItemId child;
431     for( int i = 0; i< p_node->i_children ; i++ )
432     {
433         if( i == 0 )
434         {
435             child = treectrl->GetFirstChild( node, cookie);
436         }
437         else
438         {
439             child = treectrl->GetNextChild( node, cookie );
440         }
441
442         if( !child.IsOk() )
443         {
444             /* Not enough children */
445             CreateNode( p_playlist, p_node->pp_children[i], node );
446             /* Keep the tree pointer up to date */
447             child = treectrl->GetNextChild( node, cookie );
448         }
449         else
450         {
451         }
452     }
453     treectrl->SetItemImage( node, p_node->input.i_type );
454
455 }
456
457 /* Creates the node p_node as last child of parent */
458 void Playlist::CreateNode( playlist_t *p_playlist, playlist_item_t *p_node,
459                            wxTreeItemId parent )
460 {
461     wxTreeItemId node =
462         treectrl->AppendItem( parent, wxL2U( p_node->input.psz_name ),
463                               -1,-1, new PlaylistItem( p_node ) );
464     treectrl->SetItemImage( node, p_node->input.i_type );
465
466     UpdateNodeChildren( p_playlist, p_node, node );
467 }
468
469 /* Update all children (recursively) of this node */
470 void Playlist::UpdateNodeChildren( playlist_t *p_playlist,
471                                    playlist_item_t *p_node,
472                                    wxTreeItemId node )
473 {
474
475     for( int i = 0; i< p_node->i_children ; i++ )
476     {
477         /* Append the item */
478         if( p_node->pp_children[i]->i_children == -1 )
479         {
480             wxTreeItemId item =
481                 treectrl->AppendItem( node,
482                     wxL2U( p_node->pp_children[i]->input.psz_name ), -1,-1,
483                            new PlaylistItem( p_node->pp_children[i]) );
484
485             UpdateTreeItem( p_playlist, item );
486         }
487         else
488         {
489             CreateNode( p_playlist, p_node->pp_children[i],
490                         node );
491         }
492     }
493 }
494
495 /* Update an item in the tree */
496 void Playlist::UpdateTreeItem( playlist_t *p_playlist, wxTreeItemId item )
497 {
498     if( ! item.IsOk() ) return;
499
500     wxTreeItemData *p_data = treectrl->GetItemData( item );
501     if( !p_data ) return;
502
503     playlist_item_t *p_item = ((PlaylistItem *)p_data)->p_item;
504     if( !p_item ) return;
505
506     wxString msg;
507     wxString duration = wxU( "" );
508     char *psz_author = vlc_input_item_GetInfo( &p_item->input,
509                                                      _("Meta-information"),
510                                                      _("Artist"));
511     char psz_duration[MSTRTIME_MAX_SIZE];
512     mtime_t dur = p_item->input.i_duration;
513
514     if( dur != -1 )
515     {
516         secstotimestr( psz_duration, dur/1000000 );
517         duration.Append( wxU( " ( " ) +  wxString( wxU( psz_duration ) ) +
518                          wxU( " )" ) );
519     }
520
521     if( !strcmp( psz_author, "" ) || p_item->input.b_fixed_name == VLC_TRUE )
522     {
523         msg.Printf( wxString( wxU( p_item->input.psz_name ) ) + duration );
524     }
525     else
526     {
527         msg.Printf( wxString(wxU( psz_author )) + wxT(" - ") +
528                     wxString(wxU(p_item->input.psz_name)) + duration );
529     }
530     treectrl->SetItemText( item , msg );
531     treectrl->SetItemImage( item, p_item->input.i_type );
532
533     if( p_playlist->status.p_item == p_item )
534     {
535         treectrl->SetItemBold( item, true );
536         treectrl->EnsureVisible( item );
537     }
538     else
539     {
540         treectrl->SetItemBold( item, false );
541     }
542 }
543
544 /* Process a AppendItem request */
545 void Playlist::AppendItem( wxCommandEvent& event )
546 {
547     playlist_add_t *p_add = (playlist_add_t *)event.GetClientData();
548
549     playlist_t *p_playlist =
550         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
551                                        FIND_ANYWHERE );
552     wxTreeItemId item,node;
553     if( p_playlist == NULL )
554     {
555         event.Skip();
556         return;
557     }
558
559     if( p_add->i_view != i_current_view )
560     {
561         goto update;
562     }
563
564     node = FindItem( treectrl->GetRootItem(), p_add->p_node );
565     if( !node.IsOk() )
566     {
567         goto update;
568     }
569
570     item = treectrl->AppendItem( node,
571                                  wxL2U( p_add->p_item->input.psz_name ), -1,-1,
572                                  new PlaylistItem( p_add->p_item ) );
573     treectrl->SetItemImage( item, p_add->p_item->input.i_type );
574
575     if( item.IsOk() && p_add->p_item->i_children == -1 )
576     {
577         UpdateTreeItem( p_playlist, item );
578     }
579
580 update:
581     int i_count = CountItems( treectrl->GetRootItem());
582     if( i_count != p_playlist->i_size )
583     {
584         statusbar->SetStatusText( wxString::Format( wxU(_(
585                                   "%i items in playlist (%i not shown)")),
586                                   p_playlist->i_size,
587                                   p_playlist->i_size - i_count ) );
588         if( !b_changed_view )
589         {
590             i_current_view = VIEW_CATEGORY;
591             b_changed_view = VLC_TRUE;
592             b_need_update = VLC_TRUE;
593         }
594     }
595     else
596     {
597         statusbar->SetStatusText( wxString::Format( wxU(_(
598                                   "%i items in playlist")),
599                                   p_playlist->i_size ), 0 );
600     }
601
602     vlc_object_release( p_playlist );
603     return;
604 }
605
606 /* Process a updateitem request */
607 void Playlist::UpdateItem( int i )
608 {
609     if( i < 0 ) return; /* Sanity check */
610     playlist_item_t *p_item;
611
612     playlist_t *p_playlist =
613         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
614                                        FIND_ANYWHERE );
615
616     if( p_playlist == NULL )
617     {
618         return;
619     }
620
621     p_item = playlist_LockItemGetById( p_playlist, i );
622
623     wxTreeItemId item = FindItem( treectrl->GetRootItem(), p_item);
624
625     if( item.IsOk() )
626     {
627         UpdateTreeItem( p_playlist, item );
628     }
629
630     vlc_object_release(p_playlist);
631 }
632
633 void Playlist::RemoveItem( int i )
634 {
635     if( i <= 0 ) return; /* Sanity check */
636
637     wxTreeItemId item = FindItem( treectrl->GetRootItem(), i );
638
639     if( item.IsOk() )
640     {
641         treectrl->Delete( item );
642     }
643 }
644
645
646 /**********************************************************************
647  * Search functions (internal)
648  **********************************************************************/
649
650 /* Find a wxItem from a playlist_item */
651 wxTreeItemId Playlist::FindItem( wxTreeItemId root, playlist_item_t *p_item )
652 {
653     long cookie;
654     PlaylistItem *p_wxcurrent;
655     wxTreeItemId search;
656     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
657     wxTreeItemId child;
658
659     if( p_item == p_saved_item && saved_tree_item.IsOk() )
660     {
661         return saved_tree_item;
662     }
663
664     p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
665
666     if( !p_item || !p_wxcurrent )
667     {
668         wxTreeItemId dummy;
669         return dummy;
670     }
671
672     if( p_wxcurrent->p_item == p_item )
673     {
674         return root;
675     }
676
677     while( item.IsOk() )
678     {
679         p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
680         if( p_wxcurrent->p_item == p_item )
681         {
682             saved_tree_item = item;
683             p_saved_item = p_item;
684             return item;
685         }
686         if( treectrl->ItemHasChildren( item ) )
687         {
688             wxTreeItemId search = FindItem( item, p_item );
689             if( search.IsOk() )
690             {
691                 saved_tree_item = search;
692                 p_saved_item = p_item;
693                 return search;
694             }
695         }
696         item = treectrl->GetNextChild( root, cookie );
697     }
698     /* Not found */
699     wxTreeItemId dummy;
700     return dummy;
701 }
702 /* Find a wxItem from a playlist id */
703 wxTreeItemId Playlist::FindItem( wxTreeItemId root, int i_id )
704 {
705     long cookie;
706     PlaylistItem *p_wxcurrent;
707     wxTreeItemId search;
708     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
709     wxTreeItemId child;
710
711     p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( root );
712
713     if( i_id < 0 )
714     {
715         wxTreeItemId dummy;
716         return dummy;
717     }
718
719     if( !p_wxcurrent )
720     {
721         wxTreeItemId dummy;
722         return dummy;
723     }        
724
725     if( p_wxcurrent->i_id == i_id )
726     {
727         return root;
728     }
729
730     while( item.IsOk() )
731     {
732         p_wxcurrent = (PlaylistItem *)treectrl->GetItemData( item );
733         if( p_wxcurrent->i_id == i_id )
734         {
735             return item;
736         }
737         if( treectrl->ItemHasChildren( item ) )
738         {
739             wxTreeItemId search = FindItem( item, i_id );
740             if( search.IsOk() )
741             {
742                 return search;
743             }
744         }
745         item = treectrl->GetNextChild( root, cookie );
746     }
747     /* Not found */
748     wxTreeItemId dummy;
749     return dummy;
750 }
751
752 int Playlist::CountItems( wxTreeItemId root )
753 {
754     long cookie;
755     int count = 0;
756     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
757
758     while( item.IsOk() )
759     {
760         if( treectrl->ItemHasChildren( item ) )
761         {
762             count += CountItems( item );
763         }
764         else if( ( (PlaylistItem *)treectrl->GetItemData( item ) )->
765                             p_item->i_children == -1 )
766             count++;
767         item = treectrl->GetNextChild( root, cookie );
768     }
769     return count;
770 }
771
772 /* Find a wxItem from a name (from current) */
773 wxTreeItemId Playlist::FindItemByName( wxTreeItemId root, wxString search_string, wxTreeItemId current, vlc_bool_t *pb_current_found )
774 {
775     long cookie;
776     wxTreeItemId search;
777     wxTreeItemId item = treectrl->GetFirstChild( root, cookie );
778     wxTreeItemId child;
779
780     while( item.IsOk() )
781     {
782         if( treectrl->GetItemText( item).Lower().Contains(
783                                                  search_string.Lower() ) )
784         {
785             if( !current.IsOk() || *pb_current_found == VLC_TRUE )
786             {
787                 return item;
788             }
789             else if( current.IsOk() && item == current )
790             {
791                 *pb_current_found = VLC_TRUE;
792             }
793         }
794         if( treectrl->ItemHasChildren( item ) )
795         {
796             wxTreeItemId search = FindItemByName( item, search_string, current,
797                                                   pb_current_found );
798             if( search.IsOk() )
799             {
800                 return search;
801             }
802         }
803         item = treectrl->GetNextChild( root, cookie);
804     }
805     /* Not found */
806     wxTreeItemId dummy;
807     return dummy;
808 }
809
810 /**********************************************************************
811  * Rebuild the playlist
812  **********************************************************************/
813 void Playlist::Rebuild( vlc_bool_t b_root )
814 {
815     playlist_view_t *p_view;
816     playlist_t *p_playlist =
817         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
818                                        FIND_ANYWHERE );
819     if( p_playlist == NULL )
820     {
821         return;
822     }
823
824     /* We can remove the callbacks before locking, anyway, we won't
825      * miss anything */
826     if( b_root )
827     {
828         var_DelCallback( p_playlist, "item-change", ItemChanged, this );
829         var_DelCallback( p_playlist, "playlist-current", PlaylistNext, this );
830         var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
831         var_DelCallback( p_playlist, "item-append", ItemAppended, this );
832         var_DelCallback( p_playlist, "item-deleted", ItemDeleted, this );
833
834         /* ...and rebuild it */
835         vlc_mutex_lock( &p_playlist->object_lock );
836     }
837
838     p_view = playlist_ViewFind( p_playlist, i_current_view ); /* FIXME */
839
840     /* HACK we should really get new*/
841     treectrl->DeleteAllItems();
842     treectrl->AddRoot( wxU(_("root" )), -1, -1,
843                          new PlaylistItem( p_view->p_root) );
844
845     wxTreeItemId root = treectrl->GetRootItem();
846     UpdateNode( p_playlist, p_view->p_root, root );
847
848     int i_count = CountItems( treectrl->GetRootItem() );
849
850     if( i_count < p_playlist->i_size && !b_changed_view )
851     {
852         i_current_view = VIEW_CATEGORY;
853         b_changed_view = VLC_TRUE;
854         Rebuild( VLC_FALSE );
855     }
856     else if( i_count != p_playlist->i_size )
857     {
858         statusbar->SetStatusText( wxString::Format( wxU(_(
859                                   "%i items in playlist (%i not shown)")),
860                                   p_playlist->i_size,
861                                   p_playlist->i_size - i_count ) );
862     }
863     else
864     {
865         statusbar->SetStatusText( wxString::Format( wxU(_(
866                                   "%i items in playlist")),
867                                   p_playlist->i_size ), 0 );
868     }
869
870     if( b_root )
871     {
872         /* Put callbacks back online */
873         var_AddCallback( p_playlist, "intf-change", PlaylistChanged, this );
874         var_AddCallback( p_playlist, "playlist-current", PlaylistNext, this );
875         var_AddCallback( p_playlist, "item-change", ItemChanged, this );
876         var_AddCallback( p_playlist, "item-append", ItemAppended, this );
877         var_AddCallback( p_playlist, "item-deleted", ItemDeleted, this );
878
879         vlc_mutex_unlock( &p_playlist->object_lock );
880     }
881     vlc_object_release( p_playlist );
882 }
883
884
885
886 void Playlist::ShowPlaylist( bool show )
887 {
888     if( show ) Rebuild( VLC_TRUE );
889     Show( show );
890 }
891
892 /* This function is called on a regular basis */
893 void Playlist::UpdatePlaylist()
894 {
895     i_update_counter++;
896
897     /* If the playlist isn't show there's no need to update it */
898     if( !IsShown() ) return;
899
900     if( this->b_need_update )
901     {
902         this->b_need_update = VLC_FALSE;
903         Rebuild( VLC_TRUE );
904     }
905
906     /* Updating the playing status every 0.5s is enough */
907     if( i_update_counter % 5 ) return;
908
909     playlist_t *p_playlist =
910         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
911                                        FIND_ANYWHERE );
912     if( p_playlist == NULL )
913     {
914         return;
915     }
916
917     vlc_object_release( p_playlist );
918 }
919
920 /*****************************************************************************
921  * Private methods.
922  *****************************************************************************/
923 void Playlist::DeleteItem( int item_id )
924 {
925     playlist_t *p_playlist =
926         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
927                                        FIND_ANYWHERE );
928     if( p_playlist == NULL )
929     {
930         return;
931     }
932
933     playlist_LockDelete( p_playlist, item_id );
934
935     vlc_object_release( p_playlist );
936 }
937
938 void Playlist::DeleteNode( playlist_item_t *p_item )
939 {
940     playlist_t *p_playlist =
941         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
942                                        FIND_ANYWHERE );
943     if( p_playlist == NULL )
944     {
945         return;
946     }
947
948     playlist_NodeDelete( p_playlist, p_item, VLC_TRUE , VLC_FALSE );
949
950     vlc_object_release( p_playlist );
951 }
952
953
954 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
955 {
956     Hide();
957 }
958
959 void Playlist::OnSave( wxCommandEvent& WXUNUSED(event) )
960 {
961     struct {
962         char *psz_desc;
963         char *psz_filter;
964         char *psz_module;
965     } formats[] = {{ _("M3U file"), "*.m3u", "export-m3u" },
966                    { _("PLS file"), "*.pls", "export-pls" }};
967
968     wxString filter = wxT("");
969
970     playlist_t * p_playlist =
971                 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
972                                                FIND_ANYWHERE );
973
974     if( ! p_playlist )
975     {
976         return;
977     }
978     if( p_playlist->i_size == 0 )
979     {
980         wxMessageBox( wxU(_("Playlist is empty") ), wxU(_("Can't save")),
981                       wxICON_WARNING | wxOK, this );
982         vlc_object_release( p_playlist );
983         return;
984     }
985
986     for( unsigned int i = 0; i < sizeof(formats)/sizeof(formats[0]); i++)
987     {
988         filter.Append( wxU(formats[i].psz_desc) );
989         filter.Append( wxT("|") );
990         filter.Append( wxU(formats[i].psz_filter) );
991         filter.Append( wxT("|") );
992     }
993     wxFileDialog dialog( this, wxU(_("Save playlist")),
994                          wxT(""), wxT(""), filter, wxSAVE );
995
996     if( dialog.ShowModal() == wxID_OK )
997     {
998         if( dialog.GetPath().mb_str() )
999         {
1000             playlist_Export( p_playlist, dialog.GetPath().mb_str(),
1001                              formats[dialog.GetFilterIndex()].psz_module );
1002         }
1003     }
1004
1005     vlc_object_release( p_playlist );
1006
1007 }
1008
1009 void Playlist::OnOpen( wxCommandEvent& WXUNUSED(event) )
1010 {
1011     playlist_t *p_playlist =
1012         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1013                                        FIND_ANYWHERE );
1014     if( p_playlist == NULL )
1015     {
1016         return;
1017     }
1018
1019     wxFileDialog dialog( this, wxU(_("Open playlist")), wxT(""), wxT(""),
1020         wxT("All playlists|*.pls;*.m3u;*.asx;*.b4s|M3U files|*.m3u"), wxOPEN );
1021
1022     if( dialog.ShowModal() == wxID_OK )
1023     {
1024         playlist_Import( p_playlist, dialog.GetPath().mb_str() );
1025     }
1026
1027     vlc_object_release( p_playlist );
1028 }
1029
1030 void Playlist::OnAddFile( wxCommandEvent& WXUNUSED(event) )
1031 {
1032     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE_SIMPLE, 0, 0 );
1033
1034 }
1035
1036 void Playlist::OnAddDir( wxCommandEvent& WXUNUSED(event) )
1037 {
1038     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_DIRECTORY, 0, 0 );
1039
1040 }
1041
1042 void Playlist::OnAddMRL( wxCommandEvent& WXUNUSED(event) )
1043 {
1044     p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_FILE, 0, 0 );
1045
1046 }
1047
1048 /********************************************************************
1049  * Sorting functions
1050  ********************************************************************/
1051 void Playlist::OnSort( wxCommandEvent& event )
1052 {
1053     playlist_t *p_playlist =
1054         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1055                                        FIND_ANYWHERE );
1056     PlaylistItem *p_wxitem;
1057     p_wxitem = (PlaylistItem *)treectrl->GetItemData( treectrl->GetRootItem() );
1058
1059     if( p_playlist == NULL )
1060     {
1061         return;
1062     }
1063     vlc_mutex_lock( &p_playlist->object_lock );
1064     switch( event.GetId() )
1065     {
1066         case SortTitle_Event:
1067             playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1068                                         SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1069             break;
1070         case RSortTitle_Event:
1071             playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1072                                         SORT_TITLE_NODES_FIRST, ORDER_REVERSE );
1073     }
1074     vlc_mutex_unlock( &p_playlist->object_lock );
1075
1076     vlc_object_release( p_playlist );
1077     Rebuild( VLC_TRUE );
1078 }
1079
1080 /**********************************************************************
1081  * Search functions (user)
1082  **********************************************************************/
1083 void Playlist::OnSearchTextChange( wxCommandEvent& WXUNUSED(event) )
1084 {
1085    search_button->SetDefault();
1086 }
1087
1088 void Playlist::OnSearch( wxCommandEvent& WXUNUSED(event) )
1089 {
1090     wxString search_string = search_text->GetValue();
1091
1092     vlc_bool_t pb_found = VLC_FALSE;
1093
1094     wxTreeItemId found =
1095      FindItemByName( treectrl->GetRootItem(), search_string,
1096                      search_current, &pb_found );
1097
1098     if( found.IsOk() )
1099     {
1100         search_current = found;
1101         treectrl->SelectItem( found, true );
1102     }
1103     else
1104     {
1105         wxTreeItemId dummy;
1106         search_current = dummy;
1107         found =  FindItemByName( treectrl->GetRootItem(), search_string,
1108                                  search_current, &pb_found );
1109         if( found.IsOk() )
1110         {
1111             search_current = found;
1112             treectrl->SelectItem( found, true );
1113         }
1114     }
1115 }
1116
1117 /**********************************************************************
1118  * Selection functions
1119  **********************************************************************/
1120 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
1121 {
1122 }
1123
1124 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
1125 {
1126     Rebuild( VLC_TRUE );
1127 }
1128
1129 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
1130 {
1131 }
1132
1133 /**********************************************************************
1134  * Playlist mode functions
1135  **********************************************************************/
1136 void Playlist::OnRandom( wxCommandEvent& event )
1137 {
1138     vlc_value_t val;
1139     val.b_bool = event.IsChecked();
1140     playlist_t *p_playlist =
1141         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1142                                        FIND_ANYWHERE );
1143     if( p_playlist == NULL )
1144     {
1145         return;
1146     }
1147     var_Set( p_playlist, "random", val);
1148     vlc_object_release( p_playlist );
1149 }
1150
1151 void Playlist::OnLoop( wxCommandEvent& event )
1152 {
1153     vlc_value_t val;
1154     val.b_bool = event.IsChecked();
1155     playlist_t *p_playlist =
1156         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1157                                        FIND_ANYWHERE );
1158     if( p_playlist == NULL )
1159     {
1160         return;
1161     }
1162     var_Set( p_playlist, "loop", val);
1163     vlc_object_release( p_playlist );
1164 }
1165
1166 void Playlist::OnRepeat( wxCommandEvent& event )
1167 {
1168     vlc_value_t val;
1169     val.b_bool = event.IsChecked();
1170     playlist_t *p_playlist =
1171         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1172                                        FIND_ANYWHERE );
1173     if( p_playlist == NULL )
1174     {
1175         return;
1176     }
1177     var_Set( p_playlist, "repeat", val);
1178     vlc_object_release( p_playlist );
1179 }
1180
1181 /********************************************************************
1182  * Event
1183  ********************************************************************/
1184 void Playlist::OnActivateItem( wxTreeEvent& event )
1185 {
1186     playlist_item_t *p_item,*p_node;
1187     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1188                                     VLC_OBJECT_PLAYLIST,FIND_ANYWHERE );
1189
1190     PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1191                                                             event.GetItem() );
1192     wxTreeItemId parent = treectrl->GetItemParent( event.GetItem() );
1193
1194     PlaylistItem *p_wxparent = (PlaylistItem *)treectrl->GetItemData( parent );
1195
1196     if( p_playlist == NULL )
1197     {
1198         return;
1199     }
1200
1201     if( p_wxitem->p_item->i_children == -1 )
1202     {
1203         p_node = p_wxparent->p_item;
1204         p_item = p_wxitem->p_item;
1205     }
1206     else
1207     {
1208         p_node = p_wxitem->p_item;
1209         if( p_wxitem->p_item->i_children > 0 &&
1210             p_wxitem->p_item->pp_children[0]->i_children == -1)
1211         {
1212             p_item = p_wxitem->p_item->pp_children[0];
1213         }
1214         else
1215         {
1216             p_item = NULL;
1217         }
1218     }
1219
1220     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, i_current_view,
1221                       p_node, p_item );
1222
1223     vlc_object_release( p_playlist );
1224 }
1225
1226 void Playlist::OnKeyDown( wxTreeEvent& event )
1227 {
1228     long keycode = event.GetKeyCode();
1229     /* Delete selected items */
1230     if( keycode == WXK_BACK || keycode == WXK_DELETE )
1231     {
1232         /* We send a dummy event */
1233         OnDeleteSelection( event );
1234     }
1235 }
1236
1237 void Playlist::OnEnDis( wxCommandEvent& event )
1238 {
1239     playlist_t *p_playlist =
1240         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1241                                        FIND_ANYWHERE );
1242     if( p_playlist == NULL )
1243     {
1244         return;
1245     }
1246     msg_Warn( p_intf, "not implemented" );
1247     vlc_object_release( p_playlist );
1248 }
1249
1250 /**********************************************************************
1251  * Menu
1252  **********************************************************************/
1253
1254 void Playlist::OnMenuOpen( wxMenuEvent& event)
1255 {
1256 #if defined( __WXMSW__ )
1257 #   define GetEventObject GetMenu
1258 #endif
1259
1260     if( event.GetEventObject() == p_view_menu )
1261     {
1262         p_view_menu = ViewMenu();
1263     }
1264 #if defined( __WXMSW__ )
1265 #   undef GetEventObject
1266 #endif
1267 }
1268
1269 void Playlist::OnMenuEvent( wxCommandEvent& event )
1270 {
1271     playlist_t *p_playlist =
1272         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1273                                        FIND_ANYWHERE );
1274     if( p_playlist == NULL )
1275     {
1276         return;
1277     }
1278
1279     if( event.GetId() < FirstView_Event )
1280     {
1281         event.Skip();
1282         vlc_object_release( p_playlist );
1283         return;
1284     }
1285     else if( event.GetId() < LastView_Event )
1286     {
1287
1288         int i_new_view = event.GetId() - FirstView_Event;
1289
1290         playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_new_view );
1291
1292         if( p_view != NULL )
1293         {
1294             b_changed_view = VLC_TRUE;
1295             i_current_view = i_new_view;
1296             playlist_ViewUpdate( p_playlist, i_new_view );
1297             Rebuild( VLC_TRUE );
1298             vlc_object_release( p_playlist );
1299             return;
1300         }
1301         else if( i_new_view >= VIEW_FIRST_SORTED &&
1302                  i_new_view <= VIEW_LAST_SORTED )
1303         {
1304             b_changed_view = VLC_TRUE;
1305             playlist_ViewInsert( p_playlist, i_new_view, "View" );
1306             playlist_ViewUpdate( p_playlist, i_new_view );
1307
1308             i_current_view = i_new_view;
1309
1310             Rebuild( VLC_TRUE );
1311         }
1312     }
1313     else if( event.GetId() >= FirstSD_Event && event.GetId() < LastSD_Event )
1314     {
1315         if( !playlist_IsServicesDiscoveryLoaded( p_playlist,
1316                                 pp_sds[event.GetId() - FirstSD_Event] ) )
1317         {
1318             playlist_ServicesDiscoveryAdd( p_playlist,
1319                             pp_sds[event.GetId() - FirstSD_Event] );
1320         }
1321         else
1322         {
1323             wxMutexGuiLeave();
1324             playlist_ServicesDiscoveryRemove( p_playlist,
1325                             pp_sds[event.GetId() - FirstSD_Event] );
1326             wxMutexGuiEnter();
1327         }
1328     }
1329     vlc_object_release( p_playlist );
1330 }
1331
1332 wxMenu * Playlist::ViewMenu()
1333 {
1334     playlist_t *p_playlist =
1335         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1336                                        FIND_ANYWHERE );
1337     if( p_playlist == NULL )
1338     {
1339         return NULL;
1340     }
1341
1342     if( !p_view_menu )
1343     {
1344         p_view_menu = new wxMenu;
1345     }
1346     else
1347     {
1348         wxMenuItemList::Node *node = p_view_menu->GetMenuItems().GetFirst();
1349         for( ; node; )
1350         {
1351             wxMenuItem *item = node->GetData();
1352             node = node->GetNext();
1353             p_view_menu->Delete( item );
1354         }
1355     }
1356
1357     /* FIXME : have a list of "should have" views */
1358     p_view_menu->Append( FirstView_Event + VIEW_CATEGORY,
1359                            wxU(_("By category") ) );
1360     p_view_menu->Append( FirstView_Event + VIEW_SIMPLE,
1361                            wxU(_("Manually added") ) );
1362     p_view_menu->Append( FirstView_Event + VIEW_ALL,
1363                            wxU(_("All items, unsorted") ) );
1364     p_view_menu->Append( FirstView_Event + VIEW_S_AUTHOR,
1365                            wxU(_("Sorted by author") ) );
1366
1367     vlc_object_release( p_playlist);
1368
1369     return p_view_menu;
1370 }
1371
1372 wxMenu *Playlist::SDMenu()
1373 {
1374
1375     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1376                               VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1377     if( !p_playlist )
1378     {
1379         return NULL;
1380     }
1381     p_sd_menu = new wxMenu;
1382
1383     vlc_list_t *p_list = vlc_list_find( p_playlist, VLC_OBJECT_MODULE,
1384                                         FIND_ANYWHERE );
1385
1386     int i_number = 0;
1387     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1388     {
1389         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1390
1391         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1392             i_number++;
1393     }
1394     if( i_number ) pp_sds = (char **)malloc( i_number * sizeof(void *) );
1395
1396     i_number = 0;
1397     for( int i_index = 0; i_index < p_list->i_count; i_index++ )
1398     {
1399         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1400
1401         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
1402         {
1403             p_sd_menu->AppendCheckItem( FirstSD_Event + i_number ,
1404                 wxU( p_parser->psz_longname ? p_parser->psz_longname :
1405                      (p_parser->psz_shortname ?
1406                       p_parser->psz_shortname : p_parser->psz_object_name) ) );
1407
1408             if( playlist_IsServicesDiscoveryLoaded( p_playlist,
1409                                     p_parser->psz_object_name ) )
1410             {
1411                 p_sd_menu->Check( FirstSD_Event + i_number, TRUE );
1412             }
1413
1414             pp_sds[i_number++] = p_parser->psz_object_name;
1415         }
1416     }
1417     vlc_list_release( p_list );
1418     vlc_object_release( p_playlist );
1419     return p_sd_menu;
1420 }
1421
1422
1423 /*****************************************************************************
1424  * Popup management functions
1425  *****************************************************************************/
1426 void Playlist::OnPopup( wxContextMenuEvent& event )
1427 {
1428     wxPoint pt = event.GetPosition();
1429
1430     i_popup_item = treectrl->HitTest( ScreenToClient( pt ) );
1431     if( i_popup_item.IsOk() )
1432     {
1433         PlaylistItem *p_wxitem = (PlaylistItem *)treectrl->GetItemData(
1434                                                             i_popup_item );
1435         PlaylistItem *p_wxparent= (PlaylistItem *) treectrl->GetItemData(
1436                                    treectrl->GetItemParent( i_popup_item ) );
1437         p_popup_item = p_wxitem->p_item;
1438         p_popup_parent = p_wxparent->p_item;
1439         treectrl->SelectItem( i_popup_item );
1440         if( p_popup_item->i_children == -1 )
1441             Playlist::PopupMenu( item_popup,
1442                                  ScreenToClient( wxGetMousePosition() ) );
1443         else
1444             Playlist::PopupMenu( node_popup,
1445                                  ScreenToClient( wxGetMousePosition() ) );
1446     }
1447 }
1448
1449 void Playlist::OnPopupPlay( wxMenuEvent& event )
1450 {
1451     playlist_t *p_playlist =
1452         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1453                                        FIND_ANYWHERE );
1454     if( p_playlist == NULL )
1455     {
1456         return;
1457     }
1458     if( p_popup_item != NULL )
1459     {
1460         if( p_popup_item->i_children > -1 )
1461         {
1462             if( event.GetId() == PopupPlay_Event &&
1463                 p_popup_item->i_children > 0 )
1464             {
1465                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1466                                   i_current_view, p_popup_item,
1467                                   p_popup_item->pp_children[0] );
1468             }
1469             else
1470             {
1471                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1472                                   i_current_view, p_popup_item, NULL );
1473             }
1474         }
1475         else
1476         {
1477             if( event.GetId() == PopupPlay_Event )
1478             {
1479                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
1480                                   i_current_view, p_popup_parent,
1481                                   p_popup_item );
1482             }
1483         }
1484     }
1485     vlc_object_release( p_playlist );
1486 }
1487
1488 void Playlist::OnPopupPreparse( wxMenuEvent& event )
1489 {
1490     playlist_t *p_playlist =
1491         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1492                                        FIND_ANYWHERE );
1493     if( p_playlist == NULL )
1494     {
1495         return;
1496     }
1497     Preparse( p_playlist );
1498     vlc_object_release( p_playlist );
1499 }
1500
1501 void Playlist::Preparse( playlist_t *p_playlist )
1502 {
1503     if( p_popup_item != NULL )
1504     {
1505         if( p_popup_item->i_children == -1 )
1506         {
1507             playlist_PreparseEnqueue( p_playlist, &p_popup_item->input );
1508         }
1509         else
1510         {
1511             int i = 0;
1512             playlist_item_t *p_parent = p_popup_item;
1513             for( i = 0; i< p_parent->i_children ; i++ )
1514             {
1515                 wxMenuEvent dummy;
1516                 i_popup_item = FindItem( treectrl->GetRootItem(),
1517                                          p_parent->pp_children[i] );
1518                 p_popup_item = p_parent->pp_children[i];
1519                 Preparse( p_playlist );
1520             }
1521         }
1522     }
1523 }
1524
1525 void Playlist::OnPopupDel( wxMenuEvent& event )
1526 {
1527     PlaylistItem *p_wxitem;
1528
1529     p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1530
1531     if( p_wxitem->p_item->i_children == -1 )
1532     {
1533         DeleteItem( p_wxitem->p_item->input.i_id );
1534     }
1535     else
1536     {
1537         DeleteNode( p_wxitem->p_item );
1538     }
1539 }
1540
1541 void Playlist::OnPopupSort( wxMenuEvent& event )
1542 {
1543     PlaylistItem *p_wxitem;
1544
1545     p_wxitem = (PlaylistItem *)treectrl->GetItemData( i_popup_item );
1546
1547     if( p_wxitem->p_item->i_children >= 0 )
1548     {
1549         playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
1550                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1551
1552         if( p_playlist )
1553         {
1554             vlc_mutex_lock( &p_playlist->object_lock );
1555             playlist_RecursiveNodeSort( p_playlist, p_wxitem->p_item,
1556                                         SORT_TITLE_NODES_FIRST, ORDER_NORMAL );
1557             vlc_mutex_unlock( &p_playlist->object_lock );
1558
1559             treectrl->DeleteChildren( i_popup_item );
1560             UpdateNodeChildren( p_playlist, p_wxitem->p_item, i_popup_item );
1561
1562             vlc_object_release( p_playlist );
1563         }
1564     }
1565 }
1566
1567 void Playlist::OnPopupInfo( wxMenuEvent& event )
1568 {
1569     if( p_popup_item )
1570     {
1571         iteminfo_dialog = new ItemInfoDialog( p_intf, p_popup_item, this );
1572         if( iteminfo_dialog->ShowModal() == wxID_OK )
1573         {
1574             UpdateItem( i_popup_item );
1575         }
1576         delete iteminfo_dialog;
1577     }
1578 }
1579
1580
1581 /*****************************************************************************
1582  * Custom events management
1583  *****************************************************************************/
1584 void Playlist::OnPlaylistEvent( wxCommandEvent& event )
1585 {
1586     switch( event.GetId() )
1587     {
1588         case UpdateItem_Event:
1589             UpdateItem( event.GetInt() );
1590             break;
1591         case AppendItem_Event:
1592             AppendItem( event );
1593             break;
1594         case RemoveItem_Event:
1595             RemoveItem( event.GetInt() );
1596             break;
1597     }
1598 }
1599
1600 /*****************************************************************************
1601  * PlaylistChanged: callback triggered by the intf-change playlist variable
1602  *  We don't rebuild the playlist directly here because we don't want the
1603  *  caller to block for a too long time.
1604  *****************************************************************************/
1605 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1606                             vlc_value_t oval, vlc_value_t nval, void *param )
1607 {
1608     Playlist *p_playlist_dialog = (Playlist *)param;
1609     p_playlist_dialog->b_need_update = VLC_TRUE;
1610     return VLC_SUCCESS;
1611 }
1612
1613 /*****************************************************************************
1614  * Next: callback triggered by the playlist-current playlist variable
1615  *****************************************************************************/
1616 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
1617                          vlc_value_t oval, vlc_value_t nval, void *param )
1618 {
1619     Playlist *p_playlist_dialog = (Playlist *)param;
1620
1621     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1622     event.SetInt( oval.i_int );
1623     p_playlist_dialog->AddPendingEvent( event );
1624     event.SetInt( nval.i_int );
1625     p_playlist_dialog->AddPendingEvent( event );
1626
1627     return 0;
1628 }
1629
1630 /*****************************************************************************
1631  * ItemChanged: callback triggered by the item-change playlist variable
1632  *****************************************************************************/
1633 static int ItemChanged( vlc_object_t *p_this, const char *psz_variable,
1634                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1635 {
1636     Playlist *p_playlist_dialog = (Playlist *)param;
1637
1638     wxCommandEvent event( wxEVT_PLAYLIST, UpdateItem_Event );
1639     event.SetInt( new_val.i_int );
1640     p_playlist_dialog->AddPendingEvent( event );
1641
1642     return 0;
1643 }
1644 static int ItemDeleted( vlc_object_t *p_this, const char *psz_variable,
1645                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1646 {
1647     Playlist *p_playlist_dialog = (Playlist *)param;
1648
1649     wxCommandEvent event( wxEVT_PLAYLIST, RemoveItem_Event );
1650     event.SetInt( new_val.i_int );
1651     p_playlist_dialog->AddPendingEvent( event );
1652
1653     return 0;
1654 }
1655
1656 static int ItemAppended( vlc_object_t *p_this, const char *psz_variable,
1657                          vlc_value_t oval, vlc_value_t nval, void *param )
1658 {
1659     Playlist *p_playlist_dialog = (Playlist *)param;
1660
1661     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
1662
1663     memcpy( p_add, nval.p_address, sizeof( playlist_add_t ) );
1664
1665     wxCommandEvent event( wxEVT_PLAYLIST, AppendItem_Event );
1666     event.SetClientData( (void *)p_add );
1667     p_playlist_dialog->AddPendingEvent( event );
1668
1669     return VLC_SUCCESS;
1670 }