]>
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 wxBorder
wxStaticBitmap::GetDefaultBorder() const
139 return wxBORDER_NONE
;
142 WXDWORD
wxStaticBitmap::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
144 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
147 // what kind of control are we?
148 msStyle
|= m_isIcon
? SS_ICON
: SS_BITMAP
;
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
;
154 msStyle
|= BS_OWNERDRAW
;
160 bool wxStaticBitmap::ImageIsOk() const
162 return m_image
&& m_image
->Ok();
165 void wxStaticBitmap::Free()
172 wxSize
wxStaticBitmap::DoGetBestSize() const
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();
179 void wxStaticBitmap::SetImage( const wxGDIImage
* image
)
181 wxGDIImage
* convertedImage
= ConvertImage( *image
);
182 SetImageNoCopy( convertedImage
);
185 void wxStaticBitmap::SetImageNoCopy( wxGDIImage
* image
)
189 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
190 // the image has already been copied
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
);
209 int width
= image
->GetWidth(),
210 height
= image
->GetHeight();
211 if ( width
&& height
)
216 ::MoveWindow(GetHwnd(), x
, y
, width
, height
, FALSE
);
225 InvalidateRect(GetHwndOf(GetParent()), &rect
, TRUE
);
228 // under Win32 we use the standard static control style for this
230 bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
232 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
234 wxCHECK_MSG( !m_isIcon
, FALSE
, _T("icons not supported in wxStaticBitmap") );
236 wxBitmap
* bitmap
= (wxBitmap
*)m_image
;
240 HDC hDC
= lpDIS
->hDC
;
241 HDC memDC
= ::CreateCompatibleDC(hDC
);
243 HBITMAP old
= (HBITMAP
) ::SelectObject(memDC
, (HBITMAP
) bitmap
->GetHBITMAP());
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
;
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));
257 ::BitBlt(hDC
, x1
, y1
, bitmap
->GetWidth(), bitmap
->GetHeight(), memDC
, 0, 0, SRCCOPY
);
259 ::SelectObject(memDC
, old
);
267 // We need this or the control can never be moved e.g. in Dialog Editor.
268 long wxStaticBitmap::MSWWindowProc(WXUINT nMsg
,
272 // Ensure that static items get messages. Some controls don't like this
273 // message to be intercepted (e.g. RichEdit), hence the tests.
274 if ( nMsg
== WM_NCHITTEST
)
275 return (long)HTCLIENT
;
277 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
280 #endif // wxUSE_STATBMP