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"
22 #include "wx/radiobox.h"
23 #include "wx/radiobut.h"
24 #include "wx/mac/uma.h"
26 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
28 //-------------------------------------------------------------------------------------
30 //-------------------------------------------------------------------------------------
31 // Default constructor
32 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
33 EVT_RADIOBUTTON( wxID_ANY
, wxRadioBox::OnRadioButton
)
36 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
38 if ( outer
.IsChecked() )
40 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
41 int i
= GetSelection() ;
43 event
.SetString( GetString( i
) );
44 event
.SetEventObject( this );
45 ProcessCommand(event
);
49 wxRadioBox::wxRadioBox()
53 m_radioButtonCycle
= NULL
;
56 //-------------------------------------------------------------------------------------
57 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
58 // const wxSize&, int, const wxString[], int, long,
59 // const wxValidator&, const wxString&)
60 //-------------------------------------------------------------------------------------
61 // Contructor, creating and showing a radiobox
66 //-------------------------------------------------------------------------------------
68 //-------------------------------------------------------------------------------------
69 // Destructor, destroying the radiobox item
71 wxRadioBox::~wxRadioBox()
73 m_isBeingDeleted
= true;
75 wxRadioButton
*next
,*current
;
77 current
=m_radioButtonCycle
->NextInCycle();
78 next
=current
->NextInCycle();
79 while (current
!=m_radioButtonCycle
) {
82 next
=current
->NextInCycle();
87 //-------------------------------------------------------------------------------------
89 //-------------------------------------------------------------------------------------
90 // Create the radiobox for two-step construction
92 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
93 const wxPoint
& pos
, const wxSize
& size
,
94 const wxArrayString
& choices
,
95 int majorDim
, long style
,
96 const wxValidator
& val
, const wxString
& name
)
98 wxCArrayString
chs(choices
);
100 return Create(parent
, id
, label
, pos
, size
, chs
.GetCount(),
101 chs
.GetStrings(), majorDim
, style
, val
, name
);
104 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
105 const wxPoint
& pos
, const wxSize
& size
,
106 int n
, const wxString choices
[],
107 int majorDim
, long style
,
108 const wxValidator
& val
, const wxString
& name
)
110 m_macIsUserPane
= false ;
112 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
118 m_noRowsOrCols
= majorDim
;
119 m_radioButtonCycle
= NULL
;
121 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
125 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
126 if( bounds
.right
<= bounds
.left
)
127 bounds
.right
= bounds
.left
+ 100 ;
128 if ( bounds
.bottom
<= bounds
.top
)
129 bounds
.bottom
= bounds
.top
+ 100 ;
131 m_peer
= new wxMacControl(this) ;
133 verify_noerr(CreateGroupBoxControl(MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),&bounds
, CFSTR("") ,
134 true /*primary*/ , m_peer
->GetControlRefAddr() ) ) ;
136 for (i
= 0; i
< n
; i
++)
138 wxRadioButton
*radBtn
= new wxRadioButton
142 wxStripMenuCodes(choices
[i
]),
145 i
== 0 ? wxRB_GROUP
: 0
148 m_radioButtonCycle
= radBtn
;
149 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
153 MacPostControlCreate(pos
,size
) ;
159 //-------------------------------------------------------------------------------------
161 //-------------------------------------------------------------------------------------
162 // Enables or disables the entire radiobox
164 bool wxRadioBox::Enable(bool enable
)
167 wxRadioButton
*current
;
169 if (!wxControl::Enable(enable
))
172 current
= m_radioButtonCycle
;
173 for (i
= 0; i
< m_noItems
; i
++) {
174 current
->Enable(enable
);
175 current
= current
->NextInCycle();
180 //-------------------------------------------------------------------------------------
181 // ¥ Enable(int, bool)
182 //-------------------------------------------------------------------------------------
183 // Enables or disables an given button
185 bool wxRadioBox::Enable(int item
, bool enable
)
188 wxRadioButton
*current
;
194 current
= m_radioButtonCycle
;
197 current
= current
->NextInCycle();
199 return current
->Enable(enable
);
202 //-------------------------------------------------------------------------------------
204 //-------------------------------------------------------------------------------------
205 // Returns the radiobox label
207 wxString
wxRadioBox::GetLabel() const
209 return wxControl::GetLabel();
212 //-------------------------------------------------------------------------------------
214 //-------------------------------------------------------------------------------------
215 // Returns the label for the given button
217 wxString
wxRadioBox::GetString(int item
) const
220 wxRadioButton
*current
;
223 return wxEmptyString
;
226 current
= m_radioButtonCycle
;
229 current
= current
->NextInCycle();
231 return current
->GetLabel();
234 //-------------------------------------------------------------------------------------
236 //-------------------------------------------------------------------------------------
237 // Returns the zero-based position of the selected button
239 int wxRadioBox::GetSelection() const
242 wxRadioButton
*current
;
245 current
=m_radioButtonCycle
;
246 while (!current
->GetValue()) {
248 current
=current
->NextInCycle();
254 //-------------------------------------------------------------------------------------
256 //-------------------------------------------------------------------------------------
257 // Returns the number of buttons in the radiobox
262 //-------------------------------------------------------------------------------------
263 // ¥ SetLabel(const wxString&)
264 //-------------------------------------------------------------------------------------
265 // Sets the radiobox label
267 void wxRadioBox::SetLabel(const wxString
& label
)
269 return wxControl::SetLabel(label
);
272 //-------------------------------------------------------------------------------------
273 // ¥ SetLabel(int, const wxString&)
274 //-------------------------------------------------------------------------------------
275 // Sets the label of a given button
277 void wxRadioBox::SetString(int item
,const wxString
& label
)
280 wxRadioButton
*current
;
285 current
=m_radioButtonCycle
;
288 current
=current
->NextInCycle();
290 return current
->SetLabel(label
);
293 //-------------------------------------------------------------------------------------
295 //-------------------------------------------------------------------------------------
296 // Sets a button by passing the desired position. This does not cause
297 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
299 void wxRadioBox::SetSelection(int item
)
302 wxRadioButton
*current
;
307 current
=m_radioButtonCycle
;
310 current
=current
->NextInCycle();
312 current
->SetValue(true);
316 //-------------------------------------------------------------------------------------
318 //-------------------------------------------------------------------------------------
319 // Shows or hides the entire radiobox
321 bool wxRadioBox::Show(bool show
)
324 wxRadioButton
*current
;
326 wxControl::Show(show
);
328 current
=m_radioButtonCycle
;
329 for (i
=0;i
<m_noItems
;i
++)
332 current
=current
->NextInCycle();
337 //-------------------------------------------------------------------------------------
339 //-------------------------------------------------------------------------------------
340 // Shows or hides the given button
342 bool wxRadioBox::Show(int item
, bool show
)
345 wxRadioButton
*current
;
350 current
=m_radioButtonCycle
;
353 current
=current
->NextInCycle();
355 return current
->Show(show
);
358 //-------------------------------------------------------------------------------------
360 //-------------------------------------------------------------------------------------
361 // Simulates the effect of the user issuing a command to the item
363 void wxRadioBox::Command (wxCommandEvent
& event
)
365 SetSelection (event
.GetInt());
366 ProcessCommand (event
);
369 //-------------------------------------------------------------------------------------
371 //-------------------------------------------------------------------------------------
372 // Sets the selected button to receive keyboard input
374 void wxRadioBox::SetFocus()
377 wxRadioButton
*current
;
380 current
=m_radioButtonCycle
;
381 while (!current
->GetValue()) {
383 current
=current
->NextInCycle();
389 //-------------------------------------------------------------------------------------
391 //-------------------------------------------------------------------------------------
392 // Simulates the effect of the user issuing a command to the item
394 #define RADIO_SIZE 20
396 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
399 wxRadioButton
*current
;
401 // define the position
403 int x_current
, y_current
;
404 int x_offset
,y_offset
;
405 int widthOld
, heightOld
;
406 GetSize(&widthOld
, &heightOld
);
410 GetPosition(&x_current
, &y_current
);
411 if ((x
== wxDefaultCoord
) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
412 x_offset
= x_current
;
413 if ((y
== wxDefaultCoord
)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
414 y_offset
= y_current
;
418 int charWidth
,charHeight
;
419 int maxWidth
,maxHeight
;
420 int eachWidth
[128],eachHeight
[128];
421 int totWidth
,totHeight
;
423 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
429 for (i
= 0 ; i
< m_noItems
; i
++)
431 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
432 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
433 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
434 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
435 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
438 totHeight
= GetRowCount() * ( maxHeight
) ;
439 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
441 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
443 // only change our width/height if asked for
444 if ( width
== wxDefaultCoord
)
446 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
452 if ( height
== wxDefaultCoord
)
454 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
460 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
462 // arrange radiobuttons
473 current
=m_radioButtonCycle
;
474 for ( i
= 0 ; i
< m_noItems
; i
++)
476 if (i
&&((i%GetMajorDim
())==0)) // not to do for the zero button!
478 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
480 x_offset
+= maxWidth
+ charWidth
;
486 y_offset
+= maxHeight
; /*+ charHeight/2;*/
490 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
491 current
=current
->NextInCycle();
493 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
494 y_offset
+= maxHeight
; /*+ charHeight/2;*/
496 x_offset
+= maxWidth
+ charWidth
;
500 wxSize
wxRadioBox::DoGetBestSize() const
502 int charWidth
, charHeight
;
503 int maxWidth
, maxHeight
;
504 int eachWidth
, eachHeight
;
505 int totWidth
, totHeight
;
507 wxFont font
= /*GetParent()->*/GetFont();
508 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
509 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
516 for (int i
= 0 ; i
< m_noItems
; i
++)
518 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
,NULL
, NULL
, &font
);
519 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
520 eachHeight
= (int)((3 * eachHeight
) / 2);
521 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
522 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
525 totHeight
= GetRowCount() * (maxHeight
) ;
526 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
528 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
532 // handle radio box title as well
533 GetTextExtent(GetLabel(), &eachWidth
, NULL
);
534 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
535 if (totWidth
< eachWidth
)
536 totWidth
= eachWidth
;
538 return wxSize(totWidth
, totHeight
);