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