]> git.saurik.com Git - wxWidgets.git/blame - src/msw/button.cpp
wxMSW: return correct value from wxMessageDialog::GetReturnCode().
[wxWidgets.git] / src / msw / button.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
f1e01716 2// Name: src/msw/button.cpp
2bda0e17
KB
3// Purpose: wxButton
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
6c9a19aa 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
2bda0e17
KB
9/////////////////////////////////////////////////////////////////////////////
10
edccf428
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
cd0b1709 18
2bda0e17
KB
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
edccf428 23 #pragma hdrstop
2bda0e17
KB
24#endif
25
1e6feb95
VZ
26#if wxUSE_BUTTON
27
f1e01716
WS
28#include "wx/button.h"
29
2bda0e17 30#ifndef WX_PRECOMP
bac409a0 31 #include "wx/app.h"
edccf428 32 #include "wx/brush.h"
4e938f5b 33 #include "wx/panel.h"
8a4df159 34 #include "wx/bmpbuttn.h"
fb39c7ec
RR
35 #include "wx/settings.h"
36 #include "wx/dcscreen.h"
61e6a2ab 37 #include "wx/dcclient.h"
b84aec03 38 #include "wx/toplevel.h"
d2bc8725
PC
39 #include "wx/msw/wrapcctl.h"
40 #include "wx/msw/private.h"
41 #include "wx/msw/missing.h"
2bda0e17
KB
42#endif
43
bdf14bff 44#include "wx/imaglist.h"
5f7bcb48 45#include "wx/stockitem.h"
533171c2 46#include "wx/msw/private/button.h"
d8c89c48 47#include "wx/msw/private/dc.h"
5c3c1372 48#include "wx/private/window.h"
d8c89c48 49
95912bdd
VZ
50#if wxUSE_MARKUP
51 #include "wx/generic/private/markuptext.h"
52#endif // wxUSE_MARKUP
53
f2d7fdf7
VZ
54// set the value for BCM_SETSHIELD (for the UAC shield) if it's not defined in
55// the header
56#ifndef BCM_SETSHIELD
57 #define BCM_SETSHIELD 0x160c
58#endif
59
edccf428
VZ
60// ----------------------------------------------------------------------------
61// macros
62// ----------------------------------------------------------------------------
63
4cf1a9bf
VZ
64BEGIN_EVENT_TABLE(wxButton, wxButtonBase)
65 EVT_CHAR_HOOK(wxButton::OnCharHook)
66END_EVENT_TABLE()
67
edccf428
VZ
68// ============================================================================
69// implementation
70// ============================================================================
71
72// ----------------------------------------------------------------------------
73// creation/destruction
74// ----------------------------------------------------------------------------
75
76bool wxButton::Create(wxWindow *parent,
77 wxWindowID id,
5f7bcb48 78 const wxString& lbl,
edccf428
VZ
79 const wxPoint& pos,
80 const wxSize& size,
81 long style,
82 const wxValidator& validator,
83 const wxString& name)
2bda0e17 84{
5f7bcb48
VS
85 wxString label(lbl);
86 if (label.empty() && wxIsStockID(id))
c87fc285 87 {
c4e1d0fc
VZ
88 // On Windows, some buttons aren't supposed to have mnemonics
89 label = wxGetStockLabel
90 (
91 id,
92 id == wxID_OK || id == wxID_CANCEL || id == wxID_CLOSE
93 ? wxSTOCK_NOFLAGS
94 : wxSTOCK_WITH_MNEMONIC
95 );
f1e01716
WS
96 }
97
5b2f31eb 98 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
fcf90ee1 99 return false;
edccf428 100
8292017c
VZ
101 WXDWORD exstyle;
102 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
103
8292017c
VZ
104 // if the label contains several lines we must explicitly tell the button
105 // about it or it wouldn't draw it correctly ("\n"s would just appear as
106 // black boxes)
107 //
108 // NB: we do it here and not in MSWGetStyle() because we need the label
d94de683 109 // value and the label is not set yet when MSWGetStyle() is called
533171c2 110 msStyle |= wxMSWButton::GetMultilineStyle(label);
8292017c 111
9a83f860 112 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, exstyle);
5b2f31eb
VZ
113}
114
115wxButton::~wxButton()
116{
6c20e8f8
VZ
117 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
118 if ( tlw && tlw->GetTmpDefaultItem() == this )
789367e1
VZ
119 {
120 UnsetTmpDefault();
121 }
5b2f31eb 122}
edccf428 123
5b2f31eb
VZ
124// ----------------------------------------------------------------------------
125// flags
126// ----------------------------------------------------------------------------
cd0b1709 127
5b2f31eb
VZ
128WXDWORD wxButton::MSWGetStyle(long style, WXDWORD *exstyle) const
129{
130 // buttons never have an external border, they draw their own one
131 WXDWORD msStyle = wxControl::MSWGetStyle
132 (
133 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
134 );
f6bcfd97 135
5b2f31eb 136 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
d13b34d3 137 // each other in any resizable dialog which has more than one button in
5b2f31eb
VZ
138 // the bottom
139 msStyle |= WS_CLIPSIBLINGS;
b0766406 140
5b2f31eb
VZ
141 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
142 // and wxBU_RIGHT to get BS_CENTER!
143 if ( style & wxBU_LEFT )
f6bcfd97 144 msStyle |= BS_LEFT;
5b2f31eb 145 if ( style & wxBU_RIGHT )
f6bcfd97 146 msStyle |= BS_RIGHT;
5b2f31eb 147 if ( style & wxBU_TOP )
f6bcfd97 148 msStyle |= BS_TOP;
5b2f31eb 149 if ( style & wxBU_BOTTOM )
f6bcfd97 150 msStyle |= BS_BOTTOM;
8cc4850c 151#ifndef __WXWINCE__
8a094d7b
JS
152 // flat 2d buttons
153 if ( style & wxNO_BORDER )
154 msStyle |= BS_FLAT;
8cc4850c 155#endif // __WXWINCE__
edccf428 156
5b2f31eb 157 return msStyle;
2bda0e17
KB
158}
159
e1f36ff8 160/* static */
1e6feb95 161wxSize wxButtonBase::GetDefaultSize()
e1f36ff8 162{
8c3c31d4 163 static wxSize s_sizeBtn;
e1f36ff8 164
8c3c31d4
VZ
165 if ( s_sizeBtn.x == 0 )
166 {
167 wxScreenDC dc;
a756f210 168 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
8c3c31d4 169
5c3c1372
VS
170 // The size of a standard button in the dialog units is 50x14,
171 // translate this to pixels.
172 //
173 // Windows' computes dialog units using average character width over
174 // upper- and lower-case ASCII alphabet and not using the average
175 // character width metadata stored in the font; see
176 // http://support.microsoft.com/default.aspx/kb/145994 for detailed
177 // discussion.
178 //
179 // NB: wxMulDivInt32() is used, because it correctly rounds the result
180
181 const wxSize base = wxPrivate::GetAverageASCIILetterSize(dc);
182 s_sizeBtn.x = wxMulDivInt32(50, base.x, 4);
183 s_sizeBtn.y = wxMulDivInt32(14, base.y, 8);
8c3c31d4 184 }
e1f36ff8 185
8c3c31d4 186 return s_sizeBtn;
e1f36ff8
VZ
187}
188
4438caf4 189// ----------------------------------------------------------------------------
036da5e3 190// default button handling
4438caf4
VZ
191// ----------------------------------------------------------------------------
192
d78f09e2 193/*
b0134e31
VZ
194 In normal Windows programs there is no need to handle default button
195 manually because this is taken care by the system provided you use
196 WM_NEXTDLGCTL and not just SetFocus() to switch focus betweeh the controls
197 (see http://blogs.msdn.com/oldnewthing/archive/2004/08/02/205624.aspx for
198 the full explanation why just calling SetFocus() is not enough).
199
200 However this only works if the window is a dialog, i.e. uses DefDlgProc(),
201 but not with plain windows using DefWindowProc() and we do want to have
202 default buttons inside frames as well, so we're forced to reimplement all
203 this logic ourselves. It would be great to avoid having to do this but using
204 DefDlgProc() for all the windows would almost certainly result in more
205 problems, we'd need to carefully filter messages and pass some of them to
206 DefWindowProc() and some of them to DefDlgProc() which looks dangerous (what
207 if the handling of some message changes in some Windows version?), so doing
208 this ourselves is probably a lesser evil.
209
210 Read the rest to learn everything you ever wanted to know about the default
211 buttons but were afraid to ask.
eb74a51f 212
d78f09e2
VZ
213
214 In MSW the default button should be activated when the user presses Enter
215 and the current control doesn't process Enter itself somehow. This is
216 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
217 Another aspect of "defaultness" is that the default button has different
218 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
7fb1b2b4
VZ
219 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
220 be unset if our parent window is not active so it should be unset whenever
221 we lose activation and set back when we regain it.
d78f09e2
VZ
222
223 Final complication is that when a button is active, it should be the default
224 one, i.e. pressing Enter on a button always activates it and not another
225 one.
226
227 We handle this by maintaining a permanent and a temporary default items in
228 wxControlContainer (both may be NULL). When a button becomes the current
229 control (i.e. gets focus) it sets itself as the temporary default which
230 ensures that it has the right appearance and that Enter will be redirected
231 to it. When the button loses focus, it unsets the temporary default and so
232 the default item will be the permanent default -- that is the default button
233 if any had been set or none otherwise, which is just what we want.
d78f09e2
VZ
234 */
235
036da5e3 236// set this button as the (permanently) default one in its panel
94aff5ff 237wxWindow *wxButton::SetDefault()
2bda0e17 238{
77ffb593 239 // set this one as the default button both for wxWidgets ...
94aff5ff 240 wxWindow *winOldDefault = wxButtonBase::SetDefault();
036da5e3 241
7fb1b2b4 242 // ... and Windows
fcf90ee1
WS
243 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false);
244 SetDefaultStyle(this, true);
ebc0b155
VZ
245
246 return winOldDefault;
036da5e3
VZ
247}
248
627c8d89
VZ
249// return the top level parent window if it's not being deleted yet, otherwise
250// return NULL
05c5f281
VZ
251static wxTopLevelWindow *GetTLWParentIfNotBeingDeleted(wxWindow *win)
252{
627c8d89 253 for ( ;; )
05c5f281 254 {
627c8d89
VZ
255 // IsTopLevel() will return false for a wxTLW being deleted, so we also
256 // need the parent test for this case
257 wxWindow * const parent = win->GetParent();
258 if ( !parent || win->IsTopLevel() )
9617f65b
VZ
259 {
260 if ( win->IsBeingDeleted() )
261 return NULL;
627c8d89 262
05c5f281 263 break;
9617f65b 264 }
627c8d89
VZ
265
266 win = parent;
05c5f281
VZ
267 }
268
9a83f860 269 wxASSERT_MSG( win, wxT("button without top level parent?") );
05c5f281 270
627c8d89 271 wxTopLevelWindow * const tlw = wxDynamicCast(win, wxTopLevelWindow);
9a83f860 272 wxASSERT_MSG( tlw, wxT("logic error in GetTLWParentIfNotBeingDeleted()") );
627c8d89
VZ
273
274 return tlw;
05c5f281
VZ
275}
276
7fb1b2b4 277// set this button as being currently default
036da5e3
VZ
278void wxButton::SetTmpDefault()
279{
76cae7cb 280 wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(this);
05c5f281
VZ
281 if ( !tlw )
282 return;
036da5e3 283
6c20e8f8
VZ
284 wxWindow *winOldDefault = tlw->GetDefaultItem();
285 tlw->SetTmpDefaultItem(this);
7fb1b2b4 286
fcf90ee1
WS
287 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false);
288 SetDefaultStyle(this, true);
036da5e3
VZ
289}
290
7fb1b2b4 291// unset this button as currently default, it may still stay permanent default
036da5e3
VZ
292void wxButton::UnsetTmpDefault()
293{
76cae7cb 294 wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(this);
05c5f281
VZ
295 if ( !tlw )
296 return;
036da5e3 297
6c20e8f8 298 tlw->SetTmpDefaultItem(NULL);
036da5e3 299
6c20e8f8 300 wxWindow *winOldDefault = tlw->GetDefaultItem();
7fb1b2b4 301
fcf90ee1
WS
302 SetDefaultStyle(this, false);
303 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), true);
036da5e3 304}
8ed57d93 305
036da5e3
VZ
306/* static */
307void
7fb1b2b4 308wxButton::SetDefaultStyle(wxButton *btn, bool on)
036da5e3 309{
7fb1b2b4
VZ
310 // we may be called with NULL pointer -- simpler to do the check here than
311 // in the caller which does wxDynamicCast()
312 if ( !btn )
313 return;
314
315 // first, let DefDlgProc() know about the new default button
316 if ( on )
5d1d2d46 317 {
7fb1b2b4
VZ
318 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
319 // focus at all any more
320 if ( !wxTheApp->IsActive() )
321 return;
cd0b1709 322
6c20e8f8 323 wxWindow * const tlw = wxGetTopLevelParent(btn);
9a83f860 324 wxCHECK_RET( tlw, wxT("button without top level window?") );
cef55d64 325
6c20e8f8 326 ::SendMessage(GetHwndOf(tlw), DM_SETDEFID, btn->GetId(), 0L);
cef55d64
VZ
327
328 // sending DM_SETDEFID also changes the button style to
329 // BS_DEFPUSHBUTTON so there is nothing more to do
5d1d2d46
VZ
330 }
331
7fb1b2b4
VZ
332 // then also change the style as needed
333 long style = ::GetWindowLong(GetHwndOf(btn), GWL_STYLE);
334 if ( !(style & BS_DEFPUSHBUTTON) == on )
be4017f8 335 {
7fb1b2b4
VZ
336 // don't do it with the owner drawn buttons because it will
337 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
338 // BS_DEFPUSHBUTTON != 0)!
036da5e3
VZ
339 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
340 {
7fb1b2b4
VZ
341 ::SendMessage(GetHwndOf(btn), BM_SETSTYLE,
342 on ? style | BS_DEFPUSHBUTTON
343 : style & ~BS_DEFPUSHBUTTON,
344 1L /* redraw */);
036da5e3 345 }
7fb1b2b4 346 else // owner drawn
036da5e3 347 {
7fb1b2b4
VZ
348 // redraw the button - it will notice itself that it's
349 // [not] the default one [any longer]
350 btn->Refresh();
036da5e3 351 }
be4017f8 352 }
7fb1b2b4 353 //else: already has correct style
2bda0e17
KB
354}
355
edccf428
VZ
356// ----------------------------------------------------------------------------
357// helpers
358// ----------------------------------------------------------------------------
359
360bool wxButton::SendClickEvent()
2bda0e17 361{
ce7fe42e 362 wxCommandEvent event(wxEVT_BUTTON, GetId());
edccf428
VZ
363 event.SetEventObject(this);
364
365 return ProcessCommand(event);
2bda0e17
KB
366}
367
edccf428 368void wxButton::Command(wxCommandEvent & event)
2bda0e17 369{
edccf428 370 ProcessCommand(event);
2bda0e17
KB
371}
372
edccf428
VZ
373// ----------------------------------------------------------------------------
374// event/message handlers
375// ----------------------------------------------------------------------------
2bda0e17 376
4cf1a9bf
VZ
377void wxButton::OnCharHook(wxKeyEvent& event)
378{
379 // We want to ensure that the button always processes Enter key events
380 // itself, even if it's inside some control that normally takes over them
381 // (this happens when the button is part of an in-place editor control for
382 // example).
383 if ( event.GetKeyCode() == WXK_RETURN )
384 {
385 // We should ensure that subsequent key events are still generated even
386 // if we did handle EVT_CHAR_HOOK (normally this would suppress their
387 // generation).
388 event.DoAllowNextEvent();
389 }
390 else
391 {
392 event.Skip();
393 }
394}
395
33ac7e6f 396bool wxButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
edccf428 397{
fcf90ee1 398 bool processed = false;
57c0af52 399 switch ( param )
edccf428 400 {
5aab763c
RD
401 // NOTE: Apparently older versions (NT 4?) of the common controls send
402 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
87b6002d 403 // buttons, so in order to send two EVT_BUTTON events we should
5aab763c
RD
404 // catch both types. Currently (Feb 2003) up-to-date versions of
405 // win98, win2k and winXP all send two BN_CLICKED messages for
406 // all button types, so we don't catch BN_DOUBLECLICKED anymore
407 // in order to not get 3 EVT_BUTTON events. If this is a problem
408 // then we need to figure out which version of the comctl32 changed
409 // this behaviour and test for it.
410
a95e38c0
VZ
411 case 1: // message came from an accelerator
412 case BN_CLICKED: // normal buttons send this
57c0af52
VZ
413 processed = SendClickEvent();
414 break;
edccf428 415 }
2bda0e17 416
edccf428 417 return processed;
2bda0e17
KB
418}
419
c140b7e7 420WXLRESULT wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
678cd6de 421{
87b6002d 422 // when we receive focus, we want to temporarily become the default button in
036da5e3
VZ
423 // our parent panel so that pressing "Enter" would activate us -- and when
424 // losing it we should restore the previous default button as well
be4017f8 425 if ( nMsg == WM_SETFOCUS )
678cd6de 426 {
036da5e3 427 SetTmpDefault();
be4017f8 428
036da5e3
VZ
429 // let the default processing take place too
430 }
431 else if ( nMsg == WM_KILLFOCUS )
432 {
433 UnsetTmpDefault();
678cd6de
VZ
434 }
435
436 // let the base class do all real processing
b4354db1 437 return wxAnyButton::MSWWindowProc(nMsg, wParam, lParam);
678cd6de 438}
cd0b1709 439
f2d7fdf7
VZ
440// ----------------------------------------------------------------------------
441// authentication needed handling
442// ----------------------------------------------------------------------------
443
444bool wxButton::DoGetAuthNeeded() const
445{
446 return m_authNeeded;
447}
448
449void wxButton::DoSetAuthNeeded(bool show)
450{
451 // show/hide UAC symbol on Windows Vista and later
452 if ( wxGetWinVersion() >= wxWinVersion_6 )
453 {
454 m_authNeeded = show;
455 ::SendMessage(GetHwnd(), BCM_SETSHIELD, 0, show);
456 InvalidateBestSize();
457 }
458}
459
1e6feb95 460#endif // wxUSE_BUTTON
4af4dec6 461