1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/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/arrstr.h"
17 #include "wx/radiobox.h"
18 #include "wx/radiobut.h"
19 #include "wx/mac/uma.h"
21 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
24 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
25 EVT_RADIOBUTTON( wxID_ANY
, wxRadioBox::OnRadioButton
)
29 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
31 if ( outer
.IsChecked() )
33 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
34 int i
= GetSelection() ;
36 event
.SetString( GetString( i
) );
37 event
.SetEventObject( this );
38 ProcessCommand( event
);
42 wxRadioBox::wxRadioBox()
46 m_radioButtonCycle
= NULL
;
49 wxRadioBox::~wxRadioBox()
51 m_isBeingDeleted
= true;
53 wxRadioButton
*next
, *current
;
55 current
= m_radioButtonCycle
->NextInCycle();
58 while (current
!= m_radioButtonCycle
)
60 next
= current
->NextInCycle();
70 // Create the radiobox for two-step construction
72 bool wxRadioBox::Create( wxWindow
*parent
,
73 wxWindowID id
, const wxString
& label
,
74 const wxPoint
& pos
, const wxSize
& size
,
75 const wxArrayString
& choices
,
76 int majorDim
, long style
,
77 const wxValidator
& val
, const wxString
& name
)
79 wxCArrayString
chs(choices
);
82 parent
, id
, label
, pos
, size
, chs
.GetCount(),
83 chs
.GetStrings(), majorDim
, style
, val
, name
);
86 bool wxRadioBox::Create( wxWindow
*parent
,
87 wxWindowID id
, const wxString
& label
,
88 const wxPoint
& pos
, const wxSize
& size
,
89 int n
, const wxString choices
[],
90 int majorDim
, long style
,
91 const wxValidator
& val
, const wxString
& name
)
93 m_macIsUserPane
= false ;
95 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, val
, name
) )
100 m_noItems
= (size_t)n
;
101 m_noRowsOrCols
= majorDim
;
102 m_radioButtonCycle
= NULL
;
104 SetMajorDim( majorDim
== 0 ? n
: majorDim
, style
);
108 Rect bounds
= wxMacGetBoundsForControl( this, pos
, size
);
109 if ( bounds
.right
<= bounds
.left
)
110 bounds
.right
= bounds
.left
+ 100;
111 if ( bounds
.bottom
<= bounds
.top
)
112 bounds
.bottom
= bounds
.top
+ 100;
114 m_peer
= new wxMacControl( this );
116 OSStatus err
= CreateGroupBoxControl(
117 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),
118 &bounds
, CFSTR("") , true /*primary*/,
119 m_peer
->GetControlRefAddr() );
122 for (i
= 0; i
< n
; i
++)
124 wxRadioButton
*radBtn
= new wxRadioButton(
127 wxStripMenuCodes(choices
[i
]),
128 wxPoint( 5, 20 * i
+ 10 ),
130 i
== 0 ? wxRB_GROUP
: 0 );
133 m_radioButtonCycle
= radBtn
;
134 // m_radioButtonCycle = radBtn->AddInCycle( m_radioButtonCycle );
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 (size_t 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(int item
, bool enable
)
167 wxRadioButton
*current
;
169 if (!IsValid( item
))
173 current
= m_radioButtonCycle
;
177 current
= current
->NextInCycle();
180 return current
->Enable( enable
);
183 // Returns the radiobox label
185 wxString
wxRadioBox::GetLabel() const
187 return wxControl::GetLabel();
190 // Returns the label for the given button
192 wxString
wxRadioBox::GetString(int item
) const
195 wxRadioButton
*current
;
197 if (!IsValid( item
))
198 return wxEmptyString
;
201 current
= m_radioButtonCycle
;
205 current
= current
->NextInCycle();
208 return current
->GetLabel();
211 // Returns the zero-based position of the selected button
213 int wxRadioBox::GetSelection() const
216 wxRadioButton
*current
;
219 current
= m_radioButtonCycle
;
220 while (!current
->GetValue())
223 current
= current
->NextInCycle();
229 // Sets the radiobox label
231 void wxRadioBox::SetLabel(const wxString
& label
)
233 return wxControl::SetLabel( label
);
236 // Sets the label of a given button
238 void wxRadioBox::SetString(int item
,const wxString
& label
)
241 wxRadioButton
*current
;
243 if (!IsValid( item
))
247 current
= m_radioButtonCycle
;
251 current
= current
->NextInCycle();
254 return current
->SetLabel( label
);
257 // Sets a button by passing the desired position. This does not cause
258 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
260 void wxRadioBox::SetSelection(int item
)
263 wxRadioButton
*current
;
265 if (!IsValid( item
))
269 current
= m_radioButtonCycle
;
273 current
= current
->NextInCycle();
276 current
->SetValue( true );
279 // Shows or hides the entire radiobox
281 bool wxRadioBox::Show(bool show
)
283 wxRadioButton
*current
;
285 current
= m_radioButtonCycle
;
286 for (size_t i
=0; i
<m_noItems
; i
++)
288 current
->Show( show
);
289 current
= current
->NextInCycle();
292 wxControl::Show( show
);
297 // Shows or hides the given button
299 bool wxRadioBox::Show(int item
, bool show
)
302 wxRadioButton
*current
;
304 if (!IsValid( item
))
308 current
= m_radioButtonCycle
;
312 current
= current
->NextInCycle();
315 return current
->Show( show
);
318 // Simulates the effect of the user issuing a command to the item
320 void wxRadioBox::Command( wxCommandEvent
& event
)
322 SetSelection( event
.GetInt() );
323 ProcessCommand( event
);
326 // Sets the selected button to receive keyboard input
328 void wxRadioBox::SetFocus()
331 wxRadioButton
*current
;
334 current
= m_radioButtonCycle
;
335 while (!current
->GetValue())
338 current
= current
->NextInCycle();
344 // Simulates the effect of the user issuing a command to the item
346 #define RADIO_SIZE 20
348 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
351 wxRadioButton
*current
;
353 // define the position
355 int x_current
, y_current
;
356 int x_offset
, y_offset
;
357 int widthOld
, heightOld
;
359 GetSize( &widthOld
, &heightOld
);
360 GetPosition( &x_current
, &y_current
);
364 if (!(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
366 if (x
== wxDefaultCoord
)
367 x_offset
= x_current
;
368 if (y
== wxDefaultCoord
)
369 y_offset
= y_current
;
373 int charWidth
, charHeight
;
374 int maxWidth
, maxHeight
;
375 int eachWidth
[128], eachHeight
[128];
376 int totWidth
, totHeight
;
379 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
380 &charWidth
, &charHeight
);
386 for (size_t i
= 0 ; i
< m_noItems
; i
++)
388 GetTextExtent( GetString( i
), &eachWidth
[i
], &eachHeight
[i
] );
389 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
390 eachHeight
[i
] = (int)((3 * eachHeight
[i
]) / 2);
392 if (maxWidth
< eachWidth
[i
])
393 maxWidth
= eachWidth
[i
];
394 if (maxHeight
< eachHeight
[i
])
395 maxHeight
= eachHeight
[i
];
398 totHeight
= GetRowCount() * maxHeight
;
399 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
401 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
403 // change the width / height only when specified
404 if ( width
== wxDefaultCoord
)
406 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
412 if ( height
== wxDefaultCoord
)
414 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
420 wxControl::DoSetSize( x_offset
, y_offset
, width
, height
, wxSIZE_AUTO
);
422 // arrange radio buttons
423 int x_start
, y_start
;
431 current
= m_radioButtonCycle
;
432 for (i
= 0 ; i
< (int)m_noItems
; i
++)
434 // not to do for the zero button!
435 if ((i
> 0) && ((i
% GetMajorDim()) == 0))
437 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
439 x_offset
+= maxWidth
+ charWidth
;
445 y_offset
+= maxHeight
; //+ charHeight / 2
449 current
->SetSize( x_offset
, y_offset
, eachWidth
[i
], eachHeight
[i
]);
450 current
= current
->NextInCycle();
452 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
453 y_offset
+= maxHeight
; // + charHeight / 2
455 x_offset
+= maxWidth
+ charWidth
;
459 wxSize
wxRadioBox::DoGetBestSize() const
461 int charWidth
, charHeight
;
462 int maxWidth
, maxHeight
;
463 int eachWidth
, eachHeight
;
464 int totWidth
, totHeight
;
466 wxFont font
= GetFont(); // GetParent()->GetFont()
468 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
469 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
476 for (size_t i
= 0 ; i
< m_noItems
; i
++)
478 GetTextExtent( GetString( i
), &eachWidth
, &eachHeight
, NULL
, NULL
, &font
);
479 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
);
480 eachHeight
= (int)((3 * eachHeight
) / 2);
481 if (maxWidth
< eachWidth
)
482 maxWidth
= eachWidth
;
483 if (maxHeight
< eachHeight
)
484 maxHeight
= eachHeight
;
487 totHeight
= GetRowCount() * maxHeight
;
488 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
490 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) );
494 // handle radio box title as well
495 GetTextExtent( GetLabel(), &eachWidth
, NULL
);
496 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
497 if (totWidth
< eachWidth
)
498 totWidth
= eachWidth
;
500 return wxSize( totWidth
, totHeight
);
503 #endif // wxUSE_RADIOBOX