]> git.saurik.com Git - wxWidgets.git/blame - src/os2/radiobut.cpp
set best fitting size for the toolbar in Realize()
[wxWidgets.git] / src / os2 / radiobut.cpp
CommitLineData
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 30IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
0e320a79 31
f289196b
DW
32extern void wxAssociateWinWithHandle( HWND hWnd
33 ,wxWindowOS2* pWin
34 );
35
1b086de1
DW
36void wxRadioButton::Init()
37{
38 m_bFocusJustSet = FALSE;
39} // end of wxRadioButton::Init
40
3c299c3a
DW
41void wxRadioButton::Command (
42 wxCommandEvent& rEvent
43)
cdf1e714 44{
3c299c3a
DW
45 SetValue ((rEvent.GetInt() != 0) );
46 ProcessCommand (rEvent);
47} // end of wxRadioButton::Command
48
49bool 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
112wxSize wxRadioButton::DoGetBestSize() const
113{
114 static int snRadioSize = 0;
cdf1e714 115
1b086de1
DW
116 if (!snRadioSize)
117 {
118 wxScreenDC vDC;
cdf1e714 119
1b086de1
DW
120 vDC.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
121 snRadioSize = vDC.GetCharHeight();
122 }
123
124 wxString sStr = GetLabel();
125 int nRadioWidth;
126 int nRadioHeight;
127
128 if (!sStr.empty())
cdf1e714 129 {
1b086de1
DW
130 GetTextExtent( sStr
131 ,&nRadioWidth
132 ,&nRadioHeight
3c299c3a 133 );
1b086de1
DW
134 nRadioWidth += snRadioSize + GetCharWidth();
135 if (nRadioHeight < snRadioSize)
136 nRadioHeight = snRadioSize;
3c299c3a
DW
137 }
138 else
139 {
1b086de1
DW
140 nRadioWidth = snRadioSize;
141 nRadioHeight = snRadioSize;
cdf1e714 142 }
1b086de1
DW
143 return wxSize( nRadioWidth
144 ,nRadioHeight
145 );
146} // end of wxRadioButton::DoGetBestSize
0e320a79 147
3c299c3a 148//
0e320a79 149// Get single selection, for single choice list items
3c299c3a 150//
0e320a79
DW
151bool wxRadioButton::GetValue() const
152{
3c299c3a
DW
153 return((::WinSendMsg((HWND) GetHWND(), BM_QUERYCHECK, (MPARAM)0L, (MPARAM)0L) != 0));
154} // end of wxRadioButton::GetValue
0e320a79 155
6670f564 156bool wxRadioButton::OS2Command( WXUINT wParam, WXWORD WXUNUSED(wId) )
cdf1e714 157{
2b5f62a0 158 if (wParam != BN_CLICKED)
6670f564 159 return false;
2b5f62a0
VZ
160
161 if (m_bFocusJustSet)
162 {
163 //
164 // See above: we want to ignore this event
165 //
6670f564 166 m_bFocusJustSet = false;
2b5f62a0
VZ
167 }
168 else
3c299c3a 169 {
6670f564 170 bool bIsChecked = GetValue();
2b5f62a0
VZ
171
172 if (HasFlag(wxRB_SINGLE))
173 {
174 //
175 // When we use a "manual" radio button, we have to check the button
176 // ourselves -- but it's reset to unchecked state by the user code
177 // (presumably when another button is pressed)
178 //
179 if (!bIsChecked )
180 SetValue(TRUE);
181 }
6670f564 182 wxCommandEvent rEvent( wxEVT_COMMAND_RADIOBUTTON_SELECTED, m_windowId );
3c299c3a
DW
183 rEvent.SetEventObject(this);
184 ProcessCommand(rEvent);
3c299c3a 185 }
6670f564 186 return true;
3c299c3a 187} // end of wxRadioButton::OS2Command
cdf1e714 188
1b086de1
DW
189void wxRadioButton::SetFocus()
190{
191 // when the radio button receives a WM_SETFOCUS message it generates a
192 // BN_CLICKED which is totally unexpected and leads to catastrophic results
193 // if you pop up a dialog from the radio button event handler as, when the
194 // dialog is dismissed, the focus is returned to the radio button which
195 // generates BN_CLICKED which leads to showing another dialog and so on
196 // without end!
197 //
e94d504d 198 // to avoid this, we drop the pseudo BN_CLICKED events generated when the
1b086de1 199 // button gains focus
6670f564 200 m_bFocusJustSet = true;
1b086de1
DW
201
202 wxControl::SetFocus();
203}
204
3c299c3a
DW
205void wxRadioButton::SetLabel(
206 const wxString& rsLabel
207)
cdf1e714 208{
e94d504d
SN
209 wxString sLabel = ::wxPMTextToLabel(rsLabel);
210 ::WinSetWindowText((HWND)GetHWND(), (const char *)sLabel.c_str());
3c299c3a 211} // end of wxRadioButton::SetLabel
cdf1e714 212
3c299c3a
DW
213void wxRadioButton::SetValue(
214 bool bValue
215)
cdf1e714 216{
3c299c3a 217 ::WinSendMsg((HWND)GetHWND(), BM_SETCHECK, (MPARAM)bValue, (MPARAM)0);
2b5f62a0
VZ
218 if (bValue)
219 {
220 const wxWindowList& rSiblings = GetParent()->GetChildren();
2461cfa0 221 wxWindowList::compatibility_iterator nodeThis = rSiblings.Find(this);
2b5f62a0 222
2461cfa0 223 wxCHECK_RET(nodeThis, _T("radio button not a child of its parent?"));
2b5f62a0 224
2b5f62a0 225 //
6e348b12
DW
226 // If it's not the first item of the group ...
227 //
228 if ( !HasFlag(wxRB_GROUP) )
2b5f62a0 229 {
6e348b12
DW
230 //
231 // ...turn off all radio buttons before this one
232 //
2461cfa0
SN
233 for ( wxWindowList::compatibility_iterator nodeBefore = nodeThis->GetPrevious();
234 nodeBefore;
235 nodeBefore = nodeBefore->GetPrevious() )
6e348b12 236 {
2461cfa0 237 wxRadioButton* pBtn = wxDynamicCast( nodeBefore->GetData()
2b5f62a0
VZ
238 ,wxRadioButton
239 );
6e348b12
DW
240 if (!pBtn)
241 {
242 //
243 // The radio buttons in a group must be consecutive, so there
244 // are no more of them
245 //
246 break;
247 }
248 pBtn->SetValue(FALSE);
249 if (pBtn->HasFlag(wxRB_GROUP))
250 {
251 //
252 // Even if there are other radio buttons before this one,
253 // they're not in the same group with us
254 //
255 break;
256 }
2b5f62a0
VZ
257 }
258 }
259
6e348b12 260 //
2b5f62a0
VZ
261 // ... and all after this one
262 //
2461cfa0
SN
263 for (wxWindowList::compatibility_iterator nodeAfter = nodeThis->GetNext();
264 nodeAfter;
265 nodeAfter = nodeAfter->GetNext())
2b5f62a0 266 {
2461cfa0 267 wxRadioButton* pBtn = wxDynamicCast( nodeAfter->GetData()
2b5f62a0
VZ
268 ,wxRadioButton
269 );
270
271 if (!pBtn || pBtn->HasFlag(wxRB_GROUP) )
272 {
6e348b12 273 //
2b5f62a0
VZ
274 // No more buttons or the first button of the next group
275 //
276 break;
277 }
278 pBtn->SetValue(FALSE);
279 }
280 }
3c299c3a 281} // end of wxRadioButton::SetValue
0e320a79 282
97d74dd2
DW
283MRESULT wxRadioButton::OS2WindowProc(
284 WXUINT uMsg
285, WXWPARAM wParam
286, WXLPARAM lParam
287)
288{
289 if (uMsg == WM_SETFOCUS)
290 {
291 m_bFocusJustSet = TRUE;
292
293 MRESULT mRc = wxControl::OS2WindowProc( uMsg
294 ,wParam
295 ,lParam
296 );
297
298 m_bFocusJustSet = FALSE;
299 return mRc;
300 }
301 return wxControl::OS2WindowProc( uMsg
302 ,wParam
303 ,lParam
304 );
305} // end of wxRadioButton::OS2WindowProc