1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "radiobut.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/radiobut.h"
35 #include "wx/settings.h"
36 #include "wx/dcscreen.h"
39 #include "wx/msw/private.h"
41 // ============================================================================
42 // wxRadioButton implementation
43 // ============================================================================
45 // ----------------------------------------------------------------------------
46 // wxRadioButton creation
47 // ----------------------------------------------------------------------------
50 #if wxUSE_EXTENDED_RTTI
51 WX_DEFINE_FLAGS( wxRadioButtonStyle
)
53 wxBEGIN_FLAGS( wxRadioButtonStyle
)
54 // new style border flags, we put them first to
55 // use them for streaming out
56 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
57 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
58 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
59 wxFLAGS_MEMBER(wxBORDER_RAISED
)
60 wxFLAGS_MEMBER(wxBORDER_STATIC
)
61 wxFLAGS_MEMBER(wxBORDER_NONE
)
63 // old style border flags
64 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
65 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
66 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
67 wxFLAGS_MEMBER(wxRAISED_BORDER
)
68 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
69 wxFLAGS_MEMBER(wxBORDER
)
71 // standard window styles
72 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
73 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
74 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
75 wxFLAGS_MEMBER(wxWANTS_CHARS
)
76 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
77 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
78 wxFLAGS_MEMBER(wxVSCROLL
)
79 wxFLAGS_MEMBER(wxHSCROLL
)
81 wxFLAGS_MEMBER(wxRB_GROUP
)
83 wxEND_FLAGS( wxRadioButtonStyle
)
85 IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioButton
, wxControl
,"wx/radiobut.h")
87 wxBEGIN_PROPERTIES_TABLE(wxRadioButton
)
88 wxEVENT_PROPERTY( Click
, wxEVT_COMMAND_RADIOBUTTON_SELECTED
, wxCommandEvent
)
89 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
90 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
91 wxPROPERTY( Value
,bool, SetValue
, GetValue
,, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
92 wxPROPERTY_FLAGS( WindowStyle
, wxRadioButtonStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
93 wxEND_PROPERTIES_TABLE()
95 wxBEGIN_HANDLERS_TABLE(wxRadioButton
)
96 wxEND_HANDLERS_TABLE()
98 wxCONSTRUCTOR_6( wxRadioButton
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
101 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
, wxControl
)
105 void wxRadioButton::Init()
110 bool wxRadioButton::Create(wxWindow
*parent
,
112 const wxString
& label
,
116 const wxValidator
& validator
,
117 const wxString
& name
)
119 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
122 long msStyle
= WS_TABSTOP
;
123 if ( HasFlag(wxRB_GROUP
) )
127 wxRB_SINGLE is a temporary workaround for the following problem: if you
128 have 2 radiobuttons in the same group but which are not consecutive in
129 the dialog, Windows can enter an infinite loop! The simplest way to
130 reproduce it is to create radio button, then a panel and then another
131 radio button: then checking the last button hangs the app.
133 Ideally, we'd detect (and avoid) such situation automatically but for
134 now, as I don't know how to do it, just allow the user to create
135 BS_RADIOBUTTON buttons for such situations.
137 msStyle
|= HasFlag(wxRB_SINGLE
) ? BS_RADIOBUTTON
: BS_AUTORADIOBUTTON
;
139 if ( HasFlag(wxCLIP_SIBLINGS
) )
140 msStyle
|= WS_CLIPSIBLINGS
;
142 if ( !MSWCreateControl(_T("BUTTON"), msStyle
, pos
, size
, label
, 0) )
145 // for compatibility with wxGTK, the first radio button in a group is
146 // always checked (this makes sense anyhow as you need to ensure that at
147 // least one button in the group is checked and this is the simlpest way to
149 if ( HasFlag(wxRB_GROUP
) )
155 // ----------------------------------------------------------------------------
156 // wxRadioButton functions
157 // ----------------------------------------------------------------------------
159 void wxRadioButton::SetValue(bool value
)
161 // BST_CHECKED is defined as 1, BST_UNCHECKED as 0, so we can just pass
162 // value as is (we don't use BST_XXX here as they're not defined for Win16)
163 (void)::SendMessage(GetHwnd(), BM_SETCHECK
, (WPARAM
)value
, 0L);
167 // if we set the value of one radio button we also must clear all the other
168 // buttons in the same group: Windows doesn't do it automatically
171 const wxWindowList
& siblings
= GetParent()->GetChildren();
172 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(this);
173 wxCHECK_RET( nodeThis
, _T("radio button not a child of its parent?") );
175 // if it's not the first item of the group ...
176 if ( !HasFlag(wxRB_GROUP
) )
178 // ... turn off all radio buttons before it
179 for ( wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
181 nodeBefore
= nodeBefore
->GetPrevious() )
183 wxRadioButton
*btn
= wxDynamicCast(nodeBefore
->GetData(),
187 // the radio buttons in a group must be consecutive, so
188 // there are no more of them
192 btn
->SetValue(false);
194 if ( btn
->HasFlag(wxRB_GROUP
) )
196 // even if there are other radio buttons before this one,
197 // they're not in the same group with us
203 // ... and also turn off all buttons after this one
204 for ( wxWindowList::compatibility_iterator nodeAfter
= nodeThis
->GetNext();
206 nodeAfter
= nodeAfter
->GetNext() )
208 wxRadioButton
*btn
= wxDynamicCast(nodeAfter
->GetData(),
211 if ( !btn
|| btn
->HasFlag(wxRB_GROUP
) )
213 // no more buttons or the first button of the next group
217 btn
->SetValue(false);
222 bool wxRadioButton::GetValue() const
224 wxASSERT_MSG( m_isChecked
==
225 (::SendMessage(GetHwnd(), BM_GETCHECK
, 0, 0L) != 0),
226 _T("wxRadioButton::m_isChecked is out of sync?") );
231 // ----------------------------------------------------------------------------
232 // wxRadioButton event processing
233 // ----------------------------------------------------------------------------
235 void wxRadioButton::Command (wxCommandEvent
& event
)
237 SetValue(event
.m_commandInt
!= 0);
238 ProcessCommand(event
);
241 bool wxRadioButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
243 if ( param
!= BN_CLICKED
)
248 // we have to do this for BS_RADIOBUTTON anyhow and, strangely enough,
249 // sometimes this is needed even for BS_AUTORADIOBUTTON (when we
250 // receive focus the button gets BN_CLICKED but stays unchecked!)
253 wxCommandEvent
event(wxEVT_COMMAND_RADIOBUTTON_SELECTED
, GetId());
254 event
.SetEventObject( this );
255 event
.SetInt(true); // always checked
257 ProcessCommand(event
);
263 // ----------------------------------------------------------------------------
264 // wxRadioButton geometry
265 // ----------------------------------------------------------------------------
267 wxSize
wxRadioButton::DoGetBestSize() const
269 static int s_radioSize
= 0;
274 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
276 s_radioSize
= dc
.GetCharHeight();
279 wxString str
= GetLabel();
284 GetTextExtent(str
, &wRadio
, &hRadio
);
285 wRadio
+= s_radioSize
+ GetCharWidth();
287 if ( hRadio
< s_radioSize
)
288 hRadio
= s_radioSize
;
292 wRadio
= s_radioSize
;
293 hRadio
= s_radioSize
;
296 return wxSize(wRadio
, hRadio
);
299 #endif // wxUSE_RADIOBTN