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"
22 #include "wx/arrstr.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()
53 m_radioButtonCycle
= NULL
;
56 //-------------------------------------------------------------------------------------
57 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
58 // const wxSize&, int, const wxString[], int, long,
59 // const wxValidator&, const wxString&)
60 //-------------------------------------------------------------------------------------
61 // Contructor, creating and showing a radiobox
66 //-------------------------------------------------------------------------------------
68 //-------------------------------------------------------------------------------------
69 // Destructor, destroying the radiobox item
71 wxRadioBox::~wxRadioBox()
73 m_isBeingDeleted
= true;
75 wxRadioButton
*next
,*current
;
77 current
=m_radioButtonCycle
->NextInCycle();
78 next
=current
->NextInCycle();
79 while (current
!=m_radioButtonCycle
) {
82 next
=current
->NextInCycle();
87 //-------------------------------------------------------------------------------------
89 //-------------------------------------------------------------------------------------
90 // Create the radiobox for two-step construction
92 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
93 const wxPoint
& pos
, const wxSize
& size
,
94 const wxArrayString
& choices
,
95 int majorDim
, long style
,
96 const wxValidator
& val
, const wxString
& name
)
98 wxCArrayString
chs(choices
);
100 return Create(parent
, id
, label
, pos
, size
, chs
.GetCount(),
101 chs
.GetStrings(), majorDim
, style
, val
, name
);
104 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
105 const wxPoint
& pos
, const wxSize
& size
,
106 int n
, const wxString choices
[],
107 int majorDim
, long style
,
108 const wxValidator
& val
, const wxString
& name
)
110 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
115 m_noItems
= (unsigned int)n
;
116 m_noRowsOrCols
= majorDim
;
117 m_radioButtonCycle
= NULL
;
119 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
124 MacPreControlCreate( parent
, id
, wxStripMenuCodes(label
) , pos
, size
,style
, val
, name
, &bounds
, title
) ;
126 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
127 kControlGroupBoxTextTitleProc
, (long) this ) ;
129 for (i
= 0; i
< n
; i
++)
131 wxRadioButton
*radBtn
= new wxRadioButton
135 wxStripMenuCodes(choices
[i
]),
138 i
== 0 ? wxRB_GROUP
: 0
141 m_radioButtonCycle
= radBtn
;
142 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
146 MacPostControlCreate() ;
152 //-------------------------------------------------------------------------------------
154 //-------------------------------------------------------------------------------------
155 // Enables or disables the entire radiobox
157 bool wxRadioBox::Enable(bool enable
)
159 if (!wxControl::Enable(enable
))
162 wxRadioButton
*current
= m_radioButtonCycle
;
163 for (unsigned int i
= 0; i
< m_noItems
; i
++)
165 current
->Enable(enable
);
166 current
= current
->NextInCycle();
171 //-------------------------------------------------------------------------------------
172 // ¥ Enable(unsigned int, bool)
173 //-------------------------------------------------------------------------------------
174 // Enables or disables an given button
176 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
182 wxRadioButton
*current
= m_radioButtonCycle
;
186 current
= current
->NextInCycle();
188 return current
->Enable(enable
);
191 //-------------------------------------------------------------------------------------
193 //-------------------------------------------------------------------------------------
194 // Returns the radiobox label
196 wxString
wxRadioBox::GetLabel() const
198 return wxControl::GetLabel();
201 //-------------------------------------------------------------------------------------
203 //-------------------------------------------------------------------------------------
204 // Returns the label for the given button
206 wxString
wxRadioBox::GetString(unsigned int item
) const
208 wxRadioButton
*current
;
211 return wxEmptyString
;
214 current
= m_radioButtonCycle
;
217 current
= current
->NextInCycle();
219 return current
->GetLabel();
222 //-------------------------------------------------------------------------------------
224 //-------------------------------------------------------------------------------------
225 // Returns the zero-based position of the selected button
227 int wxRadioBox::GetSelection() const
230 wxRadioButton
*current
;
233 current
=m_radioButtonCycle
;
234 while (!current
->GetValue()) {
236 current
=current
->NextInCycle();
242 //-------------------------------------------------------------------------------------
244 //-------------------------------------------------------------------------------------
245 // Returns the number of buttons in the radiobox
250 //-------------------------------------------------------------------------------------
251 // ¥ SetLabel(const wxString&)
252 //-------------------------------------------------------------------------------------
253 // Sets the radiobox label
255 void wxRadioBox::SetLabel(const wxString
& label
)
257 return wxControl::SetLabel(label
);
260 //-------------------------------------------------------------------------------------
261 // ¥ SetLabel(int, const wxString&)
262 //-------------------------------------------------------------------------------------
263 // Sets the label of a given button
265 void wxRadioBox::SetString(unsigned int item
,const wxString
& label
)
271 wxRadioButton
*current
=m_radioButtonCycle
;
275 current
=current
->NextInCycle();
277 return current
->SetLabel(label
);
280 //-------------------------------------------------------------------------------------
282 //-------------------------------------------------------------------------------------
283 // Sets a button by passing the desired position. This does not cause
284 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
286 void wxRadioBox::SetSelection(int item
)
289 wxRadioButton
*current
;
294 current
=m_radioButtonCycle
;
297 current
=current
->NextInCycle();
299 current
->SetValue(true);
303 //-------------------------------------------------------------------------------------
305 //-------------------------------------------------------------------------------------
306 // Shows or hides the entire radiobox
308 bool wxRadioBox::Show(bool show
)
310 wxRadioButton
*current
;
312 wxControl::Show(show
);
314 current
=m_radioButtonCycle
;
315 for (unsigned int i
=0; i
<m_noItems
; i
++)
318 current
=current
->NextInCycle();
323 //-------------------------------------------------------------------------------------
324 // ¥ Show(unsigned int, bool)
325 //-------------------------------------------------------------------------------------
326 // Shows or hides the given button
328 bool wxRadioBox::Show(unsigned int item
, bool show
)
334 wxRadioButton
*current
=m_radioButtonCycle
;
337 current
=current
->NextInCycle();
339 return current
->Show(show
);
342 //-------------------------------------------------------------------------------------
344 //-------------------------------------------------------------------------------------
345 // Simulates the effect of the user issuing a command to the item
347 void wxRadioBox::Command (wxCommandEvent
& event
)
349 SetSelection (event
.GetInt());
350 ProcessCommand (event
);
353 //-------------------------------------------------------------------------------------
355 //-------------------------------------------------------------------------------------
356 // Sets the selected button to receive keyboard input
358 void wxRadioBox::SetFocus()
361 wxRadioButton
*current
;
364 current
=m_radioButtonCycle
;
365 while (!current
->GetValue()) {
367 current
=current
->NextInCycle();
373 //-------------------------------------------------------------------------------------
375 //-------------------------------------------------------------------------------------
376 // Simulates the effect of the user issuing a command to the item
378 #define RADIO_SIZE 20
380 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
383 wxRadioButton
*current
;
385 // define the position
387 int x_current
, y_current
;
388 int x_offset
,y_offset
;
389 int widthOld
, heightOld
;
390 GetSize(&widthOld
, &heightOld
);
394 GetPosition(&x_current
, &y_current
);
395 if ((x
== wxDefaultCoord
) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
396 x_offset
= x_current
;
397 if ((y
== wxDefaultCoord
)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
398 y_offset
= y_current
;
402 int charWidth
,charHeight
;
403 int maxWidth
,maxHeight
;
404 int eachWidth
[128],eachHeight
[128];
405 int totWidth
,totHeight
;
407 SetFont(GetParent()->GetFont());
408 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
413 for (i
= 0 ; i
< m_noItems
; i
++)
415 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
416 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
417 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
418 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
419 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
422 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
423 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
425 // only change our width/height if asked for
426 if ( width
== wxDefaultCoord
)
428 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
434 if ( height
== wxDefaultCoord
)
436 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
442 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
444 // arrange radiobuttons
451 if ( UMAGetSystemVersion() >= 0x1030 )
453 //need to add a few more pixels for the top border on panther
454 y_start
= y_start
+ 5; //how many exactly should this be to meet the HIG?
459 current
=m_radioButtonCycle
;
460 for ( i
= 0 ; i
< m_noItems
; i
++)
462 if (i
&&((i%GetMajorDim
())==0)) // not to do for the zero button!
464 if (m_windowStyle
& wxRA_VERTICAL
)
466 x_offset
+= maxWidth
+ charWidth
;
472 y_offset
+= maxHeight
; /*+ charHeight/2;*/
476 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
477 current
=current
->NextInCycle();
479 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
480 y_offset
+= maxHeight
; /*+ charHeight/2;*/
482 x_offset
+= maxWidth
+ charWidth
;
486 wxSize
wxRadioBox::DoGetBestSize() const
488 int charWidth
, charHeight
;
489 int maxWidth
, maxHeight
;
490 int eachWidth
, eachHeight
;
491 int totWidth
, totHeight
;
493 wxFont font
= GetParent()->GetFont();
494 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
495 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
501 for (unsigned int i
= 0 ; i
< m_noItems
; i
++)
503 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
);
504 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
505 eachHeight
= (int)((3 * eachHeight
) / 2);
506 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
507 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
510 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
511 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
513 if ( UMAGetSystemVersion() >= 0x1030 )
515 //need to add a few more pixels for the static boxborder on panther
516 totHeight
= totHeight
+ 10; //how many exactly should this be to meet the HIG?
518 // handle radio box title as well
519 GetTextExtent(GetLabel(), &eachWidth
, NULL
);
520 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
521 if (totWidth
< eachWidth
)
522 totWidth
= eachWidth
;
524 return wxSize(totWidth
, totHeight
);
527 #endif // wxUSE_RADIOBOX