]> git.saurik.com Git - wxWidgets.git/blob - src/msw/button.cpp
wxSTAY_ON_TOP support in wxMessageBox (patch 581826)
[wxWidgets.git] / src / msw / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 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 "button.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 #if wxUSE_BUTTON
32
33 #ifndef WX_PRECOMP
34 #include "wx/button.h"
35 #include "wx/brush.h"
36 #include "wx/panel.h"
37 #include "wx/bmpbuttn.h"
38 #include "wx/settings.h"
39 #include "wx/dcscreen.h"
40 #endif
41
42 #include "wx/msw/private.h"
43
44 // ----------------------------------------------------------------------------
45 // macros
46 // ----------------------------------------------------------------------------
47
48 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
49
50 // this macro tries to adjust the default button height to a reasonable value
51 // using the char height as the base
52 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57
58 // ----------------------------------------------------------------------------
59 // creation/destruction
60 // ----------------------------------------------------------------------------
61
62 bool wxButton::Create(wxWindow *parent,
63 wxWindowID id,
64 const wxString& label,
65 const wxPoint& pos,
66 const wxSize& size,
67 long style,
68 const wxValidator& validator,
69 const wxString& name)
70 {
71 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
72 return FALSE;
73
74 WXDWORD exstyle;
75 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
76
77 #ifdef __WIN32__
78 // if the label contains several lines we must explicitly tell the button
79 // about it or it wouldn't draw it correctly ("\n"s would just appear as
80 // black boxes)
81 //
82 // NB: we do it here and not in MSWGetStyle() because we need the label
83 // value and m_label is not set yet when MSWGetStyle() is called;
84 // besides changing BS_MULTILINE during run-time is pointless anyhow
85 if ( label.find(_T('\n')) != wxString::npos )
86 {
87 msStyle |= BS_MULTILINE;
88 }
89 #endif // __WIN32__
90
91 return MSWCreateControl(_T("BUTTON"), msStyle, pos, size, label, exstyle);
92 }
93
94 wxButton::~wxButton()
95 {
96 }
97
98 // ----------------------------------------------------------------------------
99 // flags
100 // ----------------------------------------------------------------------------
101
102 WXDWORD wxButton::MSWGetStyle(long style, WXDWORD *exstyle) const
103 {
104 // buttons never have an external border, they draw their own one
105 WXDWORD msStyle = wxControl::MSWGetStyle
106 (
107 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
108 );
109
110 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
111 // each other in any resizeable dialog which has more than one button in
112 // the bottom
113 msStyle |= WS_CLIPSIBLINGS;
114
115 #ifdef __WIN32__
116 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
117 // and wxBU_RIGHT to get BS_CENTER!
118 if ( style & wxBU_LEFT )
119 msStyle |= BS_LEFT;
120 if ( style & wxBU_RIGHT )
121 msStyle |= BS_RIGHT;
122 if ( style & wxBU_TOP )
123 msStyle |= BS_TOP;
124 if ( style & wxBU_BOTTOM )
125 msStyle |= BS_BOTTOM;
126 #endif // __WIN32__
127
128 return msStyle;
129 }
130
131 // ----------------------------------------------------------------------------
132 // size management including autosizing
133 // ----------------------------------------------------------------------------
134
135 wxSize wxButton::DoGetBestSize() const
136 {
137 wxString label = wxGetWindowText(GetHWND());
138 int wBtn;
139 GetTextExtent(label, &wBtn, NULL);
140
141 int wChar, hChar;
142 wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont());
143
144 // add a margin - the button is wider than just its label
145 wBtn += 3*wChar;
146
147 // the button height is proportional to the height of the font used
148 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
149
150 if (!HasFlag(wxBU_EXACTFIT))
151 {
152 wxSize sz = GetDefaultSize();
153 if (wBtn > sz.x) sz.x = wBtn;
154 if (hBtn > sz.y) sz.y = hBtn;
155 return sz;
156 }
157 else
158 return wxSize(wBtn, hBtn);
159
160 }
161
162 /* static */
163 wxSize wxButtonBase::GetDefaultSize()
164 {
165 static wxSize s_sizeBtn;
166
167 if ( s_sizeBtn.x == 0 )
168 {
169 wxScreenDC dc;
170 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
171
172 // the size of a standard button in the dialog units is 50x14,
173 // translate this to pixels
174 // NB1: the multipliers come from the Windows convention
175 // NB2: the extra +1/+2 were needed to get the size be the same as the
176 // size of the buttons in the standard dialog - I don't know how
177 // this happens, but on my system this size is 75x23 in pixels and
178 // 23*8 isn't even divisible by 14... Would be nice to understand
179 // why these constants are needed though!
180 s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4;
181 s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8;
182 }
183
184 return s_sizeBtn;
185 }
186
187 // ----------------------------------------------------------------------------
188 // default button handling
189 // ----------------------------------------------------------------------------
190
191 /*
192 "Everything you ever wanted to know about the default buttons" or "Why do we
193 have to do all this?"
194
195 In MSW the default button should be activated when the user presses Enter
196 and the current control doesn't process Enter itself somehow. This is
197 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
198 Another aspect of "defaultness" is that the default button has different
199 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
200 separate from DM_SETDEFID stuff (!).
201
202 Final complication is that when a button is active, it should be the default
203 one, i.e. pressing Enter on a button always activates it and not another
204 one.
205
206 We handle this by maintaining a permanent and a temporary default items in
207 wxControlContainer (both may be NULL). When a button becomes the current
208 control (i.e. gets focus) it sets itself as the temporary default which
209 ensures that it has the right appearance and that Enter will be redirected
210 to it. When the button loses focus, it unsets the temporary default and so
211 the default item will be the permanent default -- that is the default button
212 if any had been set or none otherwise, which is just what we want.
213
214 Remark that we probably don't need to send DM_SETDEFID as we don't use
215 ::IsDialogMessage() (which relies on it) any longer but OTOH it probably
216 doesn't hurt neither.
217 */
218
219 // set this button as the (permanently) default one in its panel
220 void wxButton::SetDefault()
221 {
222 wxWindow *parent = GetParent();
223
224 wxCHECK_RET( parent, _T("button without parent?") );
225
226 // set this one as the default button both for wxWindows and Windows
227 wxWindow *winOldDefault = parent->SetDefaultItem(this);
228 ::SendMessage(GetWinHwnd(parent), DM_SETDEFID, m_windowId, 0L);
229
230 UpdateDefaultStyle(this, winOldDefault);
231 }
232
233 void wxButton::SetTmpDefault()
234 {
235 wxWindow *parent = GetParent();
236
237 wxCHECK_RET( parent, _T("button without parent?") );
238
239 wxWindow *winOldDefault = parent->GetDefaultItem();
240 parent->SetTmpDefaultItem(this);
241 if ( winOldDefault != this )
242 {
243 UpdateDefaultStyle(this, winOldDefault);
244 }
245 //else: no styles to update
246 }
247
248 void wxButton::UnsetTmpDefault()
249 {
250 wxWindow *parent = GetParent();
251
252 wxCHECK_RET( parent, _T("button without parent?") );
253
254 parent->SetTmpDefaultItem(NULL);
255
256 wxWindow *winOldDefault = parent->GetDefaultItem();
257 if ( winOldDefault != this )
258 {
259 UpdateDefaultStyle(winOldDefault, this);
260 }
261 //else: we had been default before anyhow
262 }
263
264 /* static */
265 void
266 wxButton::UpdateDefaultStyle(wxWindow *winDefault, wxWindow *winOldDefault)
267 {
268 // clear the BS_DEFPUSHBUTTON for the old default button
269 wxButton *btnOldDefault = wxDynamicCast(winOldDefault, wxButton);
270 if ( btnOldDefault && btnOldDefault != winDefault )
271 {
272 // remove the BS_DEFPUSHBUTTON style from the other button
273 long style = ::GetWindowLong(GetHwndOf(btnOldDefault), GWL_STYLE);
274
275 // don't do it with the owner drawn buttons because it will reset
276 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
277 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
278 {
279 style &= ~BS_DEFPUSHBUTTON;
280 ::SendMessage(GetHwndOf(btnOldDefault), BM_SETSTYLE, style, 1L);
281 }
282 else
283 {
284 // redraw the button - it will notice itself that it's not the
285 // default one any longer
286 btnOldDefault->Refresh();
287 }
288 }
289
290 // and set BS_DEFPUSHBUTTON for this button
291 wxButton *btnDefault = wxDynamicCast(winDefault, wxButton);
292 if ( btnDefault )
293 {
294 long style = ::GetWindowLong(GetHwndOf(btnDefault), GWL_STYLE);
295 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
296 {
297 style |= BS_DEFPUSHBUTTON;
298 ::SendMessage(GetHwndOf(btnDefault), BM_SETSTYLE, style, 1L);
299 }
300 else
301 {
302 btnDefault->Refresh();
303 }
304 }
305 }
306
307 // ----------------------------------------------------------------------------
308 // helpers
309 // ----------------------------------------------------------------------------
310
311 bool wxButton::SendClickEvent()
312 {
313 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
314 event.SetEventObject(this);
315
316 return ProcessCommand(event);
317 }
318
319 void wxButton::Command(wxCommandEvent & event)
320 {
321 ProcessCommand(event);
322 }
323
324 // ----------------------------------------------------------------------------
325 // event/message handlers
326 // ----------------------------------------------------------------------------
327
328 bool wxButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
329 {
330 bool processed = FALSE;
331 switch ( param )
332 {
333 case 1: // message came from an accelerator
334 case BN_CLICKED: // normal buttons send this
335 case BN_DOUBLECLICKED: // owner-drawn ones also send this
336 processed = SendClickEvent();
337 break;
338 }
339
340 return processed;
341 }
342
343 long wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
344 {
345 // when we receive focus, we want to temporary become the default button in
346 // our parent panel so that pressing "Enter" would activate us -- and when
347 // losing it we should restore the previous default button as well
348 if ( nMsg == WM_SETFOCUS )
349 {
350 SetTmpDefault();
351
352 // let the default processing take place too
353 }
354 else if ( nMsg == WM_KILLFOCUS )
355 {
356 UnsetTmpDefault();
357 }
358 else if ( nMsg == WM_LBUTTONDBLCLK )
359 {
360 // emulate a click event to force an owner-drawn button to change its
361 // appearance - without this, it won't do it
362 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN, wParam, lParam);
363
364 // and continue with processing the message normally as well
365 }
366
367 // let the base class do all real processing
368 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
369 }
370
371 // ----------------------------------------------------------------------------
372 // owner-drawn buttons support
373 // ----------------------------------------------------------------------------
374
375 #ifdef __WIN32__
376
377 // drawing helpers
378
379 static void DrawButtonText(HDC hdc,
380 RECT *pRect,
381 const wxString& text,
382 COLORREF col)
383 {
384 COLORREF colOld = SetTextColor(hdc, col);
385 int modeOld = SetBkMode(hdc, TRANSPARENT);
386
387 DrawText(hdc, text, text.length(), pRect,
388 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
389
390 SetBkMode(hdc, modeOld);
391 SetTextColor(hdc, colOld);
392 }
393
394 static void DrawRect(HDC hdc, const RECT& r)
395 {
396 MoveToEx(hdc, r.left, r.top, NULL);
397 LineTo(hdc, r.right, r.top);
398 LineTo(hdc, r.right, r.bottom);
399 LineTo(hdc, r.left, r.bottom);
400 LineTo(hdc, r.left, r.top);
401 }
402
403 void wxButton::MakeOwnerDrawn()
404 {
405 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
406 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
407 {
408 // make it so
409 style |= BS_OWNERDRAW;
410 SetWindowLong(GetHwnd(), GWL_STYLE, style);
411 }
412 }
413
414 bool wxButton::SetBackgroundColour(const wxColour &colour)
415 {
416 if ( !wxControl::SetBackgroundColour(colour) )
417 {
418 // nothing to do
419 return FALSE;
420 }
421
422 MakeOwnerDrawn();
423
424 Refresh();
425
426 return TRUE;
427 }
428
429 bool wxButton::SetForegroundColour(const wxColour &colour)
430 {
431 if ( !wxControl::SetForegroundColour(colour) )
432 {
433 // nothing to do
434 return FALSE;
435 }
436
437 MakeOwnerDrawn();
438
439 Refresh();
440
441 return TRUE;
442 }
443
444 /*
445 The button frame looks like this normally:
446
447 WWWWWWWWWWWWWWWWWWB
448 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
449 WH GB H = light grey (LIGHT)
450 WH GB G = dark grey (SHADOW)
451 WH GB B = black (DKSHADOW)
452 WH GB
453 WGGGGGGGGGGGGGGGGGB
454 BBBBBBBBBBBBBBBBBBB
455
456 When the button is selected, the button becomes like this (the total button
457 size doesn't change):
458
459 BBBBBBBBBBBBBBBBBBB
460 BWWWWWWWWWWWWWWWWBB
461 BWHHHHHHHHHHHHHHGBB
462 BWH GBB
463 BWH GBB
464 BWGGGGGGGGGGGGGGGBB
465 BBBBBBBBBBBBBBBBBBB
466 BBBBBBBBBBBBBBBBBBB
467
468 When the button is pushed (while selected) it is like:
469
470 BBBBBBBBBBBBBBBBBBB
471 BGGGGGGGGGGGGGGGGGB
472 BG GB
473 BG GB
474 BG GB
475 BG GB
476 BGGGGGGGGGGGGGGGGGB
477 BBBBBBBBBBBBBBBBBBB
478 */
479
480 static void DrawButtonFrame(HDC hdc, const RECT& rectBtn,
481 bool selected, bool pushed)
482 {
483 RECT r;
484 CopyRect(&r, &rectBtn);
485
486 HPEN hpenBlack = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW)),
487 hpenGrey = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW)),
488 hpenLightGr = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DLIGHT)),
489 hpenWhite = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DHILIGHT));
490
491 HPEN hpenOld = (HPEN)SelectObject(hdc, hpenBlack);
492
493 r.right--;
494 r.bottom--;
495
496 if ( pushed )
497 {
498 DrawRect(hdc, r);
499
500 (void)SelectObject(hdc, hpenGrey);
501 InflateRect(&r, -1, -1);
502
503 DrawRect(hdc, r);
504 }
505 else // !pushed
506 {
507 if ( selected )
508 {
509 DrawRect(hdc, r);
510
511 InflateRect(&r, -1, -1);
512 }
513
514 MoveToEx(hdc, r.left, r.bottom, NULL);
515 LineTo(hdc, r.right, r.bottom);
516 LineTo(hdc, r.right, r.top - 1);
517
518 (void)SelectObject(hdc, hpenWhite);
519 MoveToEx(hdc, r.left, r.bottom - 1, NULL);
520 LineTo(hdc, r.left, r.top);
521 LineTo(hdc, r.right, r.top);
522
523 (void)SelectObject(hdc, hpenLightGr);
524 MoveToEx(hdc, r.left + 1, r.bottom - 2, NULL);
525 LineTo(hdc, r.left + 1, r.top + 1);
526 LineTo(hdc, r.right - 1, r.top + 1);
527
528 (void)SelectObject(hdc, hpenGrey);
529 MoveToEx(hdc, r.left + 1, r.bottom - 1, NULL);
530 LineTo(hdc, r.right - 1, r.bottom - 1);
531 LineTo(hdc, r.right - 1, r.top);
532 }
533
534 (void)SelectObject(hdc, hpenOld);
535 DeleteObject(hpenWhite);
536 DeleteObject(hpenLightGr);
537 DeleteObject(hpenGrey);
538 DeleteObject(hpenBlack);
539 }
540
541 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis)
542 {
543 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)wxdis;
544
545 RECT rectBtn;
546 CopyRect(&rectBtn, &lpDIS->rcItem);
547
548 COLORREF colBg = wxColourToRGB(GetBackgroundColour()),
549 colFg = wxColourToRGB(GetForegroundColour());
550
551 HDC hdc = lpDIS->hDC;
552 UINT state = lpDIS->itemState;
553
554 // first, draw the background
555 HBRUSH hbrushBackground = ::CreateSolidBrush(colBg);
556
557 FillRect(hdc, &rectBtn, hbrushBackground);
558
559 // draw the border for the current state
560 bool selected = (state & ODS_SELECTED) != 0;
561 if ( !selected )
562 {
563 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
564 if ( panel )
565 {
566 selected = panel->GetDefaultItem() == this;
567 }
568 }
569 bool pushed = (SendMessage(GetHwnd(), BM_GETSTATE, 0, 0) & BST_PUSHED) != 0;
570
571 DrawButtonFrame(hdc, rectBtn, selected, pushed);
572
573 // draw the focus rect if needed
574 if ( state & ODS_FOCUS )
575 {
576 RECT rectFocus;
577 CopyRect(&rectFocus, &rectBtn);
578
579 // I don't know where does this constant come from, but this is how
580 // Windows draws them
581 InflateRect(&rectFocus, -4, -4);
582
583 DrawFocusRect(hdc, &rectFocus);
584 }
585
586 if ( pushed )
587 {
588 // the label is shifted by 1 pixel to create "pushed" effect
589 OffsetRect(&rectBtn, 1, 1);
590 }
591
592 DrawButtonText(hdc, &rectBtn, GetLabel(),
593 state & ODS_DISABLED ? GetSysColor(COLOR_GRAYTEXT)
594 : colFg);
595
596 ::DeleteObject(hbrushBackground);
597
598 return TRUE;
599 }
600
601 #endif // __WIN32__
602
603 #endif // wxUSE_BUTTON
604