]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/wxwidgets.cpp
wxWindows -> wxWidgets step 3 (refs #80)
[vlc] / modules / gui / wxwidgets / wxwidgets.cpp
1 /*****************************************************************************
2  * wxwindows.cpp : wxWidgets plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
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 "wxwidgets.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 #if (wxCHECK_VERSION(2,5,0))
67 void *wxClassInfo_sm_classTable_BUGGY = 0;
68 #endif
69
70 /*****************************************************************************
71  * Local classes declarations.
72  *****************************************************************************/
73 class Instance: public wxApp
74 {
75 public:
76     Instance();
77     Instance( intf_thread_t *_p_intf );
78
79     bool OnInit();
80     int  OnExit();
81
82 private:
83     intf_thread_t *p_intf;
84     wxLocale locale;                                /* locale we'll be using */
85 };
86
87 /*****************************************************************************
88  * Module descriptor
89  *****************************************************************************/
90 #define EMBED_TEXT N_("Embed video in interface")
91 #define EMBED_LONGTEXT N_("Embed the video inside the interface instead " \
92     "of having it in a separate window.")
93 #define BOOKMARKS_TEXT N_("Show bookmarks dialog")
94 #define BOOKMARKS_LONGTEXT N_("Show bookmarks dialog when the interface " \
95     "starts.")
96 #define TASKBAR_TEXT N_("Show taskbar entry")
97 #define TASKBAR_LONGTEXT N_("Show taskbar entry")
98 #define MINIMAL_TEXT N_("Minimal interface")
99 #define MINIMAL_LONGTEXT N_("Use minimal interface, no toolbar, few menus")
100 #define SIZE_TO_VIDEO_TEXT N_("Size to video")
101 #define SIZE_TO_VIDEO_LONGTEXT N_("Resize VLC to match the video resolution")
102 #define SYSTRAY_TEXT N_("Show systray icon")
103 #define SYSTRAY_LONGTEXT N_("Show systray icon")
104
105 vlc_module_begin();
106 #ifdef WIN32
107     int i_score = 150;
108 #else
109     int i_score = getenv( "DISPLAY" ) == NULL ? 15 : 150;
110 #endif
111     set_shortname( (char*) "wxWidgets" );
112     set_description( (char *) _("wxWidgets interface module") );
113     set_category( CAT_INTERFACE );
114     set_subcategory( SUBCAT_INTERFACE_GENERAL );
115     set_capability( "interface", i_score );
116     set_callbacks( Open, Close );
117     add_shortcut( "wxwindows" );
118     add_shortcut( "wxwin" );
119     add_shortcut( "wx" );
120     add_shortcut( "wxwidgets" );
121     set_program( "wxvlc" );
122
123     add_bool( "wxwin-embed", 1, NULL,
124               EMBED_TEXT, EMBED_LONGTEXT, VLC_FALSE );
125     add_bool( "wxwin-bookmarks", 0, NULL,
126               BOOKMARKS_TEXT, BOOKMARKS_LONGTEXT, VLC_FALSE );
127     add_bool( "wxwin-taskbar", 1, NULL,
128               TASKBAR_TEXT, TASKBAR_LONGTEXT, VLC_FALSE );
129     add_bool( "wxwin-minimal", 0, NULL,
130               MINIMAL_TEXT, MINIMAL_LONGTEXT, VLC_TRUE );
131     add_bool( "wxwin-autosize", 1, NULL,
132               SIZE_TO_VIDEO_TEXT, SIZE_TO_VIDEO_LONGTEXT, VLC_TRUE );
133 #ifdef wxHAS_TASK_BAR_ICON
134     add_bool( "wxwin-systray", 0, NULL,
135               SYSTRAY_TEXT, SYSTRAY_LONGTEXT, VLC_FALSE );
136 #endif
137     add_string( "wxwin-config-last", NULL, NULL,
138                 "last config", "last config", VLC_TRUE );
139         change_autosave();
140
141     add_submodule();
142     set_description( _("wxWidgets dialogs provider") );
143     set_capability( "dialogs provider", 50 );
144     set_callbacks( OpenDialogs, Close );
145
146 #if !defined(WIN32)
147     linked_with_a_crap_library_which_uses_atexit();
148 #endif
149 vlc_module_end();
150
151 /*****************************************************************************
152  * Open: initialize and create window
153  *****************************************************************************/
154 static int Open( vlc_object_t *p_this )
155 {
156     intf_thread_t *p_intf = (intf_thread_t *)p_this;
157
158     /* Allocate instance and initialize some members */
159     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
160     if( p_intf->p_sys == NULL )
161     {
162         msg_Err( p_intf, "out of memory" );
163         return VLC_ENOMEM;
164     }
165     memset( p_intf->p_sys, 0, sizeof( intf_sys_t ) );
166
167     p_intf->pf_run = Run;
168
169     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
170
171     /* Initialize wxWindows thread */
172     p_intf->p_sys->b_playing = 0;
173
174     p_intf->p_sys->p_input = NULL;
175     p_intf->p_sys->i_playing = -1;
176     p_intf->p_sys->b_slider_free = 1;
177     p_intf->p_sys->i_slider_pos = p_intf->p_sys->i_slider_oldpos = 0;
178
179     p_intf->p_sys->p_popup_menu = NULL;
180     p_intf->p_sys->p_video_window = NULL;
181
182     p_intf->pf_show_dialog = NULL;
183
184     /* We support play on start */
185     p_intf->b_play = VLC_TRUE;
186
187     p_intf->p_sys->b_video_autosize =
188         config_GetInt( p_intf, "wxwin-autosize" );
189
190     return VLC_SUCCESS;
191 }
192
193 static int OpenDialogs( vlc_object_t *p_this )
194 {
195     intf_thread_t *p_intf = (intf_thread_t *)p_this;
196     int i_ret = Open( p_this );
197
198     p_intf->pf_show_dialog = ShowDialog;
199
200     return i_ret;
201 }
202
203 /*****************************************************************************
204  * Close: destroy interface window
205  *****************************************************************************/
206 static void Close( vlc_object_t *p_this )
207 {
208     intf_thread_t *p_intf = (intf_thread_t *)p_this;
209
210     vlc_mutex_lock( &p_intf->object_lock );
211     p_intf->b_dead = VLC_TRUE;
212     vlc_mutex_unlock( &p_intf->object_lock );
213
214     if( p_intf->pf_show_dialog )
215     {
216         /* We must destroy the dialogs thread */
217         wxCommandEvent event( wxEVT_DIALOG, INTF_DIALOG_EXIT );
218         p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
219         vlc_thread_join( p_intf );
220     }
221
222     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
223
224     /* */
225     delete p_intf->p_sys->p_window_settings;
226
227 #if (wxCHECK_VERSION(2,5,0))
228     wxClassInfo::sm_classTable = (wxHashTable*)wxClassInfo_sm_classTable_BUGGY;
229 #endif
230
231     /* Destroy structure */
232     free( p_intf->p_sys );
233 }
234
235 /*****************************************************************************
236  * Run: wxWindows thread
237  *****************************************************************************/
238
239 //when is this called?
240 #if !defined(__BUILTIN__) && defined( WIN32 )
241 HINSTANCE hInstance = 0;
242 extern "C" BOOL WINAPI
243 DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
244 {
245     hInstance = (HINSTANCE)hModule;
246     return TRUE;
247 }
248 #endif
249
250 static void Run( intf_thread_t *p_intf )
251 {
252     if( p_intf->pf_show_dialog )
253     {
254         /* The module is used in dialog provider mode */
255
256         /* Create a new thread for wxWindows */
257         if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
258                                Init, 0, VLC_TRUE ) )
259         {
260             msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
261             p_intf->pf_show_dialog = NULL;
262         }
263     }
264     else
265     {
266         /* The module is used in interface mode */
267         Init( p_intf );
268     }
269 }
270
271 static void Init( intf_thread_t *p_intf )
272 {
273 #if !defined( WIN32 )
274     static char  *p_args[] = { "" };
275     int i_args = 1;
276 #endif
277
278     /* Hack to pass the p_intf pointer to the new wxWindow Instance object */
279 #ifdef wxTheApp
280     wxApp::SetInstance( new Instance( p_intf ) );
281 #else
282     wxTheApp = new Instance( p_intf );
283 #endif
284
285 #if defined( WIN32 )
286 #if !defined(__BUILTIN__)
287
288     //because no one knows when DllMain is called
289     if (hInstance == NULL)
290       hInstance = GetModuleHandle(NULL);
291
292     wxEntry( hInstance/*GetModuleHandle(NULL)*/, NULL, NULL, SW_SHOW );
293 #else
294     wxEntry( GetModuleHandle(NULL), NULL, NULL, SW_SHOW );
295 #endif
296 #else
297     wxEntry( i_args, p_args );
298 #endif
299 }
300
301 /* following functions are local */
302
303 /*****************************************************************************
304  * Constructors.
305  *****************************************************************************/
306 Instance::Instance( )
307 {
308 }
309
310 Instance::Instance( intf_thread_t *_p_intf )
311 {
312     /* Initialization */
313     p_intf = _p_intf;
314 }
315
316 IMPLEMENT_APP_NO_MAIN(Instance)
317
318 /*****************************************************************************
319  * Instance::OnInit: the parent interface execution starts here
320  *****************************************************************************
321  * This is the "main program" equivalent, the program execution will
322  * start here.
323  *****************************************************************************/
324 bool Instance::OnInit()
325 {
326     /* Initialization of i18n stuff.
327      * Usefull for things we don't have any control over, like wxWindows
328      * provided facilities (eg. open file dialog) */
329     locale.Init( wxLANGUAGE_DEFAULT );
330
331     /* FIXME: The stream output mrl parsing uses ',' already so we want to
332      * keep the default '.' for floating point numbers. */
333     setlocale( LC_NUMERIC, "C" );
334
335     /* Load saved window settings */
336     p_intf->p_sys->p_window_settings = new WindowSettings( p_intf );
337
338     /* Make an instance of your derived frame. Passing NULL (the default value
339      * of Frame's constructor is NULL) as the frame doesn't have a parent
340      * since it is the first window */
341
342     if( !p_intf->pf_show_dialog )
343     {
344         /* The module is used in interface mode */
345         long style = wxDEFAULT_FRAME_STYLE;
346         if ( ! config_GetInt( p_intf, "wxwin-taskbar" ) )
347         {
348             style = wxDEFAULT_FRAME_STYLE|wxFRAME_NO_TASKBAR;
349         }
350
351         Interface *MainInterface = new Interface( p_intf, style );
352         p_intf->p_sys->p_wxwindow = MainInterface;
353
354         /* Show the interface */
355         MainInterface->Show( TRUE );
356         SetTopWindow( MainInterface );
357         MainInterface->Raise();
358     }
359
360     /* Creates the dialogs provider */
361     p_intf->p_sys->p_wxwindow =
362         CreateDialogsProvider( p_intf, p_intf->pf_show_dialog ?
363                                NULL : p_intf->p_sys->p_wxwindow );
364
365     p_intf->p_sys->pf_show_dialog = ShowDialog;
366
367     /* OK, initialization is over */
368     vlc_thread_ready( p_intf );
369
370     /* Check if we need to start playing */
371     if( !p_intf->pf_show_dialog && p_intf->b_play )
372     {
373         playlist_t *p_playlist =
374             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
375                                            FIND_ANYWHERE );
376         if( p_playlist )
377         {
378             playlist_LockControl( p_playlist, PLAYLIST_AUTOPLAY );
379             vlc_object_release( p_playlist );
380         }
381     }
382
383     /* Return TRUE to tell program to continue (FALSE would terminate) */
384     return TRUE;
385 }
386
387 /*****************************************************************************
388  * Instance::OnExit: called when the interface execution stops
389  *****************************************************************************/
390 int Instance::OnExit()
391 {
392     if( p_intf->pf_show_dialog )
393     {
394          /* We need to manually clean up the dialogs class */
395          if( p_intf->p_sys->p_wxwindow ) delete p_intf->p_sys->p_wxwindow;
396     }
397
398 #if (wxCHECK_VERSION(2,5,0))
399     wxClassInfo_sm_classTable_BUGGY = wxClassInfo::sm_classTable;
400     wxClassInfo::sm_classTable = 0;
401 #endif
402
403     return 0;
404 }
405
406 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
407                         intf_dialog_args_t *p_arg )
408 {
409     wxCommandEvent event( wxEVT_DIALOG, i_dialog_event );
410     event.SetInt( i_arg );
411     event.SetClientData( p_arg );
412
413 #ifdef WIN32
414     SendMessage( (HWND)p_intf->p_sys->p_wxwindow->GetHandle(),
415                  WM_CANCELMODE, 0, 0 );
416 #endif
417     if( i_dialog_event == INTF_DIALOG_POPUPMENU && i_arg == 0 ) return;
418
419     /* Hack to prevent popup events to be enqueued when
420      * one is already active */
421     if( i_dialog_event != INTF_DIALOG_POPUPMENU ||
422         !p_intf->p_sys->p_popup_menu )
423     {
424         p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
425     }
426 }
427
428 /*****************************************************************************
429  * WindowSettings utility class
430  *****************************************************************************/
431 WindowSettings::WindowSettings( intf_thread_t *_p_intf )
432 {
433     char *psz_org = NULL;
434     char *psz;
435     int i;
436
437     /* */
438     p_intf = _p_intf;
439
440     /* */
441     for( i = 0; i < ID_MAX; i++ )
442     {
443         b_valid[i] = false;
444         b_shown[i] = false;
445         position[i] = wxDefaultPosition;
446         size[i] = wxDefaultSize;
447     }
448     b_shown[ID_MAIN] = true;
449
450     if( p_intf->pf_show_dialog ) return;
451
452     /* Parse the configuration */
453     psz_org = psz = config_GetPsz( p_intf, "wxwin-config-last" );
454     if( !psz || *psz == '\0' ) return;
455
456     msg_Dbg( p_intf, "Using last windows config '%s'", psz );
457
458     i_screen_w = 0;
459     i_screen_h = 0;
460     while( psz && *psz )
461     {
462         int id, v[4];
463
464         psz = strchr( psz, '(' );
465
466         if( !psz )
467             break;
468         psz++;
469
470         id = strtol( psz, &psz, 0 );
471         if( *psz != ',' ) /* broken cfg */
472             goto invalid;
473         psz++;
474
475         for( i = 0; i < 4; i++ )
476         {
477             v[i] = strtol( psz, &psz, 0 );
478
479             if( i < 3 )
480             {
481                 if( *psz != ',' )
482                     goto invalid;
483                 psz++;
484             }
485             else
486             {
487                 if( *psz != ')' )
488                     goto invalid;
489             }
490         }
491         if( id == ID_SCREEN )
492         {
493             i_screen_w = v[2];
494             i_screen_h = v[3];
495         }
496         else if( id >= 0 && id < ID_MAX )
497         {
498             b_valid[id] = true;
499             b_shown[id] = true;
500             position[id] = wxPoint( v[0], v[1] );
501             size[id] = wxSize( v[2], v[3] );
502
503             msg_Dbg( p_intf, "id=%d p=(%d,%d) s=(%d,%d)",
504                      id, position[id].x, position[id].y,
505                          size[id].x, size[id].y );
506         }
507
508         psz = strchr( psz, ')' );
509         if( psz ) psz++;
510     }
511
512     if( i_screen_w <= 0 || i_screen_h <= 0 )
513         goto invalid;
514
515     for( i = 0; i < ID_MAX; i++ )
516     {
517         if( !b_valid[i] )
518             continue;
519         if( position[i].x < 0 || position[i].y < 0 )
520             goto invalid;
521         if( size[i].x <= 0 || size[i].y <= 0 )
522             goto invalid;
523     }
524
525     if( psz_org ) free( psz_org );
526     return;
527
528 invalid:
529     msg_Dbg( p_intf, "last windows config is invalid (ignored)" );
530     for( i = 0; i < ID_MAX; i++ )
531     {
532         b_valid[i] = false;
533         b_shown[i] = false;
534         position[i] = wxDefaultPosition;
535         size[i] = wxDefaultSize;
536     }
537     if( psz_org ) free( psz_org );
538 }
539
540
541 WindowSettings::~WindowSettings( )
542 {
543     wxString sCfg;
544
545     if( p_intf->pf_show_dialog ) return;
546
547     sCfg = wxString::Format( wxT("(%d,0,0,%d,%d)"), ID_SCREEN,
548                              wxSystemSettings::GetMetric( wxSYS_SCREEN_X ),
549                              wxSystemSettings::GetMetric( wxSYS_SCREEN_Y ) );
550     for( int i = 0; i < ID_MAX; i++ )
551     {
552         if( !b_valid[i] || !b_shown[i] )
553             continue;
554
555         sCfg += wxString::Format( wxT("(%d,%d,%d,%d,%d)"),
556                                   i, position[i].x, position[i].y,
557                                      size[i].x, size[i].y );
558     }
559
560     config_PutPsz( p_intf, "wxwin-config-last", sCfg.mb_str() );
561 }
562
563 void WindowSettings::SetScreen( int i_screen_w, int i_screen_h )
564 {
565     int i;
566
567     for( i = 0; i < ID_MAX; i++ )
568     {
569         if( !b_valid[i] )
570             continue;
571         if( position[i].x >= i_screen_w || position[i].y >= i_screen_h )
572             goto invalid;
573     }
574     return;
575
576 invalid:
577     for( i = 0; i < ID_MAX; i++ )
578     {
579         b_valid[i] = false;
580         b_shown[i] = false;
581         position[i] = wxDefaultPosition;
582         size[i] = wxDefaultSize;
583     }
584 }
585
586 void WindowSettings::SetSettings( int id, bool _b_shown, wxPoint p, wxSize s )
587 {
588     if( id < 0 || id >= ID_MAX )
589         return;
590
591     b_valid[id] = true;
592     b_shown[id] = _b_shown;
593
594     position[id] = p;
595     size[id] = s;
596 }
597
598 bool WindowSettings::GetSettings( int id, bool& _b_shown, wxPoint& p, wxSize& s)
599 {
600     if( id < 0 || id >= ID_MAX )
601         return false;
602
603     if( !b_valid[id] )
604         return false;
605
606     _b_shown = b_shown[id];
607     p = position[id];
608     s = size[id];
609
610     return true;
611 }