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 */
116 wxPanel *main_panel = new wxPanel( this, -1 );
117 wxBoxSizer *main_sizer = new wxBoxSizer( wxHORIZONTAL );
119 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
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 );
134 list_ctrl = new wxListView( main_panel, -1,
135 wxDefaultPosition, wxDefaultSize,
136 wxLC_REPORT | wxSUNKEN_BORDER |
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")) );
143 sizer->Add( panel, 0, wxEXPAND | wxALL, 1 );
144 sizer->Add( list_ctrl, 1, wxEXPAND | wxALL, 1 );
145 main_panel->SetSizer( sizer );
147 main_sizer->Add( main_panel, 1, wxEXPAND );
148 SetSizer( main_sizer );
150 playlist_t *p_playlist =
151 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
155 /* Some global changes happened -> Rebuild all */
156 var_AddCallback( p_playlist, "playlist-current",
157 PlaylistChanged, this );
158 vlc_object_release( p_playlist );
162 BookmarksDialog::~BookmarksDialog()
164 playlist_t *p_playlist =
165 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
169 /* Some global changes happened -> Rebuild all */
170 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
172 vlc_object_release( p_playlist );
176 /*****************************************************************************
178 *****************************************************************************/
179 wxWindow *BookmarksDialog( intf_thread_t *p_intf, wxWindow *p_parent )
181 return new BookmarksDialog::BookmarksDialog( p_intf, p_parent );
184 void BookmarksDialog::Update()
186 input_thread_t *p_input =
187 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
189 if( !p_input ) return;
191 seekpoint_t **pp_bookmarks;
194 list_ctrl->DeleteAllItems();
195 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
196 &i_bookmarks ) != VLC_SUCCESS )
198 vlc_object_release( p_input );
202 for( int i = 0; i < i_bookmarks; i++ )
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 ) );
211 vlc_object_release( p_input );
214 bool BookmarksDialog::Show( bool show )
217 return wxFrame::Show( show );
220 void BookmarksDialog::OnClose( wxCommandEvent& event )
225 void BookmarksDialog::OnAdd( wxCommandEvent& event )
227 input_thread_t *p_input =
228 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
230 if( !p_input ) return;
232 seekpoint_t bookmark;
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 );
242 vlc_object_release( p_input );
247 void BookmarksDialog::OnDel( wxCommandEvent& event )
249 input_thread_t *p_input =
250 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
252 if( !p_input ) return;
254 int i_focused = list_ctrl->GetFocusedItem();
257 input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
260 vlc_object_release( p_input );
265 void BookmarksDialog::OnClear( wxCommandEvent& event )
267 input_thread_t *p_input =
268 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
270 if( !p_input ) return;
272 input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
274 vlc_object_release( p_input );
279 void BookmarksDialog::OnActivateItem( wxListEvent& event )
281 input_thread_t *p_input =
282 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
284 if( !p_input ) return;
286 input_Control( p_input, INPUT_SET_BOOKMARK, event.GetIndex() );
288 vlc_object_release( p_input );
291 void BookmarksDialog::OnUpdate( wxCommandEvent &event )
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 )
304 BookmarksDialog::BookmarksDialog *p_dialog =
305 (BookmarksDialog::BookmarksDialog *)param;
307 wxCommandEvent bookmarks_event( wxEVT_BOOKMARKS, 0 );
308 p_dialog->AddPendingEvent( bookmarks_event );