]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/statbmp.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/statbmp.cpp
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/statbmp.h"
32 #include "wx/window.h"
36 #include "wx/msw/private.h"
38 #include "wx/sysopt.h"
42 // ---------------------------------------------------------------------------
44 // ---------------------------------------------------------------------------
46 #if wxUSE_EXTENDED_RTTI
47 WX_DEFINE_FLAGS( wxStaticBitmapStyle
)
49 wxBEGIN_FLAGS( wxStaticBitmapStyle
)
50 // new style border flags, we put them first to
51 // use them for streaming out
52 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
53 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
54 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
55 wxFLAGS_MEMBER(wxBORDER_RAISED
)
56 wxFLAGS_MEMBER(wxBORDER_STATIC
)
57 wxFLAGS_MEMBER(wxBORDER_NONE
)
59 // old style border flags
60 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
61 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
62 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
63 wxFLAGS_MEMBER(wxRAISED_BORDER
)
64 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
65 wxFLAGS_MEMBER(wxBORDER
)
67 // standard window styles
68 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
69 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
70 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
71 wxFLAGS_MEMBER(wxWANTS_CHARS
)
72 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
73 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
74 wxFLAGS_MEMBER(wxVSCROLL
)
75 wxFLAGS_MEMBER(wxHSCROLL
)
77 wxEND_FLAGS( wxStaticBitmapStyle
)
79 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBitmap
, wxControl
,"wx/statbmp.h")
81 wxBEGIN_PROPERTIES_TABLE(wxStaticBitmap
)
82 wxPROPERTY_FLAGS( WindowStyle
, wxStaticBitmapStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
83 wxEND_PROPERTIES_TABLE()
85 wxBEGIN_HANDLERS_TABLE(wxStaticBitmap
)
86 wxEND_HANDLERS_TABLE()
88 wxCONSTRUCTOR_5( wxStaticBitmap
, wxWindow
* , Parent
, wxWindowID
, Id
, wxBitmap
, Bitmap
, wxPoint
, Position
, wxSize
, Size
)
91 IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap
, wxControl
)
99 // ===========================================================================
101 // ===========================================================================
103 // ---------------------------------------------------------------------------
105 // ---------------------------------------------------------------------------
107 // we may have either bitmap or icon: if a bitmap with mask is passed, we
108 // will transform it to an icon ourselves because otherwise the mask will
109 // be ignored by Windows
110 // note that this function will create a new object every time
111 // it is called even if the image needs no conversion
113 static wxGDIImage
* ConvertImage( const wxGDIImage
& bitmap
)
115 bool isIcon
= bitmap
.IsKindOf( CLASSINFO(wxIcon
) );
119 wxASSERT_MSG( wxDynamicCast(&bitmap
, wxBitmap
),
120 _T("not an icon and not a bitmap?") );
122 const wxBitmap
& bmp
= (const wxBitmap
&)bitmap
;
123 wxMask
*mask
= bmp
.GetMask();
124 if ( mask
&& mask
->GetMaskBitmap() )
126 wxIcon
* icon
= new wxIcon
;
127 icon
->CopyFromBitmap(bmp
);
132 return new wxBitmap( bmp
);
135 // copying a bitmap is a cheap operation
136 return new wxIcon( (const wxIcon
&)bitmap
);
139 bool wxStaticBitmap::Create(wxWindow
*parent
,
141 const wxGDIImage
& bitmap
,
145 const wxString
& name
)
147 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
150 // we may have either bitmap or icon: if a bitmap with mask is passed, we
151 // will transform it to an icon ourselves because otherwise the mask will
152 // be ignored by Windows
153 m_isIcon
= bitmap
.IsKindOf(CLASSINFO(wxIcon
));
155 wxGDIImage
*image
= ConvertImage( bitmap
);
156 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
158 // create the native control
159 if ( !MSWCreateControl(_T("STATIC"), wxEmptyString
, pos
, size
) )
161 // control creation failed
165 // no need to delete the new image
166 SetImageNoCopy(image
);
168 // GetBestSize will work properly now, so set the best size if needed
169 SetInitialSize(size
);
171 // Win9x and 2000 don't draw correctly the images with alpha channel so we
172 // need to draw them ourselves and it's easier to just always do it rather
173 // than check if we have an image with alpha or not
174 if ( wxGetWinVersion() <= wxWinVersion_2000
)
176 Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBitmap::DoPaintManually
));
182 WXDWORD
wxStaticBitmap::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
184 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
186 // what kind of control are we?
187 msStyle
|= m_isIcon
? SS_ICON
: SS_BITMAP
;
189 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
190 // fit to its size -- this is unexpected and doesn't happen in other ports
192 // and SS_NOTIFY is necessary to receive mouse events
193 msStyle
|= SS_CENTERIMAGE
| SS_NOTIFY
;
198 bool wxStaticBitmap::ImageIsOk() const
200 return m_image
&& m_image
->Ok();
203 wxIcon
wxStaticBitmap::GetIcon() const
205 wxCHECK_MSG( m_image
, wxIcon(), _T("no image in wxStaticBitmap") );
207 // we can't ask for an icon if all we have is a bitmap
208 wxCHECK_MSG( m_isIcon
, wxIcon(), _T("no icon in this wxStaticBitmap") );
210 return *(wxIcon
*)m_image
;
213 wxBitmap
wxStaticBitmap::GetBitmap() const
217 // don't fail because we might have replaced the bitmap with icon
218 // ourselves internally in ConvertImage() to keep the transparency but
219 // the user code doesn't know about it so it still can use GetBitmap()
220 // to retrieve the bitmap
221 return wxBitmap(GetIcon());
223 else // we have a bitmap
225 wxCHECK_MSG( m_image
, wxBitmap(), _T("no image in wxStaticBitmap") );
227 return *(wxBitmap
*)m_image
;
231 void wxStaticBitmap::Free()
238 wxSize
wxStaticBitmap::DoGetBestSize() const
242 wxSize
best(m_image
->GetWidth(), m_image
->GetHeight());
247 // this is completely arbitrary
248 return wxSize(16, 16);
251 void wxStaticBitmap::DoPaintManually(wxPaintEvent
& WXUNUSED(event
))
255 const wxSize
size(GetSize());
256 const wxBitmap
bmp(GetBitmap());
258 // Clear the background
259 dc
.SetBrush(GetBackgroundColour());
260 dc
.SetPen(*wxTRANSPARENT_PEN
);
261 dc
.DrawRectangle(0, 0, size
.GetWidth(), size
.GetHeight());
263 // Draw the image in the middle
265 (size
.GetWidth() - bmp
.GetWidth()) / 2,
266 (size
.GetHeight() - bmp
.GetHeight()) / 2,
267 true /* use mask */);
270 void wxStaticBitmap::SetImage( const wxGDIImage
* image
)
272 wxGDIImage
* convertedImage
= ConvertImage( *image
);
273 SetImageNoCopy( convertedImage
);
274 InvalidateBestSize();
277 void wxStaticBitmap::SetImageNoCopy( wxGDIImage
* image
)
281 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
282 // the image has already been copied
291 HANDLE handle
= (HANDLE
)m_image
->GetHandle();
292 LONG style
= ::GetWindowLong( (HWND
)GetHWND(), GWL_STYLE
) ;
293 ::SetWindowLong( (HWND
)GetHWND(), GWL_STYLE
, ( style
& ~( SS_BITMAP
|SS_ICON
) ) |
294 ( m_isIcon
? SS_ICON
: SS_BITMAP
) );
295 HGDIOBJ oldHandle
= (HGDIOBJ
)::SendMessage(GetHwnd(), STM_SETIMAGE
,
296 m_isIcon
? IMAGE_ICON
: IMAGE_BITMAP
, (LPARAM
)handle
);
297 // detect if this is still the handle we passed before or
298 // if the static-control made a copy of the bitmap!
299 if (m_currentHandle
!= 0 && oldHandle
!= (HGDIOBJ
) m_currentHandle
)
301 // the static control made a copy and we are responsible for deleting it
302 DeleteObject((HGDIOBJ
) oldHandle
);
304 m_currentHandle
= (WXHANDLE
)handle
;
309 int width
= image
->GetWidth(),
310 height
= image
->GetHeight();
311 if ( width
&& height
)
316 ::MoveWindow(GetHwnd(), x
, y
, width
, height
, FALSE
);
325 ::InvalidateRect(GetHwndOf(GetParent()), &rect
, TRUE
);
328 #endif // wxUSE_STATBMP