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