* modules/gui/skins/src/event.cpp: Event::CreateEvent() was trying to free
[vlc.git] / modules / gui / wxwindows / playlist.cpp
1 /*****************************************************************************
2  * playlist.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: playlist.cpp,v 1.6 2003/03/26 00:56:22 gbazin Exp $
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33
34 #ifdef WIN32                                                 /* mingw32 hack */
35 #undef Yield
36 #undef CreateDialog
37 #endif
38
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
41
42 #include <wx/wxprec.h>
43 #include <wx/wx.h>
44 #include <wx/listctrl.h>
45
46 #include <vlc/intf.h>
47
48 #include "wxwindows.h"
49
50 /*****************************************************************************
51  * Event Table.
52  *****************************************************************************/
53
54 /* IDs for the controls and the menu commands */
55 enum
56 {
57     /* menu items */
58     AddUrl_Event = 1,
59     AddDirectory_Event,
60     Close_Event,
61
62     InvertSelection_Event,
63     DeleteSelection_Event,
64     SelectAll_Event,
65
66     /* controls */
67     ListView_Event
68 };
69
70 BEGIN_EVENT_TABLE(Playlist, wxFrame)
71     /* Menu events */
72     EVT_MENU(AddUrl_Event, Playlist::OnAddUrl)
73     EVT_MENU(AddDirectory_Event, Playlist::OnAddDirectory)
74     EVT_MENU(Close_Event, Playlist::OnClose)
75     EVT_MENU(InvertSelection_Event, Playlist::OnInvertSelection)
76     EVT_MENU(DeleteSelection_Event, Playlist::OnDeleteSelection)
77     EVT_MENU(SelectAll_Event, Playlist::OnSelectAll)
78
79     /* Listview events */
80     EVT_LIST_ITEM_ACTIVATED(ListView_Event, Playlist::OnActivateItem)
81     EVT_LIST_KEY_DOWN(ListView_Event, Playlist::OnKeyDown)
82
83     /* Button events */
84     EVT_BUTTON( wxID_OK, Playlist::OnClose)
85
86     /* Special events : we don't want to destroy the window when the user
87      * clicks on (X) */
88     EVT_CLOSE(Playlist::OnClose)
89 END_EVENT_TABLE()
90
91 /*****************************************************************************
92  * Constructor.
93  *****************************************************************************/
94 Playlist::Playlist( intf_thread_t *_p_intf, Interface *_p_main_interface ):
95     wxFrame( _p_main_interface, -1, "Playlist", wxDefaultPosition,
96              wxSize::wxSize( 400, 500 ), wxDEFAULT_FRAME_STYLE )
97 {
98     /* Initializations */
99     p_intf = _p_intf;
100     p_main_interface = _p_main_interface;
101
102     /* Create our "Manage" menu */
103     wxMenu *manage_menu = new wxMenu;
104     manage_menu->Append( AddUrl_Event, _("Add &Url...") );
105     manage_menu->Append( AddDirectory_Event, _("Add &Directory...") );
106     manage_menu->AppendSeparator();
107     manage_menu->Append( Close_Event, _("&Close") );
108
109     /* Create our "Selection" menu */
110     wxMenu *selection_menu = new wxMenu;
111     selection_menu->Append( InvertSelection_Event, _("&Invert") );
112     selection_menu->Append( DeleteSelection_Event, _("&Delete") );
113     selection_menu->Append( SelectAll_Event, _("&Select All") );
114
115     /* Append the freshly created menus to the menu bar */
116     wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
117     menubar->Append( manage_menu, _("&Manage") );
118     menubar->Append( selection_menu, _("&Selection") );
119
120     /* Attach the menu bar to the frame */
121     SetMenuBar( menubar );
122
123     /* Create a panel to put everything in */
124     wxPanel *playlist_panel = new wxPanel( this, -1 );
125     playlist_panel->SetAutoLayout( TRUE );
126
127     /* Create the listview */
128     /* FIXME: the given size is arbitrary, and prevents us from resizing
129      * the window to smaller dimensions. But the sizers don't seem to adjust
130      * themselves to the size of a listview, and with a wxDefaultSize the
131      * playlist window is ridiculously small */
132     listview = new wxListView( playlist_panel, ListView_Event,
133                                wxDefaultPosition, wxSize( 350, 300 ),
134                                wxLC_REPORT | wxSUNKEN_BORDER );
135     listview->InsertColumn( 0, _("Url") );
136     listview->InsertColumn( 1, _("Duration") );
137     listview->SetColumnWidth( 0, 250 );
138     listview->SetColumnWidth( 1, 100 );
139
140     /* Create the OK button */
141     ok_button = new wxButton( playlist_panel, wxID_OK, _("OK") );
142     ok_button->SetDefault();
143
144     /* Place everything in sizers */
145     wxBoxSizer *ok_button_sizer = new wxBoxSizer( wxHORIZONTAL );
146     ok_button_sizer->Add( ok_button, 0, wxALL, 5 );
147     ok_button_sizer->Layout();
148     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
149     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
150     panel_sizer->Add( listview, 1, wxEXPAND | wxALL, 5 );
151     panel_sizer->Add( ok_button_sizer, 0, wxALIGN_CENTRE );
152     panel_sizer->Layout();
153     playlist_panel->SetSizerAndFit( panel_sizer );
154     main_sizer->Add( playlist_panel, 1, wxGROW, 0 );
155     main_sizer->Layout();
156     SetSizerAndFit( main_sizer );
157
158 #if !defined(__WXX11__)
159     /* Associate drop targets with the playlist */
160     SetDropTarget( new DragAndDrop( p_intf ) );
161 #endif
162
163     /* Update the playlist */
164     Rebuild();
165
166 }
167
168 Playlist::~Playlist()
169 {
170 }
171
172 void Playlist::Rebuild()
173 {
174     playlist_t *p_playlist =
175         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
176                                        FIND_ANYWHERE );
177     if( p_playlist == NULL )
178     {
179         return;
180     }
181
182     /* Clear the list... */
183     listview->DeleteAllItems();
184
185     /* ...and rebuild it */
186     vlc_mutex_lock( &p_playlist->object_lock );
187     for( int i = 0; i < p_playlist->i_size; i++ )
188     {
189         wxString filename = p_playlist->pp_items[i]->psz_name;
190         listview->InsertItem( i, filename );
191         /* FIXME: we should try to find the actual duration... */
192         listview->SetItem( i, 1, _("no info") );
193     }
194     vlc_mutex_unlock( &p_playlist->object_lock );
195
196     /* Change the colour for the currenty played stream */
197     wxListItem listitem;
198     listitem.m_itemId = p_playlist->i_index;
199     listitem.SetTextColour( *wxRED );
200     listview->SetItem( listitem );
201
202     vlc_object_release( p_playlist );
203 }
204
205 /* Update the colour of items */
206 void Playlist::Manage()
207 {
208     playlist_t *p_playlist =
209         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
210                                        FIND_ANYWHERE );
211     if( p_playlist == NULL )
212     {
213         return;
214     }
215
216     vlc_mutex_lock( &p_playlist->object_lock );
217     if( p_intf->p_sys->i_playing != p_playlist->i_index )
218     {
219         wxListItem listitem;
220         listitem.m_itemId = p_playlist->i_index;
221         listitem.SetTextColour( *wxRED );
222         listview->SetItem( listitem );
223
224         if( p_intf->p_sys->i_playing != -1 )
225         {
226             listitem.m_itemId = p_intf->p_sys->i_playing;
227             listitem.SetTextColour( *wxBLACK );
228             listview->SetItem( listitem );
229         }
230         p_intf->p_sys->i_playing = p_playlist->i_index;
231     }
232     vlc_mutex_unlock( &p_playlist->object_lock );
233
234     vlc_object_release( p_playlist );
235 }
236
237 /*****************************************************************************
238  * Private methods.
239  *****************************************************************************/
240 void Playlist::DeleteItem( int item )
241 {
242     playlist_t *p_playlist =
243         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
244                                        FIND_ANYWHERE );
245     if( p_playlist == NULL )
246     {
247         return;
248     }
249
250     playlist_Delete( p_playlist, item );
251     listview->DeleteItem( item );
252
253     vlc_object_release( p_playlist );
254 }
255
256 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
257 {
258     Hide();
259 }
260
261 void Playlist::OnAddUrl( wxCommandEvent& WXUNUSED(event) )
262 {
263     /* TODO */
264 }
265
266 void Playlist::OnAddDirectory( wxCommandEvent& WXUNUSED(event) )
267 {
268     /* TODO */
269 }
270
271 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
272 {
273     for( long item = 0; item < listview->GetItemCount(); item++ )
274     {
275         listview->Select( item, ! listview->IsSelected( item ) );
276     }
277 }
278
279 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
280 {
281     /* Delete from the end to the beginning, to avoid a shift of indices */
282     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
283     {
284         if( listview->IsSelected( item ) )
285         {
286             DeleteItem( item );
287         }
288     }
289
290     Rebuild();
291 }
292
293 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
294 {
295     for( long item = 0; item < listview->GetItemCount(); item++ )
296     {
297         listview->Select( item, TRUE );
298     }
299 }
300
301 void Playlist::OnActivateItem( wxListEvent& event )
302 {
303     playlist_t *p_playlist =
304         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
305                                        FIND_ANYWHERE );
306     if( p_playlist == NULL )
307     {
308         return;
309     }
310
311     playlist_Goto( p_playlist, event.GetIndex() );
312
313     vlc_object_release( p_playlist );
314 }
315
316 void Playlist::OnKeyDown( wxListEvent& event )
317 {
318     long keycode = event.GetKeyCode();
319     /* Delete selected items */
320     if( keycode == WXK_BACK || keycode == WXK_DELETE )
321     {
322         /* We send a dummy event */
323         OnDeleteSelection( event );
324     }
325 }