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 if ( outer
.IsChecked() )
43 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
44 int i
= GetSelection() ;
46 event
.SetString( GetString( i
) );
47 event
.SetEventObject( this );
48 ProcessCommand(event
);
52 wxRadioBox::wxRadioBox()
57 m_radioButtonCycle
= NULL
;
60 //-------------------------------------------------------------------------------------
61 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
62 // const wxSize&, int, const wxString[], int, long,
63 // const wxValidator&, const wxString&)
64 //-------------------------------------------------------------------------------------
65 // Contructor, creating and showing a radiobox
70 //-------------------------------------------------------------------------------------
72 //-------------------------------------------------------------------------------------
73 // Destructor, destroying the radiobox item
75 wxRadioBox::~wxRadioBox()
77 wxRadioButton
*next
,*current
;
79 current
=m_radioButtonCycle
->NextInCycle();
80 next
=current
->NextInCycle();
81 while (current
!=m_radioButtonCycle
) {
84 next
=current
->NextInCycle();
89 //-------------------------------------------------------------------------------------
91 //-------------------------------------------------------------------------------------
92 // Create the radiobox for two-step construction
94 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
95 const wxPoint
& pos
, const wxSize
& size
,
96 int n
, const wxString choices
[],
97 int majorDim
, long style
,
98 const wxValidator
& val
, const wxString
& name
)
103 m_noRowsOrCols
= majorDim
;
104 m_radioButtonCycle
= NULL
;
109 m_majorDim
= majorDim
;
115 MacPreControlCreate( parent
, id
, label
, pos
, size
,style
, val
, name
, &bounds
, title
) ;
117 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, false , 0 , 0 , 1,
118 kControlGroupBoxTextTitleProc
, (long) this ) ;
120 MacPostControlCreate() ;
122 for (i
= 0; i
< n
; i
++)
124 wxRadioButton
*radBtn
= new wxRadioButton(this, NewControlId(),choices
[i
],wxPoint(5,20*i
+10),
125 wxDefaultSize
, i
== 0 ? wxRB_GROUP
: 0 ) ;
127 m_radioButtonCycle
= radBtn
;
128 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
132 SetSize(pos
.x
,pos
.y
,size
.x
,size
.y
);
139 #pragma mark ### Specific functions (reference v2) ###
141 //-------------------------------------------------------------------------------------
143 //-------------------------------------------------------------------------------------
144 // Enables or disables the entire radiobox
146 bool wxRadioBox::Enable(bool enable
)
149 wxRadioButton
*current
;
151 if (!wxControl::Enable(enable
))
154 current
=m_radioButtonCycle
;
155 for (i
=0;i
<m_noItems
;i
++) {
156 current
->Enable(enable
);
157 current
=current
->NextInCycle();
162 //-------------------------------------------------------------------------------------
163 // ¥ Enable(int, bool)
164 //-------------------------------------------------------------------------------------
165 // Enables or disables an given button
167 void wxRadioBox::Enable(int item
, bool enable
)
170 wxRadioButton
*current
;
172 if ((item
< 0) || (item
>= m_noItems
))
175 current
=m_radioButtonCycle
;
178 current
=current
->NextInCycle();
183 //-------------------------------------------------------------------------------------
185 //-------------------------------------------------------------------------------------
186 // Finds a button matching the given string, returning the position if found
187 // or -1 if not found
189 int wxRadioBox::FindString(const wxString
& s
) const
192 wxRadioButton
*current
;
194 current
=m_radioButtonCycle
;
195 for (i
= 0; i
< m_noItems
; i
++)
197 if (s
== current
->GetLabel())
199 current
=current
->NextInCycle();
204 //-------------------------------------------------------------------------------------
206 //-------------------------------------------------------------------------------------
207 // Returns the radiobox label
209 wxString
wxRadioBox::GetLabel() const
211 return wxControl::GetLabel();
214 //-------------------------------------------------------------------------------------
216 //-------------------------------------------------------------------------------------
217 // Returns the label for the given button
219 wxString
wxRadioBox::GetLabel(int item
) const
222 wxRadioButton
*current
;
224 if ((item
< 0) || (item
>= m_noItems
))
227 current
=m_radioButtonCycle
;
230 current
=current
->NextInCycle();
232 return current
->GetLabel();
235 //-------------------------------------------------------------------------------------
237 //-------------------------------------------------------------------------------------
238 // Returns the zero-based position of the selected button
240 int wxRadioBox::GetSelection() const
243 wxRadioButton
*current
;
246 current
=m_radioButtonCycle
;
247 while (!current
->GetValue()) {
249 current
=current
->NextInCycle();
255 //-------------------------------------------------------------------------------------
257 //-------------------------------------------------------------------------------------
258 // Find string for position
260 wxString
wxRadioBox::GetString(int item
) const
263 return GetLabel(item
);
266 //-------------------------------------------------------------------------------------
267 // ¥ GetStringSelection
268 //-------------------------------------------------------------------------------------
269 // Returns the selected string
271 wxString
wxRadioBox::GetStringSelection () const
273 int sel
= GetSelection ();
275 return this->GetString (sel
);
280 //-------------------------------------------------------------------------------------
282 //-------------------------------------------------------------------------------------
283 // Returns the number of buttons in the radiobox
288 //-------------------------------------------------------------------------------------
289 // ¥ SetLabel(const wxString&)
290 //-------------------------------------------------------------------------------------
291 // Sets the radiobox label
293 void wxRadioBox::SetLabel(const wxString
& label
)
295 return wxControl::SetLabel(label
);
298 //-------------------------------------------------------------------------------------
299 // ¥ SetLabel(int, const wxString&)
300 //-------------------------------------------------------------------------------------
301 // Sets the label of a given button
303 void wxRadioBox::SetLabel(int item
,const wxString
& label
)
306 wxRadioButton
*current
;
308 if ((item
< 0) || (item
>= m_noItems
))
311 current
=m_radioButtonCycle
;
314 current
=current
->NextInCycle();
316 return current
->SetLabel(label
);
319 //-------------------------------------------------------------------------------------
321 //-------------------------------------------------------------------------------------
322 // Sets a button by passing the desired position. This does not cause
323 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
325 void wxRadioBox::SetSelection(int item
)
328 wxRadioButton
*current
;
330 if ((item
< 0) || (item
>= m_noItems
))
333 current
=m_radioButtonCycle
;
336 current
=current
->NextInCycle();
338 current
->SetValue(true);
342 //-------------------------------------------------------------------------------------
343 // ¥ SetStringSelection
344 //-------------------------------------------------------------------------------------
345 // Sets a button by passing the desired string. This does not cause
346 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
348 bool wxRadioBox::SetStringSelection (const wxString
& s
)
350 int sel
= FindString (s
);
360 //-------------------------------------------------------------------------------------
362 //-------------------------------------------------------------------------------------
363 // Shows or hides the entire radiobox
365 bool wxRadioBox::Show(bool show
)
368 wxRadioButton
*current
;
370 wxControl::Show(show
);
372 current
=m_radioButtonCycle
;
373 for (i
=0;i
<m_noItems
;i
++) {
375 current
=current
->NextInCycle();
380 //-------------------------------------------------------------------------------------
382 //-------------------------------------------------------------------------------------
383 // Shows or hides the given button
385 void wxRadioBox::Show(int item
, bool show
)
388 wxRadioButton
*current
;
390 if ((item
< 0) || (item
>= m_noItems
))
393 current
=m_radioButtonCycle
;
396 current
=current
->NextInCycle();
402 #pragma mark ### Other external functions ###
404 //-------------------------------------------------------------------------------------
406 //-------------------------------------------------------------------------------------
407 // Simulates the effect of the user issuing a command to the item
409 void wxRadioBox::Command (wxCommandEvent
& event
)
411 SetSelection (event
.GetInt());
412 ProcessCommand (event
);
415 //-------------------------------------------------------------------------------------
417 //-------------------------------------------------------------------------------------
418 // Sets the selected button to receive keyboard input
420 void wxRadioBox::SetFocus()
423 wxRadioButton
*current
;
426 current
=m_radioButtonCycle
;
427 while (!current
->GetValue()) {
429 current
=current
->NextInCycle();
436 #pragma mark ### Internal functions ###
438 //-------------------------------------------------------------------------------------
440 //-------------------------------------------------------------------------------------
441 // Simulates the effect of the user issuing a command to the item
443 #define RADIO_SIZE 40
445 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
448 wxRadioButton
*current
;
450 // define the position
452 int x_current
, y_current
;
453 int x_offset
,y_offset
;
454 int widthOld
, heightOld
;
455 GetSize(&widthOld
, &heightOld
);
459 GetPosition(&x_current
, &y_current
);
460 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
461 x_offset
= x_current
;
462 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
463 y_offset
= y_current
;
467 int charWidth
,charHeight
;
468 int maxWidth
,maxHeight
;
469 int eachWidth
[128],eachHeight
[128];
470 int totWidth
,totHeight
;
472 SetFont(GetParent()->GetFont());
473 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
478 for (i
= 0 ; i
< m_noItems
; i
++)
480 GetTextExtent(GetLabel(i
), &eachWidth
[i
], &eachHeight
[i
]);
481 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
482 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
483 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
484 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
487 totHeight
= GetNumVer() * (maxHeight
+ charHeight
/2) + charHeight
*3/2;
488 totWidth
= GetNumHor() * (maxWidth
+ charWidth
) + charWidth
;
490 // only change our width/height if asked for
493 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
501 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
507 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
509 // arrange radiobuttons
519 current
=m_radioButtonCycle
;
520 for ( i
= 0 ; i
< m_noItems
; i
++)
522 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
524 if (m_windowStyle
& wxRA_VERTICAL
)
526 x_offset
+= maxWidth
+ charWidth
;
532 y_offset
+= maxHeight
; /*+ charHeight/2;*/
536 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
537 current
=current
->NextInCycle();
539 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
540 y_offset
+= maxHeight
; /*+ charHeight/2;*/
542 x_offset
+= maxWidth
+ charWidth
;
546 //-------------------------------------------------------------------------------------
548 //-------------------------------------------------------------------------------------
549 // return the number of buttons in the vertical direction
551 int wxRadioBox::GetNumVer() const
553 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
559 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
563 //-------------------------------------------------------------------------------------
565 //-------------------------------------------------------------------------------------
566 // return the number of buttons in the horizontal direction
568 int wxRadioBox::GetNumHor() const
570 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
572 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;