1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/radiobut.cpp
3 // Purpose: wxRadioButton
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/radiobut.h"
31 #include "wx/settings.h"
32 #include "wx/dcscreen.h"
33 #include "wx/toplevel.h"
36 #include "wx/msw/private.h"
38 // ============================================================================
39 // wxRadioButton implementation
40 // ============================================================================
42 // ----------------------------------------------------------------------------
43 // wxRadioButton creation
44 // ----------------------------------------------------------------------------
46 void wxRadioButton::Init()
51 bool wxRadioButton::Create(wxWindow
*parent
,
53 const wxString
& label
,
57 const wxValidator
& validator
,
60 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
64 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
66 if ( !MSWCreateControl(wxT("BUTTON"), msStyle
, pos
, size
, label
, exstyle
) )
69 // for compatibility with wxGTK, the first radio button in a group is
70 // always checked (this makes sense anyhow as you need to ensure that at
71 // least one button in the group is checked and this is the simplest way to
73 if ( HasFlag(wxRB_GROUP
) )
79 // ----------------------------------------------------------------------------
80 // wxRadioButton functions
81 // ----------------------------------------------------------------------------
83 void wxRadioButton::SetValue(bool value
)
85 ::SendMessage(GetHwnd(), BM_SETCHECK
,
86 value
? BST_CHECKED
: BST_UNCHECKED
, 0);
93 // if we set the value of one radio button we also must clear all the other
94 // buttons in the same group: Windows doesn't do it automatically
96 // moreover, if another radiobutton in the group currently has the focus,
97 // we have to set it to this radiobutton, else the old radiobutton will be
98 // reselected automatically, if a parent window loses the focus and regains
100 wxWindow
* const focus
= FindFocus();
101 wxTopLevelWindow
* const
102 tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
103 wxCHECK_RET( tlw
, wxT("radio button outside of TLW?") );
104 wxWindow
* const focusInTLW
= tlw
->GetLastFocus();
106 const wxWindowList
& siblings
= GetParent()->GetChildren();
107 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(this);
108 wxCHECK_RET( nodeThis
, wxT("radio button not a child of its parent?") );
110 // this will be set to true in the code below if the focus is in our TLW
111 // and belongs to one of the other buttons in the same group
112 bool shouldSetFocus
= false;
114 // this will be set to true if the focus is outside of our TLW currently
115 // but the remembered focus of this TLW is one of the other buttons in the
117 bool shouldSetTLWFocus
= false;
119 // if it's not the first item of the group ...
120 if ( !HasFlag(wxRB_GROUP
) )
122 // ... turn off all radio buttons before it
123 for ( wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
125 nodeBefore
= nodeBefore
->GetPrevious() )
127 wxRadioButton
*btn
= wxDynamicCast(nodeBefore
->GetData(),
131 // don't stop on non radio buttons, we could have intermixed
132 // buttons and e.g. static labels
136 if ( btn
->HasFlag(wxRB_SINGLE
) )
138 // A wxRB_SINGLE button isn't part of this group
143 shouldSetFocus
= true;
144 else if ( btn
== focusInTLW
)
145 shouldSetTLWFocus
= true;
147 btn
->SetValue(false);
149 if ( btn
->HasFlag(wxRB_GROUP
) )
151 // even if there are other radio buttons before this one,
152 // they're not in the same group with us
158 // ... and also turn off all buttons after this one
159 for ( wxWindowList::compatibility_iterator nodeAfter
= nodeThis
->GetNext();
161 nodeAfter
= nodeAfter
->GetNext() )
163 wxRadioButton
*btn
= wxDynamicCast(nodeAfter
->GetData(),
169 if ( btn
->HasFlag(wxRB_GROUP
| wxRB_SINGLE
) )
171 // no more buttons or the first button of the next group
176 shouldSetFocus
= true;
177 else if ( btn
== focusInTLW
)
178 shouldSetTLWFocus
= true;
180 btn
->SetValue(false);
183 if ( shouldSetFocus
)
185 else if ( shouldSetTLWFocus
)
186 tlw
->SetLastFocus(this);
189 bool wxRadioButton::GetValue() const
191 wxASSERT_MSG( m_isChecked
==
192 (::SendMessage(GetHwnd(), BM_GETCHECK
, 0, 0L) != 0),
193 wxT("wxRadioButton::m_isChecked is out of sync?") );
198 // ----------------------------------------------------------------------------
199 // wxRadioButton event processing
200 // ----------------------------------------------------------------------------
202 void wxRadioButton::Command (wxCommandEvent
& event
)
204 SetValue(event
.GetInt() != 0);
205 ProcessCommand(event
);
208 bool wxRadioButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
210 if ( param
!= BN_CLICKED
)
215 // we need to manually update the button state as we use BS_RADIOBUTTON
216 // and not BS_AUTORADIOBUTTON
219 wxCommandEvent
event(wxEVT_RADIOBUTTON
, GetId());
220 event
.SetEventObject( this );
221 event
.SetInt(true); // always checked
223 ProcessCommand(event
);
229 // ----------------------------------------------------------------------------
230 // wxRadioButton geometry
231 // ----------------------------------------------------------------------------
233 wxSize
wxRadioButton::DoGetBestSize() const
235 static int s_radioSize
= 0;
240 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
242 s_radioSize
= dc
.GetCharHeight();
244 // radio button bitmap size under CE is bigger than the font height,
245 // adding just one pixel seems to work fine for the default font but it
246 // would be nice to find some better way to find the correct height
249 #endif // __WXWINCE__
252 wxString str
= GetLabel();
257 GetTextExtent(GetLabelText(str
), &wRadio
, &hRadio
);
258 wRadio
+= s_radioSize
+ GetCharWidth();
260 if ( hRadio
< s_radioSize
)
261 hRadio
= s_radioSize
;
265 wRadio
= s_radioSize
;
266 hRadio
= s_radioSize
;
269 wxSize
best(wRadio
, hRadio
);
274 WXDWORD
wxRadioButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
276 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
278 if ( HasFlag(wxRB_GROUP
) )
281 // we use BS_RADIOBUTTON and not BS_AUTORADIOBUTTON because the use of the
282 // latter can easily result in the application entering an infinite loop
283 // inside IsDialogMessage()
285 // we used to use BS_RADIOBUTTON only for wxRB_SINGLE buttons but there
286 // doesn't seem to be any harm to always use it and it prevents some hangs,
288 msStyle
|= BS_RADIOBUTTON
;
290 if ( style
& wxCLIP_SIBLINGS
)
291 msStyle
|= WS_CLIPSIBLINGS
;
292 if ( style
& wxALIGN_RIGHT
)
293 msStyle
|= BS_LEFTTEXT
| BS_RIGHT
;
299 #endif // wxUSE_RADIOBTN