]>
Commit | Line | Data |
---|---|---|
0e320a79 DW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: radiobut.cpp | |
3 | // Purpose: wxRadioButton | |
cdf1e714 | 4 | // Author: David Webster |
0e320a79 | 5 | // Modified by: |
cdf1e714 | 6 | // Created: 10/12/99 |
0e320a79 | 7 | // RCS-ID: $Id$ |
cdf1e714 | 8 | // Copyright: (c) David Webster |
65571936 | 9 | // Licence: wxWindows licence |
0e320a79 DW |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
cdf1e714 DW |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
0e320a79 DW |
17 | #endif |
18 | ||
cdf1e714 DW |
19 | #ifndef WX_PRECOMP |
20 | #include <stdio.h> | |
21 | #include "wx/setup.h" | |
0e320a79 | 22 | #include "wx/radiobut.h" |
cdf1e714 | 23 | #include "wx/brush.h" |
a4a16252 SN |
24 | #include "wx/dcscreen.h" |
25 | #include "wx/settings.h" | |
cdf1e714 DW |
26 | #endif |
27 | ||
11e59d47 | 28 | #include "wx/os2/private.h" |
0e320a79 | 29 | |
0e320a79 | 30 | IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl) |
0e320a79 | 31 | |
f289196b DW |
32 | extern void wxAssociateWinWithHandle( HWND hWnd |
33 | ,wxWindowOS2* pWin | |
34 | ); | |
35 | ||
1b086de1 DW |
36 | void wxRadioButton::Init() |
37 | { | |
38 | m_bFocusJustSet = FALSE; | |
39 | } // end of wxRadioButton::Init | |
40 | ||
3c299c3a DW |
41 | void wxRadioButton::Command ( |
42 | wxCommandEvent& rEvent | |
43 | ) | |
cdf1e714 | 44 | { |
3c299c3a DW |
45 | SetValue ((rEvent.GetInt() != 0) ); |
46 | ProcessCommand (rEvent); | |
47 | } // end of wxRadioButton::Command | |
48 | ||
49 | bool wxRadioButton::Create( | |
50 | wxWindow* pParent | |
51 | , wxWindowID vId | |
52 | , const wxString& rsLabel | |
53 | , const wxPoint& rPos | |
54 | , const wxSize& rSize | |
55 | , long lStyle | |
3c299c3a | 56 | , const wxValidator& rValidator |
3c299c3a DW |
57 | , const wxString& rsName |
58 | ) | |
0e320a79 | 59 | { |
1b086de1 DW |
60 | if ( !CreateControl( pParent |
61 | ,vId | |
62 | ,rPos | |
63 | ,rSize | |
64 | ,lStyle | |
1b086de1 | 65 | ,rValidator |
1b086de1 DW |
66 | ,rsName)) |
67 | return FALSE; | |
0e320a79 | 68 | |
2b5f62a0 | 69 | long lSstyle = WS_TABSTOP; |
0e320a79 | 70 | |
2b5f62a0 VZ |
71 | if (HasFlag(wxRB_GROUP)) |
72 | lSstyle |= WS_GROUP; | |
73 | ||
74 | // | |
75 | // wxRB_SINGLE is a temporary workaround for the following problem: if you | |
76 | // have 2 radiobuttons in the same group but which are not consecutive in | |
77 | // the dialog, Windows can enter an infinite loop! The simplest way to | |
78 | // reproduce it is to create radio button, then a panel and then another | |
79 | // radio button: then checking the last button hangs the app. | |
80 | // | |
81 | // Ideally, we'd detect (and avoid) such situation automatically but for | |
82 | // now, as I don't know how to do it, just allow the user to create | |
83 | // BS_RADIOBUTTON buttons for such situations. | |
84 | // | |
85 | lSstyle |= HasFlag(wxRB_SINGLE) ? BS_RADIOBUTTON : BS_AUTORADIOBUTTON; | |
cdf1e714 | 86 | |
1b086de1 DW |
87 | if (HasFlag(wxCLIP_SIBLINGS)) |
88 | lSstyle |= WS_CLIPSIBLINGS; | |
89 | ||
90 | if (!OS2CreateControl( _T("BUTTON") | |
91 | ,lSstyle | |
92 | ,rPos | |
93 | ,rSize | |
94 | ,rsLabel | |
95 | ,0 | |
96 | )) | |
97 | return FALSE; | |
cdf1e714 | 98 | |
f289196b | 99 | wxAssociateWinWithHandle(m_hWnd, this); |
1b086de1 DW |
100 | if (HasFlag(wxRB_GROUP)) |
101 | SetValue(TRUE); | |
0e320a79 | 102 | |
f289196b | 103 | SetFont(*wxSMALL_FONT); |
1b086de1 DW |
104 | SetSize( rPos.x |
105 | ,rPos.y | |
106 | ,rSize.x | |
107 | ,rSize.y | |
108 | ); | |
109 | return TRUE; | |
110 | } // end of wxRadioButton::Create | |
0e320a79 | 111 | |
1b086de1 DW |
112 | wxSize wxRadioButton::DoGetBestSize() const |
113 | { | |
c5f975dd SN |
114 | // We should probably compute snRadioSize but it seems to be a constant |
115 | // independent of its label's font size and not made available by OS/2. | |
116 | static int snRadioSize = RADIO_SIZE; | |
cdf1e714 | 117 | |
c5f975dd | 118 | wxString sStr = wxGetWindowText(GetHwnd()); |
1b086de1 DW |
119 | int nRadioWidth; |
120 | int nRadioHeight; | |
121 | ||
122 | if (!sStr.empty()) | |
cdf1e714 | 123 | { |
1b086de1 DW |
124 | GetTextExtent( sStr |
125 | ,&nRadioWidth | |
126 | ,&nRadioHeight | |
3c299c3a | 127 | ); |
c5f975dd | 128 | nRadioWidth += snRadioSize; |
1b086de1 DW |
129 | if (nRadioHeight < snRadioSize) |
130 | nRadioHeight = snRadioSize; | |
3c299c3a DW |
131 | } |
132 | else | |
133 | { | |
1b086de1 DW |
134 | nRadioWidth = snRadioSize; |
135 | nRadioHeight = snRadioSize; | |
cdf1e714 | 136 | } |
1b086de1 DW |
137 | return wxSize( nRadioWidth |
138 | ,nRadioHeight | |
139 | ); | |
140 | } // end of wxRadioButton::DoGetBestSize | |
0e320a79 | 141 | |
3c299c3a | 142 | // |
0e320a79 | 143 | // Get single selection, for single choice list items |
3c299c3a | 144 | // |
0e320a79 DW |
145 | bool wxRadioButton::GetValue() const |
146 | { | |
3c299c3a DW |
147 | return((::WinSendMsg((HWND) GetHWND(), BM_QUERYCHECK, (MPARAM)0L, (MPARAM)0L) != 0)); |
148 | } // end of wxRadioButton::GetValue | |
0e320a79 | 149 | |
6670f564 | 150 | bool wxRadioButton::OS2Command( WXUINT wParam, WXWORD WXUNUSED(wId) ) |
cdf1e714 | 151 | { |
2b5f62a0 | 152 | if (wParam != BN_CLICKED) |
6670f564 | 153 | return false; |
2b5f62a0 VZ |
154 | |
155 | if (m_bFocusJustSet) | |
156 | { | |
157 | // | |
158 | // See above: we want to ignore this event | |
159 | // | |
6670f564 | 160 | m_bFocusJustSet = false; |
2b5f62a0 VZ |
161 | } |
162 | else | |
3c299c3a | 163 | { |
6670f564 | 164 | bool bIsChecked = GetValue(); |
2b5f62a0 VZ |
165 | |
166 | if (HasFlag(wxRB_SINGLE)) | |
167 | { | |
168 | // | |
169 | // When we use a "manual" radio button, we have to check the button | |
170 | // ourselves -- but it's reset to unchecked state by the user code | |
171 | // (presumably when another button is pressed) | |
172 | // | |
173 | if (!bIsChecked ) | |
174 | SetValue(TRUE); | |
175 | } | |
6670f564 | 176 | wxCommandEvent rEvent( wxEVT_COMMAND_RADIOBUTTON_SELECTED, m_windowId ); |
3c299c3a DW |
177 | rEvent.SetEventObject(this); |
178 | ProcessCommand(rEvent); | |
3c299c3a | 179 | } |
6670f564 | 180 | return true; |
3c299c3a | 181 | } // end of wxRadioButton::OS2Command |
cdf1e714 | 182 | |
1b086de1 DW |
183 | void wxRadioButton::SetFocus() |
184 | { | |
185 | // when the radio button receives a WM_SETFOCUS message it generates a | |
186 | // BN_CLICKED which is totally unexpected and leads to catastrophic results | |
187 | // if you pop up a dialog from the radio button event handler as, when the | |
188 | // dialog is dismissed, the focus is returned to the radio button which | |
189 | // generates BN_CLICKED which leads to showing another dialog and so on | |
190 | // without end! | |
191 | // | |
e94d504d | 192 | // to avoid this, we drop the pseudo BN_CLICKED events generated when the |
1b086de1 | 193 | // button gains focus |
6670f564 | 194 | m_bFocusJustSet = true; |
1b086de1 DW |
195 | |
196 | wxControl::SetFocus(); | |
197 | } | |
198 | ||
3c299c3a DW |
199 | void wxRadioButton::SetLabel( |
200 | const wxString& rsLabel | |
201 | ) | |
cdf1e714 | 202 | { |
e94d504d SN |
203 | wxString sLabel = ::wxPMTextToLabel(rsLabel); |
204 | ::WinSetWindowText((HWND)GetHWND(), (const char *)sLabel.c_str()); | |
3c299c3a | 205 | } // end of wxRadioButton::SetLabel |
cdf1e714 | 206 | |
3c299c3a DW |
207 | void wxRadioButton::SetValue( |
208 | bool bValue | |
209 | ) | |
cdf1e714 | 210 | { |
3c299c3a | 211 | ::WinSendMsg((HWND)GetHWND(), BM_SETCHECK, (MPARAM)bValue, (MPARAM)0); |
2b5f62a0 VZ |
212 | if (bValue) |
213 | { | |
214 | const wxWindowList& rSiblings = GetParent()->GetChildren(); | |
2461cfa0 | 215 | wxWindowList::compatibility_iterator nodeThis = rSiblings.Find(this); |
2b5f62a0 | 216 | |
2461cfa0 | 217 | wxCHECK_RET(nodeThis, _T("radio button not a child of its parent?")); |
2b5f62a0 | 218 | |
2b5f62a0 | 219 | // |
6e348b12 DW |
220 | // If it's not the first item of the group ... |
221 | // | |
222 | if ( !HasFlag(wxRB_GROUP) ) | |
2b5f62a0 | 223 | { |
6e348b12 DW |
224 | // |
225 | // ...turn off all radio buttons before this one | |
226 | // | |
2461cfa0 SN |
227 | for ( wxWindowList::compatibility_iterator nodeBefore = nodeThis->GetPrevious(); |
228 | nodeBefore; | |
229 | nodeBefore = nodeBefore->GetPrevious() ) | |
6e348b12 | 230 | { |
2461cfa0 | 231 | wxRadioButton* pBtn = wxDynamicCast( nodeBefore->GetData() |
2b5f62a0 VZ |
232 | ,wxRadioButton |
233 | ); | |
6e348b12 DW |
234 | if (!pBtn) |
235 | { | |
236 | // | |
237 | // The radio buttons in a group must be consecutive, so there | |
238 | // are no more of them | |
239 | // | |
240 | break; | |
241 | } | |
242 | pBtn->SetValue(FALSE); | |
243 | if (pBtn->HasFlag(wxRB_GROUP)) | |
244 | { | |
245 | // | |
246 | // Even if there are other radio buttons before this one, | |
247 | // they're not in the same group with us | |
248 | // | |
249 | break; | |
250 | } | |
2b5f62a0 VZ |
251 | } |
252 | } | |
253 | ||
6e348b12 | 254 | // |
2b5f62a0 VZ |
255 | // ... and all after this one |
256 | // | |
2461cfa0 SN |
257 | for (wxWindowList::compatibility_iterator nodeAfter = nodeThis->GetNext(); |
258 | nodeAfter; | |
259 | nodeAfter = nodeAfter->GetNext()) | |
2b5f62a0 | 260 | { |
2461cfa0 | 261 | wxRadioButton* pBtn = wxDynamicCast( nodeAfter->GetData() |
2b5f62a0 VZ |
262 | ,wxRadioButton |
263 | ); | |
264 | ||
265 | if (!pBtn || pBtn->HasFlag(wxRB_GROUP) ) | |
266 | { | |
6e348b12 | 267 | // |
2b5f62a0 VZ |
268 | // No more buttons or the first button of the next group |
269 | // | |
270 | break; | |
271 | } | |
272 | pBtn->SetValue(FALSE); | |
273 | } | |
274 | } | |
3c299c3a | 275 | } // end of wxRadioButton::SetValue |
0e320a79 | 276 | |
97d74dd2 DW |
277 | MRESULT wxRadioButton::OS2WindowProc( |
278 | WXUINT uMsg | |
279 | , WXWPARAM wParam | |
280 | , WXLPARAM lParam | |
281 | ) | |
282 | { | |
283 | if (uMsg == WM_SETFOCUS) | |
284 | { | |
285 | m_bFocusJustSet = TRUE; | |
286 | ||
287 | MRESULT mRc = wxControl::OS2WindowProc( uMsg | |
288 | ,wParam | |
289 | ,lParam | |
290 | ); | |
291 | ||
292 | m_bFocusJustSet = FALSE; | |
293 | return mRc; | |
294 | } | |
295 | return wxControl::OS2WindowProc( uMsg | |
296 | ,wParam | |
297 | ,lParam | |
298 | ); | |
299 | } // end of wxRadioButton::OS2WindowProc |