]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/wxwindows.cpp
* modules/gui/wxwindows/*: The wxwindows interface is now a "dialogs provider" module...
[vlc] / modules / gui / wxwindows / wxwindows.cpp
1 /*****************************************************************************
2  * wxwindows.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: wxwindows.cpp,v 1.18 2003/07/17 17:30:40 gbazin Exp $
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
34 #ifdef WIN32                                                 /* mingw32 hack */
35 #undef Yield
36 #undef CreateDialog
37 #endif
38
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
41
42 #include <wx/wxprec.h>
43 #include <wx/wx.h>
44
45 #include <vlc/intf.h>
46
47 #include "wxwindows.h"
48
49 /* Temporary hack */
50 #ifdef __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 );
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
77 private:
78     intf_thread_t *p_intf;
79     wxLocale locale;                                /* locale we'll be using */
80 };
81
82 /*****************************************************************************
83  * Module descriptor
84  *****************************************************************************/
85 vlc_module_begin();
86     set_description( (char *) _("wxWindows interface module") );
87     set_capability( "interface", 50 );
88     set_callbacks( Open, Close );
89     add_shortcut( "wxwindows" );
90     add_shortcut( "wxwin" );
91     add_shortcut( "wx" );
92     set_program( "wxvlc" );
93
94     add_submodule();
95     set_description( _("wxWindows dialogs provider") );
96     set_capability( "dialogs provider", 50 );
97     set_callbacks( OpenDialogs, Close );
98
99 #if !defined(WIN32)
100     linked_with_a_crap_library_which_uses_atexit();
101 #endif
102 vlc_module_end();
103
104 /*****************************************************************************
105  * Open: initialize and create window
106  *****************************************************************************/
107 static int Open( vlc_object_t *p_this )
108 {
109     intf_thread_t *p_intf = (intf_thread_t *)p_this;
110
111     /* Allocate instance and initialize some members */
112     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
113     if( p_intf->p_sys == NULL )
114     {
115         msg_Err( p_intf, "out of memory" );
116         return VLC_ENOMEM;
117     }
118
119     p_intf->pf_run = Run;
120
121     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
122
123     /* Initialize wxWindows thread */
124     p_intf->p_sys->b_playing = 0;
125
126     p_intf->p_sys->p_input = NULL;
127     p_intf->p_sys->i_playing = -1;
128     p_intf->p_sys->b_slider_free = 1;
129     p_intf->p_sys->i_slider_pos = p_intf->p_sys->i_slider_oldpos = 0;
130
131     p_intf->p_sys->p_popup_menu = NULL;
132     p_intf->p_sys->b_popup_change = VLC_FALSE;
133
134     p_intf->pf_show_dialog = NULL;
135
136     return VLC_SUCCESS;
137 }
138
139 static int OpenDialogs( vlc_object_t *p_this )
140 {
141     intf_thread_t *p_intf = (intf_thread_t *)p_this;
142     int i_ret = Open( p_this );
143
144     p_intf->pf_show_dialog = ShowDialog;
145
146     return i_ret;
147 }
148
149 /*****************************************************************************
150  * Close: destroy interface window
151  *****************************************************************************/
152 static void Close( vlc_object_t *p_this )
153 {
154     intf_thread_t *p_intf = (intf_thread_t *)p_this;
155
156     if( p_intf->p_sys->p_input )
157     {
158         vlc_object_release( p_intf->p_sys->p_input );
159     }
160
161     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
162
163     /* Destroy structure */
164     free( p_intf->p_sys );
165 }
166
167 /*****************************************************************************
168  * Run: wxWindows thread
169  *****************************************************************************/
170 #if !defined(__BUILTIN__) && defined( WIN32 )
171 HINSTANCE hInstance = 0;
172 extern "C" BOOL WINAPI
173 DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
174 {
175     hInstance = (HINSTANCE)hModule;
176     return TRUE;
177 }
178 #endif
179
180 static void Run( intf_thread_t *p_intf )
181 {
182     if( p_intf->pf_show_dialog )
183     {
184         /* The module is used in dialog provider mode */
185
186         /* Create a new thread for wxWindows */
187         if( vlc_thread_create( p_intf, "Skins Dialogs Thread",
188                                Init, 0, VLC_TRUE ) )
189         {
190             msg_Err( p_intf, "cannot create Skins Dialogs Thread" );
191             p_intf->pf_show_dialog = NULL;
192         }
193     }
194     else
195     {
196         /* The module is used in interface mode */
197         Init( p_intf );
198     }
199 }
200
201 static void Init( intf_thread_t *p_intf )
202 {
203 #if !defined( WIN32 )
204     static char  *p_args[] = { "" };
205     int i_args = 1;
206 #endif
207
208     /* Hack to pass the p_intf pointer to the new wxWindow Instance object */
209     wxTheApp = new Instance( p_intf );
210
211 #if defined( WIN32 )
212 #if !defined(__BUILTIN__)
213     wxEntry( hInstance/*GetModuleHandle(NULL)*/, NULL, NULL, SW_SHOW );
214 #else
215     wxEntry( GetModuleHandle(NULL), NULL, NULL, SW_SHOW );
216 #endif
217 #else
218     wxEntry( i_args, p_args );
219 #endif
220 }
221
222 /* following functions are local */
223
224 /*****************************************************************************
225  * Constructors.
226  *****************************************************************************/
227 Instance::Instance( )
228 {
229 }
230
231 Instance::Instance( intf_thread_t *_p_intf )
232 {
233     /* Initialization */
234     p_intf = _p_intf;
235 }
236
237 IMPLEMENT_APP_NO_MAIN(Instance)
238
239 /*****************************************************************************
240  * Instance::OnInit: the parent interface execution starts here
241  *****************************************************************************
242  * This is the "main program" equivalent, the program execution will
243  * start here.
244  *****************************************************************************/
245 bool Instance::OnInit()
246 {
247     /* Initialization of i18n stuff.
248      * Usefull for things we don't have any control over, like wxWindows
249      * provided facilities (eg. open file dialog) */
250     locale.Init( wxLANGUAGE_DEFAULT );
251
252     /* Make an instance of your derived frame. Passing NULL (the default value
253      * of Frame's constructor is NULL) as the frame doesn't have a parent
254      * since it is the first window */
255
256     if( !p_intf->pf_show_dialog )
257     {
258         /* The module is used in interface mode */
259         Interface *MainInterface = new Interface( p_intf );
260         p_intf->p_sys->p_wxwindow = MainInterface;
261
262         /* Show the interface */
263         MainInterface->Show( TRUE );
264
265         SetTopWindow( MainInterface );
266
267         /* Start timer */
268         new Timer( p_intf, MainInterface );
269     }
270
271     /* Creates the dialogs provider */
272     p_intf->p_sys->p_wxwindow =
273         new DialogsProvider( p_intf, p_intf->pf_show_dialog ?
274                              NULL : p_intf->p_sys->p_wxwindow );
275
276     p_intf->p_sys->pf_show_dialog = ShowDialog;
277
278     /* OK, initialization is over */
279     vlc_thread_ready( p_intf );
280
281     /* Return TRUE to tell program to continue (FALSE would terminate) */
282     return TRUE;
283 }
284
285 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg )
286 {
287     wxCommandEvent event( wxEVT_DIALOG, i_dialog_event );
288     event.SetInt( i_arg );
289     p_intf->p_sys->p_wxwindow->AddPendingEvent( event );
290 }