* modules/gui/skins/x11/x11_window.cpp: avoid flicker when mapping a window.
[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.17 2003/06/08 18:40:10 gbazin 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     XResizeWindow( display, Wnd, 1, 1 ); // Avoid flicker
164
165     if( show )
166     {
167         // We do the call to XShapeCombineRegion() here because the window
168         // must be unmapped for this to work.
169         Drawable drawable = (( X11Graphics* )Image )->GetImage();
170
171         XLOCK;
172         XImage *image = XGetImage( display, drawable, 0, 0, Width, Height, 
173                                    AllPlanes, ZPixmap );
174         if( image )
175         {
176             // Mask for transparency
177             Region region = XCreateRegion();
178             region = XCreateRegion();
179             for( int line = 0; line < Height; line++ )
180             {
181                 int start = 0, end = 0;
182                 while( start < Width )
183                 {
184                     while( start < Width && XGetPixel( image, start, line )
185                            == 0 )
186                     {
187                         start++;
188                     }
189                     end = start;
190                     while( end < Width && XGetPixel( image, end, line ) != 0)
191                     {
192                         end++;
193                     }
194                     XRectangle rect;
195                     rect.x = start;
196                     rect.y = line;
197                     rect.width = end - start + 1;
198                     rect.height = 1;
199                     Region newRegion = XCreateRegion();
200                     XUnionRectWithRegion( &rect, region, newRegion );
201                     XDestroyRegion( region );
202                     region = newRegion;
203                     start = end + 1;
204                 }
205             }
206
207             XShapeCombineRegion( display, Wnd, ShapeBounding, 0, 0, region,
208                                  ShapeSet );
209             XDestroyRegion( region );
210
211         }
212         else
213         {
214             msg_Err( p_intf, "X11Window::OSShow XShapeCombineRegion() failed");
215         }
216
217         XMapWindow( display, Wnd );
218         XMoveResizeWindow( display, Wnd, Left, Top, Width, Height );
219         XUNLOCK;
220     }
221     else
222     {
223         XLOCK;
224         XUnmapWindow( display, Wnd );
225         XUNLOCK;
226     }
227 }
228 //---------------------------------------------------------------------------
229 bool X11Window::ProcessOSEvent( Event *evt )
230 {
231     unsigned int msg = evt->GetMessage();
232     unsigned int p1  = evt->GetParam1();
233     int          p2  = evt->GetParam2();
234     int          time;
235     int          posX, posY;
236
237     switch( msg )
238     {
239         case Expose:
240             RefreshFromImage( 0, 0, Width, Height );
241             return true;
242
243         case MotionNotify:
244             if( LButtonDown )
245                 MouseMove( (int)( (XMotionEvent *)p2 )->x,
246                            (int)( (XMotionEvent *)p2 )->y, 1 );
247             else if( RButtonDown )
248                 MouseMove( (int)( (XMotionEvent *)p2 )->x,
249                            (int)( (XMotionEvent *)p2 )->y, 2 );
250             else
251                 MouseMove( (int)( (XMotionEvent *)p2 )->x,
252                            (int)( (XMotionEvent *)p2 )->y, 0 );
253             return true;
254
255
256         case ButtonPress:
257             // Raise all the windows
258             for( list<SkinWindow *>::const_iterator win = 
259                     p_intf->p_sys->p_theme->WindowList.begin();
260                     win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
261             {
262                 XLOCK;
263                 XRaiseWindow( display, ( (X11Window *)(*win) )->GetHandle() );
264                 XUNLOCK;
265             }
266
267             switch( ( (XButtonEvent *)p2 )->button )
268             {
269                 case 1:
270                     // Left button
271                     time = OSAPI_GetTime();
272                     OSAPI_GetMousePos( posX, posY );
273                     if( time - ClickedTime < DblClickDelay && 
274                         posX == ClickedX && posY == ClickedY )
275                     {
276                         // Double-click
277                         ClickedTime = 0; 
278                         MouseDblClick( (int)( (XButtonEvent *)p2 )->x,
279                                        (int)( (XButtonEvent *)p2 )->y, 1 );
280                     }
281                     else
282                     {
283                         ClickedTime = time;
284                         ClickedX = posX;
285                         ClickedY = posY;
286                         LButtonDown = true;
287                         MouseDown( (int)( (XButtonEvent *)p2 )->x,
288                                    (int)( (XButtonEvent *)p2 )->y, 1 );
289                     }
290                     break;
291
292                 case 3:
293                     // Right button
294                     RButtonDown = true;
295                     MouseDown( (int)( (XButtonEvent *)p2 )->x,
296                                (int)( (XButtonEvent *)p2 )->y, 2 );
297                     break;
298
299                 default:
300                     break;
301             }
302             return true;
303
304         case ButtonRelease:
305             switch( ( (XButtonEvent *)p2 )->button )
306             {
307                 case 1:
308                     // Left button
309                     LButtonDown = false;
310                     MouseUp( (int)( (XButtonEvent *)p2 )->x,
311                              (int)( (XButtonEvent *)p2 )->y, 1 );
312                     break;
313
314                 case 3:
315                     // Right button
316                     RButtonDown = false;
317                     MouseUp( (int)( (XButtonEvent *)p2 )->x,
318                              (int)( (XButtonEvent *)p2 )->y, 2 );
319                     break; 
320
321                 case 4:
322                     // Scroll up
323                     MouseScroll( (int)( (XButtonEvent *)p2 )->x,
324                                  (int)( (XButtonEvent *)p2 )->y,
325                                  MOUSE_SCROLL_UP);
326                     break;
327  
328                 case 5:
329                     // Scroll down
330                     MouseScroll( (int)( (XButtonEvent *)p2 )->x,
331                                  (int)( (XButtonEvent *)p2 )->y,
332                                  MOUSE_SCROLL_DOWN);
333                     break;
334
335                 default:
336                     break;
337             }
338             return true;
339
340         case LeaveNotify:
341             OSAPI_PostMessage( this, WINDOW_LEAVE, 0, 0 );
342             return true;
343
344         case ClientMessage:
345             {
346             string type = XGetAtomName( display, ( (XClientMessageEvent*)
347                                                     p2 )->message_type );
348             if( type == "XdndEnter" )
349             {
350                 DropObject->DndEnter( ((XClientMessageEvent*)p2)->data.l );
351                 return true;
352             }
353             else if( type == "XdndPosition" )
354             {
355                 DropObject->DndPosition( ((XClientMessageEvent*)p2)->data.l );
356                 return true;
357             }
358             else if( type == "XdndLeave" )
359             {
360                 DropObject->DndLeave( ((XClientMessageEvent*)p2)->data.l );
361                 return true;
362             }
363             else if( type == "XdndDrop" )
364             {
365                 DropObject->DndDrop( ((XClientMessageEvent*)p2)->data.l );
366                 return true;
367             }
368             }
369             return false;
370             
371         default:
372             return false;
373     }
374 }
375 //---------------------------------------------------------------------------
376 void X11Window::SetTransparency( int Value )
377 {
378 /*    if( Value > -1 )
379         Alpha = Value;
380     SetLayeredWindowAttributes( hWnd, 0, Alpha, LWA_ALPHA | LWA_COLORKEY );
381     UpdateWindow( hWnd );*/
382 }
383 //---------------------------------------------------------------------------
384 void X11Window::RefreshFromImage( int x, int y, int w, int h )
385 {
386     Drawable drawable = (( X11Graphics* )Image )->GetImage();
387
388     XLOCK;
389     XCopyArea( display, drawable, Wnd, Gc, x, y, w, h, x, y );
390     XSync( display, 0);
391     XUNLOCK;
392 }
393 //---------------------------------------------------------------------------
394 void X11Window::WindowManualMove()
395 {
396     // Get mouse cursor position
397     int x, y;
398     OSAPI_GetMousePos( x, y );
399
400     // Move window and chek for magnetism
401     p_intf->p_sys->p_theme->MoveSkinMagnet( this,
402         WindowX + x - CursorX, WindowY + y - CursorY );
403
404 }
405 //---------------------------------------------------------------------------
406 void X11Window::WindowManualMoveInit()
407 {
408     OSAPI_GetMousePos( CursorX, CursorY );
409     WindowX = Left;
410     WindowY = Top;
411 }
412 //---------------------------------------------------------------------------
413 void X11Window::Move( int left, int top )
414 {
415     Left = left;
416     Top  = top;
417     XMoveWindow( display, Wnd, left, top );
418 }
419 //---------------------------------------------------------------------------
420 void X11Window::Size( int width, int height )
421 {
422     Width  = width;
423     Height = height;
424     XResizeWindow( display, Wnd, width, height );
425 }
426 //---------------------------------------------------------------------------
427
428
429 bool ToolTipCallback( void *data )
430 {
431     int direction, fontAscent, fontDescent;
432
433     Display *disp = ((tooltip_t*)data)->display;
434     Window win = ((tooltip_t*)data)->window;
435     Font font = ((tooltip_t*)data)->font;
436     GC gc = ((tooltip_t*)data)->gc;
437     string text = ((tooltip_t*)data)->text;
438     int curX = ((tooltip_t*)data)->curX;
439     int curY = ((tooltip_t*)data)->curY;
440  
441     XLOCK;
442     XClearWindow( disp, win );
443     XCharStruct overall;
444     XQueryTextExtents( disp, font, text.c_str(), text.size(), &direction, 
445                        &fontAscent, &fontDescent, &overall );
446     int w = overall.rbearing - overall.lbearing;
447     int h = overall.ascent + overall.descent;
448     XMapRaised( disp, win );
449     XMoveWindow( disp, win, curX - w/4, curY + 20 );
450     XResizeWindow( disp, win, w+8, h+8 );
451     XDrawString( disp, win, gc, 4, overall.ascent+4, text.c_str(), 
452                  text.size() );
453     XSync( disp, 0 );
454     XUNLOCK;
455     
456     return False;
457 }
458
459
460 void X11Window::ChangeToolTipText( string text )
461 {
462     if( text == "none" )
463     {
464         if( ToolTipText != "none" )
465         {
466             ToolTipText = "none";
467             XLOCK;
468             // Hide the tooltip window
469             X11TimerManager *timerManager = X11TimerManager::Instance( p_intf );
470             timerManager->removeTimer( ToolTip.timer );
471             XUnmapWindow( display, ToolTip.window );
472             XResizeWindow( display, ToolTip.window, 1, 1 );
473             XSync( display, 0 );
474             XUNLOCK;
475         }
476     }
477     else
478     {
479         if( text != ToolTipText )
480         {
481             ToolTipText = text;
482             ToolTip.text = text;
483             OSAPI_GetMousePos( ToolTip.curX, ToolTip.curY );
484             X11TimerManager *timerManager = X11TimerManager::Instance( p_intf );
485             timerManager->addTimer( ToolTip.timer );
486         }
487     }
488 }
489 //---------------------------------------------------------------------------
490
491 #endif