1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
5 // Modified by: JS Lair (99/11/15) first implementation
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "radioboxbase.h"
14 #pragma implementation "radiobox.h"
17 //-------------------------------------------------------------------------------------
19 //-------------------------------------------------------------------------------------
23 #include "wx/radiobox.h"
24 #include "wx/radiobut.h"
25 #include "wx/mac/uma.h"
27 #if !USE_SHARED_LIBRARY
28 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
32 #pragma mark ### Constructors & destructor ###
34 //-------------------------------------------------------------------------------------
36 //-------------------------------------------------------------------------------------
37 // Default constructor
38 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
39 EVT_RADIOBUTTON( -1 , wxRadioBox::OnRadioButton
)
42 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
44 if ( outer
.IsChecked() )
46 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
47 int i
= GetSelection() ;
49 event
.SetString( GetString( i
) );
50 event
.SetEventObject( this );
51 ProcessCommand(event
);
55 wxRadioBox::wxRadioBox()
60 m_radioButtonCycle
= NULL
;
63 //-------------------------------------------------------------------------------------
64 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
65 // const wxSize&, int, const wxString[], int, long,
66 // const wxValidator&, const wxString&)
67 //-------------------------------------------------------------------------------------
68 // Contructor, creating and showing a radiobox
73 //-------------------------------------------------------------------------------------
75 //-------------------------------------------------------------------------------------
76 // Destructor, destroying the radiobox item
78 wxRadioBox::~wxRadioBox()
80 m_isBeingDeleted
= TRUE
;
82 wxRadioButton
*next
,*current
;
84 current
=m_radioButtonCycle
->NextInCycle();
85 next
=current
->NextInCycle();
86 while (current
!=m_radioButtonCycle
) {
89 next
=current
->NextInCycle();
94 //-------------------------------------------------------------------------------------
96 //-------------------------------------------------------------------------------------
97 // Create the radiobox for two-step construction
99 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
100 const wxPoint
& pos
, const wxSize
& size
,
101 int n
, const wxString choices
[],
102 int majorDim
, long style
,
103 const wxValidator
& val
, const wxString
& name
)
108 m_noRowsOrCols
= majorDim
;
109 m_radioButtonCycle
= NULL
;
114 m_majorDim
= majorDim
;
120 MacPreControlCreate( parent
, id
, wxStripMenuCodes(label
) , pos
, size
,style
, val
, name
, &bounds
, title
) ;
122 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
123 kControlGroupBoxTextTitleProc
, (long) this ) ;
125 for (i
= 0; i
< n
; i
++)
127 wxRadioButton
*radBtn
= new wxRadioButton(this, NewControlId(), wxStripMenuCodes(choices
[i
]),wxPoint(5,20*i
+10),
128 wxDefaultSize
, i
== 0 ? wxRB_GROUP
: 0 ) ;
130 m_radioButtonCycle
= radBtn
;
131 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
135 MacPostControlCreate() ;
142 #pragma mark ### Specific functions (reference v2) ###
144 //-------------------------------------------------------------------------------------
146 //-------------------------------------------------------------------------------------
147 // Enables or disables the entire radiobox
149 bool wxRadioBox::Enable(bool enable
)
152 wxRadioButton
*current
;
154 if (!wxControl::Enable(enable
))
157 current
= m_radioButtonCycle
;
158 for (i
= 0; i
< m_noItems
; i
++) {
159 current
->Enable(enable
);
160 current
= current
->NextInCycle();
165 //-------------------------------------------------------------------------------------
166 // ¥ Enable(int, bool)
167 //-------------------------------------------------------------------------------------
168 // Enables or disables an given button
170 void wxRadioBox::Enable(int item
, bool enable
)
173 wxRadioButton
*current
;
175 if ((item
< 0) || (item
>= m_noItems
))
179 current
= m_radioButtonCycle
;
182 current
= current
->NextInCycle();
184 current
->Enable(enable
);
187 //-------------------------------------------------------------------------------------
189 //-------------------------------------------------------------------------------------
190 // Returns the radiobox label
192 wxString
wxRadioBox::GetLabel() const
194 return wxControl::GetLabel();
197 //-------------------------------------------------------------------------------------
199 //-------------------------------------------------------------------------------------
200 // Returns the label for the given button
202 wxString
wxRadioBox::GetString(int item
) const
205 wxRadioButton
*current
;
207 if ((item
< 0) || (item
>= m_noItems
))
208 return wxEmptyString
;
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 // Returns the number of buttons in the radiobox
247 //-------------------------------------------------------------------------------------
248 // ¥ SetLabel(const wxString&)
249 //-------------------------------------------------------------------------------------
250 // Sets the radiobox label
252 void wxRadioBox::SetLabel(const wxString
& label
)
254 return wxControl::SetLabel(label
);
257 //-------------------------------------------------------------------------------------
258 // ¥ SetLabel(int, const wxString&)
259 //-------------------------------------------------------------------------------------
260 // Sets the label of a given button
262 void wxRadioBox::SetString(int item
,const wxString
& label
)
265 wxRadioButton
*current
;
267 if ((item
< 0) || (item
>= m_noItems
))
270 current
=m_radioButtonCycle
;
273 current
=current
->NextInCycle();
275 return current
->SetLabel(label
);
278 //-------------------------------------------------------------------------------------
280 //-------------------------------------------------------------------------------------
281 // Sets a button by passing the desired position. This does not cause
282 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
284 void wxRadioBox::SetSelection(int item
)
287 wxRadioButton
*current
;
289 if ((item
< 0) || (item
>= m_noItems
))
292 current
=m_radioButtonCycle
;
295 current
=current
->NextInCycle();
297 current
->SetValue(true);
301 //-------------------------------------------------------------------------------------
303 //-------------------------------------------------------------------------------------
304 // Shows or hides the entire radiobox
306 bool wxRadioBox::Show(bool show
)
309 wxRadioButton
*current
;
311 wxControl::Show(show
);
313 current
=m_radioButtonCycle
;
314 for (i
=0;i
<m_noItems
;i
++) {
316 current
=current
->NextInCycle();
321 //-------------------------------------------------------------------------------------
323 //-------------------------------------------------------------------------------------
324 // Shows or hides the given button
326 void wxRadioBox::Show(int item
, bool show
)
329 wxRadioButton
*current
;
331 if ((item
< 0) || (item
>= m_noItems
))
334 current
=m_radioButtonCycle
;
337 current
=current
->NextInCycle();
343 #pragma mark ### Other external functions ###
345 //-------------------------------------------------------------------------------------
347 //-------------------------------------------------------------------------------------
348 // Simulates the effect of the user issuing a command to the item
350 void wxRadioBox::Command (wxCommandEvent
& event
)
352 SetSelection (event
.GetInt());
353 ProcessCommand (event
);
356 //-------------------------------------------------------------------------------------
358 //-------------------------------------------------------------------------------------
359 // Sets the selected button to receive keyboard input
361 void wxRadioBox::SetFocus()
364 wxRadioButton
*current
;
367 current
=m_radioButtonCycle
;
368 while (!current
->GetValue()) {
370 current
=current
->NextInCycle();
377 #pragma mark ### Internal functions ###
379 //-------------------------------------------------------------------------------------
381 //-------------------------------------------------------------------------------------
382 // Simulates the effect of the user issuing a command to the item
384 #define RADIO_SIZE 20
386 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
389 wxRadioButton
*current
;
391 // define the position
393 int x_current
, y_current
;
394 int x_offset
,y_offset
;
395 int widthOld
, heightOld
;
396 GetSize(&widthOld
, &heightOld
);
400 GetPosition(&x_current
, &y_current
);
401 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
402 x_offset
= x_current
;
403 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
404 y_offset
= y_current
;
408 int charWidth
,charHeight
;
409 int maxWidth
,maxHeight
;
410 int eachWidth
[128],eachHeight
[128];
411 int totWidth
,totHeight
;
413 SetFont(GetParent()->GetFont());
414 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
419 for (i
= 0 ; i
< m_noItems
; i
++)
421 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
422 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
423 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
424 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
425 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
428 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
429 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
431 // only change our width/height if asked for
434 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
442 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
448 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
450 // arrange radiobuttons
460 current
=m_radioButtonCycle
;
461 for ( i
= 0 ; i
< m_noItems
; i
++)
463 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
465 if (m_windowStyle
& wxRA_VERTICAL
)
467 x_offset
+= maxWidth
+ charWidth
;
473 y_offset
+= maxHeight
; /*+ charHeight/2;*/
477 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
478 current
=current
->NextInCycle();
480 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
481 y_offset
+= maxHeight
; /*+ charHeight/2;*/
483 x_offset
+= maxWidth
+ charWidth
;
487 wxSize
wxRadioBox::DoGetBestSize() const
489 int charWidth
, charHeight
;
490 int maxWidth
, maxHeight
;
491 int eachWidth
, eachHeight
;
492 int totWidth
, totHeight
;
494 wxFont font
= GetParent()->GetFont();
495 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
496 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
502 for (int i
= 0 ; i
< m_noItems
; i
++)
504 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
);
505 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
506 eachHeight
= (int)((3 * eachHeight
) / 2);
507 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
508 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
511 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
512 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
514 // handle radio box title as well
515 GetTextExtent(GetTitle(), &eachWidth
, NULL
);
516 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
517 if (totWidth
< eachWidth
)
518 totWidth
= eachWidth
;
520 return wxSize(totWidth
, totHeight
);
522 //-------------------------------------------------------------------------------------
524 //-------------------------------------------------------------------------------------
525 // return the number of buttons in the vertical direction
527 int wxRadioBox::GetRowCount() const
529 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
535 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
539 //-------------------------------------------------------------------------------------
541 //-------------------------------------------------------------------------------------
542 // return the number of buttons in the horizontal direction
544 int wxRadioBox::GetColumnCount() const
546 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
548 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;