1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/radiobut.cpp
3 // Purpose: wxRadioButton
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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"
39 #include "wx/msw/private.h"
41 // ============================================================================
42 // wxRadioButton implementation
43 // ============================================================================
45 // ----------------------------------------------------------------------------
46 // wxRadioButton creation
47 // ----------------------------------------------------------------------------
49 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
, wxControl
)
51 void wxRadioButton::Init()
53 m_focusJustSet
= FALSE
;
56 bool wxRadioButton::Create(wxWindow
*parent
,
58 const wxString
& label
,
62 const wxValidator
& validator
,
65 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
68 long msStyle
= HasFlag(wxRB_GROUP
) ? WS_GROUP
: 0;
70 msStyle
|= BS_AUTORADIOBUTTON
;
72 if ( HasFlag(wxCLIP_SIBLINGS
) )
73 msStyle
|= WS_CLIPSIBLINGS
;
75 if ( !MSWCreateControl(_T("BUTTON"), msStyle
, pos
, size
, label
, 0) )
78 // for compatibility with wxGTK, the first radio button in a group is
79 // always checked (this makes sense anyhow as you need to ensure that at
80 // least one button in the group is checked and this is the simlpest way to
82 if ( HasFlag(wxRB_GROUP
) )
88 // ----------------------------------------------------------------------------
89 // wxRadioButton functions
90 // ----------------------------------------------------------------------------
92 void wxRadioButton::SetValue(bool value
)
94 // BST_CHECKED is defined as 1, BST_UNCHECKED as 0, so we can just pass
95 // value as is (we don't sue BST_XXX here as they're not defined for Win16)
96 (void)::SendMessage(GetHwnd(), BM_SETCHECK
, (WPARAM
)value
, 0L);
99 bool wxRadioButton::GetValue() const
101 // NB: this will also return TRUE for BST_INDETERMINATE value if we ever
102 // have 3-state radio buttons
103 return ::SendMessage(GetHwnd(), BM_GETCHECK
, 0, 0L) != 0;
106 // ----------------------------------------------------------------------------
107 // wxRadioButton event processing
108 // ----------------------------------------------------------------------------
110 void wxRadioButton::Command (wxCommandEvent
& event
)
112 SetValue(event
.m_commandInt
!= 0);
113 ProcessCommand(event
);
116 void wxRadioButton::SetFocus()
118 // when the radio button receives a WM_SETFOCUS message it generates a
119 // BN_CLICKED which is totally unexpected and leads to catastrophic results
120 // if you pop up a dialog from the radio button event handler as, when the
121 // dialog is dismissed, the focus is returned to the radio button which
122 // generates BN_CLICKED which leads to showing another dialog and so on
125 // to aviod this, we drop the pseudo BN_CLICKED events generated when the
126 // button gains focus
127 m_focusJustSet
= TRUE
;
129 wxControl::SetFocus();
132 bool wxRadioButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
134 if ( param
!= BN_CLICKED
)
137 if ( m_focusJustSet
)
139 // see above: we want to ignore this event
140 m_focusJustSet
= FALSE
;
142 else // a real clicked event
144 wxCommandEvent
event(wxEVT_COMMAND_RADIOBUTTON_SELECTED
, GetId());
145 event
.SetEventObject( this );
146 event
.SetInt( GetValue() );
148 ProcessCommand(event
);
154 // ----------------------------------------------------------------------------
155 // wxRadioButton geometry
156 // ----------------------------------------------------------------------------
158 wxSize
wxRadioButton::DoGetBestSize() const
160 static int s_radioSize
= 0;
165 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
167 s_radioSize
= dc
.GetCharHeight();
170 wxString str
= GetLabel();
175 GetTextExtent(str
, &wRadio
, &hRadio
);
176 wRadio
+= s_radioSize
+ GetCharWidth();
178 if ( hRadio
< s_radioSize
)
179 hRadio
= s_radioSize
;
183 wRadio
= s_radioSize
;
184 hRadio
= s_radioSize
;
187 return wxSize(wRadio
, hRadio
);
190 #endif // wxUSE_RADIOBTN