]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/input_manager.cpp
* wxwidgets: repair slider dragging.
[vlc] / modules / gui / wxwidgets / input_manager.cpp
1 /*****************************************************************************
2  * slider_manager.cpp : Manage an input slider
3  *****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "input_manager.hpp"
26 #include "interface.hpp"
27 #include "video.hpp"
28
29 #include <vlc_meta.h>
30
31 /* include the toolbar graphics */
32 #include "bitmaps/prev.xpm"
33 #include "bitmaps/next.xpm"
34 #include "bitmaps/playlist.xpm"
35
36 /* IDs for the controls */
37 enum
38 {
39     SliderScroll_Event = wxID_HIGHEST,
40
41     DiscMenu_Event,
42     DiscPrev_Event,
43     DiscNext_Event
44 };
45
46 BEGIN_EVENT_TABLE(InputManager, wxPanel)
47     /* Slider events */
48     EVT_COMMAND_SCROLL(SliderScroll_Event, InputManager::OnSliderUpdate)
49
50     /* Disc Buttons events */
51     EVT_BUTTON(DiscMenu_Event, InputManager::OnDiscMenu)
52     EVT_BUTTON(DiscPrev_Event, InputManager::OnDiscPrev)
53     EVT_BUTTON(DiscNext_Event, InputManager::OnDiscNext)
54
55 END_EVENT_TABLE()
56
57 #define STATUS_STOP 0
58 #define STATUS_PLAYING 1
59 #define STATUS_PAUSE 2
60
61 /*****************************************************************************
62  * Constructor.
63  *****************************************************************************/
64 InputManager::InputManager( intf_thread_t *_p_intf, Interface *_p_main_intf,
65                             wxWindow *p_parent )
66   : wxPanel( p_parent )
67 {
68     p_intf = _p_intf;
69     p_main_intf = _p_main_intf;
70     p_input = NULL;
71     i_old_playing_status = STATUS_STOP;
72     i_old_rate = INPUT_RATE_DEFAULT;
73     b_slider_free = VLC_TRUE;
74     i_input_hide_delay = 0;
75
76     /* Create slider */
77     slider = new wxSlider( this, SliderScroll_Event, 0, 0, SLIDER_MAX_POS );
78     /* Add mouse click on slider */
79     slider->Connect( wxEVT_LEFT_DOWN,
80                      wxMouseEventHandler( InputManager::OnSliderClick ),
81                      NULL, this );
82
83     /* Create disc buttons */
84     disc_frame = new wxPanel( this );
85
86     disc_menu_button = new wxBitmapButton( disc_frame, DiscMenu_Event,
87                                            wxBitmap( playlist_xpm ) );
88     disc_prev_button = new wxBitmapButton( disc_frame, DiscPrev_Event,
89                                            wxBitmap( prev_xpm ) );
90     disc_next_button = new wxBitmapButton( disc_frame, DiscNext_Event,
91                                            wxBitmap( next_xpm ) );
92
93     disc_sizer = new wxBoxSizer( wxHORIZONTAL );
94     disc_sizer->Add( disc_menu_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
95     disc_sizer->Add( disc_prev_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
96     disc_sizer->Add( disc_next_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
97     disc_frame->SetSizer( disc_sizer );
98     disc_sizer->Layout();
99
100     /* Add everything to the panel */
101     sizer = new wxBoxSizer( wxHORIZONTAL );
102     SetSizer( sizer );
103     sizer->Add( slider, 1, wxEXPAND | wxALL, 5 );
104     sizer->Add( disc_frame, 0, wxALL, 2 );
105
106     /* Hide by default */
107     sizer->Hide( disc_frame );
108     sizer->Hide( slider );
109
110     sizer->Layout();
111     Fit();
112 }
113
114 InputManager::~InputManager()
115 {
116     vlc_mutex_lock( &p_intf->change_lock );
117     if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );
118     p_intf->p_sys->p_input = NULL;
119     vlc_mutex_unlock( &p_intf->change_lock );
120 }
121
122 /*****************************************************************************
123  * Public methods.
124  *****************************************************************************/
125 vlc_bool_t InputManager::IsPlaying()
126 {
127     return (p_input && !p_input->b_die);
128 }
129
130 /*****************************************************************************
131  * Private methods.
132  *****************************************************************************/
133 void InputManager::UpdateInput()
134 {
135     playlist_t *p_playlist =
136         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
137                                        FIND_ANYWHERE );
138     if( p_playlist != NULL )
139     {
140         LockPlaylist( p_intf->p_sys, p_playlist );
141         p_input = p_intf->p_sys->p_input = p_playlist->p_input;
142         if( p_intf->p_sys->p_input )
143              vlc_object_yield( p_intf->p_sys->p_input );
144         UnlockPlaylist( p_intf->p_sys, p_playlist );
145         vlc_object_release( p_playlist );
146     }
147 }
148
149 void InputManager::UpdateNowPlaying()
150 {
151     char *psz_now_playing = p_input->input.p_item->p_meta->psz_nowplaying ?
152                     strdup( p_input->input.p_item->p_meta->psz_nowplaying ):
153                     strdup( "" );
154     if( psz_now_playing && *psz_now_playing )
155     {
156         p_main_intf->statusbar->SetStatusText(
157                     wxString(wxU(psz_now_playing)) + wxT( " - " ) +
158                     wxU(p_input->input.p_item->psz_name), 2 );
159     }
160     else
161     {
162         p_main_intf->statusbar->SetStatusText(
163                    wxU(p_input->input.p_item->psz_name), 2 );
164     }
165     free( psz_now_playing );
166 }
167
168 void InputManager::UpdateButtons( vlc_bool_t b_play )
169 {
170     if( !b_play )
171     {
172         if( i_old_playing_status == STATUS_STOP ) return;
173
174         i_old_playing_status = STATUS_STOP;
175         p_main_intf->TogglePlayButton( PAUSE_S );
176         p_main_intf->statusbar->SetStatusText( wxT(""), 0 );
177         p_main_intf->statusbar->SetStatusText( wxT(""), 2 );
178
179 /* wxCocoa pretends to support this, but at least 2.6.x doesn't */
180 #ifndef __APPLE__
181 #ifdef wxHAS_TASK_BAR_ICON
182         if( p_main_intf->p_systray )
183         {
184             p_main_intf->p_systray->UpdateTooltip(
185                 wxString(wxT("VLC media player - ")) + wxU(_("Stopped")) );
186         }
187 #endif
188 #endif
189
190         return;
191     }
192
193     /* Manage Playing status */
194     vlc_value_t val;
195     var_Get( p_input, "state", &val );
196     val.i_int = val.i_int == PAUSE_S ? STATUS_PAUSE : STATUS_PLAYING;
197     if( i_old_playing_status != val.i_int )
198     {
199         i_old_playing_status = val.i_int;
200         p_main_intf->TogglePlayButton( val.i_int == STATUS_PAUSE ?
201                                        PAUSE_S : PLAYING_S );
202
203 /* wxCocoa pretends to support this, but at least 2.6.x doesn't */
204 #ifndef __APPLE__
205 #ifdef wxHAS_TASK_BAR_ICON
206         if( p_main_intf->p_systray )
207         {
208             p_main_intf->p_systray->UpdateTooltip(
209                 wxU(p_input->input.p_item->psz_name) + wxString(wxT(" - ")) +
210                 (val.i_int == PAUSE_S ? wxU(_("Paused")) : wxU(_("Playing"))));
211         }
212 #endif
213 #endif
214     }
215 }
216
217 void InputManager::UpdateDiscButtons()
218 {
219     vlc_value_t val;
220     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
221     if( val.i_int > 0 && !disc_frame->IsShown() )
222     {
223         vlc_value_t val;
224
225         #define HELP_MENU N_("Menu")
226         #define HELP_PCH N_("Previous chapter")
227         #define HELP_NCH N_("Next chapter")
228         #define HELP_PTR N_("Previous track")
229         #define HELP_NTR N_("Next track")
230
231         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
232
233         if( val.i_int > 0 )
234         {
235             disc_menu_button->Show();
236             disc_sizer->Show( disc_menu_button );
237             disc_sizer->Layout();
238             disc_sizer->Fit( disc_frame );
239             disc_menu_button->SetToolTip( wxU(_( HELP_MENU ) ) );
240             disc_prev_button->SetToolTip( wxU(_( HELP_PCH ) ) );
241             disc_next_button->SetToolTip( wxU(_( HELP_NCH ) ) );
242         }
243         else
244         {
245             disc_menu_button->Hide();
246             disc_sizer->Hide( disc_menu_button );
247             disc_prev_button->SetToolTip( wxU(_( HELP_PTR ) ) );
248             disc_next_button->SetToolTip( wxU(_( HELP_NTR ) ) );
249         }
250
251         ShowDiscFrame();
252     }
253     else if( val.i_int == 0 && disc_frame->IsShown() )
254     {
255         HideDiscFrame();
256     }
257 }
258
259 void InputManager::HideSlider()
260 {
261     ShowSlider( false );
262 }
263
264 void InputManager::HideDiscFrame()
265 {
266     ShowDiscFrame( false );
267 }
268
269 void InputManager::UpdateTime()
270 {
271     char psz_time[ MSTRTIME_MAX_SIZE ], psz_total[ MSTRTIME_MAX_SIZE ];
272     mtime_t i_seconds;
273
274     i_seconds = var_GetTime( p_intf->p_sys->p_input, "length" ) / 1000000;
275     secstotimestr( psz_total, i_seconds );
276
277     i_seconds = var_GetTime( p_intf->p_sys->p_input, "time" ) / 1000000;
278     secstotimestr( psz_time, i_seconds );
279
280     p_main_intf->statusbar->SetStatusText(
281         wxU(psz_time) + wxString(wxT(" / ")) +wxU(psz_total), 0 );
282 }
283
284 void InputManager::Update()
285 {
286     /* Update the input */
287     if( p_input == NULL )
288     {
289         UpdateInput();
290
291         if( p_input )
292         {
293             slider->SetValue( 0 );
294         }
295         else if( !i_input_hide_delay )
296         {
297             i_input_hide_delay = mdate() + 200000;
298         }
299         else if( i_input_hide_delay < mdate() )
300         {
301             if( disc_frame->IsShown() ) HideDiscFrame();
302             if( slider->IsShown() ) HideSlider();
303             i_input_hide_delay = 0;
304         }
305     }
306     else if( p_input->b_dead )
307     {
308         UpdateButtons( VLC_FALSE );
309         vlc_object_release( p_input );
310         p_input = NULL;
311     }
312     else
313     {
314         i_input_hide_delay = 0;
315     }
316
317     if( p_input && !p_input->b_die )
318     {
319         vlc_value_t pos, len;
320
321         UpdateTime();
322         UpdateButtons( VLC_TRUE );
323         UpdateNowPlaying();
324         UpdateDiscButtons();
325
326         /* Really manage the slider */
327         var_Get( p_input, "position", &pos );
328         var_Get( p_input, "length", &len );
329
330         if( pos.f_float > 0 && !slider->IsShown() ) ShowSlider();
331         else if(  pos.f_float <= 0 &&  slider->IsShown() ) HideSlider();
332
333         /* Update the slider if the user isn't dragging it. */
334         if( slider->IsShown() && b_slider_free )
335         {
336             i_slider_pos = (int)(SLIDER_MAX_POS * pos.f_float);
337             slider->SetValue( i_slider_pos );
338         }
339
340         /* Manage Speed status */
341         vlc_value_t val;
342         var_Get( p_input, "rate", &val );
343         if( i_old_rate != val.i_int )
344         {
345             p_main_intf->statusbar->SetStatusText(
346                 wxString::Format(wxT("x%.2f"),
347                 (float)INPUT_RATE_DEFAULT / val.i_int ), 1 );
348             i_old_rate = val.i_int;
349         }
350     }
351 }
352
353 /*****************************************************************************
354  * Event Handlers.
355  *****************************************************************************/
356 void InputManager::OnDiscMenu( wxCommandEvent& WXUNUSED(event) )
357 {
358     input_thread_t *p_input =
359         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
360                                            FIND_ANYWHERE );
361     if( p_input )
362     {
363         vlc_value_t val; val.i_int = 2;
364
365         var_Set( p_input, "title  0", val);
366         vlc_object_release( p_input );
367     }
368 }
369
370 void InputManager::OnDiscPrev( wxCommandEvent& WXUNUSED(event) )
371 {
372     input_thread_t *p_input =
373         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
374                                            FIND_ANYWHERE );
375     if( p_input )
376     {
377         int i_type = var_Type( p_input, "prev-chapter" );
378         vlc_value_t val; val.b_bool = VLC_TRUE;
379
380         var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
381                  "prev-chapter" : "prev-title", val );
382
383         vlc_object_release( p_input );
384     }
385 }
386
387 void InputManager::OnDiscNext( wxCommandEvent& WXUNUSED(event) )
388 {
389     input_thread_t *p_input =
390         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
391                                            FIND_ANYWHERE );
392     if( p_input )
393     {
394         int i_type = var_Type( p_input, "next-chapter" );
395         vlc_value_t val; val.b_bool = VLC_TRUE;
396
397         var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
398                  "next-chapter" : "next-title", val );
399
400         vlc_object_release( p_input );
401     }
402 }
403
404 void InputManager::OnSliderUpdate( wxScrollEvent& event )
405 {
406     vlc_mutex_lock( &p_intf->change_lock );
407
408 #ifdef WIN32
409     if( event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE
410         || event.GetEventType() == wxEVT_SCROLL_ENDSCROLL )
411     {
412 #endif
413         if( i_slider_pos != event.GetPosition() && p_intf->p_sys->p_input )
414         {
415             vlc_value_t pos;
416             pos.f_float = (float)event.GetPosition() / (float)SLIDER_MAX_POS;
417             var_Set( p_intf->p_sys->p_input, "position", pos );
418         }
419
420 #ifdef WIN32
421         b_slider_free = VLC_TRUE;
422     }
423     else
424     {
425         b_slider_free = VLC_FALSE;
426         if( p_intf->p_sys->p_input ) UpdateTime();
427     }
428 #endif
429
430 #undef WIN32
431     vlc_mutex_unlock( &p_intf->change_lock );
432 }
433
434 void InputManager::OnSliderClick( wxMouseEvent& event )
435 {
436     wxSlider* slider = wxStaticCast( event.GetEventObject(), wxSlider );
437     int i_min = slider->GetMin();
438     int i_max = slider->GetMax();
439     int i_pos = event.GetPosition().x;
440     int i_dim = slider->GetClientSize().x;
441     int i_val = i_min + ( i_pos * ( i_max - i_min + 1 ) ) / i_dim;
442
443     if( i_pos < 0 || i_pos >= i_dim ) return;
444
445     vlc_mutex_lock( &p_intf->change_lock );
446
447     slider->SetValue( i_val );
448
449     if( i_slider_pos != i_val && p_intf->p_sys->p_input )
450     {
451         vlc_value_t pos;
452         pos.f_float = (float)i_val / (float)SLIDER_MAX_POS;
453         var_Set( p_intf->p_sys->p_input, "position", pos );
454     }
455
456     vlc_mutex_unlock( &p_intf->change_lock );
457
458     event.Skip();
459 }
460
461 void InputManager::ShowSlider( bool show )
462 {
463     if( !!show == !!slider->IsShown() ) return;
464
465     UpdateVideoWindow( p_intf, p_main_intf->video_window );
466
467     sizer->Show( slider, show );
468     sizer->Layout();
469
470     wxCommandEvent intf_event( wxEVT_INTF, 0 );
471     p_main_intf->AddPendingEvent( intf_event );
472 }
473
474 void InputManager::ShowDiscFrame( bool show )
475 {
476     if( !!show == !!disc_frame->IsShown() ) return;
477
478     UpdateVideoWindow( p_intf, p_main_intf->video_window );
479
480     sizer->Show( disc_frame, show );
481     sizer->Layout();
482
483     wxCommandEvent intf_event( wxEVT_INTF, 0 );
484     p_main_intf->AddPendingEvent( intf_event );
485 }