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
) )
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
)
152 wxRadioButton
*current
;
154 if (!wxControl::Enable( enable
))
157 current
= m_radioButtonCycle
;
158 for (i
= 0; i
< m_noItems
; i
++)
160 current
->Enable( enable
);
161 current
= current
->NextInCycle();
167 // Enables or disables an given button
169 bool wxRadioBox::Enable(int item
, bool enable
)
172 wxRadioButton
*current
;
174 if (!IsValid( item
))
178 current
= m_radioButtonCycle
;
182 current
= current
->NextInCycle();
185 return current
->Enable( enable
);
188 // Returns the radiobox label
190 wxString
wxRadioBox::GetLabel() const
192 return wxControl::GetLabel();
195 // Returns the label for the given button
197 wxString
wxRadioBox::GetString(int item
) const
200 wxRadioButton
*current
;
202 if (!IsValid( item
))
203 return wxEmptyString
;
206 current
= m_radioButtonCycle
;
210 current
= current
->NextInCycle();
213 return current
->GetLabel();
216 // Returns the zero-based position of the selected button
218 int wxRadioBox::GetSelection() const
221 wxRadioButton
*current
;
224 current
= m_radioButtonCycle
;
225 while (!current
->GetValue())
228 current
= current
->NextInCycle();
234 // Sets the radiobox label
236 void wxRadioBox::SetLabel(const wxString
& label
)
238 return wxControl::SetLabel( label
);
241 // Sets the label of a given button
243 void wxRadioBox::SetString(int item
,const wxString
& label
)
246 wxRadioButton
*current
;
248 if (!IsValid( item
))
252 current
= m_radioButtonCycle
;
256 current
= current
->NextInCycle();
259 return current
->SetLabel( label
);
262 // Sets a button by passing the desired position. This does not cause
263 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
265 void wxRadioBox::SetSelection(int item
)
268 wxRadioButton
*current
;
270 if (!IsValid( item
))
274 current
= m_radioButtonCycle
;
278 current
= current
->NextInCycle();
281 current
->SetValue( true );
284 // Shows or hides the entire radiobox
286 bool wxRadioBox::Show(bool show
)
289 wxRadioButton
*current
;
291 current
= m_radioButtonCycle
;
292 for (i
=0; i
<m_noItems
; i
++)
294 current
->Show( show
);
295 current
= current
->NextInCycle();
298 wxControl::Show( show
);
303 // Shows or hides the given button
305 bool wxRadioBox::Show(int item
, bool show
)
308 wxRadioButton
*current
;
310 if (!IsValid( item
))
314 current
= m_radioButtonCycle
;
318 current
= current
->NextInCycle();
321 return current
->Show( show
);
324 // Simulates the effect of the user issuing a command to the item
326 void wxRadioBox::Command( wxCommandEvent
& event
)
328 SetSelection( event
.GetInt() );
329 ProcessCommand( event
);
332 // Sets the selected button to receive keyboard input
334 void wxRadioBox::SetFocus()
337 wxRadioButton
*current
;
340 current
= m_radioButtonCycle
;
341 while (!current
->GetValue())
344 current
= current
->NextInCycle();
350 // Simulates the effect of the user issuing a command to the item
352 #define RADIO_SIZE 20
354 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
357 wxRadioButton
*current
;
359 // define the position
361 int x_current
, y_current
;
362 int x_offset
, y_offset
;
363 int widthOld
, heightOld
;
365 GetSize( &widthOld
, &heightOld
);
366 GetPosition( &x_current
, &y_current
);
370 if (!(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
372 if (x
== wxDefaultCoord
)
373 x_offset
= x_current
;
374 if (y
== wxDefaultCoord
)
375 y_offset
= y_current
;
379 int charWidth
, charHeight
;
380 int maxWidth
, maxHeight
;
381 int eachWidth
[128], eachHeight
[128];
382 int totWidth
, totHeight
;
385 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
386 &charWidth
, &charHeight
);
392 for (i
= 0 ; i
< m_noItems
; i
++)
394 GetTextExtent( GetString( i
), &eachWidth
[i
], &eachHeight
[i
] );
395 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
396 eachHeight
[i
] = (int)((3 * eachHeight
[i
]) / 2);
398 if (maxWidth
< eachWidth
[i
])
399 maxWidth
= eachWidth
[i
];
400 if (maxHeight
< eachHeight
[i
])
401 maxHeight
= eachHeight
[i
];
404 totHeight
= GetRowCount() * maxHeight
;
405 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
407 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
409 // change the width / height only when specified
410 if ( width
== wxDefaultCoord
)
412 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
418 if ( height
== wxDefaultCoord
)
420 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
426 wxControl::DoSetSize( x_offset
, y_offset
, width
, height
, wxSIZE_AUTO
);
428 // arrange radio buttons
429 int x_start
, y_start
;
437 current
= m_radioButtonCycle
;
438 for ( i
= 0 ; i
< m_noItems
; i
++)
440 // not to do for the zero button!
441 if ((i
> 0) && ((i
% GetMajorDim()) == 0))
443 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
445 x_offset
+= maxWidth
+ charWidth
;
451 y_offset
+= maxHeight
; //+ charHeight / 2
455 current
->SetSize( x_offset
, y_offset
, eachWidth
[i
], eachHeight
[i
]);
456 current
= current
->NextInCycle();
458 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
459 y_offset
+= maxHeight
; // + charHeight / 2
461 x_offset
+= maxWidth
+ charWidth
;
465 wxSize
wxRadioBox::DoGetBestSize() const
467 int charWidth
, charHeight
;
468 int maxWidth
, maxHeight
;
469 int eachWidth
, eachHeight
;
470 int totWidth
, totHeight
;
472 wxFont font
= GetFont(); // GetParent()->GetFont()
474 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
475 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
482 for (int i
= 0 ; i
< m_noItems
; i
++)
484 GetTextExtent( GetString( i
), &eachWidth
, &eachHeight
, NULL
, NULL
, &font
);
485 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
);
486 eachHeight
= (int)((3 * eachHeight
) / 2);
487 if (maxWidth
< eachWidth
)
488 maxWidth
= eachWidth
;
489 if (maxHeight
< eachHeight
)
490 maxHeight
= eachHeight
;
493 totHeight
= GetRowCount() * maxHeight
;
494 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
);
496 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) );
500 // handle radio box title as well
501 GetTextExtent( GetLabel(), &eachWidth
, NULL
);
502 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
503 if (totWidth
< eachWidth
)
504 totWidth
= eachWidth
;
506 return wxSize( totWidth
, totHeight
);