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