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/radiobox.h"
18 #include "wx/arrstr.h"
19 #include "wx/radiobut.h"
20 #include "wx/mac/uma.h"
22 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
25 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
26 EVT_RADIOBUTTON( wxID_ANY
, wxRadioBox::OnRadioButton
)
30 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
32 if ( outer
.IsChecked() )
34 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
35 int i
= GetSelection() ;
37 event
.SetString(GetString(i
));
38 event
.SetEventObject( this );
39 ProcessCommand(event
);
43 wxRadioBox::wxRadioBox()
47 m_radioButtonCycle
= NULL
;
50 wxRadioBox::~wxRadioBox()
52 m_isBeingDeleted
= true;
54 wxRadioButton
*next
, *current
;
56 current
= m_radioButtonCycle
->NextInCycle();
59 while (current
!= m_radioButtonCycle
)
61 next
= current
->NextInCycle();
71 // Create the radiobox for two-step construction
73 bool wxRadioBox::Create( wxWindow
*parent
,
74 wxWindowID id
, const wxString
& label
,
75 const wxPoint
& pos
, const wxSize
& size
,
76 const wxArrayString
& choices
,
77 int majorDim
, long style
,
78 const wxValidator
& val
, const wxString
& name
)
80 wxCArrayString
chs(choices
);
83 parent
, id
, label
, pos
, size
, chs
.GetCount(),
84 chs
.GetStrings(), majorDim
, style
, val
, name
);
87 bool wxRadioBox::Create( wxWindow
*parent
,
88 wxWindowID id
, const wxString
& label
,
89 const wxPoint
& pos
, const wxSize
& size
,
90 int n
, const wxString choices
[],
91 int majorDim
, long style
,
92 const wxValidator
& val
, const wxString
& name
)
94 m_macIsUserPane
= false ;
96 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, val
, name
) )
101 m_noItems
= (unsigned int)n
;
102 m_noRowsOrCols
= majorDim
;
103 m_radioButtonCycle
= NULL
;
105 SetMajorDim( majorDim
== 0 ? n
: majorDim
, style
);
109 Rect bounds
= wxMacGetBoundsForControl( this, pos
, size
);
110 if ( bounds
.right
<= bounds
.left
)
111 bounds
.right
= bounds
.left
+ 100;
112 if ( bounds
.bottom
<= bounds
.top
)
113 bounds
.bottom
= bounds
.top
+ 100;
115 m_peer
= new wxMacControl( this );
117 OSStatus err
= CreateGroupBoxControl(
118 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),
119 &bounds
, CFSTR("") , true /*primary*/,
120 m_peer
->GetControlRefAddr() );
123 for (i
= 0; i
< n
; i
++)
125 wxRadioButton
*radBtn
= new wxRadioButton(
128 wxStripMenuCodes(choices
[i
]),
129 wxPoint( 5, 20 * i
+ 10 ),
131 i
== 0 ? wxRB_GROUP
: 0 );
134 m_radioButtonCycle
= radBtn
;
135 // m_radioButtonCycle = radBtn->AddInCycle( m_radioButtonCycle );
139 MacPostControlCreate( pos
, size
);
144 // Enables or disables the entire radiobox
146 bool wxRadioBox::Enable(bool enable
)
148 wxRadioButton
*current
;
150 if (!wxControl::Enable( enable
))
153 current
= m_radioButtonCycle
;
154 for (unsigned int i
= 0; i
< m_noItems
; i
++)
156 current
->Enable( enable
);
157 current
= current
->NextInCycle();
163 // Enables or disables an given button
165 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
167 if (!IsValid( item
))
171 wxRadioButton
*current
= m_radioButtonCycle
;
175 current
= current
->NextInCycle();
178 return current
->Enable( enable
);
181 // Returns the radiobox label
183 wxString
wxRadioBox::GetLabel() const
185 return wxControl::GetLabel();
188 // Returns the label for the given button
190 wxString
wxRadioBox::GetString(unsigned int item
) const
192 wxRadioButton
*current
;
194 if (!IsValid( item
))
195 return wxEmptyString
;
198 current
= m_radioButtonCycle
;
202 current
= current
->NextInCycle();
205 return current
->GetLabel();
208 // Returns the zero-based position of the selected button
210 int wxRadioBox::GetSelection() const
213 wxRadioButton
*current
;
216 current
= m_radioButtonCycle
;
217 while (!current
->GetValue())
220 current
= current
->NextInCycle();
226 // Sets the radiobox label
228 void wxRadioBox::SetLabel(const wxString
& label
)
230 return wxControl::SetLabel( label
);
233 // Sets the label of a given button
235 void wxRadioBox::SetString(unsigned int item
,const wxString
& label
)
237 if (!IsValid( item
))
241 wxRadioButton
*current
= m_radioButtonCycle
;
245 current
= current
->NextInCycle();
248 return current
->SetLabel( label
);
251 // Sets a button by passing the desired position. This does not cause
252 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
254 void wxRadioBox::SetSelection(int item
)
257 wxRadioButton
*current
;
259 if (!IsValid( item
))
263 current
= m_radioButtonCycle
;
267 current
= current
->NextInCycle();
270 current
->SetValue( true );
273 // Shows or hides the entire radiobox
275 bool wxRadioBox::Show(bool show
)
277 wxRadioButton
*current
;
279 current
= m_radioButtonCycle
;
280 for (unsigned int i
=0; i
<m_noItems
; i
++)
282 current
->Show( show
);
283 current
= current
->NextInCycle();
286 wxControl::Show( show
);
291 // Shows or hides the given button
293 bool wxRadioBox::Show(unsigned int item
, bool show
)
295 if (!IsValid( item
))
299 wxRadioButton
*current
= m_radioButtonCycle
;
303 current
= current
->NextInCycle();
306 return current
->Show( show
);
309 // Simulates the effect of the user issuing a command to the item
311 void wxRadioBox::Command( wxCommandEvent
& event
)
313 SetSelection( event
.GetInt() );
314 ProcessCommand( event
);
317 // Sets the selected button to receive keyboard input
319 void wxRadioBox::SetFocus()
322 wxRadioButton
*current
;
325 current
= m_radioButtonCycle
;
326 while (!current
->GetValue())
329 current
= current
->NextInCycle();
335 // Simulates the effect of the user issuing a command to the item
337 #define RADIO_SIZE 20
339 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
342 wxRadioButton
*current
;
344 // define the position
346 int x_current
, y_current
;
347 int x_offset
, y_offset
;
348 int widthOld
, heightOld
;
350 GetSize( &widthOld
, &heightOld
);
351 GetPosition( &x_current
, &y_current
);
355 if (!(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
357 if (x
== wxDefaultCoord
)
358 x_offset
= x_current
;
359 if (y
== wxDefaultCoord
)
360 y_offset
= y_current
;
364 int charWidth
, charHeight
;
365 int maxWidth
, maxHeight
;
366 int eachWidth
[128], eachHeight
[128];
367 int totWidth
, totHeight
;
370 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
371 &charWidth
, &charHeight
);
377 for (unsigned int i
= 0 ; i
< m_noItems
; i
++)
379 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
] );
380 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
381 eachHeight
[i
] = (int)((3 * eachHeight
[i
]) / 2);
383 if (maxWidth
< eachWidth
[i
])
384 maxWidth
= eachWidth
[i
];
385 if (maxHeight
< eachHeight
[i
])
386 maxHeight
= eachHeight
[i
];
389 totHeight
= GetRowCount() * maxHeight
;
390 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
392 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
394 // change the width / height only when specified
395 if ( width
== wxDefaultCoord
)
397 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
403 if ( height
== wxDefaultCoord
)
405 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
411 wxControl::DoSetSize( x_offset
, y_offset
, width
, height
, wxSIZE_AUTO
);
413 // arrange radio buttons
414 int x_start
, y_start
;
422 current
= m_radioButtonCycle
;
423 for (i
= 0 ; i
< (int)m_noItems
; i
++)
425 // not to do for the zero button!
426 if ((i
> 0) && ((i
% GetMajorDim()) == 0))
428 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
430 x_offset
+= maxWidth
+ charWidth
;
436 y_offset
+= maxHeight
; //+ charHeight / 2
440 current
->SetSize( x_offset
, y_offset
, eachWidth
[i
], eachHeight
[i
]);
441 current
= current
->NextInCycle();
443 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
444 y_offset
+= maxHeight
; // + charHeight / 2
446 x_offset
+= maxWidth
+ charWidth
;
450 wxSize
wxRadioBox::DoGetBestSize() const
452 int charWidth
, charHeight
;
453 int maxWidth
, maxHeight
;
454 int eachWidth
, eachHeight
;
455 int totWidth
, totHeight
;
457 wxFont font
= GetFont(); // GetParent()->GetFont()
459 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
460 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
467 for (unsigned int i
= 0 ; i
< m_noItems
; i
++)
469 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
, NULL
, NULL
, &font
);
470 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
);
471 eachHeight
= (int)((3 * eachHeight
) / 2);
472 if (maxWidth
< eachWidth
)
473 maxWidth
= eachWidth
;
474 if (maxHeight
< eachHeight
)
475 maxHeight
= eachHeight
;
478 totHeight
= GetRowCount() * maxHeight
;
479 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
481 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) );
485 // handle radio box title as well
486 GetTextExtent( GetLabel(), &eachWidth
, NULL
);
487 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
488 if (totWidth
< eachWidth
)
489 totWidth
= eachWidth
;
491 return wxSize( totWidth
, totHeight
);
494 #endif // wxUSE_RADIOBOX