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