1 /*****************************************************************************
2 * slider_manager.cpp : Manage an input slider
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
25 #include "input_manager.hpp"
26 #include "interface.hpp"
31 /* include the toolbar graphics */
32 #include "bitmaps/prev.xpm"
33 #include "bitmaps/next.xpm"
34 #include "bitmaps/playlist.xpm"
36 /* IDs for the controls */
39 SliderScroll_Event = wxID_HIGHEST,
46 BEGIN_EVENT_TABLE(InputManager, wxPanel)
48 EVT_COMMAND_SCROLL(SliderScroll_Event, InputManager::OnSliderUpdate)
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)
58 #define STATUS_PLAYING 1
59 #define STATUS_PAUSE 2
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
72 static LRESULT CALLBACK MovieSliderWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
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);
82 /* check whether click is not in thumb */
83 if( ! PtInRect(&tRect, click) )
85 LONG min = SendMessage(hWnd, TBM_GETRANGEMIN, 0, 0);
86 LONG max = SendMessage(hWnd, TBM_GETRANGEMAX, 0, 0);
87 LONG thumb = tRect.right-tRect.left;
90 SendMessage(hWnd, TBM_GETCHANNELRECT, 0, (LPARAM)&tRect);
92 /* following is only valid for horizontal a trackbar */
93 newpos = ((click.x-tRect.left-(thumb/2))*(max-min)+((tRect.right-tRect.left-thumb)/2))
94 /(tRect.right-tRect.left-thumb);
97 SendMessage(hWnd, TBM_SETPOS, TRUE, min+newpos);
98 /* notify parent of change */
99 SendMessage(GetParent(hWnd), WM_HSCROLL, TB_ENDTRACK, (LPARAM)hWnd);
106 return CallWindowProc((WNDPROC)GetWindowLongPtr(hWnd, GWLP_USERDATA),
107 hWnd, uMsg, wParam, lParam);
113 /*****************************************************************************
115 *****************************************************************************/
116 InputManager::InputManager( intf_thread_t *_p_intf, Interface *_p_main_intf,
118 : wxPanel( p_parent )
121 p_main_intf = _p_main_intf;
123 i_old_playing_status = STATUS_STOP;
124 i_old_rate = INPUT_RATE_DEFAULT;
125 b_slider_free = VLC_TRUE;
126 i_input_hide_delay = 0;
129 slider = new wxSlider( this, SliderScroll_Event, 0, 0, SLIDER_MAX_POS );
132 /* modify behaviour of WIN32 underlying control
133 in order to implement proper movie slider */
135 HWND sliderHwnd = (HWND)slider->GetHWND();
136 /* put original WNDPROC into USERDATA, this may be incompatible with future version of
138 SetWindowLongPtr(sliderHwnd, GWLP_USERDATA,
139 (LONG_PTR)GetWindowLongPtr(sliderHwnd, GWLP_WNDPROC));
140 /* put our own WNDPROC */
141 SetWindowLongPtr(sliderHwnd, GWLP_WNDPROC, (LONG_PTR)MovieSliderWindowProc);
145 /* Create disc buttons */
146 disc_frame = new wxPanel( this );
148 disc_menu_button = new wxBitmapButton( disc_frame, DiscMenu_Event,
149 wxBitmap( playlist_xpm ) );
150 disc_prev_button = new wxBitmapButton( disc_frame, DiscPrev_Event,
151 wxBitmap( prev_xpm ) );
152 disc_next_button = new wxBitmapButton( disc_frame, DiscNext_Event,
153 wxBitmap( next_xpm ) );
155 disc_sizer = new wxBoxSizer( wxHORIZONTAL );
156 disc_sizer->Add( disc_menu_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
157 disc_sizer->Add( disc_prev_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
158 disc_sizer->Add( disc_next_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
159 disc_frame->SetSizer( disc_sizer );
160 disc_sizer->Layout();
162 /* Add everything to the panel */
163 sizer = new wxBoxSizer( wxHORIZONTAL );
165 sizer->Add( slider, 1, wxEXPAND | wxALL, 5 );
166 sizer->Add( disc_frame, 0, wxALL, 2 );
168 /* Hide by default */
169 sizer->Hide( disc_frame );
170 sizer->Hide( slider );
176 InputManager::~InputManager()
178 vlc_mutex_lock( &p_intf->change_lock );
179 if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );
180 p_intf->p_sys->p_input = NULL;
181 vlc_mutex_unlock( &p_intf->change_lock );
184 /*****************************************************************************
186 *****************************************************************************/
187 vlc_bool_t InputManager::IsPlaying()
189 return (p_input && !p_input->b_die);
192 /*****************************************************************************
194 *****************************************************************************/
195 void InputManager::UpdateInput()
197 playlist_t *p_playlist =
198 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
200 if( p_playlist != NULL )
202 LockPlaylist( p_intf->p_sys, p_playlist );
203 p_input = p_intf->p_sys->p_input = p_playlist->p_input;
204 if( p_intf->p_sys->p_input )
205 vlc_object_yield( p_intf->p_sys->p_input );
206 UnlockPlaylist( p_intf->p_sys, p_playlist );
207 vlc_object_release( p_playlist );
211 void InputManager::UpdateNowPlaying()
213 const char *psz_now_playing =
214 input_item_GetNowPlaying( input_GetItem(p_input) ) ?
215 strdup( input_GetItem(p_input)->p_meta->psz_nowplaying ):
217 if( psz_now_playing && *psz_now_playing )
219 p_main_intf->statusbar->SetStatusText(
220 wxString(wxU(psz_now_playing)) + wxT( " - " ) +
221 wxU(input_GetItem(p_input)->psz_name), 2 );
225 p_main_intf->statusbar->SetStatusText(
226 wxU(input_GetItem(p_input)->psz_name), 2 );
228 free( psz_now_playing );
231 void InputManager::UpdateButtons( vlc_bool_t b_play )
235 if( i_old_playing_status == STATUS_STOP ) return;
237 i_old_playing_status = STATUS_STOP;
238 p_main_intf->TogglePlayButton( PAUSE_S );
239 p_main_intf->statusbar->SetStatusText( wxT(""), 0 );
240 p_main_intf->statusbar->SetStatusText( wxT(""), 2 );
242 /* wxCocoa pretends to support this, but at least 2.6.x doesn't */
244 #ifdef wxHAS_TASK_BAR_ICON
245 if( p_main_intf->p_systray )
247 p_main_intf->p_systray->UpdateTooltip(
248 wxString(wxT("VLC media player - ")) + wxU(_("Stopped")) );
256 /* Manage Playing status */
258 var_Get( p_input, "state", &val );
259 val.i_int = val.i_int == PAUSE_S ? STATUS_PAUSE : STATUS_PLAYING;
260 if( i_old_playing_status != val.i_int )
262 i_old_playing_status = val.i_int;
263 p_main_intf->TogglePlayButton( val.i_int == STATUS_PAUSE ?
264 PAUSE_S : PLAYING_S );
266 /* wxCocoa pretends to support this, but at least 2.6.x doesn't */
268 #ifdef wxHAS_TASK_BAR_ICON
269 if( p_main_intf->p_systray )
271 p_main_intf->p_systray->UpdateTooltip(
272 wxU(input_GetItem(p_input)->psz_name) + wxString(wxT(" - ")) +
273 (val.i_int == PAUSE_S ? wxU(_("Paused")) : wxU(_("Playing"))));
280 void InputManager::UpdateDiscButtons()
283 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
284 if( val.i_int > 0 && !disc_frame->IsShown() )
288 #define HELP_MENU N_("Menu")
289 #define HELP_PCH N_("Previous chapter")
290 #define HELP_NCH N_("Next chapter")
291 #define HELP_PTR N_("Previous track")
292 #define HELP_NTR N_("Next track")
294 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
298 disc_menu_button->Show();
299 disc_sizer->Show( disc_menu_button );
300 disc_sizer->Layout();
301 disc_sizer->Fit( disc_frame );
302 disc_menu_button->SetToolTip( wxU(_( HELP_MENU ) ) );
303 disc_prev_button->SetToolTip( wxU(_( HELP_PCH ) ) );
304 disc_next_button->SetToolTip( wxU(_( HELP_NCH ) ) );
308 disc_menu_button->Hide();
309 disc_sizer->Hide( disc_menu_button );
310 disc_prev_button->SetToolTip( wxU(_( HELP_PTR ) ) );
311 disc_next_button->SetToolTip( wxU(_( HELP_NTR ) ) );
316 else if( val.i_int == 0 && disc_frame->IsShown() )
322 void InputManager::HideSlider()
327 void InputManager::HideDiscFrame()
329 ShowDiscFrame( false );
332 void InputManager::UpdateTime()
334 char psz_time[ MSTRTIME_MAX_SIZE ], psz_total[ MSTRTIME_MAX_SIZE ];
337 i_seconds = var_GetTime( p_intf->p_sys->p_input, "length" ) / 1000000;
338 secstotimestr( psz_total, i_seconds );
340 i_seconds = var_GetTime( p_intf->p_sys->p_input, "time" ) / 1000000;
341 secstotimestr( psz_time, i_seconds );
343 p_main_intf->statusbar->SetStatusText(
344 wxU(psz_time) + wxString(wxT(" / ")) +wxU(psz_total), 0 );
347 void InputManager::Update()
349 /* Update the input */
350 if( p_input == NULL )
356 slider->SetValue( 0 );
358 else if( !i_input_hide_delay )
360 i_input_hide_delay = mdate() + 200000;
362 else if( i_input_hide_delay < mdate() )
364 if( disc_frame->IsShown() ) HideDiscFrame();
365 if( slider->IsShown() ) HideSlider();
366 i_input_hide_delay = 0;
369 else if( p_input->b_dead )
371 UpdateButtons( VLC_FALSE );
372 vlc_object_release( p_input );
377 i_input_hide_delay = 0;
380 if( p_input && !p_input->b_die )
382 vlc_value_t pos, len;
385 UpdateButtons( VLC_TRUE );
389 /* Really manage the slider */
390 var_Get( p_input, "position", &pos );
391 var_Get( p_input, "length", &len );
393 if( pos.f_float > 0 && !slider->IsShown() ) ShowSlider();
394 else if( pos.f_float <= 0 && slider->IsShown() ) HideSlider();
396 /* Update the slider if the user isn't dragging it. */
397 if( slider->IsShown() && b_slider_free )
399 i_slider_pos = (int)(SLIDER_MAX_POS * pos.f_float);
400 slider->SetValue( i_slider_pos );
403 /* Manage Speed status */
405 var_Get( p_input, "rate", &val );
406 if( i_old_rate != val.i_int )
408 p_main_intf->statusbar->SetStatusText(
409 wxString::Format(wxT("x%.2f"),
410 (float)INPUT_RATE_DEFAULT / val.i_int ), 1 );
411 i_old_rate = val.i_int;
416 /*****************************************************************************
418 *****************************************************************************/
419 void InputManager::OnDiscMenu( wxCommandEvent& WXUNUSED(event) )
421 input_thread_t *p_input =
422 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
426 vlc_value_t val; val.i_int = 2;
428 var_Set( p_input, "title 0", val);
429 vlc_object_release( p_input );
433 void InputManager::OnDiscPrev( wxCommandEvent& WXUNUSED(event) )
435 input_thread_t *p_input =
436 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
440 int i_type = var_Type( p_input, "prev-chapter" );
441 vlc_value_t val; val.b_bool = VLC_TRUE;
443 var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
444 "prev-chapter" : "prev-title", val );
446 vlc_object_release( p_input );
450 void InputManager::OnDiscNext( wxCommandEvent& WXUNUSED(event) )
452 input_thread_t *p_input =
453 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
457 int i_type = var_Type( p_input, "next-chapter" );
458 vlc_value_t val; val.b_bool = VLC_TRUE;
460 var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
461 "next-chapter" : "next-title", val );
463 vlc_object_release( p_input );
467 void InputManager::OnSliderUpdate( wxScrollEvent& event )
469 vlc_mutex_lock( &p_intf->change_lock );
472 if( event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE
473 || event.GetEventType() == wxEVT_SCROLL_ENDSCROLL )
476 if( i_slider_pos != event.GetPosition() && p_intf->p_sys->p_input )
479 pos.f_float = (float)event.GetPosition() / (float)SLIDER_MAX_POS;
480 var_Set( p_intf->p_sys->p_input, "position", pos );
484 b_slider_free = VLC_TRUE;
488 b_slider_free = VLC_FALSE;
489 if( p_intf->p_sys->p_input ) UpdateTime();
494 vlc_mutex_unlock( &p_intf->change_lock );
497 void InputManager::ShowSlider( bool show )
499 if( !!show == !!slider->IsShown() ) return;
501 UpdateVideoWindow( p_intf, p_main_intf->video_window );
503 sizer->Show( slider, show );
506 wxCommandEvent intf_event( wxEVT_INTF, 0 );
507 p_main_intf->AddPendingEvent( intf_event );
510 void InputManager::ShowDiscFrame( bool show )
512 if( !!show == !!disc_frame->IsShown() ) return;
514 UpdateVideoWindow( p_intf, p_main_intf->video_window );
516 sizer->Show( disc_frame, show );
519 wxCommandEvent intf_event( wxEVT_INTF, 0 );
520 p_main_intf->AddPendingEvent( intf_event );