]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/statbmp.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxStaticBitmap
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "statbmp.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/window.h"
34 #include "wx/msw/private.h"
38 #include "wx/statbmp.h"
43 // ---------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------
47 IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap
, wxControl
)
49 // ===========================================================================
51 // ===========================================================================
53 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
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
65 static wxGDIImage
* ConvertImage( const wxGDIImage
& bitmap
)
67 bool isIcon
= bitmap
.IsKindOf( CLASSINFO(wxIcon
) );
71 wxASSERT_MSG( wxDynamicCast(&bitmap
, wxBitmap
),
72 _T("not an icon and not a bitmap?") );
74 const wxBitmap
& bmp
= (const wxBitmap
&)bitmap
;
75 wxMask
*mask
= bmp
.GetMask();
76 if ( mask
&& mask
->GetMaskBitmap() )
78 wxIcon
* icon
= new wxIcon
;
79 icon
->CopyFromBitmap(bmp
);
84 return new wxBitmap( bmp
);
87 // copying a bitmap is a cheap operation
88 return new wxIcon( (const wxIcon
&)bitmap
);
93 bool wxStaticBitmap::Create(wxWindow
*parent
,
95 const wxGDIImage
& bitmap
,
101 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
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
));
111 wxASSERT_MSG( !m_isIcon
, "Icons are not supported in wxStaticBitmap under WIN16." );
114 image
= ConvertImage( bitmap
);
115 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
118 // create the native control
119 if ( !MSWCreateControl(
127 // control creation failed
131 // no need to delete the new image
132 SetImageNoCopy(image
);
137 WXDWORD
wxStaticBitmap::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
139 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
142 // what kind of control are we?
143 msStyle
|= m_isIcon
? SS_ICON
: SS_BITMAP
;
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
;
149 msStyle
|= BS_OWNERDRAW
;
155 bool wxStaticBitmap::ImageIsOk() const
157 return m_image
&& m_image
->Ok();
160 void wxStaticBitmap::Free()
167 wxSize
wxStaticBitmap::DoGetBestSize() const
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();
174 void wxStaticBitmap::SetImage( const wxGDIImage
* image
)
176 wxGDIImage
* convertedImage
= ConvertImage( *image
);
177 SetImageNoCopy( convertedImage
);
180 void wxStaticBitmap::SetImageNoCopy( wxGDIImage
* image
)
184 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
185 // the image has already been copied
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
);
204 int width
= image
->GetWidth(),
205 height
= image
->GetHeight();
206 if ( width
&& height
)
211 ::MoveWindow(GetHwnd(), x
, y
, width
, height
, FALSE
);
220 InvalidateRect(GetHwndOf(GetParent()), &rect
, TRUE
);
223 // under Win32 we use the standard static control style for this
225 bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
227 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
229 wxCHECK_MSG( !m_isIcon
, FALSE
, _T("icons not supported in wxStaticBitmap") );
231 wxBitmap
* bitmap
= (wxBitmap
*)m_image
;
235 HDC hDC
= lpDIS
->hDC
;
236 HDC memDC
= ::CreateCompatibleDC(hDC
);
238 HBITMAP old
= (HBITMAP
) ::SelectObject(memDC
, (HBITMAP
) bitmap
->GetHBITMAP());
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
;
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));
252 ::BitBlt(hDC
, x1
, y1
, bitmap
->GetWidth(), bitmap
->GetHeight(), memDC
, 0, 0, SRCCOPY
);
254 ::SelectObject(memDC
, old
);
262 // We need this or the control can never be moved e.g. in Dialog Editor.
263 long wxStaticBitmap::MSWWindowProc(WXUINT nMsg
,
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
;
272 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
275 #endif // wxUSE_STATBMP