]> git.saurik.com Git - wxWidgets.git/blob - src/msw/radiobut.cpp
Hack compilation using the latest MinGW release.
[wxWidgets.git] / src / msw / radiobut.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/radiobut.cpp
3 // Purpose: wxRadioButton
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_RADIOBTN
27
28 #include "wx/radiobut.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/settings.h"
32 #include "wx/dcscreen.h"
33 #include "wx/toplevel.h"
34 #endif
35
36 #include "wx/msw/private.h"
37
38 // ============================================================================
39 // wxRadioButton implementation
40 // ============================================================================
41
42 // ----------------------------------------------------------------------------
43 // wxRadioButton creation
44 // ----------------------------------------------------------------------------
45
46 void wxRadioButton::Init()
47 {
48 m_isChecked = false;
49 }
50
51 bool wxRadioButton::Create(wxWindow *parent,
52 wxWindowID id,
53 const wxString& label,
54 const wxPoint& pos,
55 const wxSize& size,
56 long style,
57 const wxValidator& validator,
58 const wxString& name)
59 {
60 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
61 return false;
62
63 WXDWORD exstyle = 0;
64 WXDWORD msStyle = MSWGetStyle(style, &exstyle);
65
66 if ( !MSWCreateControl(wxT("BUTTON"), msStyle, pos, size, label, exstyle) )
67 return false;
68
69 // for compatibility with wxGTK, the first radio button in a group is
70 // always checked (this makes sense anyhow as you need to ensure that at
71 // least one button in the group is checked and this is the simplest way to
72 // do it)
73 if ( HasFlag(wxRB_GROUP) )
74 SetValue(true);
75
76 return true;
77 }
78
79 // ----------------------------------------------------------------------------
80 // wxRadioButton functions
81 // ----------------------------------------------------------------------------
82
83 void wxRadioButton::SetValue(bool value)
84 {
85 ::SendMessage(GetHwnd(), BM_SETCHECK,
86 value ? BST_CHECKED : BST_UNCHECKED, 0);
87
88 m_isChecked = value;
89
90 if ( !value )
91 return;
92
93 // if we set the value of one radio button we also must clear all the other
94 // buttons in the same group: Windows doesn't do it automatically
95 //
96 // moreover, if another radiobutton in the group currently has the focus,
97 // we have to set it to this radiobutton, else the old radiobutton will be
98 // reselected automatically, if a parent window loses the focus and regains
99 // it.
100 wxWindow * const focus = FindFocus();
101 wxTopLevelWindow * const
102 tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
103 wxCHECK_RET( tlw, wxT("radio button outside of TLW?") );
104 wxWindow * const focusInTLW = tlw->GetLastFocus();
105
106 const wxWindowList& siblings = GetParent()->GetChildren();
107 wxWindowList::compatibility_iterator nodeThis = siblings.Find(this);
108 wxCHECK_RET( nodeThis, wxT("radio button not a child of its parent?") );
109
110 // this will be set to true in the code below if the focus is in our TLW
111 // and belongs to one of the other buttons in the same group
112 bool shouldSetFocus = false;
113
114 // this will be set to true if the focus is outside of our TLW currently
115 // but the remembered focus of this TLW is one of the other buttons in the
116 // same group
117 bool shouldSetTLWFocus = false;
118
119 // if it's not the first item of the group ...
120 if ( !HasFlag(wxRB_GROUP) )
121 {
122 // ... turn off all radio buttons before it
123 for ( wxWindowList::compatibility_iterator nodeBefore = nodeThis->GetPrevious();
124 nodeBefore;
125 nodeBefore = nodeBefore->GetPrevious() )
126 {
127 wxRadioButton *btn = wxDynamicCast(nodeBefore->GetData(),
128 wxRadioButton);
129 if ( !btn )
130 {
131 // don't stop on non radio buttons, we could have intermixed
132 // buttons and e.g. static labels
133 continue;
134 }
135
136 if ( btn->HasFlag(wxRB_SINGLE) )
137 {
138 // A wxRB_SINGLE button isn't part of this group
139 break;
140 }
141
142 if ( btn == focus )
143 shouldSetFocus = true;
144 else if ( btn == focusInTLW )
145 shouldSetTLWFocus = true;
146
147 btn->SetValue(false);
148
149 if ( btn->HasFlag(wxRB_GROUP) )
150 {
151 // even if there are other radio buttons before this one,
152 // they're not in the same group with us
153 break;
154 }
155 }
156 }
157
158 // ... and also turn off all buttons after this one
159 for ( wxWindowList::compatibility_iterator nodeAfter = nodeThis->GetNext();
160 nodeAfter;
161 nodeAfter = nodeAfter->GetNext() )
162 {
163 wxRadioButton *btn = wxDynamicCast(nodeAfter->GetData(),
164 wxRadioButton);
165
166 if ( !btn )
167 continue;
168
169 if ( btn->HasFlag(wxRB_GROUP | wxRB_SINGLE) )
170 {
171 // no more buttons or the first button of the next group
172 break;
173 }
174
175 if ( btn == focus )
176 shouldSetFocus = true;
177 else if ( btn == focusInTLW )
178 shouldSetTLWFocus = true;
179
180 btn->SetValue(false);
181 }
182
183 if ( shouldSetFocus )
184 SetFocus();
185 else if ( shouldSetTLWFocus )
186 tlw->SetLastFocus(this);
187 }
188
189 bool wxRadioButton::GetValue() const
190 {
191 wxASSERT_MSG( m_isChecked ==
192 (::SendMessage(GetHwnd(), BM_GETCHECK, 0, 0L) != 0),
193 wxT("wxRadioButton::m_isChecked is out of sync?") );
194
195 return m_isChecked;
196 }
197
198 // ----------------------------------------------------------------------------
199 // wxRadioButton event processing
200 // ----------------------------------------------------------------------------
201
202 void wxRadioButton::Command (wxCommandEvent& event)
203 {
204 SetValue(event.GetInt() != 0);
205 ProcessCommand(event);
206 }
207
208 bool wxRadioButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
209 {
210 if ( param != BN_CLICKED )
211 return false;
212
213 if ( !m_isChecked )
214 {
215 // we need to manually update the button state as we use BS_RADIOBUTTON
216 // and not BS_AUTORADIOBUTTON
217 SetValue(true);
218
219 wxCommandEvent event(wxEVT_RADIOBUTTON, GetId());
220 event.SetEventObject( this );
221 event.SetInt(true); // always checked
222
223 ProcessCommand(event);
224 }
225
226 return true;
227 }
228
229 // ----------------------------------------------------------------------------
230 // wxRadioButton geometry
231 // ----------------------------------------------------------------------------
232
233 wxSize wxRadioButton::DoGetBestSize() const
234 {
235 static int s_radioSize = 0;
236
237 if ( !s_radioSize )
238 {
239 wxScreenDC dc;
240 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
241
242 s_radioSize = dc.GetCharHeight();
243
244 // radio button bitmap size under CE is bigger than the font height,
245 // adding just one pixel seems to work fine for the default font but it
246 // would be nice to find some better way to find the correct height
247 #ifdef __WXWINCE__
248 s_radioSize++;
249 #endif // __WXWINCE__
250 }
251
252 wxString str = GetLabel();
253
254 int wRadio, hRadio;
255 if ( !str.empty() )
256 {
257 GetTextExtent(GetLabelText(str), &wRadio, &hRadio);
258 wRadio += s_radioSize + GetCharWidth();
259
260 if ( hRadio < s_radioSize )
261 hRadio = s_radioSize;
262 }
263 else
264 {
265 wRadio = s_radioSize;
266 hRadio = s_radioSize;
267 }
268
269 wxSize best(wRadio, hRadio);
270 CacheBestSize(best);
271 return best;
272 }
273
274 WXDWORD wxRadioButton::MSWGetStyle(long style, WXDWORD *exstyle) const
275 {
276 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
277
278 if ( HasFlag(wxRB_GROUP) )
279 msStyle |= WS_GROUP;
280
281 // we use BS_RADIOBUTTON and not BS_AUTORADIOBUTTON because the use of the
282 // latter can easily result in the application entering an infinite loop
283 // inside IsDialogMessage()
284 //
285 // we used to use BS_RADIOBUTTON only for wxRB_SINGLE buttons but there
286 // doesn't seem to be any harm to always use it and it prevents some hangs,
287 // see #9786
288 msStyle |= BS_RADIOBUTTON;
289
290 if ( style & wxCLIP_SIBLINGS )
291 msStyle |= WS_CLIPSIBLINGS;
292 if ( style & wxALIGN_RIGHT )
293 msStyle |= BS_LEFTTEXT | BS_RIGHT;
294
295
296 return msStyle;
297 }
298
299 #endif // wxUSE_RADIOBTN