]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/playlist.cpp
0556bfd5ba0e6d574f5ccd321db92ee0fd435841
[vlc] / 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.4 2002/11/23 18:42:59 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,
134                                wxSize( 350, 300 ), wxLC_REPORT );
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     /* Associate drop targets with the playlist */
159     SetDropTarget( new DragAndDrop( p_intf ) );
160
161     /* Update the playlist */
162     Rebuild();
163
164 }
165
166 Playlist::~Playlist()
167 {
168 }
169
170 void Playlist::Rebuild()
171 {
172     playlist_t *p_playlist =
173         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
174                                        FIND_ANYWHERE );
175     if( p_playlist == NULL )
176     {
177         return;
178     }
179
180     /* Clear the list... */
181     listview->DeleteAllItems();
182
183     /* ...and rebuild it */
184     vlc_mutex_lock( &p_playlist->object_lock );
185     for( int i = 0; i < p_playlist->i_size; i++ )
186     {
187         wxString filename = p_playlist->pp_items[i]->psz_name;
188         listview->InsertItem( i, filename );
189         /* FIXME: we should try to find the actual duration... */
190         listview->SetItem( i, 1, _("no info") );
191     }
192     vlc_mutex_unlock( &p_playlist->object_lock );
193
194     /* Change the colour for the currenty played stream */
195     wxListItem listitem;
196     listitem.m_itemId = p_playlist->i_index;
197     listitem.SetTextColour( *wxRED );
198     listview->SetItem( listitem );
199
200     vlc_object_release( p_playlist );
201 }
202
203 /* Update the colour of items */
204 void Playlist::Manage()
205 {
206     playlist_t *p_playlist =
207         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
208                                        FIND_ANYWHERE );
209     if( p_playlist == NULL )
210     {
211         return;
212     }
213
214     vlc_mutex_lock( &p_playlist->object_lock );
215     if( p_intf->p_sys->i_playing != p_playlist->i_index )
216     {
217         wxListItem listitem;
218         listitem.m_itemId = p_playlist->i_index;
219         listitem.SetTextColour( *wxRED );
220         listview->SetItem( listitem );
221
222         if( p_intf->p_sys->i_playing != -1 )
223         {
224             listitem.m_itemId = p_intf->p_sys->i_playing;
225             listitem.SetTextColour( *wxBLACK );
226             listview->SetItem( listitem );
227         }
228         p_intf->p_sys->i_playing = p_playlist->i_index;
229     }
230     vlc_mutex_unlock( &p_playlist->object_lock );
231
232     vlc_object_release( p_playlist );
233 }
234
235 /*****************************************************************************
236  * Private methods.
237  *****************************************************************************/
238 void Playlist::DeleteItem( int item )
239 {
240     playlist_t *p_playlist =
241         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
242                                        FIND_ANYWHERE );
243     if( p_playlist == NULL )
244     {
245         return;
246     }
247
248     playlist_Delete( p_playlist, item );
249     listview->DeleteItem( item );
250
251     vlc_object_release( p_playlist );
252 }
253
254 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
255 {
256     Hide();
257 }
258
259 void Playlist::OnAddUrl( wxCommandEvent& WXUNUSED(event) )
260 {
261     /* TODO */
262 }
263
264 void Playlist::OnAddDirectory( wxCommandEvent& WXUNUSED(event) )
265 {
266     /* TODO */
267 }
268
269 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
270 {
271     for( long item = 0; item < listview->GetItemCount(); item++ )
272     {
273         listview->Select( item, ! listview->IsSelected( item ) );
274     }
275 }
276
277 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
278 {
279     /* Delete from the end to the beginning, to avoid a shift of indices */
280     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
281     {
282         if( listview->IsSelected( item ) )
283         {
284             DeleteItem( item );
285         }
286     }
287
288     Rebuild();
289 }
290
291 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
292 {
293     for( long item = 0; item < listview->GetItemCount(); item++ )
294     {
295         listview->Select( item, TRUE );
296     }
297 }
298
299 void Playlist::OnActivateItem( wxListEvent& event )
300 {
301     playlist_t *p_playlist =
302         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
303                                        FIND_ANYWHERE );
304     if( p_playlist == NULL )
305     {
306         return;
307     }
308
309     playlist_Goto( p_playlist, event.GetIndex() );
310
311     vlc_object_release( p_playlist );
312 }
313
314 void Playlist::OnKeyDown( wxListEvent& event )
315 {
316     long keycode = event.GetKeyCode();
317     /* Delete selected items */
318     if( keycode == WXK_BACK || keycode == WXK_DELETE )
319     {
320         /* We send a dummy event */
321         OnDeleteSelection( event );
322     }
323 }
324