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
, label
, pos
, size
,style
, val
, name
, &bounds
, title
) ;
120 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, false , 0 , 0 , 1,
121 kControlGroupBoxTextTitleProc
, (long) this ) ;
123 MacPostControlCreate() ;
125 for (i
= 0; i
< n
; i
++)
127 wxRadioButton
*radBtn
= new wxRadioButton(this, NewControlId(),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 SetSize(pos
.x
,pos
.y
,size
.x
,size
.y
);
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
))
178 current
=m_radioButtonCycle
;
181 current
=current
->NextInCycle();
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
))
208 current
=m_radioButtonCycle
;
211 current
=current
->NextInCycle();
213 return current
->GetLabel();
216 //-------------------------------------------------------------------------------------
218 //-------------------------------------------------------------------------------------
219 // Returns the zero-based position of the selected button
221 int wxRadioBox::GetSelection() const
224 wxRadioButton
*current
;
227 current
=m_radioButtonCycle
;
228 while (!current
->GetValue()) {
230 current
=current
->NextInCycle();
236 //-------------------------------------------------------------------------------------
238 //-------------------------------------------------------------------------------------
239 // Returns the number of buttons in the radiobox
244 //-------------------------------------------------------------------------------------
245 // ¥ SetLabel(const wxString&)
246 //-------------------------------------------------------------------------------------
247 // Sets the radiobox label
249 void wxRadioBox::SetLabel(const wxString
& label
)
251 return wxControl::SetLabel(label
);
254 //-------------------------------------------------------------------------------------
255 // ¥ SetLabel(int, const wxString&)
256 //-------------------------------------------------------------------------------------
257 // Sets the label of a given button
259 void wxRadioBox::SetString(int item
,const wxString
& label
)
262 wxRadioButton
*current
;
264 if ((item
< 0) || (item
>= m_noItems
))
267 current
=m_radioButtonCycle
;
270 current
=current
->NextInCycle();
272 return current
->SetLabel(label
);
275 //-------------------------------------------------------------------------------------
277 //-------------------------------------------------------------------------------------
278 // Sets a button by passing the desired position. This does not cause
279 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
281 void wxRadioBox::SetSelection(int item
)
284 wxRadioButton
*current
;
286 if ((item
< 0) || (item
>= m_noItems
))
289 current
=m_radioButtonCycle
;
292 current
=current
->NextInCycle();
294 current
->SetValue(true);
298 //-------------------------------------------------------------------------------------
300 //-------------------------------------------------------------------------------------
301 // Shows or hides the entire radiobox
303 bool wxRadioBox::Show(bool show
)
306 wxRadioButton
*current
;
308 wxControl::Show(show
);
310 current
=m_radioButtonCycle
;
311 for (i
=0;i
<m_noItems
;i
++) {
313 current
=current
->NextInCycle();
318 //-------------------------------------------------------------------------------------
320 //-------------------------------------------------------------------------------------
321 // Shows or hides the given button
323 void wxRadioBox::Show(int item
, bool show
)
326 wxRadioButton
*current
;
328 if ((item
< 0) || (item
>= m_noItems
))
331 current
=m_radioButtonCycle
;
334 current
=current
->NextInCycle();
340 #pragma mark ### Other external functions ###
342 //-------------------------------------------------------------------------------------
344 //-------------------------------------------------------------------------------------
345 // Simulates the effect of the user issuing a command to the item
347 void wxRadioBox::Command (wxCommandEvent
& event
)
349 SetSelection (event
.GetInt());
350 ProcessCommand (event
);
353 //-------------------------------------------------------------------------------------
355 //-------------------------------------------------------------------------------------
356 // Sets the selected button to receive keyboard input
358 void wxRadioBox::SetFocus()
361 wxRadioButton
*current
;
364 current
=m_radioButtonCycle
;
365 while (!current
->GetValue()) {
367 current
=current
->NextInCycle();
374 #pragma mark ### Internal functions ###
376 //-------------------------------------------------------------------------------------
378 //-------------------------------------------------------------------------------------
379 // Simulates the effect of the user issuing a command to the item
381 #define RADIO_SIZE 40
383 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
386 wxRadioButton
*current
;
388 // define the position
390 int x_current
, y_current
;
391 int x_offset
,y_offset
;
392 int widthOld
, heightOld
;
393 GetSize(&widthOld
, &heightOld
);
397 GetPosition(&x_current
, &y_current
);
398 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
399 x_offset
= x_current
;
400 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
401 y_offset
= y_current
;
405 int charWidth
,charHeight
;
406 int maxWidth
,maxHeight
;
407 int eachWidth
[128],eachHeight
[128];
408 int totWidth
,totHeight
;
410 SetFont(GetParent()->GetFont());
411 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
416 for (i
= 0 ; i
< m_noItems
; i
++)
418 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
419 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
420 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
421 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
422 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
425 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
*3/2;
426 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
428 // only change our width/height if asked for
431 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
439 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
445 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
447 // arrange radiobuttons
457 current
=m_radioButtonCycle
;
458 for ( i
= 0 ; i
< m_noItems
; i
++)
460 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
462 if (m_windowStyle
& wxRA_VERTICAL
)
464 x_offset
+= maxWidth
+ charWidth
;
470 y_offset
+= maxHeight
; /*+ charHeight/2;*/
474 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
475 current
=current
->NextInCycle();
477 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
478 y_offset
+= maxHeight
; /*+ charHeight/2;*/
480 x_offset
+= maxWidth
+ charWidth
;
484 //-------------------------------------------------------------------------------------
486 //-------------------------------------------------------------------------------------
487 // return the number of buttons in the vertical direction
489 int wxRadioBox::GetRowCount() const
491 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
497 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
501 //-------------------------------------------------------------------------------------
503 //-------------------------------------------------------------------------------------
504 // return the number of buttons in the horizontal direction
506 int wxRadioBox::GetColumnCount() const
508 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
510 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;