1 /////////////////////////////////////////////////////////////////////////////
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()
51 m_radioButtonCycle
= NULL
;
54 //-------------------------------------------------------------------------------------
55 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
56 // const wxSize&, int, const wxString[], int, long,
57 // const wxValidator&, const wxString&)
58 //-------------------------------------------------------------------------------------
59 // Contructor, creating and showing a radiobox
64 //-------------------------------------------------------------------------------------
66 //-------------------------------------------------------------------------------------
67 // Destructor, destroying the radiobox item
69 wxRadioBox::~wxRadioBox()
71 m_isBeingDeleted
= true;
73 wxRadioButton
*next
,*current
;
75 current
=m_radioButtonCycle
->NextInCycle();
76 next
=current
->NextInCycle();
77 while (current
!=m_radioButtonCycle
) {
80 next
=current
->NextInCycle();
85 //-------------------------------------------------------------------------------------
87 //-------------------------------------------------------------------------------------
88 // Create the radiobox for two-step construction
90 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
91 const wxPoint
& pos
, const wxSize
& size
,
92 const wxArrayString
& choices
,
93 int majorDim
, long style
,
94 const wxValidator
& val
, const wxString
& name
)
96 wxCArrayString
chs(choices
);
98 return Create(parent
, id
, label
, pos
, size
, chs
.GetCount(),
99 chs
.GetStrings(), majorDim
, style
, val
, name
);
102 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
103 const wxPoint
& pos
, const wxSize
& size
,
104 int n
, const wxString choices
[],
105 int majorDim
, long style
,
106 const wxValidator
& val
, const wxString
& name
)
108 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
114 m_noRowsOrCols
= majorDim
;
115 m_radioButtonCycle
= NULL
;
120 m_majorDim
= majorDim
;
125 MacPreControlCreate( parent
, id
, wxStripMenuCodes(label
) , pos
, size
,style
, val
, name
, &bounds
, title
) ;
127 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
128 kControlGroupBoxTextTitleProc
, (long) this ) ;
130 for (i
= 0; i
< n
; i
++)
132 wxRadioButton
*radBtn
= new wxRadioButton
136 wxStripMenuCodes(choices
[i
]),
139 i
== 0 ? wxRB_GROUP
: 0
142 m_radioButtonCycle
= radBtn
;
143 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
147 MacPostControlCreate() ;
153 //-------------------------------------------------------------------------------------
155 //-------------------------------------------------------------------------------------
156 // Enables or disables the entire radiobox
158 bool wxRadioBox::Enable(bool enable
)
161 wxRadioButton
*current
;
163 if (!wxControl::Enable(enable
))
166 current
= m_radioButtonCycle
;
167 for (i
= 0; i
< m_noItems
; i
++) {
168 current
->Enable(enable
);
169 current
= current
->NextInCycle();
174 //-------------------------------------------------------------------------------------
175 // ¥ Enable(int, bool)
176 //-------------------------------------------------------------------------------------
177 // Enables or disables an given button
179 bool wxRadioBox::Enable(int item
, bool enable
)
182 wxRadioButton
*current
;
188 current
= m_radioButtonCycle
;
191 current
= current
->NextInCycle();
193 return current
->Enable(enable
);
196 //-------------------------------------------------------------------------------------
198 //-------------------------------------------------------------------------------------
199 // Returns the radiobox label
201 wxString
wxRadioBox::GetLabel() const
203 return wxControl::GetLabel();
206 //-------------------------------------------------------------------------------------
208 //-------------------------------------------------------------------------------------
209 // Returns the label for the given button
211 wxString
wxRadioBox::GetString(int item
) const
214 wxRadioButton
*current
;
217 return wxEmptyString
;
220 current
= m_radioButtonCycle
;
223 current
= current
->NextInCycle();
225 return current
->GetLabel();
228 //-------------------------------------------------------------------------------------
230 //-------------------------------------------------------------------------------------
231 // Returns the zero-based position of the selected button
233 int wxRadioBox::GetSelection() const
236 wxRadioButton
*current
;
239 current
=m_radioButtonCycle
;
240 while (!current
->GetValue()) {
242 current
=current
->NextInCycle();
248 //-------------------------------------------------------------------------------------
250 //-------------------------------------------------------------------------------------
251 // Returns the number of buttons in the radiobox
256 //-------------------------------------------------------------------------------------
257 // ¥ SetLabel(const wxString&)
258 //-------------------------------------------------------------------------------------
259 // Sets the radiobox label
261 void wxRadioBox::SetLabel(const wxString
& label
)
263 return wxControl::SetLabel(label
);
266 //-------------------------------------------------------------------------------------
267 // ¥ SetLabel(int, const wxString&)
268 //-------------------------------------------------------------------------------------
269 // Sets the label of a given button
271 void wxRadioBox::SetString(int item
,const wxString
& label
)
274 wxRadioButton
*current
;
279 current
=m_radioButtonCycle
;
282 current
=current
->NextInCycle();
284 return current
->SetLabel(label
);
287 //-------------------------------------------------------------------------------------
289 //-------------------------------------------------------------------------------------
290 // Sets a button by passing the desired position. This does not cause
291 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
293 void wxRadioBox::SetSelection(int item
)
296 wxRadioButton
*current
;
301 current
=m_radioButtonCycle
;
304 current
=current
->NextInCycle();
306 current
->SetValue(true);
310 //-------------------------------------------------------------------------------------
312 //-------------------------------------------------------------------------------------
313 // Shows or hides the entire radiobox
315 bool wxRadioBox::Show(bool show
)
318 wxRadioButton
*current
;
320 wxControl::Show(show
);
322 current
=m_radioButtonCycle
;
323 for (i
=0;i
<m_noItems
;i
++)
326 current
=current
->NextInCycle();
331 //-------------------------------------------------------------------------------------
333 //-------------------------------------------------------------------------------------
334 // Shows or hides the given button
336 bool wxRadioBox::Show(int item
, bool show
)
339 wxRadioButton
*current
;
344 current
=m_radioButtonCycle
;
347 current
=current
->NextInCycle();
349 return current
->Show(show
);
352 //-------------------------------------------------------------------------------------
354 //-------------------------------------------------------------------------------------
355 // Simulates the effect of the user issuing a command to the item
357 void wxRadioBox::Command (wxCommandEvent
& event
)
359 SetSelection (event
.GetInt());
360 ProcessCommand (event
);
363 //-------------------------------------------------------------------------------------
365 //-------------------------------------------------------------------------------------
366 // Sets the selected button to receive keyboard input
368 void wxRadioBox::SetFocus()
371 wxRadioButton
*current
;
374 current
=m_radioButtonCycle
;
375 while (!current
->GetValue()) {
377 current
=current
->NextInCycle();
383 //-------------------------------------------------------------------------------------
385 //-------------------------------------------------------------------------------------
386 // Simulates the effect of the user issuing a command to the item
388 #define RADIO_SIZE 20
390 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
393 wxRadioButton
*current
;
395 // define the position
397 int x_current
, y_current
;
398 int x_offset
,y_offset
;
399 int widthOld
, heightOld
;
400 GetSize(&widthOld
, &heightOld
);
404 GetPosition(&x_current
, &y_current
);
405 if ((x
== wxDefaultCoord
) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
406 x_offset
= x_current
;
407 if ((y
== wxDefaultCoord
)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
408 y_offset
= y_current
;
412 int charWidth
,charHeight
;
413 int maxWidth
,maxHeight
;
414 int eachWidth
[128],eachHeight
[128];
415 int totWidth
,totHeight
;
417 SetFont(GetParent()->GetFont());
418 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
423 for (i
= 0 ; i
< m_noItems
; i
++)
425 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
426 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
427 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
428 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
429 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
432 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
433 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
435 // only change our width/height if asked for
436 if ( width
== wxDefaultCoord
)
438 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
444 if ( height
== wxDefaultCoord
)
446 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
452 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
454 // arrange radiobuttons
461 if ( UMAGetSystemVersion() >= 0x1030 )
463 //need to add a few more pixels for the top border on panther
464 y_start
= y_start
+ 5; //how many exactly should this be to meet the HIG?
469 current
=m_radioButtonCycle
;
470 for ( i
= 0 ; i
< m_noItems
; i
++)
472 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
474 if (m_windowStyle
& wxRA_VERTICAL
)
476 x_offset
+= maxWidth
+ charWidth
;
482 y_offset
+= maxHeight
; /*+ charHeight/2;*/
486 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
487 current
=current
->NextInCycle();
489 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
490 y_offset
+= maxHeight
; /*+ charHeight/2;*/
492 x_offset
+= maxWidth
+ charWidth
;
496 wxSize
wxRadioBox::DoGetBestSize() const
498 int charWidth
, charHeight
;
499 int maxWidth
, maxHeight
;
500 int eachWidth
, eachHeight
;
501 int totWidth
, totHeight
;
503 wxFont font
= GetParent()->GetFont();
504 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
505 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
511 for (int i
= 0 ; i
< m_noItems
; i
++)
513 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
);
514 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
515 eachHeight
= (int)((3 * eachHeight
) / 2);
516 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
517 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
520 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
521 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
523 if ( UMAGetSystemVersion() >= 0x1030 )
525 //need to add a few more pixels for the static boxborder on panther
526 totHeight
= totHeight
+ 10; //how many exactly should this be to meet the HIG?
528 // handle radio box title as well
529 GetTextExtent(GetTitle(), &eachWidth
, NULL
);
530 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
531 if (totWidth
< eachWidth
)
532 totWidth
= eachWidth
;
534 return wxSize(totWidth
, totHeight
);
536 //-------------------------------------------------------------------------------------
538 //-------------------------------------------------------------------------------------
539 // return the number of buttons in the vertical direction
541 int wxRadioBox::GetRowCount() const
543 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
549 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
553 //-------------------------------------------------------------------------------------
555 //-------------------------------------------------------------------------------------
556 // return the number of buttons in the horizontal direction
558 int wxRadioBox::GetColumnCount() const
560 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
562 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;