]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/video.cpp
* modules/gui/wxwindows/*: implemented VOUT_SET_STAY_ON_TOP in ControlWindow()
[vlc] / modules / gui / wxwindows / video.cpp
1 /*****************************************************************************
2  * video.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004, 2003 VideoLAN
5  * $Id: interface.cpp 6961 2004-03-05 17:34:23Z sam $
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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 <vlc/vlc.h>
28 #include <vlc/vout.h>
29 #include <vlc/intf.h>
30 #include "stream_control.h"
31
32 #include "wxwindows.h"
33
34 static void *GetWindow( intf_thread_t *p_intf, vout_thread_t *,
35                         int *pi_x_hint, int *pi_y_hint,
36                         unsigned int *pi_width_hint,
37                         unsigned int *pi_height_hint );
38
39 static void ReleaseWindow( intf_thread_t *p_intf, void *p_window );
40
41 static int ControlWindow( intf_thread_t *p_intf, void *p_window,
42                           int i_query, va_list args );
43
44 /* IDs for the controls and the menu commands */
45 enum
46 {
47     UpdateSize_Event = wxID_HIGHEST + 1,
48     UpdateHide_Event,
49     SetStayOnTop_Event,
50 };
51
52 class VideoWindow: public wxWindow
53 {
54 public:
55     /* Constructor */
56     VideoWindow( intf_thread_t *_p_intf, wxWindow *p_parent );
57     virtual ~VideoWindow();
58
59     void *GetWindow( vout_thread_t *p_vout, int *, int *,
60                      unsigned int *, unsigned int * );
61     void ReleaseWindow( void * );
62     int  ControlWindow( void *, int, va_list );
63
64 private:
65     intf_thread_t *p_intf;
66     vout_thread_t *p_vout;
67     wxWindow *p_parent;
68     vlc_mutex_t lock;
69
70     wxWindow *p_child_window;
71
72     void UpdateSize( wxSizeEvent & );
73     void UpdateHide( wxSizeEvent & );
74     void OnControlEvent( wxCommandEvent & );
75
76     DECLARE_EVENT_TABLE();
77 };
78
79 DEFINE_LOCAL_EVENT_TYPE( wxEVT_VLC_VIDEO );
80
81 BEGIN_EVENT_TABLE(VideoWindow, wxWindow)
82     EVT_CUSTOM( wxEVT_SIZE, UpdateSize_Event, VideoWindow::UpdateSize )
83     EVT_CUSTOM( wxEVT_SIZE, UpdateHide_Event, VideoWindow::UpdateHide )
84     EVT_COMMAND( SetStayOnTop_Event, wxEVT_VLC_VIDEO,
85                  VideoWindow::OnControlEvent )
86 END_EVENT_TABLE()
87
88 /*****************************************************************************
89  * Public methods.
90  *****************************************************************************/
91 wxWindow *VideoWindow( intf_thread_t *p_intf, wxWindow *p_parent )
92 {
93     return new VideoWindow::VideoWindow( p_intf, p_parent );
94 }
95
96 /*****************************************************************************
97  * Constructor.
98  *****************************************************************************/
99 VideoWindow::VideoWindow( intf_thread_t *_p_intf, wxWindow *_p_parent ):
100     wxWindow( _p_parent, -1 )
101 {
102     /* Initializations */
103     p_intf = _p_intf;
104     p_parent = _p_parent;
105
106     vlc_mutex_init( p_intf, &lock );
107
108     p_vout = NULL;
109
110     p_intf->pf_request_window = ::GetWindow;
111     p_intf->pf_release_window = ::ReleaseWindow;
112     p_intf->pf_control_window = ::ControlWindow;
113
114     p_intf->p_sys->p_video_window = this;
115     p_child_window = new wxWindow( this, -1, wxDefaultPosition, wxSize(0,0) );
116     p_child_window->Show();
117     Show();
118
119     p_intf->p_sys->p_video_sizer = new wxBoxSizer( wxHORIZONTAL );
120     p_intf->p_sys->p_video_sizer->Add( this, 1, wxEXPAND );
121
122     ReleaseWindow( NULL );
123 }
124
125 VideoWindow::~VideoWindow()
126 {
127     vlc_mutex_lock( &lock );
128     if( p_vout )
129     {
130         if( vout_Control( p_vout, VOUT_REPARENT ) != VLC_SUCCESS )
131             vout_Control( p_vout, VOUT_CLOSE );
132     }
133     vlc_mutex_unlock( &lock );
134
135     vlc_mutex_destroy( &lock );
136 }
137
138 /*****************************************************************************
139  * Private methods.
140  *****************************************************************************/
141 static void *GetWindow( intf_thread_t *p_intf, vout_thread_t *p_vout,
142                         int *pi_x_hint, int *pi_y_hint,
143                         unsigned int *pi_width_hint,
144                         unsigned int *pi_height_hint )
145 {
146     return p_intf->p_sys->p_video_window->GetWindow( p_vout,
147                                                      pi_x_hint, pi_y_hint,
148                                                      pi_width_hint,
149                                                      pi_height_hint );
150 }
151
152 /* Part of the hack to get the X11 window handle from the GtkWidget */
153 #ifdef __WXGTK__
154 extern "C" {
155 #ifdef __WXGTK20__
156     int gdk_x11_drawable_get_xid( void * );
157 #endif
158     void *gtk_widget_get_parent_window( void * );
159 }
160 #endif
161
162 void *VideoWindow::GetWindow( vout_thread_t *_p_vout,
163                               int *pi_x_hint, int *pi_y_hint,
164                               unsigned int *pi_width_hint,
165                               unsigned int *pi_height_hint )
166 {
167 #if defined(__WXGTK__) || defined(WIN32)
168     vlc_mutex_lock( &lock );
169
170     if( p_vout )
171     {
172         msg_Dbg( p_intf, "Video window already in use" );
173         return NULL;
174     }
175
176     p_vout = _p_vout;
177
178     wxSizeEvent event( wxSize(*pi_width_hint, *pi_height_hint),
179                        UpdateSize_Event );
180     AddPendingEvent( event );
181     vlc_mutex_unlock( &lock );
182
183 #ifdef __WXGTK__
184     GtkWidget *p_widget = p_child_window->GetHandle();
185
186 #ifdef __WXGTK20__
187     return (void *)gdk_x11_drawable_get_xid(
188                gtk_widget_get_parent_window( p_widget ) );
189 #elif defined(__WXGTK__)
190     return (void *)*(int *)( (char *)gtk_widget_get_parent_window( p_widget )
191                + 2 * sizeof(void *) );
192 #endif
193
194 #elif defined(WIN32)
195     return (void*)GetHandle();
196
197 #endif
198
199 #else // defined(__WXGTK__) || defined(WIN32)
200     return NULL;
201
202 #endif
203 }
204
205 static void ReleaseWindow( intf_thread_t *p_intf, void *p_window )
206 {
207     return p_intf->p_sys->p_video_window->ReleaseWindow( p_window );
208 }
209
210 void VideoWindow::ReleaseWindow( void *p_window )
211 {
212     vlc_mutex_lock( &lock );
213
214     p_vout = NULL;
215
216 #if defined(__WXGTK__) || defined(WIN32)
217     wxSizeEvent event( wxSize(0, 0), UpdateHide_Event );
218     AddPendingEvent( event );
219 #endif
220
221     vlc_mutex_unlock( &lock );
222 }
223
224 void VideoWindow::UpdateSize( wxSizeEvent &event )
225 {
226     if( !IsShown() )
227     {
228         p_intf->p_sys->p_video_sizer->Show( this, TRUE );
229         p_intf->p_sys->p_video_sizer->Layout();
230         SetFocus();
231     }
232     p_intf->p_sys->p_video_sizer->SetMinSize( event.GetSize() );
233
234     wxCommandEvent intf_event( wxEVT_INTF, 0 );
235     p_parent->AddPendingEvent( intf_event );
236 }
237
238 void VideoWindow::UpdateHide( wxSizeEvent &event )
239 {
240     if( IsShown() )
241     {
242         p_intf->p_sys->p_video_sizer->Show( this, FALSE );
243         p_intf->p_sys->p_video_sizer->Layout();
244     }
245     p_intf->p_sys->p_video_sizer->SetMinSize( event.GetSize() );
246
247     wxCommandEvent intf_event( wxEVT_INTF, 0 );
248     p_parent->AddPendingEvent( intf_event );
249 }
250
251 void VideoWindow::OnControlEvent( wxCommandEvent &event )
252 {
253     switch( event.GetId() )
254     {
255     case SetStayOnTop_Event:
256         wxCommandEvent intf_event( wxEVT_INTF, 1 );
257         intf_event.SetInt( event.GetInt() );
258         p_parent->AddPendingEvent( intf_event );
259         break;
260     }
261 }
262
263 static int ControlWindow( intf_thread_t *p_intf, void *p_window,
264                           int i_query, va_list args )
265 {
266     return p_intf->p_sys->p_video_window->ControlWindow( p_window, i_query,
267                                                          args );
268 }
269
270 int VideoWindow::ControlWindow( void *p_window, int i_query, va_list args )
271 {
272     int i_ret = VLC_EGENERIC;
273
274     vlc_mutex_lock( &lock );
275
276     switch( i_query )
277     {
278         case VOUT_SET_ZOOM:
279         {
280             double f_arg = va_arg( args, double );
281
282             /* Update dimensions */
283             wxSizeEvent event( wxSize(p_vout->render.i_width * f_arg,
284                                       p_vout->render.i_height * f_arg),
285                                UpdateSize_Event );
286             AddPendingEvent( event );
287
288             i_ret = VLC_SUCCESS;
289         }
290         break;
291
292         case VOUT_SET_STAY_ON_TOP:
293         {
294             int i_arg = va_arg( args, int );
295             wxCommandEvent event( wxEVT_VLC_VIDEO, SetStayOnTop_Event );
296             event.SetInt( i_arg );
297             AddPendingEvent( event );
298
299             i_ret = VLC_SUCCESS;
300         }
301         break;
302
303         default:
304             msg_Dbg( p_intf, "control query not supported" );
305             break;
306     }
307
308     vlc_mutex_unlock( &lock );
309
310     return i_ret;
311 }