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 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "radioboxbase.h"
14 #pragma implementation "radiobox.h"
17 //-------------------------------------------------------------------------------------
19 //-------------------------------------------------------------------------------------
21 #include "wx/wxprec.h"
25 #include "wx/arrstr.h"
27 #include "wx/radiobox.h"
28 #include "wx/radiobut.h"
29 #include "wx/mac/uma.h"
31 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
33 //-------------------------------------------------------------------------------------
35 //-------------------------------------------------------------------------------------
36 // Default constructor
37 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
38 EVT_RADIOBUTTON( wxID_ANY
, 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 m_isBeingDeleted
= true;
81 wxRadioButton
*next
,*current
;
83 current
=m_radioButtonCycle
->NextInCycle();
84 next
=current
->NextInCycle();
85 while (current
!=m_radioButtonCycle
) {
88 next
=current
->NextInCycle();
93 //-------------------------------------------------------------------------------------
95 //-------------------------------------------------------------------------------------
96 // Create the radiobox for two-step construction
98 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
99 const wxPoint
& pos
, const wxSize
& size
,
100 const wxArrayString
& choices
,
101 int majorDim
, long style
,
102 const wxValidator
& val
, const wxString
& name
)
104 wxCArrayString
chs(choices
);
106 return Create(parent
, id
, label
, pos
, size
, chs
.GetCount(),
107 chs
.GetStrings(), majorDim
, style
, val
, name
);
110 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
111 const wxPoint
& pos
, const wxSize
& size
,
112 int n
, const wxString choices
[],
113 int majorDim
, long style
,
114 const wxValidator
& val
, const wxString
& name
)
116 m_macIsUserPane
= false ;
118 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
124 m_noRowsOrCols
= majorDim
;
125 m_radioButtonCycle
= NULL
;
130 m_majorDim
= majorDim
;
135 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
136 if( bounds
.right
<= bounds
.left
)
137 bounds
.right
= bounds
.left
+ 100 ;
138 if ( bounds
.bottom
<= bounds
.top
)
139 bounds
.bottom
= bounds
.top
+ 100 ;
141 m_peer
= new wxMacControl(this) ;
143 verify_noerr(CreateGroupBoxControl(MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),&bounds
, CFSTR("") ,
144 true /*primary*/ , m_peer
->GetControlRefAddr() ) ) ;
146 for (i
= 0; i
< n
; i
++)
148 wxRadioButton
*radBtn
= new wxRadioButton
152 wxStripMenuCodes(choices
[i
]),
155 i
== 0 ? wxRB_GROUP
: 0
158 m_radioButtonCycle
= radBtn
;
159 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
163 MacPostControlCreate(pos
,size
) ;
169 //-------------------------------------------------------------------------------------
171 //-------------------------------------------------------------------------------------
172 // Enables or disables the entire radiobox
174 bool wxRadioBox::Enable(bool enable
)
177 wxRadioButton
*current
;
179 if (!wxControl::Enable(enable
))
182 current
= m_radioButtonCycle
;
183 for (i
= 0; i
< m_noItems
; i
++) {
184 current
->Enable(enable
);
185 current
= current
->NextInCycle();
190 //-------------------------------------------------------------------------------------
191 // ¥ Enable(int, bool)
192 //-------------------------------------------------------------------------------------
193 // Enables or disables an given button
195 bool wxRadioBox::Enable(int item
, bool enable
)
198 wxRadioButton
*current
;
204 current
= m_radioButtonCycle
;
207 current
= current
->NextInCycle();
209 return current
->Enable(enable
);
212 //-------------------------------------------------------------------------------------
214 //-------------------------------------------------------------------------------------
215 // Returns the radiobox label
217 wxString
wxRadioBox::GetLabel() const
219 return wxControl::GetLabel();
222 //-------------------------------------------------------------------------------------
224 //-------------------------------------------------------------------------------------
225 // Returns the label for the given button
227 wxString
wxRadioBox::GetString(int item
) const
230 wxRadioButton
*current
;
233 return wxEmptyString
;
236 current
= m_radioButtonCycle
;
239 current
= current
->NextInCycle();
241 return current
->GetLabel();
244 //-------------------------------------------------------------------------------------
246 //-------------------------------------------------------------------------------------
247 // Returns the zero-based position of the selected button
249 int wxRadioBox::GetSelection() const
252 wxRadioButton
*current
;
255 current
=m_radioButtonCycle
;
256 while (!current
->GetValue()) {
258 current
=current
->NextInCycle();
264 //-------------------------------------------------------------------------------------
266 //-------------------------------------------------------------------------------------
267 // Returns the number of buttons in the radiobox
272 //-------------------------------------------------------------------------------------
273 // ¥ SetLabel(const wxString&)
274 //-------------------------------------------------------------------------------------
275 // Sets the radiobox label
277 void wxRadioBox::SetLabel(const wxString
& label
)
279 return wxControl::SetLabel(label
);
282 //-------------------------------------------------------------------------------------
283 // ¥ SetLabel(int, const wxString&)
284 //-------------------------------------------------------------------------------------
285 // Sets the label of a given button
287 void wxRadioBox::SetString(int item
,const wxString
& label
)
290 wxRadioButton
*current
;
295 current
=m_radioButtonCycle
;
298 current
=current
->NextInCycle();
300 return current
->SetLabel(label
);
303 //-------------------------------------------------------------------------------------
305 //-------------------------------------------------------------------------------------
306 // Sets a button by passing the desired position. This does not cause
307 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
309 void wxRadioBox::SetSelection(int item
)
312 wxRadioButton
*current
;
317 current
=m_radioButtonCycle
;
320 current
=current
->NextInCycle();
322 current
->SetValue(true);
326 //-------------------------------------------------------------------------------------
328 //-------------------------------------------------------------------------------------
329 // Shows or hides the entire radiobox
331 bool wxRadioBox::Show(bool show
)
334 wxRadioButton
*current
;
336 wxControl::Show(show
);
338 current
=m_radioButtonCycle
;
339 for (i
=0;i
<m_noItems
;i
++)
342 current
=current
->NextInCycle();
347 //-------------------------------------------------------------------------------------
349 //-------------------------------------------------------------------------------------
350 // Shows or hides the given button
352 bool wxRadioBox::Show(int item
, bool show
)
355 wxRadioButton
*current
;
360 current
=m_radioButtonCycle
;
363 current
=current
->NextInCycle();
365 return current
->Show(show
);
368 //-------------------------------------------------------------------------------------
370 //-------------------------------------------------------------------------------------
371 // Simulates the effect of the user issuing a command to the item
373 void wxRadioBox::Command (wxCommandEvent
& event
)
375 SetSelection (event
.GetInt());
376 ProcessCommand (event
);
379 //-------------------------------------------------------------------------------------
381 //-------------------------------------------------------------------------------------
382 // Sets the selected button to receive keyboard input
384 void wxRadioBox::SetFocus()
387 wxRadioButton
*current
;
390 current
=m_radioButtonCycle
;
391 while (!current
->GetValue()) {
393 current
=current
->NextInCycle();
399 //-------------------------------------------------------------------------------------
401 //-------------------------------------------------------------------------------------
402 // Simulates the effect of the user issuing a command to the item
404 #define RADIO_SIZE 20
406 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
409 wxRadioButton
*current
;
411 // define the position
413 int x_current
, y_current
;
414 int x_offset
,y_offset
;
415 int widthOld
, heightOld
;
416 GetSize(&widthOld
, &heightOld
);
420 GetPosition(&x_current
, &y_current
);
421 if ((x
== wxDefaultCoord
) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
422 x_offset
= x_current
;
423 if ((y
== wxDefaultCoord
)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
424 y_offset
= y_current
;
428 int charWidth
,charHeight
;
429 int maxWidth
,maxHeight
;
430 int eachWidth
[128],eachHeight
[128];
431 int totWidth
,totHeight
;
433 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
439 for (i
= 0 ; i
< m_noItems
; i
++)
441 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
442 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
443 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
444 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
445 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
448 totHeight
= GetRowCount() * ( maxHeight
) ;
449 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
451 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
453 // only change our width/height if asked for
454 if ( width
== wxDefaultCoord
)
456 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
462 if ( height
== wxDefaultCoord
)
464 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
470 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
472 // arrange radiobuttons
483 current
=m_radioButtonCycle
;
484 for ( i
= 0 ; i
< m_noItems
; i
++)
486 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
488 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
490 x_offset
+= maxWidth
+ charWidth
;
496 y_offset
+= maxHeight
; /*+ charHeight/2;*/
500 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
501 current
=current
->NextInCycle();
503 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
504 y_offset
+= maxHeight
; /*+ charHeight/2;*/
506 x_offset
+= maxWidth
+ charWidth
;
510 wxSize
wxRadioBox::DoGetBestSize() const
512 int charWidth
, charHeight
;
513 int maxWidth
, maxHeight
;
514 int eachWidth
, eachHeight
;
515 int totWidth
, totHeight
;
517 wxFont font
= /*GetParent()->*/GetFont();
518 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
519 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
526 for (int i
= 0 ; i
< m_noItems
; i
++)
528 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
,NULL
, NULL
, &font
);
529 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
530 eachHeight
= (int)((3 * eachHeight
) / 2);
531 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
532 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
535 totHeight
= GetRowCount() * (maxHeight
) ;
536 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
538 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
542 // handle radio box title as well
543 GetTextExtent(GetTitle(), &eachWidth
, NULL
);
544 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
545 if (totWidth
< eachWidth
)
546 totWidth
= eachWidth
;
548 return wxSize(totWidth
, totHeight
);
550 //-------------------------------------------------------------------------------------
552 //-------------------------------------------------------------------------------------
553 // return the number of buttons in the vertical direction
555 int wxRadioBox::GetRowCount() const
557 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
563 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
567 //-------------------------------------------------------------------------------------
569 //-------------------------------------------------------------------------------------
570 // return the number of buttons in the horizontal direction
572 int wxRadioBox::GetColumnCount() const
574 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
576 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;