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