]> git.saurik.com Git - wxWidgets.git/blame - src/msw/statbmp.cpp
Test wxTE_PROCESS_ENTER in the combobox page of the widgets sample.
[wxWidgets.git] / src / msw / statbmp.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
cdccdfab 2// Name: src/msw/statbmp.cpp
2bda0e17
KB
3// Purpose: wxStaticBitmap
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
9e3e0821
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
9e3e0821 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
1e6feb95
VZ
27#if wxUSE_STATBMP
28
cdccdfab 29#include "wx/statbmp.h"
0c589ad0 30
2bda0e17 31#ifndef WX_PRECOMP
b3f2ce1c 32 #include "wx/app.h"
cdccdfab 33 #include "wx/window.h"
0c589ad0 34 #include "wx/icon.h"
30724d04 35 #include "wx/dcclient.h"
2bda0e17
KB
36#endif
37
cdccdfab
WS
38#include "wx/msw/private.h"
39
9dabade2
JS
40#include "wx/sysopt.h"
41
2bda0e17 42#include <stdio.h>
2bda0e17 43
9e3e0821 44// ---------------------------------------------------------------------------
05942059 45// macros
9e3e0821
VZ
46// ---------------------------------------------------------------------------
47
05942059
VZ
48wxBEGIN_EVENT_TABLE(wxStaticBitmap, wxStaticBitmapBase)
49 EVT_SIZE(wxStaticBitmap::WXHandleSize)
50wxEND_EVENT_TABLE()
51
9e3e0821
VZ
52// ===========================================================================
53// implementation
54// ===========================================================================
55
56// ---------------------------------------------------------------------------
57// wxStaticBitmap
58// ---------------------------------------------------------------------------
2bda0e17 59
d8bffc13
MB
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
65
d8bffc13
MB
66static wxGDIImage* ConvertImage( const wxGDIImage& bitmap )
67{
80a46597 68 bool isIcon = bitmap.IsKindOf( wxCLASSINFO(wxIcon) );
d8bffc13
MB
69
70 if( !isIcon )
71 {
72 wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap),
9a83f860 73 wxT("not an icon and not a bitmap?") );
d8bffc13
MB
74
75 const wxBitmap& bmp = (const wxBitmap&)bitmap;
76 wxMask *mask = bmp.GetMask();
77 if ( mask && mask->GetMaskBitmap() )
78 {
79 wxIcon* icon = new wxIcon;
80 icon->CopyFromBitmap(bmp);
81
82 return icon;
83 }
84
85 return new wxBitmap( bmp );
86 }
87
88 // copying a bitmap is a cheap operation
89 return new wxIcon( (const wxIcon&)bitmap );
90}
91
46a5e01e
VZ
92bool wxStaticBitmap::Create(wxWindow *parent,
93 wxWindowID id,
0d0512bd 94 const wxGDIImage& bitmap,
9e3e0821
VZ
95 const wxPoint& pos,
96 const wxSize& size,
97 long style,
98 const wxString& name)
2bda0e17 99{
46a5e01e 100 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
57f4f925 101 return false;
9e3e0821 102
4004f41e
VZ
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
80a46597 106 m_isIcon = bitmap.IsKindOf(wxCLASSINFO(wxIcon));
669f7a11 107
5cb598ae 108 wxGDIImage *image = ConvertImage( bitmap );
80a46597 109 m_isIcon = image->IsKindOf( wxCLASSINFO(wxIcon) );
9e3e0821 110
46a5e01e 111 // create the native control
9a83f860 112 if ( !MSWCreateControl(wxT("STATIC"), wxEmptyString, pos, size) )
46a5e01e
VZ
113 {
114 // control creation failed
3a5bcc4d 115 return false;
46a5e01e 116 }
9e3e0821 117
d8bffc13 118 // no need to delete the new image
46a5e01e 119 SetImageNoCopy(image);
9e3e0821 120
9d17ee60 121 // GetBestSize will work properly now, so set the best size if needed
170acdc9 122 SetInitialSize(size);
57f4f925 123
9eb2347d
VZ
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
127#ifndef __WXWINCE__
b3f2ce1c
VZ
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
132 // alpha or not
133 if ( wxTheApp->GetComCtl32Version() < 600 )
a73a4ab7
VZ
134 {
135 Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBitmap::DoPaintManually));
136 }
9eb2347d 137#endif // !__WXWINCE__
a73a4ab7 138
57f4f925 139 return true;
46a5e01e 140}
9e3e0821 141
46a5e01e
VZ
142WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const
143{
144 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
9e3e0821 145
46a5e01e
VZ
146 // what kind of control are we?
147 msStyle |= m_isIcon ? SS_ICON : SS_BITMAP;
9e3e0821 148
46a5e01e
VZ
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
42b1fb63
VZ
151 //
152 // and SS_NOTIFY is necessary to receive mouse events
153 msStyle |= SS_CENTERIMAGE | SS_NOTIFY;
46a5e01e
VZ
154
155 return msStyle;
9e3e0821
VZ
156}
157
158bool wxStaticBitmap::ImageIsOk() const
159{
a1b806b9 160 return m_image && m_image->IsOk();
9e3e0821
VZ
161}
162
333c8697
VZ
163wxIcon wxStaticBitmap::GetIcon() const
164{
9a83f860 165 wxCHECK_MSG( m_image, wxIcon(), wxT("no image in wxStaticBitmap") );
333c8697
VZ
166
167 // we can't ask for an icon if all we have is a bitmap
9a83f860 168 wxCHECK_MSG( m_isIcon, wxIcon(), wxT("no icon in this wxStaticBitmap") );
333c8697
VZ
169
170 return *(wxIcon *)m_image;
171}
172
173wxBitmap wxStaticBitmap::GetBitmap() const
174{
175 if ( m_isIcon )
176 {
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());
182 }
183 else // we have a bitmap
184 {
9a83f860 185 wxCHECK_MSG( m_image, wxBitmap(), wxT("no image in wxStaticBitmap") );
333c8697
VZ
186
187 return *(wxBitmap *)m_image;
188 }
189}
190
9e3e0821
VZ
191void wxStaticBitmap::Free()
192{
5276b0a5 193 wxDELETE(m_image);
2bda0e17
KB
194}
195
ae4375b8 196wxSize wxStaticBitmap::DoGetBestClientSize() const
9d17ee60 197{
ae4375b8 198 wxSize size;
9d17ee60 199 if ( ImageIsOk() )
31582e4e 200 {
ae4375b8
VZ
201 size = m_image->GetSize();
202 }
203 else // No image yet
204 {
205 // this is completely arbitrary
206 size.x =
207 size.y = 16;
31582e4e 208 }
9d17ee60 209
ae4375b8 210 return size;
9d17ee60
RD
211}
212
05942059
VZ
213void wxStaticBitmap::WXHandleSize(wxSizeEvent& event)
214{
215 // Invalidate everything when our size changes as the image position (it's
216 // drawn centred in the window client area) changes.
217 Refresh();
218
219 event.Skip();
220}
221
9eb2347d
VZ
222#ifndef __WXWINCE__
223
a73a4ab7
VZ
224void wxStaticBitmap::DoPaintManually(wxPaintEvent& WXUNUSED(event))
225{
226 wxPaintDC dc(this);
227
228 const wxSize size(GetSize());
229 const wxBitmap bmp(GetBitmap());
230
52d80ec6
VZ
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());
a73a4ab7
VZ
236 dc.SetPen(*wxTRANSPARENT_PEN);
237 dc.DrawRectangle(0, 0, size.GetWidth(), size.GetHeight());
238
239 // Draw the image in the middle
240 dc.DrawBitmap(bmp,
241 (size.GetWidth() - bmp.GetWidth()) / 2,
242 (size.GetHeight() - bmp.GetHeight()) / 2,
243 true /* use mask */);
244}
245
9eb2347d
VZ
246#endif // !__WXWINCE__
247
d8bffc13 248void wxStaticBitmap::SetImage( const wxGDIImage* image )
2bda0e17 249{
d8bffc13
MB
250 wxGDIImage* convertedImage = ConvertImage( *image );
251 SetImageNoCopy( convertedImage );
252}
4004f41e 253
d8bffc13
MB
254void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image)
255{
256 Free();
ae4375b8 257 InvalidateBestSize();
4004f41e 258
80a46597 259 m_isIcon = image->IsKindOf( wxCLASSINFO(wxIcon) );
d8bffc13
MB
260 // the image has already been copied
261 m_image = image;
9e3e0821
VZ
262
263 int x, y;
264 int w, h;
265 GetPosition(&x, &y);
266 GetSize(&w, &h);
9e3e0821
VZ
267
268#ifdef __WIN32__
0d0512bd 269 HANDLE handle = (HANDLE)m_image->GetHandle();
d8bffc13
MB
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 ) );
3dece6c4 273 HGDIOBJ oldHandle = (HGDIOBJ)::SendMessage(GetHwnd(), STM_SETIMAGE,
9e3e0821 274 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
3dece6c4
JS
275 // detect if this is still the handle we passed before or
276 // if the static-control made a copy of the bitmap!
4fe41ce6 277 if (m_currentHandle != 0 && oldHandle != (HGDIOBJ) m_currentHandle)
3dece6c4
JS
278 {
279 // the static control made a copy and we are responsible for deleting it
cdccdfab 280 DeleteObject((HGDIOBJ) oldHandle);
3dece6c4 281 }
cdccdfab 282 m_currentHandle = (WXHANDLE)handle;
9e3e0821
VZ
283#endif // Win32
284
285 if ( ImageIsOk() )
286 {
4004f41e
VZ
287 int width = image->GetWidth(),
288 height = image->GetHeight();
9e3e0821
VZ
289 if ( width && height )
290 {
7c545786
VZ
291 w = width;
292 h = height;
293
0d0512bd 294 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
9e3e0821
VZ
295 }
296 }
297
0d0512bd
VZ
298 RECT rect;
299 rect.left = x;
300 rect.top = y;
301 rect.right = x + w;
302 rect.bottom = y + h;
57f4f925 303 ::InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
2bda0e17
KB
304}
305
1e6feb95 306#endif // wxUSE_STATBMP