]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/bookmarks.cpp
* modules/gui/wxwindows/*: some code cleanup for the bookmarks dialog.
[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
116     wxPanel *main_panel = new wxPanel( this, -1 );
117     wxBoxSizer *main_sizer = new wxBoxSizer( wxHORIZONTAL );
118
119     wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
120
121     wxPanel *panel = new wxPanel( main_panel, -1 );
122     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
123     wxButton *button_add =
124         new wxButton( panel, ButtonAdd_Event, wxU(_("Add")) );
125     wxButton *button_del =
126         new wxButton( panel, ButtonDel_Event, wxU(_("Remove")) );
127     wxButton *button_clear =
128         new wxButton( panel, ButtonClear_Event, wxU(_("Clear")) );
129     panel_sizer->Add( button_add, 0, wxEXPAND );
130     panel_sizer->Add( button_del, 0, wxEXPAND );
131     panel_sizer->Add( button_clear, 0, wxEXPAND );
132     panel->SetSizerAndFit( panel_sizer );
133
134     list_ctrl = new wxListView( main_panel, -1,
135                                 wxDefaultPosition, wxDefaultSize,
136                                 wxLC_REPORT | wxSUNKEN_BORDER |
137                                 wxLC_SINGLE_SEL );
138     list_ctrl->InsertColumn( 0, wxU(_("Description")) );
139     list_ctrl->SetColumnWidth( 0, 240 );
140     list_ctrl->InsertColumn( 1, wxU(_("Size offset")) );
141     list_ctrl->InsertColumn( 2, wxU(_("Time offset")) );
142
143     sizer->Add( panel, 0, wxEXPAND | wxALL, 1 );
144     sizer->Add( list_ctrl, 1, wxEXPAND | wxALL, 1 );
145     main_panel->SetSizer( sizer );
146
147     main_sizer->Add( main_panel, 1, wxEXPAND );
148     SetSizer( main_sizer );
149
150     playlist_t *p_playlist =
151         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
152                                        FIND_ANYWHERE );
153     if( p_playlist )
154     {
155        /* Some global changes happened -> Rebuild all */
156        var_AddCallback( p_playlist, "playlist-current",
157                         PlaylistChanged, this );
158        vlc_object_release( p_playlist );
159     }
160 }
161
162 BookmarksDialog::~BookmarksDialog()
163 {
164     playlist_t *p_playlist =
165         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
166                                        FIND_ANYWHERE );
167     if( p_playlist )
168     {
169        /* Some global changes happened -> Rebuild all */
170        var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
171
172        vlc_object_release( p_playlist );
173     }
174 }
175
176 /*****************************************************************************
177  * Private methods.
178  *****************************************************************************/
179 wxWindow *BookmarksDialog( intf_thread_t *p_intf, wxWindow *p_parent )
180 {
181     return new BookmarksDialog::BookmarksDialog( p_intf, p_parent );
182 }
183
184 void BookmarksDialog::Update()
185 {
186     input_thread_t *p_input =
187         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
188                                            FIND_ANYWHERE );
189     if( !p_input ) return;
190
191     seekpoint_t **pp_bookmarks;
192     int i_bookmarks;
193
194     list_ctrl->DeleteAllItems();
195     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
196                        &i_bookmarks ) != VLC_SUCCESS )
197     {
198         vlc_object_release( p_input );
199         return;
200     }
201
202     for( int i = 0; i < i_bookmarks; i++ )
203     {
204         list_ctrl->InsertItem( i, wxL2U( pp_bookmarks[i]->psz_name ) );
205         list_ctrl->SetItem( i, 1, wxString::Format(wxT("%i"),
206                             pp_bookmarks[i]->i_byte_offset ) );
207         list_ctrl->SetItem( i, 2, wxString::Format(wxT("%i"),
208                             pp_bookmarks[i]->i_time_offset/1000000 ) );
209     }
210
211     vlc_object_release( p_input );
212 }
213
214 bool BookmarksDialog::Show( bool show )
215 {
216     Update();
217     return wxFrame::Show( show );
218 }
219
220 void BookmarksDialog::OnClose( wxCommandEvent& event )
221 {
222     Hide();
223 }
224
225 void BookmarksDialog::OnAdd( wxCommandEvent& event )
226 {
227     input_thread_t *p_input =
228         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
229                                            FIND_ANYWHERE );
230     if( !p_input ) return;
231
232     seekpoint_t bookmark;
233     vlc_value_t pos;
234     var_Get( p_input, "position", &pos );
235     bookmark.psz_name = NULL;
236     bookmark.i_byte_offset =
237       (int64_t)((double)pos.f_float * p_input->stream.p_selected_area->i_size);
238     var_Get( p_input, "time", &pos );
239     bookmark.i_time_offset = pos.i_time;
240     input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
241
242     vlc_object_release( p_input );
243
244     Update();
245 }
246
247 void BookmarksDialog::OnDel( wxCommandEvent& event )
248 {
249     input_thread_t *p_input =
250         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
251                                            FIND_ANYWHERE );
252     if( !p_input ) return;
253
254     int i_focused = list_ctrl->GetFocusedItem();
255     if( i_focused >= 0 )
256     {
257         input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
258     }
259
260     vlc_object_release( p_input );
261
262     Update();
263 }
264
265 void BookmarksDialog::OnClear( wxCommandEvent& event )
266 {
267     input_thread_t *p_input =
268         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
269                                            FIND_ANYWHERE );
270     if( !p_input ) return;
271
272     input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
273
274     vlc_object_release( p_input );
275
276     Update();
277 }
278
279 void BookmarksDialog::OnActivateItem( wxListEvent& event )
280 {
281     input_thread_t *p_input =
282         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
283                                            FIND_ANYWHERE );
284     if( !p_input ) return;
285
286     input_Control( p_input, INPUT_SET_BOOKMARK, event.GetIndex() );
287
288     vlc_object_release( p_input );
289 }
290
291 void BookmarksDialog::OnUpdate( wxCommandEvent &event )
292 {
293     Update();
294 }
295
296 /*****************************************************************************
297  * PlaylistChanged: callback triggered by the intf-change playlist variable
298  *  We don't rebuild the playlist directly here because we don't want the
299  *  caller to block for a too long time.
300  *****************************************************************************/
301 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
302                             vlc_value_t oval, vlc_value_t nval, void *param )
303 {
304     BookmarksDialog::BookmarksDialog *p_dialog =
305         (BookmarksDialog::BookmarksDialog *)param;
306
307     wxCommandEvent bookmarks_event( wxEVT_BOOKMARKS, 0 );
308     p_dialog->AddPendingEvent( bookmarks_event );
309
310     return VLC_SUCCESS;
311 }