]>
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"
33 #include "wx/window.h"
35 #include "wx/dcclient.h"
38 #include "wx/msw/private.h"
40 #include "wx/sysopt.h"
44 // ---------------------------------------------------------------------------
46 // ---------------------------------------------------------------------------
48 wxBEGIN_EVENT_TABLE(wxStaticBitmap
, wxStaticBitmapBase
)
49 EVT_SIZE(wxStaticBitmap::WXHandleSize
)
52 // ===========================================================================
54 // ===========================================================================
56 // ---------------------------------------------------------------------------
58 // ---------------------------------------------------------------------------
60 // we may have either bitmap or icon: if a bitmap with mask is passed, we
61 // will transform it to an icon ourselves because otherwise the mask will
62 // be ignored by Windows
63 // note that this function will create a new object every time
64 // it is called even if the image needs no conversion
66 static wxGDIImage
* ConvertImage( const wxGDIImage
& bitmap
)
68 bool isIcon
= bitmap
.IsKindOf( wxCLASSINFO(wxIcon
) );
72 wxASSERT_MSG( wxDynamicCast(&bitmap
, wxBitmap
),
73 wxT("not an icon and not a bitmap?") );
75 const wxBitmap
& bmp
= (const wxBitmap
&)bitmap
;
76 wxMask
*mask
= bmp
.GetMask();
77 if ( mask
&& mask
->GetMaskBitmap() )
79 wxIcon
* icon
= new wxIcon
;
80 icon
->CopyFromBitmap(bmp
);
85 return new wxBitmap( bmp
);
88 // copying a bitmap is a cheap operation
89 return new wxIcon( (const wxIcon
&)bitmap
);
92 bool wxStaticBitmap::Create(wxWindow
*parent
,
94 const wxGDIImage
& bitmap
,
100 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
103 // we may have either bitmap or icon: if a bitmap with mask is passed, we
104 // will transform it to an icon ourselves because otherwise the mask will
105 // be ignored by Windows
106 m_isIcon
= bitmap
.IsKindOf(wxCLASSINFO(wxIcon
));
108 wxGDIImage
*image
= ConvertImage( bitmap
);
109 m_isIcon
= image
->IsKindOf( wxCLASSINFO(wxIcon
) );
111 // create the native control
112 if ( !MSWCreateControl(wxT("STATIC"), wxEmptyString
, pos
, size
) )
114 // control creation failed
118 // no need to delete the new image
119 SetImageNoCopy(image
);
121 // GetBestSize will work properly now, so set the best size if needed
122 SetInitialSize(size
);
124 // painting manually is reported not to work under Windows CE (see #10093),
125 // so don't do it there even if this probably means that alpha is not
126 // supported there -- but at least bitmaps without alpha appear correctly
128 // Windows versions before XP (and even XP if the application has no
129 // manifest and so the old comctl32.dll is used) don't draw correctly the
130 // images with alpha channel so we need to draw them ourselves and it's
131 // easier to just always do it rather than check if we have an image with
133 if ( wxTheApp
->GetComCtl32Version() < 600 )
135 Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBitmap::DoPaintManually
));
137 #endif // !__WXWINCE__
142 WXDWORD
wxStaticBitmap::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
144 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
146 // what kind of control are we?
147 msStyle
|= m_isIcon
? SS_ICON
: SS_BITMAP
;
149 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
150 // fit to its size -- this is unexpected and doesn't happen in other ports
152 // and SS_NOTIFY is necessary to receive mouse events
153 msStyle
|= SS_CENTERIMAGE
| SS_NOTIFY
;
158 bool wxStaticBitmap::ImageIsOk() const
160 return m_image
&& m_image
->IsOk();
163 wxIcon
wxStaticBitmap::GetIcon() const
165 wxCHECK_MSG( m_image
, wxIcon(), wxT("no image in wxStaticBitmap") );
167 // we can't ask for an icon if all we have is a bitmap
168 wxCHECK_MSG( m_isIcon
, wxIcon(), wxT("no icon in this wxStaticBitmap") );
170 return *(wxIcon
*)m_image
;
173 wxBitmap
wxStaticBitmap::GetBitmap() const
177 // don't fail because we might have replaced the bitmap with icon
178 // ourselves internally in ConvertImage() to keep the transparency but
179 // the user code doesn't know about it so it still can use GetBitmap()
180 // to retrieve the bitmap
181 return wxBitmap(GetIcon());
183 else // we have a bitmap
185 wxCHECK_MSG( m_image
, wxBitmap(), wxT("no image in wxStaticBitmap") );
187 return *(wxBitmap
*)m_image
;
191 void wxStaticBitmap::Free()
196 wxSize
wxStaticBitmap::DoGetBestClientSize() const
201 size
= m_image
->GetSize();
205 // this is completely arbitrary
213 void wxStaticBitmap::WXHandleSize(wxSizeEvent
& event
)
215 // Invalidate everything when our size changes as the image position (it's
216 // drawn centred in the window client area) changes.
224 void wxStaticBitmap::DoPaintManually(wxPaintEvent
& WXUNUSED(event
))
228 const wxSize
size(GetSize());
229 const wxBitmap
bmp(GetBitmap());
231 // Clear the background: notice that we're supposed to be transparent, so
232 // use the parent background colour if we don't have our own instead of
233 // falling back to the default
234 const wxWindow
*win
= UseBgCol() ? this : GetParent();
235 dc
.SetBrush(win
->GetBackgroundColour());
236 dc
.SetPen(*wxTRANSPARENT_PEN
);
237 dc
.DrawRectangle(0, 0, size
.GetWidth(), size
.GetHeight());
239 // Draw the image in the middle
241 (size
.GetWidth() - bmp
.GetWidth()) / 2,
242 (size
.GetHeight() - bmp
.GetHeight()) / 2,
243 true /* use mask */);
246 #endif // !__WXWINCE__
248 void wxStaticBitmap::SetImage( const wxGDIImage
* image
)
250 wxGDIImage
* convertedImage
= ConvertImage( *image
);
251 SetImageNoCopy( convertedImage
);
254 void wxStaticBitmap::SetImageNoCopy( wxGDIImage
* image
)
257 InvalidateBestSize();
259 m_isIcon
= image
->IsKindOf( wxCLASSINFO(wxIcon
) );
260 // the image has already been copied
269 HANDLE handle
= (HANDLE
)m_image
->GetHandle();
270 LONG style
= ::GetWindowLong( (HWND
)GetHWND(), GWL_STYLE
) ;
271 ::SetWindowLong( (HWND
)GetHWND(), GWL_STYLE
, ( style
& ~( SS_BITMAP
|SS_ICON
) ) |
272 ( m_isIcon
? SS_ICON
: SS_BITMAP
) );
273 HGDIOBJ oldHandle
= (HGDIOBJ
)::SendMessage(GetHwnd(), STM_SETIMAGE
,
274 m_isIcon
? IMAGE_ICON
: IMAGE_BITMAP
, (LPARAM
)handle
);
275 // detect if this is still the handle we passed before or
276 // if the static-control made a copy of the bitmap!
277 if (m_currentHandle
!= 0 && oldHandle
!= (HGDIOBJ
) m_currentHandle
)
279 // the static control made a copy and we are responsible for deleting it
280 DeleteObject((HGDIOBJ
) oldHandle
);
282 m_currentHandle
= (WXHANDLE
)handle
;
287 int width
= image
->GetWidth(),
288 height
= image
->GetHeight();
289 if ( width
&& height
)
294 ::MoveWindow(GetHwnd(), x
, y
, width
, height
, FALSE
);
303 ::InvalidateRect(GetHwndOf(GetParent()), &rect
, TRUE
);
306 #endif // wxUSE_STATBMP