* modules/gui/wxwindows/*: a few fixes for the win32 build.
[vlc.git] / modules / gui / wxwindows / playlist.cpp
1 /*****************************************************************************
2  * interface.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $$
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 the listview */
124     /* FIXME: the given size is arbitrary, and prevents us from resizing
125      * the window to smaller dimensions. But the sizers don't seem to adjust
126      * themselves to the size of a listview, and with a wxDefaultSize the
127      * playlist window is ridiculously small */
128     listview = new wxListView( this, ListView_Event, wxDefaultPosition,
129                                wxSize( 350, 300 ), wxLC_REPORT );
130     listview->InsertColumn( 0, _("Url") );
131     listview->InsertColumn( 1, _("Duration") );
132     listview->SetColumnWidth( 0, 250 );
133     listview->SetColumnWidth( 1, 100 );
134
135     /* Create the OK button */
136     ok_button = new wxButton( this, wxID_OK, _("OK") );
137     ok_button->SetDefault();
138
139     /* Place everything in sizers */
140     wxBoxSizer *ok_button_sizer = new wxBoxSizer( wxHORIZONTAL );
141     ok_button_sizer->Add( ok_button, 0, wxALL, 5 );
142     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
143     main_sizer->Add( listview, 1, wxEXPAND | wxALL, 5 );
144     main_sizer->Add( ok_button_sizer, 0, wxALIGN_CENTRE );
145
146     SetSizerAndFit( main_sizer );
147
148     /* Associate drop targets with the playlist */
149     SetDropTarget( new DragAndDrop( p_intf ) );
150
151     /* Update the playlist */
152     Rebuild();
153
154 }
155
156 Playlist::~Playlist()
157 {
158 }
159
160 void Playlist::Rebuild()
161 {
162     playlist_t *p_playlist =
163         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
164                                        FIND_ANYWHERE );
165     if( p_playlist == NULL )
166     {
167         return;
168     }
169
170     /* Clear the list... */
171     listview->DeleteAllItems();
172
173     /* ...and rebuild it */
174     vlc_mutex_lock( &p_playlist->object_lock );
175     for( int i = 0; i < p_playlist->i_size; i++ )
176     {
177         wxString filename = p_playlist->pp_items[i]->psz_name;
178         listview->InsertItem( i, filename );
179         /* FIXME: we should try to find the actual duration... */
180         listview->SetItem( i, 1, _("no info") );
181     }
182     vlc_mutex_unlock( &p_playlist->object_lock );
183
184     /* Change the colour for the currenty played stream */
185     wxListItem listitem;
186     listitem.m_itemId = p_playlist->i_index;
187     listitem.SetTextColour( *wxRED );
188     listview->SetItem( listitem );
189
190     vlc_object_release( p_playlist );
191 }
192
193 /* Update the colour of items */
194 void Playlist::Manage()
195 {
196     playlist_t *p_playlist =
197         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
198                                        FIND_ANYWHERE );
199     if( p_playlist == NULL )
200     {
201         return;
202     }
203
204     vlc_mutex_lock( &p_playlist->object_lock );
205     if( p_intf->p_sys->i_playing != p_playlist->i_index )
206     {
207         wxListItem listitem;
208         listitem.m_itemId = p_playlist->i_index;
209         listitem.SetTextColour( *wxRED );
210         listview->SetItem( listitem );
211
212         if( p_intf->p_sys->i_playing != -1 )
213         {
214             listitem.m_itemId = p_intf->p_sys->i_playing;
215             listitem.SetTextColour( *wxBLACK );
216             listview->SetItem( listitem );
217         }
218         p_intf->p_sys->i_playing = p_playlist->i_index;
219     }
220     vlc_mutex_unlock( &p_playlist->object_lock );
221
222     vlc_object_release( p_playlist );
223 }
224
225 /*****************************************************************************
226  * Private methods.
227  *****************************************************************************/
228 void Playlist::DeleteItem( int item )
229 {
230     playlist_t *p_playlist =
231         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
232                                        FIND_ANYWHERE );
233     if( p_playlist == NULL )
234     {
235         return;
236     }
237
238     playlist_Delete( p_playlist, item );
239     listview->DeleteItem( item );
240
241     vlc_object_release( p_playlist );
242 }
243
244 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
245 {
246     Hide();
247 }
248
249 void Playlist::OnAddUrl( wxCommandEvent& WXUNUSED(event) )
250 {
251     /* TODO */
252 }
253
254 void Playlist::OnAddDirectory( wxCommandEvent& WXUNUSED(event) )
255 {
256     /* TODO */
257 }
258
259 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
260 {
261     for( long item = 0; item < listview->GetItemCount(); item++ )
262     {
263         listview->Select( item, ! listview->IsSelected( item ) );
264     }
265 }
266
267 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
268 {
269     /* Delete from the end to the beginning, to avoid a shift of indices */
270     for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
271     {
272         if( listview->IsSelected( item ) )
273         {
274             DeleteItem( item );
275         }
276     }
277
278     Rebuild();
279 }
280
281 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
282 {
283     for( long item = 0; item < listview->GetItemCount(); item++ )
284     {
285         listview->Select( item, TRUE );
286     }
287 }
288
289 void Playlist::OnActivateItem( wxListEvent& event )
290 {
291     playlist_t *p_playlist =
292         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
293                                        FIND_ANYWHERE );
294     if( p_playlist == NULL )
295     {
296         return;
297     }
298
299     playlist_Goto( p_playlist, event.GetIndex() );
300
301     vlc_object_release( p_playlist );
302 }
303
304 void Playlist::OnKeyDown( wxListEvent& event )
305 {
306     long keycode = event.GetKeyCode();
307     /* Delete selected items */
308     if( keycode == WXK_BACK || keycode == WXK_DELETE )
309     {
310         /* We send a dummy event */
311         OnDeleteSelection( event );
312     }
313 }
314