* Fixed mouse events
[vlc.git] / modules / gui / skins / gtk2 / gtk2_window.cpp
1 /*****************************************************************************
2  * gtk2_window.cpp: GTK2 implementation of the Window class
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: gtk2_window.cpp,v 1.11 2003/04/15 16:42:02 karibu 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 //--- GENERAL ---------------------------------------------------------------
28 //#include <math.h>
29
30 //--- VLC -------------------------------------------------------------------
31 #include <vlc/intf.h>
32
33 //--- GTK2 ------------------------------------------------------------------
34 #include <gdk/gdk.h>
35 #include <glib.h>
36
37 //--- SKIN ------------------------------------------------------------------
38 #include "os_api.h"
39 #include "anchor.h"
40 #include "generic.h"
41 #include "window.h"
42 #include "os_window.h"
43 #include "event.h"
44 #include "os_event.h"
45 #include "graphics.h"
46 #include "os_graphics.h"
47 #include "skin_common.h"
48 #include "theme.h"
49
50
51 //---------------------------------------------------------------------------
52 // Fading API
53 //---------------------------------------------------------------------------
54 /*#define LWA_COLORKEY  0x00000001
55 #define LWA_ALPHA     0x00000002
56 typedef BOOL (WINAPI *SLWA)(HWND, COLORREF, BYTE, DWORD);
57 HMODULE hModule = LoadLibrary( "user32.dll" );
58 SLWA SetLayeredWindowAttributes =
59     (SLWA)GetProcAddress( hModule, "SetLayeredWindowAttributes" );
60 */
61
62 //---------------------------------------------------------------------------
63 // Skinable Window
64 //---------------------------------------------------------------------------
65 GTK2Window::GTK2Window( intf_thread_t *p_intf, GdkWindow *gwnd, int x, int y,
66     bool visible, int transition, int normalalpha, int movealpha,
67     bool dragdrop )
68     : Window( p_intf, x, y, visible, transition, normalalpha, movealpha,
69               dragdrop )
70 {
71     // Set handles
72     gWnd           = gwnd;
73     gc = gdk_gc_new( gwnd );
74
75     LButtonDown = false;
76     RButtonDown = false;
77 /*
78     // Set position parameters
79     CursorPos    = new POINT;
80     WindowPos    = new POINT;
81
82     // Create Tool Tip Window
83     ToolTipWindow = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
84         WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
85         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
86         hWnd, 0, GetModuleHandle( NULL ), 0);
87
88     // Create Tool Tip infos
89     ToolTipInfo.cbSize = sizeof(TOOLINFO);
90     ToolTipInfo.uFlags = TTF_SUBCLASS|TTF_IDISHWND;
91     ToolTipInfo.hwnd = hWnd;
92     ToolTipInfo.hinst = GetModuleHandle( NULL );
93     ToolTipInfo.uId = (unsigned int)hWnd;
94     ToolTipInfo.lpszText = NULL;
95     ToolTipInfo.rect.left = ToolTipInfo.rect.top = 0;
96         ToolTipInfo.rect.right = ToolTipInfo.rect.bottom = 0;
97
98     SendMessage( ToolTipWindow, TTM_ADDTOOL, 0,
99                     (LPARAM)(LPTOOLINFO) &ToolTipInfo );
100
101     // Drag & drop
102     if( DragDrop )
103     {
104         // Initialize the OLE library
105         OleInitialize( NULL );
106         DropTarget = (LPDROPTARGET) new GTK2DropObject();
107         // register the listview as a drop target
108         RegisterDragDrop( hWnd, DropTarget );
109     }
110 */
111     // Create Tool Tip window
112 /*    GdkWindowAttr attr;
113     attr.event_mask = GDK_ALL_EVENTS_MASK;
114     attr.width = 100;
115     attr.height = 100;
116     attr.window_type = GDK_WINDOW_CHILD;
117     attr.wclass = GDK_INPUT_OUTPUT;
118     gint mask = 0;
119     ToolTipWindow = gdk_window_new( gwnd, &attr, mask);*/
120
121 }
122 //---------------------------------------------------------------------------
123 GTK2Window::~GTK2Window()
124 {
125 /*    delete CursorPos;
126     delete WindowPos;
127
128     if( hWnd != NULL )
129     {
130         DestroyWindow( hWnd );
131     }
132     if( ToolTipWindow != NULL )
133     {
134         DestroyWindow( ToolTipWindow );
135     }
136     if( DragDrop )
137     {
138         // Remove the listview from the list of drop targets
139         RevokeDragDrop( hWnd );
140         DropTarget->Release();
141         // Uninitialize the OLE library
142         OleUninitialize();
143     }
144 */
145 }
146 //---------------------------------------------------------------------------
147 void GTK2Window::OSShow( bool show )
148 {
149     if( show )
150     {
151         gdk_window_show( gWnd );
152     }
153     else
154     {
155         gdk_window_hide( gWnd );
156     }
157 }
158 //---------------------------------------------------------------------------
159 bool GTK2Window::ProcessOSEvent( Event *evt )
160 {
161     unsigned int msg = evt->GetMessage();
162     unsigned int p1  = evt->GetParam1();
163     int          p2  = evt->GetParam2();
164
165     switch( msg )
166     {
167         case GDK_EXPOSE:
168             RefreshFromImage( 0, 0, Width, Height );
169             return true;
170
171
172         case GDK_MOTION_NOTIFY:
173 //            TRACKMOUSEEVENT TrackEvent;
174 //            TrackEvent.cbSize      = sizeof( TRACKMOUSEEVENT );
175 //            TrackEvent.dwFlags     = TME_LEAVE;
176 //            TrackEvent.hwndTrack   = hWnd;
177 //            TrackEvent.dwHoverTime = 1;
178 //            TrackMouseEvent( &TrackEvent );
179             if( LButtonDown )
180                 MouseMove( (int)( (GdkEventButton *)p2 )->x,
181                            (int)( (GdkEventButton *)p2 )->y, 1 );
182             else if( RButtonDown )
183                 MouseMove( (int)( (GdkEventButton *)p2 )->x,
184                            (int)( (GdkEventButton *)p2 )->y, 2 );
185             else
186                 MouseMove( (int)( (GdkEventButton *)p2 )->x,
187                            (int)( (GdkEventButton *)p2 )->y, 0 );
188             gdk_window_get_pointer( gWnd, 0, 0, 0 );
189             return true;
190
191
192         case GDK_BUTTON_PRESS:
193             switch( ( (GdkEventButton *)p2 )->button )
194             {
195                 case 1:
196                     // Left button
197                     LButtonDown = true;
198                     MouseDown( (int)( (GdkEventButton *)p2 )->x,
199                                (int)( (GdkEventButton *)p2 )->y, 1 );
200                     break;
201
202                 case 3:
203                     // Right button
204                     RButtonDown = true;
205                     MouseDown( (int)( (GdkEventButton *)p2 )->x,
206                                (int)( (GdkEventButton *)p2 )->y, 2 );
207                     break;
208
209                 default:
210                     break;
211             }
212             return true;
213
214         case GDK_BUTTON_RELEASE:
215             switch( ( (GdkEventButton *)p2 )->button )
216             {
217                 case 1:
218                     // Left button
219                     LButtonDown = false;
220                     MouseUp( (int)( (GdkEventButton *)p2 )->x,
221                              (int)( (GdkEventButton *)p2 )->y, 1 );
222                     break;
223
224                 case 3:
225                     // Right button
226                     RButtonDown = false;
227                     MouseUp( (int)( (GdkEventButton *)p2 )->x,
228                              (int)( (GdkEventButton *)p2 )->y, 2 );
229                     break;
230
231                 default:
232                     break;
233             }
234             return true;
235
236 /*
237         case WM_RBUTTONDOWN:
238             MouseDown( LOWORD( p2 ), HIWORD( p2 ), 2 );
239             return true;
240
241         case WM_RBUTTONUP:
242             MouseUp( LOWORD( p2 ), HIWORD( p2 ), 2 );
243             return true;
244
245         case WM_LBUTTONDBLCLK:
246             MouseDblClick( LOWORD( p2 ), HIWORD( p2 ), 1 );
247             return true;
248
249         case WM_MOUSELEAVE:
250             OSAPI_PostMessage( this, WINDOW_LEAVE, 0, 0 );
251             return true;
252 */
253         default:
254             return false;
255     }
256 }
257 //---------------------------------------------------------------------------
258 void GTK2Window::SetTransparency( int Value )
259 {
260 /*    if( Value > -1 )
261         Alpha = Value;
262     SetLayeredWindowAttributes( hWnd, 0, Alpha, LWA_ALPHA | LWA_COLORKEY );
263     UpdateWindow( hWnd );*/
264 }
265 //---------------------------------------------------------------------------
266 void GTK2Window::RefreshFromImage( int x, int y, int w, int h )
267 {
268     // Initialize painting
269 /*    HDC DC = GetWindowDC( hWnd );
270
271     // Draw image on window
272     BitBlt( DC, x, y, w, h, ( (GTK2Graphics *)Image )->GetImageHandle(),
273             x, y, SRCCOPY );
274
275     // Release window device context
276     ReleaseDC( hWnd, DC );
277
278 */
279
280     gdk_draw_drawable( gWnd, gc, (( GTK2Graphics* )Image )->GetImage(),
281             x, y, x, y, w, h );
282 }
283 //---------------------------------------------------------------------------
284 void GTK2Window::WindowManualMove()
285 {
286     // Get mouse cursor position
287     int x, y;
288     gdk_window_get_pointer( gdk_get_default_root_window(), &x, &y, NULL );
289
290     // Move window and chek for magnetism
291     p_intf->p_sys->p_theme->MoveSkinMagnet( this,
292         WindowX + x - CursorX, WindowY + y - CursorY );
293
294     fprintf( stderr, "---------\n" );
295 }
296 //---------------------------------------------------------------------------
297 void GTK2Window::WindowManualMoveInit()
298 {
299     gdk_window_get_pointer( gdk_get_default_root_window(), &CursorX, &CursorY,
300                             NULL );
301     WindowX = Left;
302     WindowY = Top;
303 }
304 //---------------------------------------------------------------------------
305 void GTK2Window::Move( int left, int top )
306 {
307     Left = left;
308     Top  = top;
309     gdk_window_move( gWnd, left, top );
310 }
311 //---------------------------------------------------------------------------
312 void GTK2Window::Size( int width, int height )
313 {
314     Width  = width;
315     Height = height;
316     gdk_window_resize( gWnd, width, height );
317 }
318 //---------------------------------------------------------------------------
319 void GTK2Window::ChangeToolTipText( string text )
320 {
321 /*    if( text == "none" )
322     {
323         if( ToolTipText != "none" )
324         {
325             ToolTipText = "none";
326             ToolTipInfo.lpszText = NULL;
327             SendMessage( ToolTipWindow, TTM_ACTIVATE, 0 , 0 );
328         }
329     }
330     else
331     {
332         if( text != ToolTipText )
333         {
334             ToolTipText = text;
335             ToolTipInfo.lpszText = (char *)ToolTipText.c_str();
336             SendMessage( ToolTipWindow, TTM_ACTIVATE, 1 , 0 );
337             SendMessage( ToolTipWindow, TTM_UPDATETIPTEXT, 0,
338                              (LPARAM)(LPTOOLINFO)&ToolTipInfo );
339         }
340     }
341 */
342 }
343 //---------------------------------------------------------------------------
344
345 #endif