]> git.sesse.net Git - vlc/blobdiff - modules/gui/wxwindows/bookmarks.cpp
* modules/gui/wxwindows/*: some code cleanup for the bookmarks dialog.
[vlc] / modules / gui / wxwindows / bookmarks.cpp
index 7727aea330b586114fd46d8287375c080b53e3da..3fc0310a8dbf5bf02aa3ef01a7be8521749963fc 100644 (file)
 
 #include "wxwindows.h"
 
+/* Callback prototype */
+static int PlaylistChanged( vlc_object_t *, const char *,
+                            vlc_value_t, vlc_value_t, void * );
+
+/*****************************************************************************
+ * Class declaration.
+ *****************************************************************************/
+class BookmarksDialog: public wxFrame
+{
+public:
+    /* Constructor */
+    BookmarksDialog( intf_thread_t *p_intf, wxWindow *p_parent );
+    virtual ~BookmarksDialog();
+
+    bool Show( bool );
+
+private:
+
+    void Update();
+
+    /* Event handlers (these functions should _not_ be virtual) */
+    void OnClose( wxCommandEvent& event );
+    void OnAdd( wxCommandEvent& event );
+    void OnDel( wxCommandEvent& event );
+    void OnClear( wxCommandEvent& event );
+    void OnActivateItem( wxListEvent& event );
+    void OnUpdate( wxCommandEvent &event );
+
+    DECLARE_EVENT_TABLE();
+
+    intf_thread_t *p_intf;
+    wxWindow *p_parent;
+
+    wxListView *list_ctrl;
+};
+
 /*****************************************************************************
  * Event Table.
  *****************************************************************************/
@@ -43,16 +79,22 @@ enum
 {
     /* menu items */
     ButtonAdd_Event = wxID_HIGHEST + 1,
-    ButtonDel_Event
+    ButtonDel_Event,
+    ButtonClear_Event
 };
 
+DEFINE_LOCAL_EVENT_TYPE( wxEVT_BOOKMARKS );
+
 BEGIN_EVENT_TABLE(BookmarksDialog, wxFrame)
     /* Hide the window when the user closes the window */
     EVT_CLOSE(BookmarksDialog::OnClose )
     EVT_BUTTON( ButtonAdd_Event, BookmarksDialog::OnAdd )
     EVT_BUTTON( ButtonDel_Event, BookmarksDialog::OnDel )
+    EVT_BUTTON( ButtonClear_Event, BookmarksDialog::OnClear )
 
     EVT_LIST_ITEM_ACTIVATED( -1, BookmarksDialog::OnActivateItem )
+
+    EVT_COMMAND( -1, wxEVT_BOOKMARKS, BookmarksDialog::OnUpdate )
 END_EVENT_TABLE()
 
 /*****************************************************************************
@@ -65,26 +107,32 @@ BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf, wxWindow *p_parent )
                wxPoint( p_parent->GetParent()->GetRect().GetX(),
                         p_parent->GetParent()->GetRect().GetY() +
                         p_parent->GetParent()->GetRect().GetHeight() + 40 ),
-             !p_parent->GetParent() ? wxDefaultSize :
-               wxSize( p_parent->GetParent()->GetRect().GetWidth(), -1 ),
+             wxSize( 500, -1 ),
              wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT )
 {
     /* Initializations */
     p_intf = _p_intf;
 
+    wxPanel *main_panel = new wxPanel( this, -1 );
+    wxBoxSizer *main_sizer = new wxBoxSizer( wxHORIZONTAL );
+
     wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
 
-    wxPanel *panel = new wxPanel( this, -1 );
+    wxPanel *panel = new wxPanel( main_panel, -1 );
     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
     wxButton *button_add =
         new wxButton( panel, ButtonAdd_Event, wxU(_("Add")) );
     wxButton *button_del =
         new wxButton( panel, ButtonDel_Event, wxU(_("Remove")) );
+    wxButton *button_clear =
+        new wxButton( panel, ButtonClear_Event, wxU(_("Clear")) );
     panel_sizer->Add( button_add, 0, wxEXPAND );
     panel_sizer->Add( button_del, 0, wxEXPAND );
+    panel_sizer->Add( button_clear, 0, wxEXPAND );
     panel->SetSizerAndFit( panel_sizer );
 
-    list_ctrl = new wxListView( this, -1, wxDefaultPosition, wxDefaultSize,
+    list_ctrl = new wxListView( main_panel, -1,
+                                wxDefaultPosition, wxDefaultSize,
                                 wxLC_REPORT | wxSUNKEN_BORDER |
                                 wxLC_SINGLE_SEL );
     list_ctrl->InsertColumn( 0, wxU(_("Description")) );
@@ -92,18 +140,46 @@ BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf, wxWindow *p_parent )
     list_ctrl->InsertColumn( 1, wxU(_("Size offset")) );
     list_ctrl->InsertColumn( 2, wxU(_("Time offset")) );
 
-    sizer->Add( panel, 0, wxEXPAND | wxALL, 5 );
-    sizer->Add( list_ctrl, 1, wxEXPAND | wxALL, 5 );
-    SetSizer( sizer );
+    sizer->Add( panel, 0, wxEXPAND | wxALL, 1 );
+    sizer->Add( list_ctrl, 1, wxEXPAND | wxALL, 1 );
+    main_panel->SetSizer( sizer );
+
+    main_sizer->Add( main_panel, 1, wxEXPAND );
+    SetSizer( main_sizer );
+
+    playlist_t *p_playlist =
+        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
+                                       FIND_ANYWHERE );
+    if( p_playlist )
+    {
+       /* Some global changes happened -> Rebuild all */
+       var_AddCallback( p_playlist, "playlist-current",
+                        PlaylistChanged, this );
+       vlc_object_release( p_playlist );
+    }
 }
 
 BookmarksDialog::~BookmarksDialog()
 {
+    playlist_t *p_playlist =
+        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
+                                       FIND_ANYWHERE );
+    if( p_playlist )
+    {
+       /* Some global changes happened -> Rebuild all */
+       var_DelCallback( p_playlist, "intf-change", PlaylistChanged, this );
+
+       vlc_object_release( p_playlist );
+    }
 }
 
 /*****************************************************************************
  * Private methods.
  *****************************************************************************/
+wxWindow *BookmarksDialog( intf_thread_t *p_intf, wxWindow *p_parent )
+{
+    return new BookmarksDialog::BookmarksDialog( p_intf, p_parent );
+}
 
 void BookmarksDialog::Update()
 {
@@ -115,6 +191,7 @@ void BookmarksDialog::Update()
     seekpoint_t **pp_bookmarks;
     int i_bookmarks;
 
+    list_ctrl->DeleteAllItems();
     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
                        &i_bookmarks ) != VLC_SUCCESS )
     {
@@ -122,7 +199,6 @@ void BookmarksDialog::Update()
         return;
     }
 
-    list_ctrl->DeleteAllItems();
     for( int i = 0; i < i_bookmarks; i++ )
     {
         list_ctrl->InsertItem( i, wxL2U( pp_bookmarks[i]->psz_name ) );
@@ -158,7 +234,7 @@ void BookmarksDialog::OnAdd( wxCommandEvent& event )
     var_Get( p_input, "position", &pos );
     bookmark.psz_name = NULL;
     bookmark.i_byte_offset =
-        (pos.f_float * p_input->stream.p_selected_area->i_size);
+      (int64_t)((double)pos.f_float * p_input->stream.p_selected_area->i_size);
     var_Get( p_input, "time", &pos );
     bookmark.i_time_offset = pos.i_time;
     input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
@@ -186,6 +262,20 @@ void BookmarksDialog::OnDel( wxCommandEvent& event )
     Update();
 }
 
+void BookmarksDialog::OnClear( wxCommandEvent& event )
+{
+    input_thread_t *p_input =
+        (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
+                                           FIND_ANYWHERE );
+    if( !p_input ) return;
+
+    input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
+
+    vlc_object_release( p_input );
+
+    Update();
+}
+
 void BookmarksDialog::OnActivateItem( wxListEvent& event )
 {
     input_thread_t *p_input =
@@ -197,3 +287,25 @@ void BookmarksDialog::OnActivateItem( wxListEvent& event )
 
     vlc_object_release( p_input );
 }
+
+void BookmarksDialog::OnUpdate( wxCommandEvent &event )
+{
+    Update();
+}
+
+/*****************************************************************************
+ * PlaylistChanged: callback triggered by the intf-change playlist variable
+ *  We don't rebuild the playlist directly here because we don't want the
+ *  caller to block for a too long time.
+ *****************************************************************************/
+static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
+                            vlc_value_t oval, vlc_value_t nval, void *param )
+{
+    BookmarksDialog::BookmarksDialog *p_dialog =
+        (BookmarksDialog::BookmarksDialog *)param;
+
+    wxCommandEvent bookmarks_event( wxEVT_BOOKMARKS, 0 );
+    p_dialog->AddPendingEvent( bookmarks_event );
+
+    return VLC_SUCCESS;
+}