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 "radioboxbase.h"
18 #pragma implementation "radiobox.h"
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 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 int n
, const wxString choices
[],
100 int majorDim
, long style
,
101 const wxValidator
& val
, const wxString
& name
)
106 m_noRowsOrCols
= majorDim
;
107 m_radioButtonCycle
= NULL
;
112 m_majorDim
= majorDim
;
118 MacPreControlCreate( parent
, id
, wxStripMenuCodes(label
) , pos
, size
,style
, val
, name
, &bounds
, title
) ;
120 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
121 kControlGroupBoxTextTitleProc
, (long) this ) ;
123 for (i
= 0; i
< n
; i
++)
125 wxRadioButton
*radBtn
= new wxRadioButton(this, NewControlId(), wxStripMenuCodes(choices
[i
]),wxPoint(5,20*i
+10),
126 wxDefaultSize
, i
== 0 ? wxRB_GROUP
: 0 ) ;
128 m_radioButtonCycle
= radBtn
;
129 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
133 MacPostControlCreate() ;
140 #pragma mark ### Specific functions (reference v2) ###
142 //-------------------------------------------------------------------------------------
144 //-------------------------------------------------------------------------------------
145 // Enables or disables the entire radiobox
147 bool wxRadioBox::Enable(bool enable
)
150 wxRadioButton
*current
;
152 if (!wxControl::Enable(enable
))
155 current
=m_radioButtonCycle
;
156 for (i
=0;i
<m_noItems
;i
++) {
157 current
->Enable(enable
);
158 current
=current
->NextInCycle();
163 //-------------------------------------------------------------------------------------
164 // ¥ Enable(int, bool)
165 //-------------------------------------------------------------------------------------
166 // Enables or disables an given button
168 void wxRadioBox::Enable(int item
, bool enable
)
171 wxRadioButton
*current
;
173 if ((item
< 0) || (item
>= m_noItems
))
176 current
=m_radioButtonCycle
;
179 current
=current
->NextInCycle();
183 //-------------------------------------------------------------------------------------
185 //-------------------------------------------------------------------------------------
186 // Returns the radiobox label
188 wxString
wxRadioBox::GetLabel() const
190 return wxControl::GetLabel();
193 //-------------------------------------------------------------------------------------
195 //-------------------------------------------------------------------------------------
196 // Returns the label for the given button
198 wxString
wxRadioBox::GetString(int item
) const
201 wxRadioButton
*current
;
203 if ((item
< 0) || (item
>= m_noItems
))
206 current
=m_radioButtonCycle
;
209 current
=current
->NextInCycle();
211 return current
->GetLabel();
214 //-------------------------------------------------------------------------------------
216 //-------------------------------------------------------------------------------------
217 // Returns the zero-based position of the selected button
219 int wxRadioBox::GetSelection() const
222 wxRadioButton
*current
;
225 current
=m_radioButtonCycle
;
226 while (!current
->GetValue()) {
228 current
=current
->NextInCycle();
234 //-------------------------------------------------------------------------------------
236 //-------------------------------------------------------------------------------------
237 // Returns the number of buttons in the radiobox
242 //-------------------------------------------------------------------------------------
243 // ¥ SetLabel(const wxString&)
244 //-------------------------------------------------------------------------------------
245 // Sets the radiobox label
247 void wxRadioBox::SetLabel(const wxString
& label
)
249 return wxControl::SetLabel(label
);
252 //-------------------------------------------------------------------------------------
253 // ¥ SetLabel(int, const wxString&)
254 //-------------------------------------------------------------------------------------
255 // Sets the label of a given button
257 void wxRadioBox::SetString(int item
,const wxString
& label
)
260 wxRadioButton
*current
;
262 if ((item
< 0) || (item
>= m_noItems
))
265 current
=m_radioButtonCycle
;
268 current
=current
->NextInCycle();
270 return current
->SetLabel(label
);
273 //-------------------------------------------------------------------------------------
275 //-------------------------------------------------------------------------------------
276 // Sets a button by passing the desired position. This does not cause
277 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
279 void wxRadioBox::SetSelection(int item
)
282 wxRadioButton
*current
;
284 if ((item
< 0) || (item
>= m_noItems
))
287 current
=m_radioButtonCycle
;
290 current
=current
->NextInCycle();
292 current
->SetValue(true);
296 //-------------------------------------------------------------------------------------
298 //-------------------------------------------------------------------------------------
299 // Shows or hides the entire radiobox
301 bool wxRadioBox::Show(bool show
)
304 wxRadioButton
*current
;
306 wxControl::Show(show
);
308 current
=m_radioButtonCycle
;
309 for (i
=0;i
<m_noItems
;i
++) {
311 current
=current
->NextInCycle();
316 //-------------------------------------------------------------------------------------
318 //-------------------------------------------------------------------------------------
319 // Shows or hides the given button
321 void wxRadioBox::Show(int item
, bool show
)
324 wxRadioButton
*current
;
326 if ((item
< 0) || (item
>= m_noItems
))
329 current
=m_radioButtonCycle
;
332 current
=current
->NextInCycle();
338 #pragma mark ### Other external functions ###
340 //-------------------------------------------------------------------------------------
342 //-------------------------------------------------------------------------------------
343 // Simulates the effect of the user issuing a command to the item
345 void wxRadioBox::Command (wxCommandEvent
& event
)
347 SetSelection (event
.GetInt());
348 ProcessCommand (event
);
351 //-------------------------------------------------------------------------------------
353 //-------------------------------------------------------------------------------------
354 // Sets the selected button to receive keyboard input
356 void wxRadioBox::SetFocus()
359 wxRadioButton
*current
;
362 current
=m_radioButtonCycle
;
363 while (!current
->GetValue()) {
365 current
=current
->NextInCycle();
372 #pragma mark ### Internal functions ###
374 //-------------------------------------------------------------------------------------
376 //-------------------------------------------------------------------------------------
377 // Simulates the effect of the user issuing a command to the item
379 #define RADIO_SIZE 40
381 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
384 wxRadioButton
*current
;
386 // define the position
388 int x_current
, y_current
;
389 int x_offset
,y_offset
;
390 int widthOld
, heightOld
;
391 GetSize(&widthOld
, &heightOld
);
395 GetPosition(&x_current
, &y_current
);
396 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
397 x_offset
= x_current
;
398 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
399 y_offset
= y_current
;
403 int charWidth
,charHeight
;
404 int maxWidth
,maxHeight
;
405 int eachWidth
[128],eachHeight
[128];
406 int totWidth
,totHeight
;
408 SetFont(GetParent()->GetFont());
409 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
414 for (i
= 0 ; i
< m_noItems
; i
++)
416 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
417 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
418 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
419 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
420 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
423 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
*3/2;
424 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
426 // only change our width/height if asked for
429 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
437 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
443 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
445 // arrange radiobuttons
455 current
=m_radioButtonCycle
;
456 for ( i
= 0 ; i
< m_noItems
; i
++)
458 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
460 if (m_windowStyle
& wxRA_VERTICAL
)
462 x_offset
+= maxWidth
+ charWidth
;
468 y_offset
+= maxHeight
; /*+ charHeight/2;*/
472 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
473 current
=current
->NextInCycle();
475 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
476 y_offset
+= maxHeight
; /*+ charHeight/2;*/
478 x_offset
+= maxWidth
+ charWidth
;
482 wxSize
wxRadioBox::DoGetBestSize() const
484 int charWidth
, charHeight
;
485 int maxWidth
, maxHeight
;
486 int eachWidth
, eachHeight
;
487 int totWidth
, totHeight
;
489 wxFont font
= GetParent()->GetFont();
490 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
491 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
497 // handle radio box title as well
498 GetTextExtent(GetTitle(), &eachWidth
, NULL
);
499 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
);
500 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
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
* 3/2;
512 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
513 return wxSize(totWidth
, totHeight
);
515 //-------------------------------------------------------------------------------------
517 //-------------------------------------------------------------------------------------
518 // return the number of buttons in the vertical direction
520 int wxRadioBox::GetRowCount() const
522 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
528 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
532 //-------------------------------------------------------------------------------------
534 //-------------------------------------------------------------------------------------
535 // return the number of buttons in the horizontal direction
537 int wxRadioBox::GetColumnCount() const
539 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
541 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;