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