]> git.saurik.com Git - wxWidgets.git/blame - src/msw/button.cpp
Fix asserts when removing the menu item starting radio group in wxOSX.
[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
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
edccf428
VZ
65// ============================================================================
66// implementation
67// ============================================================================
68
69// ----------------------------------------------------------------------------
70// creation/destruction
71// ----------------------------------------------------------------------------
72
73bool wxButton::Create(wxWindow *parent,
74 wxWindowID id,
5f7bcb48 75 const wxString& lbl,
edccf428
VZ
76 const wxPoint& pos,
77 const wxSize& size,
78 long style,
79 const wxValidator& validator,
80 const wxString& name)
2bda0e17 81{
5f7bcb48
VS
82 wxString label(lbl);
83 if (label.empty() && wxIsStockID(id))
c87fc285 84 {
c4e1d0fc
VZ
85 // On Windows, some buttons aren't supposed to have mnemonics
86 label = wxGetStockLabel
87 (
88 id,
89 id == wxID_OK || id == wxID_CANCEL || id == wxID_CLOSE
90 ? wxSTOCK_NOFLAGS
91 : wxSTOCK_WITH_MNEMONIC
92 );
f1e01716
WS
93 }
94
5b2f31eb 95 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
fcf90ee1 96 return false;
edccf428 97
8292017c
VZ
98 WXDWORD exstyle;
99 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
100
8292017c
VZ
101 // if the label contains several lines we must explicitly tell the button
102 // about it or it wouldn't draw it correctly ("\n"s would just appear as
103 // black boxes)
104 //
105 // NB: we do it here and not in MSWGetStyle() because we need the label
d94de683 106 // value and the label is not set yet when MSWGetStyle() is called
533171c2 107 msStyle |= wxMSWButton::GetMultilineStyle(label);
8292017c 108
9a83f860 109 return MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, exstyle);
5b2f31eb
VZ
110}
111
112wxButton::~wxButton()
113{
6c20e8f8
VZ
114 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
115 if ( tlw && tlw->GetTmpDefaultItem() == this )
789367e1
VZ
116 {
117 UnsetTmpDefault();
118 }
5b2f31eb 119}
edccf428 120
5b2f31eb
VZ
121// ----------------------------------------------------------------------------
122// flags
123// ----------------------------------------------------------------------------
cd0b1709 124
5b2f31eb
VZ
125WXDWORD wxButton::MSWGetStyle(long style, WXDWORD *exstyle) const
126{
127 // buttons never have an external border, they draw their own one
128 WXDWORD msStyle = wxControl::MSWGetStyle
129 (
130 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
131 );
f6bcfd97 132
5b2f31eb 133 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
d13b34d3 134 // each other in any resizable dialog which has more than one button in
5b2f31eb
VZ
135 // the bottom
136 msStyle |= WS_CLIPSIBLINGS;
b0766406 137
5b2f31eb
VZ
138 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
139 // and wxBU_RIGHT to get BS_CENTER!
140 if ( style & wxBU_LEFT )
f6bcfd97 141 msStyle |= BS_LEFT;
5b2f31eb 142 if ( style & wxBU_RIGHT )
f6bcfd97 143 msStyle |= BS_RIGHT;
5b2f31eb 144 if ( style & wxBU_TOP )
f6bcfd97 145 msStyle |= BS_TOP;
5b2f31eb 146 if ( style & wxBU_BOTTOM )
f6bcfd97 147 msStyle |= BS_BOTTOM;
8cc4850c 148#ifndef __WXWINCE__
8a094d7b
JS
149 // flat 2d buttons
150 if ( style & wxNO_BORDER )
151 msStyle |= BS_FLAT;
8cc4850c 152#endif // __WXWINCE__
edccf428 153
5b2f31eb 154 return msStyle;
2bda0e17
KB
155}
156
e1f36ff8 157/* static */
1e6feb95 158wxSize wxButtonBase::GetDefaultSize()
e1f36ff8 159{
8c3c31d4 160 static wxSize s_sizeBtn;
e1f36ff8 161
8c3c31d4
VZ
162 if ( s_sizeBtn.x == 0 )
163 {
164 wxScreenDC dc;
a756f210 165 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
8c3c31d4 166
5c3c1372
VS
167 // The size of a standard button in the dialog units is 50x14,
168 // translate this to pixels.
169 //
170 // Windows' computes dialog units using average character width over
171 // upper- and lower-case ASCII alphabet and not using the average
172 // character width metadata stored in the font; see
173 // http://support.microsoft.com/default.aspx/kb/145994 for detailed
174 // discussion.
175 //
176 // NB: wxMulDivInt32() is used, because it correctly rounds the result
177
178 const wxSize base = wxPrivate::GetAverageASCIILetterSize(dc);
179 s_sizeBtn.x = wxMulDivInt32(50, base.x, 4);
180 s_sizeBtn.y = wxMulDivInt32(14, base.y, 8);
8c3c31d4 181 }
e1f36ff8 182
8c3c31d4 183 return s_sizeBtn;
e1f36ff8
VZ
184}
185
4438caf4 186// ----------------------------------------------------------------------------
036da5e3 187// default button handling
4438caf4
VZ
188// ----------------------------------------------------------------------------
189
d78f09e2 190/*
eb74a51f
VZ
191 The comment below and all this code is probably due to not using WM_NEXTDLGCTL
192 message when changing focus (but just SetFocus() which is not enough), see
193 http://blogs.msdn.com/oldnewthing/archive/2004/08/02/205624.aspx for the
194 full explanation.
195
196 TODO: Do use WM_NEXTDLGCTL and get rid of all this code.
197
198
d78f09e2
VZ
199 "Everything you ever wanted to know about the default buttons" or "Why do we
200 have to do all this?"
201
202 In MSW the default button should be activated when the user presses Enter
203 and the current control doesn't process Enter itself somehow. This is
204 handled by ::DefWindowProc() (or maybe ::DefDialogProc()) using DM_SETDEFID
205 Another aspect of "defaultness" is that the default button has different
206 appearance: this is due to BS_DEFPUSHBUTTON style which is completely
7fb1b2b4
VZ
207 separate from DM_SETDEFID stuff (!). Also note that BS_DEFPUSHBUTTON should
208 be unset if our parent window is not active so it should be unset whenever
209 we lose activation and set back when we regain it.
d78f09e2
VZ
210
211 Final complication is that when a button is active, it should be the default
212 one, i.e. pressing Enter on a button always activates it and not another
213 one.
214
215 We handle this by maintaining a permanent and a temporary default items in
216 wxControlContainer (both may be NULL). When a button becomes the current
217 control (i.e. gets focus) it sets itself as the temporary default which
218 ensures that it has the right appearance and that Enter will be redirected
219 to it. When the button loses focus, it unsets the temporary default and so
220 the default item will be the permanent default -- that is the default button
221 if any had been set or none otherwise, which is just what we want.
222
7fb1b2b4
VZ
223 NB: all this is quite complicated by now and the worst is that normally
224 it shouldn't be necessary at all as for the normal Windows programs
225 DefWindowProc() and IsDialogMessage() take care of all this
77ffb593 226 automatically -- however in wxWidgets programs this doesn't work for
7fb1b2b4
VZ
227 nested hierarchies (i.e. a notebook inside a notebook) for unknown
228 reason and so we have to reproduce all this code ourselves. It would be
229 very nice if we could avoid doing it.
d78f09e2
VZ
230 */
231
036da5e3 232// set this button as the (permanently) default one in its panel
94aff5ff 233wxWindow *wxButton::SetDefault()
2bda0e17 234{
77ffb593 235 // set this one as the default button both for wxWidgets ...
94aff5ff 236 wxWindow *winOldDefault = wxButtonBase::SetDefault();
036da5e3 237
7fb1b2b4 238 // ... and Windows
fcf90ee1
WS
239 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false);
240 SetDefaultStyle(this, true);
ebc0b155
VZ
241
242 return winOldDefault;
036da5e3
VZ
243}
244
627c8d89
VZ
245// return the top level parent window if it's not being deleted yet, otherwise
246// return NULL
05c5f281
VZ
247static wxTopLevelWindow *GetTLWParentIfNotBeingDeleted(wxWindow *win)
248{
627c8d89 249 for ( ;; )
05c5f281 250 {
627c8d89
VZ
251 // IsTopLevel() will return false for a wxTLW being deleted, so we also
252 // need the parent test for this case
253 wxWindow * const parent = win->GetParent();
254 if ( !parent || win->IsTopLevel() )
9617f65b
VZ
255 {
256 if ( win->IsBeingDeleted() )
257 return NULL;
627c8d89 258
05c5f281 259 break;
9617f65b 260 }
627c8d89
VZ
261
262 win = parent;
05c5f281
VZ
263 }
264
9a83f860 265 wxASSERT_MSG( win, wxT("button without top level parent?") );
05c5f281 266
627c8d89 267 wxTopLevelWindow * const tlw = wxDynamicCast(win, wxTopLevelWindow);
9a83f860 268 wxASSERT_MSG( tlw, wxT("logic error in GetTLWParentIfNotBeingDeleted()") );
627c8d89
VZ
269
270 return tlw;
05c5f281
VZ
271}
272
7fb1b2b4 273// set this button as being currently default
036da5e3
VZ
274void wxButton::SetTmpDefault()
275{
05c5f281
VZ
276 wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(GetParent());
277 if ( !tlw )
278 return;
036da5e3 279
6c20e8f8
VZ
280 wxWindow *winOldDefault = tlw->GetDefaultItem();
281 tlw->SetTmpDefaultItem(this);
7fb1b2b4 282
fcf90ee1
WS
283 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false);
284 SetDefaultStyle(this, true);
036da5e3
VZ
285}
286
7fb1b2b4 287// unset this button as currently default, it may still stay permanent default
036da5e3
VZ
288void wxButton::UnsetTmpDefault()
289{
05c5f281
VZ
290 wxTopLevelWindow * const tlw = GetTLWParentIfNotBeingDeleted(GetParent());
291 if ( !tlw )
292 return;
036da5e3 293
6c20e8f8 294 tlw->SetTmpDefaultItem(NULL);
036da5e3 295
6c20e8f8 296 wxWindow *winOldDefault = tlw->GetDefaultItem();
7fb1b2b4 297
fcf90ee1
WS
298 SetDefaultStyle(this, false);
299 SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), true);
036da5e3 300}
8ed57d93 301
036da5e3
VZ
302/* static */
303void
7fb1b2b4 304wxButton::SetDefaultStyle(wxButton *btn, bool on)
036da5e3 305{
7fb1b2b4
VZ
306 // we may be called with NULL pointer -- simpler to do the check here than
307 // in the caller which does wxDynamicCast()
308 if ( !btn )
309 return;
310
311 // first, let DefDlgProc() know about the new default button
312 if ( on )
5d1d2d46 313 {
7fb1b2b4
VZ
314 // we shouldn't set BS_DEFPUSHBUTTON for any button if we don't have
315 // focus at all any more
316 if ( !wxTheApp->IsActive() )
317 return;
cd0b1709 318
6c20e8f8 319 wxWindow * const tlw = wxGetTopLevelParent(btn);
9a83f860 320 wxCHECK_RET( tlw, wxT("button without top level window?") );
cef55d64 321
6c20e8f8 322 ::SendMessage(GetHwndOf(tlw), DM_SETDEFID, btn->GetId(), 0L);
cef55d64
VZ
323
324 // sending DM_SETDEFID also changes the button style to
325 // BS_DEFPUSHBUTTON so there is nothing more to do
5d1d2d46
VZ
326 }
327
7fb1b2b4
VZ
328 // then also change the style as needed
329 long style = ::GetWindowLong(GetHwndOf(btn), GWL_STYLE);
330 if ( !(style & BS_DEFPUSHBUTTON) == on )
be4017f8 331 {
7fb1b2b4
VZ
332 // don't do it with the owner drawn buttons because it will
333 // reset BS_OWNERDRAW style bit too (as BS_OWNERDRAW &
334 // BS_DEFPUSHBUTTON != 0)!
036da5e3
VZ
335 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
336 {
7fb1b2b4
VZ
337 ::SendMessage(GetHwndOf(btn), BM_SETSTYLE,
338 on ? style | BS_DEFPUSHBUTTON
339 : style & ~BS_DEFPUSHBUTTON,
340 1L /* redraw */);
036da5e3 341 }
7fb1b2b4 342 else // owner drawn
036da5e3 343 {
7fb1b2b4
VZ
344 // redraw the button - it will notice itself that it's
345 // [not] the default one [any longer]
346 btn->Refresh();
036da5e3 347 }
be4017f8 348 }
7fb1b2b4 349 //else: already has correct style
2bda0e17
KB
350}
351
edccf428
VZ
352// ----------------------------------------------------------------------------
353// helpers
354// ----------------------------------------------------------------------------
355
356bool wxButton::SendClickEvent()
2bda0e17 357{
edccf428
VZ
358 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
359 event.SetEventObject(this);
360
361 return ProcessCommand(event);
2bda0e17
KB
362}
363
edccf428 364void wxButton::Command(wxCommandEvent & event)
2bda0e17 365{
edccf428 366 ProcessCommand(event);
2bda0e17
KB
367}
368
edccf428
VZ
369// ----------------------------------------------------------------------------
370// event/message handlers
371// ----------------------------------------------------------------------------
2bda0e17 372
33ac7e6f 373bool wxButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
edccf428 374{
fcf90ee1 375 bool processed = false;
57c0af52 376 switch ( param )
edccf428 377 {
5aab763c
RD
378 // NOTE: Apparently older versions (NT 4?) of the common controls send
379 // BN_DOUBLECLICKED but not a second BN_CLICKED for owner-drawn
87b6002d 380 // buttons, so in order to send two EVT_BUTTON events we should
5aab763c
RD
381 // catch both types. Currently (Feb 2003) up-to-date versions of
382 // win98, win2k and winXP all send two BN_CLICKED messages for
383 // all button types, so we don't catch BN_DOUBLECLICKED anymore
384 // in order to not get 3 EVT_BUTTON events. If this is a problem
385 // then we need to figure out which version of the comctl32 changed
386 // this behaviour and test for it.
387
a95e38c0
VZ
388 case 1: // message came from an accelerator
389 case BN_CLICKED: // normal buttons send this
57c0af52
VZ
390 processed = SendClickEvent();
391 break;
edccf428 392 }
2bda0e17 393
edccf428 394 return processed;
2bda0e17
KB
395}
396
c140b7e7 397WXLRESULT wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
678cd6de 398{
87b6002d 399 // when we receive focus, we want to temporarily become the default button in
036da5e3
VZ
400 // our parent panel so that pressing "Enter" would activate us -- and when
401 // losing it we should restore the previous default button as well
be4017f8 402 if ( nMsg == WM_SETFOCUS )
678cd6de 403 {
036da5e3 404 SetTmpDefault();
be4017f8 405
036da5e3
VZ
406 // let the default processing take place too
407 }
408 else if ( nMsg == WM_KILLFOCUS )
409 {
410 UnsetTmpDefault();
678cd6de
VZ
411 }
412
413 // let the base class do all real processing
b4354db1 414 return wxAnyButton::MSWWindowProc(nMsg, wParam, lParam);
678cd6de 415}
cd0b1709 416
f2d7fdf7
VZ
417// ----------------------------------------------------------------------------
418// authentication needed handling
419// ----------------------------------------------------------------------------
420
421bool wxButton::DoGetAuthNeeded() const
422{
423 return m_authNeeded;
424}
425
426void wxButton::DoSetAuthNeeded(bool show)
427{
428 // show/hide UAC symbol on Windows Vista and later
429 if ( wxGetWinVersion() >= wxWinVersion_6 )
430 {
431 m_authNeeded = show;
432 ::SendMessage(GetHwnd(), BCM_SETSHIELD, 0, show);
433 InvalidateBestSize();
434 }
435}
436
1e6feb95 437#endif // wxUSE_BUTTON
4af4dec6 438