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 //-------------------------------------------------------------------------------------
23 #include "wx/radiobox.h"
24 #include "wx/radiobut.h"
25 #include "wx/mac/uma.h"
27 #if !USE_SHARED_LIBRARY
28 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
31 //-------------------------------------------------------------------------------------
33 //-------------------------------------------------------------------------------------
34 // Default constructor
35 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
36 EVT_RADIOBUTTON( -1 , wxRadioBox::OnRadioButton
)
39 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
41 if ( outer
.IsChecked() )
43 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
44 int i
= GetSelection() ;
46 event
.SetString( GetString( i
) );
47 event
.SetEventObject( this );
48 ProcessCommand(event
);
52 wxRadioBox::wxRadioBox()
57 m_radioButtonCycle
= NULL
;
60 //-------------------------------------------------------------------------------------
61 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
62 // const wxSize&, int, const wxString[], int, long,
63 // const wxValidator&, const wxString&)
64 //-------------------------------------------------------------------------------------
65 // Contructor, creating and showing a radiobox
70 //-------------------------------------------------------------------------------------
72 //-------------------------------------------------------------------------------------
73 // Destructor, destroying the radiobox item
75 wxRadioBox::~wxRadioBox()
77 m_isBeingDeleted
= TRUE
;
79 wxRadioButton
*next
,*current
;
81 current
=m_radioButtonCycle
->NextInCycle();
82 next
=current
->NextInCycle();
83 while (current
!=m_radioButtonCycle
) {
86 next
=current
->NextInCycle();
91 //-------------------------------------------------------------------------------------
93 //-------------------------------------------------------------------------------------
94 // Create the radiobox for two-step construction
96 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
97 const wxPoint
& pos
, const wxSize
& size
,
98 int n
, const wxString choices
[],
99 int majorDim
, long style
,
100 const wxValidator
& val
, const wxString
& name
)
102 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
108 m_noRowsOrCols
= majorDim
;
109 m_radioButtonCycle
= NULL
;
114 m_majorDim
= majorDim
;
120 MacPreControlCreate( parent
, id
, wxStripMenuCodes(label
) , pos
, size
,style
, val
, name
, &bounds
, title
) ;
122 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false , 0 , 0 , 1,
123 kControlGroupBoxTextTitleProc
, (long) this ) ;
125 for (i
= 0; i
< n
; i
++)
127 wxRadioButton
*radBtn
= new wxRadioButton
131 wxStripMenuCodes(choices
[i
]),
134 i
== 0 ? wxRB_GROUP
: 0
137 m_radioButtonCycle
= radBtn
;
138 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
142 MacPostControlCreate() ;
148 //-------------------------------------------------------------------------------------
150 //-------------------------------------------------------------------------------------
151 // Enables or disables the entire radiobox
153 bool wxRadioBox::Enable(bool enable
)
156 wxRadioButton
*current
;
158 if (!wxControl::Enable(enable
))
161 current
= m_radioButtonCycle
;
162 for (i
= 0; i
< m_noItems
; i
++) {
163 current
->Enable(enable
);
164 current
= current
->NextInCycle();
169 //-------------------------------------------------------------------------------------
170 // ¥ Enable(int, bool)
171 //-------------------------------------------------------------------------------------
172 // Enables or disables an given button
174 void wxRadioBox::Enable(int item
, bool enable
)
177 wxRadioButton
*current
;
179 if ((item
< 0) || (item
>= m_noItems
))
183 current
= m_radioButtonCycle
;
186 current
= current
->NextInCycle();
188 current
->Enable(enable
);
191 //-------------------------------------------------------------------------------------
193 //-------------------------------------------------------------------------------------
194 // Returns the radiobox label
196 wxString
wxRadioBox::GetLabel() const
198 return wxControl::GetLabel();
201 //-------------------------------------------------------------------------------------
203 //-------------------------------------------------------------------------------------
204 // Returns the label for the given button
206 wxString
wxRadioBox::GetString(int item
) const
209 wxRadioButton
*current
;
211 if ((item
< 0) || (item
>= m_noItems
))
212 return wxEmptyString
;
215 current
= m_radioButtonCycle
;
218 current
= current
->NextInCycle();
220 return current
->GetLabel();
223 //-------------------------------------------------------------------------------------
225 //-------------------------------------------------------------------------------------
226 // Returns the zero-based position of the selected button
228 int wxRadioBox::GetSelection() const
231 wxRadioButton
*current
;
234 current
=m_radioButtonCycle
;
235 while (!current
->GetValue()) {
237 current
=current
->NextInCycle();
243 //-------------------------------------------------------------------------------------
245 //-------------------------------------------------------------------------------------
246 // Returns the number of buttons in the radiobox
251 //-------------------------------------------------------------------------------------
252 // ¥ SetLabel(const wxString&)
253 //-------------------------------------------------------------------------------------
254 // Sets the radiobox label
256 void wxRadioBox::SetLabel(const wxString
& label
)
258 return wxControl::SetLabel(label
);
261 //-------------------------------------------------------------------------------------
262 // ¥ SetLabel(int, const wxString&)
263 //-------------------------------------------------------------------------------------
264 // Sets the label of a given button
266 void wxRadioBox::SetString(int item
,const wxString
& label
)
269 wxRadioButton
*current
;
271 if ((item
< 0) || (item
>= m_noItems
))
274 current
=m_radioButtonCycle
;
277 current
=current
->NextInCycle();
279 return current
->SetLabel(label
);
282 //-------------------------------------------------------------------------------------
284 //-------------------------------------------------------------------------------------
285 // Sets a button by passing the desired position. This does not cause
286 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
288 void wxRadioBox::SetSelection(int item
)
291 wxRadioButton
*current
;
293 if ((item
< 0) || (item
>= m_noItems
))
296 current
=m_radioButtonCycle
;
299 current
=current
->NextInCycle();
301 current
->SetValue(true);
305 //-------------------------------------------------------------------------------------
307 //-------------------------------------------------------------------------------------
308 // Shows or hides the entire radiobox
310 bool wxRadioBox::Show(bool show
)
313 wxRadioButton
*current
;
315 wxControl::Show(show
);
317 current
=m_radioButtonCycle
;
318 for (i
=0;i
<m_noItems
;i
++) {
320 current
=current
->NextInCycle();
325 //-------------------------------------------------------------------------------------
327 //-------------------------------------------------------------------------------------
328 // Shows or hides the given button
330 void wxRadioBox::Show(int item
, bool show
)
333 wxRadioButton
*current
;
335 if ((item
< 0) || (item
>= m_noItems
))
338 current
=m_radioButtonCycle
;
341 current
=current
->NextInCycle();
346 //-------------------------------------------------------------------------------------
348 //-------------------------------------------------------------------------------------
349 // Simulates the effect of the user issuing a command to the item
351 void wxRadioBox::Command (wxCommandEvent
& event
)
353 SetSelection (event
.GetInt());
354 ProcessCommand (event
);
357 //-------------------------------------------------------------------------------------
359 //-------------------------------------------------------------------------------------
360 // Sets the selected button to receive keyboard input
362 void wxRadioBox::SetFocus()
365 wxRadioButton
*current
;
368 current
=m_radioButtonCycle
;
369 while (!current
->GetValue()) {
371 current
=current
->NextInCycle();
377 //-------------------------------------------------------------------------------------
379 //-------------------------------------------------------------------------------------
380 // Simulates the effect of the user issuing a command to the item
382 #define RADIO_SIZE 20
384 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
387 wxRadioButton
*current
;
389 // define the position
391 int x_current
, y_current
;
392 int x_offset
,y_offset
;
393 int widthOld
, heightOld
;
394 GetSize(&widthOld
, &heightOld
);
398 GetPosition(&x_current
, &y_current
);
399 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
400 x_offset
= x_current
;
401 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
402 y_offset
= y_current
;
406 int charWidth
,charHeight
;
407 int maxWidth
,maxHeight
;
408 int eachWidth
[128],eachHeight
[128];
409 int totWidth
,totHeight
;
411 SetFont(GetParent()->GetFont());
412 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
417 for (i
= 0 ; i
< m_noItems
; i
++)
419 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
420 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
421 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
422 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
423 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
426 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
427 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
429 // only change our width/height if asked for
432 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
440 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
446 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
448 // arrange radiobuttons
455 if ( UMAGetSystemVersion() >= 0x1030 )
457 //need to add a few more pixels for the top border on panther
458 y_start
= y_start
+ 5; //how many exactly should this be to meet the HIG?
463 current
=m_radioButtonCycle
;
464 for ( i
= 0 ; i
< m_noItems
; i
++)
466 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
468 if (m_windowStyle
& wxRA_VERTICAL
)
470 x_offset
+= maxWidth
+ charWidth
;
476 y_offset
+= maxHeight
; /*+ charHeight/2;*/
480 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
481 current
=current
->NextInCycle();
483 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
484 y_offset
+= maxHeight
; /*+ charHeight/2;*/
486 x_offset
+= maxWidth
+ charWidth
;
490 wxSize
wxRadioBox::DoGetBestSize() const
492 int charWidth
, charHeight
;
493 int maxWidth
, maxHeight
;
494 int eachWidth
, eachHeight
;
495 int totWidth
, totHeight
;
497 wxFont font
= GetParent()->GetFont();
498 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
499 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
505 for (int i
= 0 ; i
< m_noItems
; i
++)
507 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
);
508 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
509 eachHeight
= (int)((3 * eachHeight
) / 2);
510 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
511 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
514 totHeight
= GetRowCount() * (maxHeight
+ charHeight
/2) + charHeight
;
515 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) + charWidth
;
517 if ( UMAGetSystemVersion() >= 0x1030 )
519 //need to add a few more pixels for the static boxborder on panther
520 totHeight
= totHeight
+ 10; //how many exactly should this be to meet the HIG?
522 // handle radio box title as well
523 GetTextExtent(GetTitle(), &eachWidth
, NULL
);
524 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
525 if (totWidth
< eachWidth
)
526 totWidth
= eachWidth
;
528 return wxSize(totWidth
, totHeight
);
530 //-------------------------------------------------------------------------------------
532 //-------------------------------------------------------------------------------------
533 // return the number of buttons in the vertical direction
535 int wxRadioBox::GetRowCount() const
537 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
543 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
547 //-------------------------------------------------------------------------------------
549 //-------------------------------------------------------------------------------------
550 // return the number of buttons in the horizontal direction
552 int wxRadioBox::GetColumnCount() const
554 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
556 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;