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
))
177 current
= m_radioButtonCycle
;
180 current
= current
->NextInCycle();
182 current
->Enable(enable
);
185 //-------------------------------------------------------------------------------------
187 //-------------------------------------------------------------------------------------
188 // Returns the radiobox label
190 wxString
wxRadioBox::GetLabel() const
192 return wxControl::GetLabel();
195 //-------------------------------------------------------------------------------------
197 //-------------------------------------------------------------------------------------
198 // Returns the label for the given button
200 wxString
wxRadioBox::GetString(int item
) const
203 wxRadioButton
*current
;
205 if ((item
< 0) || (item
>= m_noItems
))
209 current
= m_radioButtonCycle
;
212 current
= current
->NextInCycle();
214 return current
->GetLabel();
217 //-------------------------------------------------------------------------------------
219 //-------------------------------------------------------------------------------------
220 // Returns the zero-based position of the selected button
222 int wxRadioBox::GetSelection() const
225 wxRadioButton
*current
;
228 current
=m_radioButtonCycle
;
229 while (!current
->GetValue()) {
231 current
=current
->NextInCycle();
237 //-------------------------------------------------------------------------------------
239 //-------------------------------------------------------------------------------------
240 // Returns the number of buttons in the radiobox
245 //-------------------------------------------------------------------------------------
246 // ¥ SetLabel(const wxString&)
247 //-------------------------------------------------------------------------------------
248 // Sets the radiobox label
250 void wxRadioBox::SetLabel(const wxString
& label
)
252 return wxControl::SetLabel(label
);
255 //-------------------------------------------------------------------------------------
256 // ¥ SetLabel(int, const wxString&)
257 //-------------------------------------------------------------------------------------
258 // Sets the label of a given button
260 void wxRadioBox::SetString(int item
,const wxString
& label
)
263 wxRadioButton
*current
;
265 if ((item
< 0) || (item
>= m_noItems
))
268 current
=m_radioButtonCycle
;
271 current
=current
->NextInCycle();
273 return current
->SetLabel(label
);
276 //-------------------------------------------------------------------------------------
278 //-------------------------------------------------------------------------------------
279 // Sets a button by passing the desired position. This does not cause
280 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
282 void wxRadioBox::SetSelection(int item
)
285 wxRadioButton
*current
;
287 if ((item
< 0) || (item
>= m_noItems
))
290 current
=m_radioButtonCycle
;
293 current
=current
->NextInCycle();
295 current
->SetValue(true);
299 //-------------------------------------------------------------------------------------
301 //-------------------------------------------------------------------------------------
302 // Shows or hides the entire radiobox
304 bool wxRadioBox::Show(bool show
)
307 wxRadioButton
*current
;
309 wxControl::Show(show
);
311 current
=m_radioButtonCycle
;
312 for (i
=0;i
<m_noItems
;i
++) {
314 current
=current
->NextInCycle();
319 //-------------------------------------------------------------------------------------
321 //-------------------------------------------------------------------------------------
322 // Shows or hides the given button
324 void wxRadioBox::Show(int item
, bool show
)
327 wxRadioButton
*current
;
329 if ((item
< 0) || (item
>= m_noItems
))
332 current
=m_radioButtonCycle
;
335 current
=current
->NextInCycle();
341 #pragma mark ### Other external functions ###
343 //-------------------------------------------------------------------------------------
345 //-------------------------------------------------------------------------------------
346 // Simulates the effect of the user issuing a command to the item
348 void wxRadioBox::Command (wxCommandEvent
& event
)
350 SetSelection (event
.GetInt());
351 ProcessCommand (event
);
354 //-------------------------------------------------------------------------------------
356 //-------------------------------------------------------------------------------------
357 // Sets the selected button to receive keyboard input
359 void wxRadioBox::SetFocus()
362 wxRadioButton
*current
;
365 current
=m_radioButtonCycle
;
366 while (!current
->GetValue()) {
368 current
=current
->NextInCycle();
375 #pragma mark ### Internal functions ###
377 //-------------------------------------------------------------------------------------
379 //-------------------------------------------------------------------------------------
380 // Simulates the effect of the user issuing a command to the item
382 #define RADIO_SIZE 40
384 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
387 wxRadioButton
*current
;
389 // define the position
391 int x_current
, y_current
;
392 int x_offset
,y_offset
;
393 int widthOld
, heightOld
;
394 GetSize(&widthOld
, &heightOld
);
398 GetPosition(&x_current
, &y_current
);
399 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
400 x_offset
= x_current
;
401 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
402 y_offset
= y_current
;
406 int charWidth
,charHeight
;
407 int maxWidth
,maxHeight
;
408 int eachWidth
[128],eachHeight
[128];
409 int totWidth
,totHeight
;
411 SetFont(GetParent()->GetFont());
412 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
417 for (i
= 0 ; i
< m_noItems
; i
++)
419 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
420 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
421 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
422 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
423 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
426 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
*3/2;
427 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
429 // only change our width/height if asked for
432 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
440 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
446 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
448 // arrange radiobuttons
458 current
=m_radioButtonCycle
;
459 for ( i
= 0 ; i
< m_noItems
; i
++)
461 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
463 if (m_windowStyle
& wxRA_VERTICAL
)
465 x_offset
+= maxWidth
+ charWidth
;
471 y_offset
+= maxHeight
; /*+ charHeight/2;*/
475 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
476 current
=current
->NextInCycle();
478 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
479 y_offset
+= maxHeight
; /*+ charHeight/2;*/
481 x_offset
+= maxWidth
+ charWidth
;
485 wxSize
wxRadioBox::DoGetBestSize() const
487 int charWidth
, charHeight
;
488 int maxWidth
, maxHeight
;
489 int eachWidth
, eachHeight
;
490 int totWidth
, totHeight
;
492 wxFont font
= GetParent()->GetFont();
493 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
494 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
500 for (int i
= 0 ; i
< m_noItems
; i
++)
502 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
);
503 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
504 eachHeight
= (int)((3 * eachHeight
) / 2);
505 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
506 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
509 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
* 3/2;
510 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
512 // handle radio box title as well
513 GetTextExtent(GetTitle(), &eachWidth
, NULL
);
514 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
515 if (totWidth
< eachWidth
)
516 totWidth
= eachWidth
;
518 return wxSize(totWidth
, totHeight
);
520 //-------------------------------------------------------------------------------------
522 //-------------------------------------------------------------------------------------
523 // return the number of buttons in the vertical direction
525 int wxRadioBox::GetRowCount() const
527 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
533 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
537 //-------------------------------------------------------------------------------------
539 //-------------------------------------------------------------------------------------
540 // return the number of buttons in the horizontal direction
542 int wxRadioBox::GetColumnCount() const
544 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
546 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;