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 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
30 //-------------------------------------------------------------------------------------
32 //-------------------------------------------------------------------------------------
33 // Default constructor
34 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
35 EVT_RADIOBUTTON( wxID_ANY
, wxRadioBox::OnRadioButton
)
38 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
40 if ( outer
.IsChecked() )
42 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
43 int i
= GetSelection() ;
45 event
.SetString( GetString( i
) );
46 event
.SetEventObject( this );
47 ProcessCommand(event
);
51 wxRadioBox::wxRadioBox()
56 m_radioButtonCycle
= NULL
;
59 //-------------------------------------------------------------------------------------
60 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
61 // const wxSize&, int, const wxString[], int, long,
62 // const wxValidator&, const wxString&)
63 //-------------------------------------------------------------------------------------
64 // Contructor, creating and showing a radiobox
69 //-------------------------------------------------------------------------------------
71 //-------------------------------------------------------------------------------------
72 // Destructor, destroying the radiobox item
74 wxRadioBox::~wxRadioBox()
76 m_isBeingDeleted
= true;
78 wxRadioButton
*next
,*current
;
80 current
=m_radioButtonCycle
->NextInCycle();
81 next
=current
->NextInCycle();
82 while (current
!=m_radioButtonCycle
) {
85 next
=current
->NextInCycle();
90 //-------------------------------------------------------------------------------------
92 //-------------------------------------------------------------------------------------
93 // Create the radiobox for two-step construction
95 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
96 const wxPoint
& pos
, const wxSize
& size
,
97 const wxArrayString
& choices
,
98 int majorDim
, long style
,
99 const wxValidator
& val
, const wxString
& name
)
101 wxCArrayString
chs(choices
);
103 return Create(parent
, id
, label
, pos
, size
, chs
.GetCount(),
104 chs
.GetStrings(), majorDim
, style
, val
, name
);
107 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
108 const wxPoint
& pos
, const wxSize
& size
,
109 int n
, const wxString choices
[],
110 int majorDim
, long style
,
111 const wxValidator
& val
, const wxString
& name
)
113 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
119 m_noRowsOrCols
= majorDim
;
120 m_radioButtonCycle
= NULL
;
125 m_majorDim
= majorDim
;
130 MacPreControlCreate( parent
, id
, wxStripMenuCodes(label
) , pos
, size
,style
, val
, name
, &bounds
, title
) ;
132 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
133 kControlGroupBoxTextTitleProc
, (long) this ) ;
135 for (i
= 0; i
< n
; i
++)
137 wxRadioButton
*radBtn
= new wxRadioButton
141 wxStripMenuCodes(choices
[i
]),
144 i
== 0 ? wxRB_GROUP
: 0
147 m_radioButtonCycle
= radBtn
;
148 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
152 MacPostControlCreate() ;
158 //-------------------------------------------------------------------------------------
160 //-------------------------------------------------------------------------------------
161 // Enables or disables the entire radiobox
163 bool wxRadioBox::Enable(bool enable
)
166 wxRadioButton
*current
;
168 if (!wxControl::Enable(enable
))
171 current
= m_radioButtonCycle
;
172 for (i
= 0; i
< m_noItems
; i
++) {
173 current
->Enable(enable
);
174 current
= current
->NextInCycle();
179 //-------------------------------------------------------------------------------------
180 // ¥ Enable(int, bool)
181 //-------------------------------------------------------------------------------------
182 // Enables or disables an given button
184 bool wxRadioBox::Enable(int item
, bool enable
)
187 wxRadioButton
*current
;
193 current
= m_radioButtonCycle
;
196 current
= current
->NextInCycle();
198 return current
->Enable(enable
);
201 //-------------------------------------------------------------------------------------
203 //-------------------------------------------------------------------------------------
204 // Returns the radiobox label
206 wxString
wxRadioBox::GetLabel() const
208 return wxControl::GetLabel();
211 //-------------------------------------------------------------------------------------
213 //-------------------------------------------------------------------------------------
214 // Returns the label for the given button
216 wxString
wxRadioBox::GetString(int item
) const
219 wxRadioButton
*current
;
222 return wxEmptyString
;
225 current
= m_radioButtonCycle
;
228 current
= current
->NextInCycle();
230 return current
->GetLabel();
233 //-------------------------------------------------------------------------------------
235 //-------------------------------------------------------------------------------------
236 // Returns the zero-based position of the selected button
238 int wxRadioBox::GetSelection() const
241 wxRadioButton
*current
;
244 current
=m_radioButtonCycle
;
245 while (!current
->GetValue()) {
247 current
=current
->NextInCycle();
253 //-------------------------------------------------------------------------------------
255 //-------------------------------------------------------------------------------------
256 // Returns the number of buttons in the radiobox
261 //-------------------------------------------------------------------------------------
262 // ¥ SetLabel(const wxString&)
263 //-------------------------------------------------------------------------------------
264 // Sets the radiobox label
266 void wxRadioBox::SetLabel(const wxString
& label
)
268 return wxControl::SetLabel(label
);
271 //-------------------------------------------------------------------------------------
272 // ¥ SetLabel(int, const wxString&)
273 //-------------------------------------------------------------------------------------
274 // Sets the label of a given button
276 void wxRadioBox::SetString(int item
,const wxString
& label
)
279 wxRadioButton
*current
;
284 current
=m_radioButtonCycle
;
287 current
=current
->NextInCycle();
289 return current
->SetLabel(label
);
292 //-------------------------------------------------------------------------------------
294 //-------------------------------------------------------------------------------------
295 // Sets a button by passing the desired position. This does not cause
296 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
298 void wxRadioBox::SetSelection(int item
)
301 wxRadioButton
*current
;
306 current
=m_radioButtonCycle
;
309 current
=current
->NextInCycle();
311 current
->SetValue(true);
315 //-------------------------------------------------------------------------------------
317 //-------------------------------------------------------------------------------------
318 // Shows or hides the entire radiobox
320 bool wxRadioBox::Show(bool show
)
323 wxRadioButton
*current
;
325 wxControl::Show(show
);
327 current
=m_radioButtonCycle
;
328 for (i
=0;i
<m_noItems
;i
++)
331 current
=current
->NextInCycle();
336 //-------------------------------------------------------------------------------------
338 //-------------------------------------------------------------------------------------
339 // Shows or hides the given button
341 bool wxRadioBox::Show(int item
, bool show
)
344 wxRadioButton
*current
;
349 current
=m_radioButtonCycle
;
352 current
=current
->NextInCycle();
354 return current
->Show(show
);
357 //-------------------------------------------------------------------------------------
359 //-------------------------------------------------------------------------------------
360 // Simulates the effect of the user issuing a command to the item
362 void wxRadioBox::Command (wxCommandEvent
& event
)
364 SetSelection (event
.GetInt());
365 ProcessCommand (event
);
368 //-------------------------------------------------------------------------------------
370 //-------------------------------------------------------------------------------------
371 // Sets the selected button to receive keyboard input
373 void wxRadioBox::SetFocus()
376 wxRadioButton
*current
;
379 current
=m_radioButtonCycle
;
380 while (!current
->GetValue()) {
382 current
=current
->NextInCycle();
388 //-------------------------------------------------------------------------------------
390 //-------------------------------------------------------------------------------------
391 // Simulates the effect of the user issuing a command to the item
393 #define RADIO_SIZE 20
395 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
398 wxRadioButton
*current
;
400 // define the position
402 int x_current
, y_current
;
403 int x_offset
,y_offset
;
404 int widthOld
, heightOld
;
405 GetSize(&widthOld
, &heightOld
);
409 GetPosition(&x_current
, &y_current
);
410 if ((x
== wxDefaultCoord
) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
411 x_offset
= x_current
;
412 if ((y
== wxDefaultCoord
)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
413 y_offset
= y_current
;
417 int charWidth
,charHeight
;
418 int maxWidth
,maxHeight
;
419 int eachWidth
[128],eachHeight
[128];
420 int totWidth
,totHeight
;
422 SetFont(GetParent()->GetFont());
423 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
428 for (i
= 0 ; i
< m_noItems
; i
++)
430 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
431 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
432 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
433 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
434 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
437 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
438 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
440 // only change our width/height if asked for
441 if ( width
== wxDefaultCoord
)
443 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
449 if ( height
== wxDefaultCoord
)
451 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
457 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
459 // arrange radiobuttons
466 if ( UMAGetSystemVersion() >= 0x1030 )
468 //need to add a few more pixels for the top border on panther
469 y_start
= y_start
+ 5; //how many exactly should this be to meet the HIG?
474 current
=m_radioButtonCycle
;
475 for ( i
= 0 ; i
< m_noItems
; i
++)
477 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
479 if (m_windowStyle
& wxRA_VERTICAL
)
481 x_offset
+= maxWidth
+ charWidth
;
487 y_offset
+= maxHeight
; /*+ charHeight/2;*/
491 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
492 current
=current
->NextInCycle();
494 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
495 y_offset
+= maxHeight
; /*+ charHeight/2;*/
497 x_offset
+= maxWidth
+ charWidth
;
501 wxSize
wxRadioBox::DoGetBestSize() const
503 int charWidth
, charHeight
;
504 int maxWidth
, maxHeight
;
505 int eachWidth
, eachHeight
;
506 int totWidth
, totHeight
;
508 wxFont font
= GetParent()->GetFont();
509 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
510 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
516 for (int i
= 0 ; i
< m_noItems
; i
++)
518 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
);
519 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
520 eachHeight
= (int)((3 * eachHeight
) / 2);
521 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
522 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
525 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
526 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
528 if ( UMAGetSystemVersion() >= 0x1030 )
530 //need to add a few more pixels for the static boxborder on panther
531 totHeight
= totHeight
+ 10; //how many exactly should this be to meet the HIG?
533 // handle radio box title as well
534 GetTextExtent(GetTitle(), &eachWidth
, NULL
);
535 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
536 if (totWidth
< eachWidth
)
537 totWidth
= eachWidth
;
539 return wxSize(totWidth
, totHeight
);
541 //-------------------------------------------------------------------------------------
543 //-------------------------------------------------------------------------------------
544 // return the number of buttons in the vertical direction
546 int wxRadioBox::GetRowCount() const
548 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
554 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
558 //-------------------------------------------------------------------------------------
560 //-------------------------------------------------------------------------------------
561 // return the number of buttons in the horizontal direction
563 int wxRadioBox::GetColumnCount() const
565 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
567 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;