]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbmp.cpp
909a09c5325afee6f66e70241dba40d1dcbb3c57
[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 if ( !m_isIcon )
89 {
90 const wxBitmap& bmp = (const wxBitmap&)bitmap;
91 wxMask *mask = bmp.GetMask();
92 if ( mask && mask->GetMaskBitmap() )
93 {
94 icon = new wxIcon;
95 icon->CopyFromBitmap(bmp);
96
97 m_isIcon = TRUE;
98 }
99 }
100
101 #ifdef __WIN32__
102 // create a static control with either SS_BITMAP or SS_ICON style depending
103 // on what we have here
104 const wxChar *classname = wxT("STATIC");
105 int winstyle = m_isIcon ? SS_ICON : SS_BITMAP;
106 #else // Win16
107 const wxChar *classname = wxT("BUTTON");
108 int winstyle = BS_OWNERDRAW;
109 #endif // Win32
110
111 m_hWnd = (WXHWND)::CreateWindow
112 (
113 classname,
114 wxT(""),
115 winstyle | WS_CHILD | WS_VISIBLE | WS_DISABLED,
116 0, 0, 0, 0,
117 (HWND)parent->GetHWND(),
118 (HMENU)m_windowId,
119 wxGetInstance(),
120 NULL
121 );
122
123 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static bitmap") );
124
125 SetImage(icon ? icon : &bitmap);
126 delete icon; // may be NULL, ok
127
128 // Subclass again for purposes of dialog editing mode
129 SubclassWin(m_hWnd);
130
131 SetFont(GetParent()->GetFont());
132
133 SetSize(x, y, width, height);
134
135 return TRUE;
136 }
137
138 bool wxStaticBitmap::ImageIsOk() const
139 {
140 return m_image && m_image->Ok();
141 }
142
143 void wxStaticBitmap::Free()
144 {
145 delete m_image;
146
147 m_image = NULL;
148 }
149
150 wxSize wxStaticBitmap::DoGetBestSize() const
151 {
152 // reuse the current size (as wxWindow does) instead of using some
153 // arbitrary default size (as wxControl, our immediate base class, does)
154 return wxWindow::DoGetBestSize();
155 }
156
157 void wxStaticBitmap::SetImage(const wxGDIImage* image)
158 {
159 Free();
160
161 const wxIcon *icon = wxDynamicCast(image, wxIcon);
162 m_isIcon = icon != NULL;
163 if ( m_isIcon )
164 {
165 m_image = new wxIcon(*icon);
166 }
167 else
168 {
169 wxASSERT_MSG( wxDynamicCast(image, wxBitmap),
170 _T("not an icon and not a bitmap?") );
171
172 const wxBitmap *bitmap = (wxBitmap *)image;
173
174 m_image = new wxBitmap(*bitmap);
175 }
176
177 int x, y;
178 int w, h;
179 GetPosition(&x, &y);
180 GetSize(&w, &h);
181
182 #ifdef __WIN32__
183 HANDLE handle = (HANDLE)m_image->GetHandle();
184 ::SendMessage(GetHwnd(), STM_SETIMAGE,
185 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
186 #endif // Win32
187
188 if ( ImageIsOk() )
189 {
190 int width = image->GetWidth(),
191 height = image->GetHeight();
192 if ( width && height )
193 {
194 w = width;
195 h = height;
196
197 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
198 }
199 }
200
201 RECT rect;
202 rect.left = x;
203 rect.top = y;
204 rect.right = x + w;
205 rect.bottom = y + h;
206 InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
207 }
208
209 // under Win32 we use the standard static control style for this
210 #ifdef __WIN16__
211 bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
212 {
213 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
214
215 wxCHECK_MSG( !m_isIcon, FALSE, _T("icons not supported in wxStaticBitmap") );
216
217 wxBitmap* bitmap = (wxBitmap *)m_image;
218 if ( !bitmap->Ok() )
219 return FALSE;
220
221 HDC hDC = lpDIS->hDC;
222 HDC memDC = ::CreateCompatibleDC(hDC);
223
224 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
225
226 if (!old)
227 return FALSE;
228
229 int x = lpDIS->rcItem.left;
230 int y = lpDIS->rcItem.top;
231 int width = lpDIS->rcItem.right - x;
232 int height = lpDIS->rcItem.bottom - y;
233
234 // Centre the bitmap in the control area
235 int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2));
236 int y1 = (int) (y + ((height - bitmap->GetHeight()) / 2));
237
238 ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY);
239
240 ::SelectObject(memDC, old);
241
242 ::DeleteDC(memDC);
243
244 return TRUE;
245 }
246 #endif // Win16
247
248 // We need this or the control can never be moved e.g. in Dialog Editor.
249 long wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
250 WXWPARAM wParam,
251 WXLPARAM lParam)
252 {
253 // Ensure that static items get messages. Some controls don't like this
254 // message to be intercepted (e.g. RichEdit), hence the tests.
255 if ( nMsg == WM_NCHITTEST )
256 return (long)HTCLIENT;
257
258 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
259 }
260