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