]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbmp.cpp
Added wxUSE_DC_CACHEING and associated code to wxMSW
[wxWidgets.git] / src / msw / statbmp.cpp
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
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "statbmp.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_STATBMP
32
33 #include "wx/window.h"
34 #include "wx/msw/private.h"
35
36 #ifndef WX_PRECOMP
37 #include "wx/icon.h"
38 #include "wx/statbmp.h"
39 #endif
40
41 #include <stdio.h>
42
43 // ---------------------------------------------------------------------------
44 // macors
45 // ---------------------------------------------------------------------------
46
47 IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
48
49 // ===========================================================================
50 // implementation
51 // ===========================================================================
52
53 // ---------------------------------------------------------------------------
54 // wxStaticBitmap
55 // ---------------------------------------------------------------------------
56
57 bool wxStaticBitmap::Create(wxWindow *parent, wxWindowID id,
58 const wxGDIImage& bitmap,
59 const wxPoint& pos,
60 const wxSize& size,
61 long style,
62 const wxString& name)
63 {
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
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;
89 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
90
91 #ifdef __WIN16__
92 wxASSERT_MSG( !m_isIcon, "Icons are not supported in wxStaticBitmap under WIN16." );
93 #endif
94
95 #ifndef __WIN16__
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 }
108 #endif
109
110 #ifdef __WIN32__
111 // create a static control with either SS_BITMAP or SS_ICON style depending
112 // on what we have here
113 const wxChar *classname = wxT("STATIC");
114 int winstyle = m_isIcon ? SS_ICON : SS_BITMAP;
115 #else // Win16
116 const wxChar *classname = wxT("BUTTON");
117 int winstyle = BS_OWNERDRAW;
118 #endif // Win32
119
120 if ( m_windowStyle & wxCLIP_SIBLINGS )
121 winstyle |= WS_CLIPSIBLINGS;
122
123
124 m_hWnd = (WXHWND)::CreateWindow
125 (
126 classname,
127 wxT(""),
128 // NOT DISABLED!!! We want to move it in Dialog Editor.
129 winstyle | WS_CHILD | WS_VISIBLE /* | WS_CLIPSIBLINGS */ , // | WS_DISABLED,
130 0, 0, 0, 0,
131 (HWND)parent->GetHWND(),
132 (HMENU)m_windowId,
133 wxGetInstance(),
134 NULL
135 );
136
137 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static bitmap") );
138
139 SetImage(icon ? icon : &bitmap);
140 delete icon; // may be NULL, ok
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
152 bool wxStaticBitmap::ImageIsOk() const
153 {
154 return m_image && m_image->Ok();
155 }
156
157 void wxStaticBitmap::Free()
158 {
159 delete m_image;
160
161 m_image = NULL;
162 }
163
164 wxSize wxStaticBitmap::DoGetBestSize() const
165 {
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();
169 }
170
171 void wxStaticBitmap::SetImage(const wxGDIImage* image)
172 {
173 Free();
174
175 const wxIcon *icon = wxDynamicCast(image, wxIcon);
176 m_isIcon = icon != NULL;
177 if ( m_isIcon )
178 {
179 m_image = new wxIcon(*icon);
180 }
181 else
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 }
190
191 int x, y;
192 int w, h;
193 GetPosition(&x, &y);
194 GetSize(&w, &h);
195
196 #ifdef __WIN32__
197 HANDLE handle = (HANDLE)m_image->GetHandle();
198 ::SendMessage(GetHwnd(), STM_SETIMAGE,
199 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
200 #endif // Win32
201
202 if ( ImageIsOk() )
203 {
204 int width = image->GetWidth(),
205 height = image->GetHeight();
206 if ( width && height )
207 {
208 w = width;
209 h = height;
210
211 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
212 }
213 }
214
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);
221 }
222
223 // under Win32 we use the standard static control style for this
224 #ifdef __WIN16__
225 bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
226 {
227 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
228
229 wxCHECK_MSG( !m_isIcon, FALSE, _T("icons not supported in wxStaticBitmap") );
230
231 wxBitmap* bitmap = (wxBitmap *)m_image;
232 if ( !bitmap->Ok() )
233 return FALSE;
234
235 HDC hDC = lpDIS->hDC;
236 HDC memDC = ::CreateCompatibleDC(hDC);
237
238 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
239
240 if (!old)
241 return FALSE;
242
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;
247
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));
251
252 ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY);
253
254 ::SelectObject(memDC, old);
255
256 ::DeleteDC(memDC);
257
258 return TRUE;
259 }
260 #endif // Win16
261
262 // We need this or the control can never be moved e.g. in Dialog Editor.
263 long wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
264 WXWPARAM wParam,
265 WXLPARAM lParam)
266 {
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;
271
272 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
273 }
274
275 #endif // wxUSE_STATBMP