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