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 void wxRadioBox::Enable(bool enable
)
129 wxRadioButton
*current
;
131 wxControl::Enable(enable
);
133 current
=m_radioButtonCycle
;
134 for (i
=0;i
<m_noItems
;i
++) {
135 current
->Enable(enable
);
136 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();
158 return current
->Enable(enable
);
162 //-------------------------------------------------------------------------------------
164 //-------------------------------------------------------------------------------------
165 // Finds a button matching the given string, returning the position if found
166 // or -1 if not found
168 int wxRadioBox::FindString(const wxString
& s
) const
171 wxRadioButton
*current
;
173 current
=m_radioButtonCycle
;
174 for (i
= 0; i
< m_noItems
; i
++)
176 if (s
== current
->GetLabel())
178 current
=current
->NextInCycle();
183 //-------------------------------------------------------------------------------------
185 //-------------------------------------------------------------------------------------
186 // Returns the radiobox label
188 wxString
wxRadioBox::GetLabel() const
190 return wxControl::GetLabel();
193 //-------------------------------------------------------------------------------------
195 //-------------------------------------------------------------------------------------
196 // Returns the label for the given button
198 wxString
wxRadioBox::GetLabel(int item
) const
201 wxRadioButton
*current
;
203 if ((item
< 0) || (item
>= m_noItems
))
206 current
=m_radioButtonCycle
;
209 current
=current
->NextInCycle();
211 return current
->GetLabel();
214 //-------------------------------------------------------------------------------------
216 //-------------------------------------------------------------------------------------
217 // Returns the zero-based position of the selected button
219 int wxRadioBox::GetSelection() const
222 wxRadioButton
*current
;
225 current
=m_radioButtonCycle
;
226 while (!current
->GetValue()) {
228 current
=current
->NextInCycle();
234 //-------------------------------------------------------------------------------------
236 //-------------------------------------------------------------------------------------
237 // Find string for position
239 wxString
wxRadioBox::GetString(int item
) const
242 return GetLabel(item
);
245 //-------------------------------------------------------------------------------------
246 // ¥ GetStringSelection
247 //-------------------------------------------------------------------------------------
248 // Returns the selected string
250 wxString
wxRadioBox::GetStringSelection () const
252 int sel
= GetSelection ();
254 return this->GetString (sel
);
259 //-------------------------------------------------------------------------------------
261 //-------------------------------------------------------------------------------------
262 // Returns the number of buttons in the radiobox
267 //-------------------------------------------------------------------------------------
268 // ¥ SetLabel(const wxString&)
269 //-------------------------------------------------------------------------------------
270 // Sets the radiobox label
272 void wxRadioBox::SetLabel(const wxString
& label
)
274 return wxControl::SetLabel(label
);
277 //-------------------------------------------------------------------------------------
278 // ¥ SetLabel(int, const wxString&)
279 //-------------------------------------------------------------------------------------
280 // Sets the label of a given button
282 void wxRadioBox::SetLabel(int item
,const wxString
& label
)
285 wxRadioButton
*current
;
287 if ((item
< 0) || (item
>= m_noItems
))
290 current
=m_radioButtonCycle
;
293 current
=current
->NextInCycle();
295 return current
->SetLabel(label
);
298 //-------------------------------------------------------------------------------------
300 //-------------------------------------------------------------------------------------
301 // Sets a button by passing the desired position. This does not cause
302 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
304 void wxRadioBox::SetSelection(int item
)
307 wxRadioButton
*current
;
309 if ((item
< 0) || (item
>= m_noItems
))
312 current
=m_radioButtonCycle
;
315 current
=current
->NextInCycle();
317 current
->SetValue(true);
321 //-------------------------------------------------------------------------------------
322 // ¥ SetStringSelection
323 //-------------------------------------------------------------------------------------
324 // Sets a button by passing the desired string. This does not cause
325 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
327 bool wxRadioBox::SetStringSelection (const wxString
& s
)
329 int sel
= FindString (s
);
339 //-------------------------------------------------------------------------------------
341 //-------------------------------------------------------------------------------------
342 // Shows or hides the entire radiobox
344 bool wxRadioBox::Show(bool show
)
347 wxRadioButton
*current
;
349 wxControl::Show(show
);
351 current
=m_radioButtonCycle
;
352 for (i
=0;i
<m_noItems
;i
++) {
354 current
=current
->NextInCycle();
359 //-------------------------------------------------------------------------------------
361 //-------------------------------------------------------------------------------------
362 // Shows or hides the given button
364 void wxRadioBox::Show(int item
, bool show
)
367 wxRadioButton
*current
;
369 if ((item
< 0) || (item
>= m_noItems
))
372 current
=m_radioButtonCycle
;
375 current
=current
->NextInCycle();
381 #pragma mark ### Other external functions ###
383 //-------------------------------------------------------------------------------------
385 //-------------------------------------------------------------------------------------
386 // Simulates the effect of the user issuing a command to the item
388 void wxRadioBox::Command (wxCommandEvent
& event
)
390 SetSelection (event
.GetInt());
391 ProcessCommand (event
);
394 //-------------------------------------------------------------------------------------
396 //-------------------------------------------------------------------------------------
397 // Sets the selected button to receive keyboard input
399 void wxRadioBox::SetFocus()
402 wxRadioButton
*current
;
405 current
=m_radioButtonCycle
;
406 while (!current
->GetValue()) {
408 current
=current
->NextInCycle();
415 #pragma mark ### Internal functions ###
417 //-------------------------------------------------------------------------------------
419 //-------------------------------------------------------------------------------------
420 // Simulates the effect of the user issuing a command to the item
422 #define RADIO_SIZE 20
424 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
427 wxRadioButton
*current
;
429 // define the position
431 int x_current
, y_current
;
432 int x_offset
,y_offset
;
436 GetPosition(&x_current
, &y_current
);
437 if ((x
== -1) || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
438 x_offset
= x_current
;
439 if ((y
== -1) || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
440 y_offset
= y_current
;
444 int charWidth
,charHeight
;
445 int maxWidth
,maxHeight
;
446 int eachWidth
[128],eachHeight
[128];
447 int totWidth
,totHeight
;
449 SetFont(GetParent()->GetFont());
450 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
455 for (i
= 0 ; i
< m_noItems
; i
++)
457 GetTextExtent(GetLabel(i
), &eachWidth
[i
], &eachHeight
[i
]);
458 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
459 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
460 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
461 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
464 totHeight
= GetNumVer() * (maxHeight
+ charHeight
/2) + charHeight
*3/2;
465 totWidth
= GetNumHor() * (maxWidth
+ charWidth
) + charWidth
;
467 wxControl::DoSetSize(x_offset
,y_offset
,totWidth
,totHeight
);
469 // arrange radiobuttons
475 y_start
= charHeight
*3/2;
479 current
=m_radioButtonCycle
;
480 for ( i
= 0 ; i
< m_noItems
; i
++)
482 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
484 if (m_windowStyle
& wxRA_VERTICAL
)
486 x_offset
+= maxWidth
+ charWidth
;
492 y_offset
+= maxHeight
+ charHeight
/2;
496 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
497 current
=current
->NextInCycle();
499 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
500 y_offset
+= maxHeight
+ charHeight
/2;
502 x_offset
+= maxWidth
+ charWidth
;
506 //-------------------------------------------------------------------------------------
508 //-------------------------------------------------------------------------------------
509 // return the number of buttons in the vertical direction
511 int wxRadioBox::GetNumVer() const
513 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
519 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
523 //-------------------------------------------------------------------------------------
525 //-------------------------------------------------------------------------------------
526 // return the number of buttons in the horizontal direction
528 int wxRadioBox::GetNumHor() const
530 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
532 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;