* mouse wheel scroll for X11 skins
[vlc.git] / modules / gui / skins / x11 / x11_window.cpp
1 /*****************************************************************************
2  * x11_window.cpp: X11 implementation of the Window class
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: x11_window.cpp,v 1.8 2003/06/01 17:13:04 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 #ifdef X11_SKINS
26
27 //--- GENERAL ---------------------------------------------------------------
28 //#include <math.h>
29
30 //--- VLC -------------------------------------------------------------------
31 #include <vlc/intf.h>
32
33 //--- X11 -------------------------------------------------------------------
34 #include <X11/Xlib.h>
35 #include <X11/extensions/shape.h>
36
37 //--- SKIN ------------------------------------------------------------------
38 #include "../os_api.h"
39 #include "../src/anchor.h"
40 #include "../controls/generic.h"
41 #include "../src/window.h"
42 #include "../os_window.h"
43 #include "../src/event.h"
44 #include "../os_event.h"
45 #include "../src/graphics.h"
46 #include "../os_graphics.h"
47 #include "../src/skin_common.h"
48 #include "../src/theme.h"
49
50
51 //---------------------------------------------------------------------------
52 // Skinable Window
53 //---------------------------------------------------------------------------
54 X11Window::X11Window( intf_thread_t *p_intf, Window wnd, int x, int y,
55     bool visible, int transition, int normalalpha, int movealpha,
56     bool dragdrop, string name )
57     : SkinWindow( p_intf, x, y, visible, transition, normalalpha, movealpha,
58               dragdrop )
59 {
60     // Set handles
61     Wnd           = wnd;
62
63     display = p_intf->p_sys->display;
64     int screen = DefaultScreen( display );
65
66     Gc = DefaultGC( display, screen );
67
68     Name        = name;
69
70     LButtonDown = false;
71     RButtonDown = false;
72
73     // Removing fading effect
74     Transition  = 0;
75 /*
76     // Set position parameters
77     CursorPos    = new POINT;
78     WindowPos    = new POINT;
79
80     // Create Tool Tip Window
81     ToolTipWindow = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
82         WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
83         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
84         hWnd, 0, GetModuleHandle( NULL ), 0);
85
86     // Create Tool Tip infos
87     ToolTipInfo.cbSize = sizeof(TOOLINFO);
88     ToolTipInfo.uFlags = TTF_SUBCLASS|TTF_IDISHWND;
89     ToolTipInfo.hwnd = hWnd;
90     ToolTipInfo.hinst = GetModuleHandle( NULL );
91     ToolTipInfo.uId = (unsigned int)hWnd;
92     ToolTipInfo.lpszText = NULL;
93     ToolTipInfo.rect.left = ToolTipInfo.rect.top = 0;
94         ToolTipInfo.rect.right = ToolTipInfo.rect.bottom = 0;
95
96     SendMessage( ToolTipWindow, TTM_ADDTOOL, 0,
97                     (LPARAM)(LPTOOLINFO) &ToolTipInfo );
98 */
99     if( DragDrop )
100     {
101         // register the listview as a drop target
102         DropObject = new X11DropObject( p_intf );
103     //    gdk_window_register_dnd( gwnd );
104     }
105
106     // Create Tool Tip window
107 /*    GdkWindowAttr attr;
108     attr.event_mask = GDK_ALL_EVENTS_MASK;
109     attr.width = 100;
110     attr.height = 100;
111     attr.window_type = GDK_WINDOW_CHILD;
112     attr.wclass = GDK_INPUT_OUTPUT;
113     gint mask = 0;
114     ToolTipWindow = gdk_window_new( gwnd, &attr, mask);*/
115
116     Open();
117     //fprintf(stderr, "kludge in x11_window.cpp\n");
118
119 }
120 //---------------------------------------------------------------------------
121 X11Window::~X11Window()
122 {
123 /*    delete CursorPos;
124     delete WindowPos;
125
126     if( hWnd != NULL )
127     {
128         DestroyWindow( hWnd );
129     }
130     if( ToolTipWindow != NULL )
131     {
132         DestroyWindow( ToolTipWindow );
133     }
134     if( DragDrop )
135     {
136         // Remove the listview from the list of drop targets
137         RevokeDragDrop( hWnd );
138         DropTarget->Release();
139         // Uninitialize the OLE library
140         OleUninitialize();
141     }*/
142  /*   if( gWnd )
143     {
144         gdk_window_destroy( gWnd );
145     }*/
146 }
147 //---------------------------------------------------------------------------
148 void X11Window::OSShow( bool show )
149 {
150     if( show )
151     {
152         XMapWindow( display, Wnd );
153         XMoveWindow( display, Wnd, Left, Top );
154     }
155     else
156     {
157         //XWithdrawWindow( display, Wnd, 0 );
158         XUnmapWindow( display, Wnd );
159     }
160 }
161 //---------------------------------------------------------------------------
162 bool X11Window::ProcessOSEvent( Event *evt )
163 {
164     unsigned int msg = evt->GetMessage();
165     unsigned int p1  = evt->GetParam1();
166     int          p2  = evt->GetParam2();
167
168     switch( msg )
169     {
170         case Expose:
171             RefreshFromImage( 0, 0, Width, Height );
172             return true;
173
174         case MotionNotify:
175             if( LButtonDown )
176                 MouseMove( (int)( (XMotionEvent *)p2 )->x,
177                            (int)( (XMotionEvent *)p2 )->y, 1 );
178             else if( RButtonDown )
179                 MouseMove( (int)( (XMotionEvent *)p2 )->x,
180                            (int)( (XMotionEvent *)p2 )->y, 2 );
181             else
182                 MouseMove( (int)( (XMotionEvent *)p2 )->x,
183                            (int)( (XMotionEvent *)p2 )->y, 0 );
184             return true;
185
186
187         case ButtonPress:
188             // Raise all the windows
189             for( list<SkinWindow *>::const_iterator win = 
190                     p_intf->p_sys->p_theme->WindowList.begin();
191                     win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
192             {
193                 XRaiseWindow( display, ( (X11Window *)(*win) )->GetHandle() );
194             }
195
196             switch( ( (XButtonEvent *)p2 )->button )
197             {
198                 case 1:
199                     // Left button
200                     LButtonDown = true;
201                     MouseDown( (int)( (XButtonEvent *)p2 )->x,
202                                (int)( (XButtonEvent *)p2 )->y, 1 );
203                     break;
204
205                 case 3:
206                     // Right button
207                     RButtonDown = true;
208                     MouseDown( (int)( (XButtonEvent *)p2 )->x,
209                                (int)( (XButtonEvent *)p2 )->y, 2 );
210                     break;
211
212                 default:
213                     break;
214             }
215             return true;
216
217         case ButtonRelease:
218             switch( ( (XButtonEvent *)p2 )->button )
219             {
220                 case 1:
221                     // Left button
222                     LButtonDown = false;
223                     MouseUp( (int)( (XButtonEvent *)p2 )->x,
224                              (int)( (XButtonEvent *)p2 )->y, 1 );
225                     break;
226
227                 case 3:
228                     // Right button
229                     RButtonDown = false;
230                     MouseUp( (int)( (XButtonEvent *)p2 )->x,
231                              (int)( (XButtonEvent *)p2 )->y, 2 );
232                     break; 
233                     
234                 case 4:
235                     // Scroll up
236                     MouseScroll( (int)( (XButtonEvent *)p2 )->x,
237                                  (int)( (XButtonEvent *)p2 )->y,
238                                  MOUSE_SCROLL_UP);
239                     break;
240                     
241                 case 5:
242                     // Scroll down
243                     MouseScroll( (int)( (XButtonEvent *)p2 )->x,
244                                  (int)( (XButtonEvent *)p2 )->y,
245                                  MOUSE_SCROLL_DOWN);
246                     break;
247
248                 default:
249                     break;
250             }
251             return true;
252
253         case LeaveNotify:
254             OSAPI_PostMessage( this, WINDOW_LEAVE, 0, 0 );
255             return true;
256
257 /*        case GDK_2BUTTON_PRESS:
258             MouseDblClick( (int)( (GdkEventButton *)p2 )->x,
259                            (int)( (GdkEventButton *)p2 )->y, 1 );
260             return true;
261
262         case GDK_DROP_START:
263             DropObject->HandleDropStart( ( (GdkEventDND *)p2 )->context );
264             return true;
265 */
266         default:
267             return false;
268     }
269 }
270 //---------------------------------------------------------------------------
271 void X11Window::SetTransparency( int Value )
272 {
273 /*    if( Value > -1 )
274         Alpha = Value;
275     SetLayeredWindowAttributes( hWnd, 0, Alpha, LWA_ALPHA | LWA_COLORKEY );
276     UpdateWindow( hWnd );*/
277 }
278 //---------------------------------------------------------------------------
279 void X11Window::RefreshFromImage( int x, int y, int w, int h )
280 {
281     Drawable drawable = (( X11Graphics* )Image )->GetImage();
282     
283     XCopyArea( display, drawable, Wnd, Gc, x, y, w, h, x, y );
284  
285     XImage *image = XGetImage( display, drawable, 0, 0, Width, Height, 
286                                AllPlanes, ZPixmap );
287  
288     // Mask for transparency
289     Region region = XCreateRegion();
290     for( int line = 0; line < Height; line++ )
291     {
292         int start = 0, end = 0;
293         while( start < Width )
294         {
295             while( start < Width && XGetPixel( image, start, line ) == 0 )
296             {
297                 start++;
298             } 
299             end = start;
300             while( end < Width && XGetPixel( image, end, line ) != 0)
301             {
302                 end++;
303             }
304             XRectangle rect;
305             rect.x = start;
306             rect.y = line;
307             rect.width = end - start + 1;
308             rect.height = 1;
309             Region newRegion = XCreateRegion();
310             XUnionRectWithRegion( &rect, region, newRegion );
311             XDestroyRegion( region );
312             region = newRegion;
313             start = end + 1;
314         }
315     }
316     XShapeCombineRegion( display, Wnd, ShapeBounding, 0, 0, region, ShapeSet );
317     XDestroyRegion( region );
318
319     XSync( display, 0);
320 }
321 //---------------------------------------------------------------------------
322 void X11Window::WindowManualMove()
323 {
324     // Get mouse cursor position
325     int x, y;
326     OSAPI_GetMousePos( x, y );
327
328     // Move window and chek for magnetism
329     p_intf->p_sys->p_theme->MoveSkinMagnet( this,
330         WindowX + x - CursorX, WindowY + y - CursorY );
331
332 }
333 //---------------------------------------------------------------------------
334 void X11Window::WindowManualMoveInit()
335 {
336     OSAPI_GetMousePos( CursorX, CursorY );
337     WindowX = Left;
338     WindowY = Top;
339 }
340 //---------------------------------------------------------------------------
341 void X11Window::Move( int left, int top )
342 {
343     Left = left;
344     Top  = top;
345     XMoveWindow( display, Wnd, left, top );
346 }
347 //---------------------------------------------------------------------------
348 void X11Window::Size( int width, int height )
349 {
350     Width  = width;
351     Height = height;
352     XResizeWindow( display, Wnd, width, height );
353 }
354 //---------------------------------------------------------------------------
355 void X11Window::ChangeToolTipText( string text )
356 {
357 /*    if( text == "none" )
358     {
359         if( ToolTipText != "none" )
360         {
361             ToolTipText = "none";
362             ToolTipInfo.lpszText = NULL;
363             SendMessage( ToolTipWindow, TTM_ACTIVATE, 0 , 0 );
364         }
365     }
366     else
367     {
368         if( text != ToolTipText )
369         {
370             ToolTipText = text;
371             ToolTipInfo.lpszText = (char *)ToolTipText.c_str();
372             SendMessage( ToolTipWindow, TTM_ACTIVATE, 1 , 0 );
373             SendMessage( ToolTipWindow, TTM_UPDATETIPTEXT, 0,
374                              (LPARAM)(LPTOOLINFO)&ToolTipInfo );
375         }
376     }
377 */
378 }
379 //---------------------------------------------------------------------------
380
381 #endif