Applied patch #420395 (adding wxCLIP_SIBBLINGS capability -- but not by default)
[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 if ( m_windowStyle & wxCLIP_SIBLINGS )
119 winstyle |= WS_CLIPSIBLINGS;
120
121
122 m_hWnd = (WXHWND)::CreateWindow
123 (
124 classname,
125 wxT(""),
126 // NOT DISABLED!!! We want to move it in Dialog Editor.
127 winstyle | WS_CHILD | WS_VISIBLE /* | WS_CLIPSIBLINGS */ , // | WS_DISABLED,
128 0, 0, 0, 0,
129 (HWND)parent->GetHWND(),
130 (HMENU)m_windowId,
131 wxGetInstance(),
132 NULL
133 );
134
135 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static bitmap") );
136
137 SetImage(icon ? icon : &bitmap);
138 delete icon; // may be NULL, ok
139
140 // Subclass again for purposes of dialog editing mode
141 SubclassWin(m_hWnd);
142
143 SetFont(GetParent()->GetFont());
144
145 SetSize(x, y, width, height);
146
147 return TRUE;
148 }
149
150 bool wxStaticBitmap::ImageIsOk() const
151 {
152 return m_image && m_image->Ok();
153 }
154
155 void wxStaticBitmap::Free()
156 {
157 delete m_image;
158
159 m_image = NULL;
160 }
161
162 wxSize wxStaticBitmap::DoGetBestSize() const
163 {
164 // reuse the current size (as wxWindow does) instead of using some
165 // arbitrary default size (as wxControl, our immediate base class, does)
166 return wxWindow::DoGetBestSize();
167 }
168
169 void wxStaticBitmap::SetImage(const wxGDIImage* image)
170 {
171 Free();
172
173 const wxIcon *icon = wxDynamicCast(image, wxIcon);
174 m_isIcon = icon != NULL;
175 if ( m_isIcon )
176 {
177 m_image = new wxIcon(*icon);
178 }
179 else
180 {
181 wxASSERT_MSG( wxDynamicCast(image, wxBitmap),
182 _T("not an icon and not a bitmap?") );
183
184 const wxBitmap *bitmap = (wxBitmap *)image;
185
186 m_image = new wxBitmap(*bitmap);
187 }
188
189 int x, y;
190 int w, h;
191 GetPosition(&x, &y);
192 GetSize(&w, &h);
193
194 #ifdef __WIN32__
195 HANDLE handle = (HANDLE)m_image->GetHandle();
196 ::SendMessage(GetHwnd(), STM_SETIMAGE,
197 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
198 #endif // Win32
199
200 if ( ImageIsOk() )
201 {
202 int width = image->GetWidth(),
203 height = image->GetHeight();
204 if ( width && height )
205 {
206 w = width;
207 h = height;
208
209 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
210 }
211 }
212
213 RECT rect;
214 rect.left = x;
215 rect.top = y;
216 rect.right = x + w;
217 rect.bottom = y + h;
218 InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
219 }
220
221 // under Win32 we use the standard static control style for this
222 #ifdef __WIN16__
223 bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
224 {
225 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
226
227 wxCHECK_MSG( !m_isIcon, FALSE, _T("icons not supported in wxStaticBitmap") );
228
229 wxBitmap* bitmap = (wxBitmap *)m_image;
230 if ( !bitmap->Ok() )
231 return FALSE;
232
233 HDC hDC = lpDIS->hDC;
234 HDC memDC = ::CreateCompatibleDC(hDC);
235
236 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
237
238 if (!old)
239 return FALSE;
240
241 int x = lpDIS->rcItem.left;
242 int y = lpDIS->rcItem.top;
243 int width = lpDIS->rcItem.right - x;
244 int height = lpDIS->rcItem.bottom - y;
245
246 // Centre the bitmap in the control area
247 int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2));
248 int y1 = (int) (y + ((height - bitmap->GetHeight()) / 2));
249
250 ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY);
251
252 ::SelectObject(memDC, old);
253
254 ::DeleteDC(memDC);
255
256 return TRUE;
257 }
258 #endif // Win16
259
260 // We need this or the control can never be moved e.g. in Dialog Editor.
261 long wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
262 WXWPARAM wParam,
263 WXLPARAM lParam)
264 {
265 // Ensure that static items get messages. Some controls don't like this
266 // message to be intercepted (e.g. RichEdit), hence the tests.
267 if ( nMsg == WM_NCHITTEST )
268 return (long)HTCLIENT;
269
270 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
271 }
272