]>
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 | |
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 | ||
9e3e0821 VZ |
48 | // =========================================================================== |
49 | // implementation | |
50 | // =========================================================================== | |
51 | ||
52 | // --------------------------------------------------------------------------- | |
53 | // wxStaticBitmap | |
54 | // --------------------------------------------------------------------------- | |
2bda0e17 | 55 | |
d8bffc13 MB |
56 | // we may have either bitmap or icon: if a bitmap with mask is passed, we |
57 | // will transform it to an icon ourselves because otherwise the mask will | |
58 | // be ignored by Windows | |
59 | // note that this function will create a new object every time | |
60 | // it is called even if the image needs no conversion | |
61 | ||
d8bffc13 MB |
62 | static wxGDIImage* ConvertImage( const wxGDIImage& bitmap ) |
63 | { | |
64 | bool isIcon = bitmap.IsKindOf( CLASSINFO(wxIcon) ); | |
65 | ||
66 | if( !isIcon ) | |
67 | { | |
68 | wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap), | |
9a83f860 | 69 | wxT("not an icon and not a bitmap?") ); |
d8bffc13 MB |
70 | |
71 | const wxBitmap& bmp = (const wxBitmap&)bitmap; | |
72 | wxMask *mask = bmp.GetMask(); | |
73 | if ( mask && mask->GetMaskBitmap() ) | |
74 | { | |
75 | wxIcon* icon = new wxIcon; | |
76 | icon->CopyFromBitmap(bmp); | |
77 | ||
78 | return icon; | |
79 | } | |
80 | ||
81 | return new wxBitmap( bmp ); | |
82 | } | |
83 | ||
84 | // copying a bitmap is a cheap operation | |
85 | return new wxIcon( (const wxIcon&)bitmap ); | |
86 | } | |
87 | ||
46a5e01e VZ |
88 | bool wxStaticBitmap::Create(wxWindow *parent, |
89 | wxWindowID id, | |
0d0512bd | 90 | const wxGDIImage& bitmap, |
9e3e0821 VZ |
91 | const wxPoint& pos, |
92 | const wxSize& size, | |
93 | long style, | |
94 | const wxString& name) | |
2bda0e17 | 95 | { |
46a5e01e | 96 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) |
57f4f925 | 97 | return false; |
9e3e0821 | 98 | |
4004f41e VZ |
99 | // we may have either bitmap or icon: if a bitmap with mask is passed, we |
100 | // will transform it to an icon ourselves because otherwise the mask will | |
101 | // be ignored by Windows | |
9e3e0821 | 102 | m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon)); |
669f7a11 | 103 | |
5cb598ae | 104 | wxGDIImage *image = ConvertImage( bitmap ); |
d8bffc13 | 105 | m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) ); |
9e3e0821 | 106 | |
46a5e01e | 107 | // create the native control |
9a83f860 | 108 | if ( !MSWCreateControl(wxT("STATIC"), wxEmptyString, pos, size) ) |
46a5e01e VZ |
109 | { |
110 | // control creation failed | |
3a5bcc4d | 111 | return false; |
46a5e01e | 112 | } |
9e3e0821 | 113 | |
d8bffc13 | 114 | // no need to delete the new image |
46a5e01e | 115 | SetImageNoCopy(image); |
9e3e0821 | 116 | |
9d17ee60 | 117 | // GetBestSize will work properly now, so set the best size if needed |
170acdc9 | 118 | SetInitialSize(size); |
57f4f925 | 119 | |
9eb2347d VZ |
120 | // painting manually is reported not to work under Windows CE (see #10093), |
121 | // so don't do it there even if this probably means that alpha is not | |
122 | // supported there -- but at least bitmaps without alpha appear correctly | |
123 | #ifndef __WXWINCE__ | |
b3f2ce1c VZ |
124 | // Windows versions before XP (and even XP if the application has no |
125 | // manifest and so the old comctl32.dll is used) don't draw correctly the | |
126 | // images with alpha channel so we need to draw them ourselves and it's | |
127 | // easier to just always do it rather than check if we have an image with | |
128 | // alpha or not | |
129 | if ( wxTheApp->GetComCtl32Version() < 600 ) | |
a73a4ab7 VZ |
130 | { |
131 | Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBitmap::DoPaintManually)); | |
132 | } | |
9eb2347d | 133 | #endif // !__WXWINCE__ |
a73a4ab7 | 134 | |
57f4f925 | 135 | return true; |
46a5e01e | 136 | } |
9e3e0821 | 137 | |
46a5e01e VZ |
138 | WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const |
139 | { | |
140 | WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle); | |
9e3e0821 | 141 | |
46a5e01e VZ |
142 | // what kind of control are we? |
143 | msStyle |= m_isIcon ? SS_ICON : SS_BITMAP; | |
9e3e0821 | 144 | |
46a5e01e VZ |
145 | // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to |
146 | // fit to its size -- this is unexpected and doesn't happen in other ports | |
42b1fb63 VZ |
147 | // |
148 | // and SS_NOTIFY is necessary to receive mouse events | |
149 | msStyle |= SS_CENTERIMAGE | SS_NOTIFY; | |
46a5e01e VZ |
150 | |
151 | return msStyle; | |
9e3e0821 VZ |
152 | } |
153 | ||
154 | bool wxStaticBitmap::ImageIsOk() const | |
155 | { | |
a1b806b9 | 156 | return m_image && m_image->IsOk(); |
9e3e0821 VZ |
157 | } |
158 | ||
333c8697 VZ |
159 | wxIcon wxStaticBitmap::GetIcon() const |
160 | { | |
9a83f860 | 161 | wxCHECK_MSG( m_image, wxIcon(), wxT("no image in wxStaticBitmap") ); |
333c8697 VZ |
162 | |
163 | // we can't ask for an icon if all we have is a bitmap | |
9a83f860 | 164 | wxCHECK_MSG( m_isIcon, wxIcon(), wxT("no icon in this wxStaticBitmap") ); |
333c8697 VZ |
165 | |
166 | return *(wxIcon *)m_image; | |
167 | } | |
168 | ||
169 | wxBitmap wxStaticBitmap::GetBitmap() const | |
170 | { | |
171 | if ( m_isIcon ) | |
172 | { | |
173 | // don't fail because we might have replaced the bitmap with icon | |
174 | // ourselves internally in ConvertImage() to keep the transparency but | |
175 | // the user code doesn't know about it so it still can use GetBitmap() | |
176 | // to retrieve the bitmap | |
177 | return wxBitmap(GetIcon()); | |
178 | } | |
179 | else // we have a bitmap | |
180 | { | |
9a83f860 | 181 | wxCHECK_MSG( m_image, wxBitmap(), wxT("no image in wxStaticBitmap") ); |
333c8697 VZ |
182 | |
183 | return *(wxBitmap *)m_image; | |
184 | } | |
185 | } | |
186 | ||
9e3e0821 VZ |
187 | void wxStaticBitmap::Free() |
188 | { | |
5276b0a5 | 189 | wxDELETE(m_image); |
2bda0e17 KB |
190 | } |
191 | ||
ae4375b8 | 192 | wxSize wxStaticBitmap::DoGetBestClientSize() const |
9d17ee60 | 193 | { |
ae4375b8 | 194 | wxSize size; |
9d17ee60 | 195 | if ( ImageIsOk() ) |
31582e4e | 196 | { |
ae4375b8 VZ |
197 | size = m_image->GetSize(); |
198 | } | |
199 | else // No image yet | |
200 | { | |
201 | // this is completely arbitrary | |
202 | size.x = | |
203 | size.y = 16; | |
31582e4e | 204 | } |
9d17ee60 | 205 | |
ae4375b8 | 206 | return size; |
9d17ee60 RD |
207 | } |
208 | ||
9eb2347d VZ |
209 | #ifndef __WXWINCE__ |
210 | ||
a73a4ab7 VZ |
211 | void wxStaticBitmap::DoPaintManually(wxPaintEvent& WXUNUSED(event)) |
212 | { | |
213 | wxPaintDC dc(this); | |
214 | ||
215 | const wxSize size(GetSize()); | |
216 | const wxBitmap bmp(GetBitmap()); | |
217 | ||
52d80ec6 VZ |
218 | // Clear the background: notice that we're supposed to be transparent, so |
219 | // use the parent background colour if we don't have our own instead of | |
220 | // falling back to the default | |
221 | const wxWindow *win = UseBgCol() ? this : GetParent(); | |
222 | dc.SetBrush(win->GetBackgroundColour()); | |
a73a4ab7 VZ |
223 | dc.SetPen(*wxTRANSPARENT_PEN); |
224 | dc.DrawRectangle(0, 0, size.GetWidth(), size.GetHeight()); | |
225 | ||
226 | // Draw the image in the middle | |
227 | dc.DrawBitmap(bmp, | |
228 | (size.GetWidth() - bmp.GetWidth()) / 2, | |
229 | (size.GetHeight() - bmp.GetHeight()) / 2, | |
230 | true /* use mask */); | |
231 | } | |
232 | ||
9eb2347d VZ |
233 | #endif // !__WXWINCE__ |
234 | ||
d8bffc13 | 235 | void wxStaticBitmap::SetImage( const wxGDIImage* image ) |
2bda0e17 | 236 | { |
d8bffc13 MB |
237 | wxGDIImage* convertedImage = ConvertImage( *image ); |
238 | SetImageNoCopy( convertedImage ); | |
239 | } | |
4004f41e | 240 | |
d8bffc13 MB |
241 | void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image) |
242 | { | |
243 | Free(); | |
ae4375b8 | 244 | InvalidateBestSize(); |
4004f41e | 245 | |
d8bffc13 MB |
246 | m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) ); |
247 | // the image has already been copied | |
248 | m_image = image; | |
9e3e0821 VZ |
249 | |
250 | int x, y; | |
251 | int w, h; | |
252 | GetPosition(&x, &y); | |
253 | GetSize(&w, &h); | |
9e3e0821 VZ |
254 | |
255 | #ifdef __WIN32__ | |
0d0512bd | 256 | HANDLE handle = (HANDLE)m_image->GetHandle(); |
d8bffc13 MB |
257 | LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ; |
258 | ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) | | |
259 | ( m_isIcon ? SS_ICON : SS_BITMAP ) ); | |
3dece6c4 | 260 | HGDIOBJ oldHandle = (HGDIOBJ)::SendMessage(GetHwnd(), STM_SETIMAGE, |
9e3e0821 | 261 | m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle); |
3dece6c4 JS |
262 | // detect if this is still the handle we passed before or |
263 | // if the static-control made a copy of the bitmap! | |
4fe41ce6 | 264 | if (m_currentHandle != 0 && oldHandle != (HGDIOBJ) m_currentHandle) |
3dece6c4 JS |
265 | { |
266 | // the static control made a copy and we are responsible for deleting it | |
cdccdfab | 267 | DeleteObject((HGDIOBJ) oldHandle); |
3dece6c4 | 268 | } |
cdccdfab | 269 | m_currentHandle = (WXHANDLE)handle; |
9e3e0821 VZ |
270 | #endif // Win32 |
271 | ||
272 | if ( ImageIsOk() ) | |
273 | { | |
4004f41e VZ |
274 | int width = image->GetWidth(), |
275 | height = image->GetHeight(); | |
9e3e0821 VZ |
276 | if ( width && height ) |
277 | { | |
7c545786 VZ |
278 | w = width; |
279 | h = height; | |
280 | ||
0d0512bd | 281 | ::MoveWindow(GetHwnd(), x, y, width, height, FALSE); |
9e3e0821 VZ |
282 | } |
283 | } | |
284 | ||
0d0512bd VZ |
285 | RECT rect; |
286 | rect.left = x; | |
287 | rect.top = y; | |
288 | rect.right = x + w; | |
289 | rect.bottom = y + h; | |
57f4f925 | 290 | ::InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE); |
2bda0e17 KB |
291 | } |
292 | ||
1e6feb95 | 293 | #endif // wxUSE_STATBMP |