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