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