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