]>
Commit | Line | Data |
---|---|---|
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$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
fd3f686c | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
9e3e0821 VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
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 | ||
4004f41e | 47 | IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl) |
2bda0e17 | 48 | |
9e3e0821 VZ |
49 | // =========================================================================== |
50 | // implementation | |
51 | // =========================================================================== | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | // wxStaticBitmap | |
55 | // --------------------------------------------------------------------------- | |
2bda0e17 | 56 | |
d8bffc13 MB |
57 | // we may have either bitmap or icon: if a bitmap with mask is passed, we |
58 | // will transform it to an icon ourselves because otherwise the mask will | |
59 | // be ignored by Windows | |
60 | // note that this function will create a new object every time | |
61 | // it is called even if the image needs no conversion | |
62 | ||
63 | #ifndef __WIN16__ | |
64 | ||
65 | static wxGDIImage* ConvertImage( const wxGDIImage& bitmap ) | |
66 | { | |
67 | bool isIcon = bitmap.IsKindOf( CLASSINFO(wxIcon) ); | |
68 | ||
69 | if( !isIcon ) | |
70 | { | |
71 | wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap), | |
72 | _T("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 | ||
91 | #endif | |
92 | ||
46a5e01e VZ |
93 | bool wxStaticBitmap::Create(wxWindow *parent, |
94 | wxWindowID id, | |
0d0512bd | 95 | const wxGDIImage& bitmap, |
9e3e0821 VZ |
96 | const wxPoint& pos, |
97 | const wxSize& size, | |
98 | long style, | |
99 | const wxString& name) | |
2bda0e17 | 100 | { |
46a5e01e VZ |
101 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) |
102 | return FALSE; | |
9e3e0821 | 103 | |
4004f41e VZ |
104 | // we may have either bitmap or icon: if a bitmap with mask is passed, we |
105 | // will transform it to an icon ourselves because otherwise the mask will | |
106 | // be ignored by Windows | |
d8bffc13 | 107 | wxGDIImage *image = (wxGDIImage *)NULL; |
9e3e0821 | 108 | m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon)); |
669f7a11 JS |
109 | |
110 | #ifdef __WIN16__ | |
111 | wxASSERT_MSG( !m_isIcon, "Icons are not supported in wxStaticBitmap under WIN16." ); | |
d8bffc13 | 112 | image = &bitmap; |
46a5e01e | 113 | #else // Win32 |
d8bffc13 MB |
114 | image = ConvertImage( bitmap ); |
115 | m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) ); | |
46a5e01e | 116 | #endif // Win16/32 |
9e3e0821 | 117 | |
46a5e01e VZ |
118 | // create the native control |
119 | if ( !MSWCreateControl( | |
9e3e0821 | 120 | #ifdef __WIN32__ |
46a5e01e | 121 | _T("STATIC"), |
9e3e0821 | 122 | #else // Win16 |
46a5e01e VZ |
123 | _T("BUTTON"), |
124 | #endif // Win32/16 | |
125 | _T(""), pos, size, style) ) | |
126 | { | |
127 | // control creation failed | |
128 | return FALSE; | |
129 | } | |
9e3e0821 | 130 | |
d8bffc13 | 131 | // no need to delete the new image |
46a5e01e | 132 | SetImageNoCopy(image); |
9e3e0821 | 133 | |
46a5e01e VZ |
134 | return TRUE; |
135 | } | |
9e3e0821 | 136 | |
46a5e01e VZ |
137 | WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const |
138 | { | |
139 | WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle); | |
9e3e0821 | 140 | |
46a5e01e VZ |
141 | #ifdef __WIN32__ |
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 | |
147 | msStyle |= SS_CENTERIMAGE; | |
148 | #else // Win16 | |
149 | msStyle |= BS_OWNERDRAW; | |
150 | #endif // Win32/16 | |
151 | ||
152 | return msStyle; | |
9e3e0821 VZ |
153 | } |
154 | ||
155 | bool wxStaticBitmap::ImageIsOk() const | |
156 | { | |
0d0512bd | 157 | return m_image && m_image->Ok(); |
9e3e0821 VZ |
158 | } |
159 | ||
160 | void wxStaticBitmap::Free() | |
161 | { | |
0d0512bd | 162 | delete m_image; |
9e3e0821 | 163 | |
0d0512bd | 164 | m_image = NULL; |
2bda0e17 KB |
165 | } |
166 | ||
f68586e5 | 167 | wxSize wxStaticBitmap::DoGetBestSize() const |
2bda0e17 | 168 | { |
4438caf4 VZ |
169 | // reuse the current size (as wxWindow does) instead of using some |
170 | // arbitrary default size (as wxControl, our immediate base class, does) | |
171 | return wxWindow::DoGetBestSize(); | |
2bda0e17 KB |
172 | } |
173 | ||
d8bffc13 | 174 | void wxStaticBitmap::SetImage( const wxGDIImage* image ) |
2bda0e17 | 175 | { |
d8bffc13 MB |
176 | wxGDIImage* convertedImage = ConvertImage( *image ); |
177 | SetImageNoCopy( convertedImage ); | |
178 | } | |
4004f41e | 179 | |
d8bffc13 MB |
180 | void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image) |
181 | { | |
182 | Free(); | |
4004f41e | 183 | |
d8bffc13 MB |
184 | m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) ); |
185 | // the image has already been copied | |
186 | m_image = image; | |
9e3e0821 VZ |
187 | |
188 | int x, y; | |
189 | int w, h; | |
190 | GetPosition(&x, &y); | |
191 | GetSize(&w, &h); | |
9e3e0821 VZ |
192 | |
193 | #ifdef __WIN32__ | |
0d0512bd | 194 | HANDLE handle = (HANDLE)m_image->GetHandle(); |
d8bffc13 MB |
195 | LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ; |
196 | ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) | | |
197 | ( m_isIcon ? SS_ICON : SS_BITMAP ) ); | |
0d0512bd | 198 | ::SendMessage(GetHwnd(), STM_SETIMAGE, |
9e3e0821 VZ |
199 | m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle); |
200 | #endif // Win32 | |
201 | ||
202 | if ( ImageIsOk() ) | |
203 | { | |
4004f41e VZ |
204 | int width = image->GetWidth(), |
205 | height = image->GetHeight(); | |
9e3e0821 VZ |
206 | if ( width && height ) |
207 | { | |
7c545786 VZ |
208 | w = width; |
209 | h = height; | |
210 | ||
0d0512bd | 211 | ::MoveWindow(GetHwnd(), x, y, width, height, FALSE); |
9e3e0821 VZ |
212 | } |
213 | } | |
214 | ||
0d0512bd VZ |
215 | RECT rect; |
216 | rect.left = x; | |
217 | rect.top = y; | |
218 | rect.right = x + w; | |
219 | rect.bottom = y + h; | |
220 | InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE); | |
2bda0e17 KB |
221 | } |
222 | ||
9e3e0821 VZ |
223 | // under Win32 we use the standard static control style for this |
224 | #ifdef __WIN16__ | |
2bda0e17 KB |
225 | bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item) |
226 | { | |
2bda0e17 KB |
227 | LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item; |
228 | ||
8f177c8e VZ |
229 | wxCHECK_MSG( !m_isIcon, FALSE, _T("icons not supported in wxStaticBitmap") ); |
230 | ||
231 | wxBitmap* bitmap = (wxBitmap *)m_image; | |
fd3f686c VZ |
232 | if ( !bitmap->Ok() ) |
233 | return FALSE; | |
2bda0e17 | 234 | |
fd3f686c VZ |
235 | HDC hDC = lpDIS->hDC; |
236 | HDC memDC = ::CreateCompatibleDC(hDC); | |
2bda0e17 | 237 | |
fd3f686c | 238 | HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP()); |
2bda0e17 | 239 | |
fd3f686c VZ |
240 | if (!old) |
241 | return FALSE; | |
2bda0e17 | 242 | |
9e3e0821 VZ |
243 | int x = lpDIS->rcItem.left; |
244 | int y = lpDIS->rcItem.top; | |
245 | int width = lpDIS->rcItem.right - x; | |
246 | int height = lpDIS->rcItem.bottom - y; | |
2bda0e17 | 247 | |
fd3f686c VZ |
248 | // Centre the bitmap in the control area |
249 | int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2)); | |
250 | int y1 = (int) (y + ((height - bitmap->GetHeight()) / 2)); | |
2bda0e17 | 251 | |
fd3f686c | 252 | ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY); |
2bda0e17 | 253 | |
fd3f686c | 254 | ::SelectObject(memDC, old); |
2bda0e17 KB |
255 | |
256 | ::DeleteDC(memDC); | |
257 | ||
258 | return TRUE; | |
259 | } | |
d1e418ea | 260 | #endif // Win16 |
2bda0e17 | 261 | |
d1e418ea | 262 | // We need this or the control can never be moved e.g. in Dialog Editor. |
9e3e0821 VZ |
263 | long wxStaticBitmap::MSWWindowProc(WXUINT nMsg, |
264 | WXWPARAM wParam, | |
265 | WXLPARAM lParam) | |
2bda0e17 | 266 | { |
9e3e0821 VZ |
267 | // Ensure that static items get messages. Some controls don't like this |
268 | // message to be intercepted (e.g. RichEdit), hence the tests. | |
269 | if ( nMsg == WM_NCHITTEST ) | |
270 | return (long)HTCLIENT; | |
2bda0e17 | 271 | |
9e3e0821 | 272 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); |
2bda0e17 | 273 | } |
d1e418ea | 274 | |
1e6feb95 | 275 | #endif // wxUSE_STATBMP |