]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/wxwindows.cpp
* modules/gui/wxwindows/*: don't forget to delete the timer.
[vlc] / modules / gui / wxwindows / wxwindows.cpp
1 /*****************************************************************************
2  * wxwindows.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: wxwindows.cpp,v 1.34 2003/10/14 22:41:41 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/intf.h>
34
35 #include "wxwindows.h"
36
37 /* Temporary hack */
38 #if defined(WIN32) && defined(_WX_INIT_H_)
39 /* Hack to detect wxWindows 2.5 which has a different wxEntry() prototype */
40 extern int wxEntry( HINSTANCE hInstance, HINSTANCE hPrevInstance = NULL,
41                     char *pCmdLine = NULL, int nCmdShow = SW_NORMAL );
42 #endif
43 #ifdef __DARWIN__
44 int wxEntry( int argc, char *argv[] , bool enterLoop = TRUE );
45 #endif
46
47 /*****************************************************************************
48  * Local prototypes.
49  *****************************************************************************/
50 static int  Open         ( vlc_object_t * );
51 static void Close        ( vlc_object_t * );
52 static int  OpenDialogs  ( vlc_object_t * );
53
54 static void Run          ( intf_thread_t * );
55 static void Init         ( intf_thread_t * );
56
57 static void ShowDialog   ( intf_thread_t *, int, int, intf_dialog_args_t * );
58
59 /*****************************************************************************
60  * Local classes declarations.
61  *****************************************************************************/
62 class Instance: public wxApp
63 {
64 public:
65     Instance();
66     Instance( intf_thread_t *_p_intf );
67
68     bool OnInit();
69     int  OnExit();
70
71 private:
72     intf_thread_t *p_intf;
73     wxLocale locale;                                /* locale we'll be using */
74 };
75
76 /*****************************************************************************
77  * Module descriptor
78  *****************************************************************************/
79 vlc_module_begin();
80 #ifdef WIN32
81     int i_score = 150;
82 #else
83     int i_score = getenv( "DISPLAY" ) == NULL ? 15 : 150;
84 #endif
85     set_description( (char *) _("wxWindows interface module") );
86     set_capability( "interface", i_score );
87     set_callbacks( Open, Close );
88     add_shortcut( "wxwindows" );
89     add_shortcut( "wxwin" );
90     add_shortcut( "wx" );
91     set_program( "wxvlc" );
92
93     add_submodule();
94     set_description( _("wxWindows dialogs provider") );
95     set_capability( "dialogs provider", 50 );
96     set_callbacks( OpenDialogs, Close );
97
98 #if !defined(WIN32)
99     linked_with_a_crap_library_which_uses_atexit();
100 #endif
101 vlc_module_end();
102
103 /*****************************************************************************
104  * Open: initialize and create window
105  *****************************************************************************/
106 static int Open( vlc_object_t *p_this )
107 {
108     intf_thread_t *p_intf = (intf_thread_t *)p_this;
109
110     /* Allocate instance and initialize some members */
111     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
112     if( p_intf->p_sys == NULL )
113     {
114         msg_Err( p_intf, "out of memory" );
115         return VLC_ENOMEM;
116     }
117
118     p_intf->pf_run = Run;
119
120     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
121
122     /* Initialize wxWindows thread */
123     p_intf->p_sys->b_playing = 0;
124
125     p_intf->p_sys->p_input = NULL;
126     p_intf->p_sys->i_playing = -1;
127     p_intf->p_sys->b_slider_free = 1;
128     p_intf->p_sys->i_slider_pos = p_intf->p_sys->i_slider_oldpos = 0;
129
130     p_intf->p_sys->p_popup_menu = NULL;
131
132     p_intf->pf_show_dialog = NULL;
133
134     return VLC_SUCCESS;
135 }
136
137 static int OpenDialogs( vlc_object_t *p_this )
138 {
139     intf_thread_t *p_intf = (intf_thread_t *)p_this;
140     int i_ret = Open( p_this );
141
142     p_intf->pf_show_dialog = ShowDialog;
143
144     return i_ret;
145 }
146
147 /*****************************************************************************
148  * Close: destroy interface window
149  *****************************************************************************/
150 static void Close( vlc_object_t *p_this )
151 {
152     intf_thread_t *p_intf = (intf_thread_t *)p_this;
153
154     if( p_intf->p_sys->p_input )
155     {
156         vlc_object_release( p_intf->p_sys->p_input );
157     }
158
159     if( p_intf->pf_show_dialog )
160     {
161         /* We must destroy the dialogs thread */
162         wxCommandEvent event( wxEVT_DIALOG, INTF_DIALOG_EXIT );
163         p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
164         vlc_thread_join( p_intf );
165     }
166
167     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
168
169     /* Destroy structure */
170     free( p_intf->p_sys );
171 }
172
173 /*****************************************************************************
174  * Run: wxWindows thread
175  *****************************************************************************/
176 #if !defined(__BUILTIN__) && defined( WIN32 )
177 HINSTANCE hInstance = 0;
178 extern "C" BOOL WINAPI
179 DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
180 {
181     hInstance = (HINSTANCE)hModule;
182     return TRUE;
183 }
184 #endif
185
186 static void Run( intf_thread_t *p_intf )
187 {
188     if( p_intf->pf_show_dialog )
189     {
190         /* The module is used in dialog provider mode */
191
192         /* Create a new thread for wxWindows */
193         if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
194                                Init, 0, VLC_TRUE ) )
195         {
196             msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
197             p_intf->pf_show_dialog = NULL;
198         }
199     }
200     else
201     {
202         /* The module is used in interface mode */
203         Init( p_intf );
204     }
205 }
206
207 static void Init( intf_thread_t *p_intf )
208 {
209 #if !defined( WIN32 )
210     static char  *p_args[] = { "" };
211     int i_args = 1;
212 #endif
213
214     /* Hack to pass the p_intf pointer to the new wxWindow Instance object */
215 #ifdef wxTheApp
216     wxApp::SetInstance( new Instance( p_intf ) );
217 #else
218     wxTheApp = new Instance( p_intf );
219 #endif
220
221 #if defined( WIN32 )
222 #if !defined(__BUILTIN__)
223     wxEntry( hInstance/*GetModuleHandle(NULL)*/, NULL, NULL, SW_SHOW );
224 #else
225     wxEntry( GetModuleHandle(NULL), NULL, NULL, SW_SHOW );
226 #endif
227 #else
228     wxEntry( i_args, p_args );
229 #endif
230 }
231
232 /* following functions are local */
233
234 /*****************************************************************************
235  * Constructors.
236  *****************************************************************************/
237 Instance::Instance( )
238 {
239 }
240
241 Instance::Instance( intf_thread_t *_p_intf )
242 {
243     /* Initialization */
244     p_intf = _p_intf;
245 }
246
247 IMPLEMENT_APP_NO_MAIN(Instance)
248
249 /*****************************************************************************
250  * Instance::OnInit: the parent interface execution starts here
251  *****************************************************************************
252  * This is the "main program" equivalent, the program execution will
253  * start here.
254  *****************************************************************************/
255 bool Instance::OnInit()
256 {
257     /* Initialization of i18n stuff.
258      * Usefull for things we don't have any control over, like wxWindows
259      * provided facilities (eg. open file dialog) */
260     locale.Init( wxLANGUAGE_DEFAULT );
261
262     /* Make an instance of your derived frame. Passing NULL (the default value
263      * of Frame's constructor is NULL) as the frame doesn't have a parent
264      * since it is the first window */
265
266     if( !p_intf->pf_show_dialog )
267     {
268         /* The module is used in interface mode */
269         Interface *MainInterface = new Interface( p_intf );
270         p_intf->p_sys->p_wxwindow = MainInterface;
271
272         /* Show the interface */
273         MainInterface->Show( TRUE );
274
275         SetTopWindow( MainInterface );
276     }
277
278     /* Creates the dialogs provider */
279     p_intf->p_sys->p_wxwindow =
280         new DialogsProvider( p_intf, p_intf->pf_show_dialog ?
281                              NULL : p_intf->p_sys->p_wxwindow );
282
283     p_intf->p_sys->pf_show_dialog = ShowDialog;
284
285     /* OK, initialization is over */
286     vlc_thread_ready( p_intf );
287
288     /* Return TRUE to tell program to continue (FALSE would terminate) */
289     return TRUE;
290 }
291
292 /*****************************************************************************
293  * Instance::OnExit: called when the interface execution stops
294  *****************************************************************************/
295 int Instance::OnExit()
296 {
297     if( p_intf->pf_show_dialog )
298     {
299          /* We need to manually clean up the dialogs class */
300          if( p_intf->p_sys->p_wxwindow ) delete p_intf->p_sys->p_wxwindow;
301     }
302     return 0;
303 }
304
305 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
306                         intf_dialog_args_t *p_arg )
307 {
308     wxCommandEvent event( wxEVT_DIALOG, i_dialog_event );
309     event.SetInt( i_arg );
310     event.SetClientData( p_arg );
311
312 #ifdef WIN32
313     SendMessage( (HWND)p_intf->p_sys->p_wxwindow->GetHandle(),
314                  WM_CANCELMODE, 0, 0 );
315 #endif
316     if( i_dialog_event == INTF_DIALOG_POPUPMENU && i_arg == 0 ) return;
317
318     /* Hack to prevent popup events to be enqueued when
319      * one is already active */
320     if( i_dialog_event != INTF_DIALOG_POPUPMENU ||
321         !p_intf->p_sys->p_popup_menu )
322     {
323         p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
324     }
325 }