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