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
= (unsigned int)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 (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 // Returns the radiobox label
182 wxString
wxRadioBox::GetLabel() const
184 return wxControl::GetLabel();
187 // Returns the label for the given button
189 wxString
wxRadioBox::GetString(unsigned int item
) const
191 wxRadioButton
*current
;
193 if (!IsValid( item
))
194 return wxEmptyString
;
197 current
= m_radioButtonCycle
;
201 current
= current
->NextInCycle();
204 return current
->GetLabel();
207 // Returns the zero-based position of the selected button
209 int wxRadioBox::GetSelection() const
212 wxRadioButton
*current
;
215 current
= m_radioButtonCycle
;
216 while (!current
->GetValue())
219 current
= current
->NextInCycle();
225 // Sets the radiobox label
227 void wxRadioBox::SetLabel(const wxString
& label
)
229 return wxControl::SetLabel( label
);
232 // Sets the label of a given button
234 void wxRadioBox::SetString(unsigned int item
,const wxString
& label
)
236 if (!IsValid( item
))
240 wxRadioButton
*current
= m_radioButtonCycle
;
244 current
= current
->NextInCycle();
247 return current
->SetLabel( label
);
250 // Sets a button by passing the desired position. This does not cause
251 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
253 void wxRadioBox::SetSelection(int item
)
256 wxRadioButton
*current
;
258 if (!IsValid( item
))
262 current
= m_radioButtonCycle
;
266 current
= current
->NextInCycle();
269 current
->SetValue( true );
272 // Shows or hides the entire radiobox
274 bool wxRadioBox::Show(bool show
)
276 wxRadioButton
*current
;
278 current
= m_radioButtonCycle
;
279 for (unsigned int i
=0; i
<m_noItems
; i
++)
281 current
->Show( show
);
282 current
= current
->NextInCycle();
285 wxControl::Show( show
);
290 // Shows or hides the given button
292 bool wxRadioBox::Show(unsigned int item
, bool show
)
294 if (!IsValid( item
))
298 wxRadioButton
*current
= m_radioButtonCycle
;
302 current
= current
->NextInCycle();
305 return current
->Show( show
);
308 // Simulates the effect of the user issuing a command to the item
310 void wxRadioBox::Command( wxCommandEvent
& event
)
312 SetSelection( event
.GetInt() );
313 ProcessCommand( event
);
316 // Sets the selected button to receive keyboard input
318 void wxRadioBox::SetFocus()
321 wxRadioButton
*current
;
324 current
= m_radioButtonCycle
;
325 while (!current
->GetValue())
328 current
= current
->NextInCycle();
334 // Simulates the effect of the user issuing a command to the item
336 #define RADIO_SIZE 20
338 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
341 wxRadioButton
*current
;
343 // define the position
345 int x_current
, y_current
;
346 int x_offset
, y_offset
;
347 int widthOld
, heightOld
;
349 GetSize( &widthOld
, &heightOld
);
350 GetPosition( &x_current
, &y_current
);
354 if (!(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
356 if (x
== wxDefaultCoord
)
357 x_offset
= x_current
;
358 if (y
== wxDefaultCoord
)
359 y_offset
= y_current
;
363 int charWidth
, charHeight
;
364 int maxWidth
, maxHeight
;
365 int eachWidth
[128], eachHeight
[128];
366 int totWidth
, totHeight
;
369 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
370 &charWidth
, &charHeight
);
376 for (unsigned int i
= 0 ; i
< m_noItems
; i
++)
378 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
] );
379 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
380 eachHeight
[i
] = (int)((3 * eachHeight
[i
]) / 2);
382 if (maxWidth
< eachWidth
[i
])
383 maxWidth
= eachWidth
[i
];
384 if (maxHeight
< eachHeight
[i
])
385 maxHeight
= eachHeight
[i
];
388 totHeight
= GetRowCount() * maxHeight
;
389 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
391 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
393 // change the width / height only when specified
394 if ( width
== wxDefaultCoord
)
396 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
402 if ( height
== wxDefaultCoord
)
404 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
410 wxControl::DoSetSize( x_offset
, y_offset
, width
, height
, wxSIZE_AUTO
);
412 // arrange radio buttons
413 int x_start
, y_start
;
421 current
= m_radioButtonCycle
;
422 for (i
= 0 ; i
< (int)m_noItems
; i
++)
424 // not to do for the zero button!
425 if ((i
> 0) && ((i
% GetMajorDim()) == 0))
427 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
429 x_offset
+= maxWidth
+ charWidth
;
435 y_offset
+= maxHeight
; //+ charHeight / 2
439 current
->SetSize( x_offset
, y_offset
, eachWidth
[i
], eachHeight
[i
]);
440 current
= current
->NextInCycle();
442 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
443 y_offset
+= maxHeight
; // + charHeight / 2
445 x_offset
+= maxWidth
+ charWidth
;
449 wxSize
wxRadioBox::DoGetBestSize() const
451 int charWidth
, charHeight
;
452 int maxWidth
, maxHeight
;
453 int eachWidth
, eachHeight
;
454 int totWidth
, totHeight
;
456 wxFont font
= GetFont(); // GetParent()->GetFont()
458 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
459 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
466 for (unsigned int i
= 0 ; i
< m_noItems
; i
++)
468 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
, NULL
, NULL
, &font
);
469 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
);
470 eachHeight
= (int)((3 * eachHeight
) / 2);
471 if (maxWidth
< eachWidth
)
472 maxWidth
= eachWidth
;
473 if (maxHeight
< eachHeight
)
474 maxHeight
= eachHeight
;
477 totHeight
= GetRowCount() * maxHeight
;
478 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
480 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) );
484 // handle radio box title as well
485 GetTextExtent( GetLabel(), &eachWidth
, NULL
);
486 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
487 if (totWidth
< eachWidth
)
488 totWidth
= eachWidth
;
490 return wxSize( totWidth
, totHeight
);
493 #endif // wxUSE_RADIOBOX