* first BMP loader for X11 skins : it can only handle 24bbp uncompressed
[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.3 2003/05/18 11:25:00 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
36 //--- SKIN ------------------------------------------------------------------
37 #include "../os_api.h"
38 #include "../src/anchor.h"
39 #include "../controls/generic.h"
40 #include "../src/window.h"
41 #include "../os_window.h"
42 #include "../src/event.h"
43 #include "../os_event.h"
44 #include "../src/graphics.h"
45 #include "../os_graphics.h"
46 #include "../src/skin_common.h"
47 #include "../src/theme.h"
48
49
50 //---------------------------------------------------------------------------
51 // Skinable Window
52 //---------------------------------------------------------------------------
53 X11Window::X11Window( intf_thread_t *p_intf, Window wnd, int x, int y,
54     bool visible, int transition, int normalalpha, int movealpha,
55     bool dragdrop, string name )
56     : SkinWindow( p_intf, x, y, visible, transition, normalalpha, movealpha,
57               dragdrop )
58 {
59     // Set handles
60     Wnd           = wnd;
61
62     display = p_intf->p_sys->display;
63     int screen = DefaultScreen( display );
64
65     Gc = DefaultGC( display, screen );
66
67     Name        = name;
68
69     LButtonDown = false;
70     RButtonDown = false;
71
72     // Removing fading effect
73     Transition  = 0;
74 /*
75     // Set position parameters
76     CursorPos    = new POINT;
77     WindowPos    = new POINT;
78
79     // Create Tool Tip Window
80     ToolTipWindow = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
81         WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
82         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
83         hWnd, 0, GetModuleHandle( NULL ), 0);
84
85     // Create Tool Tip infos
86     ToolTipInfo.cbSize = sizeof(TOOLINFO);
87     ToolTipInfo.uFlags = TTF_SUBCLASS|TTF_IDISHWND;
88     ToolTipInfo.hwnd = hWnd;
89     ToolTipInfo.hinst = GetModuleHandle( NULL );
90     ToolTipInfo.uId = (unsigned int)hWnd;
91     ToolTipInfo.lpszText = NULL;
92     ToolTipInfo.rect.left = ToolTipInfo.rect.top = 0;
93         ToolTipInfo.rect.right = ToolTipInfo.rect.bottom = 0;
94
95     SendMessage( ToolTipWindow, TTM_ADDTOOL, 0,
96                     (LPARAM)(LPTOOLINFO) &ToolTipInfo );
97 */
98     if( DragDrop )
99     {
100         // register the listview as a drop target
101         DropObject = new X11DropObject( p_intf );
102     //    gdk_window_register_dnd( gwnd );
103     }
104
105     // Create Tool Tip window
106 /*    GdkWindowAttr attr;
107     attr.event_mask = GDK_ALL_EVENTS_MASK;
108     attr.width = 100;
109     attr.height = 100;
110     attr.window_type = GDK_WINDOW_CHILD;
111     attr.wclass = GDK_INPUT_OUTPUT;
112     gint mask = 0;
113     ToolTipWindow = gdk_window_new( gwnd, &attr, mask);*/
114
115 }
116 //---------------------------------------------------------------------------
117 X11Window::~X11Window()
118 {
119 /*    delete CursorPos;
120     delete WindowPos;
121
122     if( hWnd != NULL )
123     {
124         DestroyWindow( hWnd );
125     }
126     if( ToolTipWindow != NULL )
127     {
128         DestroyWindow( ToolTipWindow );
129     }
130     if( DragDrop )
131     {
132         // Remove the listview from the list of drop targets
133         RevokeDragDrop( hWnd );
134         DropTarget->Release();
135         // Uninitialize the OLE library
136         OleUninitialize();
137     }*/
138  /*   if( gWnd )
139     {
140         gdk_window_destroy( gWnd );
141     }*/
142 }
143 //---------------------------------------------------------------------------
144 void X11Window::OSShow( bool show )
145 {
146     if( show )
147     {
148 /*        gdk_window_show( gWnd );
149         gdk_window_move( gWnd, Left, Top );*/
150     }
151     else
152     {
153 /*        gdk_window_hide( gWnd );*/
154     }
155 }
156 //---------------------------------------------------------------------------
157 bool X11Window::ProcessOSEvent( Event *evt )
158 {
159     unsigned int msg = evt->GetMessage();
160     unsigned int p1  = evt->GetParam1();
161     int          p2  = evt->GetParam2();
162     
163     switch( msg )
164     {
165         case Expose:
166             RefreshFromImage( 0, 0, Width, Height );
167             return true;
168  
169         case MotionNotify:
170  /*           if( LButtonDown )
171                 MouseMove( (int)( (GdkEventButton *)p2 )->x,
172                            (int)( (GdkEventButton *)p2 )->y, 1 );
173             else if( RButtonDown )
174                 MouseMove( (int)( (GdkEventButton *)p2 )->x,
175                            (int)( (GdkEventButton *)p2 )->y, 2 );
176             else
177                 MouseMove( (int)( (GdkEventButton *)p2 )->x,
178                            (int)( (GdkEventButton *)p2 )->y, 0 );
179             gdk_window_get_pointer( gWnd, 0, 0, 0 );*/
180             return true;
181
182
183         case ButtonPress:
184             // Raise all the windows
185 /*            for( list<SkinWindow *>::const_iterator win = 
186                     p_intf->p_sys->p_theme->WindowList.begin();
187                     win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
188             {
189                 gdk_window_raise( ( (X11Window *)(*win) )->GetHandle() );
190             }
191           
192             switch( ( (GdkEventButton *)p2 )->button )
193             {
194                 case 1:
195                     // Left button
196                     LButtonDown = true;
197                     MouseDown( (int)( (GdkEventButton *)p2 )->x,
198                                (int)( (GdkEventButton *)p2 )->y, 1 );
199                     break;
200
201                 case 3:
202                     // Right button
203                     RButtonDown = true;
204                     MouseDown( (int)( (GdkEventButton *)p2 )->x,
205                                (int)( (GdkEventButton *)p2 )->y, 2 );
206                     break;
207
208                 default:
209                     break;
210             }*/
211             return true;
212
213         case ButtonRelease:
214 /*            switch( ( (GdkEventButton *)p2 )->button )
215             {
216                 case 1:
217                     // Left button
218                     LButtonDown = false;
219                     MouseUp( (int)( (GdkEventButton *)p2 )->x,
220                              (int)( (GdkEventButton *)p2 )->y, 1 );
221                     break;
222
223                 case 3:
224                     // Right button
225                     RButtonDown = false;
226                     MouseUp( (int)( (GdkEventButton *)p2 )->x,
227                              (int)( (GdkEventButton *)p2 )->y, 2 );
228                     break;
229
230                 default:
231                     break;
232             }*/
233             return true;
234
235         case LeaveNotify:
236             OSAPI_PostMessage( this, WINDOW_LEAVE, 0, 0 );
237             return true;
238
239 /*        case GDK_2BUTTON_PRESS:
240             MouseDblClick( (int)( (GdkEventButton *)p2 )->x,
241                            (int)( (GdkEventButton *)p2 )->y, 1 );
242             return true;
243
244         case GDK_DROP_START:
245             DropObject->HandleDropStart( ( (GdkEventDND *)p2 )->context );
246             return true;
247
248         case GDK_SCROLL:
249             switch( ( (GdkEventScroll *)p2 )->direction )
250             {
251                 case GDK_SCROLL_UP:
252                     MouseScroll( ( (GdkEventScroll *)p2 )->x,
253                                  ( (GdkEventScroll *)p2 )->y,
254                                  MOUSE_SCROLL_UP);
255                     break;
256                 case GDK_SCROLL_DOWN:
257                     MouseScroll( ( (GdkEventScroll *)p2 )->x,
258                                  ( (GdkEventScroll *)p2 )->y,
259                                  MOUSE_SCROLL_DOWN);
260                     break;
261             }
262             return true;
263 */
264         default:
265             return false;
266     }
267 }
268 //---------------------------------------------------------------------------
269 void X11Window::SetTransparency( int Value )
270 {
271 /*    if( Value > -1 )
272         Alpha = Value;
273     SetLayeredWindowAttributes( hWnd, 0, Alpha, LWA_ALPHA | LWA_COLORKEY );
274     UpdateWindow( hWnd );*/
275 }
276 //---------------------------------------------------------------------------
277 void X11Window::RefreshFromImage( int x, int y, int w, int h )
278 {
279     // Initialize painting
280 /*    HDC DC = GetWindowDC( hWnd );
281
282     // Draw image on window
283     BitBlt( DC, x, y, w, h, ( (X11Graphics *)Image )->GetImageHandle(),
284             x, y, SRCCOPY );
285
286     // Release window device context
287     ReleaseDC( hWnd, DC );
288
289 */ 
290     Drawable drawable = (( X11Graphics* )Image )->GetImage();
291     
292     XCopyArea( display, drawable, Wnd, Gc, x, y, w, h, x, y );
293     XSync( display, 0);
294 /*
295     // Mask for transparency
296     GdkRegion *region = gdk_region_new();
297     for( int line = 0; line < Height; line++ )
298     {
299         int start = 0, end = 0;
300         while( start < Width )
301         {
302             while( start < Width && gdk_image_get_pixel( image, start, line ) == 0 )
303             {
304                 start++;
305             } 
306             end = start;
307             while( end < Width && gdk_image_get_pixel( image, end, line ) != 0)
308             {
309                 end++;
310             }
311             GdkRectangle rect;
312             rect.x = start;
313             rect.y = line;
314             rect.width = end - start + 1;
315             rect.height = 1;
316             GdkRegion *rectReg = gdk_region_rectangle( &rect );
317             gdk_region_union( region, rectReg );
318             gdk_region_destroy( rectReg );
319             start = end + 1;
320         }
321     }
322     gdk_window_shape_combine_region( gWnd, region, 0, 0 );
323     gdk_region_destroy( region );*/
324 }
325 //---------------------------------------------------------------------------
326 void X11Window::WindowManualMove()
327 {
328     // Get mouse cursor position
329     int x, y;
330     OSAPI_GetMousePos( x, y );
331
332     // Move window and chek for magnetism
333     p_intf->p_sys->p_theme->MoveSkinMagnet( this,
334         WindowX + x - CursorX, WindowY + y - CursorY );
335
336 }
337 //---------------------------------------------------------------------------
338 void X11Window::WindowManualMoveInit()
339 {
340 /*    gdk_window_get_pointer( gdk_get_default_root_window(), &CursorX, &CursorY,
341                             NULL );*/
342     WindowX = Left;
343     WindowY = Top;
344 }
345 //---------------------------------------------------------------------------
346 void X11Window::Move( int left, int top )
347 {
348     Left = left;
349     Top  = top;
350 /*   gdk_window_move( gWnd, left, top );*/
351 }
352 //---------------------------------------------------------------------------
353 void X11Window::Size( int width, int height )
354 {
355     Width  = width;
356     Height = height;
357 /*    gdk_window_resize( gWnd, width, height );*/
358 }
359 //---------------------------------------------------------------------------
360 void X11Window::ChangeToolTipText( string text )
361 {
362 /*    if( text == "none" )
363     {
364         if( ToolTipText != "none" )
365         {
366             ToolTipText = "none";
367             ToolTipInfo.lpszText = NULL;
368             SendMessage( ToolTipWindow, TTM_ACTIVATE, 0 , 0 );
369         }
370     }
371     else
372     {
373         if( text != ToolTipText )
374         {
375             ToolTipText = text;
376             ToolTipInfo.lpszText = (char *)ToolTipText.c_str();
377             SendMessage( ToolTipWindow, TTM_ACTIVATE, 1 , 0 );
378             SendMessage( ToolTipWindow, TTM_UPDATETIPTEXT, 0,
379                              (LPARAM)(LPTOOLINFO)&ToolTipInfo );
380         }
381     }
382 */
383 }
384 //---------------------------------------------------------------------------
385
386 #endif