]> git.sesse.net Git - vlc/blob - modules/gui/skins/gtk2/gtk2_bitmap.cpp
* GTK2 events work even better
[vlc] / modules / gui / skins / gtk2 / gtk2_bitmap.cpp
1 /*****************************************************************************
2  * gtk2_bitmap.cpp: GTK2 implementation of the Bitmap class
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: gtk2_bitmap.cpp,v 1.9 2003/04/15 20:33:58 karibu 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 #if !defined WIN32
26
27 //--- GTK2 -----------------------------------------------------------------
28 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <gdk/gdk.h>
30
31 //--- VLC -------------------------------------------------------------------
32 #include <vlc/intf.h>
33
34 //--- SKIN ------------------------------------------------------------------
35 #include "os_api.h"
36 #include "graphics.h"
37 #include "gtk2_graphics.h"
38 #include "bitmap.h"
39 #include "gtk2_bitmap.h"
40 #include "skin_common.h"
41
42
43 //---------------------------------------------------------------------------
44 //   GTK2Bitmap
45 //---------------------------------------------------------------------------
46 GTK2Bitmap::GTK2Bitmap( intf_thread_t *p_intf, string FileName, int AColor )
47     : Bitmap( p_intf, FileName, AColor )
48 {
49 /*    HBITMAP HBitmap;
50     HBITMAP HBuf;
51     BITMAP  Bmp;
52     HDC     bufDC;
53     AlphaColor = AColor;
54
55     // Create image from file if it exists
56     HBitmap = (HBITMAP) LoadImage( NULL, FileName.c_str(), IMAGE_BITMAP,
57                                    0, 0, LR_LOADFROMFILE );
58     if( HBitmap == NULL )
59     {
60         if( FileName != "" )
61             msg_Warn( p_intf, "Couldn't load bitmap: %s", FileName.c_str() );
62
63         HBitmap = CreateBitmap( 0, 0, 1, 32, NULL );
64     }
65
66     // Create device context
67     bmpDC   = CreateCompatibleDC( NULL );
68     SelectObject( bmpDC, HBitmap );
69
70     // Get size of image
71     GetObject( HBitmap, sizeof( Bmp ), &Bmp );
72     Width  = Bmp.bmWidth;
73     Height = Bmp.bmHeight;
74
75     // If alpha color is not 0, then change 0 colors to non black color to avoid
76     // window transparency
77     if( (int)AlphaColor != OSAPI_GetNonTransparentColor( 0 ) )
78     {
79         bufDC = CreateCompatibleDC( bmpDC );
80         HBuf = CreateCompatibleBitmap( bmpDC, Width, Height );
81         SelectObject( bufDC, HBuf );
82
83         LPRECT r = new RECT;
84         HBRUSH Brush = CreateSolidBrush( OSAPI_GetNonTransparentColor( 0 ) );
85         r->left   = 0;
86         r->top    = 0;
87         r->right  = Width;
88         r->bottom = Height;
89         FillRect( bufDC, r, Brush );
90         DeleteObject( Brush );
91         delete r;
92
93         TransparentBlt( bufDC, 0, 0, Width, Height, bmpDC, 0, 0, Width, Height, 0 );
94         BitBlt( bmpDC, 0, 0, Width, Height, bufDC, 0, 0, SRCCOPY );
95         DeleteDC( bufDC );
96         DeleteObject( HBuf );
97     }
98
99     // Delete objects
100     DeleteObject( HBitmap );*/
101
102     // Load the bitmap image
103     Bmp = gdk_pixbuf_new_from_file( FileName.c_str(), NULL );
104     if( Bmp == NULL )
105     {
106         if( FileName != "" )
107             msg_Warn( p_intf, "Couldn't load bitmap: %s", FileName.c_str() );
108
109      //   Bmp = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
110         Bmp = NULL;
111     }
112
113     Width = gdk_pixbuf_get_width( Bmp );
114     Height = gdk_pixbuf_get_height( Bmp );
115 }
116 //---------------------------------------------------------------------------
117 GTK2Bitmap::GTK2Bitmap( intf_thread_t *p_intf, Graphics *from, int x, int y,
118     int w, int h, int AColor ) : Bitmap( p_intf, from, x, y, w, h, AColor )
119 {
120 /*    Width  = w;
121     Height = h;
122     AlphaColor = AColor;
123     HBITMAP HBmp;
124     HDC fromDC = ( (GTK2Graphics *)from )->GetImageHandle();
125
126     // Create image
127     bmpDC = CreateCompatibleDC( fromDC );
128     HBmp  = CreateCompatibleBitmap( fromDC, Width, Height );
129     SelectObject( bmpDC, HBmp );
130     DeleteObject( HBmp );
131     BitBlt( bmpDC, 0, 0, Width, Height, fromDC, x, y, SRCCOPY );*/
132 }
133 //---------------------------------------------------------------------------
134 GTK2Bitmap::GTK2Bitmap( intf_thread_t *p_intf, Bitmap *c )
135     : Bitmap( p_intf, c )
136 {
137 /*    HBITMAP HBuf;
138
139     // Copy attibutes
140     c->GetSize( Width, Height );
141     AlphaColor = c->GetAlphaColor();
142
143     // Copy bmpDC
144     bmpDC = CreateCompatibleDC( NULL );
145     HBuf  = CreateCompatibleBitmap( bmpDC, Width, Height );
146     SelectObject( bmpDC, HBuf );
147
148     BitBlt( bmpDC, 0, 0, Width, Height, ( (GTK2Bitmap *)c )->GetBmpDC(),
149             0, 0, SRCCOPY );
150     DeleteObject( HBuf );*/
151 }
152 //---------------------------------------------------------------------------
153 GTK2Bitmap::~GTK2Bitmap()
154 {
155 /*    DeleteDC( bmpDC );*/
156 }
157 //---------------------------------------------------------------------------
158 void GTK2Bitmap::DrawBitmap( int x, int y, int w, int h, int xRef, int yRef,
159                               Graphics *dest )
160 {
161 /*    HDC destDC = ( (GTK2Graphics *)dest )->GetImageHandle();
162
163     // New method, not available in win95
164     TransparentBlt( destDC, xRef, yRef, w, h, bmpDC, x, y, w, h, AlphaColor );
165 */
166     GdkDrawable *destImg = ( (GTK2Graphics *)dest )->GetImage();
167     GdkGC *destGC = ( (GTK2Graphics *)dest )->GetGC();
168
169     gdk_pixbuf_render_to_drawable( Bmp, destImg, destGC, x, y, xRef, yRef, 
170             w, h, GDK_RGB_DITHER_NONE, 0, 0);
171 }
172 //---------------------------------------------------------------------------
173 bool GTK2Bitmap::Hit( int x, int y)
174 {
175     if( x < 0 || x >= Width || y < 0 || y >= Height )
176         return false;
177
178     guchar *pixels;
179     int rowstride, offset;
180     gboolean has_alpha;
181
182     rowstride = gdk_pixbuf_get_rowstride( Bmp );
183     pixels    = gdk_pixbuf_get_pixels( Bmp );
184     has_alpha = gdk_pixbuf_get_has_alpha( Bmp );
185
186     offset = y * rowstride + ( x * (has_alpha ? 4 : 3) );
187
188     int r = pixels [offset];
189     int g = pixels [offset + 1];
190     int b = pixels [offset + 2];
191     unsigned int c = r + g * 256 + b * 65536;
192     /* If has_alpha == TRUE, then the alpha component is in
193        pixels [offset + 3] */
194
195     if( c == AlphaColor )
196         return false;
197     else
198         return true;
199 }
200 //---------------------------------------------------------------------------
201 int GTK2Bitmap::GetBmpPixel( int x, int y )
202 {
203 //    return GetPixel( bmpDC, x, y );
204 }
205 //---------------------------------------------------------------------------
206 void GTK2Bitmap::SetBmpPixel( int x, int y, int color )
207 {
208 //    SetPixelV( bmpDC, x, y, color );
209 }
210 //---------------------------------------------------------------------------
211
212 #endif