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 //-------------------------------------------------------------------------------------
22 #include "wx/arrstr.h"
24 #include "wx/radiobox.h"
25 #include "wx/radiobut.h"
26 #include "wx/mac/uma.h"
28 #if !USE_SHARED_LIBRARY
29 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
32 //-------------------------------------------------------------------------------------
34 //-------------------------------------------------------------------------------------
35 // Default constructor
36 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
37 EVT_RADIOBUTTON( -1 , wxRadioBox::OnRadioButton
)
40 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
42 if ( outer
.IsChecked() )
44 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
45 int i
= GetSelection() ;
47 event
.SetString( GetString( i
) );
48 event
.SetEventObject( this );
49 ProcessCommand(event
);
53 wxRadioBox::wxRadioBox()
58 m_radioButtonCycle
= NULL
;
61 //-------------------------------------------------------------------------------------
62 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
63 // const wxSize&, int, const wxString[], int, long,
64 // const wxValidator&, const wxString&)
65 //-------------------------------------------------------------------------------------
66 // Contructor, creating and showing a radiobox
71 //-------------------------------------------------------------------------------------
73 //-------------------------------------------------------------------------------------
74 // Destructor, destroying the radiobox item
76 wxRadioBox::~wxRadioBox()
78 m_isBeingDeleted
= TRUE
;
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 const wxArrayString
& choices
,
100 int majorDim
, long style
,
101 const wxValidator
& val
, const wxString
& name
)
103 wxCArrayString
chs(choices
);
105 return Create(parent
, id
, label
, pos
, size
, chs
.GetCount(),
106 chs
.GetStrings(), majorDim
, style
, val
, name
);
109 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
110 const wxPoint
& pos
, const wxSize
& size
,
111 int n
, const wxString choices
[],
112 int majorDim
, long style
,
113 const wxValidator
& val
, const wxString
& name
)
115 m_macIsUserPane
= FALSE
;
117 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
123 m_noRowsOrCols
= majorDim
;
124 m_radioButtonCycle
= NULL
;
129 m_majorDim
= majorDim
;
134 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
135 if( bounds
.right
<= bounds
.left
)
136 bounds
.right
= bounds
.left
+ 100 ;
137 if ( bounds
.bottom
<= bounds
.top
)
138 bounds
.bottom
= bounds
.top
+ 100 ;
140 m_peer
= new wxMacControl() ;
142 verify_noerr(CreateGroupBoxControl(MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),&bounds
, CFSTR("") ,
143 true /*primary*/ , m_peer
->GetControlRefAddr() ) ) ;
145 for (i
= 0; i
< n
; i
++)
147 wxRadioButton
*radBtn
= new wxRadioButton
151 wxStripMenuCodes(choices
[i
]),
154 i
== 0 ? wxRB_GROUP
: 0
157 m_radioButtonCycle
= radBtn
;
158 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
162 MacPostControlCreate(pos
,size
) ;
168 //-------------------------------------------------------------------------------------
170 //-------------------------------------------------------------------------------------
171 // Enables or disables the entire radiobox
173 bool wxRadioBox::Enable(bool enable
)
176 wxRadioButton
*current
;
178 if (!wxControl::Enable(enable
))
181 current
= m_radioButtonCycle
;
182 for (i
= 0; i
< m_noItems
; i
++) {
183 current
->Enable(enable
);
184 current
= current
->NextInCycle();
189 //-------------------------------------------------------------------------------------
190 // ¥ Enable(int, bool)
191 //-------------------------------------------------------------------------------------
192 // Enables or disables an given button
194 void wxRadioBox::Enable(int item
, bool enable
)
197 wxRadioButton
*current
;
199 if ((item
< 0) || (item
>= m_noItems
))
203 current
= m_radioButtonCycle
;
206 current
= current
->NextInCycle();
208 current
->Enable(enable
);
211 //-------------------------------------------------------------------------------------
213 //-------------------------------------------------------------------------------------
214 // Returns the radiobox label
216 wxString
wxRadioBox::GetLabel() const
218 return wxControl::GetLabel();
221 //-------------------------------------------------------------------------------------
223 //-------------------------------------------------------------------------------------
224 // Returns the label for the given button
226 wxString
wxRadioBox::GetString(int item
) const
229 wxRadioButton
*current
;
231 if ((item
< 0) || (item
>= m_noItems
))
232 return wxEmptyString
;
235 current
= m_radioButtonCycle
;
238 current
= current
->NextInCycle();
240 return current
->GetLabel();
243 //-------------------------------------------------------------------------------------
245 //-------------------------------------------------------------------------------------
246 // Returns the zero-based position of the selected button
248 int wxRadioBox::GetSelection() const
251 wxRadioButton
*current
;
254 current
=m_radioButtonCycle
;
255 while (!current
->GetValue()) {
257 current
=current
->NextInCycle();
263 //-------------------------------------------------------------------------------------
265 //-------------------------------------------------------------------------------------
266 // Returns the number of buttons in the radiobox
271 //-------------------------------------------------------------------------------------
272 // ¥ SetLabel(const wxString&)
273 //-------------------------------------------------------------------------------------
274 // Sets the radiobox label
276 void wxRadioBox::SetLabel(const wxString
& label
)
278 return wxControl::SetLabel(label
);
281 //-------------------------------------------------------------------------------------
282 // ¥ SetLabel(int, const wxString&)
283 //-------------------------------------------------------------------------------------
284 // Sets the label of a given button
286 void wxRadioBox::SetString(int item
,const wxString
& label
)
289 wxRadioButton
*current
;
291 if ((item
< 0) || (item
>= m_noItems
))
294 current
=m_radioButtonCycle
;
297 current
=current
->NextInCycle();
299 return current
->SetLabel(label
);
302 //-------------------------------------------------------------------------------------
304 //-------------------------------------------------------------------------------------
305 // Sets a button by passing the desired position. This does not cause
306 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
308 void wxRadioBox::SetSelection(int item
)
311 wxRadioButton
*current
;
313 if ((item
< 0) || (item
>= m_noItems
))
316 current
=m_radioButtonCycle
;
319 current
=current
->NextInCycle();
321 current
->SetValue(true);
325 //-------------------------------------------------------------------------------------
327 //-------------------------------------------------------------------------------------
328 // Shows or hides the entire radiobox
330 bool wxRadioBox::Show(bool show
)
333 wxRadioButton
*current
;
335 wxControl::Show(show
);
337 current
=m_radioButtonCycle
;
338 for (i
=0;i
<m_noItems
;i
++) {
340 current
=current
->NextInCycle();
345 //-------------------------------------------------------------------------------------
347 //-------------------------------------------------------------------------------------
348 // Shows or hides the given button
350 void wxRadioBox::Show(int item
, bool show
)
353 wxRadioButton
*current
;
355 if ((item
< 0) || (item
>= m_noItems
))
358 current
=m_radioButtonCycle
;
361 current
=current
->NextInCycle();
366 //-------------------------------------------------------------------------------------
368 //-------------------------------------------------------------------------------------
369 // Simulates the effect of the user issuing a command to the item
371 void wxRadioBox::Command (wxCommandEvent
& event
)
373 SetSelection (event
.GetInt());
374 ProcessCommand (event
);
377 //-------------------------------------------------------------------------------------
379 //-------------------------------------------------------------------------------------
380 // Sets the selected button to receive keyboard input
382 void wxRadioBox::SetFocus()
385 wxRadioButton
*current
;
388 current
=m_radioButtonCycle
;
389 while (!current
->GetValue()) {
391 current
=current
->NextInCycle();
397 //-------------------------------------------------------------------------------------
399 //-------------------------------------------------------------------------------------
400 // Simulates the effect of the user issuing a command to the item
402 #define RADIO_SIZE 20
404 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
407 wxRadioButton
*current
;
409 // define the position
411 int x_current
, y_current
;
412 int x_offset
,y_offset
;
413 int widthOld
, heightOld
;
414 GetSize(&widthOld
, &heightOld
);
418 GetPosition(&x_current
, &y_current
);
419 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
420 x_offset
= x_current
;
421 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
422 y_offset
= y_current
;
426 int charWidth
,charHeight
;
427 int maxWidth
,maxHeight
;
428 int eachWidth
[128],eachHeight
[128];
429 int totWidth
,totHeight
;
431 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
437 for (i
= 0 ; i
< m_noItems
; i
++)
439 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
440 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
441 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
442 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
443 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
446 totHeight
= GetRowCount() * ( maxHeight
) ;
447 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
449 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
451 // only change our width/height if asked for
454 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
462 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
468 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
470 // arrange radiobuttons
481 current
=m_radioButtonCycle
;
482 for ( i
= 0 ; i
< m_noItems
; i
++)
484 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
486 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
488 x_offset
+= maxWidth
+ charWidth
;
494 y_offset
+= maxHeight
; /*+ charHeight/2;*/
498 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
499 current
=current
->NextInCycle();
501 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
502 y_offset
+= maxHeight
; /*+ charHeight/2;*/
504 x_offset
+= maxWidth
+ charWidth
;
508 wxSize
wxRadioBox::DoGetBestSize() const
510 int charWidth
, charHeight
;
511 int maxWidth
, maxHeight
;
512 int eachWidth
, eachHeight
;
513 int totWidth
, totHeight
;
515 wxFont font
= /*GetParent()->*/GetFont();
516 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
517 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
524 for (int i
= 0 ; i
< m_noItems
; i
++)
526 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
,NULL
, NULL
, &font
);
527 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
528 eachHeight
= (int)((3 * eachHeight
) / 2);
529 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
530 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
533 totHeight
= GetRowCount() * (maxHeight
) ;
534 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
536 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
540 // handle radio box title as well
541 GetTextExtent(GetTitle(), &eachWidth
, NULL
);
542 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
543 if (totWidth
< eachWidth
)
544 totWidth
= eachWidth
;
546 return wxSize(totWidth
, totHeight
);
548 //-------------------------------------------------------------------------------------
550 //-------------------------------------------------------------------------------------
551 // return the number of buttons in the vertical direction
553 int wxRadioBox::GetRowCount() const
555 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
561 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
565 //-------------------------------------------------------------------------------------
567 //-------------------------------------------------------------------------------------
568 // return the number of buttons in the horizontal direction
570 int wxRadioBox::GetColumnCount() const
572 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
574 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;