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 licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 // Need to move the radiobox in order to move the radio buttons
157 wxPoint actualPos
= GetPosition();
158 wxSize actualSize
= GetSize();
159 DoMoveWindow(actualPos
.x
, actualPos
.y
, actualSize
.x
, actualSize
.y
);
161 // radiobox should already have selection so select at least one item
167 wxRadioBox::~wxRadioBox()
169 // remove the event handlers we pushed on them from all buttons and delete
170 // the buttons themselves: this must be done as the user code expects them
171 // to disappear now and not some time later when they will be deleted by
172 // our (common) parent
173 size_t count
= m_buttons
.GetCount();
174 for ( size_t n
= 0; n
< count
; n
++ )
176 m_buttons
[n
]->PopEventHandler(TRUE
/* delete it */);
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 void wxRadioBox::SetMajorDim(int majorDim
)
188 wxCHECK_RET( majorDim
!= 0, _T("major radiobox dimension can't be 0") );
190 m_majorDim
= majorDim
;
192 int minorDim
= (GetCount() + m_majorDim
- 1) / m_majorDim
;
194 if ( GetWindowStyle() & wxRA_SPECIFY_COLS
)
196 m_numCols
= majorDim
;
197 m_numRows
= minorDim
;
199 else // wxRA_SPECIFY_ROWS
201 m_numCols
= minorDim
;
202 m_numRows
= majorDim
;
206 void wxRadioBox::Append(int count
, const wxString
*choices
)
211 wxWindow
*parent
= GetParent();
212 m_buttons
.Alloc(count
);
213 for ( int n
= 0; n
< count
; n
++ )
215 // make the first button in the box the start of new group by giving it
217 wxRadioButton
*btn
= new wxRadioButton(parent
, -1, choices
[n
],
220 n
== 0 ? wxRB_GROUP
: 0);
222 // we want to get the events from the buttons to translate it into
223 btn
->PushEventHandler(new wxRadioHookHandler(this));
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
232 void wxRadioBox::SetSelection(int n
)
234 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::SetSelection") );
238 wxRadioButton
*btn
= m_buttons
[n
];
240 // the selected button is always focused in the radiobox
243 // this will also unselect the previously selected button in our group
247 int wxRadioBox::GetSelection() const
252 void wxRadioBox::SendRadioEvent()
254 wxCHECK_RET( m_selection
!= -1, _T("no active radio button") );
256 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, GetId());
257 InitCommandEvent(event
);
258 event
.SetInt(m_selection
);
259 event
.SetString(GetString(m_selection
));
264 void wxRadioBox::OnRadioButton(wxEvent
& event
)
266 int n
= m_buttons
.Index((wxRadioButton
*)event
.GetEventObject());
267 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("click from alien radio button") );
274 // ----------------------------------------------------------------------------
275 // methods forwarded to the buttons
276 // ----------------------------------------------------------------------------
278 wxString
wxRadioBox::GetString(int n
) const
280 wxCHECK_MSG( IsValid(n
), _T(""),
281 _T("invalid index in wxRadioBox::GetString") );
283 return m_buttons
[n
]->GetLabel();
286 void wxRadioBox::SetString(int n
, const wxString
& label
)
288 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::SetString") );
290 m_buttons
[n
]->SetLabel(label
);
293 void wxRadioBox::Enable(int n
, bool enable
)
295 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::Enable") );
297 m_buttons
[n
]->Enable(enable
);
300 void wxRadioBox::Show(int n
, bool show
)
302 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::Show") );
304 m_buttons
[n
]->Show(show
);
307 // ----------------------------------------------------------------------------
308 // methods forwarded to the static box
309 // ----------------------------------------------------------------------------
311 bool wxRadioBox::Enable(bool enable
)
313 if ( !wxStaticBox::Enable(enable
) )
316 // also enable/disable the buttons
317 size_t count
= m_buttons
.GetCount();
318 for ( size_t n
= 0; n
< count
; n
++ )
326 bool wxRadioBox::Show(bool show
)
328 if ( !wxStaticBox::Show(show
) )
331 // also show/hide the buttons
332 size_t count
= m_buttons
.GetCount();
333 for ( size_t n
= 0; n
< count
; n
++ )
341 wxString
wxRadioBox::GetLabel() const
343 return wxStaticBox::GetLabel();
346 void wxRadioBox::SetLabel(const wxString
& label
)
348 wxStaticBox::SetLabel(label
);
352 void wxRadioBox::DoSetToolTip(wxToolTip
*tooltip
)
354 wxControl::DoSetToolTip(tooltip
);
356 // Also set them for all Radio Buttons
357 size_t count
= m_buttons
.GetCount();
358 for ( size_t n
= 0; n
< count
; n
++ )
361 m_buttons
[n
]->SetToolTip(tooltip
->GetTip());
363 m_buttons
[n
]->SetToolTip(NULL
);
366 #endif // wxUSE_TOOLTIPS
368 // ----------------------------------------------------------------------------
369 // buttons positioning
370 // ----------------------------------------------------------------------------
372 wxSize
wxRadioBox::GetMaxButtonSize() const
374 int widthMax
, heightMax
, width
, height
;
375 widthMax
= heightMax
= 0;
377 int count
= GetCount();
378 for ( int n
= 0; n
< count
; n
++ )
380 m_buttons
[n
]->GetBestSize(&width
, &height
);
382 if ( width
> widthMax
)
384 if ( height
> heightMax
)
388 return wxSize(widthMax
+ BUTTON_BORDER_X
, heightMax
+ BUTTON_BORDER_Y
);
391 wxSize
wxRadioBox::DoGetBestClientSize() const
393 wxSize sizeBtn
= GetMaxButtonSize();
395 sizeBtn
.x
*= m_numCols
;
396 sizeBtn
.y
*= m_numRows
;
398 // add a border around all buttons
399 sizeBtn
.x
+= 2*BOX_BORDER_X
;
400 sizeBtn
.y
+= 2*BOX_BORDER_Y
;
402 // account for the area taken by static box
403 wxRect rect
= GetBorderGeometry();
404 sizeBtn
.x
+= rect
.x
+ rect
.width
;
405 sizeBtn
.y
+= rect
.y
+ rect
.height
;
410 void wxRadioBox::DoMoveWindow(int x0
, int y0
, int width
, int height
)
412 wxStaticBox::DoMoveWindow(x0
, y0
, width
, height
);
414 wxSize sizeBtn
= GetMaxButtonSize();
415 wxPoint ptOrigin
= GetBoxAreaOrigin();
416 wxPoint clientOrigin
= GetParent() ? GetParent()->GetClientAreaOrigin() : wxPoint(0,0);
418 x0
+= ptOrigin
.x
+ BOX_BORDER_X
- clientOrigin
.x
;
419 y0
+= ptOrigin
.y
+ BOX_BORDER_Y
- clientOrigin
.y
;
424 int count
= GetCount();
425 for ( int n
= 0; n
< count
; n
++ )
427 m_buttons
[n
]->SetSize(x
, y
, sizeBtn
.x
, sizeBtn
.y
);
429 if ( GetWindowStyle() & wxRA_TOPTOBOTTOM
)
431 // from top to bottom
432 if ( (n
+ 1) % m_numRows
)
434 // continue in this column
439 // start a new column
444 else // wxRA_LEFTTORIGHT: mirror the code above
446 // from left to right
447 if ( (n
+ 1) % m_numCols
)
449 // continue in this row
462 // ----------------------------------------------------------------------------
463 // keyboard navigation
464 // ----------------------------------------------------------------------------
466 bool wxRadioBox::OnKeyDown(wxKeyEvent
& event
)
469 switch ( event
.GetKeyCode() )
491 int selOld
= GetSelection();
492 int selNew
= GetNextItem(selOld
, dir
, GetWindowStyle());
493 if ( selNew
!= selOld
)
495 SetSelection(selNew
);
497 // emulate the button click
504 #endif // wxUSE_RADIOBOX