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