1 /*****************************************************************************
2 * x11_window.cpp: X11 implementation of the Window class
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: x11_window.cpp,v 1.29 2003/10/19 22:25:10 gbazin 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::ToggleOnTop()
178 ev.type = ClientMessage;
179 ev.xclient.display = display;
180 ev.xclient.window = Wnd;
181 ev.xclient.serial = 0;
182 ev.xclient.send_event = True;
183 ev.xclient.message_type = XInternAtom( display, "_NET_WM_STATE", True );
184 ev.xclient.format = 32;
185 ev.xclient.data.l[1] = XInternAtom( display, "_NET_WM_STATE_ABOVE", False);
186 ev.xclient.data.l[2] = 0;
187 ev.xclient.data.l[3] = 0;
188 ev.xclient.data.l[4] = 0;
190 if( !p_intf->p_sys->b_on_top )
192 // Set the window on top
193 ev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
197 // Set the window not on top
198 ev.xclient.data.l[0] = 0; // _NET_WM_STATE_REMOVE
202 XSendEvent( display, DefaultRootWindow( display ), False,
203 SubstructureRedirectMask | SubstructureNotifyMask, &ev );
207 ev.xclient.data.l[1] = XInternAtom( display, "_NET_WM_STATE_STAYS_ON_TOP",
210 XSendEvent( display, DefaultRootWindow( display ), False,
211 SubstructureRedirectMask | SubstructureNotifyMask, &ev );
214 //---------------------------------------------------------------------------
215 void X11Window::OSShow( bool show )
218 XResizeWindow( display, Wnd, 1, 1 ); // Avoid flicker
223 // We do the call to XShapeCombineRegion() here because the window
224 // must be unmapped for this to work.
225 Drawable drawable = (( X11Graphics* )Image )->GetImage();
228 XImage *image = XGetImage( display, drawable, 0, 0, Width, Height,
229 AllPlanes, ZPixmap );
232 // Mask for transparency
233 Region region = XCreateRegion();
234 for( int line = 0; line < Height; line++ )
236 int start = 0, end = 0;
237 while( start < Width )
239 while( start < Width && XGetPixel( image, start, line )
245 while( end < Width && XGetPixel( image, end, line ) != 0)
252 rect.width = end - start + 1;
254 Region newRegion = XCreateRegion();
255 XUnionRectWithRegion( &rect, region, newRegion );
256 XDestroyRegion( region );
261 XDestroyImage( image );
263 XShapeCombineRegion( display, Wnd, ShapeBounding, 0, 0, region,
265 XDestroyRegion( region );
270 msg_Err( p_intf, "X11Window::OSShow XShapeCombineRegion() failed");
273 XMapWindow( display, Wnd );
274 XMoveResizeWindow( display, Wnd, Left, Top, Width, Height );
280 XUnmapWindow( display, Wnd );
284 //---------------------------------------------------------------------------
285 bool X11Window::ProcessOSEvent( Event *evt )
287 unsigned int msg = evt->GetMessage();
288 //unsigned int p1 = evt->GetParam1();
289 int p2 = evt->GetParam2();
298 RefreshFromImage( 0, 0, Width, Height );
303 MouseMove( (int)( (XMotionEvent *)p2 )->x,
304 (int)( (XMotionEvent *)p2 )->y, 1 );
305 else if( RButtonDown )
306 MouseMove( (int)( (XMotionEvent *)p2 )->x,
307 (int)( (XMotionEvent *)p2 )->y, 2 );
309 MouseMove( (int)( (XMotionEvent *)p2 )->x,
310 (int)( (XMotionEvent *)p2 )->y, 0 );
315 // Raise all the windows
316 for( list<SkinWindow *>::const_iterator win =
317 p_intf->p_sys->p_theme->WindowList.begin();
318 win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
320 Window id = ( (X11Window *)(*win) )->GetHandle();
321 // the current window is raised last
325 XRaiseWindow( display, id );
330 XRaiseWindow( display, Wnd );
334 if( ((XButtonEvent *)p2 )->state & ControlMask )
338 if( ((XButtonEvent *)p2 )->state & ShiftMask )
343 switch( ( (XButtonEvent *)p2 )->button )
347 button |= MOUSE_LEFT;
348 time = OSAPI_GetTime();
349 OSAPI_GetMousePos( posX, posY );
350 if( time - ClickedTime < DblClickDelay &&
351 posX == ClickedX && posY == ClickedY )
355 MouseDblClick( (int)( (XButtonEvent *)p2 )->x,
356 (int)( (XButtonEvent *)p2 )->y, button );
364 MouseDown( (int)( (XButtonEvent *)p2 )->x,
365 (int)( (XButtonEvent *)p2 )->y, button );
371 button |= MOUSE_RIGHT;
373 MouseDown( (int)( (XButtonEvent *)p2 )->x,
374 (int)( (XButtonEvent *)p2 )->y, button );
384 if( ((XButtonEvent *)p2 )->state & ControlMask )
388 if( ((XButtonEvent *)p2 )->state & ShiftMask )
393 switch( ( (XButtonEvent *)p2 )->button )
397 button |= MOUSE_LEFT;
399 MouseUp( (int)( (XButtonEvent *)p2 )->x,
400 (int)( (XButtonEvent *)p2 )->y, button );
404 button |= MOUSE_RIGHT;
407 MouseUp( (int)( (XButtonEvent *)p2 )->x,
408 (int)( (XButtonEvent *)p2 )->y, button );
413 MouseScroll( (int)( (XButtonEvent *)p2 )->x,
414 (int)( (XButtonEvent *)p2 )->y,
420 MouseScroll( (int)( (XButtonEvent *)p2 )->x,
421 (int)( (XButtonEvent *)p2 )->y,
431 OSAPI_PostMessage( this, WINDOW_LEAVE, 0, 0 );
436 type = XGetAtomName( display, ( (XClientMessageEvent*)
437 p2 )->message_type );
439 if( type == "XdndEnter" )
441 DropObject->DndEnter( ((XClientMessageEvent*)p2)->data.l );
444 else if( type == "XdndPosition" )
446 DropObject->DndPosition( ((XClientMessageEvent*)p2)->data.l );
449 else if( type == "XdndLeave" )
451 DropObject->DndLeave( ((XClientMessageEvent*)p2)->data.l );
454 else if( type == "XdndDrop" )
456 DropObject->DndDrop( ((XClientMessageEvent*)p2)->data.l );
465 //---------------------------------------------------------------------------
466 void X11Window::SetTransparency( int Value )
470 SetLayeredWindowAttributes( hWnd, 0, Alpha, LWA_ALPHA | LWA_COLORKEY );
471 UpdateWindow( hWnd );*/
473 //---------------------------------------------------------------------------
474 void X11Window::RefreshFromImage( int x, int y, int w, int h )
476 Drawable drawable = (( X11Graphics* )Image )->GetImage();
479 XCopyArea( display, drawable, Wnd, Gc, x, y, w, h, x, y );
483 //---------------------------------------------------------------------------
484 void X11Window::WindowManualMove()
486 // Get mouse cursor position
488 OSAPI_GetMousePos( x, y );
490 // Move window and chek for magnetism
491 p_intf->p_sys->p_theme->MoveSkinMagnet( this,
492 WindowX + x - CursorX, WindowY + y - CursorY );
495 //---------------------------------------------------------------------------
496 void X11Window::WindowManualMoveInit()
498 OSAPI_GetMousePos( CursorX, CursorY );
502 //---------------------------------------------------------------------------
503 void X11Window::Move( int left, int top )
507 XMoveWindow( display, Wnd, left, top );
509 //---------------------------------------------------------------------------
510 void X11Window::Size( int width, int height )
514 XResizeWindow( display, Wnd, width, height );
516 //---------------------------------------------------------------------------
519 bool ToolTipCallback( void *data )
521 int direction, fontAscent, fontDescent;
523 Display *disp = ((tooltip_t*)data)->display;
524 Window win = ((tooltip_t*)data)->window;
525 Font font = ((tooltip_t*)data)->font;
526 GC gc = ((tooltip_t*)data)->gc;
527 string text = ((tooltip_t*)data)->text;
528 int curX = ((tooltip_t*)data)->curX;
529 int curY = ((tooltip_t*)data)->curY;
532 XClearWindow( disp, win );
534 XQueryTextExtents( disp, font, text.c_str(), text.size(), &direction,
535 &fontAscent, &fontDescent, &overall );
536 int w = overall.rbearing - overall.lbearing;
537 int h = overall.ascent + overall.descent;
538 XMapRaised( disp, win );
539 XMoveWindow( disp, win, curX - w/4, curY + 20 );
540 XResizeWindow( disp, win, w+8, h+8 );
541 XDrawString( disp, win, gc, 4, overall.ascent+4, text.c_str(),
544 ((tooltip_t*)data)->active = True;
552 void DrawToolTipText( tooltip_t *tooltip )
554 int direction, fontAscent, fontDescent;
556 Display *disp = tooltip->display;
557 Window win = tooltip->window;
558 Font font = tooltip->font;
560 string text = tooltip->text;
561 int curX = tooltip->curX;
562 int curY = tooltip->curY;
565 XClearWindow( disp, win );
567 XQueryTextExtents( disp, font, text.c_str(), text.size(), &direction,
568 &fontAscent, &fontDescent, &overall );
569 int w = overall.rbearing - overall.lbearing;
570 int h = overall.ascent + overall.descent;
571 XMoveWindow( disp, win, curX - w/4, curY + 20 );
572 XResizeWindow( disp, win, w+8, h+8 );
573 XDrawString( disp, win, gc, 4, overall.ascent+4, text.c_str(),
580 void X11Window::ChangeToolTipText( string text )
584 if( ToolTipText != "none" )
586 ToolTipText = "none";
588 // Hide the tooltip window
589 X11TimerManager *timerManager = X11TimerManager::Instance( p_intf );
590 timerManager->removeTimer( ToolTip.timer );
591 XUnmapWindow( display, ToolTip.window );
592 XResizeWindow( display, ToolTip.window, 1, 1 );
594 ToolTip.active = False;
600 if( text != ToolTipText )
604 if( !ToolTip.active )
606 // Create the tooltip
607 OSAPI_GetMousePos( ToolTip.curX, ToolTip.curY );
608 X11TimerManager *timerManager = X11TimerManager::Instance( p_intf );
609 timerManager->addTimer( ToolTip.timer );
613 // Refresh the tooltip
614 DrawToolTipText( &ToolTip );
619 //---------------------------------------------------------------------------