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
= (size_t)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 wxRadioButton
*current
;
160 if (!wxControl::Enable(enable
))
163 current
= m_radioButtonCycle
;
164 for (size_t i
= 0; i
< m_noItems
; i
++) {
165 current
->Enable(enable
);
166 current
= current
->NextInCycle();
171 //-------------------------------------------------------------------------------------
172 // ¥ Enable(int, bool)
173 //-------------------------------------------------------------------------------------
174 // Enables or disables an given button
176 bool wxRadioBox::Enable(int item
, bool enable
)
179 wxRadioButton
*current
;
185 current
= m_radioButtonCycle
;
188 current
= current
->NextInCycle();
190 return current
->Enable(enable
);
193 //-------------------------------------------------------------------------------------
195 //-------------------------------------------------------------------------------------
196 // Returns the radiobox label
198 wxString
wxRadioBox::GetLabel() const
200 return wxControl::GetLabel();
203 //-------------------------------------------------------------------------------------
205 //-------------------------------------------------------------------------------------
206 // Returns the label for the given button
208 wxString
wxRadioBox::GetString(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(int item
,const wxString
& label
)
271 wxRadioButton
*current
;
276 current
=m_radioButtonCycle
;
279 current
=current
->NextInCycle();
281 return current
->SetLabel(label
);
284 //-------------------------------------------------------------------------------------
286 //-------------------------------------------------------------------------------------
287 // Sets a button by passing the desired position. This does not cause
288 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
290 void wxRadioBox::SetSelection(int item
)
293 wxRadioButton
*current
;
298 current
=m_radioButtonCycle
;
301 current
=current
->NextInCycle();
303 current
->SetValue(true);
307 //-------------------------------------------------------------------------------------
309 //-------------------------------------------------------------------------------------
310 // Shows or hides the entire radiobox
312 bool wxRadioBox::Show(bool show
)
314 wxRadioButton
*current
;
316 wxControl::Show(show
);
318 current
=m_radioButtonCycle
;
319 for (size_t 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 (size_t 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
);
533 #endif // wxUSE_RADIOBOX