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