]>
git.saurik.com Git - wxWidgets.git/blob - src/osx/radiobox_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/radiobox_osx.cpp
4 // Author: Stefan Csomor
5 // Modified by: JS Lair (99/11/15) first implementation
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/radiobox.h"
18 #include "wx/radiobut.h"
19 #include "wx/arrstr.h"
22 #include "wx/osx/private.h"
24 // regarding layout: note that there are differences in inter-control
25 // spacing between InterfaceBuild and the Human Interface Guidelines, we stick
26 // to the latter, as those are also used eg in the System Preferences Dialogs
28 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
31 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
32 EVT_RADIOBUTTON( wxID_ANY
, wxRadioBox::OnRadioButton
)
36 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
38 if ( outer
.IsChecked() )
40 wxCommandEvent
event( wxEVT_RADIOBOX
, m_windowId
);
41 int i
= GetSelection() ;
43 event
.SetString(GetString(i
));
44 event
.SetEventObject( this );
45 ProcessCommand(event
);
49 wxRadioBox::wxRadioBox()
53 m_radioButtonCycle
= NULL
;
56 wxRadioBox::~wxRadioBox()
60 wxRadioButton
*next
, *current
;
62 current
= m_radioButtonCycle
->NextInCycle();
65 while (current
!= m_radioButtonCycle
)
67 next
= current
->NextInCycle();
77 // Create the radiobox for two-step construction
79 bool wxRadioBox::Create( wxWindow
*parent
,
80 wxWindowID id
, const wxString
& label
,
81 const wxPoint
& pos
, const wxSize
& size
,
82 const wxArrayString
& choices
,
83 int majorDim
, long style
,
84 const wxValidator
& val
, const wxString
& name
)
86 wxCArrayString
chs(choices
);
89 parent
, id
, label
, pos
, size
, chs
.GetCount(),
90 chs
.GetStrings(), majorDim
, style
, val
, name
);
93 bool wxRadioBox::Create( wxWindow
*parent
,
94 wxWindowID id
, const wxString
& label
,
95 const wxPoint
& pos
, const wxSize
& size
,
96 int n
, const wxString choices
[],
97 int majorDim
, long style
,
98 const wxValidator
& val
, const wxString
& name
)
102 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, val
, name
) )
105 // during construction we must keep this at 0, otherwise GetBestSize fails
107 m_noRowsOrCols
= majorDim
;
108 m_radioButtonCycle
= NULL
;
110 SetMajorDim( majorDim
== 0 ? n
: majorDim
, style
);
112 m_labelOrig
= m_label
= label
;
114 SetPeer(wxWidgetImpl::CreateGroupBox( this, parent
, id
, label
, pos
, size
, style
, GetExtraStyle() ));
116 for (int i
= 0; i
< n
; i
++)
118 wxRadioButton
*radBtn
= new wxRadioButton(
121 GetLabelText(choices
[i
]),
122 wxPoint( 5, 20 * i
+ 10 ),
124 i
== 0 ? wxRB_GROUP
: 0 );
127 m_radioButtonCycle
= radBtn
;
128 // m_radioButtonCycle = radBtn->AddInCycle( m_radioButtonCycle );
131 // as all radiobuttons have been set-up, set the correct dimensions
132 m_noItems
= (unsigned int)n
;
133 SetMajorDim( majorDim
== 0 ? n
: majorDim
, style
);
136 InvalidateBestSize();
137 SetInitialSize( size
);
138 MacPostControlCreate( pos
, size
);
143 // Enables or disables the entire radiobox
145 bool wxRadioBox::Enable(bool enable
)
147 wxRadioButton
*current
;
149 if (!wxControl::Enable( enable
))
152 current
= m_radioButtonCycle
;
153 for (unsigned int i
= 0; i
< m_noItems
; i
++)
155 current
->Enable( enable
);
156 current
= current
->NextInCycle();
162 // Enables or disables an given button
164 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
166 if (!IsValid( item
))
170 wxRadioButton
*current
= m_radioButtonCycle
;
174 current
= current
->NextInCycle();
177 return current
->Enable( enable
);
180 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
182 if (!IsValid( item
))
186 wxRadioButton
*current
= m_radioButtonCycle
;
190 current
= current
->NextInCycle();
193 return current
->IsEnabled();
196 // Returns the radiobox label
198 wxString
wxRadioBox::GetLabel() const
200 return wxControl::GetLabel();
203 // Returns the label for the given button
205 wxString
wxRadioBox::GetString(unsigned int item
) const
207 wxRadioButton
*current
;
209 if (!IsValid( item
))
210 return wxEmptyString
;
213 current
= m_radioButtonCycle
;
217 current
= current
->NextInCycle();
220 return current
->GetLabel();
223 // Returns the zero-based position of the selected button
225 int wxRadioBox::GetSelection() const
228 wxRadioButton
*current
;
231 current
= m_radioButtonCycle
;
232 while (!current
->GetValue())
235 current
= current
->NextInCycle();
241 // Sets the radiobox label
243 void wxRadioBox::SetLabel(const wxString
& label
)
245 return wxControl::SetLabel( label
);
248 // Sets the label of a given button
250 void wxRadioBox::SetString(unsigned int item
,const wxString
& label
)
252 if (!IsValid( item
))
256 wxRadioButton
*current
= m_radioButtonCycle
;
260 current
= current
->NextInCycle();
263 return current
->SetLabel( label
);
266 // Sets a button by passing the desired position. This does not cause
267 // wxEVT_RADIOBOX event to get emitted
269 void wxRadioBox::SetSelection(int item
)
272 wxRadioButton
*current
;
274 if (!IsValid( item
))
278 current
= m_radioButtonCycle
;
282 current
= current
->NextInCycle();
285 current
->SetValue( true );
288 // Shows or hides the entire radiobox
290 bool wxRadioBox::Show(bool show
)
292 wxRadioButton
*current
;
294 current
= m_radioButtonCycle
;
295 for (unsigned int i
=0; i
<m_noItems
; i
++)
297 current
->Show( show
);
298 current
= current
->NextInCycle();
301 wxControl::Show( show
);
306 // Shows or hides the given button
308 bool wxRadioBox::Show(unsigned int item
, bool show
)
310 if (!IsValid( item
))
314 wxRadioButton
*current
= m_radioButtonCycle
;
318 current
= current
->NextInCycle();
321 return current
->Show( show
);
324 bool wxRadioBox::IsItemShown(unsigned int item
) const
326 if (!IsValid( item
))
330 wxRadioButton
*current
= m_radioButtonCycle
;
334 current
= current
->NextInCycle();
337 return current
->IsShown();
341 // Simulates the effect of the user issuing a command to the item
343 void wxRadioBox::Command( wxCommandEvent
& event
)
345 SetSelection( event
.GetInt() );
346 ProcessCommand( event
);
349 // Sets the selected button to receive keyboard input
351 void wxRadioBox::SetFocus()
353 wxRadioButton
*current
;
355 current
= m_radioButtonCycle
;
356 while (!current
->GetValue())
358 current
= current
->NextInCycle();
364 // Simulates the effect of the user issuing a command to the item
367 #define RADIO_SIZE 20
369 // Cocoa has an additional border are of about 3 pixels
370 #define RADIO_SIZE 23
373 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
376 wxRadioButton
*current
;
378 // define the position
380 int x_current
, y_current
;
381 int x_offset
, y_offset
;
382 int widthOld
, heightOld
;
384 GetSize( &widthOld
, &heightOld
);
385 GetPosition( &x_current
, &y_current
);
389 if (!(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
391 if (x
== wxDefaultCoord
)
392 x_offset
= x_current
;
393 if (y
== wxDefaultCoord
)
394 y_offset
= y_current
;
398 int charWidth
, charHeight
;
399 int maxWidth
, maxHeight
;
400 int eachWidth
[128], eachHeight
[128];
401 int totWidth
, totHeight
;
404 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
405 &charWidth
, &charHeight
);
411 wxSize bestSizeRadio
;
412 if ( m_radioButtonCycle
)
413 bestSizeRadio
= m_radioButtonCycle
->GetBestSize();
415 for (unsigned int i
= 0 ; i
< m_noItems
; i
++)
417 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
] );
418 eachWidth
[i
] = eachWidth
[i
] + RADIO_SIZE
;
419 eachHeight
[i
] = wxMax( eachHeight
[i
], bestSizeRadio
.y
);
421 if (maxWidth
< eachWidth
[i
])
422 maxWidth
= eachWidth
[i
];
423 if (maxHeight
< eachHeight
[i
])
424 maxHeight
= eachHeight
[i
];
427 // according to HIG (official space - 3 Pixels Diff between Frame and Layout size)
429 if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI
)
432 totHeight
= GetRowCount() * maxHeight
+ (GetRowCount() - 1) * space
;
433 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
435 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
437 // change the width / height only when specified
438 if ( width
== wxDefaultCoord
)
440 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
446 if ( height
== wxDefaultCoord
)
448 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
454 wxControl::DoSetSize( x_offset
, y_offset
, width
, height
, wxSIZE_AUTO
);
456 // arrange radio buttons
457 int x_start
, y_start
;
459 x_start
= ( width
- sz
.x
) / 2;
460 y_start
= ( height
- sz
.y
) / 2;
465 current
= m_radioButtonCycle
;
466 for (i
= 0 ; i
< (int)m_noItems
; i
++)
468 // not to do for the zero button!
469 if ((i
> 0) && ((i
% GetMajorDim()) == 0))
471 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
473 x_offset
+= maxWidth
+ charWidth
;
479 y_offset
+= maxHeight
+ space
;
483 current
->SetSize( x_offset
, y_offset
, eachWidth
[i
], eachHeight
[i
] );
484 current
= current
->NextInCycle();
486 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
487 y_offset
+= maxHeight
+ space
;
489 x_offset
+= maxWidth
+ charWidth
;
493 wxSize
wxRadioBox::DoGetBestSize() const
495 int charWidth
, charHeight
;
496 int maxWidth
, maxHeight
;
497 int eachWidth
, eachHeight
;
498 int totWidth
, totHeight
;
500 wxFont font
= GetFont(); // GetParent()->GetFont()
502 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
503 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
510 wxSize bestSizeRadio
;
511 if ( m_radioButtonCycle
)
512 bestSizeRadio
= m_radioButtonCycle
->GetBestSize();
514 for (unsigned int i
= 0 ; i
< m_noItems
; i
++)
516 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
, NULL
, NULL
, &font
);
517 eachWidth
= (eachWidth
+ RADIO_SIZE
);
518 eachHeight
= wxMax(eachHeight
, bestSizeRadio
.y
);
519 if (maxWidth
< eachWidth
)
520 maxWidth
= eachWidth
;
521 if (maxHeight
< eachHeight
)
522 maxHeight
= eachHeight
;
525 // according to HIG (official space - 3 Pixels Diff between Frame and Layout size)
527 if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI
)
530 totHeight
= GetRowCount() * maxHeight
+ (GetRowCount() - 1) * space
;
531 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
533 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) );
537 // optimum size is an additional 5 pt border to all sides
541 // handle radio box title as well
542 GetTextExtent( GetLabel(), &eachWidth
, NULL
);
543 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
544 if (totWidth
< eachWidth
)
545 totWidth
= eachWidth
;
547 return wxSize( totWidth
, totHeight
);
550 bool wxRadioBox::SetFont(const wxFont
& font
)
552 bool retval
= wxWindowBase::SetFont( font
);
554 // dont' update the native control, it has its own small font
556 // should we iterate over the children ?
561 #endif // wxUSE_RADIOBOX