]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbmp.cpp
compilation fix
[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
9 // Licence: wxWindows licence
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,
94 wxWindowID id,
95 const wxGDIImage& bitmap,
96 const wxPoint& pos,
97 const wxSize& size,
98 long style,
99 const wxString& name)
100 {
101 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
102 return FALSE;
103
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
107 wxGDIImage *image = (wxGDIImage *)NULL;
108 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
109
110 #ifdef __WIN16__
111 wxASSERT_MSG( !m_isIcon, "Icons are not supported in wxStaticBitmap under WIN16." );
112 image = &bitmap;
113 #else // Win32
114 image = ConvertImage( bitmap );
115 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
116 #endif // Win16/32
117
118 // create the native control
119 if ( !MSWCreateControl(
120 #ifdef __WIN32__
121 _T("STATIC"),
122 #else // Win16
123 _T("BUTTON"),
124 #endif // Win32/16
125 wxEmptyString, pos, size) )
126 {
127 // control creation failed
128 return FALSE;
129 }
130
131 // no need to delete the new image
132 SetImageNoCopy(image);
133
134 return TRUE;
135 }
136
137 wxBorder wxStaticBitmap::GetDefaultBorder() const
138 {
139 return wxBORDER_NONE;
140 }
141
142 WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const
143 {
144 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
145
146 #ifdef __WIN32__
147 // what kind of control are we?
148 msStyle |= m_isIcon ? SS_ICON : SS_BITMAP;
149
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;
158 }
159
160 bool wxStaticBitmap::ImageIsOk() const
161 {
162 return m_image && m_image->Ok();
163 }
164
165 void wxStaticBitmap::Free()
166 {
167 delete m_image;
168
169 m_image = NULL;
170 }
171
172 wxSize wxStaticBitmap::DoGetBestSize() const
173 {
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();
177 }
178
179 void wxStaticBitmap::SetImage( const wxGDIImage* image )
180 {
181 wxGDIImage* convertedImage = ConvertImage( *image );
182 SetImageNoCopy( convertedImage );
183 }
184
185 void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image)
186 {
187 Free();
188
189 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
190 // the image has already been copied
191 m_image = image;
192
193 int x, y;
194 int w, h;
195 GetPosition(&x, &y);
196 GetSize(&w, &h);
197
198 #ifdef __WIN32__
199 HANDLE handle = (HANDLE)m_image->GetHandle();
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 ) );
203 ::SendMessage(GetHwnd(), STM_SETIMAGE,
204 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
205 #endif // Win32
206
207 if ( ImageIsOk() )
208 {
209 int width = image->GetWidth(),
210 height = image->GetHeight();
211 if ( width && height )
212 {
213 w = width;
214 h = height;
215
216 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
217 }
218 }
219
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);
226 }
227
228 // under Win32 we use the standard static control style for this
229 #ifdef __WIN16__
230 bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
231 {
232 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
233
234 wxCHECK_MSG( !m_isIcon, FALSE, _T("icons not supported in wxStaticBitmap") );
235
236 wxBitmap* bitmap = (wxBitmap *)m_image;
237 if ( !bitmap->Ok() )
238 return FALSE;
239
240 HDC hDC = lpDIS->hDC;
241 HDC memDC = ::CreateCompatibleDC(hDC);
242
243 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
244
245 if (!old)
246 return FALSE;
247
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;
252
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));
256
257 ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY);
258
259 ::SelectObject(memDC, old);
260
261 ::DeleteDC(memDC);
262
263 return TRUE;
264 }
265 #endif // Win16
266
267 // We need this or the control can never be moved e.g. in Dialog Editor.
268 long wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
269 WXWPARAM wParam,
270 WXLPARAM lParam)
271 {
272 #ifndef __WXWINCE__
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;
277 #endif
278
279 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
280 }
281
282 #endif // wxUSE_STATBMP