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"
22 #include "wx/radiobox.h"
23 #include "wx/radiobut.h"
24 #include "wx/mac/uma.h"
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
31 #pragma mark ### Constructors & destructor ###
33 //-------------------------------------------------------------------------------------
35 //-------------------------------------------------------------------------------------
36 // Default constructor
37 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
38 EVT_RADIOBUTTON( -1 , wxRadioBox::OnRadioButton
)
41 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
43 if ( outer
.IsChecked() )
45 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
46 int i
= GetSelection() ;
48 event
.SetString( GetString( i
) );
49 event
.SetEventObject( this );
50 ProcessCommand(event
);
54 wxRadioBox::wxRadioBox()
59 m_radioButtonCycle
= NULL
;
62 //-------------------------------------------------------------------------------------
63 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
64 // const wxSize&, int, const wxString[], int, long,
65 // const wxValidator&, const wxString&)
66 //-------------------------------------------------------------------------------------
67 // Contructor, creating and showing a radiobox
72 //-------------------------------------------------------------------------------------
74 //-------------------------------------------------------------------------------------
75 // Destructor, destroying the radiobox item
77 wxRadioBox::~wxRadioBox()
79 wxRadioButton
*next
,*current
;
81 current
=m_radioButtonCycle
->NextInCycle();
82 next
=current
->NextInCycle();
83 while (current
!=m_radioButtonCycle
) {
86 next
=current
->NextInCycle();
91 //-------------------------------------------------------------------------------------
93 //-------------------------------------------------------------------------------------
94 // Create the radiobox for two-step construction
96 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
97 const wxPoint
& pos
, const wxSize
& size
,
98 int n
, const wxString choices
[],
99 int majorDim
, long style
,
100 const wxValidator
& val
, const wxString
& name
)
105 m_noRowsOrCols
= majorDim
;
106 m_radioButtonCycle
= NULL
;
111 m_majorDim
= majorDim
;
117 MacPreControlCreate( parent
, id
, label
, pos
, size
,style
, val
, name
, &bounds
, title
) ;
119 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, false , 0 , 0 , 1,
120 kControlGroupBoxTextTitleProc
, (long) this ) ;
122 MacPostControlCreate() ;
124 for (i
= 0; i
< n
; i
++)
126 wxRadioButton
*radBtn
= new wxRadioButton(this, NewControlId(),choices
[i
],wxPoint(5,20*i
+10),
127 wxDefaultSize
, i
== 0 ? wxRB_GROUP
: 0 ) ;
129 m_radioButtonCycle
= radBtn
;
130 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
134 SetSize(pos
.x
,pos
.y
,size
.x
,size
.y
);
141 #pragma mark ### Specific functions (reference v2) ###
143 //-------------------------------------------------------------------------------------
145 //-------------------------------------------------------------------------------------
146 // Enables or disables the entire radiobox
148 bool wxRadioBox::Enable(bool enable
)
151 wxRadioButton
*current
;
153 if (!wxControl::Enable(enable
))
156 current
=m_radioButtonCycle
;
157 for (i
=0;i
<m_noItems
;i
++) {
158 current
->Enable(enable
);
159 current
=current
->NextInCycle();
164 //-------------------------------------------------------------------------------------
165 // ¥ Enable(int, bool)
166 //-------------------------------------------------------------------------------------
167 // Enables or disables an given button
169 void wxRadioBox::Enable(int item
, bool enable
)
172 wxRadioButton
*current
;
174 if ((item
< 0) || (item
>= m_noItems
))
177 current
=m_radioButtonCycle
;
180 current
=current
->NextInCycle();
185 //-------------------------------------------------------------------------------------
187 //-------------------------------------------------------------------------------------
188 // Finds a button matching the given string, returning the position if found
189 // or -1 if not found
191 int wxRadioBox::FindString(const wxString
& s
) const
194 wxRadioButton
*current
;
196 current
=m_radioButtonCycle
;
197 for (i
= 0; i
< m_noItems
; i
++)
199 if (s
== current
->GetLabel())
201 current
=current
->NextInCycle();
206 //-------------------------------------------------------------------------------------
208 //-------------------------------------------------------------------------------------
209 // Returns the radiobox label
211 wxString
wxRadioBox::GetLabel() const
213 return wxControl::GetLabel();
216 //-------------------------------------------------------------------------------------
218 //-------------------------------------------------------------------------------------
219 // Returns the label for the given button
221 wxString
wxRadioBox::GetLabel(int item
) const
224 wxRadioButton
*current
;
226 if ((item
< 0) || (item
>= m_noItems
))
229 current
=m_radioButtonCycle
;
232 current
=current
->NextInCycle();
234 return current
->GetLabel();
237 //-------------------------------------------------------------------------------------
239 //-------------------------------------------------------------------------------------
240 // Returns the zero-based position of the selected button
242 int wxRadioBox::GetSelection() const
245 wxRadioButton
*current
;
248 current
=m_radioButtonCycle
;
249 while (!current
->GetValue()) {
251 current
=current
->NextInCycle();
257 //-------------------------------------------------------------------------------------
259 //-------------------------------------------------------------------------------------
260 // Find string for position
262 wxString
wxRadioBox::GetString(int item
) const
265 return GetLabel(item
);
268 //-------------------------------------------------------------------------------------
269 // ¥ GetStringSelection
270 //-------------------------------------------------------------------------------------
271 // Returns the selected string
273 wxString
wxRadioBox::GetStringSelection () const
275 int sel
= GetSelection ();
277 return this->GetString (sel
);
282 //-------------------------------------------------------------------------------------
284 //-------------------------------------------------------------------------------------
285 // Returns the number of buttons in the radiobox
290 //-------------------------------------------------------------------------------------
291 // ¥ SetLabel(const wxString&)
292 //-------------------------------------------------------------------------------------
293 // Sets the radiobox label
295 void wxRadioBox::SetLabel(const wxString
& label
)
297 return wxControl::SetLabel(label
);
300 //-------------------------------------------------------------------------------------
301 // ¥ SetLabel(int, const wxString&)
302 //-------------------------------------------------------------------------------------
303 // Sets the label of a given button
305 void wxRadioBox::SetLabel(int item
,const wxString
& label
)
308 wxRadioButton
*current
;
310 if ((item
< 0) || (item
>= m_noItems
))
313 current
=m_radioButtonCycle
;
316 current
=current
->NextInCycle();
318 return current
->SetLabel(label
);
321 //-------------------------------------------------------------------------------------
323 //-------------------------------------------------------------------------------------
324 // Sets a button by passing the desired position. This does not cause
325 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
327 void wxRadioBox::SetSelection(int item
)
330 wxRadioButton
*current
;
332 if ((item
< 0) || (item
>= m_noItems
))
335 current
=m_radioButtonCycle
;
338 current
=current
->NextInCycle();
340 current
->SetValue(true);
344 //-------------------------------------------------------------------------------------
345 // ¥ SetStringSelection
346 //-------------------------------------------------------------------------------------
347 // Sets a button by passing the desired string. This does not cause
348 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
350 bool wxRadioBox::SetStringSelection (const wxString
& s
)
352 int sel
= FindString (s
);
362 //-------------------------------------------------------------------------------------
364 //-------------------------------------------------------------------------------------
365 // Shows or hides the entire radiobox
367 bool wxRadioBox::Show(bool show
)
370 wxRadioButton
*current
;
372 wxControl::Show(show
);
374 current
=m_radioButtonCycle
;
375 for (i
=0;i
<m_noItems
;i
++) {
377 current
=current
->NextInCycle();
382 //-------------------------------------------------------------------------------------
384 //-------------------------------------------------------------------------------------
385 // Shows or hides the given button
387 void wxRadioBox::Show(int item
, bool show
)
390 wxRadioButton
*current
;
392 if ((item
< 0) || (item
>= m_noItems
))
395 current
=m_radioButtonCycle
;
398 current
=current
->NextInCycle();
404 #pragma mark ### Other external functions ###
406 //-------------------------------------------------------------------------------------
408 //-------------------------------------------------------------------------------------
409 // Simulates the effect of the user issuing a command to the item
411 void wxRadioBox::Command (wxCommandEvent
& event
)
413 SetSelection (event
.GetInt());
414 ProcessCommand (event
);
417 //-------------------------------------------------------------------------------------
419 //-------------------------------------------------------------------------------------
420 // Sets the selected button to receive keyboard input
422 void wxRadioBox::SetFocus()
425 wxRadioButton
*current
;
428 current
=m_radioButtonCycle
;
429 while (!current
->GetValue()) {
431 current
=current
->NextInCycle();
438 #pragma mark ### Internal functions ###
440 //-------------------------------------------------------------------------------------
442 //-------------------------------------------------------------------------------------
443 // Simulates the effect of the user issuing a command to the item
445 #define RADIO_SIZE 40
447 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
450 wxRadioButton
*current
;
452 // define the position
454 int x_current
, y_current
;
455 int x_offset
,y_offset
;
456 int widthOld
, heightOld
;
457 GetSize(&widthOld
, &heightOld
);
461 GetPosition(&x_current
, &y_current
);
462 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
463 x_offset
= x_current
;
464 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
465 y_offset
= y_current
;
469 int charWidth
,charHeight
;
470 int maxWidth
,maxHeight
;
471 int eachWidth
[128],eachHeight
[128];
472 int totWidth
,totHeight
;
474 SetFont(GetParent()->GetFont());
475 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
480 for (i
= 0 ; i
< m_noItems
; i
++)
482 GetTextExtent(GetLabel(i
), &eachWidth
[i
], &eachHeight
[i
]);
483 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
484 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
485 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
486 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
489 totHeight
= GetNumVer() * (maxHeight
+ charHeight
/2) + charHeight
*3/2;
490 totWidth
= GetNumHor() * (maxWidth
+ charWidth
) + charWidth
;
492 // only change our width/height if asked for
495 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
503 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
509 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
511 // arrange radiobuttons
521 current
=m_radioButtonCycle
;
522 for ( i
= 0 ; i
< m_noItems
; i
++)
524 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
526 if (m_windowStyle
& wxRA_VERTICAL
)
528 x_offset
+= maxWidth
+ charWidth
;
534 y_offset
+= maxHeight
; /*+ charHeight/2;*/
538 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
539 current
=current
->NextInCycle();
541 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
542 y_offset
+= maxHeight
; /*+ charHeight/2;*/
544 x_offset
+= maxWidth
+ charWidth
;
548 //-------------------------------------------------------------------------------------
550 //-------------------------------------------------------------------------------------
551 // return the number of buttons in the vertical direction
553 int wxRadioBox::GetNumVer() const
555 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
561 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
565 //-------------------------------------------------------------------------------------
567 //-------------------------------------------------------------------------------------
568 // return the number of buttons in the horizontal direction
570 int wxRadioBox::GetNumHor() const
572 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
574 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;