]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/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 | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
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 | #if wxUSE_BMPBUTTON | |
24 | ||
25 | #ifndef WX_PRECOMP | |
26 | #include "wx/bmpbuttn.h" | |
27 | #include "wx/log.h" | |
28 | #include "wx/dcmemory.h" | |
29 | #endif | |
30 | ||
31 | #include "wx/msw/private.h" | |
32 | #include "wx/image.h" | |
33 | ||
34 | // ---------------------------------------------------------------------------- | |
35 | // macros | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
38 | #if wxUSE_EXTENDED_RTTI | |
39 | ||
40 | WX_DEFINE_FLAGS( wxBitmapButtonStyle ) | |
41 | ||
42 | wxBEGIN_FLAGS( wxBitmapButtonStyle ) | |
43 | // new style border flags, we put them first to | |
44 | // use them for streaming out | |
45 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) | |
46 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
47 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
48 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
49 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
50 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
51 | ||
52 | // old style border flags | |
53 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) | |
54 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
55 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
56 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
57 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
58 | wxFLAGS_MEMBER(wxBORDER) | |
59 | ||
60 | // standard window styles | |
61 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
62 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
63 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
64 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
65 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) | |
66 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) | |
67 | wxFLAGS_MEMBER(wxVSCROLL) | |
68 | wxFLAGS_MEMBER(wxHSCROLL) | |
69 | ||
70 | wxFLAGS_MEMBER(wxBU_AUTODRAW) | |
71 | wxFLAGS_MEMBER(wxBU_LEFT) | |
72 | wxFLAGS_MEMBER(wxBU_RIGHT) | |
73 | wxFLAGS_MEMBER(wxBU_TOP) | |
74 | wxFLAGS_MEMBER(wxBU_BOTTOM) | |
75 | wxEND_FLAGS( wxBitmapButtonStyle ) | |
76 | ||
77 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxBitmapButton, wxButton,"wx/bmpbuttn.h") | |
78 | ||
79 | wxBEGIN_PROPERTIES_TABLE(wxBitmapButton) | |
80 | wxPROPERTY_FLAGS( WindowStyle , wxBitmapButtonStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
81 | wxEND_PROPERTIES_TABLE() | |
82 | ||
83 | wxBEGIN_HANDLERS_TABLE(wxBitmapButton) | |
84 | wxEND_HANDLERS_TABLE() | |
85 | ||
86 | wxCONSTRUCTOR_5( wxBitmapButton , wxWindow* , Parent , wxWindowID , Id , wxBitmap , Bitmap , wxPoint , Position , wxSize , Size ) | |
87 | ||
88 | #else | |
89 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton) | |
90 | #endif | |
91 | ||
92 | /* | |
93 | TODO PROPERTIES : | |
94 | ||
95 | long "style" , wxBU_AUTODRAW | |
96 | bool "default" , 0 | |
97 | bitmap "selected" , | |
98 | bitmap "focus" , | |
99 | bitmap "disabled" , | |
100 | */ | |
101 | ||
102 | #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1) | |
103 | ||
104 | bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, | |
105 | const wxBitmap& bitmap, | |
106 | const wxPoint& pos, | |
107 | const wxSize& size, long style, | |
108 | const wxValidator& wxVALIDATOR_PARAM(validator), | |
109 | const wxString& name) | |
110 | { | |
111 | m_bmpNormal = bitmap; | |
112 | SetName(name); | |
113 | ||
114 | #if wxUSE_VALIDATORS | |
115 | SetValidator(validator); | |
116 | #endif // wxUSE_VALIDATORS | |
117 | ||
118 | parent->AddChild(this); | |
119 | ||
120 | m_backgroundColour = parent->GetBackgroundColour(); | |
121 | m_foregroundColour = parent->GetForegroundColour(); | |
122 | m_windowStyle = style; | |
123 | ||
124 | if ( style & wxBU_AUTODRAW ) | |
125 | { | |
126 | m_marginX = wxDEFAULT_BUTTON_MARGIN; | |
127 | m_marginY = wxDEFAULT_BUTTON_MARGIN; | |
128 | } | |
129 | ||
130 | if (id == wxID_ANY) | |
131 | m_windowId = NewControlId(); | |
132 | else | |
133 | m_windowId = id; | |
134 | ||
135 | long msStyle = WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW ; | |
136 | ||
137 | if ( m_windowStyle & wxCLIP_SIBLINGS ) | |
138 | msStyle |= WS_CLIPSIBLINGS; | |
139 | ||
140 | #ifdef __WIN32__ | |
141 | if(m_windowStyle & wxBU_LEFT) | |
142 | msStyle |= BS_LEFT; | |
143 | if(m_windowStyle & wxBU_RIGHT) | |
144 | msStyle |= BS_RIGHT; | |
145 | if(m_windowStyle & wxBU_TOP) | |
146 | msStyle |= BS_TOP; | |
147 | if(m_windowStyle & wxBU_BOTTOM) | |
148 | msStyle |= BS_BOTTOM; | |
149 | #endif | |
150 | ||
151 | m_hWnd = (WXHWND) CreateWindowEx( | |
152 | 0, | |
153 | wxT("BUTTON"), | |
154 | wxEmptyString, | |
155 | msStyle, | |
156 | 0, 0, 0, 0, | |
157 | GetWinHwnd(parent), | |
158 | (HMENU)m_windowId, | |
159 | wxGetInstance(), | |
160 | NULL | |
161 | ); | |
162 | ||
163 | // Subclass again for purposes of dialog editing mode | |
164 | SubclassWin(m_hWnd); | |
165 | ||
166 | SetPosition(pos); | |
167 | SetBestSize(size); | |
168 | ||
169 | return true; | |
170 | } | |
171 | ||
172 | bool wxBitmapButton::SetBackgroundColour(const wxColour& colour) | |
173 | { | |
174 | if ( !wxBitmapButtonBase::SetBackgroundColour(colour) ) | |
175 | { | |
176 | // didn't change | |
177 | return false; | |
178 | } | |
179 | ||
180 | // invalidate the brush, it will be recreated the next time it's needed | |
181 | m_brushDisabled = wxNullBrush; | |
182 | ||
183 | return true; | |
184 | } | |
185 | ||
186 | // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN | |
187 | #define FOCUS_MARGIN 3 | |
188 | ||
189 | bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
190 | { | |
191 | #ifndef __WXWINCE__ | |
192 | long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE); | |
193 | if (style & BS_BITMAP) | |
194 | { | |
195 | // Let default procedure draw the bitmap, which is defined | |
196 | // in the Windows resource. | |
197 | return false; | |
198 | } | |
199 | #endif | |
200 | ||
201 | LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item; | |
202 | HDC hDC = lpDIS->hDC; | |
203 | UINT state = lpDIS->itemState; | |
204 | bool isSelected = (state & ODS_SELECTED) != 0; | |
205 | bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0; | |
206 | ||
207 | ||
208 | // choose the bitmap to use depending on the button state | |
209 | wxBitmap* bitmap; | |
210 | ||
211 | if ( isSelected && m_bmpSelected.Ok() ) | |
212 | bitmap = &m_bmpSelected; | |
213 | else if ((state & ODS_FOCUS) && m_bmpFocus.Ok()) | |
214 | bitmap = &m_bmpFocus; | |
215 | else if ((state & ODS_DISABLED) && m_bmpDisabled.Ok()) | |
216 | bitmap = &m_bmpDisabled; | |
217 | else | |
218 | bitmap = &m_bmpNormal; | |
219 | ||
220 | if ( !bitmap->Ok() ) | |
221 | return false; | |
222 | ||
223 | // centre the bitmap in the control area | |
224 | int x = lpDIS->rcItem.left; | |
225 | int y = lpDIS->rcItem.top; | |
226 | int width = lpDIS->rcItem.right - x; | |
227 | int height = lpDIS->rcItem.bottom - y; | |
228 | int wBmp = bitmap->GetWidth(); | |
229 | int hBmp = bitmap->GetHeight(); | |
230 | ||
231 | int x1,y1; | |
232 | ||
233 | if(m_windowStyle & wxBU_LEFT) | |
234 | x1 = x + (FOCUS_MARGIN+1); | |
235 | else if(m_windowStyle & wxBU_RIGHT) | |
236 | x1 = x + (width - wBmp) - (FOCUS_MARGIN+1); | |
237 | else | |
238 | x1 = x + (width - wBmp) / 2; | |
239 | ||
240 | if(m_windowStyle & wxBU_TOP) | |
241 | y1 = y + (FOCUS_MARGIN+1); | |
242 | else if(m_windowStyle & wxBU_BOTTOM) | |
243 | y1 = y + (height - hBmp) - (FOCUS_MARGIN+1); | |
244 | else | |
245 | y1 = y + (height - hBmp) / 2; | |
246 | ||
247 | if ( isSelected && autoDraw ) | |
248 | { | |
249 | x1++; | |
250 | y1++; | |
251 | } | |
252 | ||
253 | // draw the face, if auto-drawing | |
254 | if ( autoDraw ) | |
255 | { | |
256 | DrawFace((WXHDC) hDC, | |
257 | lpDIS->rcItem.left, lpDIS->rcItem.top, | |
258 | lpDIS->rcItem.right, lpDIS->rcItem.bottom, | |
259 | isSelected); | |
260 | } | |
261 | ||
262 | // draw the bitmap | |
263 | wxDC dst; | |
264 | dst.SetHDC((WXHDC) hDC, false); | |
265 | dst.DrawBitmap(*bitmap, x1, y1, true); | |
266 | ||
267 | // draw focus / disabled state, if auto-drawing | |
268 | if ( (state & ODS_DISABLED) && autoDraw ) | |
269 | { | |
270 | DrawButtonDisable((WXHDC) hDC, | |
271 | lpDIS->rcItem.left, lpDIS->rcItem.top, | |
272 | lpDIS->rcItem.right, lpDIS->rcItem.bottom, | |
273 | true); | |
274 | } | |
275 | else if ( (state & ODS_FOCUS) && autoDraw ) | |
276 | { | |
277 | DrawButtonFocus((WXHDC) hDC, | |
278 | lpDIS->rcItem.left, | |
279 | lpDIS->rcItem.top, | |
280 | lpDIS->rcItem.right, | |
281 | lpDIS->rcItem.bottom, | |
282 | isSelected); | |
283 | } | |
284 | ||
285 | return true; | |
286 | } | |
287 | ||
288 | // GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF | |
289 | ||
290 | #if defined(__WIN95__) | |
291 | ||
292 | void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, | |
293 | int right, int bottom, bool sel ) | |
294 | { | |
295 | HPEN oldp; | |
296 | HPEN penHiLight; | |
297 | HPEN penLight; | |
298 | HPEN penShadow; | |
299 | HPEN penDkShadow; | |
300 | HBRUSH brushFace; | |
301 | ||
302 | // create needed pens and brush | |
303 | penHiLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DHILIGHT)); | |
304 | penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); | |
305 | penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DSHADOW)); | |
306 | penDkShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); | |
307 | // brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE)); | |
308 | // Taking the background colour fits in better with | |
309 | // Windows XP themes. | |
310 | brushFace = CreateSolidBrush(m_backgroundColour.m_pixel); | |
311 | ||
312 | // draw the rectangle | |
313 | RECT rect; | |
314 | rect.left = left; | |
315 | rect.right = right; | |
316 | rect.top = top; | |
317 | rect.bottom = bottom; | |
318 | FillRect((HDC) dc, &rect, brushFace); | |
319 | ||
320 | // draw the border | |
321 | oldp = (HPEN) SelectObject( (HDC) dc, sel? penDkShadow : penHiLight); | |
322 | ||
323 | wxDrawLine((HDC) dc, left, top, right-1, top); | |
324 | wxDrawLine((HDC) dc, left, top+1, left, bottom-1); | |
325 | ||
326 | SelectObject( (HDC) dc, sel? penShadow : penLight); | |
327 | wxDrawLine((HDC) dc, left+1, top+1, right-2, top+1); | |
328 | wxDrawLine((HDC) dc, left+1, top+2, left+1, bottom-2); | |
329 | ||
330 | SelectObject( (HDC) dc, sel? penLight : penShadow); | |
331 | wxDrawLine((HDC) dc, left+1, bottom-2, right-1, bottom-2); | |
332 | wxDrawLine((HDC) dc, right-2, bottom-3, right-2, top); | |
333 | ||
334 | SelectObject( (HDC) dc, sel? penHiLight : penDkShadow); | |
335 | wxDrawLine((HDC) dc, left, bottom-1, right+2, bottom-1); | |
336 | wxDrawLine((HDC) dc, right-1, bottom-2, right-1, top-1); | |
337 | ||
338 | // delete allocated resources | |
339 | SelectObject((HDC) dc,oldp); | |
340 | DeleteObject(penHiLight); | |
341 | DeleteObject(penLight); | |
342 | DeleteObject(penShadow); | |
343 | DeleteObject(penDkShadow); | |
344 | DeleteObject(brushFace); | |
345 | } | |
346 | ||
347 | #else | |
348 | ||
349 | void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, | |
350 | int right, int bottom, bool sel ) | |
351 | { | |
352 | HPEN oldp; | |
353 | HPEN penBorder; | |
354 | HPEN penLight; | |
355 | HPEN penShadow; | |
356 | HBRUSH brushFace; | |
357 | ||
358 | // create needed pens and brush | |
359 | penBorder = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWFRAME)); | |
360 | penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); | |
361 | penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNHIGHLIGHT)); | |
362 | brushFace = CreateSolidBrush(COLOR_BTNFACE); | |
363 | ||
364 | // draw the rectangle | |
365 | RECT rect; | |
366 | rect.left = left; | |
367 | rect.right = right; | |
368 | rect.top = top; | |
369 | rect.bottom = bottom; | |
370 | FillRect((HDC) dc, &rect, brushFace); | |
371 | ||
372 | // draw the border | |
373 | oldp = (HPEN) SelectObject( (HDC) dc, penBorder); | |
374 | MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top); | |
375 | MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1); | |
376 | MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1); | |
377 | MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1); | |
378 | ||
379 | SelectObject( (HDC) dc, penShadow); | |
380 | if (sel) | |
381 | { | |
382 | MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL); | |
383 | LineTo((HDC) dc, left+1 ,top+1); | |
384 | LineTo((HDC) dc, right-2 ,top+1); | |
385 | } | |
386 | else | |
387 | { | |
388 | MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL); | |
389 | LineTo((HDC) dc, right-2 ,bottom-2); | |
390 | LineTo((HDC) dc, right-2 ,top); | |
391 | ||
392 | MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL); | |
393 | LineTo((HDC) dc, right-3 ,bottom-3); | |
394 | LineTo((HDC) dc, right-3 ,top+1); | |
395 | ||
396 | SelectObject( (HDC) dc, penLight); | |
397 | ||
398 | MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL); | |
399 | LineTo((HDC) dc, left+1 ,top+1); | |
400 | LineTo((HDC) dc, right-2 ,top+1); | |
401 | } | |
402 | ||
403 | // delete allocated resources | |
404 | SelectObject((HDC) dc,oldp); | |
405 | DeleteObject(penBorder); | |
406 | DeleteObject(penLight); | |
407 | DeleteObject(penShadow); | |
408 | DeleteObject(brushFace); | |
409 | } | |
410 | ||
411 | #endif // defined(__WIN95__) | |
412 | ||
413 | ||
414 | void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, | |
415 | int bottom, bool WXUNUSED(sel) ) | |
416 | { | |
417 | RECT rect; | |
418 | rect.left = left; | |
419 | rect.top = top; | |
420 | rect.right = right; | |
421 | rect.bottom = bottom; | |
422 | InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN ); | |
423 | ||
424 | // GRG: the focus rectangle should not move when the button is pushed! | |
425 | /* | |
426 | if ( sel ) | |
427 | OffsetRect( &rect, 1, 1 ); | |
428 | */ | |
429 | ||
430 | DrawFocusRect( (HDC) dc, &rect ); | |
431 | } | |
432 | ||
433 | void | |
434 | wxBitmapButton::DrawButtonDisable( WXHDC dc, | |
435 | int left, int top, int right, int bottom, | |
436 | bool with_marg ) | |
437 | { | |
438 | if ( !m_brushDisabled.Ok() ) | |
439 | { | |
440 | // draw a bitmap with two black and two background colour pixels | |
441 | wxBitmap bmp(2, 2); | |
442 | wxMemoryDC dc; | |
443 | dc.SelectObject(bmp); | |
444 | dc.SetPen(*wxBLACK_PEN); | |
445 | dc.DrawPoint(0, 0); | |
446 | dc.DrawPoint(1, 1); | |
447 | dc.SetPen(GetBackgroundColour()); | |
448 | dc.DrawPoint(0, 1); | |
449 | dc.DrawPoint(1, 0); | |
450 | ||
451 | m_brushDisabled = wxBrush(bmp); | |
452 | } | |
453 | ||
454 | SelectInHDC selectBrush((HDC)dc, GetHbrushOf(m_brushDisabled)); | |
455 | ||
456 | // ROP for "dest |= pattern" operation -- as it doesn't have a standard | |
457 | // name, give it our own | |
458 | static const DWORD PATTERNPAINT = 0xFA0089UL; | |
459 | ||
460 | if ( with_marg ) | |
461 | { | |
462 | left += m_marginX; | |
463 | top += m_marginY; | |
464 | right -= 2 * m_marginX; | |
465 | bottom -= 2 * m_marginY; | |
466 | } | |
467 | ||
468 | ::PatBlt( (HDC) dc, left, top, right, bottom, PATTERNPAINT); | |
469 | } | |
470 | ||
471 | void wxBitmapButton::SetDefault() | |
472 | { | |
473 | wxButton::SetDefault(); | |
474 | } | |
475 | ||
476 | wxSize wxBitmapButton::DoGetBestSize() const | |
477 | { | |
478 | wxSize best; | |
479 | if (m_bmpNormal.Ok()) | |
480 | { | |
481 | best.x = m_bmpNormal.GetWidth() + 2*m_marginX; | |
482 | best.y = m_bmpNormal.GetHeight() + 2*m_marginY; | |
483 | } | |
484 | ||
485 | // all buttons have at least the standard size unless the user explicitly | |
486 | // wants them to be of smaller size and used wxBU_EXACTFIT style when | |
487 | // creating the button | |
488 | if ( !HasFlag(wxBU_EXACTFIT) ) | |
489 | { | |
490 | wxSize sz = GetDefaultSize(); | |
491 | if (best.x > sz.x) | |
492 | sz.x = best.x; | |
493 | if (best.y > sz.y) | |
494 | sz.y = best.y; | |
495 | } | |
496 | ||
497 | return best; | |
498 | } | |
499 | ||
500 | #endif // wxUSE_BMPBUTTON | |
501 |