* modules/gui/wxwindows/*: the wxWindows plugin is now UNICODE happy.
[vlc.git] / 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.16 2003/05/11 13:22: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
34 #ifdef WIN32                                                 /* mingw32 hack */
35 #undef Yield
36 #undef CreateDialog
37 #endif
38
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
41
42 #include <wx/wxprec.h>
43 #include <wx/wx.h>
44 #include <wx/timer.h>
45
46 #include <vlc/intf.h>
47
48 #include "wxwindows.h"
49
50 void DisplayStreamDate( wxControl *, intf_thread_t *, int );
51
52 /*****************************************************************************
53  * Constructor.
54  *****************************************************************************/
55 Timer::Timer( intf_thread_t *_p_intf, Interface *_p_main_interface )
56 {
57     p_intf = _p_intf;
58     p_main_interface = _p_main_interface;
59     i_old_playing_status = PAUSE_S;
60
61     Start( 100 /*milliseconds*/, wxTIMER_CONTINUOUS );
62 }
63
64 Timer::~Timer()
65 {
66 }
67
68 /*****************************************************************************
69  * Private methods.
70  *****************************************************************************/
71 /*****************************************************************************
72  * wxModeManage: actualise the aspect of the interface whenever the input
73  * changes.
74  *****************************************************************************
75  * The lock has to be taken before you call the function.
76  *****************************************************************************/
77 static int wxModeManage( intf_thread_t * p_intf )
78 {
79     return 0;
80 }
81
82 /*****************************************************************************
83  * wxSetupMenus: function that generates title/chapter/audio/subpic
84  * menus with help from preceding functions
85  *****************************************************************************
86  * Function called with the lock on stream
87  *****************************************************************************/
88 static int wxSetupMenus( intf_thread_t * p_intf )
89 {
90     return 0;
91 }
92
93 /*****************************************************************************
94  * Manage: manage main thread messages
95  *****************************************************************************
96  * In this function, called approx. 10 times a second, we check what the
97  * main program wanted to tell us.
98  *****************************************************************************/
99 void Timer::Notify()
100 {
101     vlc_bool_t b_pace_control;
102
103     vlc_mutex_lock( &p_intf->change_lock );
104
105     /* If the "display popup" flag has changed */
106     if( p_intf->b_menu_change )
107     {
108         wxPoint mousepos = wxGetMousePosition();
109         PopupMenu( p_intf, p_main_interface,
110                    p_main_interface->ScreenToClient(mousepos) );
111         p_intf->b_menu_change = 0;
112     }
113
114     /* Update the log window */
115     p_intf->p_sys->p_messages_window->UpdateLog();
116
117     /* Update the playlist */
118     p_intf->p_sys->p_playlist_window->Manage();
119
120     /* Update the fileinfo windows */
121     p_intf->p_sys->p_fileinfo_window->UpdateFileInfo();
122     
123     /* Update the input */
124     if( p_intf->p_sys->p_input == NULL )
125     {
126         p_intf->p_sys->p_input = (input_thread_t *)vlc_object_find( p_intf,
127                                                        VLC_OBJECT_INPUT,
128                                                        FIND_ANYWHERE );
129
130         /* Show slider */
131         if( p_intf->p_sys->p_input )
132         {
133             //if( p_intf->p_sys->p_input->stream.b_seekable )
134             {
135                 p_main_interface->slider_frame->Show();
136                 p_main_interface->frame_sizer->Show(
137                     p_main_interface->slider_frame );
138                 p_main_interface->frame_sizer->Layout();
139                 p_main_interface->frame_sizer->Fit( p_main_interface );
140             }
141
142             p_main_interface->statusbar->SetStatusText(
143                 wxU(p_intf->p_sys->p_input->psz_source), 1 );
144
145             p_main_interface->TogglePlayButton( PLAYING_S );
146             i_old_playing_status = PLAYING_S;
147         }
148
149         /* control buttons for free pace streams */
150         b_pace_control = p_intf->p_sys->p_input->stream.b_pace_control;
151
152     }
153     else if( p_intf->p_sys->p_input->b_dead )
154     {
155         /* Hide slider */
156         //if( p_intf->p_sys->p_input->stream.b_seekable )
157         {
158             p_main_interface->slider_frame->Hide();
159             p_main_interface->frame_sizer->Hide(
160                 p_main_interface->slider_frame );
161             p_main_interface->frame_sizer->Layout();
162             p_main_interface->frame_sizer->Fit( p_main_interface );
163
164             p_main_interface->TogglePlayButton( PAUSE_S );
165             i_old_playing_status = PAUSE_S;
166         }
167
168         p_main_interface->statusbar->SetStatusText( wxT(""), 1 );
169
170         vlc_object_release( p_intf->p_sys->p_input );
171         p_intf->p_sys->p_input = NULL;
172     }
173
174
175
176     if( p_intf->p_sys->p_input )
177     {
178         input_thread_t *p_input = p_intf->p_sys->p_input;
179
180         vlc_mutex_lock( &p_input->stream.stream_lock );
181
182         if( !p_input->b_die )
183         {
184             /* New input or stream map change */
185             p_intf->p_sys->b_playing = 1;
186 #if 0
187             if( p_input->stream.b_changed )
188             {
189                 wxModeManage( p_intf );
190                 wxSetupMenus( p_intf );
191                 p_intf->p_sys->b_playing = 1;
192
193                 p_main_interface->TogglePlayButton( PLAYING_S );
194                 i_old_playing_status = PLAYING_S;
195             }
196 #endif
197
198             /* Manage the slider */
199             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
200             {
201                 stream_position_t position;
202
203                 /* Update the slider if the user isn't dragging it. */
204                 if( p_intf->p_sys->b_slider_free )
205                 {
206                     /* Update the value */
207                     vlc_mutex_unlock( &p_input->stream.stream_lock );
208                     input_Tell( p_input, &position );
209                     vlc_mutex_lock( &p_input->stream.stream_lock );
210                     if( position.i_size )
211                     {
212                         p_intf->p_sys->i_slider_pos =
213                         ( SLIDER_MAX_POS * position.i_tell ) / position.i_size;
214
215                         p_main_interface->slider->SetValue(
216                             p_intf->p_sys->i_slider_pos );
217
218                         DisplayStreamDate( p_main_interface->slider_box,p_intf,
219                                            p_intf->p_sys->i_slider_pos );
220                     }
221                 }
222             }
223
224             if( p_intf->p_sys->i_part !=
225                 p_input->stream.p_selected_area->i_part )
226             {
227                 p_intf->p_sys->b_chapter_update = 1;
228                 wxSetupMenus( p_intf );
229             }
230
231             /* Manage Playing status */
232             if( i_old_playing_status != p_input->stream.control.i_status )
233             {
234                 if( p_input->stream.control.i_status == PAUSE_S )
235                 {
236                     p_main_interface->TogglePlayButton( PAUSE_S );
237                 }
238                 else
239                 {
240                     p_main_interface->TogglePlayButton( PLAYING_S );
241                 }
242                 i_old_playing_status = p_input->stream.control.i_status;
243             }
244         }
245
246         vlc_mutex_unlock( &p_input->stream.stream_lock );
247     }
248     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
249     {
250         wxModeManage( p_intf );
251         p_intf->p_sys->b_playing = 0;
252         p_main_interface->TogglePlayButton( PAUSE_S );
253         i_old_playing_status = PAUSE_S;
254     }
255
256     if( p_intf->b_die )
257     {
258         vlc_mutex_unlock( &p_intf->change_lock );
259
260         /* Prepare to die, young Skywalker */
261         p_main_interface->Close(TRUE);
262         return;
263     }
264
265     vlc_mutex_unlock( &p_intf->change_lock );
266 }
267
268 /*****************************************************************************
269  * DisplayStreamDate: display stream date
270  *****************************************************************************
271  * This function displays the current date related to the position in
272  * the stream. It is called whenever the slider changes its value.
273  * The lock has to be taken before you call the function.
274  *****************************************************************************/
275 void DisplayStreamDate( wxControl *p_slider_frame, intf_thread_t * p_intf ,
276                         int i_pos )
277 {
278     if( p_intf->p_sys->p_input )
279     {
280 #define p_area p_intf->p_sys->p_input->stream.p_selected_area
281         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
282
283         p_slider_frame->SetLabel(
284             wxU(input_OffsetToTime( p_intf->p_sys->p_input,
285                     psz_time, p_area->i_size * i_pos / SLIDER_MAX_POS )) );
286 #undef p_area
287      }
288 }