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