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 $
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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() */
35 #include "wxwindows.h"
37 /* Callback prototype */
38 static int PlaylistChanged( vlc_object_t *, const char *,
39 vlc_value_t, vlc_value_t, void * );
41 /*****************************************************************************
43 *****************************************************************************/
44 class BookmarksDialog: public wxFrame
48 BookmarksDialog( intf_thread_t *p_intf, wxWindow *p_parent );
49 virtual ~BookmarksDialog();
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 );
65 DECLARE_EVENT_TABLE();
67 intf_thread_t *p_intf;
70 wxListView *list_ctrl;
73 /*****************************************************************************
75 *****************************************************************************/
77 /* IDs for the controls and the menu commands */
81 ButtonAdd_Event = wxID_HIGHEST + 1,
86 DEFINE_LOCAL_EVENT_TYPE( wxEVT_BOOKMARKS );
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 )
95 EVT_LIST_ITEM_ACTIVATED( -1, BookmarksDialog::OnActivateItem )
97 EVT_COMMAND( -1, wxEVT_BOOKMARKS, BookmarksDialog::OnUpdate )
100 /*****************************************************************************
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 ),
111 wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT )
113 /* Initializations */
115 SetIcon( *p_intf->p_sys->p_icon );
117 wxPanel *main_panel = new wxPanel( this, -1 );
118 wxBoxSizer *main_sizer = new wxBoxSizer( wxHORIZONTAL );
120 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
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 );
135 list_ctrl = new wxListView( main_panel, -1,
136 wxDefaultPosition, wxDefaultSize,
137 wxLC_REPORT | wxSUNKEN_BORDER |
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")) );
144 sizer->Add( panel, 0, wxEXPAND | wxALL, 1 );
145 sizer->Add( list_ctrl, 1, wxEXPAND | wxALL, 1 );
146 main_panel->SetSizer( sizer );
148 main_sizer->Add( main_panel, 1, wxEXPAND );
149 SetSizer( main_sizer );
151 playlist_t *p_playlist =
152 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
156 /* Some global changes happened -> Rebuild all */
157 var_AddCallback( p_playlist, "playlist-current",
158 PlaylistChanged, this );
159 vlc_object_release( p_playlist );
163 BookmarksDialog::~BookmarksDialog()
165 playlist_t *p_playlist =
166 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
170 /* Some global changes happened -> Rebuild all */
171 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
173 vlc_object_release( p_playlist );
177 /*****************************************************************************
179 *****************************************************************************/
180 wxWindow *BookmarksDialog( intf_thread_t *p_intf, wxWindow *p_parent )
182 return new BookmarksDialog::BookmarksDialog( p_intf, p_parent );
185 void BookmarksDialog::Update()
187 input_thread_t *p_input =
188 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
190 if( !p_input ) return;
192 seekpoint_t **pp_bookmarks;
195 list_ctrl->DeleteAllItems();
196 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
197 &i_bookmarks ) != VLC_SUCCESS )
199 vlc_object_release( p_input );
203 for( int i = 0; i < i_bookmarks; i++ )
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 ) );
212 vlc_object_release( p_input );
215 bool BookmarksDialog::Show( bool show )
218 return wxFrame::Show( show );
221 void BookmarksDialog::OnClose( wxCommandEvent& event )
226 void BookmarksDialog::OnAdd( wxCommandEvent& event )
228 input_thread_t *p_input =
229 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
231 if( !p_input ) return;
233 seekpoint_t bookmark;
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 );
243 vlc_object_release( p_input );
248 void BookmarksDialog::OnDel( wxCommandEvent& event )
250 input_thread_t *p_input =
251 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
253 if( !p_input ) return;
255 int i_focused = list_ctrl->GetFocusedItem();
258 input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
261 vlc_object_release( p_input );
266 void BookmarksDialog::OnClear( wxCommandEvent& event )
268 input_thread_t *p_input =
269 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
271 if( !p_input ) return;
273 input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
275 vlc_object_release( p_input );
280 void BookmarksDialog::OnActivateItem( wxListEvent& event )
282 input_thread_t *p_input =
283 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
285 if( !p_input ) return;
287 input_Control( p_input, INPUT_SET_BOOKMARK, event.GetIndex() );
289 vlc_object_release( p_input );
292 void BookmarksDialog::OnUpdate( wxCommandEvent &event )
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 )
305 BookmarksDialog::BookmarksDialog *p_dialog =
306 (BookmarksDialog::BookmarksDialog *)param;
308 wxCommandEvent bookmarks_event( wxEVT_BOOKMARKS, 0 );
309 p_dialog->AddPendingEvent( bookmarks_event );