1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/radiobox.cpp
3 // Purpose: wxRadioBox implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "univradiobox.h"
24 #include "wx/wxprec.h"
33 #include "wx/dcclient.h"
34 #include "wx/radiobox.h"
35 #include "wx/radiobut.h"
36 #include "wx/validate.h"
39 #include "wx/tooltip.h"
41 #include "wx/univ/theme.h"
42 #include "wx/univ/renderer.h"
43 #include "wx/univ/inphand.h"
44 #include "wx/univ/colschem.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 static const int BUTTON_BORDER_X
= 2;
51 static const int BUTTON_BORDER_Y
= 4;
53 static const int BOX_BORDER_X
= 2;
54 static const int BOX_BORDER_Y
= 2;
56 // ----------------------------------------------------------------------------
57 // wxRadioBox event handler
58 // ----------------------------------------------------------------------------
60 class wxRadioHookHandler
: public wxEvtHandler
63 wxRadioHookHandler(wxRadioBox
*radio
) { m_radio
= radio
; }
65 virtual bool ProcessEvent(wxEvent
& event
)
67 // we intercept the command events from radio buttons
68 if ( event
.GetEventType() == wxEVT_COMMAND_RADIOBUTTON_SELECTED
)
70 m_radio
->OnRadioButton(event
);
72 else if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
74 if ( m_radio
->OnKeyDown((wxKeyEvent
&)event
) )
81 return GetNextHandler()->ProcessEvent(event
);
88 // ============================================================================
90 // ============================================================================
92 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
94 // ----------------------------------------------------------------------------
95 // wxRadioBox creation
96 // ----------------------------------------------------------------------------
98 void wxRadioBox::Init()
104 bool wxRadioBox::Create(wxWindow
*parent
,
106 const wxString
& title
,
110 const wxString
*choices
,
113 const wxValidator
& val
,
114 const wxString
& name
)
116 // for compatibility with the other ports which don't handle (yet?)
117 // wxRA_LEFTTORIGHT and wxRA_TOPTOBOTTOM flags, we add them ourselves if
119 if ( !(style
& (wxRA_LEFTTORIGHT
| wxRA_TOPTOBOTTOM
)) )
121 // horizontal radiobox use left to right layout
122 if ( style
& wxRA_HORIZONTAL
)
124 style
|= wxRA_LEFTTORIGHT
;
126 else if ( style
& wxRA_VERTICAL
)
128 style
|= wxRA_TOPTOBOTTOM
;
132 wxFAIL_MSG( _T("you must specify wxRA_XXX style!") );
135 style
= wxRA_HORIZONTAL
| wxRA_LEFTTORIGHT
;
139 if ( !wxStaticBox::Create(parent
, id
, title
, pos
, size
, style
, name
) )
144 #endif // wxUSE_VALIDATORS
148 // majorDim default value is 0 which means make one row/column
149 SetMajorDim(majorDim
== 0 ? n
: majorDim
);
151 if ( size
== wxDefaultSize
)
153 SetClientSize(DoGetBestClientSize());
156 // radiobox should already have selection so select at least one item
162 wxRadioBox::~wxRadioBox()
164 // remove the event handlers we pushed on them from all buttons and delete
165 // the buttons themselves: this must be done as the user code expects them
166 // to disappear now and not some time later when they will be deleted by
167 // our (common) parent
168 size_t count
= m_buttons
.GetCount();
169 for ( size_t n
= 0; n
< count
; n
++ )
171 m_buttons
[n
]->PopEventHandler(TRUE
/* delete it */);
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 void wxRadioBox::SetMajorDim(int majorDim
)
183 wxCHECK_RET( majorDim
!= 0, _T("major radiobox dimension can't be 0") );
185 m_majorDim
= majorDim
;
187 int minorDim
= (GetCount() + m_majorDim
- 1) / m_majorDim
;
189 if ( GetWindowStyle() & wxRA_SPECIFY_COLS
)
191 m_numCols
= majorDim
;
192 m_numRows
= minorDim
;
194 else // wxRA_SPECIFY_ROWS
196 m_numCols
= minorDim
;
197 m_numRows
= majorDim
;
201 void wxRadioBox::Append(int count
, const wxString
*choices
)
206 wxWindow
*parent
= GetParent();
207 m_buttons
.Alloc(count
);
208 for ( int n
= 0; n
< count
; n
++ )
210 // make the first button in the box the start of new group by giving it
212 wxRadioButton
*btn
= new wxRadioButton(parent
, -1, choices
[n
],
215 n
== 0 ? wxRB_GROUP
: 0);
217 // we want to get the events from the buttons to translate it into
218 btn
->PushEventHandler(new wxRadioHookHandler(this));
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
227 void wxRadioBox::SetSelection(int n
)
229 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::SetSelection") );
233 wxRadioButton
*btn
= m_buttons
[n
];
235 // the selected button is always focused in the radiobox
238 // this will also unselect the previously selected button in our group
242 int wxRadioBox::GetSelection() const
247 void wxRadioBox::SendRadioEvent()
249 wxCHECK_RET( m_selection
!= -1, _T("no active radio button") );
251 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, GetId());
252 InitCommandEvent(event
);
253 event
.SetInt(m_selection
);
254 event
.SetString(GetString(m_selection
));
259 void wxRadioBox::OnRadioButton(wxEvent
& event
)
261 int n
= m_buttons
.Index((wxRadioButton
*)event
.GetEventObject());
262 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("click from alien radio button") );
269 // ----------------------------------------------------------------------------
270 // methods forwarded to the buttons
271 // ----------------------------------------------------------------------------
273 wxString
wxRadioBox::GetString(int n
) const
275 wxCHECK_MSG( IsValid(n
), _T(""),
276 _T("invalid index in wxRadioBox::GetString") );
278 return m_buttons
[n
]->GetLabel();
281 void wxRadioBox::SetString(int n
, const wxString
& label
)
283 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::SetString") );
285 m_buttons
[n
]->SetLabel(label
);
288 void wxRadioBox::Enable(int n
, bool enable
)
290 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::Enable") );
292 m_buttons
[n
]->Enable(enable
);
295 void wxRadioBox::Show(int n
, bool show
)
297 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::Show") );
299 m_buttons
[n
]->Show(show
);
302 // ----------------------------------------------------------------------------
303 // methods forwarded to the static box
304 // ----------------------------------------------------------------------------
306 bool wxRadioBox::Enable(bool enable
)
308 if ( !wxStaticBox::Enable(enable
) )
311 // also enable/disable the buttons
312 size_t count
= m_buttons
.GetCount();
313 for ( size_t n
= 0; n
< count
; n
++ )
321 bool wxRadioBox::Show(bool show
)
323 if ( !wxStaticBox::Show(show
) )
326 // also show/hide the buttons
327 size_t count
= m_buttons
.GetCount();
328 for ( size_t n
= 0; n
< count
; n
++ )
336 wxString
wxRadioBox::GetLabel() const
338 return wxStaticBox::GetLabel();
341 void wxRadioBox::SetLabel(const wxString
& label
)
343 wxStaticBox::SetLabel(label
);
347 void wxRadioBox::DoSetToolTip(wxToolTip
*tooltip
)
349 wxControl::DoSetToolTip(tooltip
);
351 // Also set them for all Radio Buttons
352 size_t count
= m_buttons
.GetCount();
353 for ( size_t n
= 0; n
< count
; n
++ )
356 m_buttons
[n
]->SetToolTip(tooltip
->GetTip());
358 m_buttons
[n
]->SetToolTip(NULL
);
361 #endif // wxUSE_TOOLTIPS
363 // ----------------------------------------------------------------------------
364 // buttons positioning
365 // ----------------------------------------------------------------------------
367 wxSize
wxRadioBox::GetMaxButtonSize() const
369 int widthMax
, heightMax
, width
, height
;
370 widthMax
= heightMax
= 0;
372 int count
= GetCount();
373 for ( int n
= 0; n
< count
; n
++ )
375 m_buttons
[n
]->GetBestSize(&width
, &height
);
377 if ( width
> widthMax
)
379 if ( height
> heightMax
)
383 return wxSize(widthMax
+ BUTTON_BORDER_X
, heightMax
+ BUTTON_BORDER_Y
);
386 wxSize
wxRadioBox::DoGetBestClientSize() const
388 wxSize sizeBtn
= GetMaxButtonSize();
390 sizeBtn
.x
*= m_numCols
;
391 sizeBtn
.y
*= m_numRows
;
393 // add a border around all buttons
394 sizeBtn
.x
+= 2*BOX_BORDER_X
;
395 sizeBtn
.y
+= 2*BOX_BORDER_Y
;
397 // account for the area taken by static box
398 wxRect rect
= GetBorderGeometry();
399 sizeBtn
.x
+= rect
.x
+ rect
.width
;
400 sizeBtn
.y
+= rect
.y
+ rect
.height
;
405 void wxRadioBox::DoMoveWindow(int x0
, int y0
, int width
, int height
)
407 wxStaticBox::DoMoveWindow(x0
, y0
, width
, height
);
409 wxSize sizeBtn
= GetMaxButtonSize();
410 wxPoint ptOrigin
= GetBoxAreaOrigin();
411 wxPoint clientOrigin
= GetParent() ? GetParent()->GetClientAreaOrigin() : wxPoint(0,0);
413 x0
+= ptOrigin
.x
+ BOX_BORDER_X
- clientOrigin
.x
;
414 y0
+= ptOrigin
.y
+ BOX_BORDER_Y
- clientOrigin
.y
;
419 int count
= GetCount();
420 for ( int n
= 0; n
< count
; n
++ )
422 m_buttons
[n
]->SetSize(x
, y
, sizeBtn
.x
, sizeBtn
.y
);
424 if ( GetWindowStyle() & wxRA_TOPTOBOTTOM
)
426 // from top to bottom
427 if ( (n
+ 1) % m_numRows
)
429 // continue in this column
434 // start a new column
439 else // wxRA_LEFTTORIGHT: mirror the code above
441 // from left to right
442 if ( (n
+ 1) % m_numCols
)
444 // continue in this row
457 // ----------------------------------------------------------------------------
458 // keyboard navigation
459 // ----------------------------------------------------------------------------
461 bool wxRadioBox::OnKeyDown(wxKeyEvent
& event
)
464 switch ( event
.GetKeyCode() )
486 int selOld
= GetSelection();
487 int selNew
= GetNextItem(selOld
, dir
, GetWindowStyle());
488 if ( selNew
!= selOld
)
490 SetSelection(selNew
);
492 // emulate the button click
499 #endif // wxUSE_RADIOBOX