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/radiobut.h"
22 #include "wx/mac/uma.h"
24 #if !USE_SHARED_LIBRARY
25 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
29 #pragma mark ### Constructors & destructor ###
31 //-------------------------------------------------------------------------------------
33 //-------------------------------------------------------------------------------------
34 // Default constructor
36 wxRadioBox::wxRadioBox()
41 m_radioButtonCycle
= NULL
;
44 //-------------------------------------------------------------------------------------
45 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
46 // const wxSize&, int, const wxString[], int, long,
47 // const wxValidator&, const wxString&)
48 //-------------------------------------------------------------------------------------
49 // Contructor, creating and showing a radiobox
54 //-------------------------------------------------------------------------------------
56 //-------------------------------------------------------------------------------------
57 // Destructor, destroying the radiobox item
59 wxRadioBox::~wxRadioBox()
61 wxRadioButton
*next
,*current
;
63 current
=m_radioButtonCycle
->NextInCycle();
64 next
=current
->NextInCycle();
65 while (current
!=m_radioButtonCycle
) {
68 next
=current
->NextInCycle();
73 //-------------------------------------------------------------------------------------
75 //-------------------------------------------------------------------------------------
76 // Create the radiobox for two-step construction
78 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
79 const wxPoint
& pos
, const wxSize
& size
,
80 int n
, const wxString choices
[],
81 int majorDim
, long style
,
82 const wxValidator
& val
, const wxString
& name
)
87 m_noRowsOrCols
= majorDim
;
88 m_radioButtonCycle
= NULL
;
93 m_majorDim
= majorDim
;
99 MacPreControlCreate( parent
, id
, label
, pos
, size
,style
, val
, name
, &bounds
, title
) ;
101 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, true , 0 , 0 , 1,
102 kControlGroupBoxTextTitleProc
, (long) this ) ;
104 MacPostControlCreate() ;
106 for (i
= 0; i
< n
; i
++)
108 wxRadioButton
*radBtn
= new wxRadioButton(this, NewControlId(),choices
[i
],wxPoint(5,20*i
+10));
109 m_radioButtonCycle
=radBtn
->AddInCycle(m_radioButtonCycle
);
113 SetSize(pos
.x
,pos
.y
,size
.x
,size
.y
);
120 #pragma mark ### Specific functions (reference v2) ###
122 //-------------------------------------------------------------------------------------
124 //-------------------------------------------------------------------------------------
125 // Enables or disables the entire radiobox
127 bool wxRadioBox::Enable(bool enable
)
130 wxRadioButton
*current
;
132 if (!wxControl::Enable(enable
))
135 current
=m_radioButtonCycle
;
136 for (i
=0;i
<m_noItems
;i
++) {
137 current
->Enable(enable
);
138 current
=current
->NextInCycle();
143 //-------------------------------------------------------------------------------------
144 // ¥ Enable(int, bool)
145 //-------------------------------------------------------------------------------------
146 // Enables or disables an given button
148 void wxRadioBox::Enable(int item
, bool enable
)
151 wxRadioButton
*current
;
153 if ((item
< 0) || (item
>= m_noItems
))
156 current
=m_radioButtonCycle
;
159 current
=current
->NextInCycle();
164 //-------------------------------------------------------------------------------------
166 //-------------------------------------------------------------------------------------
167 // Finds a button matching the given string, returning the position if found
168 // or -1 if not found
170 int wxRadioBox::FindString(const wxString
& s
) const
173 wxRadioButton
*current
;
175 current
=m_radioButtonCycle
;
176 for (i
= 0; i
< m_noItems
; i
++)
178 if (s
== current
->GetLabel())
180 current
=current
->NextInCycle();
185 //-------------------------------------------------------------------------------------
187 //-------------------------------------------------------------------------------------
188 // Returns the radiobox label
190 wxString
wxRadioBox::GetLabel() const
192 return wxControl::GetLabel();
195 //-------------------------------------------------------------------------------------
197 //-------------------------------------------------------------------------------------
198 // Returns the label for the given button
200 wxString
wxRadioBox::GetLabel(int item
) const
203 wxRadioButton
*current
;
205 if ((item
< 0) || (item
>= m_noItems
))
208 current
=m_radioButtonCycle
;
211 current
=current
->NextInCycle();
213 return current
->GetLabel();
216 //-------------------------------------------------------------------------------------
218 //-------------------------------------------------------------------------------------
219 // Returns the zero-based position of the selected button
221 int wxRadioBox::GetSelection() const
224 wxRadioButton
*current
;
227 current
=m_radioButtonCycle
;
228 while (!current
->GetValue()) {
230 current
=current
->NextInCycle();
236 //-------------------------------------------------------------------------------------
238 //-------------------------------------------------------------------------------------
239 // Find string for position
241 wxString
wxRadioBox::GetString(int item
) const
244 return GetLabel(item
);
247 //-------------------------------------------------------------------------------------
248 // ¥ GetStringSelection
249 //-------------------------------------------------------------------------------------
250 // Returns the selected string
252 wxString
wxRadioBox::GetStringSelection () const
254 int sel
= GetSelection ();
256 return this->GetString (sel
);
261 //-------------------------------------------------------------------------------------
263 //-------------------------------------------------------------------------------------
264 // Returns the number of buttons in the radiobox
269 //-------------------------------------------------------------------------------------
270 // ¥ SetLabel(const wxString&)
271 //-------------------------------------------------------------------------------------
272 // Sets the radiobox label
274 void wxRadioBox::SetLabel(const wxString
& label
)
276 return wxControl::SetLabel(label
);
279 //-------------------------------------------------------------------------------------
280 // ¥ SetLabel(int, const wxString&)
281 //-------------------------------------------------------------------------------------
282 // Sets the label of a given button
284 void wxRadioBox::SetLabel(int item
,const wxString
& label
)
287 wxRadioButton
*current
;
289 if ((item
< 0) || (item
>= m_noItems
))
292 current
=m_radioButtonCycle
;
295 current
=current
->NextInCycle();
297 return current
->SetLabel(label
);
300 //-------------------------------------------------------------------------------------
302 //-------------------------------------------------------------------------------------
303 // Sets a button by passing the desired position. This does not cause
304 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
306 void wxRadioBox::SetSelection(int item
)
309 wxRadioButton
*current
;
311 if ((item
< 0) || (item
>= m_noItems
))
314 current
=m_radioButtonCycle
;
317 current
=current
->NextInCycle();
319 current
->SetValue(true);
323 //-------------------------------------------------------------------------------------
324 // ¥ SetStringSelection
325 //-------------------------------------------------------------------------------------
326 // Sets a button by passing the desired string. This does not cause
327 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
329 bool wxRadioBox::SetStringSelection (const wxString
& s
)
331 int sel
= FindString (s
);
341 //-------------------------------------------------------------------------------------
343 //-------------------------------------------------------------------------------------
344 // Shows or hides the entire radiobox
346 bool wxRadioBox::Show(bool show
)
349 wxRadioButton
*current
;
351 wxControl::Show(show
);
353 current
=m_radioButtonCycle
;
354 for (i
=0;i
<m_noItems
;i
++) {
356 current
=current
->NextInCycle();
361 //-------------------------------------------------------------------------------------
363 //-------------------------------------------------------------------------------------
364 // Shows or hides the given button
366 void wxRadioBox::Show(int item
, bool show
)
369 wxRadioButton
*current
;
371 if ((item
< 0) || (item
>= m_noItems
))
374 current
=m_radioButtonCycle
;
377 current
=current
->NextInCycle();
383 #pragma mark ### Other external functions ###
385 //-------------------------------------------------------------------------------------
387 //-------------------------------------------------------------------------------------
388 // Simulates the effect of the user issuing a command to the item
390 void wxRadioBox::Command (wxCommandEvent
& event
)
392 SetSelection (event
.GetInt());
393 ProcessCommand (event
);
396 //-------------------------------------------------------------------------------------
398 //-------------------------------------------------------------------------------------
399 // Sets the selected button to receive keyboard input
401 void wxRadioBox::SetFocus()
404 wxRadioButton
*current
;
407 current
=m_radioButtonCycle
;
408 while (!current
->GetValue()) {
410 current
=current
->NextInCycle();
417 #pragma mark ### Internal functions ###
419 //-------------------------------------------------------------------------------------
421 //-------------------------------------------------------------------------------------
422 // Simulates the effect of the user issuing a command to the item
424 #define RADIO_SIZE 40
426 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
429 wxRadioButton
*current
;
431 // define the position
433 int x_current
, y_current
;
434 int x_offset
,y_offset
;
435 int widthOld
, heightOld
;
436 GetSize(&widthOld
, &heightOld
);
440 GetPosition(&x_current
, &y_current
);
441 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
442 x_offset
= x_current
;
443 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
444 y_offset
= y_current
;
448 int charWidth
,charHeight
;
449 int maxWidth
,maxHeight
;
450 int eachWidth
[128],eachHeight
[128];
451 int totWidth
,totHeight
;
453 SetFont(GetParent()->GetFont());
454 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
459 for (i
= 0 ; i
< m_noItems
; i
++)
461 GetTextExtent(GetLabel(i
), &eachWidth
[i
], &eachHeight
[i
]);
462 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
463 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
464 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
465 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
468 totHeight
= GetNumVer() * (maxHeight
+ charHeight
/2) + charHeight
*3/2;
469 totWidth
= GetNumHor() * (maxWidth
+ charWidth
) + charWidth
;
471 // only change our width/height if asked for
474 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
482 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
488 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
490 // arrange radiobuttons
500 current
=m_radioButtonCycle
;
501 for ( i
= 0 ; i
< m_noItems
; i
++)
503 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
505 if (m_windowStyle
& wxRA_VERTICAL
)
507 x_offset
+= maxWidth
+ charWidth
;
513 y_offset
+= maxHeight
; /*+ charHeight/2;*/
517 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
518 current
=current
->NextInCycle();
520 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
521 y_offset
+= maxHeight
; /*+ charHeight/2;*/
523 x_offset
+= maxWidth
+ charWidth
;
527 //-------------------------------------------------------------------------------------
529 //-------------------------------------------------------------------------------------
530 // return the number of buttons in the vertical direction
532 int wxRadioBox::GetNumVer() const
534 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
540 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
544 //-------------------------------------------------------------------------------------
546 //-------------------------------------------------------------------------------------
547 // return the number of buttons in the horizontal direction
549 int wxRadioBox::GetNumHor() const
551 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
553 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;