]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/statbmp.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / src / msw / statbmp.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/statbmp.cpp
3// Purpose: wxStaticBitmap
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// Copyright: (c) Julian Smart
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// ===========================================================================
12// declarations
13// ===========================================================================
14
15// ---------------------------------------------------------------------------
16// headers
17// ---------------------------------------------------------------------------
18
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#if wxUSE_STATBMP
27
28#include "wx/statbmp.h"
29
30#ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/window.h"
33 #include "wx/icon.h"
34 #include "wx/dcclient.h"
35#endif
36
37#include "wx/msw/private.h"
38
39#include "wx/sysopt.h"
40
41#include <stdio.h>
42
43// ---------------------------------------------------------------------------
44// macros
45// ---------------------------------------------------------------------------
46
47wxBEGIN_EVENT_TABLE(wxStaticBitmap, wxStaticBitmapBase)
48 EVT_SIZE(wxStaticBitmap::WXHandleSize)
49wxEND_EVENT_TABLE()
50
51// ===========================================================================
52// implementation
53// ===========================================================================
54
55// ---------------------------------------------------------------------------
56// wxStaticBitmap
57// ---------------------------------------------------------------------------
58
59// we may have either bitmap or icon: if a bitmap with mask is passed, we
60// will transform it to an icon ourselves because otherwise the mask will
61// be ignored by Windows
62// note that this function will create a new object every time
63// it is called even if the image needs no conversion
64
65static wxGDIImage* ConvertImage( const wxGDIImage& bitmap )
66{
67 bool isIcon = bitmap.IsKindOf( wxCLASSINFO(wxIcon) );
68
69 if( !isIcon )
70 {
71 wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap),
72 wxT("not an icon and not a bitmap?") );
73
74 const wxBitmap& bmp = (const wxBitmap&)bitmap;
75 wxMask *mask = bmp.GetMask();
76 if ( mask && mask->GetMaskBitmap() )
77 {
78 wxIcon* icon = new wxIcon;
79 icon->CopyFromBitmap(bmp);
80
81 return icon;
82 }
83
84 return new wxBitmap( bmp );
85 }
86
87 // copying a bitmap is a cheap operation
88 return new wxIcon( (const wxIcon&)bitmap );
89}
90
91bool wxStaticBitmap::Create(wxWindow *parent,
92 wxWindowID id,
93 const wxGDIImage& bitmap,
94 const wxPoint& pos,
95 const wxSize& size,
96 long style,
97 const wxString& name)
98{
99 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
100 return false;
101
102 // we may have either bitmap or icon: if a bitmap with mask is passed, we
103 // will transform it to an icon ourselves because otherwise the mask will
104 // be ignored by Windows
105 m_isIcon = bitmap.IsKindOf(wxCLASSINFO(wxIcon));
106
107 wxGDIImage *image = ConvertImage( bitmap );
108 m_isIcon = image->IsKindOf( wxCLASSINFO(wxIcon) );
109
110 // create the native control
111 if ( !MSWCreateControl(wxT("STATIC"), wxEmptyString, pos, size) )
112 {
113 // control creation failed
114 return false;
115 }
116
117 // no need to delete the new image
118 SetImageNoCopy(image);
119
120 // GetBestSize will work properly now, so set the best size if needed
121 SetInitialSize(size);
122
123 // painting manually is reported not to work under Windows CE (see #10093),
124 // so don't do it there even if this probably means that alpha is not
125 // supported there -- but at least bitmaps without alpha appear correctly
126#ifndef __WXWINCE__
127 // Windows versions before XP (and even XP if the application has no
128 // manifest and so the old comctl32.dll is used) don't draw correctly the
129 // images with alpha channel so we need to draw them ourselves and it's
130 // easier to just always do it rather than check if we have an image with
131 // alpha or not
132 if ( wxTheApp->GetComCtl32Version() < 600 )
133 {
134 Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBitmap::DoPaintManually));
135 }
136#endif // !__WXWINCE__
137
138 return true;
139}
140
141WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const
142{
143 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
144
145 // what kind of control are we?
146 msStyle |= m_isIcon ? SS_ICON : SS_BITMAP;
147
148 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
149 // fit to its size -- this is unexpected and doesn't happen in other ports
150 //
151 // and SS_NOTIFY is necessary to receive mouse events
152 msStyle |= SS_CENTERIMAGE | SS_NOTIFY;
153
154 return msStyle;
155}
156
157bool wxStaticBitmap::ImageIsOk() const
158{
159 return m_image && m_image->IsOk();
160}
161
162wxIcon wxStaticBitmap::GetIcon() const
163{
164 wxCHECK_MSG( m_image, wxIcon(), wxT("no image in wxStaticBitmap") );
165
166 // we can't ask for an icon if all we have is a bitmap
167 wxCHECK_MSG( m_isIcon, wxIcon(), wxT("no icon in this wxStaticBitmap") );
168
169 return *(wxIcon *)m_image;
170}
171
172wxBitmap wxStaticBitmap::GetBitmap() const
173{
174 if ( m_isIcon )
175 {
176 // don't fail because we might have replaced the bitmap with icon
177 // ourselves internally in ConvertImage() to keep the transparency but
178 // the user code doesn't know about it so it still can use GetBitmap()
179 // to retrieve the bitmap
180 return wxBitmap(GetIcon());
181 }
182 else // we have a bitmap
183 {
184 wxCHECK_MSG( m_image, wxBitmap(), wxT("no image in wxStaticBitmap") );
185
186 return *(wxBitmap *)m_image;
187 }
188}
189
190void wxStaticBitmap::Free()
191{
192 wxDELETE(m_image);
193}
194
195wxSize wxStaticBitmap::DoGetBestClientSize() const
196{
197 wxSize size;
198 if ( ImageIsOk() )
199 {
200 size = m_image->GetSize();
201 }
202 else // No image yet
203 {
204 // this is completely arbitrary
205 size.x =
206 size.y = 16;
207 }
208
209 return size;
210}
211
212void wxStaticBitmap::WXHandleSize(wxSizeEvent& event)
213{
214 // Invalidate everything when our size changes as the image position (it's
215 // drawn centred in the window client area) changes.
216 Refresh();
217
218 event.Skip();
219}
220
221#ifndef __WXWINCE__
222
223void wxStaticBitmap::DoPaintManually(wxPaintEvent& WXUNUSED(event))
224{
225 wxPaintDC dc(this);
226
227 const wxSize size(GetSize());
228 const wxBitmap bmp(GetBitmap());
229
230 // Clear the background: notice that we're supposed to be transparent, so
231 // use the parent background colour if we don't have our own instead of
232 // falling back to the default
233 const wxWindow *win = UseBgCol() ? this : GetParent();
234 dc.SetBrush(win->GetBackgroundColour());
235 dc.SetPen(*wxTRANSPARENT_PEN);
236 dc.DrawRectangle(0, 0, size.GetWidth(), size.GetHeight());
237
238 // Draw the image in the middle
239 dc.DrawBitmap(bmp,
240 (size.GetWidth() - bmp.GetWidth()) / 2,
241 (size.GetHeight() - bmp.GetHeight()) / 2,
242 true /* use mask */);
243}
244
245#endif // !__WXWINCE__
246
247void wxStaticBitmap::SetImage( const wxGDIImage* image )
248{
249 wxGDIImage* convertedImage = ConvertImage( *image );
250 SetImageNoCopy( convertedImage );
251}
252
253void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image)
254{
255 Free();
256 InvalidateBestSize();
257
258 m_isIcon = image->IsKindOf( wxCLASSINFO(wxIcon) );
259 // the image has already been copied
260 m_image = image;
261
262 int x, y;
263 int w, h;
264 GetPosition(&x, &y);
265 GetSize(&w, &h);
266
267#ifdef __WIN32__
268 HANDLE handle = (HANDLE)m_image->GetHandle();
269 LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ;
270 ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) |
271 ( m_isIcon ? SS_ICON : SS_BITMAP ) );
272 HGDIOBJ oldHandle = (HGDIOBJ)::SendMessage(GetHwnd(), STM_SETIMAGE,
273 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
274 // detect if this is still the handle we passed before or
275 // if the static-control made a copy of the bitmap!
276 if (m_currentHandle != 0 && oldHandle != (HGDIOBJ) m_currentHandle)
277 {
278 // the static control made a copy and we are responsible for deleting it
279 DeleteObject((HGDIOBJ) oldHandle);
280 }
281 m_currentHandle = (WXHANDLE)handle;
282#endif // Win32
283
284 if ( ImageIsOk() )
285 {
286 int width = image->GetWidth(),
287 height = image->GetHeight();
288 if ( width && height )
289 {
290 w = width;
291 h = height;
292
293 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
294 }
295 }
296
297 RECT rect;
298 rect.left = x;
299 rect.top = y;
300 rect.right = x + w;
301 rect.bottom = y + h;
302 ::InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
303}
304
305#endif // wxUSE_STATBMP