]>
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 // ===========================================================================
50 // ===========================================================================
52 // ---------------------------------------------------------------------------
54 // ---------------------------------------------------------------------------
56 // we may have either bitmap or icon: if a bitmap with mask is passed, we
57 // will transform it to an icon ourselves because otherwise the mask will
58 // be ignored by Windows
59 // note that this function will create a new object every time
60 // it is called even if the image needs no conversion
62 static wxGDIImage
* ConvertImage( const wxGDIImage
& bitmap
)
64 bool isIcon
= bitmap
.IsKindOf( CLASSINFO(wxIcon
) );
68 wxASSERT_MSG( wxDynamicCast(&bitmap
, wxBitmap
),
69 wxT("not an icon and not a bitmap?") );
71 const wxBitmap
& bmp
= (const wxBitmap
&)bitmap
;
72 wxMask
*mask
= bmp
.GetMask();
73 if ( mask
&& mask
->GetMaskBitmap() )
75 wxIcon
* icon
= new wxIcon
;
76 icon
->CopyFromBitmap(bmp
);
81 return new wxBitmap( bmp
);
84 // copying a bitmap is a cheap operation
85 return new wxIcon( (const wxIcon
&)bitmap
);
88 bool wxStaticBitmap::Create(wxWindow
*parent
,
90 const wxGDIImage
& bitmap
,
96 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
99 // we may have either bitmap or icon: if a bitmap with mask is passed, we
100 // will transform it to an icon ourselves because otherwise the mask will
101 // be ignored by Windows
102 m_isIcon
= bitmap
.IsKindOf(CLASSINFO(wxIcon
));
104 wxGDIImage
*image
= ConvertImage( bitmap
);
105 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
107 // create the native control
108 if ( !MSWCreateControl(wxT("STATIC"), wxEmptyString
, pos
, size
) )
110 // control creation failed
114 // no need to delete the new image
115 SetImageNoCopy(image
);
117 // GetBestSize will work properly now, so set the best size if needed
118 SetInitialSize(size
);
120 // painting manually is reported not to work under Windows CE (see #10093),
121 // so don't do it there even if this probably means that alpha is not
122 // supported there -- but at least bitmaps without alpha appear correctly
124 // Windows versions before XP (and even XP if the application has no
125 // manifest and so the old comctl32.dll is used) don't draw correctly the
126 // images with alpha channel so we need to draw them ourselves and it's
127 // easier to just always do it rather than check if we have an image with
129 if ( wxTheApp
->GetComCtl32Version() < 600 )
131 Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBitmap::DoPaintManually
));
133 #endif // !__WXWINCE__
138 WXDWORD
wxStaticBitmap::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
140 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
142 // what kind of control are we?
143 msStyle
|= m_isIcon
? SS_ICON
: SS_BITMAP
;
145 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
146 // fit to its size -- this is unexpected and doesn't happen in other ports
148 // and SS_NOTIFY is necessary to receive mouse events
149 msStyle
|= SS_CENTERIMAGE
| SS_NOTIFY
;
154 bool wxStaticBitmap::ImageIsOk() const
156 return m_image
&& m_image
->IsOk();
159 wxIcon
wxStaticBitmap::GetIcon() const
161 wxCHECK_MSG( m_image
, wxIcon(), wxT("no image in wxStaticBitmap") );
163 // we can't ask for an icon if all we have is a bitmap
164 wxCHECK_MSG( m_isIcon
, wxIcon(), wxT("no icon in this wxStaticBitmap") );
166 return *(wxIcon
*)m_image
;
169 wxBitmap
wxStaticBitmap::GetBitmap() const
173 // don't fail because we might have replaced the bitmap with icon
174 // ourselves internally in ConvertImage() to keep the transparency but
175 // the user code doesn't know about it so it still can use GetBitmap()
176 // to retrieve the bitmap
177 return wxBitmap(GetIcon());
179 else // we have a bitmap
181 wxCHECK_MSG( m_image
, wxBitmap(), wxT("no image in wxStaticBitmap") );
183 return *(wxBitmap
*)m_image
;
187 void wxStaticBitmap::Free()
192 wxSize
wxStaticBitmap::DoGetBestSize() const
196 wxSize
best(m_image
->GetWidth(), m_image
->GetHeight());
201 // this is completely arbitrary
202 return wxSize(16, 16);
207 void wxStaticBitmap::DoPaintManually(wxPaintEvent
& WXUNUSED(event
))
211 const wxSize
size(GetSize());
212 const wxBitmap
bmp(GetBitmap());
214 // Clear the background: notice that we're supposed to be transparent, so
215 // use the parent background colour if we don't have our own instead of
216 // falling back to the default
217 const wxWindow
*win
= UseBgCol() ? this : GetParent();
218 dc
.SetBrush(win
->GetBackgroundColour());
219 dc
.SetPen(*wxTRANSPARENT_PEN
);
220 dc
.DrawRectangle(0, 0, size
.GetWidth(), size
.GetHeight());
222 // Draw the image in the middle
224 (size
.GetWidth() - bmp
.GetWidth()) / 2,
225 (size
.GetHeight() - bmp
.GetHeight()) / 2,
226 true /* use mask */);
229 #endif // !__WXWINCE__
231 void wxStaticBitmap::SetImage( const wxGDIImage
* image
)
233 wxGDIImage
* convertedImage
= ConvertImage( *image
);
234 SetImageNoCopy( convertedImage
);
235 InvalidateBestSize();
238 void wxStaticBitmap::SetImageNoCopy( wxGDIImage
* image
)
242 m_isIcon
= image
->IsKindOf( CLASSINFO(wxIcon
) );
243 // the image has already been copied
252 HANDLE handle
= (HANDLE
)m_image
->GetHandle();
253 LONG style
= ::GetWindowLong( (HWND
)GetHWND(), GWL_STYLE
) ;
254 ::SetWindowLong( (HWND
)GetHWND(), GWL_STYLE
, ( style
& ~( SS_BITMAP
|SS_ICON
) ) |
255 ( m_isIcon
? SS_ICON
: SS_BITMAP
) );
256 HGDIOBJ oldHandle
= (HGDIOBJ
)::SendMessage(GetHwnd(), STM_SETIMAGE
,
257 m_isIcon
? IMAGE_ICON
: IMAGE_BITMAP
, (LPARAM
)handle
);
258 // detect if this is still the handle we passed before or
259 // if the static-control made a copy of the bitmap!
260 if (m_currentHandle
!= 0 && oldHandle
!= (HGDIOBJ
) m_currentHandle
)
262 // the static control made a copy and we are responsible for deleting it
263 DeleteObject((HGDIOBJ
) oldHandle
);
265 m_currentHandle
= (WXHANDLE
)handle
;
270 int width
= image
->GetWidth(),
271 height
= image
->GetHeight();
272 if ( width
&& height
)
277 ::MoveWindow(GetHwnd(), x
, y
, width
, height
, FALSE
);
286 ::InvalidateRect(GetHwndOf(GetParent()), &rect
, TRUE
);
289 #endif // wxUSE_STATBMP