1 /////////////////////////////////////////////////////////////////////////////
5 // Modified by: JS Lair (99/11/15) first implementation
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 //-------------------------------------------------------------------------------------
14 //-------------------------------------------------------------------------------------
17 #pragma implementation "radiobox.h"
20 #include "wx/radiobox.h"
21 #include <wx/mac/uma.h>
23 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
26 #pragma mark ### Constructors & destructor ###
28 //-------------------------------------------------------------------------------------
30 //-------------------------------------------------------------------------------------
31 // Default constructor
33 wxRadioBox::wxRadioBox()
38 m_radioButtonCycle
= NULL
;
41 //-------------------------------------------------------------------------------------
42 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
43 // const wxSize&, int, const wxString[], int, long,
44 // const wxValidator&, const wxString&)
45 //-------------------------------------------------------------------------------------
46 // Contructor, creating and showing a radiobox
51 //-------------------------------------------------------------------------------------
53 //-------------------------------------------------------------------------------------
54 // Destructor, destroying the radiobox item
56 wxRadioBox::~wxRadioBox()
58 wxRadioButton
*next
,*current
;
60 current
=m_radioButtonCycle
->NextInCycle();
61 next
=current
->NextInCycle();
62 while (current
!=m_radioButtonCycle
) {
65 next
=current
->NextInCycle();
70 //-------------------------------------------------------------------------------------
72 //-------------------------------------------------------------------------------------
73 // Create the radiobox for two-step construction
75 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
76 const wxPoint
& pos
, const wxSize
& size
,
77 int n
, const wxString choices
[],
78 int majorDim
, long style
,
79 const wxValidator
& val
, const wxString
& name
)
84 m_noRowsOrCols
= majorDim
;
85 m_radioButtonCycle
= NULL
;
90 m_majorDim
= majorDim
;
96 MacPreControlCreate( parent
, id
, label
, pos
, size
,style
, *((wxValidator
*)NULL
) , name
, &bounds
, title
) ;
98 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, true , 0 , 0 , 1,
99 kControlGroupBoxTextTitleProc
, (long) this ) ;
101 MacPostControlCreate() ;
103 for (i
= 0; i
< n
; i
++)
105 wxRadioButton
*radBtn
= new wxRadioButton(this, NewControlId(),choices
[i
],wxPoint(5,20*i
+10));
106 m_radioButtonCycle
=radBtn
->AddInCycle(m_radioButtonCycle
);
110 SetSize(pos
.x
,pos
.y
,size
.x
,size
.y
);
117 #pragma mark ### Specific functions (reference v2) ###
119 //-------------------------------------------------------------------------------------
121 //-------------------------------------------------------------------------------------
122 // Enables or disables the entire radiobox
124 bool wxRadioBox::Enable(bool enable
)
127 wxRadioButton
*current
;
129 if (!wxControl::Enable(enable
))
132 current
=m_radioButtonCycle
;
133 for (i
=0;i
<m_noItems
;i
++) {
134 current
->Enable(enable
);
135 current
=current
->NextInCycle();
140 //-------------------------------------------------------------------------------------
141 // ¥ Enable(int, bool)
142 //-------------------------------------------------------------------------------------
143 // Enables or disables an given button
145 void wxRadioBox::Enable(int item
, bool enable
)
148 wxRadioButton
*current
;
150 if ((item
< 0) || (item
>= m_noItems
))
153 current
=m_radioButtonCycle
;
156 current
=current
->NextInCycle();
161 //-------------------------------------------------------------------------------------
163 //-------------------------------------------------------------------------------------
164 // Finds a button matching the given string, returning the position if found
165 // or -1 if not found
167 int wxRadioBox::FindString(const wxString
& s
) const
170 wxRadioButton
*current
;
172 current
=m_radioButtonCycle
;
173 for (i
= 0; i
< m_noItems
; i
++)
175 if (s
== current
->GetLabel())
177 current
=current
->NextInCycle();
182 //-------------------------------------------------------------------------------------
184 //-------------------------------------------------------------------------------------
185 // Returns the radiobox label
187 wxString
wxRadioBox::GetLabel() const
189 return wxControl::GetLabel();
192 //-------------------------------------------------------------------------------------
194 //-------------------------------------------------------------------------------------
195 // Returns the label for the given button
197 wxString
wxRadioBox::GetLabel(int item
) const
200 wxRadioButton
*current
;
202 if ((item
< 0) || (item
>= m_noItems
))
205 current
=m_radioButtonCycle
;
208 current
=current
->NextInCycle();
210 return current
->GetLabel();
213 //-------------------------------------------------------------------------------------
215 //-------------------------------------------------------------------------------------
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()) {
227 current
=current
->NextInCycle();
233 //-------------------------------------------------------------------------------------
235 //-------------------------------------------------------------------------------------
236 // Find string for position
238 wxString
wxRadioBox::GetString(int item
) const
241 return GetLabel(item
);
244 //-------------------------------------------------------------------------------------
245 // ¥ GetStringSelection
246 //-------------------------------------------------------------------------------------
247 // Returns the selected string
249 wxString
wxRadioBox::GetStringSelection () const
251 int sel
= GetSelection ();
253 return this->GetString (sel
);
258 //-------------------------------------------------------------------------------------
260 //-------------------------------------------------------------------------------------
261 // Returns the number of buttons in the radiobox
266 //-------------------------------------------------------------------------------------
267 // ¥ SetLabel(const wxString&)
268 //-------------------------------------------------------------------------------------
269 // Sets the radiobox label
271 void wxRadioBox::SetLabel(const wxString
& label
)
273 return wxControl::SetLabel(label
);
276 //-------------------------------------------------------------------------------------
277 // ¥ SetLabel(int, const wxString&)
278 //-------------------------------------------------------------------------------------
279 // Sets the label of a given button
281 void wxRadioBox::SetLabel(int item
,const wxString
& label
)
284 wxRadioButton
*current
;
286 if ((item
< 0) || (item
>= m_noItems
))
289 current
=m_radioButtonCycle
;
292 current
=current
->NextInCycle();
294 return current
->SetLabel(label
);
297 //-------------------------------------------------------------------------------------
299 //-------------------------------------------------------------------------------------
300 // Sets a button by passing the desired position. This does not cause
301 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
303 void wxRadioBox::SetSelection(int item
)
306 wxRadioButton
*current
;
308 if ((item
< 0) || (item
>= m_noItems
))
311 current
=m_radioButtonCycle
;
314 current
=current
->NextInCycle();
316 current
->SetValue(true);
320 //-------------------------------------------------------------------------------------
321 // ¥ SetStringSelection
322 //-------------------------------------------------------------------------------------
323 // Sets a button by passing the desired string. This does not cause
324 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
326 bool wxRadioBox::SetStringSelection (const wxString
& s
)
328 int sel
= FindString (s
);
338 //-------------------------------------------------------------------------------------
340 //-------------------------------------------------------------------------------------
341 // Shows or hides the entire radiobox
343 bool wxRadioBox::Show(bool show
)
346 wxRadioButton
*current
;
348 wxControl::Show(show
);
350 current
=m_radioButtonCycle
;
351 for (i
=0;i
<m_noItems
;i
++) {
353 current
=current
->NextInCycle();
358 //-------------------------------------------------------------------------------------
360 //-------------------------------------------------------------------------------------
361 // Shows or hides the given button
363 void wxRadioBox::Show(int item
, bool show
)
366 wxRadioButton
*current
;
368 if ((item
< 0) || (item
>= m_noItems
))
371 current
=m_radioButtonCycle
;
374 current
=current
->NextInCycle();
380 #pragma mark ### Other external functions ###
382 //-------------------------------------------------------------------------------------
384 //-------------------------------------------------------------------------------------
385 // Simulates the effect of the user issuing a command to the item
387 void wxRadioBox::Command (wxCommandEvent
& event
)
389 SetSelection (event
.GetInt());
390 ProcessCommand (event
);
393 //-------------------------------------------------------------------------------------
395 //-------------------------------------------------------------------------------------
396 // Sets the selected button to receive keyboard input
398 void wxRadioBox::SetFocus()
401 wxRadioButton
*current
;
404 current
=m_radioButtonCycle
;
405 while (!current
->GetValue()) {
407 current
=current
->NextInCycle();
414 #pragma mark ### Internal functions ###
416 //-------------------------------------------------------------------------------------
418 //-------------------------------------------------------------------------------------
419 // Simulates the effect of the user issuing a command to the item
421 #define RADIO_SIZE 20
423 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
426 wxRadioButton
*current
;
428 // define the position
430 int x_current
, y_current
;
431 int x_offset
,y_offset
;
435 GetPosition(&x_current
, &y_current
);
436 if ((x
== -1) || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
437 x_offset
= x_current
;
438 if ((y
== -1) || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
439 y_offset
= y_current
;
443 int charWidth
,charHeight
;
444 int maxWidth
,maxHeight
;
445 int eachWidth
[128],eachHeight
[128];
446 int totWidth
,totHeight
;
448 SetFont(GetParent()->GetFont());
449 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
454 for (i
= 0 ; i
< m_noItems
; i
++)
456 GetTextExtent(GetLabel(i
), &eachWidth
[i
], &eachHeight
[i
]);
457 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
458 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
459 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
460 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
463 totHeight
= GetNumVer() * (maxHeight
+ charHeight
/2) + charHeight
*3/2;
464 totWidth
= GetNumHor() * (maxWidth
+ charWidth
) + charWidth
;
466 wxControl::DoSetSize(x_offset
,y_offset
,totWidth
,totHeight
,wxSIZE_AUTO
);
468 // arrange radiobuttons
474 y_start
= charHeight
*3/2;
478 current
=m_radioButtonCycle
;
479 for ( i
= 0 ; i
< m_noItems
; i
++)
481 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
483 if (m_windowStyle
& wxRA_VERTICAL
)
485 x_offset
+= maxWidth
+ charWidth
;
491 y_offset
+= maxHeight
+ charHeight
/2;
495 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
496 current
=current
->NextInCycle();
498 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
499 y_offset
+= maxHeight
+ charHeight
/2;
501 x_offset
+= maxWidth
+ charWidth
;
505 //-------------------------------------------------------------------------------------
507 //-------------------------------------------------------------------------------------
508 // return the number of buttons in the vertical direction
510 int wxRadioBox::GetNumVer() const
512 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
518 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
522 //-------------------------------------------------------------------------------------
524 //-------------------------------------------------------------------------------------
525 // return the number of buttons in the horizontal direction
527 int wxRadioBox::GetNumHor() const
529 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
531 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;