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 #include "wx/wxprec.h"
29 #include "wx/dcclient.h"
30 #include "wx/radiobox.h"
31 #include "wx/radiobut.h"
32 #include "wx/validate.h"
33 #include "wx/arrstr.h"
36 #include "wx/tooltip.h"
38 #include "wx/univ/theme.h"
39 #include "wx/univ/renderer.h"
40 #include "wx/univ/inphand.h"
41 #include "wx/univ/colschem.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 static const int BUTTON_BORDER_X
= 2;
48 static const int BUTTON_BORDER_Y
= 4;
50 static const int BOX_BORDER_X
= 2;
51 static const int BOX_BORDER_Y
= 2;
53 // ----------------------------------------------------------------------------
54 // wxRadioBox event handler
55 // ----------------------------------------------------------------------------
57 class wxRadioHookHandler
: public wxEvtHandler
60 wxRadioHookHandler(wxRadioBox
*radio
) { m_radio
= radio
; }
62 virtual bool ProcessEvent(wxEvent
& event
)
64 // we intercept the command events from radio buttons
65 if ( event
.GetEventType() == wxEVT_COMMAND_RADIOBUTTON_SELECTED
)
67 m_radio
->OnRadioButton(event
);
69 else if ( event
.GetEventType() == wxEVT_KEY_DOWN
)
71 if ( m_radio
->OnKeyDown((wxKeyEvent
&)event
) )
78 return GetNextHandler()->ProcessEvent(event
);
85 // ============================================================================
87 // ============================================================================
89 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
91 // ----------------------------------------------------------------------------
92 // wxRadioBox creation
93 // ----------------------------------------------------------------------------
95 void wxRadioBox::Init()
101 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
102 const wxPoint
& pos
, const wxSize
& size
,
103 const wxArrayString
& choices
,
104 int majorDim
, long style
,
105 const wxValidator
& val
, const wxString
& name
)
107 wxCArrayString
chs(choices
);
111 (void)Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
112 chs
.GetStrings(), majorDim
, style
, val
, name
);
115 bool wxRadioBox::Create(wxWindow
*parent
,
117 const wxString
& title
,
120 const wxArrayString
& choices
,
123 const wxValidator
& val
,
124 const wxString
& name
)
126 wxCArrayString
chs(choices
);
128 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
129 chs
.GetStrings(), majorDim
, style
, val
, name
);
132 bool wxRadioBox::Create(wxWindow
*parent
,
134 const wxString
& title
,
138 const wxString
*choices
,
141 const wxValidator
& wxVALIDATOR_PARAM(val
),
142 const wxString
& name
)
144 // for compatibility with the other ports which don't handle (yet?)
145 // wxRA_LEFTTORIGHT and wxRA_TOPTOBOTTOM flags, we add them ourselves if
147 if ( !(style
& (wxRA_LEFTTORIGHT
| wxRA_TOPTOBOTTOM
)) )
149 // horizontal radiobox use left to right layout
150 if ( style
& wxRA_HORIZONTAL
)
152 style
|= wxRA_LEFTTORIGHT
;
154 else if ( style
& wxRA_VERTICAL
)
156 style
|= wxRA_TOPTOBOTTOM
;
160 wxFAIL_MSG( _T("you must specify wxRA_XXX style!") );
163 style
= wxRA_HORIZONTAL
| wxRA_LEFTTORIGHT
;
167 if ( !wxStaticBox::Create(parent
, id
, title
, pos
, size
, style
, name
) )
172 #endif // wxUSE_VALIDATORS
176 // majorDim default value is 0 which means make one row/column
177 SetMajorDim(majorDim
== 0 ? n
: majorDim
);
179 if ( size
== wxDefaultSize
)
181 SetClientSize(DoGetBestClientSize());
184 // Need to move the radiobox in order to move the radio buttons
185 wxPoint actualPos
= GetPosition();
186 wxSize actualSize
= GetSize();
187 DoMoveWindow(actualPos
.x
, actualPos
.y
, actualSize
.x
, actualSize
.y
);
189 // radiobox should already have selection so select at least one item
195 wxRadioBox::~wxRadioBox()
197 // remove the event handlers we pushed on them from all buttons and delete
198 // the buttons themselves: this must be done as the user code expects them
199 // to disappear now and not some time later when they will be deleted by
200 // our (common) parent
201 size_t count
= m_buttons
.GetCount();
202 for ( size_t n
= 0; n
< count
; n
++ )
204 m_buttons
[n
]->PopEventHandler(true /* delete it */);
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 void wxRadioBox::SetMajorDim(int majorDim
)
216 wxCHECK_RET( majorDim
!= 0, _T("major radiobox dimension can't be 0") );
218 m_majorDim
= majorDim
;
220 int minorDim
= (GetCount() + m_majorDim
- 1) / m_majorDim
;
222 if ( GetWindowStyle() & wxRA_SPECIFY_COLS
)
224 m_numCols
= majorDim
;
225 m_numRows
= minorDim
;
227 else // wxRA_SPECIFY_ROWS
229 m_numCols
= minorDim
;
230 m_numRows
= majorDim
;
234 void wxRadioBox::Append(int count
, const wxString
*choices
)
239 wxWindow
*parent
= GetParent();
240 m_buttons
.Alloc(count
);
241 for ( int n
= 0; n
< count
; n
++ )
243 // make the first button in the box the start of new group by giving it
245 wxRadioButton
*btn
= new wxRadioButton(parent
, wxID_ANY
, choices
[n
],
248 n
== 0 ? wxRB_GROUP
: 0);
250 // we want to get the events from the buttons to translate it into
251 btn
->PushEventHandler(new wxRadioHookHandler(this));
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 void wxRadioBox::SetSelection(int n
)
262 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::SetSelection") );
266 wxRadioButton
*btn
= m_buttons
[n
];
268 // the selected button is always focused in the radiobox
271 // this will also unselect the previously selected button in our group
275 int wxRadioBox::GetSelection() const
280 void wxRadioBox::SendRadioEvent()
282 wxCHECK_RET( m_selection
!= -1, _T("no active radio button") );
284 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, GetId());
285 InitCommandEvent(event
);
286 event
.SetInt(m_selection
);
287 event
.SetString(GetString(m_selection
));
292 void wxRadioBox::OnRadioButton(wxEvent
& event
)
294 int n
= m_buttons
.Index((wxRadioButton
*)event
.GetEventObject());
295 wxCHECK_RET( n
!= wxNOT_FOUND
, _T("click from alien radio button") );
302 // ----------------------------------------------------------------------------
303 // methods forwarded to the buttons
304 // ----------------------------------------------------------------------------
306 wxString
wxRadioBox::GetString(int n
) const
308 wxCHECK_MSG( IsValid(n
), wxEmptyString
,
309 _T("invalid index in wxRadioBox::GetString") );
311 return m_buttons
[n
]->GetLabel();
314 void wxRadioBox::SetString(int n
, const wxString
& label
)
316 wxCHECK_RET( IsValid(n
), _T("invalid index in wxRadioBox::SetString") );
318 m_buttons
[n
]->SetLabel(label
);
321 bool wxRadioBox::Enable(int n
, bool enable
)
323 wxCHECK_MSG( IsValid(n
), false, _T("invalid index in wxRadioBox::Enable") );
325 return m_buttons
[n
]->Enable(enable
);
328 bool wxRadioBox::Show(int n
, bool show
)
330 wxCHECK_MSG( IsValid(n
), false, _T("invalid index in wxRadioBox::Show") );
332 return m_buttons
[n
]->Show(show
);
335 // ----------------------------------------------------------------------------
336 // methods forwarded to the static box
337 // ----------------------------------------------------------------------------
339 bool wxRadioBox::Enable(bool enable
)
341 if ( !wxStaticBox::Enable(enable
) )
344 // also enable/disable the buttons
345 size_t count
= m_buttons
.GetCount();
346 for ( size_t n
= 0; n
< count
; n
++ )
354 bool wxRadioBox::Show(bool show
)
356 if ( !wxStaticBox::Show(show
) )
359 // also show/hide the buttons
360 size_t count
= m_buttons
.GetCount();
361 for ( size_t n
= 0; n
< count
; n
++ )
369 wxString
wxRadioBox::GetLabel() const
371 return wxStaticBox::GetLabel();
374 void wxRadioBox::SetLabel(const wxString
& label
)
376 wxStaticBox::SetLabel(label
);
380 void wxRadioBox::DoSetToolTip(wxToolTip
*tooltip
)
382 wxControl::DoSetToolTip(tooltip
);
384 // Also set them for all Radio Buttons
385 size_t count
= m_buttons
.GetCount();
386 for ( size_t n
= 0; n
< count
; n
++ )
389 m_buttons
[n
]->SetToolTip(tooltip
->GetTip());
391 m_buttons
[n
]->SetToolTip(NULL
);
394 #endif // wxUSE_TOOLTIPS
396 // ----------------------------------------------------------------------------
397 // buttons positioning
398 // ----------------------------------------------------------------------------
400 wxSize
wxRadioBox::GetMaxButtonSize() const
402 int widthMax
, heightMax
, width
, height
;
403 widthMax
= heightMax
= 0;
405 int count
= GetCount();
406 for ( int n
= 0; n
< count
; n
++ )
408 m_buttons
[n
]->GetBestSize(&width
, &height
);
410 if ( width
> widthMax
)
412 if ( height
> heightMax
)
416 return wxSize(widthMax
+ BUTTON_BORDER_X
, heightMax
+ BUTTON_BORDER_Y
);
419 wxSize
wxRadioBox::DoGetBestClientSize() const
421 wxSize sizeBtn
= GetMaxButtonSize();
423 sizeBtn
.x
*= m_numCols
;
424 sizeBtn
.y
*= m_numRows
;
426 // add a border around all buttons
427 sizeBtn
.x
+= 2*BOX_BORDER_X
;
428 sizeBtn
.y
+= 2*BOX_BORDER_Y
;
430 // account for the area taken by static box
431 wxRect rect
= GetBorderGeometry();
432 sizeBtn
.x
+= rect
.x
+ rect
.width
;
433 sizeBtn
.y
+= rect
.y
+ rect
.height
;
438 void wxRadioBox::DoMoveWindow(int x0
, int y0
, int width
, int height
)
440 wxStaticBox::DoMoveWindow(x0
, y0
, width
, height
);
442 wxSize sizeBtn
= GetMaxButtonSize();
443 wxPoint ptOrigin
= GetBoxAreaOrigin();
444 wxPoint clientOrigin
= GetParent() ? GetParent()->GetClientAreaOrigin() : wxPoint(0,0);
446 x0
+= ptOrigin
.x
+ BOX_BORDER_X
- clientOrigin
.x
;
447 y0
+= ptOrigin
.y
+ BOX_BORDER_Y
- clientOrigin
.y
;
452 int count
= GetCount();
453 for ( int n
= 0; n
< count
; n
++ )
455 m_buttons
[n
]->SetSize(x
, y
, sizeBtn
.x
, sizeBtn
.y
);
457 if ( GetWindowStyle() & wxRA_TOPTOBOTTOM
)
459 // from top to bottom
460 if ( (n
+ 1) % m_numRows
)
462 // continue in this column
467 // start a new column
472 else // wxRA_LEFTTORIGHT: mirror the code above
474 // from left to right
475 if ( (n
+ 1) % m_numCols
)
477 // continue in this row
490 // ----------------------------------------------------------------------------
491 // keyboard navigation
492 // ----------------------------------------------------------------------------
494 bool wxRadioBox::OnKeyDown(wxKeyEvent
& event
)
497 switch ( event
.GetKeyCode() )
519 int selOld
= GetSelection();
520 int selNew
= GetNextItem(selOld
, dir
, GetWindowStyle());
521 if ( selNew
!= selOld
)
523 SetSelection(selNew
);
525 // emulate the button click
532 #endif // wxUSE_RADIOBOX