]> git.saurik.com Git - wxWidgets.git/blame - src/msw/statbmp.cpp
another custom draw fix: leave space for the image even if the current item doesn...
[wxWidgets.git] / src / msw / statbmp.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: statbmp.cpp
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
0c589ad0
BM
29#include "wx/window.h"
30#include "wx/msw/private.h"
31
2bda0e17 32#ifndef WX_PRECOMP
0c589ad0 33 #include "wx/icon.h"
9e3e0821 34 #include "wx/statbmp.h"
2bda0e17
KB
35#endif
36
9dabade2
JS
37#include "wx/sysopt.h"
38
2bda0e17 39#include <stdio.h>
2bda0e17 40
9e3e0821
VZ
41// ---------------------------------------------------------------------------
42// macors
43// ---------------------------------------------------------------------------
44
bc9fb572
JS
45#if wxUSE_EXTENDED_RTTI
46WX_DEFINE_FLAGS( wxStaticBitmapStyle )
47
3ff066a4 48wxBEGIN_FLAGS( wxStaticBitmapStyle )
bc9fb572
JS
49 // new style border flags, we put them first to
50 // use them for streaming out
3ff066a4
SC
51 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
52 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
53 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
54 wxFLAGS_MEMBER(wxBORDER_RAISED)
55 wxFLAGS_MEMBER(wxBORDER_STATIC)
56 wxFLAGS_MEMBER(wxBORDER_NONE)
57f4f925 57
bc9fb572 58 // old style border flags
3ff066a4
SC
59 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
60 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
61 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
62 wxFLAGS_MEMBER(wxRAISED_BORDER)
63 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 64 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
65
66 // standard window styles
3ff066a4
SC
67 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
68 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
69 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
70 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 71 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
72 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
73 wxFLAGS_MEMBER(wxVSCROLL)
74 wxFLAGS_MEMBER(wxHSCROLL)
bc9fb572 75
3ff066a4 76wxEND_FLAGS( wxStaticBitmapStyle )
bc9fb572
JS
77
78IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBitmap, wxControl,"wx/statbmp.h")
79
3ff066a4 80wxBEGIN_PROPERTIES_TABLE(wxStaticBitmap)
af498247 81 wxPROPERTY_FLAGS( WindowStyle , wxStaticBitmapStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 82wxEND_PROPERTIES_TABLE()
bc9fb572 83
3ff066a4
SC
84wxBEGIN_HANDLERS_TABLE(wxStaticBitmap)
85wxEND_HANDLERS_TABLE()
bc9fb572 86
3ff066a4 87wxCONSTRUCTOR_5( wxStaticBitmap, wxWindow* , Parent , wxWindowID , Id , wxBitmap, Bitmap, wxPoint , Position , wxSize , Size )
bc9fb572
JS
88
89#else
4004f41e 90IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
bc9fb572 91#endif
2bda0e17 92
066f1b7a 93/*
57f4f925
WS
94 TODO PROPERTIES :
95 bitmap
066f1b7a
SC
96*/
97
9e3e0821
VZ
98// ===========================================================================
99// implementation
100// ===========================================================================
101
102// ---------------------------------------------------------------------------
103// wxStaticBitmap
104// ---------------------------------------------------------------------------
2bda0e17 105
d8bffc13
MB
106// we may have either bitmap or icon: if a bitmap with mask is passed, we
107// will transform it to an icon ourselves because otherwise the mask will
108// be ignored by Windows
109// note that this function will create a new object every time
110// it is called even if the image needs no conversion
111
d8bffc13
MB
112static wxGDIImage* ConvertImage( const wxGDIImage& bitmap )
113{
114 bool isIcon = bitmap.IsKindOf( CLASSINFO(wxIcon) );
115
116 if( !isIcon )
117 {
118 wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap),
119 _T("not an icon and not a bitmap?") );
120
121 const wxBitmap& bmp = (const wxBitmap&)bitmap;
122 wxMask *mask = bmp.GetMask();
123 if ( mask && mask->GetMaskBitmap() )
124 {
125 wxIcon* icon = new wxIcon;
126 icon->CopyFromBitmap(bmp);
127
128 return icon;
129 }
130
131 return new wxBitmap( bmp );
132 }
133
134 // copying a bitmap is a cheap operation
135 return new wxIcon( (const wxIcon&)bitmap );
136}
137
46a5e01e
VZ
138bool wxStaticBitmap::Create(wxWindow *parent,
139 wxWindowID id,
0d0512bd 140 const wxGDIImage& bitmap,
9e3e0821
VZ
141 const wxPoint& pos,
142 const wxSize& size,
143 long style,
144 const wxString& name)
2bda0e17 145{
46a5e01e 146 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
57f4f925 147 return false;
9e3e0821 148
4004f41e
VZ
149 // we may have either bitmap or icon: if a bitmap with mask is passed, we
150 // will transform it to an icon ourselves because otherwise the mask will
151 // be ignored by Windows
9e3e0821 152 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
669f7a11 153
5cb598ae 154 wxGDIImage *image = ConvertImage( bitmap );
d8bffc13 155 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
9e3e0821 156
46a5e01e 157 // create the native control
3a5bcc4d 158 if ( !MSWCreateControl(_T("STATIC"), wxEmptyString, pos, size) )
46a5e01e
VZ
159 {
160 // control creation failed
3a5bcc4d 161 return false;
46a5e01e 162 }
9e3e0821 163
d8bffc13 164 // no need to delete the new image
46a5e01e 165 SetImageNoCopy(image);
9e3e0821 166
9d17ee60
RD
167 // GetBestSize will work properly now, so set the best size if needed
168 SetBestSize(size);
57f4f925
WS
169
170 return true;
46a5e01e 171}
9e3e0821 172
65bc172c
VZ
173wxBorder wxStaticBitmap::GetDefaultBorder() const
174{
175 return wxBORDER_NONE;
176}
177
46a5e01e
VZ
178WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const
179{
180 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
9e3e0821 181
46a5e01e
VZ
182 // what kind of control are we?
183 msStyle |= m_isIcon ? SS_ICON : SS_BITMAP;
9e3e0821 184
46a5e01e
VZ
185 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
186 // fit to its size -- this is unexpected and doesn't happen in other ports
42b1fb63
VZ
187 //
188 // and SS_NOTIFY is necessary to receive mouse events
189 msStyle |= SS_CENTERIMAGE | SS_NOTIFY;
46a5e01e
VZ
190
191 return msStyle;
9e3e0821
VZ
192}
193
194bool wxStaticBitmap::ImageIsOk() const
195{
0d0512bd 196 return m_image && m_image->Ok();
9e3e0821
VZ
197}
198
333c8697
VZ
199wxIcon wxStaticBitmap::GetIcon() const
200{
201 wxCHECK_MSG( m_image, wxIcon(), _T("no image in wxStaticBitmap") );
202
203 // we can't ask for an icon if all we have is a bitmap
204 wxCHECK_MSG( m_isIcon, wxIcon(), _T("no icon in this wxStaticBitmap") );
205
206 return *(wxIcon *)m_image;
207}
208
209wxBitmap wxStaticBitmap::GetBitmap() const
210{
211 if ( m_isIcon )
212 {
213 // don't fail because we might have replaced the bitmap with icon
214 // ourselves internally in ConvertImage() to keep the transparency but
215 // the user code doesn't know about it so it still can use GetBitmap()
216 // to retrieve the bitmap
217 return wxBitmap(GetIcon());
218 }
219 else // we have a bitmap
220 {
221 wxCHECK_MSG( m_image, wxBitmap(), _T("no image in wxStaticBitmap") );
222
223 return *(wxBitmap *)m_image;
224 }
225}
226
9e3e0821
VZ
227void wxStaticBitmap::Free()
228{
0d0512bd 229 delete m_image;
9e3e0821 230
0d0512bd 231 m_image = NULL;
2bda0e17
KB
232}
233
9d17ee60
RD
234wxSize wxStaticBitmap::DoGetBestSize() const
235{
236 if ( ImageIsOk() )
31582e4e
RD
237 {
238 wxSize best(m_image->GetWidth(), m_image->GetHeight());
239 CacheBestSize(best);
240 return best;
241 }
9d17ee60
RD
242
243 // this is completely arbitrary
244 return wxSize(16, 16);
245}
246
d8bffc13 247void wxStaticBitmap::SetImage( const wxGDIImage* image )
2bda0e17 248{
d8bffc13
MB
249 wxGDIImage* convertedImage = ConvertImage( *image );
250 SetImageNoCopy( convertedImage );
9f884528 251 InvalidateBestSize();
d8bffc13 252}
4004f41e 253
d8bffc13
MB
254void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image)
255{
256 Free();
4004f41e 257
d8bffc13
MB
258 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
259 // the image has already been copied
260 m_image = image;
9e3e0821
VZ
261
262 int x, y;
263 int w, h;
264 GetPosition(&x, &y);
265 GetSize(&w, &h);
9e3e0821
VZ
266
267#ifdef __WIN32__
0d0512bd 268 HANDLE handle = (HANDLE)m_image->GetHandle();
d8bffc13
MB
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 ) );
3dece6c4 272 HGDIOBJ oldHandle = (HGDIOBJ)::SendMessage(GetHwnd(), STM_SETIMAGE,
9e3e0821 273 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
3dece6c4
JS
274 // detect if this is still the handle we passed before or
275 // if the static-control made a copy of the bitmap!
4fe41ce6 276 if (m_currentHandle != 0 && oldHandle != (HGDIOBJ) m_currentHandle)
3dece6c4
JS
277 {
278 // the static control made a copy and we are responsible for deleting it
4fe41ce6 279 DeleteObject((HGDIOBJ) oldHandle);
3dece6c4 280 }
4fe41ce6 281 m_currentHandle = (WXHANDLE)handle;
9e3e0821
VZ
282#endif // Win32
283
284 if ( ImageIsOk() )
285 {
4004f41e
VZ
286 int width = image->GetWidth(),
287 height = image->GetHeight();
9e3e0821
VZ
288 if ( width && height )
289 {
7c545786
VZ
290 w = width;
291 h = height;
292
0d0512bd 293 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
9e3e0821
VZ
294 }
295 }
296
0d0512bd
VZ
297 RECT rect;
298 rect.left = x;
299 rect.top = y;
300 rect.right = x + w;
301 rect.bottom = y + h;
57f4f925 302 ::InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
2bda0e17
KB
303}
304
1e6feb95 305#endif // wxUSE_STATBMP
6aa01033 306