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 $
7 * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <errno.h> /* ENOMEM */
29 #include <string.h> /* strerror() */
34 #ifdef WIN32 /* mingw32 hack */
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
42 #include <wx/wxprec.h>
44 #include <wx/listctrl.h>
48 #include "wxwindows.h"
50 /*****************************************************************************
52 *****************************************************************************/
54 /* IDs for the controls and the menu commands */
62 InvertSelection_Event,
63 DeleteSelection_Event,
70 BEGIN_EVENT_TABLE(Playlist, wxFrame)
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)
80 EVT_LIST_ITEM_ACTIVATED(ListView_Event, Playlist::OnActivateItem)
81 EVT_LIST_KEY_DOWN(ListView_Event, Playlist::OnKeyDown)
84 EVT_BUTTON( wxID_OK, Playlist::OnClose)
86 /* Special events : we don't want to destroy the window when the user
88 EVT_CLOSE(Playlist::OnClose)
91 /*****************************************************************************
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 )
100 p_main_interface = _p_main_interface;
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") );
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") );
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") );
120 /* Attach the menu bar to the frame */
121 SetMenuBar( menubar );
123 /* Create a panel to put everything in */
124 wxPanel *playlist_panel = new wxPanel( this, -1 );
125 playlist_panel->SetAutoLayout( TRUE );
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,
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 );
140 /* Create the OK button */
141 ok_button = new wxButton( playlist_panel, wxID_OK, _("OK") );
142 ok_button->SetDefault();
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 );
158 /* Associate drop targets with the playlist */
159 SetDropTarget( new DragAndDrop( p_intf ) );
161 /* Update the playlist */
166 Playlist::~Playlist()
170 void Playlist::Rebuild()
172 playlist_t *p_playlist =
173 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
175 if( p_playlist == NULL )
180 /* Clear the list... */
181 listview->DeleteAllItems();
183 /* ...and rebuild it */
184 vlc_mutex_lock( &p_playlist->object_lock );
185 for( int i = 0; i < p_playlist->i_size; i++ )
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") );
192 vlc_mutex_unlock( &p_playlist->object_lock );
194 /* Change the colour for the currenty played stream */
196 listitem.m_itemId = p_playlist->i_index;
197 listitem.SetTextColour( *wxRED );
198 listview->SetItem( listitem );
200 vlc_object_release( p_playlist );
203 /* Update the colour of items */
204 void Playlist::Manage()
206 playlist_t *p_playlist =
207 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
209 if( p_playlist == NULL )
214 vlc_mutex_lock( &p_playlist->object_lock );
215 if( p_intf->p_sys->i_playing != p_playlist->i_index )
218 listitem.m_itemId = p_playlist->i_index;
219 listitem.SetTextColour( *wxRED );
220 listview->SetItem( listitem );
222 if( p_intf->p_sys->i_playing != -1 )
224 listitem.m_itemId = p_intf->p_sys->i_playing;
225 listitem.SetTextColour( *wxBLACK );
226 listview->SetItem( listitem );
228 p_intf->p_sys->i_playing = p_playlist->i_index;
230 vlc_mutex_unlock( &p_playlist->object_lock );
232 vlc_object_release( p_playlist );
235 /*****************************************************************************
237 *****************************************************************************/
238 void Playlist::DeleteItem( int item )
240 playlist_t *p_playlist =
241 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
243 if( p_playlist == NULL )
248 playlist_Delete( p_playlist, item );
249 listview->DeleteItem( item );
251 vlc_object_release( p_playlist );
254 void Playlist::OnClose( wxCommandEvent& WXUNUSED(event) )
259 void Playlist::OnAddUrl( wxCommandEvent& WXUNUSED(event) )
264 void Playlist::OnAddDirectory( wxCommandEvent& WXUNUSED(event) )
269 void Playlist::OnInvertSelection( wxCommandEvent& WXUNUSED(event) )
271 for( long item = 0; item < listview->GetItemCount(); item++ )
273 listview->Select( item, ! listview->IsSelected( item ) );
277 void Playlist::OnDeleteSelection( wxCommandEvent& WXUNUSED(event) )
279 /* Delete from the end to the beginning, to avoid a shift of indices */
280 for( long item = listview->GetItemCount() - 1; item >= 0; item-- )
282 if( listview->IsSelected( item ) )
291 void Playlist::OnSelectAll( wxCommandEvent& WXUNUSED(event) )
293 for( long item = 0; item < listview->GetItemCount(); item++ )
295 listview->Select( item, TRUE );
299 void Playlist::OnActivateItem( wxListEvent& event )
301 playlist_t *p_playlist =
302 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
304 if( p_playlist == NULL )
309 playlist_Goto( p_playlist, event.GetIndex() );
311 vlc_object_release( p_playlist );
314 void Playlist::OnKeyDown( wxListEvent& event )
316 long keycode = event.GetKeyCode();
317 /* Delete selected items */
318 if( keycode == WXK_BACK || keycode == WXK_DELETE )
320 /* We send a dummy event */
321 OnDeleteSelection( event );