]> git.saurik.com Git - wxWidgets.git/blob - src/msw/radiobut.cpp
don't generate clicked events when we just get the focus
[wxWidgets.git] / src / msw / radiobut.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/radiobut.cpp
3 // Purpose: wxRadioButton
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "radiobut.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_RADIOBTN
32
33 #ifndef WX_PRECOMP
34 #include "wx/radiobut.h"
35 #include "wx/settings.h"
36 #include "wx/brush.h"
37 #endif
38
39 #include "wx/msw/private.h"
40
41 // ============================================================================
42 // wxRadioButton implementation
43 // ============================================================================
44
45 // ----------------------------------------------------------------------------
46 // wxRadioButton creation
47 // ----------------------------------------------------------------------------
48
49 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
50
51 void wxRadioButton::Init()
52 {
53 m_focusJustSet = FALSE;
54 }
55
56 bool wxRadioButton::Create(wxWindow *parent,
57 wxWindowID id,
58 const wxString& label,
59 const wxPoint& pos,
60 const wxSize& size,
61 long style,
62 const wxValidator& validator,
63 const wxString& name)
64 {
65 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
66 return FALSE;
67
68 long msStyle = HasFlag(wxRB_GROUP) ? WS_GROUP : 0;
69
70 msStyle |= BS_AUTORADIOBUTTON;
71
72 if ( HasFlag(wxCLIP_SIBLINGS) )
73 msStyle |= WS_CLIPSIBLINGS;
74
75 if ( !MSWCreateControl(_T("BUTTON"), msStyle, pos, size, label, 0) )
76 return FALSE;
77
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
81 // do it)
82 if ( HasFlag(wxRB_GROUP) )
83 SetValue(TRUE);
84
85 return TRUE;
86 }
87
88 // ----------------------------------------------------------------------------
89 // wxRadioButton functions
90 // ----------------------------------------------------------------------------
91
92 void wxRadioButton::SetValue(bool value)
93 {
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);
97 }
98
99 bool wxRadioButton::GetValue() const
100 {
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;
104 }
105
106 // ----------------------------------------------------------------------------
107 // wxRadioButton event processing
108 // ----------------------------------------------------------------------------
109
110 void wxRadioButton::Command (wxCommandEvent& event)
111 {
112 SetValue(event.m_commandInt != 0);
113 ProcessCommand(event);
114 }
115
116 void wxRadioButton::SetFocus()
117 {
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
123 // without end!
124 //
125 // to aviod this, we drop the pseudo BN_CLICKED events generated when the
126 // button gains focus
127 m_focusJustSet = TRUE;
128
129 wxControl::SetFocus();
130 }
131
132 bool wxRadioButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
133 {
134 if ( param != BN_CLICKED )
135 return FALSE;
136
137 if ( m_focusJustSet )
138 {
139 // see above: we want to ignore this event
140 m_focusJustSet = FALSE;
141 }
142 else // a real clicked event
143 {
144 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, GetId());
145 event.SetEventObject( this );
146 event.SetInt( GetValue() );
147
148 ProcessCommand(event);
149 }
150
151 return TRUE;
152 }
153
154 // ----------------------------------------------------------------------------
155 // wxRadioButton geometry
156 // ----------------------------------------------------------------------------
157
158 wxSize wxRadioButton::DoGetBestSize() const
159 {
160 static int s_radioSize = 0;
161
162 if ( !s_radioSize )
163 {
164 wxScreenDC dc;
165 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
166
167 s_radioSize = dc.GetCharHeight();
168 }
169
170 wxString str = GetLabel();
171
172 int wRadio, hRadio;
173 if ( !str.empty() )
174 {
175 GetTextExtent(str, &wRadio, &hRadio);
176 wRadio += s_radioSize + GetCharWidth();
177
178 if ( hRadio < s_radioSize )
179 hRadio = s_radioSize;
180 }
181 else
182 {
183 wRadio = s_radioSize;
184 hRadio = s_radioSize;
185 }
186
187 return wxSize(wRadio, hRadio);
188 }
189
190 #endif // wxUSE_RADIOBTN