* modules/gui/wxwindows/*: misc small improvements and fixes.
[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.7 2003/04/01 16:11:43 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     SetIcon( *p_intf->p_sys->p_icon );
102
103     /* Create our "Manage" menu */
104     wxMenu *manage_menu = new wxMenu;
105     manage_menu->Append( AddUrl_Event, _("Add &Url...") );
106     manage_menu->Append( AddDirectory_Event, _("Add &Directory...") );
107     manage_menu->AppendSeparator();
108     manage_menu->Append( Close_Event, _("&Close") );
109
110     /* Create our "Selection" menu */
111     wxMenu *selection_menu = new wxMenu;
112     selection_menu->Append( InvertSelection_Event, _("&Invert") );
113     selection_menu->Append( DeleteSelection_Event, _("&Delete") );
114     selection_menu->Append( SelectAll_Event, _("&Select All") );
115
116     /* Append the freshly created menus to the menu bar */
117     wxMenuBar *menubar = new wxMenuBar( wxMB_DOCKABLE );
118     menubar->Append( manage_menu, _("&Manage") );
119     menubar->Append( selection_menu, _("&Selection") );
120
121     /* Attach the menu bar to the frame */
122     SetMenuBar( menubar );
123
124     /* Create a panel to put everything in */
125     wxPanel *playlist_panel = new wxPanel( this, -1 );
126     playlist_panel->SetAutoLayout( TRUE );
127
128     /* Create the listview */
129     /* FIXME: the given size is arbitrary, and prevents us from resizing
130      * the window to smaller dimensions. But the sizers don't seem to adjust
131      * themselves to the size of a listview, and with a wxDefaultSize the
132      * playlist window is ridiculously small */
133     listview = new wxListView( playlist_panel, ListView_Event,
134                                wxDefaultPosition, wxSize( 350, 300 ),
135                                wxLC_REPORT | wxSUNKEN_BORDER );
136     listview->InsertColumn( 0, _("Url") );
137     listview->InsertColumn( 1, _("Duration") );
138     listview->SetColumnWidth( 0, 250 );
139     listview->SetColumnWidth( 1, 100 );
140
141     /* Create the OK button */
142     ok_button = new wxButton( playlist_panel, wxID_OK, _("OK") );
143     ok_button->SetDefault();
144
145     /* Place everything in sizers */
146     wxBoxSizer *ok_button_sizer = new wxBoxSizer( wxHORIZONTAL );
147     ok_button_sizer->Add( ok_button, 0, wxALL, 5 );
148     ok_button_sizer->Layout();
149     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
150     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
151     panel_sizer->Add( listview, 1, wxEXPAND | wxALL, 5 );
152     panel_sizer->Add( ok_button_sizer, 0, wxALIGN_CENTRE );
153     panel_sizer->Layout();
154     playlist_panel->SetSizerAndFit( panel_sizer );
155     main_sizer->Add( playlist_panel, 1, wxGROW, 0 );
156     main_sizer->Layout();
157     SetSizerAndFit( main_sizer );
158
159 #if !defined(__WXX11__)
160     /* Associate drop targets with the playlist */
161     SetDropTarget( new DragAndDrop( p_intf ) );
162 #endif
163
164     /* Update the playlist */
165     Rebuild();
166
167 }
168
169 Playlist::~Playlist()
170 {
171 }
172
173 void Playlist::Rebuild()
174 {
175     playlist_t *p_playlist =
176         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
177                                        FIND_ANYWHERE );
178     if( p_playlist == NULL )
179     {
180         return;
181     }
182
183     /* Clear the list... */
184     listview->DeleteAllItems();
185
186     /* ...and rebuild it */
187     vlc_mutex_lock( &p_playlist->object_lock );
188     for( int i = 0; i < p_playlist->i_size; i++ )
189     {
190         wxString filename = p_playlist->pp_items[i]->psz_name;
191         listview->InsertItem( i, filename );
192         /* FIXME: we should try to find the actual duration... */
193         listview->SetItem( i, 1, _("no info") );
194     }
195     vlc_mutex_unlock( &p_playlist->object_lock );
196
197     /* Change the colour for the currenty played stream */
198     wxListItem listitem;
199     listitem.m_itemId = p_playlist->i_index;
200     listitem.SetTextColour( *wxRED );
201     listview->SetItem( listitem );
202
203     vlc_object_release( p_playlist );
204 }
205
206 /* Update the colour of items */
207 void Playlist::Manage()
208 {
209     playlist_t *p_playlist =
210         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
211                                        FIND_ANYWHERE );
212     if( p_playlist == NULL )
213     {
214         return;
215     }
216
217     vlc_mutex_lock( &p_playlist->object_lock );
218     if( p_intf->p_sys->i_playing != p_playlist->i_index )
219     {
220         wxListItem listitem;
221         listitem.m_itemId = p_playlist->i_index;
222         listitem.SetTextColour( *wxRED );
223         listview->SetItem( listitem );
224
225         if( p_intf->p_sys->i_playing != -1 )
226         {
227             listitem.m_itemId = p_intf->p_sys->i_playing;
228             listitem.SetTextColour( *wxBLACK );
229             listview->SetItem( listitem );
230         }
231         p_intf->p_sys->i_playing = p_playlist->i_index;
232     }
233     vlc_mutex_unlock( &p_playlist->object_lock );
234
235     vlc_object_release( p_playlist );
236 }
237
238 /*****************************************************************************
239  * Private methods.
240  *****************************************************************************/
241 void Playlist::DeleteItem( int item )
242 {
243     playlist_t *p_playlist =
244         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
245                                        FIND_ANYWHERE );
246     if( p_playlist == NULL )
247     {
248         return;
249     }
250
251     playlist_Delete( p_playlist, item );
252     listview->DeleteItem( item );
253
254     vlc_object_release( p_playlist );
255 }
256
257 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
258 {
259     Hide();
260 }
261
262 void Playlist::OnAddUrl( wxCommandEvent& WXUNUSED(event) )
263 {
264     /* TODO */
265 }
266
267 void Playlist::OnAddDirectory( wxCommandEvent& WXUNUSED(event) )
268 {
269     /* TODO */
270 }
271
272 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
273 {
274     for( long item = 0; item < listview->GetItemCount(); item++ )
275     {
276         listview->Select( item, ! listview->IsSelected( item ) );
277     }
278 }
279
280 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
281 {
282     /* Delete from the end to the beginning, to avoid a shift of indices */
283     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
284     {
285         if( listview->IsSelected( item ) )
286         {
287             DeleteItem( item );
288         }
289     }
290
291     Rebuild();
292 }
293
294 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
295 {
296     for( long item = 0; item < listview->GetItemCount(); item++ )
297     {
298         listview->Select( item, TRUE );
299     }
300 }
301
302 void Playlist::OnActivateItem( wxListEvent& event )
303 {
304     playlist_t *p_playlist =
305         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
306                                        FIND_ANYWHERE );
307     if( p_playlist == NULL )
308     {
309         return;
310     }
311
312     playlist_Goto( p_playlist, event.GetIndex() );
313
314     vlc_object_release( p_playlist );
315 }
316
317 void Playlist::OnKeyDown( wxListEvent& event )
318 {
319     long keycode = event.GetKeyCode();
320     /* Delete selected items */
321     if( keycode == WXK_BACK || keycode == WXK_DELETE )
322     {
323         /* We send a dummy event */
324         OnDeleteSelection( event );
325     }
326 }