]>
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
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ===========================================================================
13 // ===========================================================================
15 // ---------------------------------------------------------------------------
17 // ---------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/statbmp.h"
32 #include "wx/window.h"
34 #include "wx/dcclient.h"
37 #include "wx/msw/private.h"
39 #include "wx/sysopt.h"
43 // ---------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------
47 wxBEGIN_EVENT_TABLE(wxStaticBitmap
, wxStaticBitmapBase
)
48 EVT_SIZE(wxStaticBitmap::WXHandleSize
)
51 // ===========================================================================
53 // ===========================================================================
55 // ---------------------------------------------------------------------------
57 // ---------------------------------------------------------------------------
59 // we may have either bitmap or icon: if a bitmap with mask is passed, we
60 // will transform it to an icon ourselves because otherwise the mask will
61 // be ignored by Windows
62 // note that this function will create a new object every time
63 // it is called even if the image needs no conversion
65 static wxGDIImage
* ConvertImage( const wxGDIImage
& bitmap
)
67 bool isIcon
= bitmap
.IsKindOf( wxCLASSINFO(wxIcon
) );
71 wxASSERT_MSG( wxDynamicCast(&bitmap
, wxBitmap
),
72 wxT("not an icon and not a bitmap?") );
74 const wxBitmap
& bmp
= (const wxBitmap
&)bitmap
;
75 wxMask
*mask
= bmp
.GetMask();
76 if ( mask
&& mask
->GetMaskBitmap() )
78 wxIcon
* icon
= new wxIcon
;
79 icon
->CopyFromBitmap(bmp
);
84 return new wxBitmap( bmp
);
87 // copying a bitmap is a cheap operation
88 return new wxIcon( (const wxIcon
&)bitmap
);
91 bool wxStaticBitmap::Create(wxWindow
*parent
,
93 const wxGDIImage
& bitmap
,
99 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
102 // we may have either bitmap or icon: if a bitmap with mask is passed, we
103 // will transform it to an icon ourselves because otherwise the mask will
104 // be ignored by Windows
105 m_isIcon
= bitmap
.IsKindOf(wxCLASSINFO(wxIcon
));
107 wxGDIImage
*image
= ConvertImage( bitmap
);
108 m_isIcon
= image
->IsKindOf( wxCLASSINFO(wxIcon
) );
110 // create the native control
111 if ( !MSWCreateControl(wxT("STATIC"), wxEmptyString
, pos
, size
) )
113 // control creation failed
117 // no need to delete the new image
118 SetImageNoCopy(image
);
120 // GetBestSize will work properly now, so set the best size if needed
121 SetInitialSize(size
);
123 // painting manually is reported not to work under Windows CE (see #10093),
124 // so don't do it there even if this probably means that alpha is not
125 // supported there -- but at least bitmaps without alpha appear correctly
127 // Windows versions before XP (and even XP if the application has no
128 // manifest and so the old comctl32.dll is used) don't draw correctly the
129 // images with alpha channel so we need to draw them ourselves and it's
130 // easier to just always do it rather than check if we have an image with
132 if ( wxTheApp
->GetComCtl32Version() < 600 )
134 Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBitmap::DoPaintManually
));
136 #endif // !__WXWINCE__
141 WXDWORD
wxStaticBitmap::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
143 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
145 // what kind of control are we?
146 msStyle
|= m_isIcon
? SS_ICON
: SS_BITMAP
;
148 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
149 // fit to its size -- this is unexpected and doesn't happen in other ports
151 // and SS_NOTIFY is necessary to receive mouse events
152 msStyle
|= SS_CENTERIMAGE
| SS_NOTIFY
;
157 bool wxStaticBitmap::ImageIsOk() const
159 return m_image
&& m_image
->IsOk();
162 wxIcon
wxStaticBitmap::GetIcon() const
164 wxCHECK_MSG( m_image
, wxIcon(), wxT("no image in wxStaticBitmap") );
166 // we can't ask for an icon if all we have is a bitmap
167 wxCHECK_MSG( m_isIcon
, wxIcon(), wxT("no icon in this wxStaticBitmap") );
169 return *(wxIcon
*)m_image
;
172 wxBitmap
wxStaticBitmap::GetBitmap() const
176 // don't fail because we might have replaced the bitmap with icon
177 // ourselves internally in ConvertImage() to keep the transparency but
178 // the user code doesn't know about it so it still can use GetBitmap()
179 // to retrieve the bitmap
180 return wxBitmap(GetIcon());
182 else // we have a bitmap
184 wxCHECK_MSG( m_image
, wxBitmap(), wxT("no image in wxStaticBitmap") );
186 return *(wxBitmap
*)m_image
;
190 void wxStaticBitmap::Free()
195 wxSize
wxStaticBitmap::DoGetBestClientSize() const
200 size
= m_image
->GetSize();
204 // this is completely arbitrary
212 void wxStaticBitmap::WXHandleSize(wxSizeEvent
& event
)
214 // Invalidate everything when our size changes as the image position (it's
215 // drawn centred in the window client area) changes.
223 void wxStaticBitmap::DoPaintManually(wxPaintEvent
& WXUNUSED(event
))
227 const wxSize
size(GetSize());
228 const wxBitmap
bmp(GetBitmap());
230 // Clear the background: notice that we're supposed to be transparent, so
231 // use the parent background colour if we don't have our own instead of
232 // falling back to the default
233 const wxWindow
*win
= UseBgCol() ? this : GetParent();
234 dc
.SetBrush(win
->GetBackgroundColour());
235 dc
.SetPen(*wxTRANSPARENT_PEN
);
236 dc
.DrawRectangle(0, 0, size
.GetWidth(), size
.GetHeight());
238 // Draw the image in the middle
240 (size
.GetWidth() - bmp
.GetWidth()) / 2,
241 (size
.GetHeight() - bmp
.GetHeight()) / 2,
242 true /* use mask */);
245 #endif // !__WXWINCE__
247 void wxStaticBitmap::SetImage( const wxGDIImage
* image
)
249 wxGDIImage
* convertedImage
= ConvertImage( *image
);
250 SetImageNoCopy( convertedImage
);
253 void wxStaticBitmap::SetImageNoCopy( wxGDIImage
* image
)
256 InvalidateBestSize();
258 m_isIcon
= image
->IsKindOf( wxCLASSINFO(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 HGDIOBJ oldHandle
= (HGDIOBJ
)::SendMessage(GetHwnd(), STM_SETIMAGE
,
273 m_isIcon
? IMAGE_ICON
: IMAGE_BITMAP
, (LPARAM
)handle
);
274 // detect if this is still the handle we passed before or
275 // if the static-control made a copy of the bitmap!
276 if (m_currentHandle
!= 0 && oldHandle
!= (HGDIOBJ
) m_currentHandle
)
278 // the static control made a copy and we are responsible for deleting it
279 DeleteObject((HGDIOBJ
) oldHandle
);
281 m_currentHandle
= (WXHANDLE
)handle
;
286 int width
= image
->GetWidth(),
287 height
= image
->GetHeight();
288 if ( width
&& height
)
293 ::MoveWindow(GetHwnd(), x
, y
, width
, height
, FALSE
);
302 ::InvalidateRect(GetHwndOf(GetParent()), &rect
, TRUE
);
305 #endif // wxUSE_STATBMP