1 /*****************************************************************************
2 * bookmarks.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2004 the VideoLAN team
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 "wxwidgets.h"
36 #include <wx/dialog.h>
38 /* Callback prototype */
39 static int PlaylistChanged( vlc_object_t *, const char *,
40 vlc_value_t, vlc_value_t, void * );
42 /*****************************************************************************
44 *****************************************************************************/
46 /* IDs for the controls and the menu commands */
50 ButtonAdd_Event = wxID_HIGHEST + 1,
57 class BookmarksDialog: public wxFrame
61 BookmarksDialog( intf_thread_t *p_intf, wxWindow *p_parent );
62 virtual ~BookmarksDialog();
70 /* Event handlers (these functions should _not_ be virtual) */
71 void OnClose( wxCloseEvent& event );
72 void OnAdd( wxCommandEvent& event );
73 void OnDel( wxCommandEvent& event );
74 void OnClear( wxCommandEvent& event );
75 void OnActivateItem( wxListEvent& event );
76 void OnUpdate( wxCommandEvent &event );
77 void OnEdit( wxCommandEvent& event );
78 void OnExtract( wxCommandEvent& event );
80 DECLARE_EVENT_TABLE();
82 intf_thread_t *p_intf;
85 wxListView *list_ctrl;
88 /*****************************************************************************
90 *****************************************************************************/
92 DEFINE_LOCAL_EVENT_TYPE( wxEVT_BOOKMARKS );
94 BEGIN_EVENT_TABLE(BookmarksDialog, wxFrame)
95 /* Hide the window when the user closes the window */
96 EVT_CLOSE(BookmarksDialog::OnClose )
97 EVT_BUTTON( ButtonAdd_Event, BookmarksDialog::OnAdd )
98 EVT_BUTTON( ButtonDel_Event, BookmarksDialog::OnDel )
99 EVT_BUTTON( ButtonClear_Event, BookmarksDialog::OnClear )
100 EVT_BUTTON( ButtonExtract_Event, BookmarksDialog::OnExtract )
101 EVT_BUTTON( ButtonEdit_Event, BookmarksDialog::OnEdit )
103 EVT_LIST_ITEM_ACTIVATED( -1, BookmarksDialog::OnActivateItem )
105 EVT_COMMAND( -1, wxEVT_BOOKMARKS, BookmarksDialog::OnUpdate )
108 /* Declaration of class BookmarkEditDialog */
109 class BookmarkEditDialog : public wxDialog
113 BookmarkEditDialog( intf_thread_t *p_intf, wxWindow *p_parent,
114 seekpoint_t *p_seekpoint );
115 virtual ~BookmarkEditDialog();
116 seekpoint_t *p_seekpoint;
119 wxTextCtrl *name_text, *time_text, *bytes_text;
121 void OnOK( wxCommandEvent& event);
122 void OnCancel( wxCommandEvent& event);
124 DECLARE_EVENT_TABLE();
126 intf_thread_t *p_intf;
129 BEGIN_EVENT_TABLE( BookmarkEditDialog, wxDialog)
130 EVT_BUTTON( wxID_OK, BookmarkEditDialog::OnOK)
132 /****************************************************************************
134 ***************************************************************************/
135 BookmarkEditDialog::BookmarkEditDialog( intf_thread_t *_p_intf,
136 wxWindow *_p_parent, seekpoint_t *_p_seekpoint ):wxDialog(
137 _p_parent, -1, wxU(_("Edit bookmark")), wxDefaultPosition,
138 wxDefaultSize, wxDEFAULT_FRAME_STYLE )
140 /* Initializations */
142 p_seekpoint = _p_seekpoint;
143 SetIcon( *p_intf->p_sys->p_icon );
145 /* Create a panel to put everything in*/
146 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
148 wxFlexGridSizer * sizer = new wxFlexGridSizer( 2 , 3 , 1 );
149 name_text = new wxTextCtrl( this, -1, wxU( p_seekpoint->psz_name ?
150 p_seekpoint->psz_name : "" ),
151 wxDefaultPosition, wxSize( 100, 20) );
152 time_text = new wxTextCtrl( this, -1, wxString::Format(wxT("%d"),
153 (int)(p_seekpoint->i_time_offset / 1000000) ),
154 wxDefaultPosition, wxSize( 100, 20) );
155 bytes_text = new wxTextCtrl( this, -1, wxString::Format(wxT("%d"),
156 (int)p_seekpoint->i_byte_offset ),
157 wxDefaultPosition, wxSize( 100, 20) );
159 sizer->Add( new wxStaticText( this, -1, wxU(_("Name") ) ), 0, wxLEFT, 5 );
160 sizer->Add( name_text, 0, wxEXPAND|wxRIGHT , 5 );
161 sizer->Add( new wxStaticText( this, -1, wxU(_("Time") ) ), 0, wxLEFT, 5 );
162 sizer->Add( time_text , 0, wxEXPAND|wxRIGHT , 5);
163 sizer->Add( new wxStaticText( this, -1, wxU(_("Bytes") ) ), 0, wxLEFT, 5 );
164 sizer->Add( bytes_text, 0, wxEXPAND|wxRIGHT, 5);
166 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
167 wxButton *ok_button = new wxButton( this, wxID_OK, wxU(_("OK") ) );
168 ok_button->SetDefault();
169 button_sizer->Add( ok_button );
170 button_sizer->Add( new wxButton( this, wxID_CANCEL, wxU(_("Cancel") ) ) );
172 panel_sizer->Add( sizer, 0, wxEXPAND | wxTOP|wxBOTTOM, 5 );
173 panel_sizer->Add( button_sizer, 0, wxEXPAND | wxBOTTOM, 5 );
174 panel_sizer->Layout();
175 SetSizerAndFit( panel_sizer );
178 BookmarkEditDialog::~BookmarkEditDialog()
181 void BookmarkEditDialog::OnOK( wxCommandEvent &event )
183 if( p_seekpoint->psz_name ) free( p_seekpoint->psz_name );
184 p_seekpoint->psz_name = strdup( name_text->GetValue().mb_str() );
185 p_seekpoint->i_byte_offset = atoi( bytes_text->GetValue().mb_str() );
186 p_seekpoint->i_time_offset = 1000000 *
187 atoll( time_text->GetValue().mb_str() ) ;
191 void BookmarkEditDialog::OnCancel( wxCommandEvent &event )
193 EndModal( wxID_CANCEL );
196 /*****************************************************************************
198 *****************************************************************************/
199 BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf, wxWindow *p_parent )
200 : wxFrame( p_parent->GetParent() ? p_parent->GetParent() : p_parent,
201 -1, wxU(_("Bookmarks")),
202 !p_parent->GetParent() ? wxDefaultPosition :
203 wxPoint( p_parent->GetParent()->GetRect().GetX(),
204 p_parent->GetParent()->GetRect().GetY() +
205 p_parent->GetParent()->GetRect().GetHeight() + 40 ),
207 wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT )
209 /* Initializations */
211 SetIcon( *p_intf->p_sys->p_icon );
213 wxPanel *main_panel = new wxPanel( this, -1 );
214 wxBoxSizer *main_sizer = new wxBoxSizer( wxHORIZONTAL );
216 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
218 wxPanel *panel = new wxPanel( main_panel, -1 );
219 wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
220 wxButton *button_add =
221 new wxButton( panel, ButtonAdd_Event, wxU(_("Add")) );
222 wxButton *button_del =
223 new wxButton( panel, ButtonDel_Event, wxU(_("Remove")) );
224 wxButton *button_clear =
225 new wxButton( panel, ButtonClear_Event, wxU(_("Clear")) );
226 wxButton *button_edit =
227 new wxButton( panel, ButtonEdit_Event, wxU(_("Edit")) );
228 wxButton *button_extract =
229 new wxButton( panel, ButtonExtract_Event, wxU(_("Extract")) );
231 #define ADD_TEXT "Adds a bookmark at the current position in the stream"
232 #define REMOVE_TEXT "Removes the selected bookmarks"
233 #define CLEAR_TEXT "Removes all the bookmarks for that stream"
234 #define EDIT_TEXT "Edit the properties of a bookmark"
235 #define EXTRACT_TEXT "If you select two or more bookmarks, this will " \
236 "launch the streaming/transcoding wizard to allow you to " \
237 "stream or save the part of the stream between these bookmarks"
238 button_add->SetToolTip( wxU(_( ADD_TEXT ) ) );
239 button_del->SetToolTip( wxU(_( REMOVE_TEXT ) ) );
240 button_clear->SetToolTip( wxU(_( CLEAR_TEXT ) ) );
241 button_edit->SetToolTip( wxU(_( EDIT_TEXT ) ) );
242 button_extract->SetToolTip( wxU(_( EXTRACT_TEXT ) ) );
244 panel_sizer->Add( button_add, 0, wxEXPAND );
245 panel_sizer->Add( button_del, 0, wxEXPAND );
246 panel_sizer->Add( button_clear, 0, wxEXPAND );
248 panel_sizer->Add( button_edit, 0, wxEXPAND );
249 panel_sizer->Add( 0, 0, 1 );
250 panel_sizer->Add( button_extract, 0, wxEXPAND );
252 panel->SetSizerAndFit( panel_sizer );
254 list_ctrl = new wxListView( main_panel, -1,
255 wxDefaultPosition, wxDefaultSize,
256 wxLC_REPORT | wxSUNKEN_BORDER );
257 list_ctrl->InsertColumn( 0, wxU(_("Description")) );
258 list_ctrl->SetColumnWidth( 0, 240 );
259 list_ctrl->InsertColumn( 1, wxU(_("Size offset")) );
260 list_ctrl->InsertColumn( 2, wxU(_("Time offset")) );
262 sizer->Add( panel, 0, wxEXPAND | wxALL, 1 );
263 sizer->Add( list_ctrl, 1, wxEXPAND | wxALL, 1 );
264 main_panel->SetSizer( sizer );
266 main_sizer->Add( main_panel, 1, wxEXPAND );
267 SetSizer( main_sizer );
269 playlist_t *p_playlist =
270 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
274 /* Some global changes happened -> Rebuild all */
275 var_AddCallback( p_playlist, "playlist-current",
276 PlaylistChanged, this );
277 vlc_object_release( p_playlist );
281 BookmarksDialog::~BookmarksDialog()
283 playlist_t *p_playlist =
284 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
288 var_DelCallback( p_playlist, "playlist-current",
289 PlaylistChanged, this );
290 vlc_object_release( p_playlist );
294 /*****************************************************************************
296 *****************************************************************************/
297 wxFrame *BookmarksDialog( intf_thread_t *p_intf, wxWindow *p_parent )
299 return new class BookmarksDialog( p_intf, p_parent );
302 void BookmarksDialog::Update()
304 input_thread_t *p_input =
305 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
307 if( !p_input ) return;
309 seekpoint_t **pp_bookmarks;
312 list_ctrl->DeleteAllItems();
313 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
314 &i_bookmarks ) != VLC_SUCCESS )
316 vlc_object_release( p_input );
320 for( int i = 0; i < i_bookmarks; i++ )
322 list_ctrl->InsertItem( i, wxL2U( pp_bookmarks[i]->psz_name ) );
323 /* FIXME: see if we can use the 64 bits integer format string */
324 list_ctrl->SetItem( i, 1, wxString::Format(wxT("%d"),
325 (int)(pp_bookmarks[i]->i_byte_offset) ) );
326 list_ctrl->SetItem( i, 2, wxString::Format(wxT("%d"),
327 (int)(pp_bookmarks[i]->i_time_offset / 1000000) ) );
330 vlc_object_release( p_input );
333 bool BookmarksDialog::Show( bool show )
336 return wxFrame::Show( show );
339 void BookmarksDialog::OnClose( wxCloseEvent& event )
344 void BookmarksDialog::OnAdd( wxCommandEvent& event )
346 input_thread_t *p_input =
347 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
349 if( !p_input ) return;
351 seekpoint_t bookmark;
353 bookmark.psz_name = NULL;
354 bookmark.i_byte_offset = 0;
355 bookmark.i_time_offset = 0;
357 var_Get( p_input, "position", &pos );
358 bookmark.psz_name = NULL;
359 input_Control( p_input, INPUT_GET_BYTE_POSITION, &bookmark.i_byte_offset );
360 var_Get( p_input, "time", &pos );
361 bookmark.i_time_offset = pos.i_time;
362 input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
363 vlc_object_release( p_input );
368 void BookmarksDialog::OnDel( wxCommandEvent& event )
370 input_thread_t *p_input =
371 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
373 if( !p_input ) return;
375 int i_focused = list_ctrl->GetFocusedItem();
378 input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
381 vlc_object_release( p_input );
386 void BookmarksDialog::OnClear( wxCommandEvent& event )
388 input_thread_t *p_input =
389 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
391 if( !p_input ) return;
393 input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
395 vlc_object_release( p_input );
400 void BookmarksDialog::OnExtract( wxCommandEvent& event )
402 long i_first = list_ctrl->GetNextItem( -1, wxLIST_NEXT_ALL,
403 wxLIST_STATE_SELECTED );
404 long i_second = list_ctrl->GetNextItem( i_first, wxLIST_NEXT_ALL,
405 wxLIST_STATE_SELECTED );
407 if( i_first == -1 || i_second == -1 )
409 wxMessageBox( wxU(_("You must select two bookmarks") ),
410 wxU(_("Invalid selection") ), wxICON_WARNING | wxOK,
414 input_thread_t *p_input =
415 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
419 wxMessageBox( wxU(_("The stream must be playing or paused for "
420 "bookmarks to work" ) ), wxU(_("No input found")),
421 wxICON_WARNING | wxOK,
426 seekpoint_t **pp_bookmarks;
429 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
430 &i_bookmarks ) != VLC_SUCCESS )
432 vlc_object_release( p_input );
436 if( i_first < i_bookmarks && i_second <= i_bookmarks )
438 WizardDialog *p_wizard_dialog = new WizardDialog( p_intf, this,
439 p_input->input.p_item->psz_uri,
440 pp_bookmarks[i_first]->i_time_offset/1000000,
441 pp_bookmarks[i_second]->i_time_offset/1000000 );
442 vlc_object_release( p_input );
443 if( p_wizard_dialog )
445 p_wizard_dialog->Run();
446 delete p_wizard_dialog;
451 vlc_object_release( p_input );
455 void BookmarksDialog::OnActivateItem( wxListEvent& event )
457 input_thread_t *p_input =
458 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
460 if( !p_input ) return;
462 input_Control( p_input, INPUT_SET_BOOKMARK, event.GetIndex() );
464 vlc_object_release( p_input );
467 void BookmarksDialog::OnEdit( wxCommandEvent& event )
469 input_thread_t *p_old_input;
470 input_thread_t *p_input =
471 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
473 if( !p_input ) return;
476 seekpoint_t **pp_bookmarks;
479 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
480 &i_bookmarks ) != VLC_SUCCESS )
482 vlc_object_release( p_input );
485 p_old_input = p_input;
486 vlc_object_release( p_input );
488 long i_first = list_ctrl->GetNextItem( -1, wxLIST_NEXT_ALL,
489 wxLIST_STATE_SELECTED );
491 if( i_first > -1 && i_first <= i_bookmarks )
493 BookmarkEditDialog *p_bmk_edit;
494 p_bmk_edit = new BookmarkEditDialog( p_intf, this,
495 pp_bookmarks[i_first]);
497 if( p_bmk_edit->ShowModal() == wxID_OK )
499 p_input =(input_thread_t *)vlc_object_find( p_intf,
500 VLC_OBJECT_INPUT, FIND_ANYWHERE );
503 wxMessageBox( wxU( _("No input found. The stream must be "
504 "playing or paused for bookmarks to work.") ),
505 wxU( _("No input") ), wxICON_WARNING | wxOK,
509 if( p_old_input != p_input )
511 wxMessageBox( wxU( _("Input has changed, unable to save "
512 "bookmark. Use \"pause\" while editing "
513 "bookmarks to keep the same input.") ),
514 wxU( _("Input has changed ") ),
515 wxICON_WARNING | wxOK, this );
516 vlc_object_release( p_input );
520 if( input_Control( p_input, INPUT_CHANGE_BOOKMARK,
521 p_bmk_edit->p_seekpoint, i_first ) !=
524 vlc_object_release( p_input );
528 vlc_object_release( p_input );
534 void BookmarksDialog::OnUpdate( wxCommandEvent &event )
539 /*****************************************************************************
540 * PlaylistChanged: callback triggered by the intf-change playlist variable
541 * We don't rebuild the playlist directly here because we don't want the
542 * caller to block for a too long time.
543 *****************************************************************************/
544 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
545 vlc_value_t oval, vlc_value_t nval, void *param )
547 class BookmarksDialog *p_dialog = (class BookmarksDialog *)param;
549 wxCommandEvent bookmarks_event( wxEVT_BOOKMARKS, 0 );
550 p_dialog->AddPendingEvent( bookmarks_event );