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 #if wxUSE_EXTENDED_RTTI
48 WX_DEFINE_FLAGS( wxStaticBitmapStyle
)
50 WX_BEGIN_FLAGS( wxStaticBitmapStyle
)
51 // new style border flags, we put them first to
52 // use them for streaming out
53 WX_FLAGS_MEMBER(wxBORDER_SIMPLE
)
54 WX_FLAGS_MEMBER(wxBORDER_SUNKEN
)
55 WX_FLAGS_MEMBER(wxBORDER_DOUBLE
)
56 WX_FLAGS_MEMBER(wxBORDER_RAISED
)
57 WX_FLAGS_MEMBER(wxBORDER_STATIC
)
58 WX_FLAGS_MEMBER(wxBORDER_NONE
)
60 // old style border flags
61 WX_FLAGS_MEMBER(wxSIMPLE_BORDER
)
62 WX_FLAGS_MEMBER(wxSUNKEN_BORDER
)
63 WX_FLAGS_MEMBER(wxDOUBLE_BORDER
)
64 WX_FLAGS_MEMBER(wxRAISED_BORDER
)
65 WX_FLAGS_MEMBER(wxSTATIC_BORDER
)
66 WX_FLAGS_MEMBER(wxNO_BORDER
)
68 // standard window styles
69 WX_FLAGS_MEMBER(wxTAB_TRAVERSAL
)
70 WX_FLAGS_MEMBER(wxCLIP_CHILDREN
)
71 WX_FLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
72 WX_FLAGS_MEMBER(wxWANTS_CHARS
)
73 WX_FLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
74 WX_FLAGS_MEMBER(wxALWAYS_SHOW_SB
)
75 WX_FLAGS_MEMBER(wxVSCROLL
)
76 WX_FLAGS_MEMBER(wxHSCROLL
)
78 WX_END_FLAGS( wxStaticBitmapStyle
)
80 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBitmap
, wxControl
,"wx/statbmp.h")
82 WX_BEGIN_PROPERTIES_TABLE(wxStaticBitmap
)
83 WX_PROPERTY_FLAGS( WindowStyle
, wxStaticBitmapStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
84 WX_END_PROPERTIES_TABLE()
86 WX_BEGIN_HANDLERS_TABLE(wxStaticBitmap
)
87 WX_END_HANDLERS_TABLE()
89 WX_CONSTRUCTOR_5( wxStaticBitmap
, wxWindow
* , Parent
, wxWindowID
, Id
, wxBitmap
, Bitmap
, wxPoint
, Position
, wxSize
, Size
)
92 IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap
, wxControl
)
100 // ===========================================================================
102 // ===========================================================================
104 // ---------------------------------------------------------------------------
106 // ---------------------------------------------------------------------------
108 // we may have either bitmap or icon: if a bitmap with mask is passed, we
109 // will transform it to an icon ourselves because otherwise the mask will
110 // be ignored by Windows
111 // note that this function will create a new object every time
112 // it is called even if the image needs no conversion
116 static wxGDIImage
* ConvertImage( const wxGDIImage
& bitmap
)
118 bool isIcon
= bitmap
.IsKindOf( CLASSINFO(wxIcon
) );
122 wxASSERT_MSG( wxDynamicCast(&bitmap
, wxBitmap
),
123 _T("not an icon and not a bitmap?") );
125 const wxBitmap
& bmp
= (const wxBitmap
&)bitmap
;
126 wxMask
*mask
= bmp
.GetMask();
127 if ( mask
&& mask
->GetMaskBitmap() )
129 wxIcon
* icon
= new wxIcon
;
130 icon
->CopyFromBitmap(bmp
);
135 return new wxBitmap( bmp
);
138 // copying a bitmap is a cheap operation
139 return new wxIcon( (const wxIcon
&)bitmap
);
144 bool wxStaticBitmap::Create(wxWindow
*parent
,
146 const wxGDIImage
& bitmap
,
150 const wxString
& name
)
152 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
155 // we may have either bitmap or icon: if a bitmap with mask is passed, we
156 // will transform it to an icon ourselves because otherwise the mask will
157 // be ignored by Windows
158 wxGDIImage
*image
= (wxGDIImage
*)NULL
;
159 m_isIcon
= bitmap
.IsKindOf(CLASSINFO(wxIcon
));
162 wxASSERT_MSG( !m_isIcon
, "Icons are not supported in wxStaticBitmap under WIN16." );
165 image
= ConvertImage( bitmap
);
166 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
169 // create the native control
170 if ( !MSWCreateControl(
176 wxEmptyString
, pos
, size
) )
178 // control creation failed
182 // no need to delete the new image
183 SetImageNoCopy(image
);
188 wxBorder
wxStaticBitmap::GetDefaultBorder() const
190 return wxBORDER_NONE
;
193 WXDWORD
wxStaticBitmap::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
195 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
198 // what kind of control are we?
199 msStyle
|= m_isIcon
? SS_ICON
: SS_BITMAP
;
201 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
202 // fit to its size -- this is unexpected and doesn't happen in other ports
203 msStyle
|= SS_CENTERIMAGE
;
205 msStyle
|= BS_OWNERDRAW
;
211 bool wxStaticBitmap::ImageIsOk() const
213 return m_image
&& m_image
->Ok();
216 void wxStaticBitmap::Free()
223 wxSize
wxStaticBitmap::DoGetBestSize() const
225 // reuse the current size (as wxWindow does) instead of using some
226 // arbitrary default size (as wxControl, our immediate base class, does)
227 return wxWindow::DoGetBestSize();
230 void wxStaticBitmap::SetImage( const wxGDIImage
* image
)
232 wxGDIImage
* convertedImage
= ConvertImage( *image
);
233 SetImageNoCopy( convertedImage
);
236 void wxStaticBitmap::SetImageNoCopy( wxGDIImage
* image
)
240 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
241 // the image has already been copied
250 HANDLE handle
= (HANDLE
)m_image
->GetHandle();
251 LONG style
= ::GetWindowLong( (HWND
)GetHWND(), GWL_STYLE
) ;
252 ::SetWindowLong( (HWND
)GetHWND(), GWL_STYLE
, ( style
& ~( SS_BITMAP
|SS_ICON
) ) |
253 ( m_isIcon
? SS_ICON
: SS_BITMAP
) );
254 ::SendMessage(GetHwnd(), STM_SETIMAGE
,
255 m_isIcon
? IMAGE_ICON
: IMAGE_BITMAP
, (LPARAM
)handle
);
260 int width
= image
->GetWidth(),
261 height
= image
->GetHeight();
262 if ( width
&& height
)
267 ::MoveWindow(GetHwnd(), x
, y
, width
, height
, FALSE
);
276 InvalidateRect(GetHwndOf(GetParent()), &rect
, TRUE
);
279 // under Win32 we use the standard static control style for this
281 bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
283 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
285 wxCHECK_MSG( !m_isIcon
, FALSE
, _T("icons not supported in wxStaticBitmap") );
287 wxBitmap
* bitmap
= (wxBitmap
*)m_image
;
291 HDC hDC
= lpDIS
->hDC
;
292 HDC memDC
= ::CreateCompatibleDC(hDC
);
294 HBITMAP old
= (HBITMAP
) ::SelectObject(memDC
, (HBITMAP
) bitmap
->GetHBITMAP());
299 int x
= lpDIS
->rcItem
.left
;
300 int y
= lpDIS
->rcItem
.top
;
301 int width
= lpDIS
->rcItem
.right
- x
;
302 int height
= lpDIS
->rcItem
.bottom
- y
;
304 // Centre the bitmap in the control area
305 int x1
= (int) (x
+ ((width
- bitmap
->GetWidth()) / 2));
306 int y1
= (int) (y
+ ((height
- bitmap
->GetHeight()) / 2));
308 ::BitBlt(hDC
, x1
, y1
, bitmap
->GetWidth(), bitmap
->GetHeight(), memDC
, 0, 0, SRCCOPY
);
310 ::SelectObject(memDC
, old
);
318 // We need this or the control can never be moved e.g. in Dialog Editor.
319 long wxStaticBitmap::MSWWindowProc(WXUINT nMsg
,
324 // Ensure that static items get messages. Some controls don't like this
325 // message to be intercepted (e.g. RichEdit), hence the tests.
326 if ( nMsg
== WM_NCHITTEST
)
327 return (long)HTCLIENT
;
330 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
333 #endif // wxUSE_STATBMP