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"
37 #include "wx/dcscreen.h"
40 #include "wx/msw/private.h"
42 // ============================================================================
43 // wxRadioButton implementation
44 // ============================================================================
46 // ----------------------------------------------------------------------------
47 // wxRadioButton creation
48 // ----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
, wxControl
)
52 void wxRadioButton::Init()
54 m_focusJustSet
= FALSE
;
57 bool wxRadioButton::Create(wxWindow
*parent
,
59 const wxString
& label
,
63 const wxValidator
& validator
,
66 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
69 long msStyle
= HasFlag(wxRB_GROUP
) ? WS_GROUP
: 0;
71 msStyle
|= BS_AUTORADIOBUTTON
;
73 if ( HasFlag(wxCLIP_SIBLINGS
) )
74 msStyle
|= WS_CLIPSIBLINGS
;
76 if ( !MSWCreateControl(_T("BUTTON"), msStyle
, pos
, size
, label
, 0) )
79 // for compatibility with wxGTK, the first radio button in a group is
80 // always checked (this makes sense anyhow as you need to ensure that at
81 // least one button in the group is checked and this is the simlpest way to
83 if ( HasFlag(wxRB_GROUP
) )
89 // ----------------------------------------------------------------------------
90 // wxRadioButton functions
91 // ----------------------------------------------------------------------------
93 void wxRadioButton::SetValue(bool value
)
95 // BST_CHECKED is defined as 1, BST_UNCHECKED as 0, so we can just pass
96 // value as is (we don't sue BST_XXX here as they're not defined for Win16)
97 (void)::SendMessage(GetHwnd(), BM_SETCHECK
, (WPARAM
)value
, 0L);
100 bool wxRadioButton::GetValue() const
102 // NB: this will also return TRUE for BST_INDETERMINATE value if we ever
103 // have 3-state radio buttons
104 return ::SendMessage(GetHwnd(), BM_GETCHECK
, 0, 0L) != 0;
107 // ----------------------------------------------------------------------------
108 // wxRadioButton event processing
109 // ----------------------------------------------------------------------------
111 void wxRadioButton::Command (wxCommandEvent
& event
)
113 SetValue(event
.m_commandInt
!= 0);
114 ProcessCommand(event
);
117 void wxRadioButton::SetFocus()
119 // when the radio button receives a WM_SETFOCUS message it generates a
120 // BN_CLICKED which is totally unexpected and leads to catastrophic results
121 // if you pop up a dialog from the radio button event handler as, when the
122 // dialog is dismissed, the focus is returned to the radio button which
123 // generates BN_CLICKED which leads to showing another dialog and so on
126 // to aviod this, we drop the pseudo BN_CLICKED events generated when the
127 // button gains focus
128 m_focusJustSet
= TRUE
;
130 wxControl::SetFocus();
133 bool wxRadioButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
135 if ( param
!= BN_CLICKED
)
138 if ( m_focusJustSet
)
140 // see above: we want to ignore this event
141 m_focusJustSet
= FALSE
;
143 else // a real clicked event
145 wxCommandEvent
event(wxEVT_COMMAND_RADIOBUTTON_SELECTED
, GetId());
146 event
.SetEventObject( this );
147 event
.SetInt( GetValue() );
149 ProcessCommand(event
);
155 // ----------------------------------------------------------------------------
156 // wxRadioButton geometry
157 // ----------------------------------------------------------------------------
159 wxSize
wxRadioButton::DoGetBestSize() const
161 static int s_radioSize
= 0;
166 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
168 s_radioSize
= dc
.GetCharHeight();
171 wxString str
= GetLabel();
176 GetTextExtent(str
, &wRadio
, &hRadio
);
177 wRadio
+= s_radioSize
+ GetCharWidth();
179 if ( hRadio
< s_radioSize
)
180 hRadio
= s_radioSize
;
184 wRadio
= s_radioSize
;
185 hRadio
= s_radioSize
;
188 return wxSize(wRadio
, hRadio
);
191 long wxRadioButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
193 if (nMsg
== WM_SETFOCUS
)
195 m_focusJustSet
= TRUE
;
197 long ret
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
199 m_focusJustSet
= FALSE
;
203 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
206 #endif // wxUSE_RADIOBTN