]> git.saurik.com Git - wxWidgets.git/blame - src/msw/statbmp.cpp
wxCocoa: Better icon support
[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
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
9e3e0821
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
9e3e0821 21 #pragma implementation "statbmp.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
9e3e0821 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_STATBMP
32
0c589ad0
BM
33#include "wx/window.h"
34#include "wx/msw/private.h"
35
2bda0e17 36#ifndef WX_PRECOMP
0c589ad0 37 #include "wx/icon.h"
9e3e0821 38 #include "wx/statbmp.h"
2bda0e17
KB
39#endif
40
41#include <stdio.h>
2bda0e17 42
9e3e0821
VZ
43// ---------------------------------------------------------------------------
44// macors
45// ---------------------------------------------------------------------------
46
bc9fb572
JS
47#if wxUSE_EXTENDED_RTTI
48WX_DEFINE_FLAGS( wxStaticBitmapStyle )
49
3ff066a4 50wxBEGIN_FLAGS( wxStaticBitmapStyle )
bc9fb572
JS
51 // new style border flags, we put them first to
52 // use them for streaming out
3ff066a4
SC
53 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
54 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
55 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
56 wxFLAGS_MEMBER(wxBORDER_RAISED)
57 wxFLAGS_MEMBER(wxBORDER_STATIC)
58 wxFLAGS_MEMBER(wxBORDER_NONE)
bc9fb572
JS
59
60 // old style border flags
3ff066a4
SC
61 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
62 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
63 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
64 wxFLAGS_MEMBER(wxRAISED_BORDER)
65 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 66 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
67
68 // standard window styles
3ff066a4
SC
69 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
70 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
71 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
72 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 73 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
74 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
75 wxFLAGS_MEMBER(wxVSCROLL)
76 wxFLAGS_MEMBER(wxHSCROLL)
bc9fb572 77
3ff066a4 78wxEND_FLAGS( wxStaticBitmapStyle )
bc9fb572
JS
79
80IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBitmap, wxControl,"wx/statbmp.h")
81
3ff066a4
SC
82wxBEGIN_PROPERTIES_TABLE(wxStaticBitmap)
83 wxPROPERTY_FLAGS( WindowStyle , wxStaticBitmapStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
84wxEND_PROPERTIES_TABLE()
bc9fb572 85
3ff066a4
SC
86wxBEGIN_HANDLERS_TABLE(wxStaticBitmap)
87wxEND_HANDLERS_TABLE()
bc9fb572 88
3ff066a4 89wxCONSTRUCTOR_5( wxStaticBitmap, wxWindow* , Parent , wxWindowID , Id , wxBitmap, Bitmap, wxPoint , Position , wxSize , Size )
bc9fb572
JS
90
91#else
4004f41e 92IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
bc9fb572 93#endif
2bda0e17 94
066f1b7a
SC
95/*
96 TODO PROPERTIES :
97 bitmap
98*/
99
9e3e0821
VZ
100// ===========================================================================
101// implementation
102// ===========================================================================
103
104// ---------------------------------------------------------------------------
105// wxStaticBitmap
106// ---------------------------------------------------------------------------
2bda0e17 107
d8bffc13
MB
108// we may have either bitmap or icon: if a bitmap with mask is passed, we
109// will transform it to an icon ourselves because otherwise the mask will
110// be ignored by Windows
111// note that this function will create a new object every time
112// it is called even if the image needs no conversion
113
114#ifndef __WIN16__
115
116static wxGDIImage* ConvertImage( const wxGDIImage& bitmap )
117{
118 bool isIcon = bitmap.IsKindOf( CLASSINFO(wxIcon) );
119
120 if( !isIcon )
121 {
122 wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap),
123 _T("not an icon and not a bitmap?") );
124
125 const wxBitmap& bmp = (const wxBitmap&)bitmap;
126 wxMask *mask = bmp.GetMask();
127 if ( mask && mask->GetMaskBitmap() )
128 {
129 wxIcon* icon = new wxIcon;
130 icon->CopyFromBitmap(bmp);
131
132 return icon;
133 }
134
135 return new wxBitmap( bmp );
136 }
137
138 // copying a bitmap is a cheap operation
139 return new wxIcon( (const wxIcon&)bitmap );
140}
141
142#endif
143
46a5e01e
VZ
144bool wxStaticBitmap::Create(wxWindow *parent,
145 wxWindowID id,
0d0512bd 146 const wxGDIImage& bitmap,
9e3e0821
VZ
147 const wxPoint& pos,
148 const wxSize& size,
149 long style,
150 const wxString& name)
2bda0e17 151{
46a5e01e
VZ
152 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
153 return FALSE;
9e3e0821 154
4004f41e
VZ
155 // we may have either bitmap or icon: if a bitmap with mask is passed, we
156 // will transform it to an icon ourselves because otherwise the mask will
157 // be ignored by Windows
d8bffc13 158 wxGDIImage *image = (wxGDIImage *)NULL;
9e3e0821 159 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
669f7a11
JS
160
161#ifdef __WIN16__
162 wxASSERT_MSG( !m_isIcon, "Icons are not supported in wxStaticBitmap under WIN16." );
d8bffc13 163 image = &bitmap;
46a5e01e 164#else // Win32
d8bffc13
MB
165 image = ConvertImage( bitmap );
166 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
46a5e01e 167#endif // Win16/32
9e3e0821 168
46a5e01e
VZ
169 // create the native control
170 if ( !MSWCreateControl(
9e3e0821 171#ifdef __WIN32__
46a5e01e 172 _T("STATIC"),
9e3e0821 173#else // Win16
46a5e01e
VZ
174 _T("BUTTON"),
175#endif // Win32/16
fda7962d 176 wxEmptyString, pos, size) )
46a5e01e
VZ
177 {
178 // control creation failed
179 return FALSE;
180 }
9e3e0821 181
d8bffc13 182 // no need to delete the new image
46a5e01e 183 SetImageNoCopy(image);
9e3e0821 184
46a5e01e
VZ
185 return TRUE;
186}
9e3e0821 187
65bc172c
VZ
188wxBorder wxStaticBitmap::GetDefaultBorder() const
189{
190 return wxBORDER_NONE;
191}
192
46a5e01e
VZ
193WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const
194{
195 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
9e3e0821 196
46a5e01e
VZ
197#ifdef __WIN32__
198 // what kind of control are we?
199 msStyle |= m_isIcon ? SS_ICON : SS_BITMAP;
9e3e0821 200
46a5e01e
VZ
201 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
202 // fit to its size -- this is unexpected and doesn't happen in other ports
203 msStyle |= SS_CENTERIMAGE;
204#else // Win16
205 msStyle |= BS_OWNERDRAW;
206#endif // Win32/16
207
208 return msStyle;
9e3e0821
VZ
209}
210
211bool wxStaticBitmap::ImageIsOk() const
212{
0d0512bd 213 return m_image && m_image->Ok();
9e3e0821
VZ
214}
215
216void wxStaticBitmap::Free()
217{
0d0512bd 218 delete m_image;
9e3e0821 219
0d0512bd 220 m_image = NULL;
2bda0e17
KB
221}
222
f68586e5 223wxSize wxStaticBitmap::DoGetBestSize() const
2bda0e17 224{
4438caf4
VZ
225 // reuse the current size (as wxWindow does) instead of using some
226 // arbitrary default size (as wxControl, our immediate base class, does)
227 return wxWindow::DoGetBestSize();
2bda0e17
KB
228}
229
d8bffc13 230void wxStaticBitmap::SetImage( const wxGDIImage* image )
2bda0e17 231{
d8bffc13
MB
232 wxGDIImage* convertedImage = ConvertImage( *image );
233 SetImageNoCopy( convertedImage );
234}
4004f41e 235
d8bffc13
MB
236void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image)
237{
238 Free();
4004f41e 239
d8bffc13
MB
240 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
241 // the image has already been copied
242 m_image = image;
9e3e0821
VZ
243
244 int x, y;
245 int w, h;
246 GetPosition(&x, &y);
247 GetSize(&w, &h);
9e3e0821
VZ
248
249#ifdef __WIN32__
0d0512bd 250 HANDLE handle = (HANDLE)m_image->GetHandle();
d8bffc13
MB
251 LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ;
252 ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) |
253 ( m_isIcon ? SS_ICON : SS_BITMAP ) );
0d0512bd 254 ::SendMessage(GetHwnd(), STM_SETIMAGE,
9e3e0821
VZ
255 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
256#endif // Win32
257
258 if ( ImageIsOk() )
259 {
4004f41e
VZ
260 int width = image->GetWidth(),
261 height = image->GetHeight();
9e3e0821
VZ
262 if ( width && height )
263 {
7c545786
VZ
264 w = width;
265 h = height;
266
0d0512bd 267 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
9e3e0821
VZ
268 }
269 }
270
0d0512bd
VZ
271 RECT rect;
272 rect.left = x;
273 rect.top = y;
274 rect.right = x + w;
275 rect.bottom = y + h;
276 InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
2bda0e17
KB
277}
278
9e3e0821
VZ
279// under Win32 we use the standard static control style for this
280#ifdef __WIN16__
2bda0e17
KB
281bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
282{
2bda0e17
KB
283 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
284
8f177c8e
VZ
285 wxCHECK_MSG( !m_isIcon, FALSE, _T("icons not supported in wxStaticBitmap") );
286
287 wxBitmap* bitmap = (wxBitmap *)m_image;
fd3f686c
VZ
288 if ( !bitmap->Ok() )
289 return FALSE;
2bda0e17 290
fd3f686c
VZ
291 HDC hDC = lpDIS->hDC;
292 HDC memDC = ::CreateCompatibleDC(hDC);
2bda0e17 293
fd3f686c 294 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
2bda0e17 295
fd3f686c
VZ
296 if (!old)
297 return FALSE;
2bda0e17 298
9e3e0821
VZ
299 int x = lpDIS->rcItem.left;
300 int y = lpDIS->rcItem.top;
301 int width = lpDIS->rcItem.right - x;
302 int height = lpDIS->rcItem.bottom - y;
2bda0e17 303
fd3f686c
VZ
304 // Centre the bitmap in the control area
305 int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2));
306 int y1 = (int) (y + ((height - bitmap->GetHeight()) / 2));
2bda0e17 307
fd3f686c 308 ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY);
2bda0e17 309
fd3f686c 310 ::SelectObject(memDC, old);
2bda0e17
KB
311
312 ::DeleteDC(memDC);
313
314 return TRUE;
315}
d1e418ea 316#endif // Win16
2bda0e17 317
d1e418ea 318// We need this or the control can never be moved e.g. in Dialog Editor.
9e3e0821
VZ
319long wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
320 WXWPARAM wParam,
321 WXLPARAM lParam)
2bda0e17 322{
4676948b 323#ifndef __WXWINCE__
9e3e0821
VZ
324 // Ensure that static items get messages. Some controls don't like this
325 // message to be intercepted (e.g. RichEdit), hence the tests.
326 if ( nMsg == WM_NCHITTEST )
327 return (long)HTCLIENT;
4676948b 328#endif
2bda0e17 329
9e3e0821 330 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
2bda0e17 331}
d1e418ea 332
1e6feb95 333#endif // wxUSE_STATBMP