]>
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 // default border for this control is none
102 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
104 style
|= wxBORDER_NONE
;
107 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
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
));
117 wxASSERT_MSG( !m_isIcon
, "Icons are not supported in wxStaticBitmap under WIN16." );
120 image
= ConvertImage( bitmap
);
121 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
124 // create the native control
125 if ( !MSWCreateControl(
133 // control creation failed
137 // no need to delete the new image
138 SetImageNoCopy(image
);
143 WXDWORD
wxStaticBitmap::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
145 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
148 // what kind of control are we?
149 msStyle
|= m_isIcon
? SS_ICON
: SS_BITMAP
;
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
;
155 msStyle
|= BS_OWNERDRAW
;
161 bool wxStaticBitmap::ImageIsOk() const
163 return m_image
&& m_image
->Ok();
166 void wxStaticBitmap::Free()
173 wxSize
wxStaticBitmap::DoGetBestSize() const
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();
180 void wxStaticBitmap::SetImage( const wxGDIImage
* image
)
182 wxGDIImage
* convertedImage
= ConvertImage( *image
);
183 SetImageNoCopy( convertedImage
);
186 void wxStaticBitmap::SetImageNoCopy( wxGDIImage
* image
)
190 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
191 // the image has already been copied
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
);
210 int width
= image
->GetWidth(),
211 height
= image
->GetHeight();
212 if ( width
&& height
)
217 ::MoveWindow(GetHwnd(), x
, y
, width
, height
, FALSE
);
226 InvalidateRect(GetHwndOf(GetParent()), &rect
, TRUE
);
229 // under Win32 we use the standard static control style for this
231 bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
233 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
235 wxCHECK_MSG( !m_isIcon
, FALSE
, _T("icons not supported in wxStaticBitmap") );
237 wxBitmap
* bitmap
= (wxBitmap
*)m_image
;
241 HDC hDC
= lpDIS
->hDC
;
242 HDC memDC
= ::CreateCompatibleDC(hDC
);
244 HBITMAP old
= (HBITMAP
) ::SelectObject(memDC
, (HBITMAP
) bitmap
->GetHBITMAP());
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
;
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));
258 ::BitBlt(hDC
, x1
, y1
, bitmap
->GetWidth(), bitmap
->GetHeight(), memDC
, 0, 0, SRCCOPY
);
260 ::SelectObject(memDC
, old
);
268 // We need this or the control can never be moved e.g. in Dialog Editor.
269 long wxStaticBitmap::MSWWindowProc(WXUINT nMsg
,
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
;
278 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
281 #endif // wxUSE_STATBMP