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