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 //-------------------------------------------------------------------------------------
22 #include "wx/arrstr.h"
24 #include "wx/radiobox.h"
25 #include "wx/radiobut.h"
26 #include "wx/mac/uma.h"
28 #if !USE_SHARED_LIBRARY
29 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
32 //-------------------------------------------------------------------------------------
34 //-------------------------------------------------------------------------------------
35 // Default constructor
36 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
37 EVT_RADIOBUTTON( -1 , wxRadioBox::OnRadioButton
)
40 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
42 if ( outer
.IsChecked() )
44 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
45 int i
= GetSelection() ;
47 event
.SetString( GetString( i
) );
48 event
.SetEventObject( this );
49 ProcessCommand(event
);
53 wxRadioBox::wxRadioBox()
58 m_radioButtonCycle
= NULL
;
61 //-------------------------------------------------------------------------------------
62 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
63 // const wxSize&, int, const wxString[], int, long,
64 // const wxValidator&, const wxString&)
65 //-------------------------------------------------------------------------------------
66 // Contructor, creating and showing a radiobox
71 //-------------------------------------------------------------------------------------
73 //-------------------------------------------------------------------------------------
74 // Destructor, destroying the radiobox item
76 wxRadioBox::~wxRadioBox()
78 m_isBeingDeleted
= TRUE
;
80 wxRadioButton
*next
,*current
;
82 current
=m_radioButtonCycle
->NextInCycle();
83 next
=current
->NextInCycle();
84 while (current
!=m_radioButtonCycle
) {
87 next
=current
->NextInCycle();
92 //-------------------------------------------------------------------------------------
94 //-------------------------------------------------------------------------------------
95 // Create the radiobox for two-step construction
97 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
98 const wxPoint
& pos
, const wxSize
& size
,
99 const wxArrayString
& choices
,
100 int majorDim
, long style
,
101 const wxValidator
& val
, const wxString
& name
)
103 wxCArrayString
chs(choices
);
105 return Create(parent
, id
, label
, pos
, size
, chs
.GetCount(),
106 chs
.GetStrings(), majorDim
, style
, val
, name
);
109 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
110 const wxPoint
& pos
, const wxSize
& size
,
111 int n
, const wxString choices
[],
112 int majorDim
, long style
,
113 const wxValidator
& val
, const wxString
& name
)
115 m_macIsUserPane
= FALSE
;
117 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
123 m_noRowsOrCols
= majorDim
;
124 m_radioButtonCycle
= NULL
;
129 m_majorDim
= majorDim
;
134 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
135 if( bounds
.right
<= bounds
.left
)
136 bounds
.right
= bounds
.left
+ 100 ;
137 if ( bounds
.bottom
<= bounds
.top
)
138 bounds
.bottom
= bounds
.top
+ 100 ;
140 verify_noerr(CreateGroupBoxControl(MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),&bounds
, CFSTR("") ,
141 true /*primary*/ , (ControlRef
*)&m_macControl
) ) ;
143 for (i
= 0; i
< n
; i
++)
145 wxRadioButton
*radBtn
= new wxRadioButton
149 wxStripMenuCodes(choices
[i
]),
152 i
== 0 ? wxRB_GROUP
: 0
155 m_radioButtonCycle
= radBtn
;
156 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
160 MacPostControlCreate(pos
,size
) ;
166 //-------------------------------------------------------------------------------------
168 //-------------------------------------------------------------------------------------
169 // Enables or disables the entire radiobox
171 bool wxRadioBox::Enable(bool enable
)
174 wxRadioButton
*current
;
176 if (!wxControl::Enable(enable
))
179 current
= m_radioButtonCycle
;
180 for (i
= 0; i
< m_noItems
; i
++) {
181 current
->Enable(enable
);
182 current
= current
->NextInCycle();
187 //-------------------------------------------------------------------------------------
188 // ¥ Enable(int, bool)
189 //-------------------------------------------------------------------------------------
190 // Enables or disables an given button
192 void wxRadioBox::Enable(int item
, bool enable
)
195 wxRadioButton
*current
;
197 if ((item
< 0) || (item
>= m_noItems
))
201 current
= m_radioButtonCycle
;
204 current
= current
->NextInCycle();
206 current
->Enable(enable
);
209 //-------------------------------------------------------------------------------------
211 //-------------------------------------------------------------------------------------
212 // Returns the radiobox label
214 wxString
wxRadioBox::GetLabel() const
216 return wxControl::GetLabel();
219 //-------------------------------------------------------------------------------------
221 //-------------------------------------------------------------------------------------
222 // Returns the label for the given button
224 wxString
wxRadioBox::GetString(int item
) const
227 wxRadioButton
*current
;
229 if ((item
< 0) || (item
>= m_noItems
))
230 return wxEmptyString
;
233 current
= m_radioButtonCycle
;
236 current
= current
->NextInCycle();
238 return current
->GetLabel();
241 //-------------------------------------------------------------------------------------
243 //-------------------------------------------------------------------------------------
244 // Returns the zero-based position of the selected button
246 int wxRadioBox::GetSelection() const
249 wxRadioButton
*current
;
252 current
=m_radioButtonCycle
;
253 while (!current
->GetValue()) {
255 current
=current
->NextInCycle();
261 //-------------------------------------------------------------------------------------
263 //-------------------------------------------------------------------------------------
264 // Returns the number of buttons in the radiobox
269 //-------------------------------------------------------------------------------------
270 // ¥ SetLabel(const wxString&)
271 //-------------------------------------------------------------------------------------
272 // Sets the radiobox label
274 void wxRadioBox::SetLabel(const wxString
& label
)
276 return wxControl::SetLabel(label
);
279 //-------------------------------------------------------------------------------------
280 // ¥ SetLabel(int, const wxString&)
281 //-------------------------------------------------------------------------------------
282 // Sets the label of a given button
284 void wxRadioBox::SetString(int item
,const wxString
& label
)
287 wxRadioButton
*current
;
289 if ((item
< 0) || (item
>= m_noItems
))
292 current
=m_radioButtonCycle
;
295 current
=current
->NextInCycle();
297 return current
->SetLabel(label
);
300 //-------------------------------------------------------------------------------------
302 //-------------------------------------------------------------------------------------
303 // Sets a button by passing the desired position. This does not cause
304 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
306 void wxRadioBox::SetSelection(int item
)
309 wxRadioButton
*current
;
311 if ((item
< 0) || (item
>= m_noItems
))
314 current
=m_radioButtonCycle
;
317 current
=current
->NextInCycle();
319 current
->SetValue(true);
323 //-------------------------------------------------------------------------------------
325 //-------------------------------------------------------------------------------------
326 // Shows or hides the entire radiobox
328 bool wxRadioBox::Show(bool show
)
331 wxRadioButton
*current
;
333 wxControl::Show(show
);
335 current
=m_radioButtonCycle
;
336 for (i
=0;i
<m_noItems
;i
++) {
338 current
=current
->NextInCycle();
343 //-------------------------------------------------------------------------------------
345 //-------------------------------------------------------------------------------------
346 // Shows or hides the given button
348 void wxRadioBox::Show(int item
, bool show
)
351 wxRadioButton
*current
;
353 if ((item
< 0) || (item
>= m_noItems
))
356 current
=m_radioButtonCycle
;
359 current
=current
->NextInCycle();
364 //-------------------------------------------------------------------------------------
366 //-------------------------------------------------------------------------------------
367 // Simulates the effect of the user issuing a command to the item
369 void wxRadioBox::Command (wxCommandEvent
& event
)
371 SetSelection (event
.GetInt());
372 ProcessCommand (event
);
375 //-------------------------------------------------------------------------------------
377 //-------------------------------------------------------------------------------------
378 // Sets the selected button to receive keyboard input
380 void wxRadioBox::SetFocus()
383 wxRadioButton
*current
;
386 current
=m_radioButtonCycle
;
387 while (!current
->GetValue()) {
389 current
=current
->NextInCycle();
395 //-------------------------------------------------------------------------------------
397 //-------------------------------------------------------------------------------------
398 // Simulates the effect of the user issuing a command to the item
400 #define RADIO_SIZE 20
402 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
405 wxRadioButton
*current
;
407 // define the position
409 int x_current
, y_current
;
410 int x_offset
,y_offset
;
411 int widthOld
, heightOld
;
412 GetSize(&widthOld
, &heightOld
);
416 GetPosition(&x_current
, &y_current
);
417 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
418 x_offset
= x_current
;
419 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
420 y_offset
= y_current
;
424 int charWidth
,charHeight
;
425 int maxWidth
,maxHeight
;
426 int eachWidth
[128],eachHeight
[128];
427 int totWidth
,totHeight
;
429 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
435 for (i
= 0 ; i
< m_noItems
; i
++)
437 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
438 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
439 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
440 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
441 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
444 totHeight
= GetRowCount() * ( maxHeight
) ;
445 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
447 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
449 // only change our width/height if asked for
452 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
460 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
466 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
468 // arrange radiobuttons
479 current
=m_radioButtonCycle
;
480 for ( i
= 0 ; i
< m_noItems
; i
++)
482 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
484 if (m_windowStyle
& wxRA_VERTICAL
)
486 x_offset
+= maxWidth
+ charWidth
;
492 y_offset
+= maxHeight
; /*+ charHeight/2;*/
496 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
497 current
=current
->NextInCycle();
499 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
500 y_offset
+= maxHeight
; /*+ charHeight/2;*/
502 x_offset
+= maxWidth
+ charWidth
;
506 wxSize
wxRadioBox::DoGetBestSize() const
508 int charWidth
, charHeight
;
509 int maxWidth
, maxHeight
;
510 int eachWidth
, eachHeight
;
511 int totWidth
, totHeight
;
513 wxFont font
= GetParent()->GetFont();
514 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
515 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
522 for (int i
= 0 ; i
< m_noItems
; i
++)
524 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
,NULL
, NULL
, &font
);
525 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
526 eachHeight
= (int)((3 * eachHeight
) / 2);
527 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
528 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
531 totHeight
= GetRowCount() * (maxHeight
) ;
532 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
534 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
538 // handle radio box title as well
539 GetTextExtent(GetTitle(), &eachWidth
, NULL
);
540 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
541 if (totWidth
< eachWidth
)
542 totWidth
= eachWidth
;
544 return wxSize(totWidth
, totHeight
);
546 //-------------------------------------------------------------------------------------
548 //-------------------------------------------------------------------------------------
549 // return the number of buttons in the vertical direction
551 int wxRadioBox::GetRowCount() 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::GetColumnCount() const
570 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
572 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;