]> git.saurik.com Git - wxWidgets.git/blame - src/msw/statbmp.cpp
A few tweaks and cleanups
[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$
8// Copyright: (c) Julian Smart and Markus Holzem
fd3f686c 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
9e3e0821
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
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
4004f41e 47IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
2bda0e17 48
9e3e0821
VZ
49// ===========================================================================
50// implementation
51// ===========================================================================
52
53// ---------------------------------------------------------------------------
54// wxStaticBitmap
55// ---------------------------------------------------------------------------
2bda0e17 56
d8bffc13
MB
57// we may have either bitmap or icon: if a bitmap with mask is passed, we
58// will transform it to an icon ourselves because otherwise the mask will
59// be ignored by Windows
60// note that this function will create a new object every time
61// it is called even if the image needs no conversion
62
63#ifndef __WIN16__
64
65static wxGDIImage* ConvertImage( const wxGDIImage& bitmap )
66{
67 bool isIcon = bitmap.IsKindOf( CLASSINFO(wxIcon) );
68
69 if( !isIcon )
70 {
71 wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap),
72 _T("not an icon and not a bitmap?") );
73
74 const wxBitmap& bmp = (const wxBitmap&)bitmap;
75 wxMask *mask = bmp.GetMask();
76 if ( mask && mask->GetMaskBitmap() )
77 {
78 wxIcon* icon = new wxIcon;
79 icon->CopyFromBitmap(bmp);
80
81 return icon;
82 }
83
84 return new wxBitmap( bmp );
85 }
86
87 // copying a bitmap is a cheap operation
88 return new wxIcon( (const wxIcon&)bitmap );
89}
90
91#endif
92
debe6624 93bool wxStaticBitmap::Create(wxWindow *parent, 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{
9e3e0821
VZ
100 Init();
101
102 SetName(name);
103 if (parent)
104 parent->AddChild(this);
105
106 m_backgroundColour = parent->GetBackgroundColour() ;
107 m_foregroundColour = parent->GetForegroundColour() ;
108
109 if ( id == -1 )
110 m_windowId = (int)NewControlId();
111 else
112 m_windowId = id;
113
114 int x = pos.x;
115 int y = pos.y;
116 int width = size.x;
117 int height = size.y;
118
119 m_windowStyle = style;
120
4004f41e
VZ
121 // we may have either bitmap or icon: if a bitmap with mask is passed, we
122 // will transform it to an icon ourselves because otherwise the mask will
123 // be ignored by Windows
d8bffc13 124 wxGDIImage *image = (wxGDIImage *)NULL;
9e3e0821 125 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
669f7a11
JS
126
127#ifdef __WIN16__
128 wxASSERT_MSG( !m_isIcon, "Icons are not supported in wxStaticBitmap under WIN16." );
d8bffc13 129 image = &bitmap;
669f7a11
JS
130#endif
131
132#ifndef __WIN16__
d8bffc13
MB
133 image = ConvertImage( bitmap );
134 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
669f7a11 135#endif
9e3e0821
VZ
136
137#ifdef __WIN32__
138 // create a static control with either SS_BITMAP or SS_ICON style depending
139 // on what we have here
223d09f6 140 const wxChar *classname = wxT("STATIC");
9e3e0821
VZ
141 int winstyle = m_isIcon ? SS_ICON : SS_BITMAP;
142#else // Win16
223d09f6 143 const wxChar *classname = wxT("BUTTON");
58a33cb4 144 int winstyle = BS_OWNERDRAW;
9e3e0821
VZ
145#endif // Win32
146
b0766406
JS
147 if ( m_windowStyle & wxCLIP_SIBLINGS )
148 winstyle |= WS_CLIPSIBLINGS;
149
150
9e3e0821
VZ
151 m_hWnd = (WXHWND)::CreateWindow
152 (
153 classname,
223d09f6 154 wxT(""),
d0b223a1 155 // NOT DISABLED!!! We want to move it in Dialog Editor.
f6bcfd97 156 winstyle | WS_CHILD | WS_VISIBLE /* | WS_CLIPSIBLINGS */ , // | WS_DISABLED,
9e3e0821
VZ
157 0, 0, 0, 0,
158 (HWND)parent->GetHWND(),
159 (HMENU)m_windowId,
160 wxGetInstance(),
161 NULL
162 );
163
223d09f6 164 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static bitmap") );
9e3e0821 165
d8bffc13
MB
166 // no need to delete the new image
167 SetImageNoCopy( image );
9e3e0821
VZ
168
169 // Subclass again for purposes of dialog editing mode
170 SubclassWin(m_hWnd);
171
172 SetFont(GetParent()->GetFont());
173
174 SetSize(x, y, width, height);
175
176 return TRUE;
177}
178
179bool wxStaticBitmap::ImageIsOk() const
180{
0d0512bd 181 return m_image && m_image->Ok();
9e3e0821
VZ
182}
183
184void wxStaticBitmap::Free()
185{
0d0512bd 186 delete m_image;
9e3e0821 187
0d0512bd 188 m_image = NULL;
2bda0e17
KB
189}
190
f68586e5 191wxSize wxStaticBitmap::DoGetBestSize() const
2bda0e17 192{
4438caf4
VZ
193 // reuse the current size (as wxWindow does) instead of using some
194 // arbitrary default size (as wxControl, our immediate base class, does)
195 return wxWindow::DoGetBestSize();
2bda0e17
KB
196}
197
d8bffc13 198void wxStaticBitmap::SetImage( const wxGDIImage* image )
2bda0e17 199{
d8bffc13
MB
200 wxGDIImage* convertedImage = ConvertImage( *image );
201 SetImageNoCopy( convertedImage );
202}
4004f41e 203
d8bffc13
MB
204void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image)
205{
206 Free();
4004f41e 207
d8bffc13
MB
208 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
209 // the image has already been copied
210 m_image = image;
9e3e0821
VZ
211
212 int x, y;
213 int w, h;
214 GetPosition(&x, &y);
215 GetSize(&w, &h);
9e3e0821
VZ
216
217#ifdef __WIN32__
0d0512bd 218 HANDLE handle = (HANDLE)m_image->GetHandle();
d8bffc13
MB
219 LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ;
220 ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) |
221 ( m_isIcon ? SS_ICON : SS_BITMAP ) );
0d0512bd 222 ::SendMessage(GetHwnd(), STM_SETIMAGE,
9e3e0821
VZ
223 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
224#endif // Win32
225
226 if ( ImageIsOk() )
227 {
4004f41e
VZ
228 int width = image->GetWidth(),
229 height = image->GetHeight();
9e3e0821
VZ
230 if ( width && height )
231 {
7c545786
VZ
232 w = width;
233 h = height;
234
0d0512bd 235 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
9e3e0821
VZ
236 }
237 }
238
0d0512bd
VZ
239 RECT rect;
240 rect.left = x;
241 rect.top = y;
242 rect.right = x + w;
243 rect.bottom = y + h;
244 InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
2bda0e17
KB
245}
246
9e3e0821
VZ
247// under Win32 we use the standard static control style for this
248#ifdef __WIN16__
2bda0e17
KB
249bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
250{
2bda0e17
KB
251 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
252
8f177c8e
VZ
253 wxCHECK_MSG( !m_isIcon, FALSE, _T("icons not supported in wxStaticBitmap") );
254
255 wxBitmap* bitmap = (wxBitmap *)m_image;
fd3f686c
VZ
256 if ( !bitmap->Ok() )
257 return FALSE;
2bda0e17 258
fd3f686c
VZ
259 HDC hDC = lpDIS->hDC;
260 HDC memDC = ::CreateCompatibleDC(hDC);
2bda0e17 261
fd3f686c 262 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
2bda0e17 263
fd3f686c
VZ
264 if (!old)
265 return FALSE;
2bda0e17 266
9e3e0821
VZ
267 int x = lpDIS->rcItem.left;
268 int y = lpDIS->rcItem.top;
269 int width = lpDIS->rcItem.right - x;
270 int height = lpDIS->rcItem.bottom - y;
2bda0e17 271
fd3f686c
VZ
272 // Centre the bitmap in the control area
273 int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2));
274 int y1 = (int) (y + ((height - bitmap->GetHeight()) / 2));
2bda0e17 275
fd3f686c 276 ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY);
2bda0e17 277
fd3f686c 278 ::SelectObject(memDC, old);
2bda0e17
KB
279
280 ::DeleteDC(memDC);
281
282 return TRUE;
283}
d1e418ea 284#endif // Win16
2bda0e17 285
d1e418ea 286// We need this or the control can never be moved e.g. in Dialog Editor.
9e3e0821
VZ
287long wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
288 WXWPARAM wParam,
289 WXLPARAM lParam)
2bda0e17 290{
9e3e0821
VZ
291 // Ensure that static items get messages. Some controls don't like this
292 // message to be intercepted (e.g. RichEdit), hence the tests.
293 if ( nMsg == WM_NCHITTEST )
294 return (long)HTCLIENT;
2bda0e17 295
9e3e0821 296 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
2bda0e17 297}
d1e418ea 298
1e6feb95 299#endif // wxUSE_STATBMP