]>
Commit | Line | Data |
---|---|---|
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 | ||
0c589ad0 BM |
31 | #include "wx/window.h" |
32 | #include "wx/msw/private.h" | |
33 | ||
2bda0e17 | 34 | #ifndef WX_PRECOMP |
0c589ad0 | 35 | #include "wx/icon.h" |
9e3e0821 | 36 | #include "wx/statbmp.h" |
2bda0e17 KB |
37 | #endif |
38 | ||
39 | #include <stdio.h> | |
2bda0e17 | 40 | |
9e3e0821 VZ |
41 | // --------------------------------------------------------------------------- |
42 | // macors | |
43 | // --------------------------------------------------------------------------- | |
44 | ||
2bda0e17 | 45 | #if !USE_SHARED_LIBRARY |
9e3e0821 | 46 | IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl) |
2bda0e17 KB |
47 | #endif |
48 | ||
9e3e0821 VZ |
49 | // =========================================================================== |
50 | // implementation | |
51 | // =========================================================================== | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | // wxStaticBitmap | |
55 | // --------------------------------------------------------------------------- | |
2bda0e17 | 56 | |
debe6624 | 57 | bool wxStaticBitmap::Create(wxWindow *parent, wxWindowID id, |
9e3e0821 VZ |
58 | const wxBitmap& bitmap, |
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 | ||
85 | m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon)); | |
86 | ||
87 | #ifdef __WIN32__ | |
88 | // create a static control with either SS_BITMAP or SS_ICON style depending | |
89 | // on what we have here | |
837e5743 | 90 | const wxChar *classname = _T("STATIC"); |
9e3e0821 VZ |
91 | int winstyle = m_isIcon ? SS_ICON : SS_BITMAP; |
92 | #else // Win16 | |
837e5743 | 93 | const wxChar *classname = _T("BUTTON"); |
58a33cb4 | 94 | int winstyle = BS_OWNERDRAW; |
9e3e0821 VZ |
95 | #endif // Win32 |
96 | ||
97 | m_hWnd = (WXHWND)::CreateWindow | |
98 | ( | |
99 | classname, | |
837e5743 | 100 | _T(""), |
9e3e0821 VZ |
101 | winstyle | WS_CHILD | WS_VISIBLE, |
102 | 0, 0, 0, 0, | |
103 | (HWND)parent->GetHWND(), | |
104 | (HMENU)m_windowId, | |
105 | wxGetInstance(), | |
106 | NULL | |
107 | ); | |
108 | ||
837e5743 | 109 | wxCHECK_MSG( m_hWnd, FALSE, _T("Failed to create static bitmap") ); |
9e3e0821 VZ |
110 | |
111 | SetBitmap(bitmap); | |
112 | ||
113 | // Subclass again for purposes of dialog editing mode | |
114 | SubclassWin(m_hWnd); | |
115 | ||
116 | SetFont(GetParent()->GetFont()); | |
117 | ||
118 | SetSize(x, y, width, height); | |
119 | ||
120 | return TRUE; | |
121 | } | |
122 | ||
123 | bool wxStaticBitmap::ImageIsOk() const | |
124 | { | |
125 | if ( m_isIcon && m_image.icon ) | |
126 | return m_image.icon->Ok(); | |
127 | else if ( m_image.bitmap ) | |
128 | return m_image.bitmap->Ok(); | |
129 | else | |
130 | return FALSE; | |
131 | } | |
132 | ||
133 | void wxStaticBitmap::Free() | |
134 | { | |
135 | if ( m_isIcon ) | |
136 | delete m_image.icon; | |
137 | else | |
138 | delete m_image.bitmap; | |
139 | ||
140 | m_image.icon = NULL; | |
2bda0e17 KB |
141 | } |
142 | ||
bfc6fde4 | 143 | void wxStaticBitmap::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
2bda0e17 | 144 | { |
9e3e0821 VZ |
145 | int currentX, currentY; |
146 | GetPosition(¤tX, ¤tY); | |
147 | int x1 = x; | |
148 | int y1 = y; | |
2bda0e17 | 149 | |
9e3e0821 VZ |
150 | if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
151 | x1 = currentX; | |
152 | if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
153 | y1 = currentY; | |
2bda0e17 | 154 | |
9e3e0821 | 155 | AdjustForParentClientOrigin(x1, y1, sizeFlags); |
81d66cf3 | 156 | |
9e3e0821 VZ |
157 | int actualWidth = width; |
158 | int actualHeight = height; | |
2bda0e17 | 159 | |
9e3e0821 VZ |
160 | int ww, hh; |
161 | GetSize(&ww, &hh); | |
2bda0e17 | 162 | |
9e3e0821 VZ |
163 | // If we're prepared to use the existing width, then... |
164 | if (width == -1 && ((sizeFlags & wxSIZE_AUTO_WIDTH) != wxSIZE_AUTO_WIDTH)) | |
165 | actualWidth = ww; | |
166 | else | |
167 | actualWidth = width; | |
2bda0e17 | 168 | |
9e3e0821 VZ |
169 | // If we're prepared to use the existing height, then... |
170 | if (height == -1 && ((sizeFlags & wxSIZE_AUTO_HEIGHT) != wxSIZE_AUTO_HEIGHT)) | |
171 | actualHeight = hh; | |
172 | else | |
173 | actualHeight = height; | |
2bda0e17 | 174 | |
9e3e0821 | 175 | MoveWindow((HWND) GetHWND(), x1, y1, actualWidth, actualHeight, TRUE); |
2bda0e17 KB |
176 | } |
177 | ||
178 | void wxStaticBitmap::SetBitmap(const wxBitmap& bitmap) | |
179 | { | |
9e3e0821 VZ |
180 | Free(); |
181 | ||
182 | m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon)); | |
183 | if ( m_isIcon ) | |
184 | m_image.icon = new wxIcon((const wxIcon&)bitmap); | |
185 | else | |
186 | m_image.bitmap = new wxBitmap(bitmap); | |
187 | ||
188 | int x, y; | |
189 | int w, h; | |
190 | GetPosition(&x, &y); | |
191 | GetSize(&w, &h); | |
192 | RECT rect = { x, y, x + w, y + h }; | |
193 | ||
194 | #ifdef __WIN32__ | |
195 | HANDLE handle = m_isIcon ? (HANDLE)m_image.icon->GetHICON() | |
196 | : (HANDLE)m_image.bitmap->GetHBITMAP(); | |
197 | ::SendMessage((HWND)m_hWnd, STM_SETIMAGE, | |
198 | m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle); | |
199 | #endif // Win32 | |
200 | ||
201 | if ( ImageIsOk() ) | |
202 | { | |
203 | int width = bitmap.GetWidth(), | |
204 | height = bitmap.GetHeight(); | |
205 | if ( width && height ) | |
206 | { | |
207 | ::MoveWindow((HWND)GetHWND(), x, y, width, height, FALSE); | |
208 | } | |
209 | } | |
210 | ||
211 | InvalidateRect((HWND)GetParent()->GetHWND(), &rect, TRUE); | |
2bda0e17 KB |
212 | } |
213 | ||
9e3e0821 VZ |
214 | // under Win32 we use the standard static control style for this |
215 | #ifdef __WIN16__ | |
2bda0e17 KB |
216 | bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item) |
217 | { | |
2bda0e17 KB |
218 | LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item; |
219 | ||
9e3e0821 | 220 | wxBitmap* bitmap = m_image.bitmap; |
fd3f686c VZ |
221 | if ( !bitmap->Ok() ) |
222 | return FALSE; | |
2bda0e17 | 223 | |
fd3f686c VZ |
224 | HDC hDC = lpDIS->hDC; |
225 | HDC memDC = ::CreateCompatibleDC(hDC); | |
2bda0e17 | 226 | |
fd3f686c | 227 | HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP()); |
2bda0e17 | 228 | |
fd3f686c VZ |
229 | if (!old) |
230 | return FALSE; | |
2bda0e17 | 231 | |
9e3e0821 VZ |
232 | int x = lpDIS->rcItem.left; |
233 | int y = lpDIS->rcItem.top; | |
234 | int width = lpDIS->rcItem.right - x; | |
235 | int height = lpDIS->rcItem.bottom - y; | |
2bda0e17 | 236 | |
fd3f686c VZ |
237 | // Centre the bitmap in the control area |
238 | int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2)); | |
239 | int y1 = (int) (y + ((height - bitmap->GetHeight()) / 2)); | |
2bda0e17 | 240 | |
fd3f686c | 241 | ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY); |
2bda0e17 | 242 | |
fd3f686c | 243 | ::SelectObject(memDC, old); |
2bda0e17 KB |
244 | |
245 | ::DeleteDC(memDC); | |
246 | ||
247 | return TRUE; | |
248 | } | |
249 | ||
9e3e0821 VZ |
250 | long wxStaticBitmap::MSWWindowProc(WXUINT nMsg, |
251 | WXWPARAM wParam, | |
252 | WXLPARAM lParam) | |
2bda0e17 | 253 | { |
9e3e0821 VZ |
254 | // Ensure that static items get messages. Some controls don't like this |
255 | // message to be intercepted (e.g. RichEdit), hence the tests. | |
256 | if ( nMsg == WM_NCHITTEST ) | |
257 | return (long)HTCLIENT; | |
2bda0e17 | 258 | |
9e3e0821 | 259 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); |
2bda0e17 | 260 | } |
9e3e0821 | 261 | #endif // Win16 |