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"
22 #include "wx/radiobox.h"
23 #include "wx/radiobut.h"
24 #include "wx/mac/uma.h"
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
31 #pragma mark ### Constructors & destructor ###
33 //-------------------------------------------------------------------------------------
35 //-------------------------------------------------------------------------------------
36 // Default constructor
37 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
38 EVT_RADIOBUTTON( -1 , wxRadioBox::OnRadioButton
)
41 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
43 if ( outer
.IsChecked() )
45 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
46 int i
= GetSelection() ;
48 event
.SetString( GetString( i
) );
49 event
.SetEventObject( this );
50 ProcessCommand(event
);
54 wxRadioBox::wxRadioBox()
59 m_radioButtonCycle
= NULL
;
62 //-------------------------------------------------------------------------------------
63 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
64 // const wxSize&, int, const wxString[], int, long,
65 // const wxValidator&, const wxString&)
66 //-------------------------------------------------------------------------------------
67 // Contructor, creating and showing a radiobox
72 //-------------------------------------------------------------------------------------
74 //-------------------------------------------------------------------------------------
75 // Destructor, destroying the radiobox item
77 wxRadioBox::~wxRadioBox()
79 wxRadioButton
*next
,*current
;
81 current
=m_radioButtonCycle
->NextInCycle();
82 next
=current
->NextInCycle();
83 while (current
!=m_radioButtonCycle
) {
86 next
=current
->NextInCycle();
91 //-------------------------------------------------------------------------------------
93 //-------------------------------------------------------------------------------------
94 // Create the radiobox for two-step construction
96 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
97 const wxPoint
& pos
, const wxSize
& size
,
98 int n
, const wxString choices
[],
99 int majorDim
, long style
,
100 const wxValidator
& val
, const wxString
& name
)
105 m_noRowsOrCols
= majorDim
;
106 m_radioButtonCycle
= NULL
;
111 m_majorDim
= majorDim
;
117 MacPreControlCreate( parent
, id
, label
, pos
, size
,style
, val
, name
, &bounds
, title
) ;
119 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, false , 0 , 0 , 1,
120 kControlGroupBoxTextTitleProc
, (long) this ) ;
122 MacPostControlCreate() ;
124 for (i
= 0; i
< n
; i
++)
126 wxRadioButton
*radBtn
= new wxRadioButton(this, NewControlId(),choices
[i
],wxPoint(5,20*i
+10),
127 wxDefaultSize
, i
== 0 ? wxRB_GROUP
: 0 ) ;
129 m_radioButtonCycle
= radBtn
;
130 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
134 SetSize(pos
.x
,pos
.y
,size
.x
,size
.y
);
141 #pragma mark ### Specific functions (reference v2) ###
143 //-------------------------------------------------------------------------------------
145 //-------------------------------------------------------------------------------------
146 // Enables or disables the entire radiobox
148 bool wxRadioBox::Enable(bool enable
)
151 wxRadioButton
*current
;
153 if (!wxControl::Enable(enable
))
156 current
=m_radioButtonCycle
;
157 for (i
=0;i
<m_noItems
;i
++) {
158 current
->Enable(enable
);
159 current
=current
->NextInCycle();
164 //-------------------------------------------------------------------------------------
165 // ¥ Enable(int, bool)
166 //-------------------------------------------------------------------------------------
167 // Enables or disables an given button
169 void wxRadioBox::Enable(int item
, bool enable
)
172 wxRadioButton
*current
;
174 if ((item
< 0) || (item
>= m_noItems
))
177 current
=m_radioButtonCycle
;
180 current
=current
->NextInCycle();
184 //-------------------------------------------------------------------------------------
186 //-------------------------------------------------------------------------------------
187 // Returns the radiobox label
189 wxString
wxRadioBox::GetLabel() const
191 return wxControl::GetLabel();
194 //-------------------------------------------------------------------------------------
196 //-------------------------------------------------------------------------------------
197 // Returns the label for the given button
199 wxString
wxRadioBox::GetString(int item
) const
202 wxRadioButton
*current
;
204 if ((item
< 0) || (item
>= m_noItems
))
207 current
=m_radioButtonCycle
;
210 current
=current
->NextInCycle();
212 return current
->GetLabel();
215 //-------------------------------------------------------------------------------------
217 //-------------------------------------------------------------------------------------
218 // Returns the zero-based position of the selected button
220 int wxRadioBox::GetSelection() const
223 wxRadioButton
*current
;
226 current
=m_radioButtonCycle
;
227 while (!current
->GetValue()) {
229 current
=current
->NextInCycle();
235 //-------------------------------------------------------------------------------------
237 //-------------------------------------------------------------------------------------
238 // Returns the number of buttons in the radiobox
243 //-------------------------------------------------------------------------------------
244 // ¥ SetLabel(const wxString&)
245 //-------------------------------------------------------------------------------------
246 // Sets the radiobox label
248 void wxRadioBox::SetLabel(const wxString
& label
)
250 return wxControl::SetLabel(label
);
253 //-------------------------------------------------------------------------------------
254 // ¥ SetLabel(int, const wxString&)
255 //-------------------------------------------------------------------------------------
256 // Sets the label of a given button
258 void wxRadioBox::SetString(int item
,const wxString
& label
)
261 wxRadioButton
*current
;
263 if ((item
< 0) || (item
>= m_noItems
))
266 current
=m_radioButtonCycle
;
269 current
=current
->NextInCycle();
271 return current
->SetLabel(label
);
274 //-------------------------------------------------------------------------------------
276 //-------------------------------------------------------------------------------------
277 // Sets a button by passing the desired position. This does not cause
278 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
280 void wxRadioBox::SetSelection(int item
)
283 wxRadioButton
*current
;
285 if ((item
< 0) || (item
>= m_noItems
))
288 current
=m_radioButtonCycle
;
291 current
=current
->NextInCycle();
293 current
->SetValue(true);
297 //-------------------------------------------------------------------------------------
299 //-------------------------------------------------------------------------------------
300 // Shows or hides the entire radiobox
302 bool wxRadioBox::Show(bool show
)
305 wxRadioButton
*current
;
307 wxControl::Show(show
);
309 current
=m_radioButtonCycle
;
310 for (i
=0;i
<m_noItems
;i
++) {
312 current
=current
->NextInCycle();
317 //-------------------------------------------------------------------------------------
319 //-------------------------------------------------------------------------------------
320 // Shows or hides the given button
322 void wxRadioBox::Show(int item
, bool show
)
325 wxRadioButton
*current
;
327 if ((item
< 0) || (item
>= m_noItems
))
330 current
=m_radioButtonCycle
;
333 current
=current
->NextInCycle();
339 #pragma mark ### Other external functions ###
341 //-------------------------------------------------------------------------------------
343 //-------------------------------------------------------------------------------------
344 // Simulates the effect of the user issuing a command to the item
346 void wxRadioBox::Command (wxCommandEvent
& event
)
348 SetSelection (event
.GetInt());
349 ProcessCommand (event
);
352 //-------------------------------------------------------------------------------------
354 //-------------------------------------------------------------------------------------
355 // Sets the selected button to receive keyboard input
357 void wxRadioBox::SetFocus()
360 wxRadioButton
*current
;
363 current
=m_radioButtonCycle
;
364 while (!current
->GetValue()) {
366 current
=current
->NextInCycle();
373 #pragma mark ### Internal functions ###
375 //-------------------------------------------------------------------------------------
377 //-------------------------------------------------------------------------------------
378 // Simulates the effect of the user issuing a command to the item
380 #define RADIO_SIZE 40
382 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
385 wxRadioButton
*current
;
387 // define the position
389 int x_current
, y_current
;
390 int x_offset
,y_offset
;
391 int widthOld
, heightOld
;
392 GetSize(&widthOld
, &heightOld
);
396 GetPosition(&x_current
, &y_current
);
397 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
398 x_offset
= x_current
;
399 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
400 y_offset
= y_current
;
404 int charWidth
,charHeight
;
405 int maxWidth
,maxHeight
;
406 int eachWidth
[128],eachHeight
[128];
407 int totWidth
,totHeight
;
409 SetFont(GetParent()->GetFont());
410 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
415 for (i
= 0 ; i
< m_noItems
; i
++)
417 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
418 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
419 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
420 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
421 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
424 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
*3/2;
425 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
427 // only change our width/height if asked for
430 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
438 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
444 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
446 // arrange radiobuttons
456 current
=m_radioButtonCycle
;
457 for ( i
= 0 ; i
< m_noItems
; i
++)
459 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
461 if (m_windowStyle
& wxRA_VERTICAL
)
463 x_offset
+= maxWidth
+ charWidth
;
469 y_offset
+= maxHeight
; /*+ charHeight/2;*/
473 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
474 current
=current
->NextInCycle();
476 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
477 y_offset
+= maxHeight
; /*+ charHeight/2;*/
479 x_offset
+= maxWidth
+ charWidth
;
483 //-------------------------------------------------------------------------------------
485 //-------------------------------------------------------------------------------------
486 // return the number of buttons in the vertical direction
488 int wxRadioBox::GetRowCount() const
490 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
496 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
500 //-------------------------------------------------------------------------------------
502 //-------------------------------------------------------------------------------------
503 // return the number of buttons in the horizontal direction
505 int wxRadioBox::GetColumnCount() const
507 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
509 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;