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