]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: bmpbuttn.cpp | |
3 | // Purpose: wxBitmapButton | |
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 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "bmpbuttn.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
10a0bdb1 | 24 | #include "wx/bmpbuttn.h" |
84f19880 | 25 | #include "wx/log.h" |
2bda0e17 KB |
26 | #endif |
27 | ||
28 | #include "wx/msw/private.h" | |
29 | ||
2bda0e17 | 30 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton) |
2bda0e17 KB |
31 | |
32 | #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1) | |
33 | ||
debe6624 | 34 | bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap, |
2bda0e17 | 35 | const wxPoint& pos, |
debe6624 | 36 | const wxSize& size, long style, |
2bda0e17 KB |
37 | const wxValidator& validator, |
38 | const wxString& name) | |
39 | { | |
40 | m_buttonBitmap = bitmap; | |
41 | SetName(name); | |
11b6a93b VZ |
42 | |
43 | #if wxUSE_VALIDATORS | |
2bda0e17 | 44 | SetValidator(validator); |
11b6a93b | 45 | #endif // wxUSE_VALIDATORS |
2bda0e17 KB |
46 | |
47 | parent->AddChild(this); | |
48 | ||
5ab1fa8e GRG |
49 | m_backgroundColour = parent->GetBackgroundColour(); |
50 | m_foregroundColour = parent->GetForegroundColour(); | |
2bda0e17 KB |
51 | m_windowStyle = style; |
52 | m_marginX = 0; | |
53 | m_marginY = 0; | |
54 | ||
55 | if ( style & wxBU_AUTODRAW ) | |
56 | { | |
fd3f686c VZ |
57 | m_marginX = wxDEFAULT_BUTTON_MARGIN; |
58 | m_marginY = wxDEFAULT_BUTTON_MARGIN; | |
2bda0e17 KB |
59 | } |
60 | ||
61 | int x = pos.x; | |
62 | int y = pos.y; | |
63 | int width = size.x; | |
64 | int height = size.y; | |
65 | ||
66 | if (id == -1) | |
67 | m_windowId = NewControlId(); | |
68 | else | |
69 | m_windowId = id; | |
70 | ||
71 | if ( width == -1 && bitmap.Ok()) | |
fd3f686c | 72 | width = bitmap.GetWidth() + 2*m_marginX; |
2bda0e17 KB |
73 | |
74 | if ( height == -1 && bitmap.Ok()) | |
fd3f686c | 75 | height = bitmap.GetHeight() + 2*m_marginY; |
2bda0e17 | 76 | |
42e69d6b VZ |
77 | m_hWnd = (WXHWND)CreateWindowEx |
78 | ( | |
79 | 0, | |
223d09f6 KB |
80 | wxT("BUTTON"), |
81 | wxT(""), | |
42e69d6b VZ |
82 | WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW , |
83 | 0, 0, 0, 0, | |
84 | GetWinHwnd(parent), | |
85 | (HMENU)m_windowId, | |
86 | wxGetInstance(), | |
87 | NULL | |
88 | ); | |
2bda0e17 KB |
89 | |
90 | // Subclass again for purposes of dialog editing mode | |
42e69d6b | 91 | SubclassWin(m_hWnd); |
2bda0e17 | 92 | |
5ab1fa8e | 93 | SetFont(parent->GetFont()); |
2bda0e17 KB |
94 | |
95 | SetSize(x, y, width, height); | |
2bda0e17 KB |
96 | |
97 | return TRUE; | |
98 | } | |
99 | ||
100 | void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap) | |
101 | { | |
102 | m_buttonBitmap = bitmap; | |
103 | } | |
104 | ||
105 | bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
106 | { | |
2bda0e17 | 107 | #if defined(__WIN95__) |
ba681060 | 108 | long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE); |
fd3f686c VZ |
109 | if (style & BS_BITMAP) |
110 | { | |
fd3f686c VZ |
111 | // Let default procedure draw the bitmap, which is defined |
112 | // in the Windows resource. | |
113 | return FALSE; | |
114 | } | |
2bda0e17 KB |
115 | #endif |
116 | ||
117 | LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item; | |
118 | ||
d9c8e68e VZ |
119 | // choose the bitmap to use depending on the buttons state |
120 | wxBitmap* bitmap; | |
2bda0e17 | 121 | |
42e69d6b | 122 | UINT state = lpDIS->itemState; |
d9c8e68e VZ |
123 | bool isSelected = (state & ODS_SELECTED) != 0; |
124 | if ( isSelected && m_buttonBitmapSelected.Ok() ) | |
42e69d6b VZ |
125 | bitmap = &m_buttonBitmapSelected; |
126 | else if ((state & ODS_FOCUS) && m_buttonBitmapFocus.Ok()) | |
127 | bitmap = &m_buttonBitmapFocus; | |
128 | else if ((state & ODS_DISABLED) && m_buttonBitmapDisabled.Ok()) | |
129 | bitmap = &m_buttonBitmapDisabled; | |
d9c8e68e VZ |
130 | else |
131 | bitmap = &m_buttonBitmap; | |
2bda0e17 | 132 | |
42e69d6b VZ |
133 | if ( !bitmap->Ok() ) |
134 | return FALSE; | |
2bda0e17 | 135 | |
d9c8e68e | 136 | // draw it on the memory DC |
42e69d6b VZ |
137 | HDC hDC = lpDIS->hDC; |
138 | HDC memDC = ::CreateCompatibleDC(hDC); | |
2bda0e17 | 139 | |
42e69d6b | 140 | HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP()); |
2bda0e17 | 141 | |
42e69d6b | 142 | if (!old) |
d9c8e68e VZ |
143 | { |
144 | wxLogLastError(_T("SelectObject")); | |
145 | ||
42e69d6b | 146 | return FALSE; |
d9c8e68e | 147 | } |
2bda0e17 | 148 | |
42e69d6b VZ |
149 | int x = lpDIS->rcItem.left; |
150 | int y = lpDIS->rcItem.top; | |
151 | int width = lpDIS->rcItem.right - x; | |
152 | int height = lpDIS->rcItem.bottom - y; | |
2bda0e17 | 153 | |
d9c8e68e VZ |
154 | int wBmp = bitmap->GetWidth(), |
155 | hBmp = bitmap->GetHeight(); | |
156 | ||
42e69d6b | 157 | // Draw the face, if auto-drawing |
d9c8e68e VZ |
158 | bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0; |
159 | if ( autoDraw ) | |
160 | { | |
161 | DrawFace((WXHDC) hDC, | |
162 | lpDIS->rcItem.left, lpDIS->rcItem.top, | |
163 | lpDIS->rcItem.right, lpDIS->rcItem.bottom, | |
164 | isSelected); | |
165 | } | |
2bda0e17 | 166 | |
fd3f686c | 167 | // Centre the bitmap in the control area |
d9c8e68e VZ |
168 | int x1 = x + (width - wBmp) / 2; |
169 | int y1 = y + (height - hBmp) / 2; | |
2bda0e17 | 170 | |
d9c8e68e | 171 | if ( isSelected && autoDraw ) |
fd3f686c | 172 | { |
d9c8e68e VZ |
173 | x1++; |
174 | y1++; | |
fd3f686c | 175 | } |
2bda0e17 | 176 | |
d9c8e68e | 177 | BOOL ok; |
8f177c8e VZ |
178 | |
179 | // no MaskBlt() under Win16 | |
180 | #ifdef __WIN32__ | |
4fe5383d VZ |
181 | wxMask *mask = bitmap->GetMask(); |
182 | if ( mask ) | |
183 | { | |
d9c8e68e VZ |
184 | // the fg ROP is applied for the pixels of the mask bitmap which are 1 |
185 | // (for a wxMask this means that this is a non transparent pixel), the | |
186 | // bg ROP is applied for all the others | |
2e8005cc | 187 | |
2e8005cc | 188 | HBRUSH hbrBackground = |
6148d1e6 | 189 | ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour())); |
2e8005cc VZ |
190 | HBRUSH hbrOld = (HBRUSH)::SelectObject(hDC, hbrBackground); |
191 | ||
d9c8e68e VZ |
192 | ok = ::MaskBlt( |
193 | hDC, x1, y1, wBmp, hBmp, // dst | |
194 | memDC, 0, 0, // src | |
195 | (HBITMAP)mask->GetMaskBitmap(), 0, 0, // mask | |
196 | MAKEROP4(SRCCOPY, // fg ROP | |
2e8005cc | 197 | PATCOPY) // bg ROP |
d9c8e68e | 198 | ); |
2e8005cc VZ |
199 | |
200 | ::SelectObject(hDC, hbrOld); | |
201 | ::DeleteObject(hbrBackground); | |
4fe5383d VZ |
202 | } |
203 | else | |
6148d1e6 VZ |
204 | { |
205 | // this will make the check below fail and BitBlt() will be used if | |
206 | // MaskBlt() is not supported (for example, under Win95) | |
207 | ok = FALSE; | |
208 | } | |
209 | ||
210 | if ( !ok ) | |
8f177c8e | 211 | #endif // Win32 |
4fe5383d | 212 | { |
d9c8e68e VZ |
213 | ok = ::BitBlt(hDC, x1, y1, wBmp, hBmp, // dst |
214 | memDC, 0, 0, // src | |
215 | SRCCOPY); // ROP | |
216 | } | |
217 | ||
218 | if ( !ok ) | |
219 | { | |
220 | wxLogLastError(_T("Mask/BitBlt()")); | |
4fe5383d | 221 | } |
2bda0e17 | 222 | |
d9c8e68e VZ |
223 | if ( (state & ODS_DISABLED) && autoDraw ) |
224 | { | |
225 | DrawButtonDisable((WXHDC) hDC, | |
226 | lpDIS->rcItem.left, lpDIS->rcItem.top, | |
227 | lpDIS->rcItem.right, lpDIS->rcItem.bottom, | |
228 | TRUE); | |
229 | } | |
230 | else if ( (state & ODS_FOCUS) && autoDraw ) | |
231 | { | |
232 | DrawButtonFocus((WXHDC) hDC, | |
233 | lpDIS->rcItem.left, | |
234 | lpDIS->rcItem.top, | |
235 | lpDIS->rcItem.right, | |
236 | lpDIS->rcItem.bottom, | |
237 | isSelected); | |
238 | } | |
2bda0e17 | 239 | |
fd3f686c | 240 | ::SelectObject(memDC, old); |
2bda0e17 KB |
241 | |
242 | ::DeleteDC(memDC); | |
243 | ||
244 | return TRUE; | |
245 | } | |
246 | ||
5ab1fa8e GRG |
247 | // GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF |
248 | ||
249 | #if defined(__WIN95__) | |
250 | ||
2bda0e17 KB |
251 | void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel ) |
252 | { | |
d9c8e68e | 253 | HPEN oldp; |
5ab1fa8e | 254 | HPEN penHiLight; |
d9c8e68e VZ |
255 | HPEN penLight; |
256 | HPEN penShadow; | |
5ab1fa8e | 257 | HPEN penDkShadow; |
d9c8e68e | 258 | HBRUSH brushFace; |
2bda0e17 | 259 | |
5ab1fa8e GRG |
260 | // create needed pens and brush |
261 | penHiLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DHILIGHT)); | |
262 | penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); | |
263 | penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DSHADOW)); | |
264 | penDkShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); | |
265 | brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE)); | |
266 | ||
267 | // draw the rectangle | |
268 | RECT rect; | |
269 | rect.left = left; | |
270 | rect.right = right; | |
271 | rect.top = top; | |
272 | rect.bottom = bottom; | |
273 | FillRect((HDC) dc, &rect, brushFace); | |
274 | ||
275 | // draw the border | |
276 | oldp = (HPEN) SelectObject( (HDC) dc, sel? penDkShadow : penHiLight); | |
277 | MoveToEx((HDC) dc, left, top, NULL); LineTo((HDC) dc, right-1, top); | |
278 | MoveToEx((HDC) dc, left, top+1, NULL); LineTo((HDC) dc, left, bottom-1); | |
279 | ||
280 | SelectObject( (HDC) dc, sel? penShadow : penLight); | |
281 | MoveToEx((HDC) dc, left+1, top+1, NULL); LineTo((HDC) dc, right-2, top+1); | |
282 | MoveToEx((HDC) dc, left+1, top+2, NULL); LineTo((HDC) dc, left+1, bottom-2); | |
283 | ||
284 | SelectObject( (HDC) dc, sel? penLight : penShadow); | |
285 | MoveToEx((HDC) dc, left+1, bottom-2, NULL); LineTo((HDC) dc, right-1, bottom-2); | |
286 | MoveToEx((HDC) dc, right-2, bottom-3, NULL); LineTo((HDC) dc, right-2, top); | |
287 | ||
288 | SelectObject( (HDC) dc, sel? penHiLight : penDkShadow); | |
289 | MoveToEx((HDC) dc, left, bottom-1, NULL); LineTo((HDC) dc, right+2, bottom-1); | |
290 | MoveToEx((HDC) dc, right-1, bottom-2, NULL); LineTo((HDC) dc, right-1, top-1); | |
291 | ||
292 | // delete allocated resources | |
293 | SelectObject((HDC) dc,oldp); | |
294 | DeleteObject(penHiLight); | |
295 | DeleteObject(penLight); | |
296 | DeleteObject(penShadow); | |
297 | DeleteObject(penDkShadow); | |
298 | DeleteObject(brushFace); | |
299 | } | |
300 | ||
301 | #else | |
2bda0e17 | 302 | |
5ab1fa8e GRG |
303 | void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel ) |
304 | { | |
305 | HPEN oldp; | |
306 | HPEN penBorder; | |
307 | HPEN penLight; | |
308 | HPEN penShadow; | |
309 | HBRUSH brushFace; | |
2bda0e17 | 310 | |
5ab1fa8e GRG |
311 | // create needed pens and brush |
312 | penBorder = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWFRAME)); | |
313 | penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); | |
314 | penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNHIGHLIGHT)); | |
315 | brushFace = CreateSolidBrush(COLOR_BTNFACE); | |
2bda0e17 | 316 | |
5ab1fa8e GRG |
317 | // draw the rectangle |
318 | RECT rect; | |
319 | rect.left = left; | |
320 | rect.right = right; | |
321 | rect.top = top; | |
322 | rect.bottom = bottom; | |
323 | FillRect((HDC) dc, &rect, brushFace); | |
2bda0e17 | 324 | |
5ab1fa8e GRG |
325 | // draw the border |
326 | oldp = (HPEN) SelectObject( (HDC) dc, penBorder); | |
d9c8e68e VZ |
327 | MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top); |
328 | MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1); | |
329 | MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1); | |
330 | MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1); | |
2bda0e17 | 331 | |
5ab1fa8e | 332 | SelectObject( (HDC) dc, penShadow); |
fd3f686c VZ |
333 | if (sel) |
334 | { | |
5ab1fa8e GRG |
335 | MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL); |
336 | LineTo((HDC) dc, left+1 ,top+1); | |
337 | LineTo((HDC) dc, right-2 ,top+1); | |
fd3f686c VZ |
338 | } |
339 | else | |
340 | { | |
5ab1fa8e GRG |
341 | MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL); |
342 | LineTo((HDC) dc, right-2 ,bottom-2); | |
343 | LineTo((HDC) dc, right-2 ,top); | |
344 | ||
345 | MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL); | |
346 | LineTo((HDC) dc, right-3 ,bottom-3); | |
347 | LineTo((HDC) dc, right-3 ,top+1); | |
348 | ||
349 | SelectObject( (HDC) dc, penLight); | |
350 | ||
351 | MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL); | |
352 | LineTo((HDC) dc, left+1 ,top+1); | |
353 | LineTo((HDC) dc, right-2 ,top+1); | |
fd3f686c | 354 | } |
2bda0e17 | 355 | |
5ab1fa8e GRG |
356 | // delete allocated resources |
357 | SelectObject((HDC) dc,oldp); | |
d9c8e68e VZ |
358 | DeleteObject(penBorder); |
359 | DeleteObject(penLight); | |
360 | DeleteObject(penShadow); | |
361 | DeleteObject(brushFace); | |
2bda0e17 KB |
362 | } |
363 | ||
5ab1fa8e GRG |
364 | #endif // defined(__WIN95__) |
365 | ||
366 | ||
4fe5383d VZ |
367 | // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN |
368 | #define FOCUS_MARGIN 3 | |
2bda0e17 KB |
369 | |
370 | void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel ) | |
371 | { | |
fd3f686c VZ |
372 | RECT rect; |
373 | rect.left = left; | |
374 | rect.top = top; | |
375 | rect.right = right; | |
376 | rect.bottom = bottom; | |
5ab1fa8e GRG |
377 | InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN ); |
378 | ||
379 | // GRG: the focus rectangle should not move when the button is pushed! | |
380 | /* | |
fd3f686c | 381 | if ( sel ) |
5ab1fa8e GRG |
382 | OffsetRect( &rect, 1, 1 ); |
383 | */ | |
384 | DrawFocusRect( (HDC) dc, &rect ); | |
2bda0e17 KB |
385 | } |
386 | ||
387 | extern HBRUSH wxDisableButtonBrush; | |
388 | void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg ) | |
389 | { | |
5ab1fa8e | 390 | HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush ); |
2bda0e17 | 391 | |
d9c8e68e | 392 | // VZ: what's this?? there is no such ROP AFAIK |
ce3ed50d | 393 | #ifdef __SALFORDC__ |
d9c8e68e | 394 | DWORD dwRop = 0xFA0089L; |
ce3ed50d | 395 | #else |
d9c8e68e | 396 | DWORD dwRop = 0xFA0089UL; |
ce3ed50d | 397 | #endif |
d9c8e68e VZ |
398 | |
399 | if ( with_marg ) | |
400 | { | |
401 | left += m_marginX; | |
402 | top += m_marginY; | |
403 | right -= 2 * m_marginX; | |
404 | bottom -= 2 * m_marginY; | |
405 | } | |
406 | ||
407 | ::PatBlt( (HDC) dc, left, top, right, bottom, dwRop); | |
408 | ||
5ab1fa8e | 409 | ::SelectObject( (HDC) dc, old ); |
2bda0e17 KB |
410 | } |
411 | ||
0655ad29 VZ |
412 | void wxBitmapButton::SetDefault() |
413 | { | |
414 | wxButton::SetDefault(); | |
415 | } |