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