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 wxDefaultSize
, i
== 0 ? wxRB_GROUP
: 0 ) ;
111 m_radioButtonCycle
= radBtn
;
112 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
116 SetSize(pos
.x
,pos
.y
,size
.x
,size
.y
);
123 #pragma mark ### Specific functions (reference v2) ###
125 //-------------------------------------------------------------------------------------
127 //-------------------------------------------------------------------------------------
128 // Enables or disables the entire radiobox
130 bool wxRadioBox::Enable(bool enable
)
133 wxRadioButton
*current
;
135 if (!wxControl::Enable(enable
))
138 current
=m_radioButtonCycle
;
139 for (i
=0;i
<m_noItems
;i
++) {
140 current
->Enable(enable
);
141 current
=current
->NextInCycle();
146 //-------------------------------------------------------------------------------------
147 // ¥ Enable(int, bool)
148 //-------------------------------------------------------------------------------------
149 // Enables or disables an given button
151 void wxRadioBox::Enable(int item
, bool enable
)
154 wxRadioButton
*current
;
156 if ((item
< 0) || (item
>= m_noItems
))
159 current
=m_radioButtonCycle
;
162 current
=current
->NextInCycle();
167 //-------------------------------------------------------------------------------------
169 //-------------------------------------------------------------------------------------
170 // Finds a button matching the given string, returning the position if found
171 // or -1 if not found
173 int wxRadioBox::FindString(const wxString
& s
) const
176 wxRadioButton
*current
;
178 current
=m_radioButtonCycle
;
179 for (i
= 0; i
< m_noItems
; i
++)
181 if (s
== current
->GetLabel())
183 current
=current
->NextInCycle();
188 //-------------------------------------------------------------------------------------
190 //-------------------------------------------------------------------------------------
191 // Returns the radiobox label
193 wxString
wxRadioBox::GetLabel() const
195 return wxControl::GetLabel();
198 //-------------------------------------------------------------------------------------
200 //-------------------------------------------------------------------------------------
201 // Returns the label for the given button
203 wxString
wxRadioBox::GetLabel(int item
) const
206 wxRadioButton
*current
;
208 if ((item
< 0) || (item
>= m_noItems
))
211 current
=m_radioButtonCycle
;
214 current
=current
->NextInCycle();
216 return current
->GetLabel();
219 //-------------------------------------------------------------------------------------
221 //-------------------------------------------------------------------------------------
222 // Returns the zero-based position of the selected button
224 int wxRadioBox::GetSelection() const
227 wxRadioButton
*current
;
230 current
=m_radioButtonCycle
;
231 while (!current
->GetValue()) {
233 current
=current
->NextInCycle();
239 //-------------------------------------------------------------------------------------
241 //-------------------------------------------------------------------------------------
242 // Find string for position
244 wxString
wxRadioBox::GetString(int item
) const
247 return GetLabel(item
);
250 //-------------------------------------------------------------------------------------
251 // ¥ GetStringSelection
252 //-------------------------------------------------------------------------------------
253 // Returns the selected string
255 wxString
wxRadioBox::GetStringSelection () const
257 int sel
= GetSelection ();
259 return this->GetString (sel
);
264 //-------------------------------------------------------------------------------------
266 //-------------------------------------------------------------------------------------
267 // Returns the number of buttons in the radiobox
272 //-------------------------------------------------------------------------------------
273 // ¥ SetLabel(const wxString&)
274 //-------------------------------------------------------------------------------------
275 // Sets the radiobox label
277 void wxRadioBox::SetLabel(const wxString
& label
)
279 return wxControl::SetLabel(label
);
282 //-------------------------------------------------------------------------------------
283 // ¥ SetLabel(int, const wxString&)
284 //-------------------------------------------------------------------------------------
285 // Sets the label of a given button
287 void wxRadioBox::SetLabel(int item
,const wxString
& label
)
290 wxRadioButton
*current
;
292 if ((item
< 0) || (item
>= m_noItems
))
295 current
=m_radioButtonCycle
;
298 current
=current
->NextInCycle();
300 return current
->SetLabel(label
);
303 //-------------------------------------------------------------------------------------
305 //-------------------------------------------------------------------------------------
306 // Sets a button by passing the desired position. This does not cause
307 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
309 void wxRadioBox::SetSelection(int item
)
312 wxRadioButton
*current
;
314 if ((item
< 0) || (item
>= m_noItems
))
317 current
=m_radioButtonCycle
;
320 current
=current
->NextInCycle();
322 current
->SetValue(true);
326 //-------------------------------------------------------------------------------------
327 // ¥ SetStringSelection
328 //-------------------------------------------------------------------------------------
329 // Sets a button by passing the desired string. This does not cause
330 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
332 bool wxRadioBox::SetStringSelection (const wxString
& s
)
334 int sel
= FindString (s
);
344 //-------------------------------------------------------------------------------------
346 //-------------------------------------------------------------------------------------
347 // Shows or hides the entire radiobox
349 bool wxRadioBox::Show(bool show
)
352 wxRadioButton
*current
;
354 wxControl::Show(show
);
356 current
=m_radioButtonCycle
;
357 for (i
=0;i
<m_noItems
;i
++) {
359 current
=current
->NextInCycle();
364 //-------------------------------------------------------------------------------------
366 //-------------------------------------------------------------------------------------
367 // Shows or hides the given button
369 void wxRadioBox::Show(int item
, bool show
)
372 wxRadioButton
*current
;
374 if ((item
< 0) || (item
>= m_noItems
))
377 current
=m_radioButtonCycle
;
380 current
=current
->NextInCycle();
386 #pragma mark ### Other external functions ###
388 //-------------------------------------------------------------------------------------
390 //-------------------------------------------------------------------------------------
391 // Simulates the effect of the user issuing a command to the item
393 void wxRadioBox::Command (wxCommandEvent
& event
)
395 SetSelection (event
.GetInt());
396 ProcessCommand (event
);
399 //-------------------------------------------------------------------------------------
401 //-------------------------------------------------------------------------------------
402 // Sets the selected button to receive keyboard input
404 void wxRadioBox::SetFocus()
407 wxRadioButton
*current
;
410 current
=m_radioButtonCycle
;
411 while (!current
->GetValue()) {
413 current
=current
->NextInCycle();
420 #pragma mark ### Internal functions ###
422 //-------------------------------------------------------------------------------------
424 //-------------------------------------------------------------------------------------
425 // Simulates the effect of the user issuing a command to the item
427 #define RADIO_SIZE 40
429 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
432 wxRadioButton
*current
;
434 // define the position
436 int x_current
, y_current
;
437 int x_offset
,y_offset
;
438 int widthOld
, heightOld
;
439 GetSize(&widthOld
, &heightOld
);
443 GetPosition(&x_current
, &y_current
);
444 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
445 x_offset
= x_current
;
446 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
447 y_offset
= y_current
;
451 int charWidth
,charHeight
;
452 int maxWidth
,maxHeight
;
453 int eachWidth
[128],eachHeight
[128];
454 int totWidth
,totHeight
;
456 SetFont(GetParent()->GetFont());
457 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
462 for (i
= 0 ; i
< m_noItems
; i
++)
464 GetTextExtent(GetLabel(i
), &eachWidth
[i
], &eachHeight
[i
]);
465 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
466 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
467 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
468 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
471 totHeight
= GetNumVer() * (maxHeight
+ charHeight
/2) + charHeight
*3/2;
472 totWidth
= GetNumHor() * (maxWidth
+ charWidth
) + charWidth
;
474 // only change our width/height if asked for
477 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
485 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
491 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
493 // arrange radiobuttons
503 current
=m_radioButtonCycle
;
504 for ( i
= 0 ; i
< m_noItems
; i
++)
506 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
508 if (m_windowStyle
& wxRA_VERTICAL
)
510 x_offset
+= maxWidth
+ charWidth
;
516 y_offset
+= maxHeight
; /*+ charHeight/2;*/
520 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
521 current
=current
->NextInCycle();
523 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
524 y_offset
+= maxHeight
; /*+ charHeight/2;*/
526 x_offset
+= maxWidth
+ charWidth
;
530 //-------------------------------------------------------------------------------------
532 //-------------------------------------------------------------------------------------
533 // return the number of buttons in the vertical direction
535 int wxRadioBox::GetNumVer() const
537 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
543 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
547 //-------------------------------------------------------------------------------------
549 //-------------------------------------------------------------------------------------
550 // return the number of buttons in the horizontal direction
552 int wxRadioBox::GetNumHor() const
554 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
556 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;