914c2b1213d1c6d184323a008cd757668abb1555
[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.16 2003/06/08 18:17:50 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/Xatom.h>
36 #include <X11/extensions/shape.h>
37
38 //--- SKIN ------------------------------------------------------------------
39 #include "../os_api.h"
40 #include "../src/anchor.h"
41 #include "../controls/generic.h"
42 #include "../src/window.h"
43 #include "../os_window.h"
44 #include "../src/event.h"
45 #include "../os_event.h"
46 #include "../src/graphics.h"
47 #include "../os_graphics.h"
48 #include "../src/skin_common.h"
49 #include "../src/theme.h"
50 #include "../os_theme.h"
51 #include "x11_timer.h"
52
53
54 bool ToolTipCallback( void *data );
55
56
57 //---------------------------------------------------------------------------
58 // Skinable Window
59 //---------------------------------------------------------------------------
60 X11Window::X11Window( intf_thread_t *p_intf, Window wnd, int x, int y,
61     bool visible, int transition, int normalalpha, int movealpha,
62     bool dragdrop, string name )
63     : SkinWindow( p_intf, x, y, visible, transition, normalalpha, movealpha,
64               dragdrop )
65 {
66     // Set handles
67     Wnd         = wnd;
68     display     = p_intf->p_sys->display;
69     int screen  = DefaultScreen( display );
70     Name        = name;
71     LButtonDown = false;
72     RButtonDown = false;
73
74     // Creation of a graphic context that doesn't generate a GraphicsExpose
75     // event when using functions like XCopyArea
76     XGCValues gcVal;
77     gcVal.graphics_exposures = False;
78     XLOCK;
79     Gc = XCreateGC( display, wnd, GCGraphicsExposures, &gcVal );
80     XUNLOCK;
81
82     // Removing fading effect
83     Transition  = 0;
84
85     if( DragDrop )
86     {
87         // register the listview as a drop target
88         DropObject = new X11DropObject( p_intf );
89
90         Atom xdndAtom = XInternAtom( display, "XdndAware", False );
91         char xdndVersion = 4;
92         XLOCK;
93         XChangeProperty( display, wnd, xdndAtom, XA_ATOM, 32, 
94                          PropModeReplace, (unsigned char *)&xdndVersion, 1);
95         XUNLOCK;
96     }
97
98     // Create Tool Tip window
99     XColor color;
100     color.red = 0xffff;
101     color.green = 0xffff;
102     color.blue = 0xa000;
103     Colormap cm = DefaultColormap( display, screen );
104     Window root = DefaultRootWindow( display );
105
106     XLOCK;
107     XAllocColor( display, cm, &color );
108     XSetWindowAttributes attr;
109     attr.background_pixel = color.pixel;
110     attr.override_redirect = True;
111     ToolTip.window = XCreateWindow( display, root, 0, 0, 1, 1, 1, 0,
112                                     InputOutput, CopyFromParent,
113                                     CWBackPixel|CWOverrideRedirect, &attr );
114     ToolTip.font = XLoadFont( display,
115                               "-*-helvetica-bold-r-*-*-*-80-*-*-*-*-*-*" );
116     gcVal.font = ToolTip.font;
117     gcVal.foreground = 0;
118     gcVal.background = color.pixel;
119     ToolTip.gc = XCreateGC( display, ToolTip.window, 
120                             GCBackground|GCForeground|GCFont, &gcVal );
121     XUNLOCK;
122
123     ToolTip.display = display;
124     X11Timer *timer = new X11Timer( p_intf, 500000, ToolTipCallback, &ToolTip );
125     ToolTip.p_intf = p_intf;
126     ToolTip.timer = timer;
127
128     // Double-click handling
129     ClickedX = 0;
130     ClickedY = 0;
131     ClickedTime = 0;
132     // TODO: can be retrieved somewhere ?
133     DblClickDelay = 400;
134    
135 }
136 //---------------------------------------------------------------------------
137 X11Window::~X11Window()
138 {
139 /*    delete CursorPos;
140     delete WindowPos;
141
142     if( hWnd != NULL )
143     {
144         DestroyWindow( hWnd );
145     }*/
146
147     XFreeGC( display, Gc );
148     XDestroyWindow( display, ToolTip.window );
149
150     /*
151     if( DragDrop )
152     {
153         // Remove the listview from the list of drop targets
154         RevokeDragDrop( hWnd );
155         DropTarget->Release();
156         // Uninitialize the OLE library
157         OleUninitialize();
158     }*/
159 }
160 //---------------------------------------------------------------------------
161 void X11Window::OSShow( bool show )
162 {
163     if( show )
164     {
165         // We do the call to XShapeCombineRegion() here because the window
166         // must be unmapped for this to work.
167         Drawable drawable = (( X11Graphics* )Image )->GetImage();
168
169         XLOCK;
170         XImage *image = XGetImage( display, drawable, 0, 0, Width, Height, 
171                                    AllPlanes, ZPixmap );
172         if( image )
173         {
174             // Mask for transparency
175             Region region = XCreateRegion();
176             region = XCreateRegion();
177             for( int line = 0; line < Height; line++ )
178             {
179                 int start = 0, end = 0;
180                 while( start < Width )
181                 {
182                     while( start < Width && XGetPixel( image, start, line )
183                            == 0 )
184                     {
185                         start++;
186                     }
187                     end = start;
188                     while( end < Width && XGetPixel( image, end, line ) != 0)
189                     {
190                         end++;
191                     }
192                     XRectangle rect;
193                     rect.x = start;
194                     rect.y = line;
195                     rect.width = end - start + 1;
196                     rect.height = 1;
197                     Region newRegion = XCreateRegion();
198                     XUnionRectWithRegion( &rect, region, newRegion );
199                     XDestroyRegion( region );
200                     region = newRegion;
201                     start = end + 1;
202                 }
203             }
204
205             XShapeCombineRegion( display, Wnd, ShapeBounding, 0, 0, region,
206                                  ShapeSet );
207             XDestroyRegion( region );
208
209         }
210         else
211         {
212             msg_Err( p_intf, "X11Window::OSShow XShapeCombineRegion() failed");
213         }
214
215         XMapWindow( display, Wnd );
216         XMoveWindow( display, Wnd, Left, Top );
217         XUNLOCK;
218     }
219     else
220     {
221         XLOCK;
222         XUnmapWindow( display, Wnd );
223         XUNLOCK;
224     }
225 }
226 //---------------------------------------------------------------------------
227 bool X11Window::ProcessOSEvent( Event *evt )
228 {
229     unsigned int msg = evt->GetMessage();
230     unsigned int p1  = evt->GetParam1();
231     int          p2  = evt->GetParam2();
232     int          time;
233     int          posX, posY;
234
235     switch( msg )
236     {
237         case Expose:
238             RefreshFromImage( 0, 0, Width, Height );
239             return true;
240
241         case MotionNotify:
242             if( LButtonDown )
243                 MouseMove( (int)( (XMotionEvent *)p2 )->x,
244                            (int)( (XMotionEvent *)p2 )->y, 1 );
245             else if( RButtonDown )
246                 MouseMove( (int)( (XMotionEvent *)p2 )->x,
247                            (int)( (XMotionEvent *)p2 )->y, 2 );
248             else
249                 MouseMove( (int)( (XMotionEvent *)p2 )->x,
250                            (int)( (XMotionEvent *)p2 )->y, 0 );
251             return true;
252
253
254         case ButtonPress:
255             // Raise all the windows
256             for( list<SkinWindow *>::const_iterator win = 
257                     p_intf->p_sys->p_theme->WindowList.begin();
258                     win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
259             {
260                 XLOCK;
261                 XRaiseWindow( display, ( (X11Window *)(*win) )->GetHandle() );
262                 XUNLOCK;
263             }
264
265             switch( ( (XButtonEvent *)p2 )->button )
266             {
267                 case 1:
268                     // Left button
269                     time = OSAPI_GetTime();
270                     OSAPI_GetMousePos( posX, posY );
271                     if( time - ClickedTime < DblClickDelay && 
272                         posX == ClickedX && posY == ClickedY )
273                     {
274                         // Double-click
275                         ClickedTime = 0; 
276                         MouseDblClick( (int)( (XButtonEvent *)p2 )->x,
277                                        (int)( (XButtonEvent *)p2 )->y, 1 );
278                     }
279                     else
280                     {
281                         ClickedTime = time;
282                         ClickedX = posX;
283                         ClickedY = posY;
284                         LButtonDown = true;
285                         MouseDown( (int)( (XButtonEvent *)p2 )->x,
286                                    (int)( (XButtonEvent *)p2 )->y, 1 );
287                     }
288                     break;
289
290                 case 3:
291                     // Right button
292                     RButtonDown = true;
293                     MouseDown( (int)( (XButtonEvent *)p2 )->x,
294                                (int)( (XButtonEvent *)p2 )->y, 2 );
295                     break;
296
297                 default:
298                     break;
299             }
300             return true;
301
302         case ButtonRelease:
303             switch( ( (XButtonEvent *)p2 )->button )
304             {
305                 case 1:
306                     // Left button
307                     LButtonDown = false;
308                     MouseUp( (int)( (XButtonEvent *)p2 )->x,
309                              (int)( (XButtonEvent *)p2 )->y, 1 );
310                     break;
311
312                 case 3:
313                     // Right button
314                     RButtonDown = false;
315                     MouseUp( (int)( (XButtonEvent *)p2 )->x,
316                              (int)( (XButtonEvent *)p2 )->y, 2 );
317                     break; 
318
319                 case 4:
320                     // Scroll up
321                     MouseScroll( (int)( (XButtonEvent *)p2 )->x,
322                                  (int)( (XButtonEvent *)p2 )->y,
323                                  MOUSE_SCROLL_UP);
324                     break;
325  
326                 case 5:
327                     // Scroll down
328                     MouseScroll( (int)( (XButtonEvent *)p2 )->x,
329                                  (int)( (XButtonEvent *)p2 )->y,
330                                  MOUSE_SCROLL_DOWN);
331                     break;
332
333                 default:
334                     break;
335             }
336             return true;
337
338         case LeaveNotify:
339             OSAPI_PostMessage( this, WINDOW_LEAVE, 0, 0 );
340             return true;
341
342         case ClientMessage:
343             {
344             string type = XGetAtomName( display, ( (XClientMessageEvent*)
345                                                     p2 )->message_type );
346             if( type == "XdndEnter" )
347             {
348                 DropObject->DndEnter( ((XClientMessageEvent*)p2)->data.l );
349                 return true;
350             }
351             else if( type == "XdndPosition" )
352             {
353                 DropObject->DndPosition( ((XClientMessageEvent*)p2)->data.l );
354                 return true;
355             }
356             else if( type == "XdndLeave" )
357             {
358                 DropObject->DndLeave( ((XClientMessageEvent*)p2)->data.l );
359                 return true;
360             }
361             else if( type == "XdndDrop" )
362             {
363                 DropObject->DndDrop( ((XClientMessageEvent*)p2)->data.l );
364                 return true;
365             }
366             }
367             return false;
368             
369         default:
370             return false;
371     }
372 }
373 //---------------------------------------------------------------------------
374 void X11Window::SetTransparency( int Value )
375 {
376 /*    if( Value > -1 )
377         Alpha = Value;
378     SetLayeredWindowAttributes( hWnd, 0, Alpha, LWA_ALPHA | LWA_COLORKEY );
379     UpdateWindow( hWnd );*/
380 }
381 //---------------------------------------------------------------------------
382 void X11Window::RefreshFromImage( int x, int y, int w, int h )
383 {
384     Drawable drawable = (( X11Graphics* )Image )->GetImage();
385
386     XLOCK;
387     XCopyArea( display, drawable, Wnd, Gc, x, y, w, h, x, y );
388     XSync( display, 0);
389     XUNLOCK;
390 }
391 //---------------------------------------------------------------------------
392 void X11Window::WindowManualMove()
393 {
394     // Get mouse cursor position
395     int x, y;
396     OSAPI_GetMousePos( x, y );
397
398     // Move window and chek for magnetism
399     p_intf->p_sys->p_theme->MoveSkinMagnet( this,
400         WindowX + x - CursorX, WindowY + y - CursorY );
401
402 }
403 //---------------------------------------------------------------------------
404 void X11Window::WindowManualMoveInit()
405 {
406     OSAPI_GetMousePos( CursorX, CursorY );
407     WindowX = Left;
408     WindowY = Top;
409 }
410 //---------------------------------------------------------------------------
411 void X11Window::Move( int left, int top )
412 {
413     Left = left;
414     Top  = top;
415     XMoveWindow( display, Wnd, left, top );
416 }
417 //---------------------------------------------------------------------------
418 void X11Window::Size( int width, int height )
419 {
420     Width  = width;
421     Height = height;
422     XResizeWindow( display, Wnd, width, height );
423 }
424 //---------------------------------------------------------------------------
425
426
427 bool ToolTipCallback( void *data )
428 {
429     int direction, fontAscent, fontDescent;
430
431     Display *disp = ((tooltip_t*)data)->display;
432     Window win = ((tooltip_t*)data)->window;
433     Font font = ((tooltip_t*)data)->font;
434     GC gc = ((tooltip_t*)data)->gc;
435     string text = ((tooltip_t*)data)->text;
436     int curX = ((tooltip_t*)data)->curX;
437     int curY = ((tooltip_t*)data)->curY;
438  
439     XLOCK;
440     XClearWindow( disp, win );
441     XCharStruct overall;
442     XQueryTextExtents( disp, font, text.c_str(), text.size(), &direction, 
443                        &fontAscent, &fontDescent, &overall );
444     int w = overall.rbearing - overall.lbearing;
445     int h = overall.ascent + overall.descent;
446     XMapRaised( disp, win );
447     XMoveWindow( disp, win, curX - w/4, curY + 20 );
448     XResizeWindow( disp, win, w+8, h+8 );
449     XDrawString( disp, win, gc, 4, overall.ascent+4, text.c_str(), 
450                  text.size() );
451     XSync( disp, 0 );
452     XUNLOCK;
453     
454     return False;
455 }
456
457
458 void X11Window::ChangeToolTipText( string text )
459 {
460     if( text == "none" )
461     {
462         if( ToolTipText != "none" )
463         {
464             ToolTipText = "none";
465             XLOCK;
466             // Hide the tooltip window
467             X11TimerManager *timerManager = X11TimerManager::Instance( p_intf );
468             timerManager->removeTimer( ToolTip.timer );
469             XUnmapWindow( display, ToolTip.window );
470             XResizeWindow( display, ToolTip.window, 1, 1 );
471             XSync( display, 0 );
472             XUNLOCK;
473         }
474     }
475     else
476     {
477         if( text != ToolTipText )
478         {
479             ToolTipText = text;
480             ToolTip.text = text;
481             OSAPI_GetMousePos( ToolTip.curX, ToolTip.curY );
482             X11TimerManager *timerManager = X11TimerManager::Instance( p_intf );
483             timerManager->addTimer( ToolTip.timer );
484         }
485     }
486 }
487 //---------------------------------------------------------------------------
488
489 #endif