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 //-------------------------------------------------------------------------------------
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 m_isBeingDeleted
= TRUE
;
82 wxRadioButton
*next
,*current
;
84 current
=m_radioButtonCycle
->NextInCycle();
85 next
=current
->NextInCycle();
86 while (current
!=m_radioButtonCycle
) {
89 next
=current
->NextInCycle();
94 //-------------------------------------------------------------------------------------
96 //-------------------------------------------------------------------------------------
97 // Create the radiobox for two-step construction
99 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
100 const wxPoint
& pos
, const wxSize
& size
,
101 int n
, const wxString choices
[],
102 int majorDim
, long style
,
103 const wxValidator
& val
, const wxString
& name
)
105 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
111 m_noRowsOrCols
= majorDim
;
112 m_radioButtonCycle
= NULL
;
117 m_majorDim
= majorDim
;
123 MacPreControlCreate( parent
, id
, wxStripMenuCodes(label
) , pos
, size
,style
, val
, name
, &bounds
, title
) ;
125 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
126 kControlGroupBoxTextTitleProc
, (long) this ) ;
128 for (i
= 0; i
< n
; i
++)
130 wxRadioButton
*radBtn
= new wxRadioButton
134 wxStripMenuCodes(choices
[i
]),
137 i
== 0 ? wxRB_GROUP
: 0
140 m_radioButtonCycle
= radBtn
;
141 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
145 MacPostControlCreate() ;
152 #pragma mark ### Specific functions (reference v2) ###
154 //-------------------------------------------------------------------------------------
156 //-------------------------------------------------------------------------------------
157 // Enables or disables the entire radiobox
159 bool wxRadioBox::Enable(bool enable
)
162 wxRadioButton
*current
;
164 if (!wxControl::Enable(enable
))
167 current
= m_radioButtonCycle
;
168 for (i
= 0; i
< m_noItems
; i
++) {
169 current
->Enable(enable
);
170 current
= current
->NextInCycle();
175 //-------------------------------------------------------------------------------------
176 // ¥ Enable(int, bool)
177 //-------------------------------------------------------------------------------------
178 // Enables or disables an given button
180 void wxRadioBox::Enable(int item
, bool enable
)
183 wxRadioButton
*current
;
185 if ((item
< 0) || (item
>= m_noItems
))
189 current
= m_radioButtonCycle
;
192 current
= current
->NextInCycle();
194 current
->Enable(enable
);
197 //-------------------------------------------------------------------------------------
199 //-------------------------------------------------------------------------------------
200 // Returns the radiobox label
202 wxString
wxRadioBox::GetLabel() const
204 return wxControl::GetLabel();
207 //-------------------------------------------------------------------------------------
209 //-------------------------------------------------------------------------------------
210 // Returns the label for the given button
212 wxString
wxRadioBox::GetString(int item
) const
215 wxRadioButton
*current
;
217 if ((item
< 0) || (item
>= m_noItems
))
218 return wxEmptyString
;
221 current
= m_radioButtonCycle
;
224 current
= current
->NextInCycle();
226 return current
->GetLabel();
229 //-------------------------------------------------------------------------------------
231 //-------------------------------------------------------------------------------------
232 // Returns the zero-based position of the selected button
234 int wxRadioBox::GetSelection() const
237 wxRadioButton
*current
;
240 current
=m_radioButtonCycle
;
241 while (!current
->GetValue()) {
243 current
=current
->NextInCycle();
249 //-------------------------------------------------------------------------------------
251 //-------------------------------------------------------------------------------------
252 // Returns the number of buttons in the radiobox
257 //-------------------------------------------------------------------------------------
258 // ¥ SetLabel(const wxString&)
259 //-------------------------------------------------------------------------------------
260 // Sets the radiobox label
262 void wxRadioBox::SetLabel(const wxString
& label
)
264 return wxControl::SetLabel(label
);
267 //-------------------------------------------------------------------------------------
268 // ¥ SetLabel(int, const wxString&)
269 //-------------------------------------------------------------------------------------
270 // Sets the label of a given button
272 void wxRadioBox::SetString(int item
,const wxString
& label
)
275 wxRadioButton
*current
;
277 if ((item
< 0) || (item
>= m_noItems
))
280 current
=m_radioButtonCycle
;
283 current
=current
->NextInCycle();
285 return current
->SetLabel(label
);
288 //-------------------------------------------------------------------------------------
290 //-------------------------------------------------------------------------------------
291 // Sets a button by passing the desired position. This does not cause
292 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
294 void wxRadioBox::SetSelection(int item
)
297 wxRadioButton
*current
;
299 if ((item
< 0) || (item
>= m_noItems
))
302 current
=m_radioButtonCycle
;
305 current
=current
->NextInCycle();
307 current
->SetValue(true);
311 //-------------------------------------------------------------------------------------
313 //-------------------------------------------------------------------------------------
314 // Shows or hides the entire radiobox
316 bool wxRadioBox::Show(bool show
)
319 wxRadioButton
*current
;
321 wxControl::Show(show
);
323 current
=m_radioButtonCycle
;
324 for (i
=0;i
<m_noItems
;i
++) {
326 current
=current
->NextInCycle();
331 //-------------------------------------------------------------------------------------
333 //-------------------------------------------------------------------------------------
334 // Shows or hides the given button
336 void wxRadioBox::Show(int item
, bool show
)
339 wxRadioButton
*current
;
341 if ((item
< 0) || (item
>= m_noItems
))
344 current
=m_radioButtonCycle
;
347 current
=current
->NextInCycle();
353 #pragma mark ### Other external functions ###
355 //-------------------------------------------------------------------------------------
357 //-------------------------------------------------------------------------------------
358 // Simulates the effect of the user issuing a command to the item
360 void wxRadioBox::Command (wxCommandEvent
& event
)
362 SetSelection (event
.GetInt());
363 ProcessCommand (event
);
366 //-------------------------------------------------------------------------------------
368 //-------------------------------------------------------------------------------------
369 // Sets the selected button to receive keyboard input
371 void wxRadioBox::SetFocus()
374 wxRadioButton
*current
;
377 current
=m_radioButtonCycle
;
378 while (!current
->GetValue()) {
380 current
=current
->NextInCycle();
387 #pragma mark ### Internal functions ###
389 //-------------------------------------------------------------------------------------
391 //-------------------------------------------------------------------------------------
392 // Simulates the effect of the user issuing a command to the item
394 #define RADIO_SIZE 20
396 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
399 wxRadioButton
*current
;
401 // define the position
403 int x_current
, y_current
;
404 int x_offset
,y_offset
;
405 int widthOld
, heightOld
;
406 GetSize(&widthOld
, &heightOld
);
410 GetPosition(&x_current
, &y_current
);
411 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
412 x_offset
= x_current
;
413 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
414 y_offset
= y_current
;
418 int charWidth
,charHeight
;
419 int maxWidth
,maxHeight
;
420 int eachWidth
[128],eachHeight
[128];
421 int totWidth
,totHeight
;
423 SetFont(GetParent()->GetFont());
424 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
429 for (i
= 0 ; i
< m_noItems
; i
++)
431 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
432 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
433 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
434 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
435 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
438 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
439 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
441 // only change our width/height if asked for
444 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
452 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
458 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
460 // arrange radiobuttons
470 current
=m_radioButtonCycle
;
471 for ( i
= 0 ; i
< m_noItems
; i
++)
473 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
475 if (m_windowStyle
& wxRA_VERTICAL
)
477 x_offset
+= maxWidth
+ charWidth
;
483 y_offset
+= maxHeight
; /*+ charHeight/2;*/
487 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
488 current
=current
->NextInCycle();
490 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
491 y_offset
+= maxHeight
; /*+ charHeight/2;*/
493 x_offset
+= maxWidth
+ charWidth
;
497 wxSize
wxRadioBox::DoGetBestSize() const
499 int charWidth
, charHeight
;
500 int maxWidth
, maxHeight
;
501 int eachWidth
, eachHeight
;
502 int totWidth
, totHeight
;
504 wxFont font
= GetParent()->GetFont();
505 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
506 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
512 for (int i
= 0 ; i
< m_noItems
; i
++)
514 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
);
515 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
516 eachHeight
= (int)((3 * eachHeight
) / 2);
517 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
518 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
521 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
522 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
524 // handle radio box title as well
525 GetTextExtent(GetTitle(), &eachWidth
, NULL
);
526 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
527 if (totWidth
< eachWidth
)
528 totWidth
= eachWidth
;
530 return wxSize(totWidth
, totHeight
);
532 //-------------------------------------------------------------------------------------
534 //-------------------------------------------------------------------------------------
535 // return the number of buttons in the vertical direction
537 int wxRadioBox::GetRowCount() const
539 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
545 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
549 //-------------------------------------------------------------------------------------
551 //-------------------------------------------------------------------------------------
552 // return the number of buttons in the horizontal direction
554 int wxRadioBox::GetColumnCount() const
556 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
558 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;