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/arrstr.h"
21 #include "wx/radiobox.h"
22 #include "wx/radiobut.h"
23 #include "wx/mac/uma.h"
25 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
27 //-------------------------------------------------------------------------------------
29 //-------------------------------------------------------------------------------------
30 // Default constructor
31 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
32 EVT_RADIOBUTTON( wxID_ANY
, wxRadioBox::OnRadioButton
)
35 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
37 if ( outer
.IsChecked() )
39 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
40 int i
= GetSelection() ;
42 event
.SetString(GetString(i
));
43 event
.SetEventObject( this );
44 ProcessCommand(event
);
48 wxRadioBox::wxRadioBox()
52 m_radioButtonCycle
= NULL
;
55 //-------------------------------------------------------------------------------------
56 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
57 // const wxSize&, int, const wxString[], int, long,
58 // const wxValidator&, const wxString&)
59 //-------------------------------------------------------------------------------------
60 // Contructor, creating and showing a radiobox
65 //-------------------------------------------------------------------------------------
67 //-------------------------------------------------------------------------------------
68 // Destructor, destroying the radiobox item
70 wxRadioBox::~wxRadioBox()
72 m_isBeingDeleted
= true;
74 wxRadioButton
*next
,*current
;
76 current
=m_radioButtonCycle
->NextInCycle();
77 next
=current
->NextInCycle();
78 while (current
!=m_radioButtonCycle
) {
81 next
=current
->NextInCycle();
86 //-------------------------------------------------------------------------------------
88 //-------------------------------------------------------------------------------------
89 // Create the radiobox for two-step construction
91 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
92 const wxPoint
& pos
, const wxSize
& size
,
93 const wxArrayString
& choices
,
94 int majorDim
, long style
,
95 const wxValidator
& val
, const wxString
& name
)
97 wxCArrayString
chs(choices
);
99 return Create(parent
, id
, label
, pos
, size
, chs
.GetCount(),
100 chs
.GetStrings(), majorDim
, style
, val
, name
);
103 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
104 const wxPoint
& pos
, const wxSize
& size
,
105 int n
, const wxString choices
[],
106 int majorDim
, long style
,
107 const wxValidator
& val
, const wxString
& name
)
109 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
114 m_noItems
= (unsigned int)n
;
115 m_noRowsOrCols
= majorDim
;
116 m_radioButtonCycle
= NULL
;
118 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
123 MacPreControlCreate( parent
, id
, wxStripMenuCodes(label
) , pos
, size
,style
, val
, name
, &bounds
, title
) ;
125 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
126 kControlGroupBoxTextTitleProc
, (long) this ) ;
128 for (i
= 0; i
< n
; i
++)
130 wxRadioButton
*radBtn
= new wxRadioButton
134 wxStripMenuCodes(choices
[i
]),
137 i
== 0 ? wxRB_GROUP
: 0
140 m_radioButtonCycle
= radBtn
;
141 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
145 MacPostControlCreate() ;
151 //-------------------------------------------------------------------------------------
153 //-------------------------------------------------------------------------------------
154 // Enables or disables the entire radiobox
156 bool wxRadioBox::Enable(bool enable
)
158 if (!wxControl::Enable(enable
))
161 wxRadioButton
*current
= m_radioButtonCycle
;
162 for (unsigned int i
= 0; i
< m_noItems
; i
++)
164 current
->Enable(enable
);
165 current
= current
->NextInCycle();
170 //-------------------------------------------------------------------------------------
171 // ¥ Enable(unsigned int, bool)
172 //-------------------------------------------------------------------------------------
173 // Enables or disables an given button
175 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
181 wxRadioButton
*current
= m_radioButtonCycle
;
185 current
= current
->NextInCycle();
187 return current
->Enable(enable
);
190 //-------------------------------------------------------------------------------------
192 //-------------------------------------------------------------------------------------
193 // Returns the radiobox label
195 wxString
wxRadioBox::GetLabel() const
197 return wxControl::GetLabel();
200 //-------------------------------------------------------------------------------------
202 //-------------------------------------------------------------------------------------
203 // Returns the label for the given button
205 wxString
wxRadioBox::GetString(unsigned int item
) const
207 wxRadioButton
*current
;
210 return wxEmptyString
;
213 current
= m_radioButtonCycle
;
216 current
= current
->NextInCycle();
218 return current
->GetLabel();
221 //-------------------------------------------------------------------------------------
223 //-------------------------------------------------------------------------------------
224 // Returns the zero-based position of the selected button
226 int wxRadioBox::GetSelection() const
229 wxRadioButton
*current
;
232 current
=m_radioButtonCycle
;
233 while (!current
->GetValue()) {
235 current
=current
->NextInCycle();
241 //-------------------------------------------------------------------------------------
243 //-------------------------------------------------------------------------------------
244 // Returns the number of buttons in the radiobox
249 //-------------------------------------------------------------------------------------
250 // ¥ SetLabel(const wxString&)
251 //-------------------------------------------------------------------------------------
252 // Sets the radiobox label
254 void wxRadioBox::SetLabel(const wxString
& label
)
256 return wxControl::SetLabel(label
);
259 //-------------------------------------------------------------------------------------
260 // ¥ SetLabel(int, const wxString&)
261 //-------------------------------------------------------------------------------------
262 // Sets the label of a given button
264 void wxRadioBox::SetString(unsigned int item
,const wxString
& label
)
270 wxRadioButton
*current
=m_radioButtonCycle
;
274 current
=current
->NextInCycle();
276 return current
->SetLabel(label
);
279 //-------------------------------------------------------------------------------------
281 //-------------------------------------------------------------------------------------
282 // Sets a button by passing the desired position. This does not cause
283 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
285 void wxRadioBox::SetSelection(int item
)
288 wxRadioButton
*current
;
293 current
=m_radioButtonCycle
;
296 current
=current
->NextInCycle();
298 current
->SetValue(true);
302 //-------------------------------------------------------------------------------------
304 //-------------------------------------------------------------------------------------
305 // Shows or hides the entire radiobox
307 bool wxRadioBox::Show(bool show
)
309 wxRadioButton
*current
;
311 wxControl::Show(show
);
313 current
=m_radioButtonCycle
;
314 for (unsigned int i
=0; i
<m_noItems
; i
++)
317 current
=current
->NextInCycle();
322 //-------------------------------------------------------------------------------------
323 // ¥ Show(unsigned int, bool)
324 //-------------------------------------------------------------------------------------
325 // Shows or hides the given button
327 bool wxRadioBox::Show(unsigned int item
, bool show
)
333 wxRadioButton
*current
=m_radioButtonCycle
;
336 current
=current
->NextInCycle();
338 return current
->Show(show
);
341 //-------------------------------------------------------------------------------------
343 //-------------------------------------------------------------------------------------
344 // Simulates the effect of the user issuing a command to the item
346 void wxRadioBox::Command (wxCommandEvent
& event
)
348 SetSelection (event
.GetInt());
349 ProcessCommand (event
);
352 //-------------------------------------------------------------------------------------
354 //-------------------------------------------------------------------------------------
355 // Sets the selected button to receive keyboard input
357 void wxRadioBox::SetFocus()
360 wxRadioButton
*current
;
363 current
=m_radioButtonCycle
;
364 while (!current
->GetValue()) {
366 current
=current
->NextInCycle();
372 //-------------------------------------------------------------------------------------
374 //-------------------------------------------------------------------------------------
375 // Simulates the effect of the user issuing a command to the item
377 #define RADIO_SIZE 20
379 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
382 wxRadioButton
*current
;
384 // define the position
386 int x_current
, y_current
;
387 int x_offset
,y_offset
;
388 int widthOld
, heightOld
;
389 GetSize(&widthOld
, &heightOld
);
393 GetPosition(&x_current
, &y_current
);
394 if ((x
== wxDefaultCoord
) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
395 x_offset
= x_current
;
396 if ((y
== wxDefaultCoord
)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
397 y_offset
= y_current
;
401 int charWidth
,charHeight
;
402 int maxWidth
,maxHeight
;
403 int eachWidth
[128],eachHeight
[128];
404 int totWidth
,totHeight
;
406 SetFont(GetParent()->GetFont());
407 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
412 for (i
= 0 ; i
< m_noItems
; i
++)
414 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
415 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
416 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
417 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
418 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
421 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
422 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
424 // only change our width/height if asked for
425 if ( width
== wxDefaultCoord
)
427 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
433 if ( height
== wxDefaultCoord
)
435 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
441 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
443 // arrange radiobuttons
450 if ( UMAGetSystemVersion() >= 0x1030 )
452 //need to add a few more pixels for the top border on panther
453 y_start
= y_start
+ 5; //how many exactly should this be to meet the HIG?
458 current
=m_radioButtonCycle
;
459 for ( i
= 0 ; i
< m_noItems
; i
++)
461 if (i
&&((i%GetMajorDim
())==0)) // not to do for the zero button!
463 if (m_windowStyle
& wxRA_VERTICAL
)
465 x_offset
+= maxWidth
+ charWidth
;
471 y_offset
+= maxHeight
; /*+ charHeight/2;*/
475 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
476 current
=current
->NextInCycle();
478 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
479 y_offset
+= maxHeight
; /*+ charHeight/2;*/
481 x_offset
+= maxWidth
+ charWidth
;
485 wxSize
wxRadioBox::DoGetBestSize() const
487 int charWidth
, charHeight
;
488 int maxWidth
, maxHeight
;
489 int eachWidth
, eachHeight
;
490 int totWidth
, totHeight
;
492 wxFont font
= GetParent()->GetFont();
493 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
494 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
500 for (unsigned int i
= 0 ; i
< m_noItems
; i
++)
502 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
);
503 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
504 eachHeight
= (int)((3 * eachHeight
) / 2);
505 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
506 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
509 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
510 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
512 if ( UMAGetSystemVersion() >= 0x1030 )
514 //need to add a few more pixels for the static boxborder on panther
515 totHeight
= totHeight
+ 10; //how many exactly should this be to meet the HIG?
517 // handle radio box title as well
518 GetTextExtent(GetLabel(), &eachWidth
, NULL
);
519 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
520 if (totWidth
< eachWidth
)
521 totWidth
= eachWidth
;
523 return wxSize(totWidth
, totHeight
);
526 #endif // wxUSE_RADIOBOX