]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbmp.cpp
changes to match listctrl.h changes (forgot to commit yesterday, sorry)
[wxWidgets.git] / src / msw / statbmp.cpp
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
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "statbmp.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_STATBMP
32
33 #include "wx/window.h"
34 #include "wx/msw/private.h"
35
36 #ifndef WX_PRECOMP
37 #include "wx/icon.h"
38 #include "wx/statbmp.h"
39 #endif
40
41 #include <stdio.h>
42
43 // ---------------------------------------------------------------------------
44 // macors
45 // ---------------------------------------------------------------------------
46
47 IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
48
49 // ===========================================================================
50 // implementation
51 // ===========================================================================
52
53 // ---------------------------------------------------------------------------
54 // wxStaticBitmap
55 // ---------------------------------------------------------------------------
56
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
93 bool wxStaticBitmap::Create(wxWindow *parent, wxWindowID id,
94 const wxGDIImage& bitmap,
95 const wxPoint& pos,
96 const wxSize& size,
97 long style,
98 const wxString& name)
99 {
100 Init();
101
102 SetName(name);
103 if (parent)
104 parent->AddChild(this);
105
106 m_backgroundColour = parent->GetBackgroundColour() ;
107 m_foregroundColour = parent->GetForegroundColour() ;
108
109 if ( id == -1 )
110 m_windowId = (int)NewControlId();
111 else
112 m_windowId = id;
113
114 int x = pos.x;
115 int y = pos.y;
116 int width = size.x;
117 int height = size.y;
118
119 m_windowStyle = style;
120
121 // we may have either bitmap or icon: if a bitmap with mask is passed, we
122 // will transform it to an icon ourselves because otherwise the mask will
123 // be ignored by Windows
124 wxGDIImage *image = (wxGDIImage *)NULL;
125 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
126
127 #ifdef __WIN16__
128 wxASSERT_MSG( !m_isIcon, "Icons are not supported in wxStaticBitmap under WIN16." );
129 image = &bitmap;
130 #endif
131
132 #ifndef __WIN16__
133 image = ConvertImage( bitmap );
134 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
135 #endif
136
137 #ifdef __WIN32__
138 // create a static control with either SS_BITMAP or SS_ICON style depending
139 // on what we have here
140 const wxChar *classname = wxT("STATIC");
141 int winstyle = m_isIcon ? SS_ICON : SS_BITMAP;
142 #else // Win16
143 const wxChar *classname = wxT("BUTTON");
144 int winstyle = BS_OWNERDRAW;
145 #endif // Win32
146
147 if ( m_windowStyle & wxCLIP_SIBLINGS )
148 winstyle |= WS_CLIPSIBLINGS;
149
150
151 m_hWnd = (WXHWND)::CreateWindow
152 (
153 classname,
154 wxT(""),
155 // NOT DISABLED!!! We want to move it in Dialog Editor.
156 winstyle | WS_CHILD | WS_VISIBLE /* | WS_CLIPSIBLINGS */ , // | WS_DISABLED,
157 0, 0, 0, 0,
158 (HWND)parent->GetHWND(),
159 (HMENU)m_windowId,
160 wxGetInstance(),
161 NULL
162 );
163
164 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static bitmap") );
165
166 // no need to delete the new image
167 SetImageNoCopy( image );
168
169 // Subclass again for purposes of dialog editing mode
170 SubclassWin(m_hWnd);
171
172 SetFont(GetParent()->GetFont());
173
174 SetSize(x, y, width, height);
175
176 return TRUE;
177 }
178
179 bool wxStaticBitmap::ImageIsOk() const
180 {
181 return m_image && m_image->Ok();
182 }
183
184 void wxStaticBitmap::Free()
185 {
186 delete m_image;
187
188 m_image = NULL;
189 }
190
191 wxSize wxStaticBitmap::DoGetBestSize() const
192 {
193 // reuse the current size (as wxWindow does) instead of using some
194 // arbitrary default size (as wxControl, our immediate base class, does)
195 return wxWindow::DoGetBestSize();
196 }
197
198 void wxStaticBitmap::SetImage( const wxGDIImage* image )
199 {
200 wxGDIImage* convertedImage = ConvertImage( *image );
201 SetImageNoCopy( convertedImage );
202 }
203
204 void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image)
205 {
206 Free();
207
208 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
209 // the image has already been copied
210 m_image = image;
211
212 int x, y;
213 int w, h;
214 GetPosition(&x, &y);
215 GetSize(&w, &h);
216
217 #ifdef __WIN32__
218 HANDLE handle = (HANDLE)m_image->GetHandle();
219 LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ;
220 ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) |
221 ( m_isIcon ? SS_ICON : SS_BITMAP ) );
222 ::SendMessage(GetHwnd(), STM_SETIMAGE,
223 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
224 #endif // Win32
225
226 if ( ImageIsOk() )
227 {
228 int width = image->GetWidth(),
229 height = image->GetHeight();
230 if ( width && height )
231 {
232 w = width;
233 h = height;
234
235 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
236 }
237 }
238
239 RECT rect;
240 rect.left = x;
241 rect.top = y;
242 rect.right = x + w;
243 rect.bottom = y + h;
244 InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
245 }
246
247 // under Win32 we use the standard static control style for this
248 #ifdef __WIN16__
249 bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
250 {
251 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
252
253 wxCHECK_MSG( !m_isIcon, FALSE, _T("icons not supported in wxStaticBitmap") );
254
255 wxBitmap* bitmap = (wxBitmap *)m_image;
256 if ( !bitmap->Ok() )
257 return FALSE;
258
259 HDC hDC = lpDIS->hDC;
260 HDC memDC = ::CreateCompatibleDC(hDC);
261
262 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
263
264 if (!old)
265 return FALSE;
266
267 int x = lpDIS->rcItem.left;
268 int y = lpDIS->rcItem.top;
269 int width = lpDIS->rcItem.right - x;
270 int height = lpDIS->rcItem.bottom - y;
271
272 // Centre the bitmap in the control area
273 int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2));
274 int y1 = (int) (y + ((height - bitmap->GetHeight()) / 2));
275
276 ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY);
277
278 ::SelectObject(memDC, old);
279
280 ::DeleteDC(memDC);
281
282 return TRUE;
283 }
284 #endif // Win16
285
286 // We need this or the control can never be moved e.g. in Dialog Editor.
287 long wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
288 WXWPARAM wParam,
289 WXLPARAM lParam)
290 {
291 // Ensure that static items get messages. Some controls don't like this
292 // message to be intercepted (e.g. RichEdit), hence the tests.
293 if ( nMsg == WM_NCHITTEST )
294 return (long)HTCLIENT;
295
296 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
297 }
298
299 #endif // wxUSE_STATBMP