1 /*****************************************************************************
2 * video.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2004, 2003 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include "wxwindows.h"
33 static void *GetWindow( intf_thread_t *p_intf, vout_thread_t *,
34 int *pi_x_hint, int *pi_y_hint,
35 unsigned int *pi_width_hint,
36 unsigned int *pi_height_hint );
38 static void ReleaseWindow( intf_thread_t *p_intf, void *p_window );
40 static int ControlWindow( intf_thread_t *p_intf, void *p_window,
41 int i_query, va_list args );
43 /* IDs for the controls and the menu commands */
46 UpdateSize_Event = wxID_HIGHEST + 1,
52 class VideoWindow: public wxWindow
56 VideoWindow( intf_thread_t *_p_intf, wxWindow *p_parent );
57 virtual ~VideoWindow();
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 );
65 intf_thread_t *p_intf;
66 vout_thread_t *p_vout;
70 vlc_bool_t b_auto_size;
72 wxWindow *p_child_window;
76 void UpdateSize( wxEvent& event );
77 void UpdateHide( wxEvent& event );
78 void OnControlEvent( wxCommandEvent& event );
79 void OnHideTimer( wxTimerEvent& WXUNUSED(event));
81 DECLARE_EVENT_TABLE();
84 DEFINE_LOCAL_EVENT_TYPE( wxEVT_VLC_VIDEO );
86 BEGIN_EVENT_TABLE(VideoWindow, wxWindow)
87 EVT_CUSTOM( wxEVT_SIZE, UpdateSize_Event, VideoWindow::UpdateSize )
88 EVT_CUSTOM( wxEVT_SIZE, UpdateHide_Event, VideoWindow::UpdateHide )
89 EVT_COMMAND( SetStayOnTop_Event, wxEVT_VLC_VIDEO,
90 VideoWindow::OnControlEvent )
91 EVT_TIMER( ID_HIDE_TIMER, VideoWindow::OnHideTimer )
94 /*****************************************************************************
96 *****************************************************************************/
97 wxWindow *CreateVideoWindow( intf_thread_t *p_intf, wxWindow *p_parent )
99 return new VideoWindow( p_intf, p_parent );
102 /*****************************************************************************
104 *****************************************************************************/
105 VideoWindow::VideoWindow( intf_thread_t *_p_intf, wxWindow *_p_parent ):
106 wxWindow( _p_parent, -1 )
108 /* Initializations */
110 p_parent = _p_parent;
112 vlc_mutex_init( p_intf, &lock );
114 b_auto_size = config_GetInt( p_intf, "wxwin-autosize" );
118 m_hide_timer.SetOwner( this, ID_HIDE_TIMER );
120 p_intf->pf_request_window = ::GetWindow;
121 p_intf->pf_release_window = ::ReleaseWindow;
122 p_intf->pf_control_window = ::ControlWindow;
124 p_intf->p_sys->p_video_window = this;
126 wxSize child_size = wxSize(0,0);
129 // Maybe this size should be an option
130 child_size = wxSize( wxSystemSettings::GetMetric(wxSYS_SCREEN_X) / 2,
131 wxSystemSettings::GetMetric(wxSYS_SCREEN_Y) / 2 );
133 SetSize( child_size );
136 p_child_window = new wxWindow( this, -1, wxDefaultPosition, child_size );
138 p_child_window->Show();
142 p_child_window->SetBackgroundColour( *wxBLACK );
143 SetBackgroundColour( *wxBLACK );
145 p_intf->p_sys->p_video_sizer = new wxBoxSizer( wxHORIZONTAL );
146 p_intf->p_sys->p_video_sizer->Add( this, 1, wxEXPAND );
148 ReleaseWindow( NULL );
151 VideoWindow::~VideoWindow()
153 vlc_mutex_lock( &lock );
156 if( !p_intf->psz_switch_intf )
158 if( vout_Control( p_vout, VOUT_CLOSE ) != VLC_SUCCESS )
159 vout_Control( p_vout, VOUT_REPARENT );
163 if( vout_Control( p_vout, VOUT_REPARENT ) != VLC_SUCCESS )
164 vout_Control( p_vout, VOUT_CLOSE );
168 p_intf->pf_request_window = NULL;
169 p_intf->pf_release_window = NULL;
170 p_intf->pf_control_window = NULL;
171 vlc_mutex_unlock( &lock );
173 vlc_mutex_destroy( &lock );
176 /*****************************************************************************
178 *****************************************************************************/
179 static void *GetWindow( intf_thread_t *p_intf, vout_thread_t *p_vout,
180 int *pi_x_hint, int *pi_y_hint,
181 unsigned int *pi_width_hint,
182 unsigned int *pi_height_hint )
184 return p_intf->p_sys->p_video_window->GetWindow( p_vout,
185 pi_x_hint, pi_y_hint,
190 /* Part of the hack to get the X11 window handle from the GtkWidget */
194 int gdk_x11_drawable_get_xid( void * );
196 void *gtk_widget_get_parent_window( void * );
200 void *VideoWindow::GetWindow( vout_thread_t *_p_vout,
201 int *pi_x_hint, int *pi_y_hint,
202 unsigned int *pi_width_hint,
203 unsigned int *pi_height_hint )
205 #if defined(__WXGTK__) || defined(WIN32)
206 vlc_mutex_lock( &lock );
210 vlc_mutex_unlock( &lock );
211 msg_Dbg( p_intf, "Video window already in use" );
217 wxSizeEvent event( wxSize(*pi_width_hint, *pi_height_hint),
219 AddPendingEvent( event );
220 vlc_mutex_unlock( &lock );
223 GtkWidget *p_widget = p_child_window->GetHandle();
226 return (void *)gdk_x11_drawable_get_xid(
227 gtk_widget_get_parent_window( p_widget ) );
228 #elif defined(__WXGTK__)
229 return (void *)*(int *)( (char *)gtk_widget_get_parent_window( p_widget )
230 + 2 * sizeof(void *) );
234 return (void*)GetHandle();
238 #else // defined(__WXGTK__) || defined(WIN32)
244 static void ReleaseWindow( intf_thread_t *p_intf, void *p_window )
246 return p_intf->p_sys->p_video_window->ReleaseWindow( p_window );
249 void VideoWindow::ReleaseWindow( void *p_window )
251 vlc_mutex_lock( &lock );
253 vlc_mutex_unlock( &lock );
255 if( !b_auto_size ) return;
257 #if defined(__WXGTK__) || defined(WIN32)
258 wxSizeEvent event( wxSize(0, 0), UpdateHide_Event );
259 AddPendingEvent( event );
263 void VideoWindow::UpdateSize( wxEvent &_event )
267 if( !b_auto_size ) return;
269 wxSizeEvent * event = (wxSizeEvent*)(&_event);
272 p_intf->p_sys->p_video_sizer->Show( this, TRUE );
273 p_intf->p_sys->p_video_sizer->Layout();
277 p_intf->p_sys->p_video_sizer->SetMinSize( event->GetSize() );
279 wxCommandEvent intf_event( wxEVT_INTF, 0 );
280 p_parent->AddPendingEvent( intf_event );
283 void VideoWindow::UpdateHide( wxEvent &_event )
285 if( b_auto_size) m_hide_timer.Start( 200, wxTIMER_ONE_SHOT );
288 void VideoWindow::OnHideTimer( wxTimerEvent& WXUNUSED(event))
290 //wxSizeEvent * event = (wxSizeEvent*)(&_event);
293 p_intf->p_sys->p_video_sizer->Show( this, FALSE );
294 p_intf->p_sys->p_video_sizer->Layout();
300 //ok I cheat here, but is it ever not 0,0?
301 p_intf->p_sys->p_video_sizer->SetMinSize( wxSize(0,0) );
303 wxCommandEvent intf_event( wxEVT_INTF, 0 );
304 p_parent->AddPendingEvent( intf_event );
307 void VideoWindow::OnControlEvent( wxCommandEvent &event )
309 switch( event.GetId() )
311 case SetStayOnTop_Event:
312 wxCommandEvent intf_event( wxEVT_INTF, 1 );
313 intf_event.SetInt( event.GetInt() );
314 p_parent->AddPendingEvent( intf_event );
319 static int ControlWindow( intf_thread_t *p_intf, void *p_window,
320 int i_query, va_list args )
322 return p_intf->p_sys->p_video_window->ControlWindow( p_window, i_query,
326 int VideoWindow::ControlWindow( void *p_window, int i_query, va_list args )
328 int i_ret = VLC_EGENERIC;
330 vlc_mutex_lock( &lock );
336 if( !b_auto_size ) break;
338 double f_arg = va_arg( args, double );
340 /* Update dimensions */
341 wxSizeEvent event( wxSize((int)(p_vout->i_window_width * f_arg),
342 (int)(p_vout->i_window_height * f_arg)),
346 AddPendingEvent( event );
352 case VOUT_SET_STAY_ON_TOP:
354 int i_arg = va_arg( args, int );
355 wxCommandEvent event( wxEVT_VLC_VIDEO, SetStayOnTop_Event );
356 event.SetInt( i_arg );
357 AddPendingEvent( event );
364 msg_Dbg( p_intf, "control query not supported" );
368 vlc_mutex_unlock( &lock );