]>
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 // ---------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
)
54 // ===========================================================================
56 // ===========================================================================
58 // ---------------------------------------------------------------------------
60 // ---------------------------------------------------------------------------
62 // we may have either bitmap or icon: if a bitmap with mask is passed, we
63 // will transform it to an icon ourselves because otherwise the mask will
64 // be ignored by Windows
65 // note that this function will create a new object every time
66 // it is called even if the image needs no conversion
70 static wxGDIImage
* ConvertImage( const wxGDIImage
& bitmap
)
72 bool isIcon
= bitmap
.IsKindOf( CLASSINFO(wxIcon
) );
76 wxASSERT_MSG( wxDynamicCast(&bitmap
, wxBitmap
),
77 _T("not an icon and not a bitmap?") );
79 const wxBitmap
& bmp
= (const wxBitmap
&)bitmap
;
80 wxMask
*mask
= bmp
.GetMask();
81 if ( mask
&& mask
->GetMaskBitmap() )
83 wxIcon
* icon
= new wxIcon
;
84 icon
->CopyFromBitmap(bmp
);
89 return new wxBitmap( bmp
);
92 // copying a bitmap is a cheap operation
93 return new wxIcon( (const wxIcon
&)bitmap
);
98 bool wxStaticBitmap::Create(wxWindow
*parent
,
100 const wxGDIImage
& bitmap
,
104 const wxString
& name
)
106 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
109 // we may have either bitmap or icon: if a bitmap with mask is passed, we
110 // will transform it to an icon ourselves because otherwise the mask will
111 // be ignored by Windows
112 wxGDIImage
*image
= (wxGDIImage
*)NULL
;
113 m_isIcon
= bitmap
.IsKindOf(CLASSINFO(wxIcon
));
116 wxASSERT_MSG( !m_isIcon
, "Icons are not supported in wxStaticBitmap under WIN16." );
119 image
= ConvertImage( bitmap
);
120 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
123 // create the native control
124 if ( !MSWCreateControl(
130 wxEmptyString
, pos
, size
) )
132 // control creation failed
136 // no need to delete the new image
137 SetImageNoCopy(image
);
142 wxBorder
wxStaticBitmap::GetDefaultBorder() const
144 return wxBORDER_NONE
;
147 WXDWORD
wxStaticBitmap::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
149 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
152 // what kind of control are we?
153 msStyle
|= m_isIcon
? SS_ICON
: SS_BITMAP
;
155 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
156 // fit to its size -- this is unexpected and doesn't happen in other ports
157 msStyle
|= SS_CENTERIMAGE
;
159 msStyle
|= BS_OWNERDRAW
;
165 bool wxStaticBitmap::ImageIsOk() const
167 return m_image
&& m_image
->Ok();
170 void wxStaticBitmap::Free()
177 wxSize
wxStaticBitmap::DoGetBestSize() const
179 // reuse the current size (as wxWindow does) instead of using some
180 // arbitrary default size (as wxControl, our immediate base class, does)
181 return wxWindow::DoGetBestSize();
184 void wxStaticBitmap::SetImage( const wxGDIImage
* image
)
186 wxGDIImage
* convertedImage
= ConvertImage( *image
);
187 SetImageNoCopy( convertedImage
);
190 void wxStaticBitmap::SetImageNoCopy( wxGDIImage
* image
)
194 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
195 // the image has already been copied
204 HANDLE handle
= (HANDLE
)m_image
->GetHandle();
205 LONG style
= ::GetWindowLong( (HWND
)GetHWND(), GWL_STYLE
) ;
206 ::SetWindowLong( (HWND
)GetHWND(), GWL_STYLE
, ( style
& ~( SS_BITMAP
|SS_ICON
) ) |
207 ( m_isIcon
? SS_ICON
: SS_BITMAP
) );
208 ::SendMessage(GetHwnd(), STM_SETIMAGE
,
209 m_isIcon
? IMAGE_ICON
: IMAGE_BITMAP
, (LPARAM
)handle
);
214 int width
= image
->GetWidth(),
215 height
= image
->GetHeight();
216 if ( width
&& height
)
221 ::MoveWindow(GetHwnd(), x
, y
, width
, height
, FALSE
);
230 InvalidateRect(GetHwndOf(GetParent()), &rect
, TRUE
);
233 // under Win32 we use the standard static control style for this
235 bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
237 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
239 wxCHECK_MSG( !m_isIcon
, FALSE
, _T("icons not supported in wxStaticBitmap") );
241 wxBitmap
* bitmap
= (wxBitmap
*)m_image
;
245 HDC hDC
= lpDIS
->hDC
;
246 HDC memDC
= ::CreateCompatibleDC(hDC
);
248 HBITMAP old
= (HBITMAP
) ::SelectObject(memDC
, (HBITMAP
) bitmap
->GetHBITMAP());
253 int x
= lpDIS
->rcItem
.left
;
254 int y
= lpDIS
->rcItem
.top
;
255 int width
= lpDIS
->rcItem
.right
- x
;
256 int height
= lpDIS
->rcItem
.bottom
- y
;
258 // Centre the bitmap in the control area
259 int x1
= (int) (x
+ ((width
- bitmap
->GetWidth()) / 2));
260 int y1
= (int) (y
+ ((height
- bitmap
->GetHeight()) / 2));
262 ::BitBlt(hDC
, x1
, y1
, bitmap
->GetWidth(), bitmap
->GetHeight(), memDC
, 0, 0, SRCCOPY
);
264 ::SelectObject(memDC
, old
);
272 // We need this or the control can never be moved e.g. in Dialog Editor.
273 long wxStaticBitmap::MSWWindowProc(WXUINT nMsg
,
278 // Ensure that static items get messages. Some controls don't like this
279 // message to be intercepted (e.g. RichEdit), hence the tests.
280 if ( nMsg
== WM_NCHITTEST
)
281 return (long)HTCLIENT
;
284 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
287 #endif // wxUSE_STATBMP