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"
23 #include "wx/arrstr.h"
25 #include "wx/radiobox.h"
26 #include "wx/radiobut.h"
27 #include "wx/mac/uma.h"
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
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 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() ;
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 void wxRadioBox::Enable(int item
, bool enable
)
198 wxRadioButton
*current
;
200 if ((item
< 0) || (item
>= m_noItems
))
204 current
= m_radioButtonCycle
;
207 current
= current
->NextInCycle();
209 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
;
232 if ((item
< 0) || (item
>= m_noItems
))
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
;
292 if ((item
< 0) || (item
>= m_noItems
))
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
;
314 if ((item
< 0) || (item
>= m_noItems
))
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
++) {
341 current
=current
->NextInCycle();
346 //-------------------------------------------------------------------------------------
348 //-------------------------------------------------------------------------------------
349 // Shows or hides the given button
351 void wxRadioBox::Show(int item
, bool show
)
354 wxRadioButton
*current
;
356 if ((item
< 0) || (item
>= m_noItems
))
359 current
=m_radioButtonCycle
;
362 current
=current
->NextInCycle();
367 //-------------------------------------------------------------------------------------
369 //-------------------------------------------------------------------------------------
370 // Simulates the effect of the user issuing a command to the item
372 void wxRadioBox::Command (wxCommandEvent
& event
)
374 SetSelection (event
.GetInt());
375 ProcessCommand (event
);
378 //-------------------------------------------------------------------------------------
380 //-------------------------------------------------------------------------------------
381 // Sets the selected button to receive keyboard input
383 void wxRadioBox::SetFocus()
386 wxRadioButton
*current
;
389 current
=m_radioButtonCycle
;
390 while (!current
->GetValue()) {
392 current
=current
->NextInCycle();
398 //-------------------------------------------------------------------------------------
400 //-------------------------------------------------------------------------------------
401 // Simulates the effect of the user issuing a command to the item
403 #define RADIO_SIZE 20
405 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
408 wxRadioButton
*current
;
410 // define the position
412 int x_current
, y_current
;
413 int x_offset
,y_offset
;
414 int widthOld
, heightOld
;
415 GetSize(&widthOld
, &heightOld
);
419 GetPosition(&x_current
, &y_current
);
420 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
421 x_offset
= x_current
;
422 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
423 y_offset
= y_current
;
427 int charWidth
,charHeight
;
428 int maxWidth
,maxHeight
;
429 int eachWidth
[128],eachHeight
[128];
430 int totWidth
,totHeight
;
432 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
438 for (i
= 0 ; i
< m_noItems
; i
++)
440 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
441 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
442 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
443 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
444 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
447 totHeight
= GetRowCount() * ( maxHeight
) ;
448 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
450 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
452 // only change our width/height if asked for
455 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
463 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
469 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
471 // arrange radiobuttons
482 current
=m_radioButtonCycle
;
483 for ( i
= 0 ; i
< m_noItems
; i
++)
485 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
487 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
489 x_offset
+= maxWidth
+ charWidth
;
495 y_offset
+= maxHeight
; /*+ charHeight/2;*/
499 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
500 current
=current
->NextInCycle();
502 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
503 y_offset
+= maxHeight
; /*+ charHeight/2;*/
505 x_offset
+= maxWidth
+ charWidth
;
509 wxSize
wxRadioBox::DoGetBestSize() const
511 int charWidth
, charHeight
;
512 int maxWidth
, maxHeight
;
513 int eachWidth
, eachHeight
;
514 int totWidth
, totHeight
;
516 wxFont font
= /*GetParent()->*/GetFont();
517 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
518 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
525 for (int i
= 0 ; i
< m_noItems
; i
++)
527 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
,NULL
, NULL
, &font
);
528 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
529 eachHeight
= (int)((3 * eachHeight
) / 2);
530 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
531 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
534 totHeight
= GetRowCount() * (maxHeight
) ;
535 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
537 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
541 // handle radio box title as well
542 GetTextExtent(GetTitle(), &eachWidth
, NULL
);
543 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
544 if (totWidth
< eachWidth
)
545 totWidth
= eachWidth
;
547 return wxSize(totWidth
, totHeight
);
549 //-------------------------------------------------------------------------------------
551 //-------------------------------------------------------------------------------------
552 // return the number of buttons in the vertical direction
554 int wxRadioBox::GetRowCount() const
556 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
562 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
566 //-------------------------------------------------------------------------------------
568 //-------------------------------------------------------------------------------------
569 // return the number of buttons in the horizontal direction
571 int wxRadioBox::GetColumnCount() const
573 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
575 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;