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 wxRadioBox::wxRadioBox()
103 wxRadioBox::wxRadioBox(wxWindow
*parent
,
105 const wxString
& title
,
109 const wxString
*choices
,
112 const wxValidator
& val
,
113 const wxString
& name
)
117 (void)Create(parent
, id
, title
, pos
, size
, n
, choices
, majorDim
, style
, val
, name
);
120 void wxRadioBox::Init()
126 bool wxRadioBox::Create(wxWindow
*parent
,
128 const wxString
& title
,
132 const wxString
*choices
,
135 const wxValidator
& val
,
136 const wxString
& name
)
138 // for compatibility with the other ports which don't handle (yet?)
139 // wxRA_LEFTTORIGHT and wxRA_TOPTOBOTTOM flags, we add them ourselves if
141 if ( !(style
& (wxRA_LEFTTORIGHT
| wxRA_TOPTOBOTTOM
)) )
143 // horizontal radiobox use left to right layout
144 if ( style
& wxRA_HORIZONTAL
)
146 style
|= wxRA_LEFTTORIGHT
;
148 else if ( style
& wxRA_VERTICAL
)
150 style
|= wxRA_TOPTOBOTTOM
;
154 wxFAIL_MSG( _T("you must specify wxRA_XXX style!") );
157 style
= wxRA_HORIZONTAL
| wxRA_LEFTTORIGHT
;
161 if ( !wxStaticBox::Create(parent
, id
, title
, pos
, size
, style
, name
) )
166 #endif // wxUSE_VALIDATORS
170 // majorDim default value is 0 which means make one row/column
171 SetMajorDim(majorDim
== 0 ? n
: majorDim
);
173 if ( size
== wxDefaultSize
)
175 SetClientSize(DoGetBestClientSize());
178 // Need to move the radiobox in order to move the radio buttons
179 wxPoint actualPos
= GetPosition();
180 wxSize actualSize
= GetSize();
181 DoMoveWindow(actualPos
.x
, actualPos
.y
, actualSize
.x
, actualSize
.y
);
183 // radiobox should already have selection so select at least one item
189 wxRadioBox::~wxRadioBox()
191 // remove the event handlers we pushed on them from all buttons and delete
192 // the buttons themselves: this must be done as the user code expects them
193 // to disappear now and not some time later when they will be deleted by
194 // our (common) parent
195 size_t count
= m_buttons
.GetCount();
196 for ( size_t n
= 0; n
< count
; n
++ )
198 m_buttons
[n
]->PopEventHandler(TRUE
/* delete it */);
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 void wxRadioBox::SetMajorDim(int majorDim
)
210 wxCHECK_RET( majorDim
!= 0, _T("major radiobox dimension can't be 0") );
212 m_majorDim
= majorDim
;
214 int minorDim
= (GetCount() + m_majorDim
- 1) / m_majorDim
;
216 if ( GetWindowStyle() & wxRA_SPECIFY_COLS
)
218 m_numCols
= majorDim
;
219 m_numRows
= minorDim
;
221 else // wxRA_SPECIFY_ROWS
223 m_numCols
= minorDim
;
224 m_numRows
= majorDim
;
228 void wxRadioBox::Append(int count
, const wxString
*choices
)
233 wxWindow
*parent
= GetParent();
234 m_buttons
.Alloc(count
);
235 for ( int n
= 0; n
< count
; n
++ )
237 // make the first button in the box the start of new group by giving it
239 wxRadioButton
*btn
= new wxRadioButton(parent
, -1, choices
[n
],
242 n
== 0 ? wxRB_GROUP
: 0);
244 // we want to get the events from the buttons to translate it into
245 btn
->PushEventHandler(new wxRadioHookHandler(this));
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 void wxRadioBox::SetSelection(int n
)
256 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::SetSelection") );
260 wxRadioButton
*btn
= m_buttons
[n
];
262 // the selected button is always focused in the radiobox
265 // this will also unselect the previously selected button in our group
269 int wxRadioBox::GetSelection() const
274 void wxRadioBox::SendRadioEvent()
276 wxCHECK_RET( m_selection
!= -1, _T("no active radio button") );
278 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, GetId());
279 InitCommandEvent(event
);
280 event
.SetInt(m_selection
);
281 event
.SetString(GetString(m_selection
));
286 void wxRadioBox::OnRadioButton(wxEvent
& event
)
288 int n
= m_buttons
.Index((wxRadioButton
*)event
.GetEventObject());
289 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("click from alien radio button") );
296 // ----------------------------------------------------------------------------
297 // methods forwarded to the buttons
298 // ----------------------------------------------------------------------------
300 wxString
wxRadioBox::GetString(int n
) const
302 wxCHECK_MSG( IsValid(n
), _T(""),
303 _T("invalid index in wxRadioBox::GetString") );
305 return m_buttons
[n
]->GetLabel();
308 void wxRadioBox::SetString(int n
, const wxString
& label
)
310 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::SetString") );
312 m_buttons
[n
]->SetLabel(label
);
315 void wxRadioBox::Enable(int n
, bool enable
)
317 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::Enable") );
319 m_buttons
[n
]->Enable(enable
);
322 void wxRadioBox::Show(int n
, bool show
)
324 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::Show") );
326 m_buttons
[n
]->Show(show
);
329 // ----------------------------------------------------------------------------
330 // methods forwarded to the static box
331 // ----------------------------------------------------------------------------
333 bool wxRadioBox::Enable(bool enable
)
335 if ( !wxStaticBox::Enable(enable
) )
338 // also enable/disable the buttons
339 size_t count
= m_buttons
.GetCount();
340 for ( size_t n
= 0; n
< count
; n
++ )
348 bool wxRadioBox::Show(bool show
)
350 if ( !wxStaticBox::Show(show
) )
353 // also show/hide the buttons
354 size_t count
= m_buttons
.GetCount();
355 for ( size_t n
= 0; n
< count
; n
++ )
363 wxString
wxRadioBox::GetLabel() const
365 return wxStaticBox::GetLabel();
368 void wxRadioBox::SetLabel(const wxString
& label
)
370 wxStaticBox::SetLabel(label
);
374 void wxRadioBox::DoSetToolTip(wxToolTip
*tooltip
)
376 wxControl::DoSetToolTip(tooltip
);
378 // Also set them for all Radio Buttons
379 size_t count
= m_buttons
.GetCount();
380 for ( size_t n
= 0; n
< count
; n
++ )
383 m_buttons
[n
]->SetToolTip(tooltip
->GetTip());
385 m_buttons
[n
]->SetToolTip(NULL
);
388 #endif // wxUSE_TOOLTIPS
390 // ----------------------------------------------------------------------------
391 // buttons positioning
392 // ----------------------------------------------------------------------------
394 wxSize
wxRadioBox::GetMaxButtonSize() const
396 int widthMax
, heightMax
, width
, height
;
397 widthMax
= heightMax
= 0;
399 int count
= GetCount();
400 for ( int n
= 0; n
< count
; n
++ )
402 m_buttons
[n
]->GetBestSize(&width
, &height
);
404 if ( width
> widthMax
)
406 if ( height
> heightMax
)
410 return wxSize(widthMax
+ BUTTON_BORDER_X
, heightMax
+ BUTTON_BORDER_Y
);
413 wxSize
wxRadioBox::DoGetBestClientSize() const
415 wxSize sizeBtn
= GetMaxButtonSize();
417 sizeBtn
.x
*= m_numCols
;
418 sizeBtn
.y
*= m_numRows
;
420 // add a border around all buttons
421 sizeBtn
.x
+= 2*BOX_BORDER_X
;
422 sizeBtn
.y
+= 2*BOX_BORDER_Y
;
424 // account for the area taken by static box
425 wxRect rect
= GetBorderGeometry();
426 sizeBtn
.x
+= rect
.x
+ rect
.width
;
427 sizeBtn
.y
+= rect
.y
+ rect
.height
;
432 void wxRadioBox::DoMoveWindow(int x0
, int y0
, int width
, int height
)
434 wxStaticBox::DoMoveWindow(x0
, y0
, width
, height
);
436 wxSize sizeBtn
= GetMaxButtonSize();
437 wxPoint ptOrigin
= GetBoxAreaOrigin();
438 wxPoint clientOrigin
= GetParent() ? GetParent()->GetClientAreaOrigin() : wxPoint(0,0);
440 x0
+= ptOrigin
.x
+ BOX_BORDER_X
- clientOrigin
.x
;
441 y0
+= ptOrigin
.y
+ BOX_BORDER_Y
- clientOrigin
.y
;
446 int count
= GetCount();
447 for ( int n
= 0; n
< count
; n
++ )
449 m_buttons
[n
]->SetSize(x
, y
, sizeBtn
.x
, sizeBtn
.y
);
451 if ( GetWindowStyle() & wxRA_TOPTOBOTTOM
)
453 // from top to bottom
454 if ( (n
+ 1) % m_numRows
)
456 // continue in this column
461 // start a new column
466 else // wxRA_LEFTTORIGHT: mirror the code above
468 // from left to right
469 if ( (n
+ 1) % m_numCols
)
471 // continue in this row
484 // ----------------------------------------------------------------------------
485 // keyboard navigation
486 // ----------------------------------------------------------------------------
488 bool wxRadioBox::OnKeyDown(wxKeyEvent
& event
)
491 switch ( event
.GetKeyCode() )
513 int selOld
= GetSelection();
514 int selNew
= GetNextItem(selOld
, dir
, GetWindowStyle());
515 if ( selNew
!= selOld
)
517 SetSelection(selNew
);
519 // emulate the button click
526 #endif // wxUSE_RADIOBOX