1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/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/radiobox.h"
23 #include "wx/radiobut.h"
24 #include "wx/arrstr.h"
27 #include "wx/mac/uma.h"
29 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
31 //-------------------------------------------------------------------------------------
33 //-------------------------------------------------------------------------------------
34 // Default constructor
35 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
36 EVT_RADIOBUTTON( wxID_ANY
, wxRadioBox::OnRadioButton
)
39 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
41 if ( outer
.IsChecked() )
43 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
44 int i
= GetSelection() ;
46 event
.SetString(GetString(i
));
47 event
.SetEventObject( this );
48 ProcessCommand(event
);
52 wxRadioBox::wxRadioBox()
56 m_radioButtonCycle
= NULL
;
59 //-------------------------------------------------------------------------------------
60 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
61 // const wxSize&, int, const wxString[], int, long,
62 // const wxValidator&, const wxString&)
63 //-------------------------------------------------------------------------------------
64 // Contructor, creating and showing a radiobox
69 //-------------------------------------------------------------------------------------
71 //-------------------------------------------------------------------------------------
72 // Destructor, destroying the radiobox item
74 wxRadioBox::~wxRadioBox()
76 m_isBeingDeleted
= true;
78 wxRadioButton
*next
,*current
;
80 current
=m_radioButtonCycle
->NextInCycle();
81 next
=current
->NextInCycle();
82 while (current
!=m_radioButtonCycle
) {
85 next
=current
->NextInCycle();
90 //-------------------------------------------------------------------------------------
92 //-------------------------------------------------------------------------------------
93 // Create the radiobox for two-step construction
95 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
96 const wxPoint
& pos
, const wxSize
& size
,
97 const wxArrayString
& choices
,
98 int majorDim
, long style
,
99 const wxValidator
& val
, const wxString
& name
)
101 wxCArrayString
chs(choices
);
103 return Create(parent
, id
, label
, pos
, size
, chs
.GetCount(),
104 chs
.GetStrings(), majorDim
, style
, val
, name
);
107 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
108 const wxPoint
& pos
, const wxSize
& size
,
109 int n
, const wxString choices
[],
110 int majorDim
, long style
,
111 const wxValidator
& val
, const wxString
& name
)
113 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
118 m_noItems
= (unsigned int)n
;
119 m_noRowsOrCols
= majorDim
;
120 m_radioButtonCycle
= NULL
;
122 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
127 MacPreControlCreate( parent
, id
, wxStripMenuCodes(label
) , pos
, size
,style
, val
, name
, &bounds
, title
) ;
129 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
130 kControlGroupBoxTextTitleProc
, (long) this ) ;
132 for (i
= 0; i
< n
; i
++)
134 wxRadioButton
*radBtn
= new wxRadioButton
138 wxStripMenuCodes(choices
[i
]),
141 i
== 0 ? wxRB_GROUP
: 0
144 m_radioButtonCycle
= radBtn
;
145 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
149 MacPostControlCreate() ;
155 //-------------------------------------------------------------------------------------
157 //-------------------------------------------------------------------------------------
158 // Enables or disables the entire radiobox
160 bool wxRadioBox::Enable(bool enable
)
162 if (!wxControl::Enable(enable
))
165 wxRadioButton
*current
= m_radioButtonCycle
;
166 for (unsigned int i
= 0; i
< m_noItems
; i
++)
168 current
->Enable(enable
);
169 current
= current
->NextInCycle();
174 //-------------------------------------------------------------------------------------
175 // ¥ Enable(unsigned int, bool)
176 //-------------------------------------------------------------------------------------
177 // Enables or disables an given button
179 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
185 wxRadioButton
*current
= m_radioButtonCycle
;
189 current
= current
->NextInCycle();
191 return current
->Enable(enable
);
194 //-------------------------------------------------------------------------------------
196 //-------------------------------------------------------------------------------------
197 // Returns the radiobox label
199 wxString
wxRadioBox::GetLabel() const
201 return wxControl::GetLabel();
204 //-------------------------------------------------------------------------------------
206 //-------------------------------------------------------------------------------------
207 // Returns the label for the given button
209 wxString
wxRadioBox::GetString(unsigned int item
) const
211 wxRadioButton
*current
;
214 return wxEmptyString
;
217 current
= m_radioButtonCycle
;
220 current
= current
->NextInCycle();
222 return current
->GetLabel();
225 //-------------------------------------------------------------------------------------
227 //-------------------------------------------------------------------------------------
228 // Returns the zero-based position of the selected button
230 int wxRadioBox::GetSelection() const
233 wxRadioButton
*current
;
236 current
=m_radioButtonCycle
;
237 while (!current
->GetValue()) {
239 current
=current
->NextInCycle();
245 //-------------------------------------------------------------------------------------
247 //-------------------------------------------------------------------------------------
248 // Returns the number of buttons in the radiobox
253 //-------------------------------------------------------------------------------------
254 // ¥ SetLabel(const wxString&)
255 //-------------------------------------------------------------------------------------
256 // Sets the radiobox label
258 void wxRadioBox::SetLabel(const wxString
& label
)
260 return wxControl::SetLabel(label
);
263 //-------------------------------------------------------------------------------------
264 // ¥ SetLabel(int, const wxString&)
265 //-------------------------------------------------------------------------------------
266 // Sets the label of a given button
268 void wxRadioBox::SetString(unsigned int item
,const wxString
& label
)
274 wxRadioButton
*current
=m_radioButtonCycle
;
278 current
=current
->NextInCycle();
280 return current
->SetLabel(label
);
283 //-------------------------------------------------------------------------------------
285 //-------------------------------------------------------------------------------------
286 // Sets a button by passing the desired position. This does not cause
287 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
289 void wxRadioBox::SetSelection(int item
)
292 wxRadioButton
*current
;
297 current
=m_radioButtonCycle
;
300 current
=current
->NextInCycle();
302 current
->SetValue(true);
306 //-------------------------------------------------------------------------------------
308 //-------------------------------------------------------------------------------------
309 // Shows or hides the entire radiobox
311 bool wxRadioBox::Show(bool show
)
313 wxRadioButton
*current
;
315 wxControl::Show(show
);
317 current
=m_radioButtonCycle
;
318 for (unsigned int i
=0; i
<m_noItems
; i
++)
321 current
=current
->NextInCycle();
326 //-------------------------------------------------------------------------------------
327 // ¥ Show(unsigned int, bool)
328 //-------------------------------------------------------------------------------------
329 // Shows or hides the given button
331 bool wxRadioBox::Show(unsigned int item
, bool show
)
337 wxRadioButton
*current
=m_radioButtonCycle
;
340 current
=current
->NextInCycle();
342 return current
->Show(show
);
345 //-------------------------------------------------------------------------------------
347 //-------------------------------------------------------------------------------------
348 // Simulates the effect of the user issuing a command to the item
350 void wxRadioBox::Command (wxCommandEvent
& event
)
352 SetSelection (event
.GetInt());
353 ProcessCommand (event
);
356 //-------------------------------------------------------------------------------------
358 //-------------------------------------------------------------------------------------
359 // Sets the selected button to receive keyboard input
361 void wxRadioBox::SetFocus()
364 wxRadioButton
*current
;
367 current
=m_radioButtonCycle
;
368 while (!current
->GetValue()) {
370 current
=current
->NextInCycle();
376 //-------------------------------------------------------------------------------------
378 //-------------------------------------------------------------------------------------
379 // Simulates the effect of the user issuing a command to the item
381 #define RADIO_SIZE 20
383 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
386 wxRadioButton
*current
;
388 // define the position
390 int x_current
, y_current
;
391 int x_offset
,y_offset
;
392 int widthOld
, heightOld
;
393 GetSize(&widthOld
, &heightOld
);
397 GetPosition(&x_current
, &y_current
);
398 if ((x
== wxDefaultCoord
) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
399 x_offset
= x_current
;
400 if ((y
== wxDefaultCoord
)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
401 y_offset
= y_current
;
405 int charWidth
,charHeight
;
406 int maxWidth
,maxHeight
;
407 int eachWidth
[128],eachHeight
[128];
408 int totWidth
,totHeight
;
410 SetFont(GetParent()->GetFont());
411 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
416 for (i
= 0 ; i
< m_noItems
; i
++)
418 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
419 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
420 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
421 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
422 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
425 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
426 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
428 // only change our width/height if asked for
429 if ( width
== wxDefaultCoord
)
431 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
437 if ( height
== wxDefaultCoord
)
439 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
445 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
447 // arrange radiobuttons
454 if ( UMAGetSystemVersion() >= 0x1030 )
456 //need to add a few more pixels for the top border on panther
457 y_start
= y_start
+ 5; //how many exactly should this be to meet the HIG?
462 current
=m_radioButtonCycle
;
463 for ( i
= 0 ; i
< m_noItems
; i
++)
465 if (i
&&((i%GetMajorDim
())==0)) // not to do for the zero button!
467 if (m_windowStyle
& wxRA_VERTICAL
)
469 x_offset
+= maxWidth
+ charWidth
;
475 y_offset
+= maxHeight
; /*+ charHeight/2;*/
479 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
480 current
=current
->NextInCycle();
482 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
483 y_offset
+= maxHeight
; /*+ charHeight/2;*/
485 x_offset
+= maxWidth
+ charWidth
;
489 wxSize
wxRadioBox::DoGetBestSize() const
491 int charWidth
, charHeight
;
492 int maxWidth
, maxHeight
;
493 int eachWidth
, eachHeight
;
494 int totWidth
, totHeight
;
496 wxFont font
= GetParent()->GetFont();
497 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
498 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
504 for (unsigned int i
= 0 ; i
< m_noItems
; i
++)
506 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
);
507 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
508 eachHeight
= (int)((3 * eachHeight
) / 2);
509 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
510 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
513 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
514 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
516 if ( UMAGetSystemVersion() >= 0x1030 )
518 //need to add a few more pixels for the static boxborder on panther
519 totHeight
= totHeight
+ 10; //how many exactly should this be to meet the HIG?
521 // handle radio box title as well
522 GetTextExtent(GetLabel(), &eachWidth
, NULL
);
523 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
524 if (totWidth
< eachWidth
)
525 totWidth
= eachWidth
;
527 return wxSize(totWidth
, totHeight
);
530 #endif // wxUSE_RADIOBOX