* fixed a timing issue under windows ?
[vlc.git] / modules / gui / skins / gtk2 / gtk2_run.cpp
1 /*****************************************************************************
2  * gtk2_run.cpp:
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: gtk2_run.cpp,v 1.17 2003/04/21 02:50:49 asmax Exp $
6  *
7  * Authors: Cyril Deguet     <asmax@videolan.org>
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,
22  * USA.
23  *****************************************************************************/
24
25 #if !defined WIN32
26
27 //--- GTK2 ------------------------------------------------------------------
28 #include <glib.h>
29 #include <gdk/gdk.h>
30 #include <gtk/gtk.h>
31
32 //--- WWWINDOWS -------------------------------------------------------------
33 #include <wx/wx.h>
34
35 //--- VLC -------------------------------------------------------------------
36 #include <vlc/intf.h>
37
38 //--- SKIN ------------------------------------------------------------------
39 #include "../os_api.h"
40 #include "../src/event.h"
41 #include "../os_event.h"
42 #include "../src/banks.h"
43 #include "../src/window.h"
44 #include "../os_window.h"
45 #include "../src/theme.h"
46 #include "../os_theme.h"
47 #include "../src/skin_common.h"
48 #include "../src/vlcproc.h"
49 #include "../src/wxdialogs.h"
50
51 // include the icon graphic
52 #include "share/vlc32x32.xpm"
53
54 //---------------------------------------------------------------------------
55 class CallBackObjects
56 {
57     public:
58         VlcProc *Proc;
59         GMainLoop *Loop;
60 };
61
62 //---------------------------------------------------------------------------
63 // Specific method
64 //---------------------------------------------------------------------------
65 bool IsVLCEvent( unsigned int msg );
66 int  SkinManage( intf_thread_t *p_intf );
67
68
69 //---------------------------------------------------------------------------
70 // Local classes declarations.
71 //---------------------------------------------------------------------------
72 class Instance: public wxApp
73 {
74 public:
75     Instance();
76     Instance( intf_thread_t *_p_intf, CallBackObjects *callback );
77
78     bool OnInit();
79     OpenDialog *open;
80
81 private:
82     intf_thread_t *p_intf;
83     CallBackObjects *callbackobj;
84 };
85
86
87 //---------------------------------------------------------------------------
88 // GTK2 interface
89 //---------------------------------------------------------------------------
90 void GTK2Proc( GdkEvent *event, gpointer data )
91 {
92     // Get objects from data
93     CallBackObjects *obj = (CallBackObjects *)data;
94     VlcProc *proc        = obj->Proc;
95
96     // Get pointer to thread info
97     intf_thread_t *p_intf = proc->GetpIntf();
98
99     // Variables
100     unsigned int msg;
101     Event *evt;
102     list<Window *>::const_iterator win;
103     GdkWindow *gwnd = ((GdkEventAny *)event)->window;
104
105     // Create event to dispatch in windows
106     // Skin event
107     if( event->type == GDK_CLIENT_EVENT )
108     {
109         msg = ( (GdkEventClient *)event )->data.l[0];
110         evt = (Event *)new OSEvent( p_intf, 
111             ((GdkEventAny *)event)->window,
112             msg,
113             ( (GdkEventClient *)event )->data.l[1],
114             ( (GdkEventClient *)event )->data.l[2] );
115     }
116     // System event
117     else
118     {
119         msg = event->type;
120         evt = (Event *)new OSEvent( p_intf,
121             ((GdkEventAny *)event)->window, msg, 0, (long)event );
122     }
123
124     // Process keyboard shortcuts
125     if( msg == GDK_KEY_PRESS )
126     {
127         int KeyModifier = 0;
128         // If key is ALT
129         if( ((GdkEventKey *)event)->state & GDK_MOD1_MASK )
130         {
131             KeyModifier = 1;
132         }
133         // If key is CTRL
134         else if( ((GdkEventKey *)event)->state & GDK_CONTROL_MASK )
135         {
136             KeyModifier = 2;
137         }
138         int key = ((GdkEventKey *)event)->keyval;
139         // Translate into lower case
140         if( key >= 'a' && key <= 'z' )
141         {
142             key -= ('a' - 'A');
143         }
144         if( KeyModifier > 0 )
145             p_intf->p_sys->p_theme->EvtBank->TestShortcut( key , KeyModifier );
146     }
147
148     // Send event
149     else if( IsVLCEvent( msg ) )
150     {
151         if( !proc->EventProc( evt ) )
152         {
153             fprintf( stderr, "Quit\n" );
154             wxExit();
155             return;      // Exit VLC !
156         }
157     }
158     else if( gwnd == NULL )
159     {
160         for( win = p_intf->p_sys->p_theme->WindowList.begin();
161              win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
162         {
163             (*win)->ProcessEvent( evt );
164         }
165     }
166     else
167     {
168         // Find window matching with gwnd
169         for( win = p_intf->p_sys->p_theme->WindowList.begin();
170              win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
171         {
172             // If it is the correct window
173             if( gwnd == ( (GTK2Window *)(*win) )->GetHandle() )
174             {
175                 // Send event and check if processed
176                 if( (*win)->ProcessEvent( evt ) )
177                 {
178                     delete (OSEvent *)evt;
179                     return;
180                 }
181                 else
182                 {
183                     break;
184                 }
185             }
186         }
187     }
188
189     evt->DestructParameters();
190     delete (OSEvent *)evt;
191
192     // Check if vlc is closing
193     proc->IsClosing();
194
195     gtk_main_do_event( event );
196
197 }
198 //---------------------------------------------------------------------------
199
200
201 //---------------------------------------------------------------------------
202 // Implementation of Instance class
203 //---------------------------------------------------------------------------
204 Instance::Instance( )
205 {
206 }
207
208 Instance::Instance( intf_thread_t *_p_intf, CallBackObjects *callback )
209 {
210     // Initialization
211     p_intf = _p_intf;
212     callbackobj = callback;
213 }
214
215 IMPLEMENT_APP_NO_MAIN(Instance)
216
217 bool Instance::OnInit()
218 {
219     // Set event callback. Yes, it's a big hack ;)
220     gdk_event_handler_set( GTK2Proc, (gpointer)callbackobj, NULL );
221
222     p_intf->p_sys->p_icon = new wxIcon( vlc_xpm );
223     p_intf->p_sys->OpenDlg = new OpenDialog( p_intf, NULL, FILE_ACCESS );
224     p_intf->p_sys->MessagesDlg = new Messages( p_intf, NULL );
225     p_intf->p_sys->SoutDlg = new SoutDialog( p_intf, NULL );
226     p_intf->p_sys->PrefsDlg = new PrefsDialog( p_intf, NULL );
227     p_intf->p_sys->InfoDlg = new FileInfo( p_intf, NULL );
228     
229     vlc_mutex_lock( &p_intf->p_sys->init_lock );
230     vlc_cond_signal( &p_intf->p_sys->init_cond );
231     vlc_mutex_unlock( &p_intf->p_sys->init_lock );
232     
233     return TRUE;
234 }
235
236
237
238 //---------------------------------------------------------------------------
239 // REFRESH TIMER CALLBACK
240 //---------------------------------------------------------------------------
241 gboolean RefreshTimer( gpointer data )
242 {
243     intf_thread_t *p_intf = (intf_thread_t *)data;
244     SkinManage( p_intf );
245     return true;
246 }
247 //---------------------------------------------------------------------------
248
249
250 //---------------------------------------------------------------------------
251 // GTK2 interface
252 //---------------------------------------------------------------------------
253 void OSRun( intf_thread_t *p_intf )
254 {
255     static char  *p_args[] = { "" };
256
257     // Create VLC event object processing
258     CallBackObjects *callbackobj = new CallBackObjects();
259     callbackobj->Proc = new VlcProc( p_intf );
260
261     wxTheApp = new Instance( p_intf, callbackobj );
262
263     vlc_mutex_lock( &p_intf->p_sys->init_lock );
264     vlc_cond_wait( &p_intf->p_sys->init_cond, &p_intf->p_sys->init_lock );
265     vlc_mutex_unlock( &p_intf->p_sys->init_lock );
266  
267     // Add timer
268     g_timeout_add( 200, (GSourceFunc)RefreshTimer, (gpointer)p_intf );
269     
270     wxEntry( 1, p_args );
271     
272     delete callbackobj;
273 }
274 //---------------------------------------------------------------------------
275 bool IsVLCEvent( unsigned int msg )
276 {
277     return( msg > VLC_MESSAGE && msg < VLC_WINDOW );
278 }
279 //---------------------------------------------------------------------------
280
281 #endif