1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/radiobox.cpp
4 // Author: Stefan Csomor
5 // Modified by: JS Lair (99/11/15) first implementation
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/radiobox.h"
19 #include "wx/radiobut.h"
20 #include "wx/arrstr.h"
23 #include "wx/osx/private.h"
25 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
28 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
29 EVT_RADIOBUTTON( wxID_ANY
, wxRadioBox::OnRadioButton
)
33 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
35 if ( outer
.IsChecked() )
37 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
38 int i
= GetSelection() ;
40 event
.SetString(GetString(i
));
41 event
.SetEventObject( this );
42 ProcessCommand(event
);
46 wxRadioBox::wxRadioBox()
50 m_radioButtonCycle
= NULL
;
53 wxRadioBox::~wxRadioBox()
57 wxRadioButton
*next
, *current
;
59 current
= m_radioButtonCycle
->NextInCycle();
62 while (current
!= m_radioButtonCycle
)
64 next
= current
->NextInCycle();
74 // Create the radiobox for two-step construction
76 bool wxRadioBox::Create( wxWindow
*parent
,
77 wxWindowID id
, const wxString
& label
,
78 const wxPoint
& pos
, const wxSize
& size
,
79 const wxArrayString
& choices
,
80 int majorDim
, long style
,
81 const wxValidator
& val
, const wxString
& name
)
83 wxCArrayString
chs(choices
);
86 parent
, id
, label
, pos
, size
, chs
.GetCount(),
87 chs
.GetStrings(), majorDim
, style
, val
, name
);
90 bool wxRadioBox::Create( wxWindow
*parent
,
91 wxWindowID id
, const wxString
& label
,
92 const wxPoint
& pos
, const wxSize
& size
,
93 int n
, const wxString choices
[],
94 int majorDim
, long style
,
95 const wxValidator
& val
, const wxString
& name
)
97 m_macIsUserPane
= false ;
99 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, val
, name
) )
104 m_noItems
= (unsigned int)n
;
105 m_noRowsOrCols
= majorDim
;
106 m_radioButtonCycle
= NULL
;
108 SetMajorDim( majorDim
== 0 ? n
: majorDim
, style
);
110 m_labelOrig
= m_label
= label
;
112 m_peer
= wxWidgetImpl::CreateGroupBox( this, parent
, id
, label
, pos
, size
, style
, GetExtraStyle() );
114 for (i
= 0; i
< n
; i
++)
116 wxRadioButton
*radBtn
= new wxRadioButton(
119 GetLabelText(choices
[i
]),
120 wxPoint( 5, 20 * i
+ 10 ),
122 i
== 0 ? wxRB_GROUP
: 0 );
125 m_radioButtonCycle
= radBtn
;
126 // m_radioButtonCycle = radBtn->AddInCycle( m_radioButtonCycle );
130 MacPostControlCreate( pos
, size
);
135 // Enables or disables the entire radiobox
137 bool wxRadioBox::Enable(bool enable
)
139 wxRadioButton
*current
;
141 if (!wxControl::Enable( enable
))
144 current
= m_radioButtonCycle
;
145 for (unsigned int i
= 0; i
< m_noItems
; i
++)
147 current
->Enable( enable
);
148 current
= current
->NextInCycle();
154 // Enables or disables an given button
156 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
158 if (!IsValid( item
))
162 wxRadioButton
*current
= m_radioButtonCycle
;
166 current
= current
->NextInCycle();
169 return current
->Enable( enable
);
172 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
174 if (!IsValid( item
))
178 wxRadioButton
*current
= m_radioButtonCycle
;
182 current
= current
->NextInCycle();
185 return current
->IsEnabled();
188 // Returns the radiobox label
190 wxString
wxRadioBox::GetLabel() const
192 return wxControl::GetLabel();
195 // Returns the label for the given button
197 wxString
wxRadioBox::GetString(unsigned int item
) const
199 wxRadioButton
*current
;
201 if (!IsValid( item
))
202 return wxEmptyString
;
205 current
= m_radioButtonCycle
;
209 current
= current
->NextInCycle();
212 return current
->GetLabel();
215 // Returns the zero-based position of the selected button
217 int wxRadioBox::GetSelection() const
220 wxRadioButton
*current
;
223 current
= m_radioButtonCycle
;
224 while (!current
->GetValue())
227 current
= current
->NextInCycle();
233 // Sets the radiobox label
235 void wxRadioBox::SetLabel(const wxString
& label
)
237 return wxControl::SetLabel( label
);
240 // Sets the label of a given button
242 void wxRadioBox::SetString(unsigned int item
,const wxString
& label
)
244 if (!IsValid( item
))
248 wxRadioButton
*current
= m_radioButtonCycle
;
252 current
= current
->NextInCycle();
255 return current
->SetLabel( label
);
258 // Sets a button by passing the desired position. This does not cause
259 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
261 void wxRadioBox::SetSelection(int item
)
264 wxRadioButton
*current
;
266 if (!IsValid( item
))
270 current
= m_radioButtonCycle
;
274 current
= current
->NextInCycle();
277 current
->SetValue( true );
280 // Shows or hides the entire radiobox
282 bool wxRadioBox::Show(bool show
)
284 wxRadioButton
*current
;
286 current
= m_radioButtonCycle
;
287 for (unsigned int i
=0; i
<m_noItems
; i
++)
289 current
->Show( show
);
290 current
= current
->NextInCycle();
293 wxControl::Show( show
);
298 // Shows or hides the given button
300 bool wxRadioBox::Show(unsigned int item
, bool show
)
302 if (!IsValid( item
))
306 wxRadioButton
*current
= m_radioButtonCycle
;
310 current
= current
->NextInCycle();
313 return current
->Show( show
);
316 bool wxRadioBox::IsItemShown(unsigned int item
) const
318 if (!IsValid( item
))
322 wxRadioButton
*current
= m_radioButtonCycle
;
326 current
= current
->NextInCycle();
329 return current
->IsShown();
333 // Simulates the effect of the user issuing a command to the item
335 void wxRadioBox::Command( wxCommandEvent
& event
)
337 SetSelection( event
.GetInt() );
338 ProcessCommand( event
);
341 // Sets the selected button to receive keyboard input
343 void wxRadioBox::SetFocus()
345 wxRadioButton
*current
;
347 current
= m_radioButtonCycle
;
348 while (!current
->GetValue())
350 current
= current
->NextInCycle();
356 // Simulates the effect of the user issuing a command to the item
358 #define RADIO_SIZE 20
360 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
363 wxRadioButton
*current
;
365 // define the position
367 int x_current
, y_current
;
368 int x_offset
, y_offset
;
369 int widthOld
, heightOld
;
371 GetSize( &widthOld
, &heightOld
);
372 GetPosition( &x_current
, &y_current
);
376 if (!(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
378 if (x
== wxDefaultCoord
)
379 x_offset
= x_current
;
380 if (y
== wxDefaultCoord
)
381 y_offset
= y_current
;
385 int charWidth
, charHeight
;
386 int maxWidth
, maxHeight
;
387 int eachWidth
[128], eachHeight
[128];
388 int totWidth
, totHeight
;
391 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
392 &charWidth
, &charHeight
);
398 for (unsigned int i
= 0 ; i
< m_noItems
; i
++)
400 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
] );
401 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
402 eachHeight
[i
] = (int) eachHeight
[i
];
404 if (maxWidth
< eachWidth
[i
])
405 maxWidth
= eachWidth
[i
];
406 if (maxHeight
< eachHeight
[i
])
407 maxHeight
= eachHeight
[i
];
410 totHeight
= GetRowCount() * maxHeight
+ (GetRowCount() - 1) * maxHeight
/ 2;
411 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
413 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
415 // change the width / height only when specified
416 if ( width
== wxDefaultCoord
)
418 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
424 if ( height
== wxDefaultCoord
)
426 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
432 wxControl::DoSetSize( x_offset
, y_offset
, width
, height
, wxSIZE_AUTO
);
434 // arrange radio buttons
435 int x_start
, y_start
;
437 x_start
= ( width
- sz
.x
) / 2;
438 y_start
= ( height
- sz
.y
) / 2;
443 current
= m_radioButtonCycle
;
444 for (i
= 0 ; i
< (int)m_noItems
; i
++)
446 // not to do for the zero button!
447 if ((i
> 0) && ((i
% GetMajorDim()) == 0))
449 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
451 x_offset
+= maxWidth
+ charWidth
;
457 y_offset
+= 3 * maxHeight
/ 2 ; //+ charHeight / 2
461 current
->SetSize( x_offset
, y_offset
, eachWidth
[i
], eachHeight
[i
]);
462 current
= current
->NextInCycle();
464 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
465 y_offset
+= 3 * maxHeight
/ 2 ; // + charHeight / 2
467 x_offset
+= maxWidth
+ charWidth
;
471 wxSize
wxRadioBox::DoGetBestSize() const
473 int charWidth
, charHeight
;
474 int maxWidth
, maxHeight
;
475 int eachWidth
, eachHeight
;
476 int totWidth
, totHeight
;
478 wxFont font
= GetFont(); // GetParent()->GetFont()
480 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
481 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
488 for (unsigned int i
= 0 ; i
< m_noItems
; i
++)
490 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
, NULL
, NULL
, &font
);
491 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
);
492 eachHeight
= (int)eachHeight
;
493 if (maxWidth
< eachWidth
)
494 maxWidth
= eachWidth
;
495 if (maxHeight
< eachHeight
)
496 maxHeight
= eachHeight
;
499 totHeight
= GetRowCount() * maxHeight
+ (GetRowCount() - 1) * maxHeight
/ 2;
500 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
502 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) );
506 // optimum size is an additional 5 pt border to all sides
510 // handle radio box title as well
511 GetTextExtent( GetLabel(), &eachWidth
, NULL
);
512 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
513 if (totWidth
< eachWidth
)
514 totWidth
= eachWidth
;
516 return wxSize( totWidth
, totHeight
);
519 bool wxRadioBox::SetFont(const wxFont
& font
)
521 bool retval
= wxWindowBase::SetFont( font
);
523 // dont' update the native control, it has its own small font
528 #endif // wxUSE_RADIOBOX