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