]>
Commit | Line | Data |
---|---|---|
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 | |
6c9a19aa | 7 | // Copyright: (c) Julian Smart |
65571936 | 8 | // Licence: wxWindows licence |
2bda0e17 KB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
9e3e0821 VZ |
11 | // =========================================================================== |
12 | // declarations | |
13 | // =========================================================================== | |
14 | ||
15 | // --------------------------------------------------------------------------- | |
16 | // headers | |
17 | // --------------------------------------------------------------------------- | |
18 | ||
2bda0e17 KB |
19 | // For compilers that support precompilation, includes "wx.h". |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
9e3e0821 | 23 | #pragma hdrstop |
2bda0e17 KB |
24 | #endif |
25 | ||
1e6feb95 VZ |
26 | #if wxUSE_STATBMP |
27 | ||
cdccdfab | 28 | #include "wx/statbmp.h" |
0c589ad0 | 29 | |
2bda0e17 | 30 | #ifndef WX_PRECOMP |
b3f2ce1c | 31 | #include "wx/app.h" |
cdccdfab | 32 | #include "wx/window.h" |
0c589ad0 | 33 | #include "wx/icon.h" |
30724d04 | 34 | #include "wx/dcclient.h" |
2bda0e17 KB |
35 | #endif |
36 | ||
cdccdfab WS |
37 | #include "wx/msw/private.h" |
38 | ||
9dabade2 JS |
39 | #include "wx/sysopt.h" |
40 | ||
2bda0e17 | 41 | #include <stdio.h> |
2bda0e17 | 42 | |
9e3e0821 | 43 | // --------------------------------------------------------------------------- |
05942059 | 44 | // macros |
9e3e0821 VZ |
45 | // --------------------------------------------------------------------------- |
46 | ||
05942059 VZ |
47 | wxBEGIN_EVENT_TABLE(wxStaticBitmap, wxStaticBitmapBase) |
48 | EVT_SIZE(wxStaticBitmap::WXHandleSize) | |
49 | wxEND_EVENT_TABLE() | |
50 | ||
9e3e0821 VZ |
51 | // =========================================================================== |
52 | // implementation | |
53 | // =========================================================================== | |
54 | ||
55 | // --------------------------------------------------------------------------- | |
56 | // wxStaticBitmap | |
57 | // --------------------------------------------------------------------------- | |
2bda0e17 | 58 | |
d8bffc13 MB |
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 | ||
d8bffc13 MB |
65 | static wxGDIImage* ConvertImage( const wxGDIImage& bitmap ) |
66 | { | |
80a46597 | 67 | bool isIcon = bitmap.IsKindOf( wxCLASSINFO(wxIcon) ); |
d8bffc13 MB |
68 | |
69 | if( !isIcon ) | |
70 | { | |
71 | wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap), | |
9a83f860 | 72 | wxT("not an icon and not a bitmap?") ); |
d8bffc13 MB |
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 | ||
46a5e01e VZ |
91 | bool wxStaticBitmap::Create(wxWindow *parent, |
92 | wxWindowID id, | |
0d0512bd | 93 | const wxGDIImage& bitmap, |
9e3e0821 VZ |
94 | const wxPoint& pos, |
95 | const wxSize& size, | |
96 | long style, | |
97 | const wxString& name) | |
2bda0e17 | 98 | { |
46a5e01e | 99 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) |
57f4f925 | 100 | return false; |
9e3e0821 | 101 | |
4004f41e VZ |
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 | |
80a46597 | 105 | m_isIcon = bitmap.IsKindOf(wxCLASSINFO(wxIcon)); |
669f7a11 | 106 | |
5cb598ae | 107 | wxGDIImage *image = ConvertImage( bitmap ); |
80a46597 | 108 | m_isIcon = image->IsKindOf( wxCLASSINFO(wxIcon) ); |
9e3e0821 | 109 | |
46a5e01e | 110 | // create the native control |
9a83f860 | 111 | if ( !MSWCreateControl(wxT("STATIC"), wxEmptyString, pos, size) ) |
46a5e01e VZ |
112 | { |
113 | // control creation failed | |
3a5bcc4d | 114 | return false; |
46a5e01e | 115 | } |
9e3e0821 | 116 | |
d8bffc13 | 117 | // no need to delete the new image |
46a5e01e | 118 | SetImageNoCopy(image); |
9e3e0821 | 119 | |
9d17ee60 | 120 | // GetBestSize will work properly now, so set the best size if needed |
170acdc9 | 121 | SetInitialSize(size); |
57f4f925 | 122 | |
9eb2347d VZ |
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__ | |
b3f2ce1c VZ |
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 ) | |
a73a4ab7 VZ |
133 | { |
134 | Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBitmap::DoPaintManually)); | |
135 | } | |
9eb2347d | 136 | #endif // !__WXWINCE__ |
a73a4ab7 | 137 | |
57f4f925 | 138 | return true; |
46a5e01e | 139 | } |
9e3e0821 | 140 | |
46a5e01e VZ |
141 | WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const |
142 | { | |
143 | WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle); | |
9e3e0821 | 144 | |
46a5e01e VZ |
145 | // what kind of control are we? |
146 | msStyle |= m_isIcon ? SS_ICON : SS_BITMAP; | |
9e3e0821 | 147 | |
46a5e01e VZ |
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 | |
42b1fb63 VZ |
150 | // |
151 | // and SS_NOTIFY is necessary to receive mouse events | |
152 | msStyle |= SS_CENTERIMAGE | SS_NOTIFY; | |
46a5e01e VZ |
153 | |
154 | return msStyle; | |
9e3e0821 VZ |
155 | } |
156 | ||
157 | bool wxStaticBitmap::ImageIsOk() const | |
158 | { | |
a1b806b9 | 159 | return m_image && m_image->IsOk(); |
9e3e0821 VZ |
160 | } |
161 | ||
333c8697 VZ |
162 | wxIcon wxStaticBitmap::GetIcon() const |
163 | { | |
9a83f860 | 164 | wxCHECK_MSG( m_image, wxIcon(), wxT("no image in wxStaticBitmap") ); |
333c8697 VZ |
165 | |
166 | // we can't ask for an icon if all we have is a bitmap | |
9a83f860 | 167 | wxCHECK_MSG( m_isIcon, wxIcon(), wxT("no icon in this wxStaticBitmap") ); |
333c8697 VZ |
168 | |
169 | return *(wxIcon *)m_image; | |
170 | } | |
171 | ||
172 | wxBitmap 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 | { | |
9a83f860 | 184 | wxCHECK_MSG( m_image, wxBitmap(), wxT("no image in wxStaticBitmap") ); |
333c8697 VZ |
185 | |
186 | return *(wxBitmap *)m_image; | |
187 | } | |
188 | } | |
189 | ||
9e3e0821 VZ |
190 | void wxStaticBitmap::Free() |
191 | { | |
5276b0a5 | 192 | wxDELETE(m_image); |
2bda0e17 KB |
193 | } |
194 | ||
ae4375b8 | 195 | wxSize wxStaticBitmap::DoGetBestClientSize() const |
9d17ee60 | 196 | { |
ae4375b8 | 197 | wxSize size; |
9d17ee60 | 198 | if ( ImageIsOk() ) |
31582e4e | 199 | { |
ae4375b8 VZ |
200 | size = m_image->GetSize(); |
201 | } | |
202 | else // No image yet | |
203 | { | |
204 | // this is completely arbitrary | |
205 | size.x = | |
206 | size.y = 16; | |
31582e4e | 207 | } |
9d17ee60 | 208 | |
ae4375b8 | 209 | return size; |
9d17ee60 RD |
210 | } |
211 | ||
05942059 VZ |
212 | void 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 | ||
9eb2347d VZ |
221 | #ifndef __WXWINCE__ |
222 | ||
a73a4ab7 VZ |
223 | void wxStaticBitmap::DoPaintManually(wxPaintEvent& WXUNUSED(event)) |
224 | { | |
225 | wxPaintDC dc(this); | |
226 | ||
227 | const wxSize size(GetSize()); | |
228 | const wxBitmap bmp(GetBitmap()); | |
229 | ||
52d80ec6 VZ |
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()); | |
a73a4ab7 VZ |
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 | ||
9eb2347d VZ |
245 | #endif // !__WXWINCE__ |
246 | ||
d8bffc13 | 247 | void wxStaticBitmap::SetImage( const wxGDIImage* image ) |
2bda0e17 | 248 | { |
d8bffc13 MB |
249 | wxGDIImage* convertedImage = ConvertImage( *image ); |
250 | SetImageNoCopy( convertedImage ); | |
251 | } | |
4004f41e | 252 | |
d8bffc13 MB |
253 | void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image) |
254 | { | |
255 | Free(); | |
ae4375b8 | 256 | InvalidateBestSize(); |
4004f41e | 257 | |
80a46597 | 258 | m_isIcon = image->IsKindOf( wxCLASSINFO(wxIcon) ); |
d8bffc13 MB |
259 | // the image has already been copied |
260 | m_image = image; | |
9e3e0821 VZ |
261 | |
262 | int x, y; | |
263 | int w, h; | |
264 | GetPosition(&x, &y); | |
265 | GetSize(&w, &h); | |
9e3e0821 VZ |
266 | |
267 | #ifdef __WIN32__ | |
0d0512bd | 268 | HANDLE handle = (HANDLE)m_image->GetHandle(); |
d8bffc13 MB |
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 ) ); | |
3dece6c4 | 272 | HGDIOBJ oldHandle = (HGDIOBJ)::SendMessage(GetHwnd(), STM_SETIMAGE, |
9e3e0821 | 273 | m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle); |
3dece6c4 JS |
274 | // detect if this is still the handle we passed before or |
275 | // if the static-control made a copy of the bitmap! | |
4fe41ce6 | 276 | if (m_currentHandle != 0 && oldHandle != (HGDIOBJ) m_currentHandle) |
3dece6c4 JS |
277 | { |
278 | // the static control made a copy and we are responsible for deleting it | |
cdccdfab | 279 | DeleteObject((HGDIOBJ) oldHandle); |
3dece6c4 | 280 | } |
cdccdfab | 281 | m_currentHandle = (WXHANDLE)handle; |
9e3e0821 VZ |
282 | #endif // Win32 |
283 | ||
284 | if ( ImageIsOk() ) | |
285 | { | |
4004f41e VZ |
286 | int width = image->GetWidth(), |
287 | height = image->GetHeight(); | |
9e3e0821 VZ |
288 | if ( width && height ) |
289 | { | |
7c545786 VZ |
290 | w = width; |
291 | h = height; | |
292 | ||
0d0512bd | 293 | ::MoveWindow(GetHwnd(), x, y, width, height, FALSE); |
9e3e0821 VZ |
294 | } |
295 | } | |
296 | ||
0d0512bd VZ |
297 | RECT rect; |
298 | rect.left = x; | |
299 | rect.top = y; | |
300 | rect.right = x + w; | |
301 | rect.bottom = y + h; | |
57f4f925 | 302 | ::InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE); |
2bda0e17 KB |
303 | } |
304 | ||
1e6feb95 | 305 | #endif // wxUSE_STATBMP |