]> git.saurik.com Git - wxWidgets.git/blame - src/msw/statbmp.cpp
avoiding warning because of missing define
[wxWidgets.git] / src / msw / statbmp.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
cdccdfab 2// Name: src/msw/statbmp.cpp
2bda0e17
KB
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
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
9e3e0821 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
1e6feb95
VZ
27#if wxUSE_STATBMP
28
cdccdfab 29#include "wx/statbmp.h"
0c589ad0 30
2bda0e17 31#ifndef WX_PRECOMP
b3f2ce1c 32 #include "wx/app.h"
cdccdfab 33 #include "wx/window.h"
0c589ad0 34 #include "wx/icon.h"
30724d04 35 #include "wx/dcclient.h"
2bda0e17
KB
36#endif
37
cdccdfab
WS
38#include "wx/msw/private.h"
39
9dabade2
JS
40#include "wx/sysopt.h"
41
2bda0e17 42#include <stdio.h>
2bda0e17 43
9e3e0821
VZ
44// ---------------------------------------------------------------------------
45// macors
46// ---------------------------------------------------------------------------
47
bc9fb572
JS
48#if wxUSE_EXTENDED_RTTI
49WX_DEFINE_FLAGS( wxStaticBitmapStyle )
50
3ff066a4 51wxBEGIN_FLAGS( wxStaticBitmapStyle )
bc9fb572
JS
52 // new style border flags, we put them first to
53 // use them for streaming out
3ff066a4
SC
54 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
55 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
56 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
57 wxFLAGS_MEMBER(wxBORDER_RAISED)
58 wxFLAGS_MEMBER(wxBORDER_STATIC)
59 wxFLAGS_MEMBER(wxBORDER_NONE)
57f4f925 60
bc9fb572 61 // old style border flags
3ff066a4
SC
62 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
63 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
64 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
65 wxFLAGS_MEMBER(wxRAISED_BORDER)
66 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 67 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
68
69 // standard window styles
3ff066a4
SC
70 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
71 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
72 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
73 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 74 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
75 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
76 wxFLAGS_MEMBER(wxVSCROLL)
77 wxFLAGS_MEMBER(wxHSCROLL)
bc9fb572 78
3ff066a4 79wxEND_FLAGS( wxStaticBitmapStyle )
bc9fb572
JS
80
81IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBitmap, wxControl,"wx/statbmp.h")
82
3ff066a4 83wxBEGIN_PROPERTIES_TABLE(wxStaticBitmap)
af498247 84 wxPROPERTY_FLAGS( WindowStyle , wxStaticBitmapStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 85wxEND_PROPERTIES_TABLE()
bc9fb572 86
3ff066a4
SC
87wxBEGIN_HANDLERS_TABLE(wxStaticBitmap)
88wxEND_HANDLERS_TABLE()
bc9fb572 89
3ff066a4 90wxCONSTRUCTOR_5( wxStaticBitmap, wxWindow* , Parent , wxWindowID , Id , wxBitmap, Bitmap, wxPoint , Position , wxSize , Size )
bc9fb572
JS
91
92#else
4004f41e 93IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
bc9fb572 94#endif
2bda0e17 95
066f1b7a 96/*
57f4f925
WS
97 TODO PROPERTIES :
98 bitmap
066f1b7a
SC
99*/
100
9e3e0821
VZ
101// ===========================================================================
102// implementation
103// ===========================================================================
104
105// ---------------------------------------------------------------------------
106// wxStaticBitmap
107// ---------------------------------------------------------------------------
2bda0e17 108
d8bffc13
MB
109// we may have either bitmap or icon: if a bitmap with mask is passed, we
110// will transform it to an icon ourselves because otherwise the mask will
111// be ignored by Windows
112// note that this function will create a new object every time
113// it is called even if the image needs no conversion
114
d8bffc13
MB
115static wxGDIImage* ConvertImage( const wxGDIImage& bitmap )
116{
117 bool isIcon = bitmap.IsKindOf( CLASSINFO(wxIcon) );
118
119 if( !isIcon )
120 {
121 wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap),
122 _T("not an icon and not a bitmap?") );
123
124 const wxBitmap& bmp = (const wxBitmap&)bitmap;
125 wxMask *mask = bmp.GetMask();
126 if ( mask && mask->GetMaskBitmap() )
127 {
128 wxIcon* icon = new wxIcon;
129 icon->CopyFromBitmap(bmp);
130
131 return icon;
132 }
133
134 return new wxBitmap( bmp );
135 }
136
137 // copying a bitmap is a cheap operation
138 return new wxIcon( (const wxIcon&)bitmap );
139}
140
46a5e01e
VZ
141bool wxStaticBitmap::Create(wxWindow *parent,
142 wxWindowID id,
0d0512bd 143 const wxGDIImage& bitmap,
9e3e0821
VZ
144 const wxPoint& pos,
145 const wxSize& size,
146 long style,
147 const wxString& name)
2bda0e17 148{
46a5e01e 149 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
57f4f925 150 return false;
9e3e0821 151
4004f41e
VZ
152 // we may have either bitmap or icon: if a bitmap with mask is passed, we
153 // will transform it to an icon ourselves because otherwise the mask will
154 // be ignored by Windows
9e3e0821 155 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
669f7a11 156
5cb598ae 157 wxGDIImage *image = ConvertImage( bitmap );
d8bffc13 158 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
9e3e0821 159
46a5e01e 160 // create the native control
3a5bcc4d 161 if ( !MSWCreateControl(_T("STATIC"), wxEmptyString, pos, size) )
46a5e01e
VZ
162 {
163 // control creation failed
3a5bcc4d 164 return false;
46a5e01e 165 }
9e3e0821 166
d8bffc13 167 // no need to delete the new image
46a5e01e 168 SetImageNoCopy(image);
9e3e0821 169
9d17ee60 170 // GetBestSize will work properly now, so set the best size if needed
170acdc9 171 SetInitialSize(size);
57f4f925 172
b3f2ce1c
VZ
173 // Windows versions before XP (and even XP if the application has no
174 // manifest and so the old comctl32.dll is used) don't draw correctly the
175 // images with alpha channel so we need to draw them ourselves and it's
176 // easier to just always do it rather than check if we have an image with
177 // alpha or not
178 if ( wxTheApp->GetComCtl32Version() < 600 )
a73a4ab7
VZ
179 {
180 Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBitmap::DoPaintManually));
181 }
182
57f4f925 183 return true;
46a5e01e 184}
9e3e0821 185
46a5e01e
VZ
186WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const
187{
188 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
9e3e0821 189
46a5e01e
VZ
190 // what kind of control are we?
191 msStyle |= m_isIcon ? SS_ICON : SS_BITMAP;
9e3e0821 192
46a5e01e
VZ
193 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
194 // fit to its size -- this is unexpected and doesn't happen in other ports
42b1fb63
VZ
195 //
196 // and SS_NOTIFY is necessary to receive mouse events
197 msStyle |= SS_CENTERIMAGE | SS_NOTIFY;
46a5e01e
VZ
198
199 return msStyle;
9e3e0821
VZ
200}
201
202bool wxStaticBitmap::ImageIsOk() const
203{
0d0512bd 204 return m_image && m_image->Ok();
9e3e0821
VZ
205}
206
333c8697
VZ
207wxIcon wxStaticBitmap::GetIcon() const
208{
209 wxCHECK_MSG( m_image, wxIcon(), _T("no image in wxStaticBitmap") );
210
211 // we can't ask for an icon if all we have is a bitmap
212 wxCHECK_MSG( m_isIcon, wxIcon(), _T("no icon in this wxStaticBitmap") );
213
214 return *(wxIcon *)m_image;
215}
216
217wxBitmap wxStaticBitmap::GetBitmap() const
218{
219 if ( m_isIcon )
220 {
221 // don't fail because we might have replaced the bitmap with icon
222 // ourselves internally in ConvertImage() to keep the transparency but
223 // the user code doesn't know about it so it still can use GetBitmap()
224 // to retrieve the bitmap
225 return wxBitmap(GetIcon());
226 }
227 else // we have a bitmap
228 {
229 wxCHECK_MSG( m_image, wxBitmap(), _T("no image in wxStaticBitmap") );
230
231 return *(wxBitmap *)m_image;
232 }
233}
234
9e3e0821
VZ
235void wxStaticBitmap::Free()
236{
0d0512bd 237 delete m_image;
9e3e0821 238
0d0512bd 239 m_image = NULL;
2bda0e17
KB
240}
241
9d17ee60
RD
242wxSize wxStaticBitmap::DoGetBestSize() const
243{
244 if ( ImageIsOk() )
31582e4e
RD
245 {
246 wxSize best(m_image->GetWidth(), m_image->GetHeight());
247 CacheBestSize(best);
248 return best;
249 }
9d17ee60
RD
250
251 // this is completely arbitrary
252 return wxSize(16, 16);
253}
254
a73a4ab7
VZ
255void wxStaticBitmap::DoPaintManually(wxPaintEvent& WXUNUSED(event))
256{
257 wxPaintDC dc(this);
258
259 const wxSize size(GetSize());
260 const wxBitmap bmp(GetBitmap());
261
52d80ec6
VZ
262 // Clear the background: notice that we're supposed to be transparent, so
263 // use the parent background colour if we don't have our own instead of
264 // falling back to the default
265 const wxWindow *win = UseBgCol() ? this : GetParent();
266 dc.SetBrush(win->GetBackgroundColour());
a73a4ab7
VZ
267 dc.SetPen(*wxTRANSPARENT_PEN);
268 dc.DrawRectangle(0, 0, size.GetWidth(), size.GetHeight());
269
270 // Draw the image in the middle
271 dc.DrawBitmap(bmp,
272 (size.GetWidth() - bmp.GetWidth()) / 2,
273 (size.GetHeight() - bmp.GetHeight()) / 2,
274 true /* use mask */);
275}
276
d8bffc13 277void wxStaticBitmap::SetImage( const wxGDIImage* image )
2bda0e17 278{
d8bffc13
MB
279 wxGDIImage* convertedImage = ConvertImage( *image );
280 SetImageNoCopy( convertedImage );
9f884528 281 InvalidateBestSize();
d8bffc13 282}
4004f41e 283
d8bffc13
MB
284void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image)
285{
286 Free();
4004f41e 287
d8bffc13
MB
288 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
289 // the image has already been copied
290 m_image = image;
9e3e0821
VZ
291
292 int x, y;
293 int w, h;
294 GetPosition(&x, &y);
295 GetSize(&w, &h);
9e3e0821
VZ
296
297#ifdef __WIN32__
0d0512bd 298 HANDLE handle = (HANDLE)m_image->GetHandle();
d8bffc13
MB
299 LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ;
300 ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) |
301 ( m_isIcon ? SS_ICON : SS_BITMAP ) );
3dece6c4 302 HGDIOBJ oldHandle = (HGDIOBJ)::SendMessage(GetHwnd(), STM_SETIMAGE,
9e3e0821 303 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
3dece6c4
JS
304 // detect if this is still the handle we passed before or
305 // if the static-control made a copy of the bitmap!
4fe41ce6 306 if (m_currentHandle != 0 && oldHandle != (HGDIOBJ) m_currentHandle)
3dece6c4
JS
307 {
308 // the static control made a copy and we are responsible for deleting it
cdccdfab 309 DeleteObject((HGDIOBJ) oldHandle);
3dece6c4 310 }
cdccdfab 311 m_currentHandle = (WXHANDLE)handle;
9e3e0821
VZ
312#endif // Win32
313
314 if ( ImageIsOk() )
315 {
4004f41e
VZ
316 int width = image->GetWidth(),
317 height = image->GetHeight();
9e3e0821
VZ
318 if ( width && height )
319 {
7c545786
VZ
320 w = width;
321 h = height;
322
0d0512bd 323 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
9e3e0821
VZ
324 }
325 }
326
0d0512bd
VZ
327 RECT rect;
328 rect.left = x;
329 rect.top = y;
330 rect.right = x + w;
331 rect.bottom = y + h;
57f4f925 332 ::InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
2bda0e17
KB
333}
334
1e6feb95 335#endif // wxUSE_STATBMP