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