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 //-------------------------------------------------------------------------------------
14 //-------------------------------------------------------------------------------------
16 #include "wx/wxprec.h"
20 #include "wx/arrstr.h"
21 #include "wx/radiobox.h"
22 #include "wx/radiobut.h"
23 #include "wx/mac/uma.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()
55 m_isBeingDeleted
= true;
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
= (size_t)n
;
105 m_noRowsOrCols
= majorDim
;
106 m_radioButtonCycle
= NULL
;
108 SetMajorDim( majorDim
== 0 ? n
: majorDim
, style
);
112 Rect bounds
= wxMacGetBoundsForControl( this, pos
, size
);
113 if ( bounds
.right
<= bounds
.left
)
114 bounds
.right
= bounds
.left
+ 100;
115 if ( bounds
.bottom
<= bounds
.top
)
116 bounds
.bottom
= bounds
.top
+ 100;
118 m_peer
= new wxMacControl( this );
120 OSStatus err
= CreateGroupBoxControl(
121 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),
122 &bounds
, CFSTR("") , true /*primary*/,
123 m_peer
->GetControlRefAddr() );
126 for (i
= 0; i
< n
; i
++)
128 wxRadioButton
*radBtn
= new wxRadioButton(
131 wxStripMenuCodes(choices
[i
]),
132 wxPoint( 5, 20 * i
+ 10 ),
134 i
== 0 ? wxRB_GROUP
: 0 );
137 m_radioButtonCycle
= radBtn
;
138 // m_radioButtonCycle = radBtn->AddInCycle( m_radioButtonCycle );
142 MacPostControlCreate( pos
, size
);
147 // Enables or disables the entire radiobox
149 bool wxRadioBox::Enable(bool enable
)
151 wxRadioButton
*current
;
153 if (!wxControl::Enable( enable
))
156 current
= m_radioButtonCycle
;
157 for (size_t i
= 0; i
< m_noItems
; i
++)
159 current
->Enable( enable
);
160 current
= current
->NextInCycle();
166 // Enables or disables an given button
168 bool wxRadioBox::Enable(int item
, bool enable
)
171 wxRadioButton
*current
;
173 if (!IsValid( item
))
177 current
= m_radioButtonCycle
;
181 current
= current
->NextInCycle();
184 return current
->Enable( enable
);
187 // Returns the radiobox label
189 wxString
wxRadioBox::GetLabel() const
191 return wxControl::GetLabel();
194 // Returns the label for the given button
196 wxString
wxRadioBox::GetString(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(int item
,const wxString
& label
)
245 wxRadioButton
*current
;
247 if (!IsValid( item
))
251 current
= m_radioButtonCycle
;
255 current
= current
->NextInCycle();
258 return current
->SetLabel( label
);
261 // Sets a button by passing the desired position. This does not cause
262 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
264 void wxRadioBox::SetSelection(int item
)
267 wxRadioButton
*current
;
269 if (!IsValid( item
))
273 current
= m_radioButtonCycle
;
277 current
= current
->NextInCycle();
280 current
->SetValue( true );
283 // Shows or hides the entire radiobox
285 bool wxRadioBox::Show(bool show
)
287 wxRadioButton
*current
;
289 current
= m_radioButtonCycle
;
290 for (size_t i
=0; i
<m_noItems
; i
++)
292 current
->Show( show
);
293 current
= current
->NextInCycle();
296 wxControl::Show( show
);
301 // Shows or hides the given button
303 bool wxRadioBox::Show(int item
, bool show
)
306 wxRadioButton
*current
;
308 if (!IsValid( item
))
312 current
= m_radioButtonCycle
;
316 current
= current
->NextInCycle();
319 return current
->Show( show
);
322 // Simulates the effect of the user issuing a command to the item
324 void wxRadioBox::Command( wxCommandEvent
& event
)
326 SetSelection( event
.GetInt() );
327 ProcessCommand( event
);
330 // Sets the selected button to receive keyboard input
332 void wxRadioBox::SetFocus()
335 wxRadioButton
*current
;
338 current
= m_radioButtonCycle
;
339 while (!current
->GetValue())
342 current
= current
->NextInCycle();
348 // Simulates the effect of the user issuing a command to the item
350 #define RADIO_SIZE 20
352 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
355 wxRadioButton
*current
;
357 // define the position
359 int x_current
, y_current
;
360 int x_offset
, y_offset
;
361 int widthOld
, heightOld
;
363 GetSize( &widthOld
, &heightOld
);
364 GetPosition( &x_current
, &y_current
);
368 if (!(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
370 if (x
== wxDefaultCoord
)
371 x_offset
= x_current
;
372 if (y
== wxDefaultCoord
)
373 y_offset
= y_current
;
377 int charWidth
, charHeight
;
378 int maxWidth
, maxHeight
;
379 int eachWidth
[128], eachHeight
[128];
380 int totWidth
, totHeight
;
383 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
384 &charWidth
, &charHeight
);
390 for (size_t i
= 0 ; i
< m_noItems
; i
++)
392 GetTextExtent( GetString( i
), &eachWidth
[i
], &eachHeight
[i
] );
393 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
394 eachHeight
[i
] = (int)((3 * eachHeight
[i
]) / 2);
396 if (maxWidth
< eachWidth
[i
])
397 maxWidth
= eachWidth
[i
];
398 if (maxHeight
< eachHeight
[i
])
399 maxHeight
= eachHeight
[i
];
402 totHeight
= GetRowCount() * maxHeight
;
403 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
405 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
407 // change the width / height only when specified
408 if ( width
== wxDefaultCoord
)
410 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
416 if ( height
== wxDefaultCoord
)
418 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
424 wxControl::DoSetSize( x_offset
, y_offset
, width
, height
, wxSIZE_AUTO
);
426 // arrange radio buttons
427 int x_start
, y_start
;
435 current
= m_radioButtonCycle
;
436 for ( i
= 0 ; i
< m_noItems
; i
++)
438 // not to do for the zero button!
439 if ((i
> 0) && ((i
% GetMajorDim()) == 0))
441 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
443 x_offset
+= maxWidth
+ charWidth
;
449 y_offset
+= maxHeight
; //+ charHeight / 2
453 current
->SetSize( x_offset
, y_offset
, eachWidth
[i
], eachHeight
[i
]);
454 current
= current
->NextInCycle();
456 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
457 y_offset
+= maxHeight
; // + charHeight / 2
459 x_offset
+= maxWidth
+ charWidth
;
463 wxSize
wxRadioBox::DoGetBestSize() const
465 int charWidth
, charHeight
;
466 int maxWidth
, maxHeight
;
467 int eachWidth
, eachHeight
;
468 int totWidth
, totHeight
;
470 wxFont font
= GetFont(); // GetParent()->GetFont()
472 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
473 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
480 for (size_t i
= 0 ; i
< m_noItems
; i
++)
482 GetTextExtent( GetString( i
), &eachWidth
, &eachHeight
, NULL
, NULL
, &font
);
483 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
);
484 eachHeight
= (int)((3 * eachHeight
) / 2);
485 if (maxWidth
< eachWidth
)
486 maxWidth
= eachWidth
;
487 if (maxHeight
< eachHeight
)
488 maxHeight
= eachHeight
;
491 totHeight
= GetRowCount() * maxHeight
;
492 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
494 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) );
498 // handle radio box title as well
499 GetTextExtent( GetLabel(), &eachWidth
, NULL
);
500 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
501 if (totWidth
< eachWidth
)
502 totWidth
= eachWidth
;
504 return wxSize( totWidth
, totHeight
);
507 #endif // wxUSE_RADIOBOX