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