1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/button.cpp
3 // Purpose: wxButton
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_BUTTON
27
28 #include "wx/button.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/brush.h"
33 #include "wx/panel.h"
34 #include "wx/bmpbuttn.h"
35 #include "wx/settings.h"
36 #include "wx/dcscreen.h"
37 #include "wx/dcclient.h"
38 #include "wx/toplevel.h"
39 #include "wx/msw/wrapcctl.h"
40 #include "wx/msw/private.h"
41 #include "wx/msw/missing.h"
42 #endif
43
44 #include "wx/imaglist.h"
45 #include "wx/stockitem.h"
46 #include "wx/msw/private/button.h"
47 #include "wx/msw/private/dc.h"
48 #include "wx/private/window.h"
49
50 #if wxUSE_MARKUP
51 #include "wx/generic/private/markuptext.h"
52 #endif // wxUSE_MARKUP
53
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
60 // ----------------------------------------------------------------------------
61 // macros
62 // ----------------------------------------------------------------------------
63
BEGIN_EVENT_TABLE(wxButton,wxButtonBase)64 BEGIN_EVENT_TABLE(wxButton, wxButtonBase)
65 EVT_CHAR_HOOK(wxButton::OnCharHook)
66 END_EVENT_TABLE()
67
68 // ============================================================================
69 // implementation
70 // ============================================================================
71
72 // ----------------------------------------------------------------------------
73 // creation/destruction
74 // ----------------------------------------------------------------------------
75
76 bool wxButton::Create(wxWindow *parent,
77 wxWindowID id,
78 const wxString& lbl,
79 const wxPoint& pos,
80 const wxSize& size,
81 long style,
82 const wxValidator& validator,
83 const wxString& name)
84 {
85 wxString label(lbl);
86 if (label.empty() && wxIsStockID(id))
87 {
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 );
96 }
97
98 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
99 return false;
100
101 WXDWORD exstyle;
102 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
103
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
109 // value and the label is not set yet when MSWGetStyle() is called
110 msStyle |= wxMSWButton::GetMultilineStyle(label);
111
112 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, exstyle);
113 }
114
~wxButton()115 wxButton::~wxButton()
116 {
117 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
118 if ( tlw && tlw->GetTmpDefaultItem() == this )
119 {
120 UnsetTmpDefault();
121 }
122 }
123
124 // ----------------------------------------------------------------------------
125 // flags
126 // ----------------------------------------------------------------------------
127
MSWGetStyle(long style,WXDWORD * exstyle) const128 WXDWORD 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 );
135
136 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
137 // each other in any resizable dialog which has more than one button in
138 // the bottom
139 msStyle |= WS_CLIPSIBLINGS;
140
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 )
144 msStyle |= BS_LEFT;
145 if ( style & wxBU_RIGHT )
146 msStyle |= BS_RIGHT;
147 if ( style & wxBU_TOP )
148 msStyle |= BS_TOP;
149 if ( style & wxBU_BOTTOM )
150 msStyle |= BS_BOTTOM;
151 #ifndef __WXWINCE__
152 // flat 2d buttons
153 if ( style & wxNO_BORDER )
154 msStyle |= BS_FLAT;
155 #endif // __WXWINCE__
156
157 return msStyle;
158 }
159
160 /* static */
GetDefaultSize()161 wxSize wxButtonBase::GetDefaultSize()
162 {
163 static wxSize s_sizeBtn;
164
165 if ( s_sizeBtn.x == 0 )
166 {
167 wxScreenDC dc;
168 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
169
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);
184 }
185
186 return s_sizeBtn;
187 }
188
189 // ----------------------------------------------------------------------------
190 // default button handling
191 // ----------------------------------------------------------------------------
192
193 /*
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.
212
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
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.
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.
234 */
235
236 // set this button as the (permanently) default one in its panel
SetDefault()237 wxWindow *wxButton::SetDefault()
238 {
239 // set this one as the default button both for wxWidgets ...
240 wxWindow *winOldDefault = wxButtonBase::SetDefault();
241
242 // ... and Windows
243 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false);
244 SetDefaultStyle(this, true);
245
246 return winOldDefault;
247 }
248
249 // return the top level parent window if it's not being deleted yet, otherwise
250 // return NULL
GetTLWParentIfNotBeingDeleted(wxWindow * win)251 static wxTopLevelWindow *GetTLWParentIfNotBeingDeleted(wxWindow *win)
252 {
253 for ( ;; )
254 {
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() )
259 {
260 if ( win->IsBeingDeleted() )
261 return NULL;
262
263 break;
264 }
265
266 win = parent;
267 }
268
269 wxASSERT_MSG( win, wxT("button without top level parent?") );
270
271 wxTopLevelWindow * const tlw = wxDynamicCast(win, wxTopLevelWindow);
272 wxASSERT_MSG( tlw, wxT("logic error in GetTLWParentIfNotBeingDeleted()") );
273
274 return tlw;
275 }
276
277 // set this button as being currently default
SetTmpDefault()278 void wxButton::SetTmpDefault()
279 {
280 wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(this);
281 if ( !tlw )
282 return;
283
284 wxWindow *winOldDefault = tlw->GetDefaultItem();
285 tlw->SetTmpDefaultItem(this);
286
287 // Notice that the order of these statements is important, the old button
288 // is not reset if we do it the other way round, probably because of
289 // something done by the default DM_SETDEFID handler.
290 SetDefaultStyle(this, true);
291 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false);
292 }
293
294 // unset this button as currently default, it may still stay permanent default
UnsetTmpDefault()295 void wxButton::UnsetTmpDefault()
296 {
297 wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(this);
298 if ( !tlw )
299 return;
300
301 tlw->SetTmpDefaultItem(NULL);
302
303 wxWindow *winOldDefault = tlw->GetDefaultItem();
304
305 // Just as in SetTmpDefault() above, the order is important here.
306 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), true);
307 SetDefaultStyle(this, false);
308 }
309
310 /* static */
311 void
SetDefaultStyle(wxButton * btn,bool on)312 wxButton::SetDefaultStyle(wxButton *btn, bool on)
313 {
314 // we may be called with NULL pointer -- simpler to do the check here than
315 // in the caller which does wxDynamicCast()
316 if ( !btn )
317 return;
318
319 // first, let DefDlgProc() know about the new default button
320 if ( on )
321 {
322 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
323 // focus at all any more
324 if ( !wxTheApp->IsActive() )
325 return;
326
327 wxWindow * const tlw = wxGetTopLevelParent(btn);
328 wxCHECK_RET( tlw, wxT("button without top level window?") );
329
330 ::SendMessage(GetHwndOf(tlw), DM_SETDEFID, btn->GetId(), 0L);
331
332 // sending DM_SETDEFID also changes the button style to
333 // BS_DEFPUSHBUTTON so there is nothing more to do
334 }
335
336 // then also change the style as needed
337 long style = ::GetWindowLong(GetHwndOf(btn), GWL_STYLE);
338 if ( !(style & BS_DEFPUSHBUTTON) == on )
339 {
340 // don't do it with the owner drawn buttons because it will
341 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
342 // BS_DEFPUSHBUTTON != 0)!
343 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
344 {
345 ::SendMessage(GetHwndOf(btn), BM_SETSTYLE,
346 on ? style | BS_DEFPUSHBUTTON
347 : style & ~BS_DEFPUSHBUTTON,
348 1L /* redraw */);
349 }
350 else // owner drawn
351 {
352 // redraw the button - it will notice itself that it's
353 // [not] the default one [any longer]
354 btn->Refresh();
355 }
356 }
357 //else: already has correct style
358 }
359
360 // ----------------------------------------------------------------------------
361 // helpers
362 // ----------------------------------------------------------------------------
363
SendClickEvent()364 bool wxButton::SendClickEvent()
365 {
366 wxCommandEvent event(wxEVT_BUTTON, GetId());
367 event.SetEventObject(this);
368
369 return ProcessCommand(event);
370 }
371
Command(wxCommandEvent & event)372 void wxButton::Command(wxCommandEvent & event)
373 {
374 ProcessCommand(event);
375 }
376
377 // ----------------------------------------------------------------------------
378 // event/message handlers
379 // ----------------------------------------------------------------------------
380
OnCharHook(wxKeyEvent & event)381 void wxButton::OnCharHook(wxKeyEvent& event)
382 {
383 // We want to ensure that the button always processes Enter key events
384 // itself, even if it's inside some control that normally takes over them
385 // (this happens when the button is part of an in-place editor control for
386 // example).
387 if ( event.GetKeyCode() == WXK_RETURN )
388 {
389 // We should ensure that subsequent key events are still generated even
390 // if we did handle EVT_CHAR_HOOK (normally this would suppress their
391 // generation).
392 event.DoAllowNextEvent();
393 }
394 else
395 {
396 event.Skip();
397 }
398 }
399
MSWCommand(WXUINT param,WXWORD WXUNUSED (id))400 bool wxButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
401 {
402 bool processed = false;
403 switch ( param )
404 {
405 // NOTE: Apparently older versions (NT 4?) of the common controls send
406 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
407 // buttons, so in order to send two EVT_BUTTON events we should
408 // catch both types. Currently (Feb 2003) up-to-date versions of
409 // win98, win2k and winXP all send two BN_CLICKED messages for
410 // all button types, so we don't catch BN_DOUBLECLICKED anymore
411 // in order to not get 3 EVT_BUTTON events. If this is a problem
412 // then we need to figure out which version of the comctl32 changed
413 // this behaviour and test for it.
414
415 case 1: // message came from an accelerator
416 case BN_CLICKED: // normal buttons send this
417 processed = SendClickEvent();
418 break;
419 }
420
421 return processed;
422 }
423
MSWWindowProc(WXUINT nMsg,WXWPARAM wParam,WXLPARAM lParam)424 WXLRESULT wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
425 {
426 // when we receive focus, we want to temporarily become the default button in
427 // our parent panel so that pressing "Enter" would activate us -- and when
428 // losing it we should restore the previous default button as well
429 if ( nMsg == WM_SETFOCUS )
430 {
431 SetTmpDefault();
432
433 // let the default processing take place too
434 }
435 else if ( nMsg == WM_KILLFOCUS )
436 {
437 UnsetTmpDefault();
438 }
439
440 // let the base class do all real processing
441 return wxAnyButton::MSWWindowProc(nMsg, wParam, lParam);
442 }
443
444 // ----------------------------------------------------------------------------
445 // authentication needed handling
446 // ----------------------------------------------------------------------------
447
DoGetAuthNeeded() const448 bool wxButton::DoGetAuthNeeded() const
449 {
450 return m_authNeeded;
451 }
452
DoSetAuthNeeded(bool show)453 void wxButton::DoSetAuthNeeded(bool show)
454 {
455 // show/hide UAC symbol on Windows Vista and later
456 if ( wxGetWinVersion() >= wxWinVersion_6 )
457 {
458 m_authNeeded = show;
459 ::SendMessage(GetHwnd(), BCM_SETSHIELD, 0, show);
460 InvalidateBestSize();
461 }
462 }
463
464 #endif // wxUSE_BUTTON
465
466