1 /*****************************************************************************
2 * x11_window.cpp: X11 implementation of the Window class
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: x11_window.cpp,v 1.26 2003/06/22 12:54:03 asmax Exp $
7 * Authors: Cyril Deguet <asmax@videolan.org>
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.
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.
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,
23 *****************************************************************************/
27 //--- GENERAL ---------------------------------------------------------------
30 //--- VLC -------------------------------------------------------------------
33 //--- X11 -------------------------------------------------------------------
35 #include <X11/Xatom.h>
36 #include <X11/extensions/shape.h>
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"
54 static bool ToolTipCallback( void *data );
55 static void DrawToolTipText( tooltip_t *tooltip );
58 //---------------------------------------------------------------------------
60 //---------------------------------------------------------------------------
61 X11Window::X11Window( intf_thread_t *p_intf, Window wnd, int x, int y,
62 bool visible, int transition, int normalalpha, int movealpha,
63 bool dragdrop, string name )
64 : SkinWindow( p_intf, x, y, visible, transition, normalalpha, movealpha,
69 display = p_intf->p_sys->display;
70 int screen = DefaultScreen( display );
75 // Creation of a graphic context that doesn't generate a GraphicsExpose
76 // event when using functions like XCopyArea
78 gcVal.graphics_exposures = False;
80 Gc = XCreateGC( display, wnd, GCGraphicsExposures, &gcVal );
83 // Removing fading effect
88 // register the listview as a drop target
89 DropObject = new X11DropObject( p_intf, Wnd );
91 Atom xdndAtom = XInternAtom( display, "XdndAware", False );
94 XChangeProperty( display, wnd, xdndAtom, XA_ATOM, 32,
95 PropModeReplace, (unsigned char *)&xdndVersion, 1);
99 // Associate vlc icon to the window
101 XWMHints *hints = XGetWMHints( display, Wnd );
104 hints = XAllocWMHints();
106 if( p_intf->p_sys->iconPixmap != None )
108 hints->icon_pixmap = p_intf->p_sys->iconPixmap;
109 hints->flags |= IconPixmapHint;
111 if( p_intf->p_sys->iconMask != None )
113 hints->icon_mask = p_intf->p_sys->iconMask;
114 hints->flags |= IconMaskHint;
116 XSetWMHints( display, Wnd, hints );
120 // Create Tool Tip window
123 color.green = 0xffff;
125 Colormap cm = DefaultColormap( display, screen );
126 Window root = DefaultRootWindow( display );
129 XAllocColor( display, cm, &color );
130 XSetWindowAttributes attr;
131 attr.background_pixel = color.pixel;
132 attr.override_redirect = True;
133 ToolTip.window = XCreateWindow( display, root, 0, 0, 1, 1, 1, 0,
134 InputOutput, CopyFromParent,
135 CWBackPixel|CWOverrideRedirect, &attr );
136 ToolTip.font = XLoadFont( display,
137 "-*-helvetica-bold-r-*-*-*-80-*-*-*-*-*-*" );
138 gcVal.font = ToolTip.font;
139 gcVal.foreground = 0;
140 gcVal.background = color.pixel;
141 ToolTip.gc = XCreateGC( display, ToolTip.window,
142 GCBackground|GCForeground|GCFont, &gcVal );
145 ToolTip.display = display;
146 X11Timer *timer = new X11Timer( p_intf, 500000, ToolTipCallback, &ToolTip );
147 ToolTip.p_intf = p_intf;
148 ToolTip.timer = timer;
149 ToolTip.active = False;
151 // Double-click handling
155 // TODO: can be retrieved somewhere ?
159 //---------------------------------------------------------------------------
160 X11Window::~X11Window()
166 delete ToolTip.timer;
168 XFreeGC( display, ToolTip.gc );
169 XFreeGC( display, Gc );
170 XDestroyWindow( display, ToolTip.window );
171 XDestroyWindow( display, Wnd );
174 //---------------------------------------------------------------------------
175 void X11Window::OSShow( bool show )
178 XResizeWindow( display, Wnd, 1, 1 ); // Avoid flicker
183 // We do the call to XShapeCombineRegion() here because the window
184 // must be unmapped for this to work.
185 Drawable drawable = (( X11Graphics* )Image )->GetImage();
188 XImage *image = XGetImage( display, drawable, 0, 0, Width, Height,
189 AllPlanes, ZPixmap );
192 // Mask for transparency
193 Region region = XCreateRegion();
194 for( int line = 0; line < Height; line++ )
196 int start = 0, end = 0;
197 while( start < Width )
199 while( start < Width && XGetPixel( image, start, line )
205 while( end < Width && XGetPixel( image, end, line ) != 0)
212 rect.width = end - start + 1;
214 Region newRegion = XCreateRegion();
215 XUnionRectWithRegion( &rect, region, newRegion );
216 XDestroyRegion( region );
221 XDestroyImage( image );
223 XShapeCombineRegion( display, Wnd, ShapeBounding, 0, 0, region,
225 XDestroyRegion( region );
230 msg_Err( p_intf, "X11Window::OSShow XShapeCombineRegion() failed");
233 XMapWindow( display, Wnd );
234 XMoveResizeWindow( display, Wnd, Left, Top, Width, Height );
240 XUnmapWindow( display, Wnd );
244 //---------------------------------------------------------------------------
245 bool X11Window::ProcessOSEvent( Event *evt )
247 unsigned int msg = evt->GetMessage();
248 //unsigned int p1 = evt->GetParam1();
249 int p2 = evt->GetParam2();
258 RefreshFromImage( 0, 0, Width, Height );
263 MouseMove( (int)( (XMotionEvent *)p2 )->x,
264 (int)( (XMotionEvent *)p2 )->y, 1 );
265 else if( RButtonDown )
266 MouseMove( (int)( (XMotionEvent *)p2 )->x,
267 (int)( (XMotionEvent *)p2 )->y, 2 );
269 MouseMove( (int)( (XMotionEvent *)p2 )->x,
270 (int)( (XMotionEvent *)p2 )->y, 0 );
275 // Raise all the windows
276 for( list<SkinWindow *>::const_iterator win =
277 p_intf->p_sys->p_theme->WindowList.begin();
278 win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
281 XRaiseWindow( display, ( (X11Window *)(*win) )->GetHandle() );
286 if( ((XButtonEvent *)p2 )->state & ControlMask )
290 if( ((XButtonEvent *)p2 )->state & ShiftMask )
295 switch( ( (XButtonEvent *)p2 )->button )
299 button |= MOUSE_LEFT;
300 time = OSAPI_GetTime();
301 OSAPI_GetMousePos( posX, posY );
302 if( time - ClickedTime < DblClickDelay &&
303 posX == ClickedX && posY == ClickedY )
307 MouseDblClick( (int)( (XButtonEvent *)p2 )->x,
308 (int)( (XButtonEvent *)p2 )->y, button );
316 MouseDown( (int)( (XButtonEvent *)p2 )->x,
317 (int)( (XButtonEvent *)p2 )->y, button );
323 button |= MOUSE_RIGHT;
325 MouseDown( (int)( (XButtonEvent *)p2 )->x,
326 (int)( (XButtonEvent *)p2 )->y, button );
336 if( ((XButtonEvent *)p2 )->state & ControlMask )
340 if( ((XButtonEvent *)p2 )->state & ShiftMask )
345 switch( ( (XButtonEvent *)p2 )->button )
349 button |= MOUSE_LEFT;
351 MouseUp( (int)( (XButtonEvent *)p2 )->x,
352 (int)( (XButtonEvent *)p2 )->y, button );
356 button |= MOUSE_RIGHT;
359 MouseUp( (int)( (XButtonEvent *)p2 )->x,
360 (int)( (XButtonEvent *)p2 )->y, button );
365 MouseScroll( (int)( (XButtonEvent *)p2 )->x,
366 (int)( (XButtonEvent *)p2 )->y,
372 MouseScroll( (int)( (XButtonEvent *)p2 )->x,
373 (int)( (XButtonEvent *)p2 )->y,
383 OSAPI_PostMessage( this, WINDOW_LEAVE, 0, 0 );
388 type = XGetAtomName( display, ( (XClientMessageEvent*)
389 p2 )->message_type );
391 if( type == "XdndEnter" )
393 DropObject->DndEnter( ((XClientMessageEvent*)p2)->data.l );
396 else if( type == "XdndPosition" )
398 DropObject->DndPosition( ((XClientMessageEvent*)p2)->data.l );
401 else if( type == "XdndLeave" )
403 DropObject->DndLeave( ((XClientMessageEvent*)p2)->data.l );
406 else if( type == "XdndDrop" )
408 DropObject->DndDrop( ((XClientMessageEvent*)p2)->data.l );
417 //---------------------------------------------------------------------------
418 void X11Window::SetTransparency( int Value )
422 SetLayeredWindowAttributes( hWnd, 0, Alpha, LWA_ALPHA | LWA_COLORKEY );
423 UpdateWindow( hWnd );*/
425 //---------------------------------------------------------------------------
426 void X11Window::RefreshFromImage( int x, int y, int w, int h )
428 Drawable drawable = (( X11Graphics* )Image )->GetImage();
431 XCopyArea( display, drawable, Wnd, Gc, x, y, w, h, x, y );
435 //---------------------------------------------------------------------------
436 void X11Window::WindowManualMove()
438 // Get mouse cursor position
440 OSAPI_GetMousePos( x, y );
442 // Move window and chek for magnetism
443 p_intf->p_sys->p_theme->MoveSkinMagnet( this,
444 WindowX + x - CursorX, WindowY + y - CursorY );
447 //---------------------------------------------------------------------------
448 void X11Window::WindowManualMoveInit()
450 OSAPI_GetMousePos( CursorX, CursorY );
454 //---------------------------------------------------------------------------
455 void X11Window::Move( int left, int top )
459 XMoveWindow( display, Wnd, left, top );
461 //---------------------------------------------------------------------------
462 void X11Window::Size( int width, int height )
466 XResizeWindow( display, Wnd, width, height );
468 //---------------------------------------------------------------------------
471 bool ToolTipCallback( void *data )
473 int direction, fontAscent, fontDescent;
475 Display *disp = ((tooltip_t*)data)->display;
476 Window win = ((tooltip_t*)data)->window;
477 Font font = ((tooltip_t*)data)->font;
478 GC gc = ((tooltip_t*)data)->gc;
479 string text = ((tooltip_t*)data)->text;
480 int curX = ((tooltip_t*)data)->curX;
481 int curY = ((tooltip_t*)data)->curY;
484 XClearWindow( disp, win );
486 XQueryTextExtents( disp, font, text.c_str(), text.size(), &direction,
487 &fontAscent, &fontDescent, &overall );
488 int w = overall.rbearing - overall.lbearing;
489 int h = overall.ascent + overall.descent;
490 XMapRaised( disp, win );
491 XMoveWindow( disp, win, curX - w/4, curY + 20 );
492 XResizeWindow( disp, win, w+8, h+8 );
493 XDrawString( disp, win, gc, 4, overall.ascent+4, text.c_str(),
496 ((tooltip_t*)data)->active = True;
504 void DrawToolTipText( tooltip_t *tooltip )
506 int direction, fontAscent, fontDescent;
508 Display *disp = tooltip->display;
509 Window win = tooltip->window;
510 Font font = tooltip->font;
512 string text = tooltip->text;
513 int curX = tooltip->curX;
514 int curY = tooltip->curY;
517 XClearWindow( disp, win );
519 XQueryTextExtents( disp, font, text.c_str(), text.size(), &direction,
520 &fontAscent, &fontDescent, &overall );
521 int w = overall.rbearing - overall.lbearing;
522 int h = overall.ascent + overall.descent;
523 XMoveWindow( disp, win, curX - w/4, curY + 20 );
524 XResizeWindow( disp, win, w+8, h+8 );
525 XDrawString( disp, win, gc, 4, overall.ascent+4, text.c_str(),
532 void X11Window::ChangeToolTipText( string text )
536 if( ToolTipText != "none" )
538 ToolTipText = "none";
540 // Hide the tooltip window
541 X11TimerManager *timerManager = X11TimerManager::Instance( p_intf );
542 timerManager->removeTimer( ToolTip.timer );
543 XUnmapWindow( display, ToolTip.window );
544 XResizeWindow( display, ToolTip.window, 1, 1 );
546 ToolTip.active = False;
552 if( text != ToolTipText )
556 if( !ToolTip.active )
558 // Create the tooltip
559 OSAPI_GetMousePos( ToolTip.curX, ToolTip.curY );
560 X11TimerManager *timerManager = X11TimerManager::Instance( p_intf );
561 timerManager->addTimer( ToolTip.timer );
565 // Refresh the tooltip
566 DrawToolTipText( &ToolTip );
571 //---------------------------------------------------------------------------