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
35 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
36 EVT_RADIOBUTTON( -1 , wxRadioBox::OnRadioButton
)
39 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
41 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
42 int i
= GetSelection() ;
44 event
.SetString( GetString( i
) );
45 event
.SetEventObject( this );
46 ProcessCommand(event
);
49 wxRadioBox::wxRadioBox()
54 m_radioButtonCycle
= NULL
;
57 //-------------------------------------------------------------------------------------
58 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
59 // const wxSize&, int, const wxString[], int, long,
60 // const wxValidator&, const wxString&)
61 //-------------------------------------------------------------------------------------
62 // Contructor, creating and showing a radiobox
67 //-------------------------------------------------------------------------------------
69 //-------------------------------------------------------------------------------------
70 // Destructor, destroying the radiobox item
72 wxRadioBox::~wxRadioBox()
74 wxRadioButton
*next
,*current
;
76 current
=m_radioButtonCycle
->NextInCycle();
77 next
=current
->NextInCycle();
78 while (current
!=m_radioButtonCycle
) {
81 next
=current
->NextInCycle();
86 //-------------------------------------------------------------------------------------
88 //-------------------------------------------------------------------------------------
89 // Create the radiobox for two-step construction
91 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
92 const wxPoint
& pos
, const wxSize
& size
,
93 int n
, const wxString choices
[],
94 int majorDim
, long style
,
95 const wxValidator
& val
, const wxString
& name
)
100 m_noRowsOrCols
= majorDim
;
101 m_radioButtonCycle
= NULL
;
106 m_majorDim
= majorDim
;
112 MacPreControlCreate( parent
, id
, label
, pos
, size
,style
, val
, name
, &bounds
, title
) ;
114 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, true , 0 , 0 , 1,
115 kControlGroupBoxTextTitleProc
, (long) this ) ;
117 MacPostControlCreate() ;
119 for (i
= 0; i
< n
; i
++)
121 wxRadioButton
*radBtn
= new wxRadioButton(this, NewControlId(),choices
[i
],wxPoint(5,20*i
+10),
122 wxDefaultSize
, i
== 0 ? wxRB_GROUP
: 0 ) ;
124 m_radioButtonCycle
= radBtn
;
125 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
129 SetSize(pos
.x
,pos
.y
,size
.x
,size
.y
);
136 #pragma mark ### Specific functions (reference v2) ###
138 //-------------------------------------------------------------------------------------
140 //-------------------------------------------------------------------------------------
141 // Enables or disables the entire radiobox
143 bool wxRadioBox::Enable(bool enable
)
146 wxRadioButton
*current
;
148 if (!wxControl::Enable(enable
))
151 current
=m_radioButtonCycle
;
152 for (i
=0;i
<m_noItems
;i
++) {
153 current
->Enable(enable
);
154 current
=current
->NextInCycle();
159 //-------------------------------------------------------------------------------------
160 // ¥ Enable(int, bool)
161 //-------------------------------------------------------------------------------------
162 // Enables or disables an given button
164 void wxRadioBox::Enable(int item
, bool enable
)
167 wxRadioButton
*current
;
169 if ((item
< 0) || (item
>= m_noItems
))
172 current
=m_radioButtonCycle
;
175 current
=current
->NextInCycle();
180 //-------------------------------------------------------------------------------------
182 //-------------------------------------------------------------------------------------
183 // Finds a button matching the given string, returning the position if found
184 // or -1 if not found
186 int wxRadioBox::FindString(const wxString
& s
) const
189 wxRadioButton
*current
;
191 current
=m_radioButtonCycle
;
192 for (i
= 0; i
< m_noItems
; i
++)
194 if (s
== current
->GetLabel())
196 current
=current
->NextInCycle();
201 //-------------------------------------------------------------------------------------
203 //-------------------------------------------------------------------------------------
204 // Returns the radiobox label
206 wxString
wxRadioBox::GetLabel() const
208 return wxControl::GetLabel();
211 //-------------------------------------------------------------------------------------
213 //-------------------------------------------------------------------------------------
214 // Returns the label for the given button
216 wxString
wxRadioBox::GetLabel(int item
) const
219 wxRadioButton
*current
;
221 if ((item
< 0) || (item
>= m_noItems
))
224 current
=m_radioButtonCycle
;
227 current
=current
->NextInCycle();
229 return current
->GetLabel();
232 //-------------------------------------------------------------------------------------
234 //-------------------------------------------------------------------------------------
235 // Returns the zero-based position of the selected button
237 int wxRadioBox::GetSelection() const
240 wxRadioButton
*current
;
243 current
=m_radioButtonCycle
;
244 while (!current
->GetValue()) {
246 current
=current
->NextInCycle();
252 //-------------------------------------------------------------------------------------
254 //-------------------------------------------------------------------------------------
255 // Find string for position
257 wxString
wxRadioBox::GetString(int item
) const
260 return GetLabel(item
);
263 //-------------------------------------------------------------------------------------
264 // ¥ GetStringSelection
265 //-------------------------------------------------------------------------------------
266 // Returns the selected string
268 wxString
wxRadioBox::GetStringSelection () const
270 int sel
= GetSelection ();
272 return this->GetString (sel
);
277 //-------------------------------------------------------------------------------------
279 //-------------------------------------------------------------------------------------
280 // Returns the number of buttons in the radiobox
285 //-------------------------------------------------------------------------------------
286 // ¥ SetLabel(const wxString&)
287 //-------------------------------------------------------------------------------------
288 // Sets the radiobox label
290 void wxRadioBox::SetLabel(const wxString
& label
)
292 return wxControl::SetLabel(label
);
295 //-------------------------------------------------------------------------------------
296 // ¥ SetLabel(int, const wxString&)
297 //-------------------------------------------------------------------------------------
298 // Sets the label of a given button
300 void wxRadioBox::SetLabel(int item
,const wxString
& label
)
303 wxRadioButton
*current
;
305 if ((item
< 0) || (item
>= m_noItems
))
308 current
=m_radioButtonCycle
;
311 current
=current
->NextInCycle();
313 return current
->SetLabel(label
);
316 //-------------------------------------------------------------------------------------
318 //-------------------------------------------------------------------------------------
319 // Sets a button by passing the desired position. This does not cause
320 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
322 void wxRadioBox::SetSelection(int item
)
325 wxRadioButton
*current
;
327 if ((item
< 0) || (item
>= m_noItems
))
330 current
=m_radioButtonCycle
;
333 current
=current
->NextInCycle();
335 current
->SetValue(true);
339 //-------------------------------------------------------------------------------------
340 // ¥ SetStringSelection
341 //-------------------------------------------------------------------------------------
342 // Sets a button by passing the desired string. This does not cause
343 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
345 bool wxRadioBox::SetStringSelection (const wxString
& s
)
347 int sel
= FindString (s
);
357 //-------------------------------------------------------------------------------------
359 //-------------------------------------------------------------------------------------
360 // Shows or hides the entire radiobox
362 bool wxRadioBox::Show(bool show
)
365 wxRadioButton
*current
;
367 wxControl::Show(show
);
369 current
=m_radioButtonCycle
;
370 for (i
=0;i
<m_noItems
;i
++) {
372 current
=current
->NextInCycle();
377 //-------------------------------------------------------------------------------------
379 //-------------------------------------------------------------------------------------
380 // Shows or hides the given button
382 void wxRadioBox::Show(int item
, bool show
)
385 wxRadioButton
*current
;
387 if ((item
< 0) || (item
>= m_noItems
))
390 current
=m_radioButtonCycle
;
393 current
=current
->NextInCycle();
399 #pragma mark ### Other external functions ###
401 //-------------------------------------------------------------------------------------
403 //-------------------------------------------------------------------------------------
404 // Simulates the effect of the user issuing a command to the item
406 void wxRadioBox::Command (wxCommandEvent
& event
)
408 SetSelection (event
.GetInt());
409 ProcessCommand (event
);
412 //-------------------------------------------------------------------------------------
414 //-------------------------------------------------------------------------------------
415 // Sets the selected button to receive keyboard input
417 void wxRadioBox::SetFocus()
420 wxRadioButton
*current
;
423 current
=m_radioButtonCycle
;
424 while (!current
->GetValue()) {
426 current
=current
->NextInCycle();
433 #pragma mark ### Internal functions ###
435 //-------------------------------------------------------------------------------------
437 //-------------------------------------------------------------------------------------
438 // Simulates the effect of the user issuing a command to the item
440 #define RADIO_SIZE 40
442 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
445 wxRadioButton
*current
;
447 // define the position
449 int x_current
, y_current
;
450 int x_offset
,y_offset
;
451 int widthOld
, heightOld
;
452 GetSize(&widthOld
, &heightOld
);
456 GetPosition(&x_current
, &y_current
);
457 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
458 x_offset
= x_current
;
459 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
460 y_offset
= y_current
;
464 int charWidth
,charHeight
;
465 int maxWidth
,maxHeight
;
466 int eachWidth
[128],eachHeight
[128];
467 int totWidth
,totHeight
;
469 SetFont(GetParent()->GetFont());
470 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
475 for (i
= 0 ; i
< m_noItems
; i
++)
477 GetTextExtent(GetLabel(i
), &eachWidth
[i
], &eachHeight
[i
]);
478 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
479 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
480 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
481 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
484 totHeight
= GetNumVer() * (maxHeight
+ charHeight
/2) + charHeight
*3/2;
485 totWidth
= GetNumHor() * (maxWidth
+ charWidth
) + charWidth
;
487 // only change our width/height if asked for
490 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
498 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
504 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
506 // arrange radiobuttons
516 current
=m_radioButtonCycle
;
517 for ( i
= 0 ; i
< m_noItems
; i
++)
519 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
521 if (m_windowStyle
& wxRA_VERTICAL
)
523 x_offset
+= maxWidth
+ charWidth
;
529 y_offset
+= maxHeight
; /*+ charHeight/2;*/
533 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
534 current
=current
->NextInCycle();
536 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
537 y_offset
+= maxHeight
; /*+ charHeight/2;*/
539 x_offset
+= maxWidth
+ charWidth
;
543 //-------------------------------------------------------------------------------------
545 //-------------------------------------------------------------------------------------
546 // return the number of buttons in the vertical direction
548 int wxRadioBox::GetNumVer() const
550 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
556 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
560 //-------------------------------------------------------------------------------------
562 //-------------------------------------------------------------------------------------
563 // return the number of buttons in the horizontal direction
565 int wxRadioBox::GetNumHor() const
567 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
569 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;