]> git.sesse.net Git - vlc/blob - modules/gui/skins/x11/x11_bitmap.cpp
* x11_timer.cpp: compilation fix
[vlc] / modules / gui / skins / x11 / x11_bitmap.cpp
1 /*****************************************************************************
2  * x11_bitmap.cpp: X11 implementation of the Bitmap class
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: x11_bitmap.cpp,v 1.11 2003/06/09 12:33:16 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 //--- X11 -------------------------------------------------------------------
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30 #include <Imlib2.h>
31
32 //--- VLC -------------------------------------------------------------------
33 #include <vlc/intf.h>
34
35 //--- SKIN ------------------------------------------------------------------
36 #include "../os_api.h"
37 #include "../src/graphics.h"
38 #include "x11_graphics.h"
39 #include "../src/bitmap.h"
40 #include "x11_bitmap.h"
41 #include "../src/theme.h"
42 #include "../os_theme.h"
43 #include "../src/skin_common.h"
44
45 #include <stdio.h>
46
47 // macros to read little endian numbers
48 #define U16( p ) ( ((uint8_t*)(p))[0] | ((uint8_t*)(p))[1] << 8 )
49 #define U32( p ) ( U16( p ) | ((uint8_t*)(p))[2] << 16 | ((uint8_t*)(p))[3] << 24 )
50
51 //---------------------------------------------------------------------------
52 //   X11Bitmap
53 //---------------------------------------------------------------------------
54 X11Bitmap::X11Bitmap( intf_thread_t *_p_intf, string FileName, int AColor )
55     : Bitmap( p_intf, FileName, AColor )
56 {
57     p_intf = _p_intf;
58
59     // Find the display
60     display = p_intf->p_sys->display;
61     int screen = DefaultScreen( display );
62     Screen *screenptr = DefaultScreenOfDisplay( display );
63     Visual *visual = DefaultVisualOfScreen( screenptr );
64     Img = NULL;
65     Width = 0;
66     Height = 0;
67
68     if( FileName == "" )
69     {
70         return;
71     }
72
73     AlphaColor = (AColor & 0xff) << 16 | (AColor & 0xff00) | 
74                  (AColor & 0xff0000) >> 16;
75
76     XLOCK;
77     imlib_context_set_display( display );
78     imlib_context_set_visual( visual );
79     imlib_context_set_colormap( DefaultColormap( display, screen ) );
80     imlib_context_set_dither( 1 );
81     imlib_context_set_blend( 1 );
82
83     Img = imlib_load_image_immediately( FileName.c_str() );
84     imlib_context_set_image( Img );
85     Width = imlib_image_get_width();
86     Height = imlib_image_get_height();
87
88     // Add an alpha layer
89     DATA32 *data = imlib_image_get_data();
90     DATA32 *ptr = data;
91     for( int j = 0; j < Height; j++)
92     {
93         for( int i = 0; i < Width; i++)
94         {
95             if( AlphaColor != 0 && *ptr == 0xff000000 )
96             {
97                 // Avoid transparency for black pixels
98                 *ptr = 0xff00000c;
99             }
100             else if( (*ptr & 0xffffff) == AlphaColor )
101             {
102                 *ptr &= 0x00ffffff;
103             }
104             ptr++;
105         }
106     }
107     imlib_image_set_has_alpha( 1 );
108     imlib_image_set_irrelevant_alpha( 0 );
109     imlib_image_put_back_data( data );
110     XUNLOCK;
111 }
112 //---------------------------------------------------------------------------
113 X11Bitmap::X11Bitmap( intf_thread_t *_p_intf, Graphics *from, int x, int y,
114     int w, int h, int AColor ) : Bitmap( p_intf, from, x, y, w, h, AColor )
115 {
116     p_intf = _p_intf;
117 /*    Width  = w;
118     Height = h;
119     AlphaColor = AColor;
120     HBITMAP HBmp;
121     HDC fromDC = ( (X11Graphics *)from )->GetImageHandle();
122
123     // Create image
124     bmpDC = CreateCompatibleDC( fromDC );
125     HBmp  = CreateCompatibleBitmap( fromDC, Width, Height );
126     SelectObject( bmpDC, HBmp );
127     DeleteObject( HBmp );
128     BitBlt( bmpDC, 0, 0, Width, Height, fromDC, x, y, SRCCOPY );*/
129 }
130 //---------------------------------------------------------------------------
131 X11Bitmap::X11Bitmap( intf_thread_t *_p_intf, Bitmap *c )
132     : Bitmap( p_intf, c )
133 {
134     p_intf = _p_intf;
135 /*    HBITMAP HBuf;
136
137     // Copy attibutes
138     c->GetSize( Width, Height );
139     AlphaColor = c->GetAlphaColor();
140
141     // Copy bmpDC
142     bmpDC = CreateCompatibleDC( NULL );
143     HBuf  = CreateCompatibleBitmap( bmpDC, Width, Height );
144     SelectObject( bmpDC, HBuf );
145
146     BitBlt( bmpDC, 0, 0, Width, Height, ( (X11Bitmap *)c )->GetBmpDC(),
147             0, 0, SRCCOPY );
148     DeleteObject( HBuf );*/
149 }
150 //---------------------------------------------------------------------------
151 X11Bitmap::~X11Bitmap()
152 {
153     if( Img )
154     {
155         XLOCK;
156         imlib_context_set_image( Img );
157         imlib_free_image();
158         XUNLOCK;
159     }
160 }
161 //---------------------------------------------------------------------------
162 void X11Bitmap::DrawBitmap( int x, int y, int w, int h, int xRef, int yRef,
163                               Graphics *dest )
164 {
165     if( Img )
166     {
167         XLOCK;
168         Drawable destImg = ( (X11Graphics *)dest )->GetImage();
169         imlib_context_set_image( Img );
170         imlib_context_set_drawable( destImg );
171         imlib_render_image_part_on_drawable_at_size( x, y, w, h, xRef, yRef, w, h );
172         XUNLOCK;
173     }
174 }
175 //---------------------------------------------------------------------------
176 bool X11Bitmap::Hit( int x, int y)
177 {
178     int c = GetBmpPixel( x, y );
179
180     if( c == -1 || (unsigned int)c == AlphaColor )
181         return false;
182     else
183         return true;
184 }
185 //---------------------------------------------------------------------------
186 int X11Bitmap::GetBmpPixel( int x, int y )
187 {
188     if( !Img || x < 0 || x >= Width || y < 0 || y >= Height )
189         return -1;
190
191     return 42;
192 /*    guchar *pixels;
193     int rowstride, offset;
194     gboolean has_alpha;
195
196     rowstride = gdk_pixbuf_get_rowstride( Bmp );
197     pixels    = gdk_pixbuf_get_pixels( Bmp ); 
198     has_alpha = gdk_pixbuf_get_has_alpha( Bmp );
199
200     offset = y * rowstride + ( x * (has_alpha ? 4 : 3) );
201
202     int r = pixels [offset];
203     int g = pixels [offset + 1] << 8;
204     int b = pixels [offset + 2] << 16;
205
206     return r + g + b;*/
207 }
208 //---------------------------------------------------------------------------
209 void X11Bitmap::SetBmpPixel( int x, int y, int color )
210 {
211 //    SetPixelV( bmpDC, x, y, color );
212 }
213 //---------------------------------------------------------------------------
214
215 #endif