]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/input_manager.cpp
- wxwidgets: for win32, patch movie wxSlider underlying control behaviour so that...
[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 #if WIN32
62
63 #include <commctrl.h>
64
65 /*
66 ** On win32, clicking on the slider channel causes the thumb to jump up or down a page size
67 ** like a scrollbar.  This is not particularily useful for a movie track, where we'd rather
68 ** see the thumb to jump where the mouse is.
69 ** Therefore, we replace the slider (TRACKBAR control) window proc with our, which intercept
70 ** the mouse down event, and move the thumb accordingly
71 */
72 static LRESULT CALLBACK MovieSliderWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
73 {
74     switch( uMsg )
75     {
76         case WM_LBUTTONDOWN:
77         {
78             POINT click = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
79             RECT tRect = {0, 0, 0, 0};
80             SendMessage(hWnd, TBM_GETTHUMBRECT, 0, (LPARAM)&tRect);
81
82             /* check whether click is not in thumb */
83             if( ! PtInRect(&tRect, click) )
84             {
85                 LONG min = SendMessage(hWnd, TBM_GETRANGEMIN, 0, 0);
86                 LONG max = SendMessage(hWnd, TBM_GETRANGEMAX, 0, 0);
87                 LONG thumb = SendMessage(hWnd, TBM_GETTHUMBLENGTH, 0, 0);;
88                 LONG newpos;
89
90                 SendMessage(hWnd, TBM_GETCHANNELRECT, 0, (LPARAM)&tRect);
91
92                 /* following is only valid for horizontal trackbar */
93                 newpos = ((click.x-tRect.left-(thumb/2))*(max-min)+((tRect.right-tRect.left-thumb)/2))/(tRect.right-tRect.left-thumb);
94
95                 /* set new postion */
96                 SendMessage(hWnd, TBM_SETPOS, TRUE, newpos);
97                 /* notify parent of change */
98                 SendMessage(GetParent(hWnd), WM_HSCROLL, TB_ENDTRACK, (LPARAM)hWnd);
99
100                 return 0;
101             }
102         }
103
104         default:
105             return CallWindowProc((WNDPROC)GetWindowLongPtr(hWnd, GWLP_USERDATA),
106                         hWnd, uMsg, wParam, lParam);
107     }
108 }
109
110 #endif
111
112 /*****************************************************************************
113  * Constructor.
114  *****************************************************************************/
115 InputManager::InputManager( intf_thread_t *_p_intf, Interface *_p_main_intf,
116                             wxWindow *p_parent )
117   : wxPanel( p_parent )
118 {
119     p_intf = _p_intf;
120     p_main_intf = _p_main_intf;
121     p_input = NULL;
122     i_old_playing_status = STATUS_STOP;
123     i_old_rate = INPUT_RATE_DEFAULT;
124     b_slider_free = VLC_TRUE;
125     i_input_hide_delay = 0;
126
127     /* Create slider */
128     slider = new wxSlider( this, SliderScroll_Event, 0, 0, SLIDER_MAX_POS );
129
130 #if WIN32
131     /* modify behaviour of WIN32 underlying control
132       in order to implement proper movie slider */
133     {
134         HWND sliderHwnd = (HWND)slider->GetHWND();
135         /* put original WNDPROC into USERDATA, this may be incompatible with future version of
136            wxwidgets. */
137         SetWindowLongPtr(sliderHwnd, GWLP_USERDATA,
138             (LONG_PTR)GetWindowLongPtr(sliderHwnd, GWLP_WNDPROC));
139         /* put our own WNDPROC */
140         SetWindowLongPtr(sliderHwnd, GWLP_WNDPROC, (LONG_PTR)MovieSliderWindowProc);
141     }
142 #endif
143
144     /* Create disc buttons */
145     disc_frame = new wxPanel( this );
146
147     disc_menu_button = new wxBitmapButton( disc_frame, DiscMenu_Event,
148                                            wxBitmap( playlist_xpm ) );
149     disc_prev_button = new wxBitmapButton( disc_frame, DiscPrev_Event,
150                                            wxBitmap( prev_xpm ) );
151     disc_next_button = new wxBitmapButton( disc_frame, DiscNext_Event,
152                                            wxBitmap( next_xpm ) );
153
154     disc_sizer = new wxBoxSizer( wxHORIZONTAL );
155     disc_sizer->Add( disc_menu_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
156     disc_sizer->Add( disc_prev_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
157     disc_sizer->Add( disc_next_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
158     disc_frame->SetSizer( disc_sizer );
159     disc_sizer->Layout();
160
161     /* Add everything to the panel */
162     sizer = new wxBoxSizer( wxHORIZONTAL );
163     SetSizer( sizer );
164     sizer->Add( slider, 1, wxEXPAND | wxALL, 5 );
165     sizer->Add( disc_frame, 0, wxALL, 2 );
166
167     /* Hide by default */
168     sizer->Hide( disc_frame );
169     sizer->Hide( slider );
170
171     sizer->Layout();
172     Fit();
173 }
174
175 InputManager::~InputManager()
176 {
177     vlc_mutex_lock( &p_intf->change_lock );
178     if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );
179     p_intf->p_sys->p_input = NULL;
180     vlc_mutex_unlock( &p_intf->change_lock );
181 }
182
183 /*****************************************************************************
184  * Public methods.
185  *****************************************************************************/
186 vlc_bool_t InputManager::IsPlaying()
187 {
188     return (p_input && !p_input->b_die);
189 }
190
191 /*****************************************************************************
192  * Private methods.
193  *****************************************************************************/
194 void InputManager::UpdateInput()
195 {
196     playlist_t *p_playlist =
197         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
198                                        FIND_ANYWHERE );
199     if( p_playlist != NULL )
200     {
201         LockPlaylist( p_intf->p_sys, p_playlist );
202         p_input = p_intf->p_sys->p_input = p_playlist->p_input;
203         if( p_intf->p_sys->p_input )
204              vlc_object_yield( p_intf->p_sys->p_input );
205         UnlockPlaylist( p_intf->p_sys, p_playlist );
206         vlc_object_release( p_playlist );
207     }
208 }
209
210 void InputManager::UpdateNowPlaying()
211 {
212     char *psz_now_playing = input_GetItem(p_input)->p_meta->psz_nowplaying ?
213                     strdup( input_GetItem(p_input)->p_meta->psz_nowplaying ):
214                     strdup( "" );
215     if( psz_now_playing && *psz_now_playing )
216     {
217         p_main_intf->statusbar->SetStatusText(
218                     wxString(wxU(psz_now_playing)) + wxT( " - " ) +
219                     wxU(input_GetItem(p_input)->psz_name), 2 );
220     }
221     else
222     {
223         p_main_intf->statusbar->SetStatusText(
224                    wxU(input_GetItem(p_input)->psz_name), 2 );
225     }
226     free( psz_now_playing );
227 }
228
229 void InputManager::UpdateButtons( vlc_bool_t b_play )
230 {
231     if( !b_play )
232     {
233         if( i_old_playing_status == STATUS_STOP ) return;
234
235         i_old_playing_status = STATUS_STOP;
236         p_main_intf->TogglePlayButton( PAUSE_S );
237         p_main_intf->statusbar->SetStatusText( wxT(""), 0 );
238         p_main_intf->statusbar->SetStatusText( wxT(""), 2 );
239
240 /* wxCocoa pretends to support this, but at least 2.6.x doesn't */
241 #ifndef __APPLE__
242 #ifdef wxHAS_TASK_BAR_ICON
243         if( p_main_intf->p_systray )
244         {
245             p_main_intf->p_systray->UpdateTooltip(
246                 wxString(wxT("VLC media player - ")) + wxU(_("Stopped")) );
247         }
248 #endif
249 #endif
250
251         return;
252     }
253
254     /* Manage Playing status */
255     vlc_value_t val;
256     var_Get( p_input, "state", &val );
257     val.i_int = val.i_int == PAUSE_S ? STATUS_PAUSE : STATUS_PLAYING;
258     if( i_old_playing_status != val.i_int )
259     {
260         i_old_playing_status = val.i_int;
261         p_main_intf->TogglePlayButton( val.i_int == STATUS_PAUSE ?
262                                        PAUSE_S : PLAYING_S );
263
264 /* wxCocoa pretends to support this, but at least 2.6.x doesn't */
265 #ifndef __APPLE__
266 #ifdef wxHAS_TASK_BAR_ICON
267         if( p_main_intf->p_systray )
268         {
269             p_main_intf->p_systray->UpdateTooltip(
270                 wxU(input_GetItem(p_input)->psz_name) + wxString(wxT(" - ")) +
271                 (val.i_int == PAUSE_S ? wxU(_("Paused")) : wxU(_("Playing"))));
272         }
273 #endif
274 #endif
275     }
276 }
277
278 void InputManager::UpdateDiscButtons()
279 {
280     vlc_value_t val;
281     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
282     if( val.i_int > 0 && !disc_frame->IsShown() )
283     {
284         vlc_value_t val;
285
286         #define HELP_MENU N_("Menu")
287         #define HELP_PCH N_("Previous chapter")
288         #define HELP_NCH N_("Next chapter")
289         #define HELP_PTR N_("Previous track")
290         #define HELP_NTR N_("Next track")
291
292         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
293
294         if( val.i_int > 0 )
295         {
296             disc_menu_button->Show();
297             disc_sizer->Show( disc_menu_button );
298             disc_sizer->Layout();
299             disc_sizer->Fit( disc_frame );
300             disc_menu_button->SetToolTip( wxU(_( HELP_MENU ) ) );
301             disc_prev_button->SetToolTip( wxU(_( HELP_PCH ) ) );
302             disc_next_button->SetToolTip( wxU(_( HELP_NCH ) ) );
303         }
304         else
305         {
306             disc_menu_button->Hide();
307             disc_sizer->Hide( disc_menu_button );
308             disc_prev_button->SetToolTip( wxU(_( HELP_PTR ) ) );
309             disc_next_button->SetToolTip( wxU(_( HELP_NTR ) ) );
310         }
311
312         ShowDiscFrame();
313     }
314     else if( val.i_int == 0 && disc_frame->IsShown() )
315     {
316         HideDiscFrame();
317     }
318 }
319
320 void InputManager::HideSlider()
321 {
322     ShowSlider( false );
323 }
324
325 void InputManager::HideDiscFrame()
326 {
327     ShowDiscFrame( false );
328 }
329
330 void InputManager::UpdateTime()
331 {
332     char psz_time[ MSTRTIME_MAX_SIZE ], psz_total[ MSTRTIME_MAX_SIZE ];
333     mtime_t i_seconds;
334
335     i_seconds = var_GetTime( p_intf->p_sys->p_input, "length" ) / 1000000;
336     secstotimestr( psz_total, i_seconds );
337
338     i_seconds = var_GetTime( p_intf->p_sys->p_input, "time" ) / 1000000;
339     secstotimestr( psz_time, i_seconds );
340
341     p_main_intf->statusbar->SetStatusText(
342         wxU(psz_time) + wxString(wxT(" / ")) +wxU(psz_total), 0 );
343 }
344
345 void InputManager::Update()
346 {
347     /* Update the input */
348     if( p_input == NULL )
349     {
350         UpdateInput();
351
352         if( p_input )
353         {
354             slider->SetValue( 0 );
355         }
356         else if( !i_input_hide_delay )
357         {
358             i_input_hide_delay = mdate() + 200000;
359         }
360         else if( i_input_hide_delay < mdate() )
361         {
362             if( disc_frame->IsShown() ) HideDiscFrame();
363             if( slider->IsShown() ) HideSlider();
364             i_input_hide_delay = 0;
365         }
366     }
367     else if( p_input->b_dead )
368     {
369         UpdateButtons( VLC_FALSE );
370         vlc_object_release( p_input );
371         p_input = NULL;
372     }
373     else
374     {
375         i_input_hide_delay = 0;
376     }
377
378     if( p_input && !p_input->b_die )
379     {
380         vlc_value_t pos, len;
381
382         UpdateTime();
383         UpdateButtons( VLC_TRUE );
384         UpdateNowPlaying();
385         UpdateDiscButtons();
386
387         /* Really manage the slider */
388         var_Get( p_input, "position", &pos );
389         var_Get( p_input, "length", &len );
390
391         if( pos.f_float > 0 && !slider->IsShown() ) ShowSlider();
392         else if(  pos.f_float <= 0 &&  slider->IsShown() ) HideSlider();
393
394         /* Update the slider if the user isn't dragging it. */
395         if( slider->IsShown() && b_slider_free )
396         {
397             i_slider_pos = (int)(SLIDER_MAX_POS * pos.f_float);
398             slider->SetValue( i_slider_pos );
399         }
400
401         /* Manage Speed status */
402         vlc_value_t val;
403         var_Get( p_input, "rate", &val );
404         if( i_old_rate != val.i_int )
405         {
406             p_main_intf->statusbar->SetStatusText(
407                 wxString::Format(wxT("x%.2f"),
408                 (float)INPUT_RATE_DEFAULT / val.i_int ), 1 );
409             i_old_rate = val.i_int;
410         }
411     }
412 }
413
414 /*****************************************************************************
415  * Event Handlers.
416  *****************************************************************************/
417 void InputManager::OnDiscMenu( wxCommandEvent& WXUNUSED(event) )
418 {
419     input_thread_t *p_input =
420         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
421                                            FIND_ANYWHERE );
422     if( p_input )
423     {
424         vlc_value_t val; val.i_int = 2;
425
426         var_Set( p_input, "title  0", val);
427         vlc_object_release( p_input );
428     }
429 }
430
431 void InputManager::OnDiscPrev( wxCommandEvent& WXUNUSED(event) )
432 {
433     input_thread_t *p_input =
434         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
435                                            FIND_ANYWHERE );
436     if( p_input )
437     {
438         int i_type = var_Type( p_input, "prev-chapter" );
439         vlc_value_t val; val.b_bool = VLC_TRUE;
440
441         var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
442                  "prev-chapter" : "prev-title", val );
443
444         vlc_object_release( p_input );
445     }
446 }
447
448 void InputManager::OnDiscNext( wxCommandEvent& WXUNUSED(event) )
449 {
450     input_thread_t *p_input =
451         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
452                                            FIND_ANYWHERE );
453     if( p_input )
454     {
455         int i_type = var_Type( p_input, "next-chapter" );
456         vlc_value_t val; val.b_bool = VLC_TRUE;
457
458         var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
459                  "next-chapter" : "next-title", val );
460
461         vlc_object_release( p_input );
462     }
463 }
464
465 void InputManager::OnSliderUpdate( wxScrollEvent& event )
466 {
467     vlc_mutex_lock( &p_intf->change_lock );
468
469 #ifdef WIN32
470     if( event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE
471         || event.GetEventType() == wxEVT_SCROLL_ENDSCROLL )
472     {
473 #endif
474         if( i_slider_pos != event.GetPosition() && p_intf->p_sys->p_input )
475         {
476             vlc_value_t pos;
477             pos.f_float = (float)event.GetPosition() / (float)SLIDER_MAX_POS;
478             var_Set( p_intf->p_sys->p_input, "position", pos );
479         }
480
481 #ifdef WIN32
482         b_slider_free = VLC_TRUE;
483     }
484     else
485     {
486         b_slider_free = VLC_FALSE;
487         if( p_intf->p_sys->p_input ) UpdateTime();
488     }
489 #endif
490
491 #undef WIN32
492     vlc_mutex_unlock( &p_intf->change_lock );
493 }
494
495 void InputManager::ShowSlider( bool show )
496 {
497     if( !!show == !!slider->IsShown() ) return;
498
499     UpdateVideoWindow( p_intf, p_main_intf->video_window );
500
501     sizer->Show( slider, show );
502     sizer->Layout();
503
504     wxCommandEvent intf_event( wxEVT_INTF, 0 );
505     p_main_intf->AddPendingEvent( intf_event );
506 }
507
508 void InputManager::ShowDiscFrame( bool show )
509 {
510     if( !!show == !!disc_frame->IsShown() ) return;
511
512     UpdateVideoWindow( p_intf, p_main_intf->video_window );
513
514     sizer->Show( disc_frame, show );
515     sizer->Layout();
516
517     wxCommandEvent intf_event( wxEVT_INTF, 0 );
518     p_main_intf->AddPendingEvent( intf_event );
519 }