bookmarks.cpp 10.2 KB
Newer Older
gbazin's avatar
gbazin committed
1
2
3
4
/*****************************************************************************
 * bookmarks.cpp : wxWindows plugin for vlc
 *****************************************************************************
 * Copyright (C) 2000-2004 VideoLAN
5
 * $Id$
gbazin's avatar
gbazin committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <errno.h>                                                 /* ENOMEM */
#include <string.h>                                            /* strerror() */
#include <stdio.h>

#include <vlc/vlc.h>
#include <vlc/intf.h>

#include "wxwindows.h"

37
38
39
40
/* Callback prototype */
static int PlaylistChanged( vlc_object_t *, const char *,
                            vlc_value_t, vlc_value_t, void * );

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*****************************************************************************
 * 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;
};

gbazin's avatar
gbazin committed
73
74
75
76
77
78
79
80
81
/*****************************************************************************
 * Event Table.
 *****************************************************************************/

/* IDs for the controls and the menu commands */
enum
{
    /* menu items */
    ButtonAdd_Event = wxID_HIGHEST + 1,
82
83
    ButtonDel_Event,
    ButtonClear_Event
gbazin's avatar
gbazin committed
84
85
};

86
87
DEFINE_LOCAL_EVENT_TYPE( wxEVT_BOOKMARKS );

gbazin's avatar
gbazin committed
88
89
90
91
92
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 )
93
    EVT_BUTTON( ButtonClear_Event, BookmarksDialog::OnClear )
gbazin's avatar
gbazin committed
94
95

    EVT_LIST_ITEM_ACTIVATED( -1, BookmarksDialog::OnActivateItem )
96
97

    EVT_COMMAND( -1, wxEVT_BOOKMARKS, BookmarksDialog::OnUpdate )
gbazin's avatar
gbazin committed
98
99
100
101
102
103
104
105
106
107
108
109
END_EVENT_TABLE()

/*****************************************************************************
 * Constructor.
 *****************************************************************************/
BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf, wxWindow *p_parent )
  : wxFrame( p_parent->GetParent() ? p_parent->GetParent() : p_parent,
             -1, wxU(_("Bookmarks")),
             !p_parent->GetParent() ? wxDefaultPosition :
               wxPoint( p_parent->GetParent()->GetRect().GetX(),
                        p_parent->GetParent()->GetRect().GetY() +
                        p_parent->GetParent()->GetRect().GetHeight() + 40 ),
110
             wxSize( 500, -1 ),
gbazin's avatar
gbazin committed
111
112
113
114
             wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT )
{
    /* Initializations */
    p_intf = _p_intf;
115
    SetIcon( *p_intf->p_sys->p_icon );
gbazin's avatar
gbazin committed
116

117
118
119
    wxPanel *main_panel = new wxPanel( this, -1 );
    wxBoxSizer *main_sizer = new wxBoxSizer( wxHORIZONTAL );

gbazin's avatar
gbazin committed
120
121
    wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );

122
    wxPanel *panel = new wxPanel( main_panel, -1 );
gbazin's avatar
gbazin committed
123
124
125
126
127
    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")) );
128
129
    wxButton *button_clear =
        new wxButton( panel, ButtonClear_Event, wxU(_("Clear")) );
gbazin's avatar
gbazin committed
130
131
    panel_sizer->Add( button_add, 0, wxEXPAND );
    panel_sizer->Add( button_del, 0, wxEXPAND );
132
    panel_sizer->Add( button_clear, 0, wxEXPAND );
gbazin's avatar
gbazin committed
133
134
    panel->SetSizerAndFit( panel_sizer );

135
136
    list_ctrl = new wxListView( main_panel, -1,
                                wxDefaultPosition, wxDefaultSize,
gbazin's avatar
gbazin committed
137
138
139
140
141
142
143
                                wxLC_REPORT | wxSUNKEN_BORDER |
                                wxLC_SINGLE_SEL );
    list_ctrl->InsertColumn( 0, wxU(_("Description")) );
    list_ctrl->SetColumnWidth( 0, 240 );
    list_ctrl->InsertColumn( 1, wxU(_("Size offset")) );
    list_ctrl->InsertColumn( 2, wxU(_("Time offset")) );

144
145
146
147
148
149
    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 );
150
151
152
153
154
155
156

    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 */
157
158
159
       var_AddCallback( p_playlist, "playlist-current",
                        PlaylistChanged, this );
       vlc_object_release( p_playlist );
160
    }
gbazin's avatar
gbazin committed
161
162
163
164
}

BookmarksDialog::~BookmarksDialog()
{
165
166
167
168
169
    playlist_t *p_playlist =
        (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                       FIND_ANYWHERE );
    if( p_playlist )
    {
170
171
       var_DelCallback( p_playlist, "playlist-current",
                        PlaylistChanged, this );
172
       vlc_object_release( p_playlist );
173
    }
gbazin's avatar
gbazin committed
174
175
176
177
178
}

/*****************************************************************************
 * Private methods.
 *****************************************************************************/
179
180
181
182
wxWindow *BookmarksDialog( intf_thread_t *p_intf, wxWindow *p_parent )
{
    return new BookmarksDialog::BookmarksDialog( p_intf, p_parent );
}
gbazin's avatar
gbazin committed
183
184
185
186
187
188
189
190
191
192
193

void BookmarksDialog::Update()
{
    input_thread_t *p_input =
        (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
                                           FIND_ANYWHERE );
    if( !p_input ) return;

    seekpoint_t **pp_bookmarks;
    int i_bookmarks;

194
    list_ctrl->DeleteAllItems();
gbazin's avatar
gbazin committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
    if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
                       &i_bookmarks ) != VLC_SUCCESS )
    {
        vlc_object_release( p_input );
        return;
    }

    for( int i = 0; i < i_bookmarks; i++ )
    {
        list_ctrl->InsertItem( i, wxL2U( pp_bookmarks[i]->psz_name ) );
        list_ctrl->SetItem( i, 1, wxString::Format(wxT("%i"),
                            pp_bookmarks[i]->i_byte_offset ) );
        list_ctrl->SetItem( i, 2, wxString::Format(wxT("%i"),
                            pp_bookmarks[i]->i_time_offset/1000000 ) );
    }

    vlc_object_release( p_input );
}

bool BookmarksDialog::Show( bool show )
{
    Update();
    return wxFrame::Show( show );
}

void BookmarksDialog::OnClose( wxCommandEvent& event )
{
    Hide();
}

void BookmarksDialog::OnAdd( wxCommandEvent& event )
{
    input_thread_t *p_input =
        (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
                                           FIND_ANYWHERE );
    if( !p_input ) return;

    seekpoint_t bookmark;
    vlc_value_t pos;
    var_Get( p_input, "position", &pos );
    bookmark.psz_name = NULL;
    bookmark.i_byte_offset =
237
      (int64_t)((double)pos.f_float * p_input->stream.p_selected_area->i_size);
gbazin's avatar
gbazin committed
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
    var_Get( p_input, "time", &pos );
    bookmark.i_time_offset = pos.i_time;
    input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );

    vlc_object_release( p_input );

    Update();
}

void BookmarksDialog::OnDel( wxCommandEvent& event )
{
    input_thread_t *p_input =
        (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
                                           FIND_ANYWHERE );
    if( !p_input ) return;

    int i_focused = list_ctrl->GetFocusedItem();
    if( i_focused >= 0 )
    {
        input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
    }

    vlc_object_release( p_input );

    Update();
}

265
266
267
268
269
270
271
272
273
274
275
276
277
278
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();
}

gbazin's avatar
gbazin committed
279
280
281
282
283
284
285
286
287
288
289
void BookmarksDialog::OnActivateItem( wxListEvent& 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_SET_BOOKMARK, event.GetIndex() );

    vlc_object_release( p_input );
}
290
291
292
293
294
295
296
297
298
299
300
301
302
303

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 )
{
304
305
    BookmarksDialog::BookmarksDialog *p_dialog =
        (BookmarksDialog::BookmarksDialog *)param;
306
307
308
309
310
311

    wxCommandEvent bookmarks_event( wxEVT_BOOKMARKS, 0 );
    p_dialog->AddPendingEvent( bookmarks_event );

    return VLC_SUCCESS;
}