]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/bookmarks.cpp
* modules/gui/wxwindows/wxwindows.cpp: don't "play on start" when in dialogs provider...
[vlc] / modules / gui / wxwindows / bookmarks.cpp
1 /*****************************************************************************
2  * bookmarks.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id: bookmarks.cpp 6961 2004-03-05 17:34:23Z sam $
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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 #include <vlc/intf.h>
34
35 #include "wxwindows.h"
36
37 /* Callback prototype */
38 static int PlaylistChanged( vlc_object_t *, const char *,
39                             vlc_value_t, vlc_value_t, void * );
40
41 /*****************************************************************************
42  * Class declaration.
43  *****************************************************************************/
44 class BookmarksDialog: public wxFrame
45 {
46 public:
47     /* Constructor */
48     BookmarksDialog( intf_thread_t *p_intf, wxWindow *p_parent );
49     virtual ~BookmarksDialog();
50
51     bool Show( bool );
52
53 private:
54
55     void Update();
56
57     /* Event handlers (these functions should _not_ be virtual) */
58     void OnClose( wxCommandEvent& event );
59     void OnAdd( wxCommandEvent& event );
60     void OnDel( wxCommandEvent& event );
61     void OnClear( wxCommandEvent& event );
62     void OnActivateItem( wxListEvent& event );
63     void OnUpdate( wxCommandEvent &event );
64
65     DECLARE_EVENT_TABLE();
66
67     intf_thread_t *p_intf;
68     wxWindow *p_parent;
69
70     wxListView *list_ctrl;
71 };
72
73 /*****************************************************************************
74  * Event Table.
75  *****************************************************************************/
76
77 /* IDs for the controls and the menu commands */
78 enum
79 {
80     /* menu items */
81     ButtonAdd_Event = wxID_HIGHEST + 1,
82     ButtonDel_Event,
83     ButtonClear_Event
84 };
85
86 DEFINE_LOCAL_EVENT_TYPE( wxEVT_BOOKMARKS );
87
88 BEGIN_EVENT_TABLE(BookmarksDialog, wxFrame)
89     /* Hide the window when the user closes the window */
90     EVT_CLOSE(BookmarksDialog::OnClose )
91     EVT_BUTTON( ButtonAdd_Event, BookmarksDialog::OnAdd )
92     EVT_BUTTON( ButtonDel_Event, BookmarksDialog::OnDel )
93     EVT_BUTTON( ButtonClear_Event, BookmarksDialog::OnClear )
94
95     EVT_LIST_ITEM_ACTIVATED( -1, BookmarksDialog::OnActivateItem )
96
97     EVT_COMMAND( -1, wxEVT_BOOKMARKS, BookmarksDialog::OnUpdate )
98 END_EVENT_TABLE()
99
100 /*****************************************************************************
101  * Constructor.
102  *****************************************************************************/
103 BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf, wxWindow *p_parent )
104   : wxFrame( p_parent->GetParent() ? p_parent->GetParent() : p_parent,
105              -1, wxU(_("Bookmarks")),
106              !p_parent->GetParent() ? wxDefaultPosition :
107                wxPoint( p_parent->GetParent()->GetRect().GetX(),
108                         p_parent->GetParent()->GetRect().GetY() +
109                         p_parent->GetParent()->GetRect().GetHeight() + 40 ),
110              wxSize( 500, -1 ),
111              wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT )
112 {
113     /* Initializations */
114     p_intf = _p_intf;
115     SetIcon( *p_intf->p_sys->p_icon );
116
117     wxPanel *main_panel = new wxPanel( this, -1 );
118     wxBoxSizer *main_sizer = new wxBoxSizer( wxHORIZONTAL );
119
120     wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
121
122     wxPanel *panel = new wxPanel( main_panel, -1 );
123     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
124     wxButton *button_add =
125         new wxButton( panel, ButtonAdd_Event, wxU(_("Add")) );
126     wxButton *button_del =
127         new wxButton( panel, ButtonDel_Event, wxU(_("Remove")) );
128     wxButton *button_clear =
129         new wxButton( panel, ButtonClear_Event, wxU(_("Clear")) );
130     panel_sizer->Add( button_add, 0, wxEXPAND );
131     panel_sizer->Add( button_del, 0, wxEXPAND );
132     panel_sizer->Add( button_clear, 0, wxEXPAND );
133     panel->SetSizerAndFit( panel_sizer );
134
135     list_ctrl = new wxListView( main_panel, -1,
136                                 wxDefaultPosition, wxDefaultSize,
137                                 wxLC_REPORT | wxSUNKEN_BORDER |
138                                 wxLC_SINGLE_SEL );
139     list_ctrl->InsertColumn( 0, wxU(_("Description")) );
140     list_ctrl->SetColumnWidth( 0, 240 );
141     list_ctrl->InsertColumn( 1, wxU(_("Size offset")) );
142     list_ctrl->InsertColumn( 2, wxU(_("Time offset")) );
143
144     sizer->Add( panel, 0, wxEXPAND | wxALL, 1 );
145     sizer->Add( list_ctrl, 1, wxEXPAND | wxALL, 1 );
146     main_panel->SetSizer( sizer );
147
148     main_sizer->Add( main_panel, 1, wxEXPAND );
149     SetSizer( main_sizer );
150
151     playlist_t *p_playlist =
152         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
153                                        FIND_ANYWHERE );
154     if( p_playlist )
155     {
156        /* Some global changes happened -> Rebuild all */
157        var_AddCallback( p_playlist, "playlist-current",
158                         PlaylistChanged, this );
159        vlc_object_release( p_playlist );
160     }
161 }
162
163 BookmarksDialog::~BookmarksDialog()
164 {
165     playlist_t *p_playlist =
166         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
167                                        FIND_ANYWHERE );
168     if( p_playlist )
169     {
170        /* Some global changes happened -> Rebuild all */
171        var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
172
173        vlc_object_release( p_playlist );
174     }
175 }
176
177 /*****************************************************************************
178  * Private methods.
179  *****************************************************************************/
180 wxWindow *BookmarksDialog( intf_thread_t *p_intf, wxWindow *p_parent )
181 {
182     return new BookmarksDialog::BookmarksDialog( p_intf, p_parent );
183 }
184
185 void BookmarksDialog::Update()
186 {
187     input_thread_t *p_input =
188         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
189                                            FIND_ANYWHERE );
190     if( !p_input ) return;
191
192     seekpoint_t **pp_bookmarks;
193     int i_bookmarks;
194
195     list_ctrl->DeleteAllItems();
196     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
197                        &i_bookmarks ) != VLC_SUCCESS )
198     {
199         vlc_object_release( p_input );
200         return;
201     }
202
203     for( int i = 0; i < i_bookmarks; i++ )
204     {
205         list_ctrl->InsertItem( i, wxL2U( pp_bookmarks[i]->psz_name ) );
206         list_ctrl->SetItem( i, 1, wxString::Format(wxT("%i"),
207                             pp_bookmarks[i]->i_byte_offset ) );
208         list_ctrl->SetItem( i, 2, wxString::Format(wxT("%i"),
209                             pp_bookmarks[i]->i_time_offset/1000000 ) );
210     }
211
212     vlc_object_release( p_input );
213 }
214
215 bool BookmarksDialog::Show( bool show )
216 {
217     Update();
218     return wxFrame::Show( show );
219 }
220
221 void BookmarksDialog::OnClose( wxCommandEvent& event )
222 {
223     Hide();
224 }
225
226 void BookmarksDialog::OnAdd( wxCommandEvent& event )
227 {
228     input_thread_t *p_input =
229         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
230                                            FIND_ANYWHERE );
231     if( !p_input ) return;
232
233     seekpoint_t bookmark;
234     vlc_value_t pos;
235     var_Get( p_input, "position", &pos );
236     bookmark.psz_name = NULL;
237     bookmark.i_byte_offset =
238       (int64_t)((double)pos.f_float * p_input->stream.p_selected_area->i_size);
239     var_Get( p_input, "time", &pos );
240     bookmark.i_time_offset = pos.i_time;
241     input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
242
243     vlc_object_release( p_input );
244
245     Update();
246 }
247
248 void BookmarksDialog::OnDel( wxCommandEvent& event )
249 {
250     input_thread_t *p_input =
251         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
252                                            FIND_ANYWHERE );
253     if( !p_input ) return;
254
255     int i_focused = list_ctrl->GetFocusedItem();
256     if( i_focused >= 0 )
257     {
258         input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
259     }
260
261     vlc_object_release( p_input );
262
263     Update();
264 }
265
266 void BookmarksDialog::OnClear( wxCommandEvent& event )
267 {
268     input_thread_t *p_input =
269         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
270                                            FIND_ANYWHERE );
271     if( !p_input ) return;
272
273     input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
274
275     vlc_object_release( p_input );
276
277     Update();
278 }
279
280 void BookmarksDialog::OnActivateItem( wxListEvent& event )
281 {
282     input_thread_t *p_input =
283         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
284                                            FIND_ANYWHERE );
285     if( !p_input ) return;
286
287     input_Control( p_input, INPUT_SET_BOOKMARK, event.GetIndex() );
288
289     vlc_object_release( p_input );
290 }
291
292 void BookmarksDialog::OnUpdate( wxCommandEvent &event )
293 {
294     Update();
295 }
296
297 /*****************************************************************************
298  * PlaylistChanged: callback triggered by the intf-change playlist variable
299  *  We don't rebuild the playlist directly here because we don't want the
300  *  caller to block for a too long time.
301  *****************************************************************************/
302 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
303                             vlc_value_t oval, vlc_value_t nval, void *param )
304 {
305     BookmarksDialog::BookmarksDialog *p_dialog =
306         (BookmarksDialog::BookmarksDialog *)param;
307
308     wxCommandEvent bookmarks_event( wxEVT_BOOKMARKS, 0 );
309     p_dialog->AddPendingEvent( bookmarks_event );
310
311     return VLC_SUCCESS;
312 }