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