1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/radiobox.cpp
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 //-------------------------------------------------------------------------------------
14 //-------------------------------------------------------------------------------------
16 #include "wx/wxprec.h"
20 #include "wx/arrstr.h"
22 #include "wx/radiobox.h"
23 #include "wx/radiobut.h"
24 #include "wx/mac/uma.h"
26 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
28 //-------------------------------------------------------------------------------------
30 //-------------------------------------------------------------------------------------
31 // Default constructor
32 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
33 EVT_RADIOBUTTON( wxID_ANY
, wxRadioBox::OnRadioButton
)
36 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
38 if ( outer
.IsChecked() )
40 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
41 int i
= GetSelection() ;
43 event
.SetString( GetString( i
) );
44 event
.SetEventObject( this );
45 ProcessCommand(event
);
49 wxRadioBox::wxRadioBox()
54 m_radioButtonCycle
= NULL
;
57 //-------------------------------------------------------------------------------------
58 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
59 // const wxSize&, int, const wxString[], int, long,
60 // const wxValidator&, const wxString&)
61 //-------------------------------------------------------------------------------------
62 // Contructor, creating and showing a radiobox
67 //-------------------------------------------------------------------------------------
69 //-------------------------------------------------------------------------------------
70 // Destructor, destroying the radiobox item
72 wxRadioBox::~wxRadioBox()
74 m_isBeingDeleted
= true;
76 wxRadioButton
*next
,*current
;
78 current
=m_radioButtonCycle
->NextInCycle();
79 next
=current
->NextInCycle();
80 while (current
!=m_radioButtonCycle
) {
83 next
=current
->NextInCycle();
88 //-------------------------------------------------------------------------------------
90 //-------------------------------------------------------------------------------------
91 // Create the radiobox for two-step construction
93 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
94 const wxPoint
& pos
, const wxSize
& size
,
95 const wxArrayString
& choices
,
96 int majorDim
, long style
,
97 const wxValidator
& val
, const wxString
& name
)
99 wxCArrayString
chs(choices
);
101 return Create(parent
, id
, label
, pos
, size
, chs
.GetCount(),
102 chs
.GetStrings(), majorDim
, style
, val
, name
);
105 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
106 const wxPoint
& pos
, const wxSize
& size
,
107 int n
, const wxString choices
[],
108 int majorDim
, long style
,
109 const wxValidator
& val
, const wxString
& name
)
111 m_macIsUserPane
= false ;
113 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
119 m_noRowsOrCols
= majorDim
;
120 m_radioButtonCycle
= NULL
;
125 m_majorDim
= majorDim
;
130 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
131 if( bounds
.right
<= bounds
.left
)
132 bounds
.right
= bounds
.left
+ 100 ;
133 if ( bounds
.bottom
<= bounds
.top
)
134 bounds
.bottom
= bounds
.top
+ 100 ;
136 m_peer
= new wxMacControl(this) ;
138 verify_noerr(CreateGroupBoxControl(MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),&bounds
, CFSTR("") ,
139 true /*primary*/ , m_peer
->GetControlRefAddr() ) ) ;
141 for (i
= 0; i
< n
; i
++)
143 wxRadioButton
*radBtn
= new wxRadioButton
147 wxStripMenuCodes(choices
[i
]),
150 i
== 0 ? wxRB_GROUP
: 0
153 m_radioButtonCycle
= radBtn
;
154 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
158 MacPostControlCreate(pos
,size
) ;
164 //-------------------------------------------------------------------------------------
166 //-------------------------------------------------------------------------------------
167 // Enables or disables the entire radiobox
169 bool wxRadioBox::Enable(bool enable
)
172 wxRadioButton
*current
;
174 if (!wxControl::Enable(enable
))
177 current
= m_radioButtonCycle
;
178 for (i
= 0; i
< m_noItems
; i
++) {
179 current
->Enable(enable
);
180 current
= current
->NextInCycle();
185 //-------------------------------------------------------------------------------------
186 // ¥ Enable(int, bool)
187 //-------------------------------------------------------------------------------------
188 // Enables or disables an given button
190 bool wxRadioBox::Enable(int item
, bool enable
)
193 wxRadioButton
*current
;
199 current
= m_radioButtonCycle
;
202 current
= current
->NextInCycle();
204 return current
->Enable(enable
);
207 //-------------------------------------------------------------------------------------
209 //-------------------------------------------------------------------------------------
210 // Returns the radiobox label
212 wxString
wxRadioBox::GetLabel() const
214 return wxControl::GetLabel();
217 //-------------------------------------------------------------------------------------
219 //-------------------------------------------------------------------------------------
220 // Returns the label for the given button
222 wxString
wxRadioBox::GetString(int item
) const
225 wxRadioButton
*current
;
228 return wxEmptyString
;
231 current
= m_radioButtonCycle
;
234 current
= current
->NextInCycle();
236 return current
->GetLabel();
239 //-------------------------------------------------------------------------------------
241 //-------------------------------------------------------------------------------------
242 // Returns the zero-based position of the selected button
244 int wxRadioBox::GetSelection() const
247 wxRadioButton
*current
;
250 current
=m_radioButtonCycle
;
251 while (!current
->GetValue()) {
253 current
=current
->NextInCycle();
259 //-------------------------------------------------------------------------------------
261 //-------------------------------------------------------------------------------------
262 // Returns the number of buttons in the radiobox
267 //-------------------------------------------------------------------------------------
268 // ¥ SetLabel(const wxString&)
269 //-------------------------------------------------------------------------------------
270 // Sets the radiobox label
272 void wxRadioBox::SetLabel(const wxString
& label
)
274 return wxControl::SetLabel(label
);
277 //-------------------------------------------------------------------------------------
278 // ¥ SetLabel(int, const wxString&)
279 //-------------------------------------------------------------------------------------
280 // Sets the label of a given button
282 void wxRadioBox::SetString(int item
,const wxString
& label
)
285 wxRadioButton
*current
;
290 current
=m_radioButtonCycle
;
293 current
=current
->NextInCycle();
295 return current
->SetLabel(label
);
298 //-------------------------------------------------------------------------------------
300 //-------------------------------------------------------------------------------------
301 // Sets a button by passing the desired position. This does not cause
302 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
304 void wxRadioBox::SetSelection(int item
)
307 wxRadioButton
*current
;
312 current
=m_radioButtonCycle
;
315 current
=current
->NextInCycle();
317 current
->SetValue(true);
321 //-------------------------------------------------------------------------------------
323 //-------------------------------------------------------------------------------------
324 // Shows or hides the entire radiobox
326 bool wxRadioBox::Show(bool show
)
329 wxRadioButton
*current
;
331 wxControl::Show(show
);
333 current
=m_radioButtonCycle
;
334 for (i
=0;i
<m_noItems
;i
++)
337 current
=current
->NextInCycle();
342 //-------------------------------------------------------------------------------------
344 //-------------------------------------------------------------------------------------
345 // Shows or hides the given button
347 bool wxRadioBox::Show(int item
, bool show
)
350 wxRadioButton
*current
;
355 current
=m_radioButtonCycle
;
358 current
=current
->NextInCycle();
360 return current
->Show(show
);
363 //-------------------------------------------------------------------------------------
365 //-------------------------------------------------------------------------------------
366 // Simulates the effect of the user issuing a command to the item
368 void wxRadioBox::Command (wxCommandEvent
& event
)
370 SetSelection (event
.GetInt());
371 ProcessCommand (event
);
374 //-------------------------------------------------------------------------------------
376 //-------------------------------------------------------------------------------------
377 // Sets the selected button to receive keyboard input
379 void wxRadioBox::SetFocus()
382 wxRadioButton
*current
;
385 current
=m_radioButtonCycle
;
386 while (!current
->GetValue()) {
388 current
=current
->NextInCycle();
394 //-------------------------------------------------------------------------------------
396 //-------------------------------------------------------------------------------------
397 // Simulates the effect of the user issuing a command to the item
399 #define RADIO_SIZE 20
401 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
404 wxRadioButton
*current
;
406 // define the position
408 int x_current
, y_current
;
409 int x_offset
,y_offset
;
410 int widthOld
, heightOld
;
411 GetSize(&widthOld
, &heightOld
);
415 GetPosition(&x_current
, &y_current
);
416 if ((x
== wxDefaultCoord
) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
417 x_offset
= x_current
;
418 if ((y
== wxDefaultCoord
)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
419 y_offset
= y_current
;
423 int charWidth
,charHeight
;
424 int maxWidth
,maxHeight
;
425 int eachWidth
[128],eachHeight
[128];
426 int totWidth
,totHeight
;
428 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
434 for (i
= 0 ; i
< m_noItems
; i
++)
436 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
437 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
438 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
439 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
440 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
443 totHeight
= GetRowCount() * ( maxHeight
) ;
444 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
446 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
448 // only change our width/height if asked for
449 if ( width
== wxDefaultCoord
)
451 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
457 if ( height
== wxDefaultCoord
)
459 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
465 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
467 // arrange radiobuttons
478 current
=m_radioButtonCycle
;
479 for ( i
= 0 ; i
< m_noItems
; i
++)
481 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
483 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
485 x_offset
+= maxWidth
+ charWidth
;
491 y_offset
+= maxHeight
; /*+ charHeight/2;*/
495 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
496 current
=current
->NextInCycle();
498 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
499 y_offset
+= maxHeight
; /*+ charHeight/2;*/
501 x_offset
+= maxWidth
+ charWidth
;
505 wxSize
wxRadioBox::DoGetBestSize() const
507 int charWidth
, charHeight
;
508 int maxWidth
, maxHeight
;
509 int eachWidth
, eachHeight
;
510 int totWidth
, totHeight
;
512 wxFont font
= /*GetParent()->*/GetFont();
513 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
514 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
521 for (int i
= 0 ; i
< m_noItems
; i
++)
523 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
,NULL
, NULL
, &font
);
524 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
525 eachHeight
= (int)((3 * eachHeight
) / 2);
526 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
527 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
530 totHeight
= GetRowCount() * (maxHeight
) ;
531 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
533 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
537 // handle radio box title as well
538 GetTextExtent(GetLabel(), &eachWidth
, NULL
);
539 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
540 if (totWidth
< eachWidth
)
541 totWidth
= eachWidth
;
543 return wxSize(totWidth
, totHeight
);
545 //-------------------------------------------------------------------------------------
547 //-------------------------------------------------------------------------------------
548 // return the number of buttons in the vertical direction
550 int wxRadioBox::GetRowCount() const
552 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
558 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
562 //-------------------------------------------------------------------------------------
564 //-------------------------------------------------------------------------------------
565 // return the number of buttons in the horizontal direction
567 int wxRadioBox::GetColumnCount() const
569 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
571 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;