]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/button.cpp | |
3 | // Purpose: wxButton | |
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 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_BUTTON | |
28 | ||
29 | #include "wx/button.h" | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/app.h" | |
33 | #include "wx/brush.h" | |
34 | #include "wx/panel.h" | |
35 | #include "wx/bmpbuttn.h" | |
36 | #include "wx/settings.h" | |
37 | #include "wx/dcscreen.h" | |
38 | #include "wx/dcclient.h" | |
39 | #include "wx/toplevel.h" | |
40 | #include "wx/imaglist.h" | |
41 | #endif | |
42 | ||
43 | #include "wx/stockitem.h" | |
44 | #include "wx/msw/private.h" | |
45 | #include "wx/msw/private/button.h" | |
46 | #include "wx/msw/private/dc.h" | |
47 | ||
48 | using namespace wxMSWImpl; | |
49 | ||
50 | #if wxUSE_UXTHEME | |
51 | #include "wx/msw/uxtheme.h" | |
52 | ||
53 | // no need to include tmschema.h | |
54 | #ifndef BP_PUSHBUTTON | |
55 | #define BP_PUSHBUTTON 1 | |
56 | ||
57 | #define PBS_NORMAL 1 | |
58 | #define PBS_HOT 2 | |
59 | #define PBS_PRESSED 3 | |
60 | #define PBS_DISABLED 4 | |
61 | #define PBS_DEFAULTED 5 | |
62 | ||
63 | #define TMT_CONTENTMARGINS 3602 | |
64 | #endif | |
65 | ||
66 | // provide the necessary declarations ourselves if they're missing from | |
67 | // headers | |
68 | #ifndef BCM_SETIMAGELIST | |
69 | #define BCM_SETIMAGELIST 0x1602 | |
70 | #define BCM_SETTEXTMARGIN 0x1604 | |
71 | ||
72 | enum | |
73 | { | |
74 | BUTTON_IMAGELIST_ALIGN_LEFT, | |
75 | BUTTON_IMAGELIST_ALIGN_RIGHT, | |
76 | BUTTON_IMAGELIST_ALIGN_TOP, | |
77 | BUTTON_IMAGELIST_ALIGN_BOTTOM | |
78 | }; | |
79 | ||
80 | struct BUTTON_IMAGELIST | |
81 | { | |
82 | HIMAGELIST himl; | |
83 | RECT margin; | |
84 | UINT uAlign; | |
85 | }; | |
86 | #endif | |
87 | #endif // wxUSE_UXTHEME | |
88 | ||
89 | #ifndef WM_THEMECHANGED | |
90 | #define WM_THEMECHANGED 0x031A | |
91 | #endif | |
92 | ||
93 | #ifndef ODS_NOACCEL | |
94 | #define ODS_NOACCEL 0x0100 | |
95 | #endif | |
96 | ||
97 | #ifndef ODS_NOFOCUSRECT | |
98 | #define ODS_NOFOCUSRECT 0x0200 | |
99 | #endif | |
100 | ||
101 | #ifndef DT_HIDEPREFIX | |
102 | #define DT_HIDEPREFIX 0x00100000 | |
103 | #endif | |
104 | ||
105 | // ---------------------------------------------------------------------------- | |
106 | // button image data | |
107 | // ---------------------------------------------------------------------------- | |
108 | ||
109 | // we use different data classes for owner drawn buttons and for themed XP ones | |
110 | ||
111 | class wxButtonImageData | |
112 | { | |
113 | public: | |
114 | wxButtonImageData() { } | |
115 | virtual ~wxButtonImageData() { } | |
116 | ||
117 | virtual wxBitmap GetBitmap(wxButton::State which) const = 0; | |
118 | virtual void SetBitmap(const wxBitmap& bitmap, wxButton::State which) = 0; | |
119 | ||
120 | virtual wxSize GetBitmapMargins() const = 0; | |
121 | virtual void SetBitmapMargins(wxCoord x, wxCoord y) = 0; | |
122 | ||
123 | virtual wxDirection GetBitmapPosition() const = 0; | |
124 | virtual void SetBitmapPosition(wxDirection dir) = 0; | |
125 | ||
126 | private: | |
127 | wxDECLARE_NO_COPY_CLASS(wxButtonImageData); | |
128 | }; | |
129 | ||
130 | namespace | |
131 | { | |
132 | ||
133 | // the gap between button edge and the interior area used by Windows for the | |
134 | // standard buttons | |
135 | const int OD_BUTTON_MARGIN = 4; | |
136 | ||
137 | class wxODButtonImageData : public wxButtonImageData | |
138 | { | |
139 | public: | |
140 | wxODButtonImageData(wxButton *btn, const wxBitmap& bitmap) | |
141 | { | |
142 | SetBitmap(bitmap, wxButton::State_Normal); | |
143 | ||
144 | m_dir = wxLEFT; | |
145 | ||
146 | // we use margins when we have both bitmap and text, but when we have | |
147 | // only the bitmap it should take up the entire button area | |
148 | if ( btn->ShowsLabel() ) | |
149 | { | |
150 | m_margin.x = btn->GetCharWidth(); | |
151 | m_margin.y = btn->GetCharHeight() / 2; | |
152 | } | |
153 | } | |
154 | ||
155 | virtual wxBitmap GetBitmap(wxButton::State which) const | |
156 | { | |
157 | return m_bitmaps[which]; | |
158 | } | |
159 | ||
160 | virtual void SetBitmap(const wxBitmap& bitmap, wxButton::State which) | |
161 | { | |
162 | m_bitmaps[which] = bitmap; | |
163 | } | |
164 | ||
165 | virtual wxSize GetBitmapMargins() const | |
166 | { | |
167 | return m_margin; | |
168 | } | |
169 | ||
170 | virtual void SetBitmapMargins(wxCoord x, wxCoord y) | |
171 | { | |
172 | m_margin = wxSize(x, y); | |
173 | } | |
174 | ||
175 | virtual wxDirection GetBitmapPosition() const | |
176 | { | |
177 | return m_dir; | |
178 | } | |
179 | ||
180 | virtual void SetBitmapPosition(wxDirection dir) | |
181 | { | |
182 | m_dir = dir; | |
183 | } | |
184 | ||
185 | private: | |
186 | // just store the values passed to us to be able to retrieve them later | |
187 | // from the drawing code | |
188 | wxBitmap m_bitmaps[wxButton::State_Max]; | |
189 | wxSize m_margin; | |
190 | wxDirection m_dir; | |
191 | ||
192 | wxDECLARE_NO_COPY_CLASS(wxODButtonImageData); | |
193 | }; | |
194 | ||
195 | #if wxUSE_UXTHEME | |
196 | ||
197 | // somehow the margin is one pixel greater than the value returned by | |
198 | // GetThemeMargins() call | |
199 | const int XP_BUTTON_EXTRA_MARGIN = 1; | |
200 | ||
201 | class wxXPButtonImageData : public wxButtonImageData | |
202 | { | |
203 | public: | |
204 | // we must be constructed with the size of our images as we need to create | |
205 | // the image list | |
206 | wxXPButtonImageData(wxButton *btn, const wxBitmap& bitmap) | |
207 | : m_iml(bitmap.GetWidth(), bitmap.GetHeight(), true /* use mask */, | |
208 | wxButton::State_Max), | |
209 | m_hwndBtn(GetHwndOf(btn)) | |
210 | { | |
211 | // initialize all bitmaps to normal state | |
212 | for ( int n = 0; n < wxButton::State_Max; n++ ) | |
213 | { | |
214 | m_iml.Add(bitmap); | |
215 | } | |
216 | ||
217 | m_data.himl = GetHimagelistOf(&m_iml); | |
218 | ||
219 | // use default margins | |
220 | m_data.margin.left = | |
221 | m_data.margin.right = btn->GetCharWidth(); | |
222 | m_data.margin.top = | |
223 | m_data.margin.bottom = btn->GetCharHeight() / 2; | |
224 | ||
225 | // and default alignment | |
226 | m_data.uAlign = BUTTON_IMAGELIST_ALIGN_LEFT; | |
227 | ||
228 | UpdateImageInfo(); | |
229 | } | |
230 | ||
231 | virtual wxBitmap GetBitmap(wxButton::State which) const | |
232 | { | |
233 | return m_iml.GetBitmap(which); | |
234 | } | |
235 | ||
236 | virtual void SetBitmap(const wxBitmap& bitmap, wxButton::State which) | |
237 | { | |
238 | m_iml.Replace(which, bitmap); | |
239 | ||
240 | UpdateImageInfo(); | |
241 | } | |
242 | ||
243 | virtual wxSize GetBitmapMargins() const | |
244 | { | |
245 | return wxSize(m_data.margin.left, m_data.margin.top); | |
246 | } | |
247 | ||
248 | virtual void SetBitmapMargins(wxCoord x, wxCoord y) | |
249 | { | |
250 | RECT& margin = m_data.margin; | |
251 | margin.left = | |
252 | margin.right = x; | |
253 | margin.top = | |
254 | margin.bottom = y; | |
255 | ||
256 | if ( !::SendMessage(m_hwndBtn, BCM_SETTEXTMARGIN, 0, (LPARAM)&margin) ) | |
257 | { | |
258 | wxLogDebug("SendMessage(BCM_SETTEXTMARGIN) failed"); | |
259 | } | |
260 | } | |
261 | ||
262 | virtual wxDirection GetBitmapPosition() const | |
263 | { | |
264 | switch ( m_data.uAlign ) | |
265 | { | |
266 | default: | |
267 | wxFAIL_MSG( "invalid image alignment" ); | |
268 | // fall through | |
269 | ||
270 | case BUTTON_IMAGELIST_ALIGN_LEFT: | |
271 | return wxLEFT; | |
272 | ||
273 | case BUTTON_IMAGELIST_ALIGN_RIGHT: | |
274 | return wxRIGHT; | |
275 | ||
276 | case BUTTON_IMAGELIST_ALIGN_TOP: | |
277 | return wxTOP; | |
278 | ||
279 | case BUTTON_IMAGELIST_ALIGN_BOTTOM: | |
280 | return wxBOTTOM; | |
281 | } | |
282 | } | |
283 | ||
284 | virtual void SetBitmapPosition(wxDirection dir) | |
285 | { | |
286 | UINT alignNew; | |
287 | switch ( dir ) | |
288 | { | |
289 | default: | |
290 | wxFAIL_MSG( "invalid direction" ); | |
291 | // fall through | |
292 | ||
293 | case wxLEFT: | |
294 | alignNew = BUTTON_IMAGELIST_ALIGN_LEFT; | |
295 | break; | |
296 | ||
297 | case wxRIGHT: | |
298 | alignNew = BUTTON_IMAGELIST_ALIGN_RIGHT; | |
299 | break; | |
300 | ||
301 | case wxTOP: | |
302 | alignNew = BUTTON_IMAGELIST_ALIGN_TOP; | |
303 | break; | |
304 | ||
305 | case wxBOTTOM: | |
306 | alignNew = BUTTON_IMAGELIST_ALIGN_BOTTOM; | |
307 | break; | |
308 | } | |
309 | ||
310 | if ( alignNew != m_data.uAlign ) | |
311 | { | |
312 | m_data.uAlign = alignNew; | |
313 | UpdateImageInfo(); | |
314 | } | |
315 | } | |
316 | ||
317 | private: | |
318 | void UpdateImageInfo() | |
319 | { | |
320 | if ( !::SendMessage(m_hwndBtn, BCM_SETIMAGELIST, 0, (LPARAM)&m_data) ) | |
321 | { | |
322 | wxLogDebug("SendMessage(BCM_SETIMAGELIST) failed"); | |
323 | } | |
324 | } | |
325 | ||
326 | // we store image list separately to be able to use convenient wxImageList | |
327 | // methods instead of working with raw HIMAGELIST | |
328 | wxImageList m_iml; | |
329 | ||
330 | // store the rest of the data in BCM_SETIMAGELIST-friendly form | |
331 | BUTTON_IMAGELIST m_data; | |
332 | ||
333 | // the button we're associated with | |
334 | const HWND m_hwndBtn; | |
335 | ||
336 | ||
337 | wxDECLARE_NO_COPY_CLASS(wxXPButtonImageData); | |
338 | }; | |
339 | ||
340 | #endif // wxUSE_UXTHEME | |
341 | ||
342 | } // anonymous namespace | |
343 | ||
344 | // ---------------------------------------------------------------------------- | |
345 | // macros | |
346 | // ---------------------------------------------------------------------------- | |
347 | ||
348 | #if wxUSE_EXTENDED_RTTI | |
349 | ||
350 | WX_DEFINE_FLAGS( wxButtonStyle ) | |
351 | ||
352 | wxBEGIN_FLAGS( wxButtonStyle ) | |
353 | // new style border flags, we put them first to | |
354 | // use them for streaming out | |
355 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) | |
356 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
357 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
358 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
359 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
360 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
361 | ||
362 | // old style border flags | |
363 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) | |
364 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
365 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
366 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
367 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
368 | wxFLAGS_MEMBER(wxBORDER) | |
369 | ||
370 | // standard window styles | |
371 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
372 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
373 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
374 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
375 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) | |
376 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) | |
377 | wxFLAGS_MEMBER(wxVSCROLL) | |
378 | wxFLAGS_MEMBER(wxHSCROLL) | |
379 | ||
380 | wxFLAGS_MEMBER(wxBU_LEFT) | |
381 | wxFLAGS_MEMBER(wxBU_RIGHT) | |
382 | wxFLAGS_MEMBER(wxBU_TOP) | |
383 | wxFLAGS_MEMBER(wxBU_BOTTOM) | |
384 | wxFLAGS_MEMBER(wxBU_EXACTFIT) | |
385 | wxEND_FLAGS( wxButtonStyle ) | |
386 | ||
387 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxButton, wxControl,"wx/button.h") | |
388 | ||
389 | wxBEGIN_PROPERTIES_TABLE(wxButton) | |
390 | wxEVENT_PROPERTY( Click , wxEVT_COMMAND_BUTTON_CLICKED , wxCommandEvent) | |
391 | ||
392 | wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
393 | wxPROPERTY( Label, wxString , SetLabel, GetLabel, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
394 | ||
395 | wxPROPERTY_FLAGS( WindowStyle , wxButtonStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
396 | ||
397 | wxEND_PROPERTIES_TABLE() | |
398 | ||
399 | wxBEGIN_HANDLERS_TABLE(wxButton) | |
400 | wxEND_HANDLERS_TABLE() | |
401 | ||
402 | wxCONSTRUCTOR_6( wxButton , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle ) | |
403 | ||
404 | ||
405 | #else | |
406 | IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) | |
407 | #endif | |
408 | ||
409 | // ============================================================================ | |
410 | // implementation | |
411 | // ============================================================================ | |
412 | ||
413 | // ---------------------------------------------------------------------------- | |
414 | // helper functions from wx/msw/private/button.h | |
415 | // ---------------------------------------------------------------------------- | |
416 | ||
417 | void wxMSWButton::UpdateMultilineStyle(HWND hwnd, const wxString& label) | |
418 | { | |
419 | // update BS_MULTILINE style depending on the new label (resetting it | |
420 | // doesn't seem to do anything very useful but it shouldn't hurt and we do | |
421 | // have to set it whenever the label becomes multi line as otherwise it | |
422 | // wouldn't be shown correctly as we don't use BS_MULTILINE when creating | |
423 | // the control unless it already has new lines in its label) | |
424 | long styleOld = ::GetWindowLong(hwnd, GWL_STYLE), | |
425 | styleNew; | |
426 | if ( label.find(wxT('\n')) != wxString::npos ) | |
427 | styleNew = styleOld | BS_MULTILINE; | |
428 | else | |
429 | styleNew = styleOld & ~BS_MULTILINE; | |
430 | ||
431 | if ( styleNew != styleOld ) | |
432 | ::SetWindowLong(hwnd, GWL_STYLE, styleNew); | |
433 | } | |
434 | ||
435 | wxSize wxMSWButton::GetFittingSize(wxWindow *win, const wxSize& sizeLabel) | |
436 | { | |
437 | // FIXME: this is pure guesswork, need to retrieve the real button margins | |
438 | wxSize sizeBtn = sizeLabel; | |
439 | ||
440 | sizeBtn.x += 3*win->GetCharWidth(); | |
441 | sizeBtn.y = 11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(sizeLabel.y)/10; | |
442 | ||
443 | return sizeBtn; | |
444 | } | |
445 | ||
446 | wxSize wxMSWButton::ComputeBestSize(wxControl *btn) | |
447 | { | |
448 | wxClientDC dc(btn); | |
449 | ||
450 | wxSize sizeBtn; | |
451 | dc.GetMultiLineTextExtent(btn->GetLabelText(), &sizeBtn.x, &sizeBtn.y); | |
452 | ||
453 | sizeBtn = GetFittingSize(btn, sizeBtn); | |
454 | ||
455 | // all buttons have at least the standard size unless the user explicitly | |
456 | // wants them to be of smaller size and used wxBU_EXACTFIT style when | |
457 | // creating the button | |
458 | if ( !btn->HasFlag(wxBU_EXACTFIT) ) | |
459 | { | |
460 | wxSize sizeDef = wxButton::GetDefaultSize(); | |
461 | if ( sizeBtn.x < sizeDef.x ) | |
462 | sizeBtn.x = sizeDef.x; | |
463 | if ( sizeBtn.y < sizeDef.y ) | |
464 | sizeBtn.y = sizeDef.y; | |
465 | } | |
466 | ||
467 | btn->CacheBestSize(sizeBtn); | |
468 | ||
469 | return sizeBtn; | |
470 | } | |
471 | ||
472 | // ---------------------------------------------------------------------------- | |
473 | // creation/destruction | |
474 | // ---------------------------------------------------------------------------- | |
475 | ||
476 | bool wxButton::Create(wxWindow *parent, | |
477 | wxWindowID id, | |
478 | const wxString& lbl, | |
479 | const wxPoint& pos, | |
480 | const wxSize& size, | |
481 | long style, | |
482 | const wxValidator& validator, | |
483 | const wxString& name) | |
484 | { | |
485 | wxString label(lbl); | |
486 | if (label.empty() && wxIsStockID(id)) | |
487 | { | |
488 | // On Windows, some buttons aren't supposed to have mnemonics | |
489 | label = wxGetStockLabel | |
490 | ( | |
491 | id, | |
492 | id == wxID_OK || id == wxID_CANCEL || id == wxID_CLOSE | |
493 | ? wxSTOCK_NOFLAGS | |
494 | : wxSTOCK_WITH_MNEMONIC | |
495 | ); | |
496 | } | |
497 | ||
498 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) | |
499 | return false; | |
500 | ||
501 | WXDWORD exstyle; | |
502 | WXDWORD msStyle = MSWGetStyle(style, &exstyle); | |
503 | ||
504 | // if the label contains several lines we must explicitly tell the button | |
505 | // about it or it wouldn't draw it correctly ("\n"s would just appear as | |
506 | // black boxes) | |
507 | // | |
508 | // NB: we do it here and not in MSWGetStyle() because we need the label | |
509 | // value and the label is not set yet when MSWGetStyle() is called | |
510 | msStyle |= wxMSWButton::GetMultilineStyle(label); | |
511 | ||
512 | return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, exstyle); | |
513 | } | |
514 | ||
515 | wxButton::~wxButton() | |
516 | { | |
517 | wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); | |
518 | if ( tlw && tlw->GetTmpDefaultItem() == this ) | |
519 | { | |
520 | UnsetTmpDefault(); | |
521 | } | |
522 | ||
523 | delete m_imageData; | |
524 | } | |
525 | ||
526 | // ---------------------------------------------------------------------------- | |
527 | // flags | |
528 | // ---------------------------------------------------------------------------- | |
529 | ||
530 | WXDWORD wxButton::MSWGetStyle(long style, WXDWORD *exstyle) const | |
531 | { | |
532 | // buttons never have an external border, they draw their own one | |
533 | WXDWORD msStyle = wxControl::MSWGetStyle | |
534 | ( | |
535 | (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle | |
536 | ); | |
537 | ||
538 | // we must use WS_CLIPSIBLINGS with the buttons or they would draw over | |
539 | // each other in any resizeable dialog which has more than one button in | |
540 | // the bottom | |
541 | msStyle |= WS_CLIPSIBLINGS; | |
542 | ||
543 | // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT | |
544 | // and wxBU_RIGHT to get BS_CENTER! | |
545 | if ( style & wxBU_LEFT ) | |
546 | msStyle |= BS_LEFT; | |
547 | if ( style & wxBU_RIGHT ) | |
548 | msStyle |= BS_RIGHT; | |
549 | if ( style & wxBU_TOP ) | |
550 | msStyle |= BS_TOP; | |
551 | if ( style & wxBU_BOTTOM ) | |
552 | msStyle |= BS_BOTTOM; | |
553 | #ifndef __WXWINCE__ | |
554 | // flat 2d buttons | |
555 | if ( style & wxNO_BORDER ) | |
556 | msStyle |= BS_FLAT; | |
557 | #endif // __WXWINCE__ | |
558 | ||
559 | return msStyle; | |
560 | } | |
561 | ||
562 | void wxButton::SetLabel(const wxString& label) | |
563 | { | |
564 | wxMSWButton::UpdateMultilineStyle(GetHwnd(), label); | |
565 | ||
566 | wxButtonBase::SetLabel(label); | |
567 | } | |
568 | ||
569 | // ---------------------------------------------------------------------------- | |
570 | // size management including autosizing | |
571 | // ---------------------------------------------------------------------------- | |
572 | ||
573 | wxSize wxButton::DoGetBestSize() const | |
574 | { | |
575 | wxSize size; | |
576 | ||
577 | // account for the text part | |
578 | if ( ShowsLabel() ) | |
579 | { | |
580 | size = wxMSWButton::ComputeBestSize(const_cast<wxButton *>(this)); | |
581 | } | |
582 | ||
583 | if ( m_imageData ) | |
584 | { | |
585 | // account for the bitmap size | |
586 | const wxSize sizeBmp = m_imageData->GetBitmap(State_Normal).GetSize(); | |
587 | const wxDirection dirBmp = m_imageData->GetBitmapPosition(); | |
588 | if ( dirBmp == wxLEFT || dirBmp == wxRIGHT ) | |
589 | { | |
590 | size.x += sizeBmp.x; | |
591 | if ( sizeBmp.y > size.y ) | |
592 | size.y = sizeBmp.y; | |
593 | } | |
594 | else // bitmap on top/below the text | |
595 | { | |
596 | size.y += sizeBmp.y; | |
597 | if ( sizeBmp.x > size.x ) | |
598 | size.x = sizeBmp.x; | |
599 | } | |
600 | ||
601 | // account for the user-specified margins | |
602 | size += 2*m_imageData->GetBitmapMargins(); | |
603 | ||
604 | // and also for the margins we always add internally (unless we have no | |
605 | // border at all in which case the button has exactly the same size as | |
606 | // bitmap and so no margins should be used) | |
607 | if ( !HasFlag(wxBORDER_NONE) ) | |
608 | { | |
609 | int marginH = 0, | |
610 | marginV = 0; | |
611 | #if wxUSE_UXTHEME | |
612 | if ( wxUxThemeEngine::GetIfActive() ) | |
613 | { | |
614 | wxUxThemeHandle theme(const_cast<wxButton *>(this), L"BUTTON"); | |
615 | ||
616 | MARGINS margins; | |
617 | wxUxThemeEngine::Get()->GetThemeMargins(theme, NULL, | |
618 | BP_PUSHBUTTON, | |
619 | PBS_NORMAL, | |
620 | TMT_CONTENTMARGINS, | |
621 | NULL, | |
622 | &margins); | |
623 | ||
624 | // XP doesn't draw themed buttons correctly when the client | |
625 | // area is smaller than 8x8 - enforce this minimum size for | |
626 | // small bitmaps | |
627 | size.IncTo(wxSize(8, 8)); | |
628 | ||
629 | marginH = margins.cxLeftWidth + margins.cxRightWidth | |
630 | + 2*XP_BUTTON_EXTRA_MARGIN; | |
631 | marginV = margins.cyTopHeight + margins.cyBottomHeight | |
632 | + 2*XP_BUTTON_EXTRA_MARGIN; | |
633 | } | |
634 | else | |
635 | #endif // wxUSE_UXTHEME | |
636 | { | |
637 | marginH = | |
638 | marginV = OD_BUTTON_MARGIN; | |
639 | } | |
640 | ||
641 | size.IncBy(marginH, marginV); | |
642 | } | |
643 | ||
644 | CacheBestSize(size); | |
645 | } | |
646 | ||
647 | return size; | |
648 | } | |
649 | ||
650 | /* static */ | |
651 | wxSize wxButtonBase::GetDefaultSize() | |
652 | { | |
653 | static wxSize s_sizeBtn; | |
654 | ||
655 | if ( s_sizeBtn.x == 0 ) | |
656 | { | |
657 | wxScreenDC dc; | |
658 | dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
659 | ||
660 | // the size of a standard button in the dialog units is 50x14, | |
661 | // translate this to pixels | |
662 | // NB1: the multipliers come from the Windows convention | |
663 | // NB2: the extra +1/+2 were needed to get the size be the same as the | |
664 | // size of the buttons in the standard dialog - I don't know how | |
665 | // this happens, but on my system this size is 75x23 in pixels and | |
666 | // 23*8 isn't even divisible by 14... Would be nice to understand | |
667 | // why these constants are needed though! | |
668 | s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4; | |
669 | s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8; | |
670 | } | |
671 | ||
672 | return s_sizeBtn; | |
673 | } | |
674 | ||
675 | // ---------------------------------------------------------------------------- | |
676 | // default button handling | |
677 | // ---------------------------------------------------------------------------- | |
678 | ||
679 | /* | |
680 | "Everything you ever wanted to know about the default buttons" or "Why do we | |
681 | have to do all this?" | |
682 | ||
683 | In MSW the default button should be activated when the user presses Enter | |
684 | and the current control doesn't process Enter itself somehow. This is | |
685 | handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID | |
686 | Another aspect of "defaultness" is that the default button has different | |
687 | appearance: this is due to BS_DEFPUSHBUTTON style which is completely | |
688 | separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should | |
689 | be unset if our parent window is not active so it should be unset whenever | |
690 | we lose activation and set back when we regain it. | |
691 | ||
692 | Final complication is that when a button is active, it should be the default | |
693 | one, i.e. pressing Enter on a button always activates it and not another | |
694 | one. | |
695 | ||
696 | We handle this by maintaining a permanent and a temporary default items in | |
697 | wxControlContainer (both may be NULL). When a button becomes the current | |
698 | control (i.e. gets focus) it sets itself as the temporary default which | |
699 | ensures that it has the right appearance and that Enter will be redirected | |
700 | to it. When the button loses focus, it unsets the temporary default and so | |
701 | the default item will be the permanent default -- that is the default button | |
702 | if any had been set or none otherwise, which is just what we want. | |
703 | ||
704 | NB: all this is quite complicated by now and the worst is that normally | |
705 | it shouldn't be necessary at all as for the normal Windows programs | |
706 | DefWindowProc() and IsDialogMessage() take care of all this | |
707 | automatically -- however in wxWidgets programs this doesn't work for | |
708 | nested hierarchies (i.e. a notebook inside a notebook) for unknown | |
709 | reason and so we have to reproduce all this code ourselves. It would be | |
710 | very nice if we could avoid doing it. | |
711 | */ | |
712 | ||
713 | // set this button as the (permanently) default one in its panel | |
714 | wxWindow *wxButton::SetDefault() | |
715 | { | |
716 | // set this one as the default button both for wxWidgets ... | |
717 | wxWindow *winOldDefault = wxButtonBase::SetDefault(); | |
718 | ||
719 | // ... and Windows | |
720 | SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false); | |
721 | SetDefaultStyle(this, true); | |
722 | ||
723 | return winOldDefault; | |
724 | } | |
725 | ||
726 | // return the top level parent window if it's not being deleted yet, otherwise | |
727 | // return NULL | |
728 | static wxTopLevelWindow *GetTLWParentIfNotBeingDeleted(wxWindow *win) | |
729 | { | |
730 | for ( ;; ) | |
731 | { | |
732 | // IsTopLevel() will return false for a wxTLW being deleted, so we also | |
733 | // need the parent test for this case | |
734 | wxWindow * const parent = win->GetParent(); | |
735 | if ( !parent || win->IsTopLevel() ) | |
736 | { | |
737 | if ( win->IsBeingDeleted() ) | |
738 | return NULL; | |
739 | ||
740 | break; | |
741 | } | |
742 | ||
743 | win = parent; | |
744 | } | |
745 | ||
746 | wxASSERT_MSG( win, wxT("button without top level parent?") ); | |
747 | ||
748 | wxTopLevelWindow * const tlw = wxDynamicCast(win, wxTopLevelWindow); | |
749 | wxASSERT_MSG( tlw, wxT("logic error in GetTLWParentIfNotBeingDeleted()") ); | |
750 | ||
751 | return tlw; | |
752 | } | |
753 | ||
754 | // set this button as being currently default | |
755 | void wxButton::SetTmpDefault() | |
756 | { | |
757 | wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(GetParent()); | |
758 | if ( !tlw ) | |
759 | return; | |
760 | ||
761 | wxWindow *winOldDefault = tlw->GetDefaultItem(); | |
762 | tlw->SetTmpDefaultItem(this); | |
763 | ||
764 | SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false); | |
765 | SetDefaultStyle(this, true); | |
766 | } | |
767 | ||
768 | // unset this button as currently default, it may still stay permanent default | |
769 | void wxButton::UnsetTmpDefault() | |
770 | { | |
771 | wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(GetParent()); | |
772 | if ( !tlw ) | |
773 | return; | |
774 | ||
775 | tlw->SetTmpDefaultItem(NULL); | |
776 | ||
777 | wxWindow *winOldDefault = tlw->GetDefaultItem(); | |
778 | ||
779 | SetDefaultStyle(this, false); | |
780 | SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), true); | |
781 | } | |
782 | ||
783 | /* static */ | |
784 | void | |
785 | wxButton::SetDefaultStyle(wxButton *btn, bool on) | |
786 | { | |
787 | // we may be called with NULL pointer -- simpler to do the check here than | |
788 | // in the caller which does wxDynamicCast() | |
789 | if ( !btn ) | |
790 | return; | |
791 | ||
792 | // first, let DefDlgProc() know about the new default button | |
793 | if ( on ) | |
794 | { | |
795 | // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have | |
796 | // focus at all any more | |
797 | if ( !wxTheApp->IsActive() ) | |
798 | return; | |
799 | ||
800 | wxWindow * const tlw = wxGetTopLevelParent(btn); | |
801 | wxCHECK_RET( tlw, wxT("button without top level window?") ); | |
802 | ||
803 | ::SendMessage(GetHwndOf(tlw), DM_SETDEFID, btn->GetId(), 0L); | |
804 | ||
805 | // sending DM_SETDEFID also changes the button style to | |
806 | // BS_DEFPUSHBUTTON so there is nothing more to do | |
807 | } | |
808 | ||
809 | // then also change the style as needed | |
810 | long style = ::GetWindowLong(GetHwndOf(btn), GWL_STYLE); | |
811 | if ( !(style & BS_DEFPUSHBUTTON) == on ) | |
812 | { | |
813 | // don't do it with the owner drawn buttons because it will | |
814 | // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW & | |
815 | // BS_DEFPUSHBUTTON != 0)! | |
816 | if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW ) | |
817 | { | |
818 | ::SendMessage(GetHwndOf(btn), BM_SETSTYLE, | |
819 | on ? style | BS_DEFPUSHBUTTON | |
820 | : style & ~BS_DEFPUSHBUTTON, | |
821 | 1L /* redraw */); | |
822 | } | |
823 | else // owner drawn | |
824 | { | |
825 | // redraw the button - it will notice itself that it's | |
826 | // [not] the default one [any longer] | |
827 | btn->Refresh(); | |
828 | } | |
829 | } | |
830 | //else: already has correct style | |
831 | } | |
832 | ||
833 | // ---------------------------------------------------------------------------- | |
834 | // helpers | |
835 | // ---------------------------------------------------------------------------- | |
836 | ||
837 | bool wxButton::SendClickEvent() | |
838 | { | |
839 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId()); | |
840 | event.SetEventObject(this); | |
841 | ||
842 | return ProcessCommand(event); | |
843 | } | |
844 | ||
845 | void wxButton::Command(wxCommandEvent & event) | |
846 | { | |
847 | ProcessCommand(event); | |
848 | } | |
849 | ||
850 | // ---------------------------------------------------------------------------- | |
851 | // event/message handlers | |
852 | // ---------------------------------------------------------------------------- | |
853 | ||
854 | bool wxButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
855 | { | |
856 | bool processed = false; | |
857 | switch ( param ) | |
858 | { | |
859 | // NOTE: Apparently older versions (NT 4?) of the common controls send | |
860 | // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn | |
861 | // buttons, so in order to send two EVT_BUTTON events we should | |
862 | // catch both types. Currently (Feb 2003) up-to-date versions of | |
863 | // win98, win2k and winXP all send two BN_CLICKED messages for | |
864 | // all button types, so we don't catch BN_DOUBLECLICKED anymore | |
865 | // in order to not get 3 EVT_BUTTON events. If this is a problem | |
866 | // then we need to figure out which version of the comctl32 changed | |
867 | // this behaviour and test for it. | |
868 | ||
869 | case 1: // message came from an accelerator | |
870 | case BN_CLICKED: // normal buttons send this | |
871 | processed = SendClickEvent(); | |
872 | break; | |
873 | } | |
874 | ||
875 | return processed; | |
876 | } | |
877 | ||
878 | WXLRESULT wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
879 | { | |
880 | // when we receive focus, we want to temporarily become the default button in | |
881 | // our parent panel so that pressing "Enter" would activate us -- and when | |
882 | // losing it we should restore the previous default button as well | |
883 | if ( nMsg == WM_SETFOCUS ) | |
884 | { | |
885 | SetTmpDefault(); | |
886 | ||
887 | // let the default processing take place too | |
888 | } | |
889 | else if ( nMsg == WM_KILLFOCUS ) | |
890 | { | |
891 | UnsetTmpDefault(); | |
892 | } | |
893 | else if ( nMsg == WM_LBUTTONDBLCLK ) | |
894 | { | |
895 | // emulate a click event to force an owner-drawn button to change its | |
896 | // appearance - without this, it won't do it | |
897 | (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN, wParam, lParam); | |
898 | ||
899 | // and continue with processing the message normally as well | |
900 | } | |
901 | #if wxUSE_UXTHEME | |
902 | else if ( nMsg == WM_THEMECHANGED ) | |
903 | { | |
904 | // need to recalculate the best size here | |
905 | // as the theme size might have changed | |
906 | InvalidateBestSize(); | |
907 | } | |
908 | #endif // wxUSE_UXTHEME | |
909 | // must use m_mouseInWindow here instead of IsMouseInWindow() | |
910 | // since we need to know the first time the mouse enters the window | |
911 | // and IsMouseInWindow() would return true in this case | |
912 | else if ( (nMsg == WM_MOUSEMOVE && !m_mouseInWindow) || | |
913 | nMsg == WM_MOUSELEAVE ) | |
914 | { | |
915 | if ( | |
916 | IsEnabled() && | |
917 | ( | |
918 | #if wxUSE_UXTHEME | |
919 | wxUxThemeEngine::GetIfActive() || | |
920 | #endif // wxUSE_UXTHEME | |
921 | m_imageData && m_imageData->GetBitmap(State_Current).IsOk() | |
922 | ) | |
923 | ) | |
924 | { | |
925 | Refresh(); | |
926 | } | |
927 | } | |
928 | ||
929 | // let the base class do all real processing | |
930 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
931 | } | |
932 | ||
933 | // ---------------------------------------------------------------------------- | |
934 | // button images | |
935 | // ---------------------------------------------------------------------------- | |
936 | ||
937 | wxBitmap wxButton::DoGetBitmap(State which) const | |
938 | { | |
939 | return m_imageData ? m_imageData->GetBitmap(which) : wxBitmap(); | |
940 | } | |
941 | ||
942 | void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which) | |
943 | { | |
944 | // allocate the image data when the first bitmap is set | |
945 | if ( !m_imageData ) | |
946 | { | |
947 | #if wxUSE_UXTHEME | |
948 | // using image list doesn't work correctly if we don't have any label | |
949 | // (even if we use BUTTON_IMAGELIST_ALIGN_CENTER alignment and | |
950 | // BS_BITMAP style), at least under Windows 2003 so use owner drawn | |
951 | // strategy for bitmap-only buttons | |
952 | if ( ShowsLabel() && wxUxThemeEngine::GetIfActive() ) | |
953 | { | |
954 | m_imageData = new wxXPButtonImageData(this, bitmap); | |
955 | } | |
956 | else | |
957 | #endif // wxUSE_UXTHEME | |
958 | { | |
959 | m_imageData = new wxODButtonImageData(this, bitmap); | |
960 | MakeOwnerDrawn(); | |
961 | } | |
962 | } | |
963 | else | |
964 | { | |
965 | m_imageData->SetBitmap(bitmap, which); | |
966 | } | |
967 | ||
968 | // it should be enough to only invalidate the best size when the normal | |
969 | // bitmap changes as all bitmaps assigned to the button should be of the | |
970 | // same size anyhow | |
971 | if ( which == State_Normal ) | |
972 | InvalidateBestSize(); | |
973 | ||
974 | Refresh(); | |
975 | } | |
976 | ||
977 | wxSize wxButton::DoGetBitmapMargins() const | |
978 | { | |
979 | return m_imageData ? m_imageData->GetBitmapMargins() : wxSize(0, 0); | |
980 | } | |
981 | ||
982 | void wxButton::DoSetBitmapMargins(wxCoord x, wxCoord y) | |
983 | { | |
984 | wxCHECK_RET( m_imageData, "SetBitmap() must be called first" ); | |
985 | ||
986 | m_imageData->SetBitmapMargins(x, y); | |
987 | } | |
988 | ||
989 | void wxButton::DoSetBitmapPosition(wxDirection dir) | |
990 | { | |
991 | wxCHECK_RET( m_imageData, "SetBitmap() must be called first" ); | |
992 | ||
993 | m_imageData->SetBitmapPosition(dir); | |
994 | } | |
995 | ||
996 | // ---------------------------------------------------------------------------- | |
997 | // owner-drawn buttons support | |
998 | // ---------------------------------------------------------------------------- | |
999 | ||
1000 | // drawing helpers | |
1001 | namespace | |
1002 | { | |
1003 | ||
1004 | // return the button state using both the ODS_XXX flags specified in state | |
1005 | // parameter and the current button state | |
1006 | wxButton::State GetButtonState(wxButton *btn, UINT state) | |
1007 | { | |
1008 | if ( state & ODS_DISABLED ) | |
1009 | return wxButton::State_Disabled; | |
1010 | ||
1011 | if ( state & ODS_SELECTED ) | |
1012 | return wxButton::State_Pressed; | |
1013 | ||
1014 | if ( btn->HasCapture() || btn->IsMouseInWindow() ) | |
1015 | return wxButton::State_Current; | |
1016 | ||
1017 | if ( state & ODS_FOCUS ) | |
1018 | return wxButton::State_Focused; | |
1019 | ||
1020 | return wxButton::State_Normal; | |
1021 | } | |
1022 | ||
1023 | void DrawButtonText(HDC hdc, | |
1024 | RECT *pRect, | |
1025 | const wxString& text, | |
1026 | COLORREF col, | |
1027 | int flags) | |
1028 | { | |
1029 | wxTextColoursChanger changeFg(hdc, col, CLR_INVALID); | |
1030 | wxBkModeChanger changeBkMode(hdc, wxBRUSHSTYLE_TRANSPARENT); | |
1031 | ||
1032 | // center text horizontally in any case | |
1033 | flags |= DT_CENTER; | |
1034 | ||
1035 | if ( text.find(wxT('\n')) != wxString::npos ) | |
1036 | { | |
1037 | // draw multiline label | |
1038 | ||
1039 | // first we need to compute its bounding rect | |
1040 | RECT rc; | |
1041 | ::CopyRect(&rc, pRect); | |
1042 | ::DrawText(hdc, text.wx_str(), text.length(), &rc, | |
1043 | DT_CENTER | DT_CALCRECT); | |
1044 | ||
1045 | // now center this rect inside the entire button area | |
1046 | const LONG w = rc.right - rc.left; | |
1047 | const LONG h = rc.bottom - rc.top; | |
1048 | rc.left = (pRect->right - pRect->left)/2 - w/2; | |
1049 | rc.right = rc.left+w; | |
1050 | rc.top = (pRect->bottom - pRect->top)/2 - h/2; | |
1051 | rc.bottom = rc.top+h; | |
1052 | ||
1053 | ::DrawText(hdc, text.wx_str(), text.length(), &rc, flags); | |
1054 | } | |
1055 | else // single line label | |
1056 | { | |
1057 | // centre text vertically too (notice that we must have DT_SINGLELINE | |
1058 | // for DT_VCENTER to work) | |
1059 | ::DrawText(hdc, text.wx_str(), text.length(), pRect, | |
1060 | flags | DT_SINGLELINE | DT_VCENTER); | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | void DrawRect(HDC hdc, const RECT& r) | |
1065 | { | |
1066 | wxDrawLine(hdc, r.left, r.top, r.right, r.top); | |
1067 | wxDrawLine(hdc, r.right, r.top, r.right, r.bottom); | |
1068 | wxDrawLine(hdc, r.right, r.bottom, r.left, r.bottom); | |
1069 | wxDrawLine(hdc, r.left, r.bottom, r.left, r.top); | |
1070 | } | |
1071 | ||
1072 | /* | |
1073 | The button frame looks like this normally: | |
1074 | ||
1075 | WWWWWWWWWWWWWWWWWWB | |
1076 | WHHHHHHHHHHHHHHHHGB W = white (HILIGHT) | |
1077 | WH GB H = light grey (LIGHT) | |
1078 | WH GB G = dark grey (SHADOW) | |
1079 | WH GB B = black (DKSHADOW) | |
1080 | WH GB | |
1081 | WGGGGGGGGGGGGGGGGGB | |
1082 | BBBBBBBBBBBBBBBBBBB | |
1083 | ||
1084 | When the button is selected, the button becomes like this (the total button | |
1085 | size doesn't change): | |
1086 | ||
1087 | BBBBBBBBBBBBBBBBBBB | |
1088 | BWWWWWWWWWWWWWWWWBB | |
1089 | BWHHHHHHHHHHHHHHGBB | |
1090 | BWH GBB | |
1091 | BWH GBB | |
1092 | BWGGGGGGGGGGGGGGGBB | |
1093 | BBBBBBBBBBBBBBBBBBB | |
1094 | BBBBBBBBBBBBBBBBBBB | |
1095 | ||
1096 | When the button is pushed (while selected) it is like: | |
1097 | ||
1098 | BBBBBBBBBBBBBBBBBBB | |
1099 | BGGGGGGGGGGGGGGGGGB | |
1100 | BG GB | |
1101 | BG GB | |
1102 | BG GB | |
1103 | BG GB | |
1104 | BGGGGGGGGGGGGGGGGGB | |
1105 | BBBBBBBBBBBBBBBBBBB | |
1106 | */ | |
1107 | void DrawButtonFrame(HDC hdc, RECT& rectBtn, | |
1108 | bool selected, bool pushed) | |
1109 | { | |
1110 | RECT r; | |
1111 | CopyRect(&r, &rectBtn); | |
1112 | ||
1113 | AutoHPEN hpenBlack(GetSysColor(COLOR_3DDKSHADOW)), | |
1114 | hpenGrey(GetSysColor(COLOR_3DSHADOW)), | |
1115 | hpenLightGr(GetSysColor(COLOR_3DLIGHT)), | |
1116 | hpenWhite(GetSysColor(COLOR_3DHILIGHT)); | |
1117 | ||
1118 | SelectInHDC selectPen(hdc, hpenBlack); | |
1119 | ||
1120 | r.right--; | |
1121 | r.bottom--; | |
1122 | ||
1123 | if ( pushed ) | |
1124 | { | |
1125 | DrawRect(hdc, r); | |
1126 | ||
1127 | (void)SelectObject(hdc, hpenGrey); | |
1128 | ::InflateRect(&r, -1, -1); | |
1129 | ||
1130 | DrawRect(hdc, r); | |
1131 | } | |
1132 | else // !pushed | |
1133 | { | |
1134 | if ( selected ) | |
1135 | { | |
1136 | DrawRect(hdc, r); | |
1137 | ||
1138 | ::InflateRect(&r, -1, -1); | |
1139 | } | |
1140 | ||
1141 | wxDrawLine(hdc, r.left, r.bottom, r.right, r.bottom); | |
1142 | wxDrawLine(hdc, r.right, r.bottom, r.right, r.top - 1); | |
1143 | ||
1144 | (void)SelectObject(hdc, hpenWhite); | |
1145 | wxDrawLine(hdc, r.left, r.bottom - 1, r.left, r.top); | |
1146 | wxDrawLine(hdc, r.left, r.top, r.right, r.top); | |
1147 | ||
1148 | (void)SelectObject(hdc, hpenLightGr); | |
1149 | wxDrawLine(hdc, r.left + 1, r.bottom - 2, r.left + 1, r.top + 1); | |
1150 | wxDrawLine(hdc, r.left + 1, r.top + 1, r.right - 1, r.top + 1); | |
1151 | ||
1152 | (void)SelectObject(hdc, hpenGrey); | |
1153 | wxDrawLine(hdc, r.left + 1, r.bottom - 1, r.right - 1, r.bottom - 1); | |
1154 | wxDrawLine(hdc, r.right - 1, r.bottom - 1, r.right - 1, r.top); | |
1155 | } | |
1156 | ||
1157 | InflateRect(&rectBtn, -OD_BUTTON_MARGIN, -OD_BUTTON_MARGIN); | |
1158 | } | |
1159 | ||
1160 | #if wxUSE_UXTHEME | |
1161 | void DrawXPBackground(wxButton *button, HDC hdc, RECT& rectBtn, UINT state) | |
1162 | { | |
1163 | wxUxThemeHandle theme(button, L"BUTTON"); | |
1164 | ||
1165 | // this array is indexed by wxButton::State values and so must be kept in | |
1166 | // sync with it | |
1167 | static const int uxStates[] = | |
1168 | { | |
1169 | PBS_NORMAL, PBS_HOT, PBS_PRESSED, PBS_DISABLED, PBS_DEFAULTED | |
1170 | }; | |
1171 | ||
1172 | int iState = uxStates[GetButtonState(button, state)]; | |
1173 | ||
1174 | wxUxThemeEngine * const engine = wxUxThemeEngine::Get(); | |
1175 | ||
1176 | // draw parent background if needed | |
1177 | if ( engine->IsThemeBackgroundPartiallyTransparent | |
1178 | ( | |
1179 | theme, | |
1180 | BP_PUSHBUTTON, | |
1181 | iState | |
1182 | ) ) | |
1183 | { | |
1184 | engine->DrawThemeParentBackground(GetHwndOf(button), hdc, &rectBtn); | |
1185 | } | |
1186 | ||
1187 | // draw background | |
1188 | engine->DrawThemeBackground(theme, hdc, BP_PUSHBUTTON, iState, | |
1189 | &rectBtn, NULL); | |
1190 | ||
1191 | // calculate content area margins | |
1192 | MARGINS margins; | |
1193 | engine->GetThemeMargins(theme, hdc, BP_PUSHBUTTON, iState, | |
1194 | TMT_CONTENTMARGINS, &rectBtn, &margins); | |
1195 | ::InflateRect(&rectBtn, -margins.cxLeftWidth, -margins.cyTopHeight); | |
1196 | ::InflateRect(&rectBtn, -XP_BUTTON_EXTRA_MARGIN, -XP_BUTTON_EXTRA_MARGIN); | |
1197 | ||
1198 | if ( button->UseBgCol() ) | |
1199 | { | |
1200 | COLORREF colBg = wxColourToRGB(button->GetBackgroundColour()); | |
1201 | AutoHBRUSH hbrushBackground(colBg); | |
1202 | ||
1203 | // don't overwrite the focus rect | |
1204 | RECT rectClient; | |
1205 | ::CopyRect(&rectClient, &rectBtn); | |
1206 | ::InflateRect(&rectClient, -1, -1); | |
1207 | FillRect(hdc, &rectClient, hbrushBackground); | |
1208 | } | |
1209 | } | |
1210 | #endif // wxUSE_UXTHEME | |
1211 | ||
1212 | } // anonymous namespace | |
1213 | ||
1214 | // ---------------------------------------------------------------------------- | |
1215 | // owner drawn buttons support | |
1216 | // ---------------------------------------------------------------------------- | |
1217 | ||
1218 | void wxButton::MakeOwnerDrawn() | |
1219 | { | |
1220 | long style = GetWindowLong(GetHwnd(), GWL_STYLE); | |
1221 | if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW ) | |
1222 | { | |
1223 | // make it so | |
1224 | style |= BS_OWNERDRAW; | |
1225 | SetWindowLong(GetHwnd(), GWL_STYLE, style); | |
1226 | } | |
1227 | } | |
1228 | ||
1229 | bool wxButton::SetBackgroundColour(const wxColour &colour) | |
1230 | { | |
1231 | if ( !wxControl::SetBackgroundColour(colour) ) | |
1232 | { | |
1233 | // nothing to do | |
1234 | return false; | |
1235 | } | |
1236 | ||
1237 | MakeOwnerDrawn(); | |
1238 | ||
1239 | Refresh(); | |
1240 | ||
1241 | return true; | |
1242 | } | |
1243 | ||
1244 | bool wxButton::SetForegroundColour(const wxColour &colour) | |
1245 | { | |
1246 | if ( !wxControl::SetForegroundColour(colour) ) | |
1247 | { | |
1248 | // nothing to do | |
1249 | return false; | |
1250 | } | |
1251 | ||
1252 | MakeOwnerDrawn(); | |
1253 | ||
1254 | Refresh(); | |
1255 | ||
1256 | return true; | |
1257 | } | |
1258 | ||
1259 | bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis) | |
1260 | { | |
1261 | LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)wxdis; | |
1262 | HDC hdc = lpDIS->hDC; | |
1263 | ||
1264 | UINT state = lpDIS->itemState; | |
1265 | bool pushed = (SendMessage(GetHwnd(), BM_GETSTATE, 0, 0) & BST_PUSHED) != 0; | |
1266 | ||
1267 | RECT rectBtn; | |
1268 | CopyRect(&rectBtn, &lpDIS->rcItem); | |
1269 | ||
1270 | // draw the button background | |
1271 | if ( !HasFlag(wxBORDER_NONE) ) | |
1272 | { | |
1273 | #if wxUSE_UXTHEME | |
1274 | if ( wxUxThemeEngine::GetIfActive() ) | |
1275 | { | |
1276 | DrawXPBackground(this, hdc, rectBtn, state); | |
1277 | } | |
1278 | else | |
1279 | #endif // wxUSE_UXTHEME | |
1280 | { | |
1281 | COLORREF colBg = wxColourToRGB(GetBackgroundColour()); | |
1282 | ||
1283 | // first, draw the background | |
1284 | AutoHBRUSH hbrushBackground(colBg); | |
1285 | FillRect(hdc, &rectBtn, hbrushBackground); | |
1286 | ||
1287 | // draw the border for the current state | |
1288 | bool selected = (state & ODS_SELECTED) != 0; | |
1289 | if ( !selected ) | |
1290 | { | |
1291 | wxTopLevelWindow * | |
1292 | tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); | |
1293 | if ( tlw ) | |
1294 | { | |
1295 | selected = tlw->GetDefaultItem() == this; | |
1296 | } | |
1297 | } | |
1298 | ||
1299 | DrawButtonFrame(hdc, rectBtn, selected, pushed); | |
1300 | } | |
1301 | ||
1302 | // draw the focus rectangle if we need it | |
1303 | if ( (state & ODS_FOCUS) && !(state & ODS_NOFOCUSRECT) ) | |
1304 | { | |
1305 | DrawFocusRect(hdc, &rectBtn); | |
1306 | ||
1307 | #if wxUSE_UXTHEME | |
1308 | if ( !wxUxThemeEngine::GetIfActive() ) | |
1309 | #endif // wxUSE_UXTHEME | |
1310 | { | |
1311 | if ( pushed ) | |
1312 | { | |
1313 | // the label is shifted by 1 pixel to create "pushed" effect | |
1314 | OffsetRect(&rectBtn, 1, 1); | |
1315 | } | |
1316 | } | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | ||
1321 | // draw the image, if any | |
1322 | if ( m_imageData ) | |
1323 | { | |
1324 | wxBitmap bmp = m_imageData->GetBitmap(GetButtonState(this, state)); | |
1325 | if ( !bmp.IsOk() ) | |
1326 | bmp = m_imageData->GetBitmap(State_Normal); | |
1327 | ||
1328 | const wxSize sizeBmp = bmp.GetSize(); | |
1329 | const wxSize margin = m_imageData->GetBitmapMargins(); | |
1330 | const wxSize sizeBmpWithMargins(sizeBmp + 2*margin); | |
1331 | wxRect rectButton(wxRectFromRECT(rectBtn)); | |
1332 | ||
1333 | // for simplicity, we start with centred rectangle and then move it to | |
1334 | // the appropriate edge | |
1335 | wxRect rectBitmap = wxRect(sizeBmp).CentreIn(rectButton); | |
1336 | switch ( m_imageData->GetBitmapPosition() ) | |
1337 | { | |
1338 | default: | |
1339 | wxFAIL_MSG( "invalid direction" ); | |
1340 | // fall through | |
1341 | ||
1342 | case wxLEFT: | |
1343 | rectBitmap.x = rectButton.x + margin.x; | |
1344 | rectButton.x += sizeBmpWithMargins.x; | |
1345 | rectButton.width -= sizeBmpWithMargins.x; | |
1346 | break; | |
1347 | ||
1348 | case wxRIGHT: | |
1349 | rectBitmap.x = rectButton.GetRight() - sizeBmp.x - margin.x; | |
1350 | rectButton.width -= sizeBmpWithMargins.x; | |
1351 | break; | |
1352 | ||
1353 | case wxTOP: | |
1354 | rectBitmap.y = rectButton.y + margin.y; | |
1355 | rectButton.y += sizeBmpWithMargins.y; | |
1356 | rectButton.height -= sizeBmpWithMargins.y; | |
1357 | break; | |
1358 | ||
1359 | case wxBOTTOM: | |
1360 | rectBitmap.y = rectButton.GetBottom() - sizeBmp.y - margin.y; | |
1361 | rectButton.height -= sizeBmpWithMargins.y; | |
1362 | break; | |
1363 | } | |
1364 | ||
1365 | wxDCTemp dst((WXHDC)hdc); | |
1366 | dst.DrawBitmap(bmp, rectBitmap.GetPosition(), true); | |
1367 | ||
1368 | wxCopyRectToRECT(rectButton, rectBtn); | |
1369 | } | |
1370 | ||
1371 | ||
1372 | // finally draw the label | |
1373 | if ( ShowsLabel() ) | |
1374 | { | |
1375 | COLORREF colFg = state & ODS_DISABLED | |
1376 | ? ::GetSysColor(COLOR_GRAYTEXT) | |
1377 | : wxColourToRGB(GetForegroundColour()); | |
1378 | ||
1379 | // notice that DT_HIDEPREFIX doesn't work on old (pre-Windows 2000) | |
1380 | // systems but by happy coincidence ODS_NOACCEL is not used under them | |
1381 | // neither so DT_HIDEPREFIX should never be used there | |
1382 | DrawButtonText(hdc, &rectBtn, GetLabel(), colFg, | |
1383 | state & ODS_NOACCEL ? DT_HIDEPREFIX : 0); | |
1384 | } | |
1385 | ||
1386 | return true; | |
1387 | } | |
1388 | ||
1389 | #endif // wxUSE_BUTTON | |
1390 |