]>
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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/window.h"
30 #include "wx/msw/private.h"
34 #include "wx/statbmp.h"
37 #include "wx/sysopt.h"
41 // ---------------------------------------------------------------------------
43 // ---------------------------------------------------------------------------
45 #if wxUSE_EXTENDED_RTTI
46 WX_DEFINE_FLAGS( wxStaticBitmapStyle
)
48 wxBEGIN_FLAGS( wxStaticBitmapStyle
)
49 // new style border flags, we put them first to
50 // use them for streaming out
51 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
52 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
53 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
54 wxFLAGS_MEMBER(wxBORDER_RAISED
)
55 wxFLAGS_MEMBER(wxBORDER_STATIC
)
56 wxFLAGS_MEMBER(wxBORDER_NONE
)
58 // old style border flags
59 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
60 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
61 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
62 wxFLAGS_MEMBER(wxRAISED_BORDER
)
63 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
64 wxFLAGS_MEMBER(wxBORDER
)
66 // standard window styles
67 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
68 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
69 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
70 wxFLAGS_MEMBER(wxWANTS_CHARS
)
71 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
72 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
73 wxFLAGS_MEMBER(wxVSCROLL
)
74 wxFLAGS_MEMBER(wxHSCROLL
)
76 wxEND_FLAGS( wxStaticBitmapStyle
)
78 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBitmap
, wxControl
,"wx/statbmp.h")
80 wxBEGIN_PROPERTIES_TABLE(wxStaticBitmap
)
81 wxPROPERTY_FLAGS( WindowStyle
, wxStaticBitmapStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
82 wxEND_PROPERTIES_TABLE()
84 wxBEGIN_HANDLERS_TABLE(wxStaticBitmap
)
85 wxEND_HANDLERS_TABLE()
87 wxCONSTRUCTOR_5( wxStaticBitmap
, wxWindow
* , Parent
, wxWindowID
, Id
, wxBitmap
, Bitmap
, wxPoint
, Position
, wxSize
, Size
)
90 IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap
, wxControl
)
98 // ===========================================================================
100 // ===========================================================================
102 // ---------------------------------------------------------------------------
104 // ---------------------------------------------------------------------------
106 // we may have either bitmap or icon: if a bitmap with mask is passed, we
107 // will transform it to an icon ourselves because otherwise the mask will
108 // be ignored by Windows
109 // note that this function will create a new object every time
110 // it is called even if the image needs no conversion
112 static wxGDIImage
* ConvertImage( const wxGDIImage
& bitmap
)
114 bool isIcon
= bitmap
.IsKindOf( CLASSINFO(wxIcon
) );
118 wxASSERT_MSG( wxDynamicCast(&bitmap
, wxBitmap
),
119 _T("not an icon and not a bitmap?") );
121 const wxBitmap
& bmp
= (const wxBitmap
&)bitmap
;
122 wxMask
*mask
= bmp
.GetMask();
123 if ( mask
&& mask
->GetMaskBitmap() )
125 wxIcon
* icon
= new wxIcon
;
126 icon
->CopyFromBitmap(bmp
);
131 return new wxBitmap( bmp
);
134 // copying a bitmap is a cheap operation
135 return new wxIcon( (const wxIcon
&)bitmap
);
138 bool wxStaticBitmap::Create(wxWindow
*parent
,
140 const wxGDIImage
& bitmap
,
144 const wxString
& name
)
146 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
149 // we may have either bitmap or icon: if a bitmap with mask is passed, we
150 // will transform it to an icon ourselves because otherwise the mask will
151 // be ignored by Windows
152 m_isIcon
= bitmap
.IsKindOf(CLASSINFO(wxIcon
));
154 wxGDIImage
*image
= ConvertImage( bitmap
);
155 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
157 // create the native control
158 if ( !MSWCreateControl(_T("STATIC"), wxEmptyString
, pos
, size
) )
160 // control creation failed
164 // no need to delete the new image
165 SetImageNoCopy(image
);
167 // GetBestSize will work properly now, so set the best size if needed
173 wxBorder
wxStaticBitmap::GetDefaultBorder() const
175 return wxBORDER_NONE
;
178 WXDWORD
wxStaticBitmap::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
180 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
182 // what kind of control are we?
183 msStyle
|= m_isIcon
? SS_ICON
: SS_BITMAP
;
185 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
186 // fit to its size -- this is unexpected and doesn't happen in other ports
188 // and SS_NOTIFY is necessary to receive mouse events
189 msStyle
|= SS_CENTERIMAGE
| SS_NOTIFY
;
194 bool wxStaticBitmap::ImageIsOk() const
196 return m_image
&& m_image
->Ok();
199 wxIcon
wxStaticBitmap::GetIcon() const
201 wxCHECK_MSG( m_image
, wxIcon(), _T("no image in wxStaticBitmap") );
203 // we can't ask for an icon if all we have is a bitmap
204 wxCHECK_MSG( m_isIcon
, wxIcon(), _T("no icon in this wxStaticBitmap") );
206 return *(wxIcon
*)m_image
;
209 wxBitmap
wxStaticBitmap::GetBitmap() const
213 // don't fail because we might have replaced the bitmap with icon
214 // ourselves internally in ConvertImage() to keep the transparency but
215 // the user code doesn't know about it so it still can use GetBitmap()
216 // to retrieve the bitmap
217 return wxBitmap(GetIcon());
219 else // we have a bitmap
221 wxCHECK_MSG( m_image
, wxBitmap(), _T("no image in wxStaticBitmap") );
223 return *(wxBitmap
*)m_image
;
227 void wxStaticBitmap::Free()
234 wxSize
wxStaticBitmap::DoGetBestSize() const
238 wxSize
best(m_image
->GetWidth(), m_image
->GetHeight());
243 // this is completely arbitrary
244 return wxSize(16, 16);
247 void wxStaticBitmap::SetImage( const wxGDIImage
* image
)
249 wxGDIImage
* convertedImage
= ConvertImage( *image
);
250 SetImageNoCopy( convertedImage
);
251 InvalidateBestSize();
254 void wxStaticBitmap::SetImageNoCopy( wxGDIImage
* image
)
258 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
259 // the image has already been copied
268 HANDLE handle
= (HANDLE
)m_image
->GetHandle();
269 LONG style
= ::GetWindowLong( (HWND
)GetHWND(), GWL_STYLE
) ;
270 ::SetWindowLong( (HWND
)GetHWND(), GWL_STYLE
, ( style
& ~( SS_BITMAP
|SS_ICON
) ) |
271 ( m_isIcon
? SS_ICON
: SS_BITMAP
) );
272 ::SendMessage(GetHwnd(), STM_SETIMAGE
,
273 m_isIcon
? IMAGE_ICON
: IMAGE_BITMAP
, (LPARAM
)handle
);
278 int width
= image
->GetWidth(),
279 height
= image
->GetHeight();
280 if ( width
&& height
)
285 ::MoveWindow(GetHwnd(), x
, y
, width
, height
, FALSE
);
294 ::InvalidateRect(GetHwndOf(GetParent()), &rect
, TRUE
);
297 #endif // wxUSE_STATBMP