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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "radioboxbase.h"
14 #pragma implementation "radiobox.h"
17 //-------------------------------------------------------------------------------------
19 //-------------------------------------------------------------------------------------
22 #include "wx/arrstr.h"
24 #include "wx/radiobox.h"
25 #include "wx/radiobut.h"
26 #include "wx/mac/uma.h"
28 #if !USE_SHARED_LIBRARY
29 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
32 //-------------------------------------------------------------------------------------
34 //-------------------------------------------------------------------------------------
35 // Default constructor
36 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
37 EVT_RADIOBUTTON( -1 , wxRadioBox::OnRadioButton
)
40 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
42 if ( outer
.IsChecked() )
44 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
45 int i
= GetSelection() ;
47 event
.SetString( GetString( i
) );
48 event
.SetEventObject( this );
49 ProcessCommand(event
);
53 wxRadioBox::wxRadioBox()
58 m_radioButtonCycle
= NULL
;
61 //-------------------------------------------------------------------------------------
62 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
63 // const wxSize&, int, const wxString[], int, long,
64 // const wxValidator&, const wxString&)
65 //-------------------------------------------------------------------------------------
66 // Contructor, creating and showing a radiobox
71 //-------------------------------------------------------------------------------------
73 //-------------------------------------------------------------------------------------
74 // Destructor, destroying the radiobox item
76 wxRadioBox::~wxRadioBox()
78 m_isBeingDeleted
= true;
80 wxRadioButton
*next
,*current
;
82 current
=m_radioButtonCycle
->NextInCycle();
83 next
=current
->NextInCycle();
84 while (current
!=m_radioButtonCycle
) {
87 next
=current
->NextInCycle();
92 //-------------------------------------------------------------------------------------
94 //-------------------------------------------------------------------------------------
95 // Create the radiobox for two-step construction
97 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
98 const wxPoint
& pos
, const wxSize
& size
,
99 const wxArrayString
& choices
,
100 int majorDim
, long style
,
101 const wxValidator
& val
, const wxString
& name
)
103 wxCArrayString
chs(choices
);
105 return Create(parent
, id
, label
, pos
, size
, chs
.GetCount(),
106 chs
.GetStrings(), majorDim
, style
, val
, name
);
109 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
110 const wxPoint
& pos
, const wxSize
& size
,
111 int n
, const wxString choices
[],
112 int majorDim
, long style
,
113 const wxValidator
& val
, const wxString
& name
)
115 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
121 m_noRowsOrCols
= majorDim
;
122 m_radioButtonCycle
= NULL
;
127 m_majorDim
= majorDim
;
132 MacPreControlCreate( parent
, id
, wxStripMenuCodes(label
) , pos
, size
,style
, val
, name
, &bounds
, title
) ;
134 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
135 kControlGroupBoxTextTitleProc
, (long) this ) ;
137 for (i
= 0; i
< n
; i
++)
139 wxRadioButton
*radBtn
= new wxRadioButton
143 wxStripMenuCodes(choices
[i
]),
146 i
== 0 ? wxRB_GROUP
: 0
149 m_radioButtonCycle
= radBtn
;
150 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
154 MacPostControlCreate() ;
160 //-------------------------------------------------------------------------------------
162 //-------------------------------------------------------------------------------------
163 // Enables or disables the entire radiobox
165 bool wxRadioBox::Enable(bool enable
)
168 wxRadioButton
*current
;
170 if (!wxControl::Enable(enable
))
173 current
= m_radioButtonCycle
;
174 for (i
= 0; i
< m_noItems
; i
++) {
175 current
->Enable(enable
);
176 current
= current
->NextInCycle();
181 //-------------------------------------------------------------------------------------
182 // ¥ Enable(int, bool)
183 //-------------------------------------------------------------------------------------
184 // Enables or disables an given button
186 bool wxRadioBox::Enable(int item
, bool enable
)
189 wxRadioButton
*current
;
191 if ((item
< 0) || (item
>= m_noItems
))
195 current
= m_radioButtonCycle
;
198 current
= current
->NextInCycle();
200 return current
->Enable(enable
);
203 //-------------------------------------------------------------------------------------
205 //-------------------------------------------------------------------------------------
206 // Returns the radiobox label
208 wxString
wxRadioBox::GetLabel() const
210 return wxControl::GetLabel();
213 //-------------------------------------------------------------------------------------
215 //-------------------------------------------------------------------------------------
216 // Returns the label for the given button
218 wxString
wxRadioBox::GetString(int item
) const
221 wxRadioButton
*current
;
223 if ((item
< 0) || (item
>= m_noItems
))
224 return wxEmptyString
;
227 current
= m_radioButtonCycle
;
230 current
= current
->NextInCycle();
232 return current
->GetLabel();
235 //-------------------------------------------------------------------------------------
237 //-------------------------------------------------------------------------------------
238 // Returns the zero-based position of the selected button
240 int wxRadioBox::GetSelection() const
243 wxRadioButton
*current
;
246 current
=m_radioButtonCycle
;
247 while (!current
->GetValue()) {
249 current
=current
->NextInCycle();
255 //-------------------------------------------------------------------------------------
257 //-------------------------------------------------------------------------------------
258 // Returns the number of buttons in the radiobox
263 //-------------------------------------------------------------------------------------
264 // ¥ SetLabel(const wxString&)
265 //-------------------------------------------------------------------------------------
266 // Sets the radiobox label
268 void wxRadioBox::SetLabel(const wxString
& label
)
270 return wxControl::SetLabel(label
);
273 //-------------------------------------------------------------------------------------
274 // ¥ SetLabel(int, const wxString&)
275 //-------------------------------------------------------------------------------------
276 // Sets the label of a given button
278 void wxRadioBox::SetString(int item
,const wxString
& label
)
281 wxRadioButton
*current
;
283 if ((item
< 0) || (item
>= m_noItems
))
286 current
=m_radioButtonCycle
;
289 current
=current
->NextInCycle();
291 return current
->SetLabel(label
);
294 //-------------------------------------------------------------------------------------
296 //-------------------------------------------------------------------------------------
297 // Sets a button by passing the desired position. This does not cause
298 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
300 void wxRadioBox::SetSelection(int item
)
303 wxRadioButton
*current
;
305 if ((item
< 0) || (item
>= m_noItems
))
308 current
=m_radioButtonCycle
;
311 current
=current
->NextInCycle();
313 current
->SetValue(true);
317 //-------------------------------------------------------------------------------------
319 //-------------------------------------------------------------------------------------
320 // Shows or hides the entire radiobox
322 bool wxRadioBox::Show(bool show
)
325 wxRadioButton
*current
;
327 wxControl::Show(show
);
329 current
=m_radioButtonCycle
;
330 for (i
=0;i
<m_noItems
;i
++) {
332 current
=current
->NextInCycle();
337 //-------------------------------------------------------------------------------------
339 //-------------------------------------------------------------------------------------
340 // Shows or hides the given button
342 void wxRadioBox::Show(int item
, bool show
)
345 wxRadioButton
*current
;
347 if ((item
< 0) || (item
>= m_noItems
))
350 current
=m_radioButtonCycle
;
353 current
=current
->NextInCycle();
358 //-------------------------------------------------------------------------------------
360 //-------------------------------------------------------------------------------------
361 // Simulates the effect of the user issuing a command to the item
363 void wxRadioBox::Command (wxCommandEvent
& event
)
365 SetSelection (event
.GetInt());
366 ProcessCommand (event
);
369 //-------------------------------------------------------------------------------------
371 //-------------------------------------------------------------------------------------
372 // Sets the selected button to receive keyboard input
374 void wxRadioBox::SetFocus()
377 wxRadioButton
*current
;
380 current
=m_radioButtonCycle
;
381 while (!current
->GetValue()) {
383 current
=current
->NextInCycle();
389 //-------------------------------------------------------------------------------------
391 //-------------------------------------------------------------------------------------
392 // Simulates the effect of the user issuing a command to the item
394 #define RADIO_SIZE 20
396 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
399 wxRadioButton
*current
;
401 // define the position
403 int x_current
, y_current
;
404 int x_offset
,y_offset
;
405 int widthOld
, heightOld
;
406 GetSize(&widthOld
, &heightOld
);
410 GetPosition(&x_current
, &y_current
);
411 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
412 x_offset
= x_current
;
413 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
414 y_offset
= y_current
;
418 int charWidth
,charHeight
;
419 int maxWidth
,maxHeight
;
420 int eachWidth
[128],eachHeight
[128];
421 int totWidth
,totHeight
;
423 SetFont(GetParent()->GetFont());
424 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
429 for (i
= 0 ; i
< m_noItems
; i
++)
431 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
432 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
433 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
434 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
435 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
438 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
439 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
441 // only change our width/height if asked for
444 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
452 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
458 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
460 // arrange radiobuttons
467 if ( UMAGetSystemVersion() >= 0x1030 )
469 //need to add a few more pixels for the top border on panther
470 y_start
= y_start
+ 5; //how many exactly should this be to meet the HIG?
475 current
=m_radioButtonCycle
;
476 for ( i
= 0 ; i
< m_noItems
; i
++)
478 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
480 if (m_windowStyle
& wxRA_VERTICAL
)
482 x_offset
+= maxWidth
+ charWidth
;
488 y_offset
+= maxHeight
; /*+ charHeight/2;*/
492 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
493 current
=current
->NextInCycle();
495 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
496 y_offset
+= maxHeight
; /*+ charHeight/2;*/
498 x_offset
+= maxWidth
+ charWidth
;
502 wxSize
wxRadioBox::DoGetBestSize() const
504 int charWidth
, charHeight
;
505 int maxWidth
, maxHeight
;
506 int eachWidth
, eachHeight
;
507 int totWidth
, totHeight
;
509 wxFont font
= GetParent()->GetFont();
510 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
511 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
517 for (int i
= 0 ; i
< m_noItems
; i
++)
519 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
);
520 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
521 eachHeight
= (int)((3 * eachHeight
) / 2);
522 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
523 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
526 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
527 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
529 if ( UMAGetSystemVersion() >= 0x1030 )
531 //need to add a few more pixels for the static boxborder on panther
532 totHeight
= totHeight
+ 10; //how many exactly should this be to meet the HIG?
534 // handle radio box title as well
535 GetTextExtent(GetTitle(), &eachWidth
, NULL
);
536 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
537 if (totWidth
< eachWidth
)
538 totWidth
= eachWidth
;
540 return wxSize(totWidth
, totHeight
);
542 //-------------------------------------------------------------------------------------
544 //-------------------------------------------------------------------------------------
545 // return the number of buttons in the vertical direction
547 int wxRadioBox::GetRowCount() const
549 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
555 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
559 //-------------------------------------------------------------------------------------
561 //-------------------------------------------------------------------------------------
562 // return the number of buttons in the horizontal direction
564 int wxRadioBox::GetColumnCount() const
566 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
568 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;