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 //-------------------------------------------------------------------------------------
17 #include "wx/arrstr.h"
19 #include "wx/radiobox.h"
20 #include "wx/radiobut.h"
21 #include "wx/mac/uma.h"
23 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
25 //-------------------------------------------------------------------------------------
27 //-------------------------------------------------------------------------------------
28 // Default constructor
29 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
30 EVT_RADIOBUTTON( wxID_ANY
, wxRadioBox::OnRadioButton
)
33 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
35 if ( outer
.IsChecked() )
37 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
38 int i
= GetSelection() ;
40 event
.SetString( GetString( i
) );
41 event
.SetEventObject( this );
42 ProcessCommand(event
);
46 wxRadioBox::wxRadioBox()
50 m_radioButtonCycle
= NULL
;
53 //-------------------------------------------------------------------------------------
54 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
55 // const wxSize&, int, const wxString[], int, long,
56 // const wxValidator&, const wxString&)
57 //-------------------------------------------------------------------------------------
58 // Contructor, creating and showing a radiobox
63 //-------------------------------------------------------------------------------------
65 //-------------------------------------------------------------------------------------
66 // Destructor, destroying the radiobox item
68 wxRadioBox::~wxRadioBox()
70 m_isBeingDeleted
= true;
72 wxRadioButton
*next
,*current
;
74 current
=m_radioButtonCycle
->NextInCycle();
75 next
=current
->NextInCycle();
76 while (current
!=m_radioButtonCycle
) {
79 next
=current
->NextInCycle();
84 //-------------------------------------------------------------------------------------
86 //-------------------------------------------------------------------------------------
87 // Create the radiobox for two-step construction
89 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
90 const wxPoint
& pos
, const wxSize
& size
,
91 const wxArrayString
& choices
,
92 int majorDim
, long style
,
93 const wxValidator
& val
, const wxString
& name
)
95 wxCArrayString
chs(choices
);
97 return Create(parent
, id
, label
, pos
, size
, chs
.GetCount(),
98 chs
.GetStrings(), majorDim
, style
, val
, name
);
101 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
102 const wxPoint
& pos
, const wxSize
& size
,
103 int n
, const wxString choices
[],
104 int majorDim
, long style
,
105 const wxValidator
& val
, const wxString
& name
)
107 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
113 m_noRowsOrCols
= majorDim
;
114 m_radioButtonCycle
= NULL
;
116 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
121 MacPreControlCreate( parent
, id
, wxStripMenuCodes(label
) , pos
, size
,style
, val
, name
, &bounds
, title
) ;
123 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
124 kControlGroupBoxTextTitleProc
, (long) this ) ;
126 for (i
= 0; i
< n
; i
++)
128 wxRadioButton
*radBtn
= new wxRadioButton
132 wxStripMenuCodes(choices
[i
]),
135 i
== 0 ? wxRB_GROUP
: 0
138 m_radioButtonCycle
= radBtn
;
139 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
143 MacPostControlCreate() ;
149 //-------------------------------------------------------------------------------------
151 //-------------------------------------------------------------------------------------
152 // Enables or disables the entire radiobox
154 bool wxRadioBox::Enable(bool enable
)
157 wxRadioButton
*current
;
159 if (!wxControl::Enable(enable
))
162 current
= m_radioButtonCycle
;
163 for (i
= 0; i
< m_noItems
; i
++) {
164 current
->Enable(enable
);
165 current
= current
->NextInCycle();
170 //-------------------------------------------------------------------------------------
171 // ¥ Enable(int, bool)
172 //-------------------------------------------------------------------------------------
173 // Enables or disables an given button
175 bool wxRadioBox::Enable(int item
, bool enable
)
178 wxRadioButton
*current
;
184 current
= m_radioButtonCycle
;
187 current
= current
->NextInCycle();
189 return current
->Enable(enable
);
192 //-------------------------------------------------------------------------------------
194 //-------------------------------------------------------------------------------------
195 // Returns the radiobox label
197 wxString
wxRadioBox::GetLabel() const
199 return wxControl::GetLabel();
202 //-------------------------------------------------------------------------------------
204 //-------------------------------------------------------------------------------------
205 // Returns the label for the given button
207 wxString
wxRadioBox::GetString(int item
) const
210 wxRadioButton
*current
;
213 return wxEmptyString
;
216 current
= m_radioButtonCycle
;
219 current
= current
->NextInCycle();
221 return current
->GetLabel();
224 //-------------------------------------------------------------------------------------
226 //-------------------------------------------------------------------------------------
227 // Returns the zero-based position of the selected button
229 int wxRadioBox::GetSelection() const
232 wxRadioButton
*current
;
235 current
=m_radioButtonCycle
;
236 while (!current
->GetValue()) {
238 current
=current
->NextInCycle();
244 //-------------------------------------------------------------------------------------
246 //-------------------------------------------------------------------------------------
247 // Returns the number of buttons in the radiobox
252 //-------------------------------------------------------------------------------------
253 // ¥ SetLabel(const wxString&)
254 //-------------------------------------------------------------------------------------
255 // Sets the radiobox label
257 void wxRadioBox::SetLabel(const wxString
& label
)
259 return wxControl::SetLabel(label
);
262 //-------------------------------------------------------------------------------------
263 // ¥ SetLabel(int, const wxString&)
264 //-------------------------------------------------------------------------------------
265 // Sets the label of a given button
267 void wxRadioBox::SetString(int item
,const wxString
& label
)
270 wxRadioButton
*current
;
275 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
)
314 wxRadioButton
*current
;
316 wxControl::Show(show
);
318 current
=m_radioButtonCycle
;
319 for (i
=0;i
<m_noItems
;i
++)
322 current
=current
->NextInCycle();
327 //-------------------------------------------------------------------------------------
329 //-------------------------------------------------------------------------------------
330 // Shows or hides the given button
332 bool wxRadioBox::Show(int item
, bool show
)
335 wxRadioButton
*current
;
340 current
=m_radioButtonCycle
;
343 current
=current
->NextInCycle();
345 return current
->Show(show
);
348 //-------------------------------------------------------------------------------------
350 //-------------------------------------------------------------------------------------
351 // Simulates the effect of the user issuing a command to the item
353 void wxRadioBox::Command (wxCommandEvent
& event
)
355 SetSelection (event
.GetInt());
356 ProcessCommand (event
);
359 //-------------------------------------------------------------------------------------
361 //-------------------------------------------------------------------------------------
362 // Sets the selected button to receive keyboard input
364 void wxRadioBox::SetFocus()
367 wxRadioButton
*current
;
370 current
=m_radioButtonCycle
;
371 while (!current
->GetValue()) {
373 current
=current
->NextInCycle();
379 //-------------------------------------------------------------------------------------
381 //-------------------------------------------------------------------------------------
382 // Simulates the effect of the user issuing a command to the item
384 #define RADIO_SIZE 20
386 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
389 wxRadioButton
*current
;
391 // define the position
393 int x_current
, y_current
;
394 int x_offset
,y_offset
;
395 int widthOld
, heightOld
;
396 GetSize(&widthOld
, &heightOld
);
400 GetPosition(&x_current
, &y_current
);
401 if ((x
== wxDefaultCoord
) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
402 x_offset
= x_current
;
403 if ((y
== wxDefaultCoord
)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
404 y_offset
= y_current
;
408 int charWidth
,charHeight
;
409 int maxWidth
,maxHeight
;
410 int eachWidth
[128],eachHeight
[128];
411 int totWidth
,totHeight
;
413 SetFont(GetParent()->GetFont());
414 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
419 for (i
= 0 ; i
< m_noItems
; i
++)
421 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
422 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
423 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
424 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
425 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
428 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
429 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
431 // only change our width/height if asked for
432 if ( width
== wxDefaultCoord
)
434 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
440 if ( height
== wxDefaultCoord
)
442 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
448 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
450 // arrange radiobuttons
457 if ( UMAGetSystemVersion() >= 0x1030 )
459 //need to add a few more pixels for the top border on panther
460 y_start
= y_start
+ 5; //how many exactly should this be to meet the HIG?
465 current
=m_radioButtonCycle
;
466 for ( i
= 0 ; i
< m_noItems
; i
++)
468 if (i
&&((i%GetMajorDim
())==0)) // not to do for the zero button!
470 if (m_windowStyle
& wxRA_VERTICAL
)
472 x_offset
+= maxWidth
+ charWidth
;
478 y_offset
+= maxHeight
; /*+ charHeight/2;*/
482 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
483 current
=current
->NextInCycle();
485 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
486 y_offset
+= maxHeight
; /*+ charHeight/2;*/
488 x_offset
+= maxWidth
+ charWidth
;
492 wxSize
wxRadioBox::DoGetBestSize() const
494 int charWidth
, charHeight
;
495 int maxWidth
, maxHeight
;
496 int eachWidth
, eachHeight
;
497 int totWidth
, totHeight
;
499 wxFont font
= GetParent()->GetFont();
500 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
501 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
507 for (int i
= 0 ; i
< m_noItems
; i
++)
509 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
);
510 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
511 eachHeight
= (int)((3 * eachHeight
) / 2);
512 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
513 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
516 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
517 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
519 if ( UMAGetSystemVersion() >= 0x1030 )
521 //need to add a few more pixels for the static boxborder on panther
522 totHeight
= totHeight
+ 10; //how many exactly should this be to meet the HIG?
524 // handle radio box title as well
525 GetTextExtent(GetLabel(), &eachWidth
, NULL
);
526 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
527 if (totWidth
< eachWidth
)
528 totWidth
= eachWidth
;
530 return wxSize(totWidth
, totHeight
);