]> git.sesse.net Git - vlc/blob - modules/gui/skins/x11/x11_window.cpp
891f70fb31368f787bd9f547b3c990cd8bb8fc28
[vlc] / 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.7 2003/05/26 02:09:27 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/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                 default:
235                     break;
236             }
237             return true;
238
239         case LeaveNotify:
240             OSAPI_PostMessage( this, WINDOW_LEAVE, 0, 0 );
241             return true;
242
243 /*        case GDK_2BUTTON_PRESS:
244             MouseDblClick( (int)( (GdkEventButton *)p2 )->x,
245                            (int)( (GdkEventButton *)p2 )->y, 1 );
246             return true;
247
248         case GDK_DROP_START:
249             DropObject->HandleDropStart( ( (GdkEventDND *)p2 )->context );
250             return true;
251
252         case GDK_SCROLL:
253             switch( ( (GdkEventScroll *)p2 )->direction )
254             {
255                 case GDK_SCROLL_UP:
256                     MouseScroll( ( (GdkEventScroll *)p2 )->x,
257                                  ( (GdkEventScroll *)p2 )->y,
258                                  MOUSE_SCROLL_UP);
259                     break;
260                 case GDK_SCROLL_DOWN:
261                     MouseScroll( ( (GdkEventScroll *)p2 )->x,
262                                  ( (GdkEventScroll *)p2 )->y,
263                                  MOUSE_SCROLL_DOWN);
264                     break;
265             }
266             return true;
267 */
268         default:
269             return false;
270     }
271 }
272 //---------------------------------------------------------------------------
273 void X11Window::SetTransparency( int Value )
274 {
275 /*    if( Value > -1 )
276         Alpha = Value;
277     SetLayeredWindowAttributes( hWnd, 0, Alpha, LWA_ALPHA | LWA_COLORKEY );
278     UpdateWindow( hWnd );*/
279 }
280 //---------------------------------------------------------------------------
281 void X11Window::RefreshFromImage( int x, int y, int w, int h )
282 {
283     Drawable drawable = (( X11Graphics* )Image )->GetImage();
284     
285     XCopyArea( display, drawable, Wnd, Gc, x, y, w, h, x, y );
286  
287     XImage *image = XGetImage( display, drawable, 0, 0, Width, Height, 
288                                AllPlanes, ZPixmap );
289  
290     // Mask for transparency
291     Region region = XCreateRegion();
292     for( int line = 0; line < Height; line++ )
293     {
294         int start = 0, end = 0;
295         while( start < Width )
296         {
297             while( start < Width && XGetPixel( image, start, line ) == 0 )
298             {
299                 start++;
300             } 
301             end = start;
302             while( end < Width && XGetPixel( image, end, line ) != 0)
303             {
304                 end++;
305             }
306             XRectangle rect;
307             rect.x = start;
308             rect.y = line;
309             rect.width = end - start + 1;
310             rect.height = 1;
311             Region newRegion = XCreateRegion();
312             XUnionRectWithRegion( &rect, region, newRegion );
313             XDestroyRegion( region );
314             region = newRegion;
315             start = end + 1;
316         }
317     }
318     XShapeCombineRegion( display, Wnd, ShapeBounding, 0, 0, region, ShapeSet );
319     XDestroyRegion( region );
320
321     XSync( display, 0);
322 }
323 //---------------------------------------------------------------------------
324 void X11Window::WindowManualMove()
325 {
326     // Get mouse cursor position
327     int x, y;
328     OSAPI_GetMousePos( x, y );
329
330     // Move window and chek for magnetism
331     p_intf->p_sys->p_theme->MoveSkinMagnet( this,
332         WindowX + x - CursorX, WindowY + y - CursorY );
333
334 }
335 //---------------------------------------------------------------------------
336 void X11Window::WindowManualMoveInit()
337 {
338     OSAPI_GetMousePos( CursorX, CursorY );
339     WindowX = Left;
340     WindowY = Top;
341 }
342 //---------------------------------------------------------------------------
343 void X11Window::Move( int left, int top )
344 {
345     Left = left;
346     Top  = top;
347     XMoveWindow( display, Wnd, left, top );
348 }
349 //---------------------------------------------------------------------------
350 void X11Window::Size( int width, int height )
351 {
352     Width  = width;
353     Height = height;
354     XResizeWindow( display, Wnd, width, height );
355 }
356 //---------------------------------------------------------------------------
357 void X11Window::ChangeToolTipText( string text )
358 {
359 /*    if( text == "none" )
360     {
361         if( ToolTipText != "none" )
362         {
363             ToolTipText = "none";
364             ToolTipInfo.lpszText = NULL;
365             SendMessage( ToolTipWindow, TTM_ACTIVATE, 0 , 0 );
366         }
367     }
368     else
369     {
370         if( text != ToolTipText )
371         {
372             ToolTipText = text;
373             ToolTipInfo.lpszText = (char *)ToolTipText.c_str();
374             SendMessage( ToolTipWindow, TTM_ACTIVATE, 1 , 0 );
375             SendMessage( ToolTipWindow, TTM_UPDATETIPTEXT, 0,
376                              (LPARAM)(LPTOOLINFO)&ToolTipInfo );
377         }
378     }
379 */
380 }
381 //---------------------------------------------------------------------------
382
383 #endif