]> git.saurik.com Git - wxWidgets.git/blame - src/msw/statbmp.cpp
fixed wxImage->wxBitmap conversion for images with alpha channel
[wxWidgets.git] / src / msw / statbmp.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: statbmp.cpp
3// Purpose: wxStaticBitmap
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
9e3e0821
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
9e3e0821 21 #pragma implementation "statbmp.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
9e3e0821 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_STATBMP
32
0c589ad0
BM
33#include "wx/window.h"
34#include "wx/msw/private.h"
35
2bda0e17 36#ifndef WX_PRECOMP
0c589ad0 37 #include "wx/icon.h"
9e3e0821 38 #include "wx/statbmp.h"
2bda0e17
KB
39#endif
40
41#include <stdio.h>
2bda0e17 42
9e3e0821
VZ
43// ---------------------------------------------------------------------------
44// macors
45// ---------------------------------------------------------------------------
46
bc9fb572
JS
47#if wxUSE_EXTENDED_RTTI
48WX_DEFINE_FLAGS( wxStaticBitmapStyle )
49
3ff066a4 50wxBEGIN_FLAGS( wxStaticBitmapStyle )
bc9fb572
JS
51 // new style border flags, we put them first to
52 // use them for streaming out
3ff066a4
SC
53 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
54 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
55 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
56 wxFLAGS_MEMBER(wxBORDER_RAISED)
57 wxFLAGS_MEMBER(wxBORDER_STATIC)
58 wxFLAGS_MEMBER(wxBORDER_NONE)
bc9fb572
JS
59
60 // old style border flags
3ff066a4
SC
61 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
62 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
63 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
64 wxFLAGS_MEMBER(wxRAISED_BORDER)
65 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 66 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
67
68 // standard window styles
3ff066a4
SC
69 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
70 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
71 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
72 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 73 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
74 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
75 wxFLAGS_MEMBER(wxVSCROLL)
76 wxFLAGS_MEMBER(wxHSCROLL)
bc9fb572 77
3ff066a4 78wxEND_FLAGS( wxStaticBitmapStyle )
bc9fb572
JS
79
80IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBitmap, wxControl,"wx/statbmp.h")
81
3ff066a4 82wxBEGIN_PROPERTIES_TABLE(wxStaticBitmap)
af498247 83 wxPROPERTY_FLAGS( WindowStyle , wxStaticBitmapStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 84wxEND_PROPERTIES_TABLE()
bc9fb572 85
3ff066a4
SC
86wxBEGIN_HANDLERS_TABLE(wxStaticBitmap)
87wxEND_HANDLERS_TABLE()
bc9fb572 88
3ff066a4 89wxCONSTRUCTOR_5( wxStaticBitmap, wxWindow* , Parent , wxWindowID , Id , wxBitmap, Bitmap, wxPoint , Position , wxSize , Size )
bc9fb572
JS
90
91#else
4004f41e 92IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
bc9fb572 93#endif
2bda0e17 94
066f1b7a
SC
95/*
96 TODO PROPERTIES :
97 bitmap
98*/
99
9e3e0821
VZ
100// ===========================================================================
101// implementation
102// ===========================================================================
103
104// ---------------------------------------------------------------------------
105// wxStaticBitmap
106// ---------------------------------------------------------------------------
2bda0e17 107
d8bffc13
MB
108// we may have either bitmap or icon: if a bitmap with mask is passed, we
109// will transform it to an icon ourselves because otherwise the mask will
110// be ignored by Windows
111// note that this function will create a new object every time
112// it is called even if the image needs no conversion
113
d8bffc13
MB
114static wxGDIImage* ConvertImage( const wxGDIImage& bitmap )
115{
116 bool isIcon = bitmap.IsKindOf( CLASSINFO(wxIcon) );
117
118 if( !isIcon )
119 {
120 wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap),
121 _T("not an icon and not a bitmap?") );
122
123 const wxBitmap& bmp = (const wxBitmap&)bitmap;
124 wxMask *mask = bmp.GetMask();
125 if ( mask && mask->GetMaskBitmap() )
126 {
127 wxIcon* icon = new wxIcon;
128 icon->CopyFromBitmap(bmp);
129
130 return icon;
131 }
132
133 return new wxBitmap( bmp );
134 }
135
136 // copying a bitmap is a cheap operation
137 return new wxIcon( (const wxIcon&)bitmap );
138}
139
46a5e01e
VZ
140bool wxStaticBitmap::Create(wxWindow *parent,
141 wxWindowID id,
0d0512bd 142 const wxGDIImage& bitmap,
9e3e0821
VZ
143 const wxPoint& pos,
144 const wxSize& size,
145 long style,
146 const wxString& name)
2bda0e17 147{
46a5e01e
VZ
148 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
149 return FALSE;
9e3e0821 150
4004f41e
VZ
151 // we may have either bitmap or icon: if a bitmap with mask is passed, we
152 // will transform it to an icon ourselves because otherwise the mask will
153 // be ignored by Windows
9e3e0821 154 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
669f7a11 155
5cb598ae 156 wxGDIImage *image = ConvertImage( bitmap );
d8bffc13 157 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
9e3e0821 158
46a5e01e 159 // create the native control
3a5bcc4d 160 if ( !MSWCreateControl(_T("STATIC"), wxEmptyString, pos, size) )
46a5e01e
VZ
161 {
162 // control creation failed
3a5bcc4d 163 return false;
46a5e01e 164 }
9e3e0821 165
d8bffc13 166 // no need to delete the new image
46a5e01e 167 SetImageNoCopy(image);
9e3e0821 168
9d17ee60
RD
169 // GetBestSize will work properly now, so set the best size if needed
170 SetBestSize(size);
171
46a5e01e
VZ
172 return TRUE;
173}
9e3e0821 174
65bc172c
VZ
175wxBorder wxStaticBitmap::GetDefaultBorder() const
176{
177 return wxBORDER_NONE;
178}
179
46a5e01e
VZ
180WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const
181{
182 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
9e3e0821 183
46a5e01e
VZ
184 // what kind of control are we?
185 msStyle |= m_isIcon ? SS_ICON : SS_BITMAP;
9e3e0821 186
46a5e01e
VZ
187 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
188 // fit to its size -- this is unexpected and doesn't happen in other ports
189 msStyle |= SS_CENTERIMAGE;
46a5e01e
VZ
190
191 return msStyle;
9e3e0821
VZ
192}
193
194bool wxStaticBitmap::ImageIsOk() const
195{
0d0512bd 196 return m_image && m_image->Ok();
9e3e0821
VZ
197}
198
199void wxStaticBitmap::Free()
200{
0d0512bd 201 delete m_image;
9e3e0821 202
0d0512bd 203 m_image = NULL;
2bda0e17
KB
204}
205
9d17ee60
RD
206wxSize wxStaticBitmap::DoGetBestSize() const
207{
208 if ( ImageIsOk() )
209 return wxSize(m_image->GetWidth(), m_image->GetHeight());
210
211 // this is completely arbitrary
212 return wxSize(16, 16);
213}
214
d8bffc13 215void wxStaticBitmap::SetImage( const wxGDIImage* image )
2bda0e17 216{
d8bffc13
MB
217 wxGDIImage* convertedImage = ConvertImage( *image );
218 SetImageNoCopy( convertedImage );
9f884528 219 InvalidateBestSize();
d8bffc13 220}
4004f41e 221
d8bffc13
MB
222void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image)
223{
224 Free();
4004f41e 225
d8bffc13
MB
226 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
227 // the image has already been copied
228 m_image = image;
9e3e0821
VZ
229
230 int x, y;
231 int w, h;
232 GetPosition(&x, &y);
233 GetSize(&w, &h);
9e3e0821
VZ
234
235#ifdef __WIN32__
0d0512bd 236 HANDLE handle = (HANDLE)m_image->GetHandle();
d8bffc13
MB
237 LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ;
238 ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) |
239 ( m_isIcon ? SS_ICON : SS_BITMAP ) );
0d0512bd 240 ::SendMessage(GetHwnd(), STM_SETIMAGE,
9e3e0821
VZ
241 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
242#endif // Win32
243
244 if ( ImageIsOk() )
245 {
4004f41e
VZ
246 int width = image->GetWidth(),
247 height = image->GetHeight();
9e3e0821
VZ
248 if ( width && height )
249 {
7c545786
VZ
250 w = width;
251 h = height;
252
0d0512bd 253 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
9e3e0821
VZ
254 }
255 }
256
0d0512bd
VZ
257 RECT rect;
258 rect.left = x;
259 rect.top = y;
260 rect.right = x + w;
261 rect.bottom = y + h;
262 InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
2bda0e17
KB
263}
264
d1e418ea 265// We need this or the control can never be moved e.g. in Dialog Editor.
c140b7e7 266WXLRESULT wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
9e3e0821
VZ
267 WXWPARAM wParam,
268 WXLPARAM lParam)
2bda0e17 269{
4676948b 270#ifndef __WXWINCE__
9e3e0821
VZ
271 // Ensure that static items get messages. Some controls don't like this
272 // message to be intercepted (e.g. RichEdit), hence the tests.
273 if ( nMsg == WM_NCHITTEST )
274 return (long)HTCLIENT;
4676948b 275#endif
2bda0e17 276
9e3e0821 277 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
2bda0e17 278}
d1e418ea 279
1e6feb95 280#endif // wxUSE_STATBMP