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 *****************************************************************************/
45 /* IDs for the controls and the menu commands */
49 ButtonAdd_Event = wxID_HIGHEST + 1,
54 DEFINE_LOCAL_EVENT_TYPE( wxEVT_BOOKMARKS );
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 )
63 EVT_LIST_ITEM_ACTIVATED( -1, BookmarksDialog::OnActivateItem )
65 EVT_COMMAND( -1, wxEVT_BOOKMARKS, BookmarksDialog::OnUpdate )
68 /*****************************************************************************
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 ),
79 wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT )
84 wxPanel *main_panel = new wxPanel( this, -1 );
85 wxBoxSizer *main_sizer = new wxBoxSizer( wxHORIZONTAL );
87 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
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 );
102 list_ctrl = new wxListView( main_panel, -1,
103 wxDefaultPosition, wxDefaultSize,
104 wxLC_REPORT | wxSUNKEN_BORDER |
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")) );
111 sizer->Add( panel, 0, wxEXPAND | wxALL, 1 );
112 sizer->Add( list_ctrl, 1, wxEXPAND | wxALL, 1 );
113 main_panel->SetSizer( sizer );
115 main_sizer->Add( main_panel, 1, wxEXPAND );
116 SetSizer( main_sizer );
118 playlist_t *p_playlist =
119 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
123 /* Some global changes happened -> Rebuild all */
124 var_AddCallback( p_playlist, "playlist-current",
125 PlaylistChanged, this );
126 vlc_object_release( p_playlist );
130 BookmarksDialog::~BookmarksDialog()
132 playlist_t *p_playlist =
133 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
137 /* Some global changes happened -> Rebuild all */
138 var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
140 vlc_object_release( p_playlist );
144 /*****************************************************************************
146 *****************************************************************************/
148 void BookmarksDialog::Update()
150 input_thread_t *p_input =
151 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
153 if( !p_input ) return;
155 seekpoint_t **pp_bookmarks;
158 list_ctrl->DeleteAllItems();
159 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
160 &i_bookmarks ) != VLC_SUCCESS )
162 vlc_object_release( p_input );
166 for( int i = 0; i < i_bookmarks; i++ )
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 ) );
175 vlc_object_release( p_input );
178 bool BookmarksDialog::Show( bool show )
181 return wxFrame::Show( show );
184 void BookmarksDialog::OnClose( wxCommandEvent& event )
189 void BookmarksDialog::OnAdd( wxCommandEvent& event )
191 input_thread_t *p_input =
192 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
194 if( !p_input ) return;
196 seekpoint_t bookmark;
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 );
206 vlc_object_release( p_input );
211 void BookmarksDialog::OnDel( wxCommandEvent& event )
213 input_thread_t *p_input =
214 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
216 if( !p_input ) return;
218 int i_focused = list_ctrl->GetFocusedItem();
221 input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
224 vlc_object_release( p_input );
229 void BookmarksDialog::OnClear( wxCommandEvent& event )
231 input_thread_t *p_input =
232 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
234 if( !p_input ) return;
236 input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
238 vlc_object_release( p_input );
243 void BookmarksDialog::OnActivateItem( wxListEvent& event )
245 input_thread_t *p_input =
246 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
248 if( !p_input ) return;
250 input_Control( p_input, INPUT_SET_BOOKMARK, event.GetIndex() );
252 vlc_object_release( p_input );
255 void BookmarksDialog::OnUpdate( wxCommandEvent &event )
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 )
268 BookmarksDialog *p_dialog = (BookmarksDialog *)param;
270 wxCommandEvent bookmarks_event( wxEVT_BOOKMARKS, 0 );
271 p_dialog->AddPendingEvent( bookmarks_event );