]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/timer.cpp
* modules/gui/wxwindows/*: enable popup menu support in the "dialogs provider".
[vlc] / modules / gui / wxwindows / timer.cpp
1 /*****************************************************************************
2  * timer.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: timer.cpp,v 1.27 2003/07/17 18:58:23 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/aout.h>
34
35 #ifdef WIN32                                                 /* mingw32 hack */
36 #undef Yield
37 #undef CreateDialog
38 #endif
39
40 /* Let vlc take care of the i18n stuff */
41 #define WXINTL_NO_GETTEXT_MACRO
42
43 #include <wx/wxprec.h>
44 #include <wx/wx.h>
45 #include <wx/timer.h>
46
47 #include <vlc/intf.h>
48
49 #include "wxwindows.h"
50
51 void DisplayStreamDate( wxControl *, intf_thread_t *, int );
52
53 /* Callback prototype */
54 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
55                        vlc_value_t old_val, vlc_value_t new_val, void *param );
56
57 /*****************************************************************************
58  * Constructor.
59  *****************************************************************************/
60 Timer::Timer( intf_thread_t *_p_intf, Interface *_p_main_interface )
61 {
62     p_intf = _p_intf;
63     p_main_interface = _p_main_interface;
64     i_old_playing_status = PAUSE_S;
65     i_old_rate = DEFAULT_RATE;
66
67     /* Register callback for the intf-popupmenu variable */
68     playlist_t *p_playlist =
69         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
70                                        FIND_ANYWHERE );
71     if( p_playlist != NULL )
72     {
73         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
74         vlc_object_release( p_playlist );
75     }
76
77     Start( 100 /*milliseconds*/, wxTIMER_CONTINUOUS );
78 }
79
80 Timer::~Timer()
81 {
82 }
83
84 /*****************************************************************************
85  * Private methods.
86  *****************************************************************************/
87
88 /*****************************************************************************
89  * Manage: manage main thread messages
90  *****************************************************************************
91  * In this function, called approx. 10 times a second, we check what the
92  * main program wanted to tell us.
93  *****************************************************************************/
94 void Timer::Notify()
95 {
96     vlc_bool_t b_pace_control;
97
98     vlc_mutex_lock( &p_intf->change_lock );
99
100     /* If the "display popup" flag has changed */
101     if( p_intf->p_sys->b_popup_change )
102     {
103 #if 0
104         wxPoint mousepos = wxGetMousePosition();
105
106         wxMouseEvent event = wxMouseEvent( wxEVT_RIGHT_UP );
107         event.m_x = p_main_interface->ScreenToClient(mousepos).x;
108         event.m_y = p_main_interface->ScreenToClient(mousepos).y;
109
110         p_main_interface->AddPendingEvent(event);
111 #endif
112         if( p_intf->p_sys->pf_show_dialog )
113             p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU, 0 );
114
115         p_intf->p_sys->b_popup_change = VLC_FALSE;
116     }
117
118     /* Update the input */
119     if( p_intf->p_sys->p_input == NULL )
120     {
121         p_intf->p_sys->p_input = (input_thread_t *)vlc_object_find( p_intf,
122                                                        VLC_OBJECT_INPUT,
123                                                        FIND_ANYWHERE );
124
125         /* Show slider */
126         if( p_intf->p_sys->p_input )
127         {
128             //if( p_intf->p_sys->p_input->stream.b_seekable )
129             {
130                 p_main_interface->slider_frame->Show();
131                 p_main_interface->frame_sizer->Show(
132                     p_main_interface->slider_frame );
133                 p_main_interface->frame_sizer->Layout();
134                 p_main_interface->frame_sizer->Fit( p_main_interface );
135             }
136
137             p_main_interface->statusbar->SetStatusText(
138                 wxU(p_intf->p_sys->p_input->psz_source), 2 );
139
140             p_main_interface->TogglePlayButton( PLAYING_S );
141             i_old_playing_status = PLAYING_S;
142
143             /* Take care of the volume */
144             audio_volume_t i_volume;
145             aout_VolumeGet( p_intf, &i_volume );
146             p_main_interface->volctrl->SetValue( i_volume * 200 /
147                                                  AOUT_VOLUME_MAX );
148             p_main_interface->volctrl->SetToolTip(
149                 wxString::Format((wxString)wxU(_("Volume")) + wxT(" %d"),
150                 i_volume * 200 / AOUT_VOLUME_MAX ) );
151
152             /* control buttons for free pace streams */
153             b_pace_control = p_intf->p_sys->p_input->stream.b_pace_control;
154         }
155     }
156     else if( p_intf->p_sys->p_input->b_dead )
157     {
158         /* Hide slider */
159         //if( p_intf->p_sys->p_input->stream.b_seekable )
160         {
161             p_main_interface->slider_frame->Hide();
162             p_main_interface->frame_sizer->Hide(
163                 p_main_interface->slider_frame );
164             p_main_interface->frame_sizer->Layout();
165             p_main_interface->frame_sizer->Fit( p_main_interface );
166
167             p_main_interface->TogglePlayButton( PAUSE_S );
168             i_old_playing_status = PAUSE_S;
169         }
170
171         p_main_interface->statusbar->SetStatusText( wxT(""), 2 );
172
173         vlc_object_release( p_intf->p_sys->p_input );
174         p_intf->p_sys->p_input = NULL;
175     }
176
177
178
179     if( p_intf->p_sys->p_input )
180     {
181         input_thread_t *p_input = p_intf->p_sys->p_input;
182
183         vlc_mutex_lock( &p_input->stream.stream_lock );
184
185         if( !p_input->b_die )
186         {
187             /* New input or stream map change */
188             p_intf->p_sys->b_playing = 1;
189 #if 0
190             if( p_input->stream.b_changed )
191             {
192                 wxModeManage( p_intf );
193                 wxSetupMenus( p_intf );
194                 p_intf->p_sys->b_playing = 1;
195
196                 p_main_interface->TogglePlayButton( PLAYING_S );
197                 i_old_playing_status = PLAYING_S;
198             }
199 #endif
200
201             /* Manage the slider */
202             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
203             {
204                 stream_position_t position;
205
206                 /* Update the slider if the user isn't dragging it. */
207                 if( p_intf->p_sys->b_slider_free )
208                 {
209                     /* Update the value */
210                     vlc_mutex_unlock( &p_input->stream.stream_lock );
211                     input_Tell( p_input, &position );
212                     vlc_mutex_lock( &p_input->stream.stream_lock );
213                     if( position.i_size )
214                     {
215                         p_intf->p_sys->i_slider_pos =
216                         ( SLIDER_MAX_POS * position.i_tell ) / position.i_size;
217
218                         p_main_interface->slider->SetValue(
219                             p_intf->p_sys->i_slider_pos );
220
221                         DisplayStreamDate( p_main_interface->slider_box,p_intf,
222                                            p_intf->p_sys->i_slider_pos );
223                     }
224                 }
225             }
226
227             /* Manage Playing status */
228             if( i_old_playing_status != p_input->stream.control.i_status )
229             {
230                 if( p_input->stream.control.i_status == PAUSE_S )
231                 {
232                     p_main_interface->TogglePlayButton( PAUSE_S );
233                 }
234                 else
235                 {
236                     p_main_interface->TogglePlayButton( PLAYING_S );
237                 }
238                 i_old_playing_status = p_input->stream.control.i_status;
239             }
240
241             /* Manage Speed status */
242             if( i_old_rate != p_input->stream.control.i_rate )
243             {
244                 p_main_interface->statusbar->SetStatusText(
245                     wxString::Format(wxT("x%.2f"),
246                     1000.0 / p_input->stream.control.i_rate), 1 );
247                 i_old_rate = p_input->stream.control.i_rate;
248             }
249         }
250
251         vlc_mutex_unlock( &p_input->stream.stream_lock );
252     }
253     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
254     {
255         p_intf->p_sys->b_playing = 0;
256         p_main_interface->TogglePlayButton( PAUSE_S );
257         i_old_playing_status = PAUSE_S;
258     }
259
260     if( p_intf->b_die )
261     {
262         vlc_mutex_unlock( &p_intf->change_lock );
263
264         /* Prepare to die, young Skywalker */
265         p_main_interface->Close(TRUE);
266         return;
267     }
268
269     vlc_mutex_unlock( &p_intf->change_lock );
270 }
271
272 /*****************************************************************************
273  * DisplayStreamDate: display stream date
274  *****************************************************************************
275  * This function displays the current date related to the position in
276  * the stream. It is called whenever the slider changes its value.
277  * The lock has to be taken before you call the function.
278  *****************************************************************************/
279 void DisplayStreamDate( wxControl *p_slider_frame, intf_thread_t * p_intf ,
280                         int i_pos )
281 {
282     if( p_intf->p_sys->p_input )
283     {
284 #define p_area p_intf->p_sys->p_input->stream.p_selected_area
285         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
286
287         p_slider_frame->SetLabel(
288             wxU(input_OffsetToTime( p_intf->p_sys->p_input,
289                     psz_time, p_area->i_size * i_pos / SLIDER_MAX_POS )) );
290 #undef p_area
291      }
292 }
293
294 /*****************************************************************************
295  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
296  *  We don't show the menu directly here because we don't want the
297  *  caller to block for a too long time.
298  *****************************************************************************/
299 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
300                         vlc_value_t old_val, vlc_value_t new_val, void *param )
301 {
302     intf_thread_t *p_intf = (intf_thread_t *)param;
303
304     p_intf->p_sys->b_popup_change = VLC_TRUE;
305
306     return VLC_SUCCESS;
307 }