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 #if !USE_SHARED_LIBRARY
24 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
28 #pragma mark ### Constructors & destructor ###
30 //-------------------------------------------------------------------------------------
32 //-------------------------------------------------------------------------------------
33 // Default constructor
35 wxRadioBox::wxRadioBox()
40 m_radioButtonCycle
= NULL
;
43 //-------------------------------------------------------------------------------------
44 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
45 // const wxSize&, int, const wxString[], int, long,
46 // const wxValidator&, const wxString&)
47 //-------------------------------------------------------------------------------------
48 // Contructor, creating and showing a radiobox
53 //-------------------------------------------------------------------------------------
55 //-------------------------------------------------------------------------------------
56 // Destructor, destroying the radiobox item
58 wxRadioBox::~wxRadioBox()
60 wxRadioButton
*next
,*current
;
62 current
=m_radioButtonCycle
->NextInCycle();
63 next
=current
->NextInCycle();
64 while (current
!=m_radioButtonCycle
) {
67 next
=current
->NextInCycle();
72 //-------------------------------------------------------------------------------------
74 //-------------------------------------------------------------------------------------
75 // Create the radiobox for two-step construction
77 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
78 const wxPoint
& pos
, const wxSize
& size
,
79 int n
, const wxString choices
[],
80 int majorDim
, long style
,
81 const wxValidator
& val
, const wxString
& name
)
86 m_noRowsOrCols
= majorDim
;
87 m_radioButtonCycle
= NULL
;
92 m_majorDim
= majorDim
;
98 MacPreControlCreate( parent
, id
, label
, pos
, size
,style
, *((wxValidator
*)NULL
) , name
, &bounds
, title
) ;
100 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, true , 0 , 0 , 1,
101 kControlGroupBoxTextTitleProc
, (long) this ) ;
103 MacPostControlCreate() ;
105 for (i
= 0; i
< n
; i
++)
107 wxRadioButton
*radBtn
= new wxRadioButton(this, NewControlId(),choices
[i
],wxPoint(5,20*i
+10));
108 m_radioButtonCycle
=radBtn
->AddInCycle(m_radioButtonCycle
);
112 SetSize(pos
.x
,pos
.y
,size
.x
,size
.y
);
119 #pragma mark ### Specific functions (reference v2) ###
121 //-------------------------------------------------------------------------------------
123 //-------------------------------------------------------------------------------------
124 // Enables or disables the entire radiobox
126 bool wxRadioBox::Enable(bool enable
)
129 wxRadioButton
*current
;
131 if (!wxControl::Enable(enable
))
134 current
=m_radioButtonCycle
;
135 for (i
=0;i
<m_noItems
;i
++) {
136 current
->Enable(enable
);
137 current
=current
->NextInCycle();
142 //-------------------------------------------------------------------------------------
143 // ¥ Enable(int, bool)
144 //-------------------------------------------------------------------------------------
145 // Enables or disables an given button
147 void wxRadioBox::Enable(int item
, bool enable
)
150 wxRadioButton
*current
;
152 if ((item
< 0) || (item
>= m_noItems
))
155 current
=m_radioButtonCycle
;
158 current
=current
->NextInCycle();
163 //-------------------------------------------------------------------------------------
165 //-------------------------------------------------------------------------------------
166 // Finds a button matching the given string, returning the position if found
167 // or -1 if not found
169 int wxRadioBox::FindString(const wxString
& s
) const
172 wxRadioButton
*current
;
174 current
=m_radioButtonCycle
;
175 for (i
= 0; i
< m_noItems
; i
++)
177 if (s
== current
->GetLabel())
179 current
=current
->NextInCycle();
184 //-------------------------------------------------------------------------------------
186 //-------------------------------------------------------------------------------------
187 // Returns the radiobox label
189 wxString
wxRadioBox::GetLabel() const
191 return wxControl::GetLabel();
194 //-------------------------------------------------------------------------------------
196 //-------------------------------------------------------------------------------------
197 // Returns the label for the given button
199 wxString
wxRadioBox::GetLabel(int item
) const
202 wxRadioButton
*current
;
204 if ((item
< 0) || (item
>= m_noItems
))
207 current
=m_radioButtonCycle
;
210 current
=current
->NextInCycle();
212 return current
->GetLabel();
215 //-------------------------------------------------------------------------------------
217 //-------------------------------------------------------------------------------------
218 // Returns the zero-based position of the selected button
220 int wxRadioBox::GetSelection() const
223 wxRadioButton
*current
;
226 current
=m_radioButtonCycle
;
227 while (!current
->GetValue()) {
229 current
=current
->NextInCycle();
235 //-------------------------------------------------------------------------------------
237 //-------------------------------------------------------------------------------------
238 // Find string for position
240 wxString
wxRadioBox::GetString(int item
) const
243 return GetLabel(item
);
246 //-------------------------------------------------------------------------------------
247 // ¥ GetStringSelection
248 //-------------------------------------------------------------------------------------
249 // Returns the selected string
251 wxString
wxRadioBox::GetStringSelection () const
253 int sel
= GetSelection ();
255 return this->GetString (sel
);
260 //-------------------------------------------------------------------------------------
262 //-------------------------------------------------------------------------------------
263 // Returns the number of buttons in the radiobox
268 //-------------------------------------------------------------------------------------
269 // ¥ SetLabel(const wxString&)
270 //-------------------------------------------------------------------------------------
271 // Sets the radiobox label
273 void wxRadioBox::SetLabel(const wxString
& label
)
275 return wxControl::SetLabel(label
);
278 //-------------------------------------------------------------------------------------
279 // ¥ SetLabel(int, const wxString&)
280 //-------------------------------------------------------------------------------------
281 // Sets the label of a given button
283 void wxRadioBox::SetLabel(int item
,const wxString
& label
)
286 wxRadioButton
*current
;
288 if ((item
< 0) || (item
>= m_noItems
))
291 current
=m_radioButtonCycle
;
294 current
=current
->NextInCycle();
296 return current
->SetLabel(label
);
299 //-------------------------------------------------------------------------------------
301 //-------------------------------------------------------------------------------------
302 // Sets a button by passing the desired position. This does not cause
303 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
305 void wxRadioBox::SetSelection(int item
)
308 wxRadioButton
*current
;
310 if ((item
< 0) || (item
>= m_noItems
))
313 current
=m_radioButtonCycle
;
316 current
=current
->NextInCycle();
318 current
->SetValue(true);
322 //-------------------------------------------------------------------------------------
323 // ¥ SetStringSelection
324 //-------------------------------------------------------------------------------------
325 // Sets a button by passing the desired string. This does not cause
326 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
328 bool wxRadioBox::SetStringSelection (const wxString
& s
)
330 int sel
= FindString (s
);
340 //-------------------------------------------------------------------------------------
342 //-------------------------------------------------------------------------------------
343 // Shows or hides the entire radiobox
345 bool wxRadioBox::Show(bool show
)
348 wxRadioButton
*current
;
350 wxControl::Show(show
);
352 current
=m_radioButtonCycle
;
353 for (i
=0;i
<m_noItems
;i
++) {
355 current
=current
->NextInCycle();
360 //-------------------------------------------------------------------------------------
362 //-------------------------------------------------------------------------------------
363 // Shows or hides the given button
365 void wxRadioBox::Show(int item
, bool show
)
368 wxRadioButton
*current
;
370 if ((item
< 0) || (item
>= m_noItems
))
373 current
=m_radioButtonCycle
;
376 current
=current
->NextInCycle();
382 #pragma mark ### Other external functions ###
384 //-------------------------------------------------------------------------------------
386 //-------------------------------------------------------------------------------------
387 // Simulates the effect of the user issuing a command to the item
389 void wxRadioBox::Command (wxCommandEvent
& event
)
391 SetSelection (event
.GetInt());
392 ProcessCommand (event
);
395 //-------------------------------------------------------------------------------------
397 //-------------------------------------------------------------------------------------
398 // Sets the selected button to receive keyboard input
400 void wxRadioBox::SetFocus()
403 wxRadioButton
*current
;
406 current
=m_radioButtonCycle
;
407 while (!current
->GetValue()) {
409 current
=current
->NextInCycle();
416 #pragma mark ### Internal functions ###
418 //-------------------------------------------------------------------------------------
420 //-------------------------------------------------------------------------------------
421 // Simulates the effect of the user issuing a command to the item
423 #define RADIO_SIZE 20
425 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
428 wxRadioButton
*current
;
430 // define the position
432 int x_current
, y_current
;
433 int x_offset
,y_offset
;
437 GetPosition(&x_current
, &y_current
);
438 if ((x
== -1) || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
439 x_offset
= x_current
;
440 if ((y
== -1) || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
441 y_offset
= y_current
;
445 int charWidth
,charHeight
;
446 int maxWidth
,maxHeight
;
447 int eachWidth
[128],eachHeight
[128];
448 int totWidth
,totHeight
;
450 SetFont(GetParent()->GetFont());
451 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
456 for (i
= 0 ; i
< m_noItems
; i
++)
458 GetTextExtent(GetLabel(i
), &eachWidth
[i
], &eachHeight
[i
]);
459 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
460 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
461 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
462 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
465 totHeight
= GetNumVer() * (maxHeight
+ charHeight
/2) + charHeight
*3/2;
466 totWidth
= GetNumHor() * (maxWidth
+ charWidth
) + charWidth
;
468 wxControl::DoSetSize(x_offset
,y_offset
,totWidth
,totHeight
,wxSIZE_AUTO
);
470 // arrange radiobuttons
476 y_start
= charHeight
*3/2;
480 current
=m_radioButtonCycle
;
481 for ( i
= 0 ; i
< m_noItems
; i
++)
483 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
485 if (m_windowStyle
& wxRA_VERTICAL
)
487 x_offset
+= maxWidth
+ charWidth
;
493 y_offset
+= maxHeight
+ charHeight
/2;
497 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
498 current
=current
->NextInCycle();
500 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
501 y_offset
+= maxHeight
+ charHeight
/2;
503 x_offset
+= maxWidth
+ charWidth
;
507 //-------------------------------------------------------------------------------------
509 //-------------------------------------------------------------------------------------
510 // return the number of buttons in the vertical direction
512 int wxRadioBox::GetNumVer() const
514 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
520 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
524 //-------------------------------------------------------------------------------------
526 //-------------------------------------------------------------------------------------
527 // return the number of buttons in the horizontal direction
529 int wxRadioBox::GetNumHor() const
531 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
533 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;