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