1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/radiobut.cpp
3 // Purpose: wxRadioButton
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/radiobut.h"
32 #include "wx/settings.h"
33 #include "wx/dcscreen.h"
34 #include "wx/toplevel.h"
37 #include "wx/msw/private.h"
39 // ============================================================================
40 // wxRadioButton implementation
41 // ============================================================================
43 // ----------------------------------------------------------------------------
44 // wxRadioButton creation
45 // ----------------------------------------------------------------------------
47 void wxRadioButton::Init()
52 bool wxRadioButton::Create(wxWindow
*parent
,
54 const wxString
& label
,
58 const wxValidator
& validator
,
61 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
65 WXDWORD msStyle
= MSWGetStyle(style
, &exstyle
);
67 if ( !MSWCreateControl(wxT("BUTTON"), msStyle
, pos
, size
, label
, exstyle
) )
70 // for compatibility with wxGTK, the first radio button in a group is
71 // always checked (this makes sense anyhow as you need to ensure that at
72 // least one button in the group is checked and this is the simplest way to
74 if ( HasFlag(wxRB_GROUP
) )
80 // ----------------------------------------------------------------------------
81 // wxRadioButton functions
82 // ----------------------------------------------------------------------------
84 void wxRadioButton::SetValue(bool value
)
86 ::SendMessage(GetHwnd(), BM_SETCHECK
,
87 value
? BST_CHECKED
: BST_UNCHECKED
, 0);
94 // if we set the value of one radio button we also must clear all the other
95 // buttons in the same group: Windows doesn't do it automatically
97 // moreover, if another radiobutton in the group currently has the focus,
98 // we have to set it to this radiobutton, else the old radiobutton will be
99 // reselected automatically, if a parent window loses the focus and regains
101 wxWindow
* const focus
= FindFocus();
102 wxTopLevelWindow
* const
103 tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
104 wxCHECK_RET( tlw
, wxT("radio button outside of TLW?") );
105 wxWindow
* const focusInTLW
= tlw
->GetLastFocus();
107 const wxWindowList
& siblings
= GetParent()->GetChildren();
108 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(this);
109 wxCHECK_RET( nodeThis
, wxT("radio button not a child of its parent?") );
111 // this will be set to true in the code below if the focus is in our TLW
112 // and belongs to one of the other buttons in the same group
113 bool shouldSetFocus
= false;
115 // this will be set to true if the focus is outside of our TLW currently
116 // but the remembered focus of this TLW is one of the other buttons in the
118 bool shouldSetTLWFocus
= false;
120 // if it's not the first item of the group ...
121 if ( !HasFlag(wxRB_GROUP
) )
123 // ... turn off all radio buttons before it
124 for ( wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
126 nodeBefore
= nodeBefore
->GetPrevious() )
128 wxRadioButton
*btn
= wxDynamicCast(nodeBefore
->GetData(),
132 // don't stop on non radio buttons, we could have intermixed
133 // buttons and e.g. static labels
137 if ( btn
->HasFlag(wxRB_SINGLE
) )
139 // A wxRB_SINGLE button isn't part of this group
144 shouldSetFocus
= true;
145 else if ( btn
== focusInTLW
)
146 shouldSetTLWFocus
= true;
148 btn
->SetValue(false);
150 if ( btn
->HasFlag(wxRB_GROUP
) )
152 // even if there are other radio buttons before this one,
153 // they're not in the same group with us
159 // ... and also turn off all buttons after this one
160 for ( wxWindowList::compatibility_iterator nodeAfter
= nodeThis
->GetNext();
162 nodeAfter
= nodeAfter
->GetNext() )
164 wxRadioButton
*btn
= wxDynamicCast(nodeAfter
->GetData(),
170 if ( btn
->HasFlag(wxRB_GROUP
| wxRB_SINGLE
) )
172 // no more buttons or the first button of the next group
177 shouldSetFocus
= true;
178 else if ( btn
== focusInTLW
)
179 shouldSetTLWFocus
= true;
181 btn
->SetValue(false);
184 if ( shouldSetFocus
)
186 else if ( shouldSetTLWFocus
)
187 tlw
->SetLastFocus(this);
190 bool wxRadioButton::GetValue() const
192 wxASSERT_MSG( m_isChecked
==
193 (::SendMessage(GetHwnd(), BM_GETCHECK
, 0, 0L) != 0),
194 wxT("wxRadioButton::m_isChecked is out of sync?") );
199 // ----------------------------------------------------------------------------
200 // wxRadioButton event processing
201 // ----------------------------------------------------------------------------
203 void wxRadioButton::Command (wxCommandEvent
& event
)
205 SetValue(event
.GetInt() != 0);
206 ProcessCommand(event
);
209 bool wxRadioButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
211 if ( param
!= BN_CLICKED
)
216 // we need to manually update the button state as we use BS_RADIOBUTTON
217 // and not BS_AUTORADIOBUTTON
220 wxCommandEvent
event(wxEVT_RADIOBUTTON
, GetId());
221 event
.SetEventObject( this );
222 event
.SetInt(true); // always checked
224 ProcessCommand(event
);
230 // ----------------------------------------------------------------------------
231 // wxRadioButton geometry
232 // ----------------------------------------------------------------------------
234 wxSize
wxRadioButton::DoGetBestSize() const
236 static int s_radioSize
= 0;
241 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
243 s_radioSize
= dc
.GetCharHeight();
245 // radio button bitmap size under CE is bigger than the font height,
246 // adding just one pixel seems to work fine for the default font but it
247 // would be nice to find some better way to find the correct height
250 #endif // __WXWINCE__
253 wxString str
= GetLabel();
258 GetTextExtent(GetLabelText(str
), &wRadio
, &hRadio
);
259 wRadio
+= s_radioSize
+ GetCharWidth();
261 if ( hRadio
< s_radioSize
)
262 hRadio
= s_radioSize
;
266 wRadio
= s_radioSize
;
267 hRadio
= s_radioSize
;
270 wxSize
best(wRadio
, hRadio
);
275 WXDWORD
wxRadioButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
277 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
279 if ( HasFlag(wxRB_GROUP
) )
282 // we use BS_RADIOBUTTON and not BS_AUTORADIOBUTTON because the use of the
283 // latter can easily result in the application entering an infinite loop
284 // inside IsDialogMessage()
286 // we used to use BS_RADIOBUTTON only for wxRB_SINGLE buttons but there
287 // doesn't seem to be any harm to always use it and it prevents some hangs,
289 msStyle
|= BS_RADIOBUTTON
;
291 if ( style
& wxCLIP_SIBLINGS
)
292 msStyle
|= WS_CLIPSIBLINGS
;
293 if ( style
& wxALIGN_RIGHT
)
294 msStyle
|= BS_LEFTTEXT
| BS_RIGHT
;
300 #endif // wxUSE_RADIOBTN