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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "radioboxbase.h"
14 #pragma implementation "radiobox.h"
17 //-------------------------------------------------------------------------------------
19 //-------------------------------------------------------------------------------------
21 #include "wx/wxprec.h"
25 #include "wx/arrstr.h"
27 #include "wx/radiobox.h"
28 #include "wx/radiobut.h"
29 #include "wx/mac/uma.h"
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
35 //-------------------------------------------------------------------------------------
37 //-------------------------------------------------------------------------------------
38 // Default constructor
39 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
40 EVT_RADIOBUTTON( -1 , wxRadioBox::OnRadioButton
)
43 void wxRadioBox::OnRadioButton( wxCommandEvent
&outer
)
45 if ( outer
.IsChecked() )
47 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
48 int i
= GetSelection() ;
50 event
.SetString( GetString( i
) );
51 event
.SetEventObject( this );
52 ProcessCommand(event
);
56 wxRadioBox::wxRadioBox()
61 m_radioButtonCycle
= NULL
;
64 //-------------------------------------------------------------------------------------
65 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
66 // const wxSize&, int, const wxString[], int, long,
67 // const wxValidator&, const wxString&)
68 //-------------------------------------------------------------------------------------
69 // Contructor, creating and showing a radiobox
74 //-------------------------------------------------------------------------------------
76 //-------------------------------------------------------------------------------------
77 // Destructor, destroying the radiobox item
79 wxRadioBox::~wxRadioBox()
81 m_isBeingDeleted
= TRUE
;
83 wxRadioButton
*next
,*current
;
85 current
=m_radioButtonCycle
->NextInCycle();
86 next
=current
->NextInCycle();
87 while (current
!=m_radioButtonCycle
) {
90 next
=current
->NextInCycle();
95 //-------------------------------------------------------------------------------------
97 //-------------------------------------------------------------------------------------
98 // Create the radiobox for two-step construction
100 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
101 const wxPoint
& pos
, const wxSize
& size
,
102 const wxArrayString
& choices
,
103 int majorDim
, long style
,
104 const wxValidator
& val
, const wxString
& name
)
106 wxCArrayString
chs(choices
);
108 return Create(parent
, id
, label
, pos
, size
, chs
.GetCount(),
109 chs
.GetStrings(), majorDim
, style
, val
, name
);
112 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
113 const wxPoint
& pos
, const wxSize
& size
,
114 int n
, const wxString choices
[],
115 int majorDim
, long style
,
116 const wxValidator
& val
, const wxString
& name
)
118 m_macIsUserPane
= FALSE
;
120 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, val
, name
) )
126 m_noRowsOrCols
= majorDim
;
127 m_radioButtonCycle
= NULL
;
132 m_majorDim
= majorDim
;
137 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
138 if( bounds
.right
<= bounds
.left
)
139 bounds
.right
= bounds
.left
+ 100 ;
140 if ( bounds
.bottom
<= bounds
.top
)
141 bounds
.bottom
= bounds
.top
+ 100 ;
143 m_peer
= new wxMacControl() ;
145 verify_noerr(CreateGroupBoxControl(MAC_WXHWND(parent
->MacGetTopLevelWindowRef()),&bounds
, CFSTR("") ,
146 true /*primary*/ , m_peer
->GetControlRefAddr() ) ) ;
148 for (i
= 0; i
< n
; i
++)
150 wxRadioButton
*radBtn
= new wxRadioButton
154 wxStripMenuCodes(choices
[i
]),
157 i
== 0 ? wxRB_GROUP
: 0
160 m_radioButtonCycle
= radBtn
;
161 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
165 MacPostControlCreate(pos
,size
) ;
171 //-------------------------------------------------------------------------------------
173 //-------------------------------------------------------------------------------------
174 // Enables or disables the entire radiobox
176 bool wxRadioBox::Enable(bool enable
)
179 wxRadioButton
*current
;
181 if (!wxControl::Enable(enable
))
184 current
= m_radioButtonCycle
;
185 for (i
= 0; i
< m_noItems
; i
++) {
186 current
->Enable(enable
);
187 current
= current
->NextInCycle();
192 //-------------------------------------------------------------------------------------
193 // ¥ Enable(int, bool)
194 //-------------------------------------------------------------------------------------
195 // Enables or disables an given button
197 void wxRadioBox::Enable(int item
, bool enable
)
200 wxRadioButton
*current
;
202 if ((item
< 0) || (item
>= m_noItems
))
206 current
= m_radioButtonCycle
;
209 current
= current
->NextInCycle();
211 current
->Enable(enable
);
214 //-------------------------------------------------------------------------------------
216 //-------------------------------------------------------------------------------------
217 // Returns the radiobox label
219 wxString
wxRadioBox::GetLabel() const
221 return wxControl::GetLabel();
224 //-------------------------------------------------------------------------------------
226 //-------------------------------------------------------------------------------------
227 // Returns the label for the given button
229 wxString
wxRadioBox::GetString(int item
) const
232 wxRadioButton
*current
;
234 if ((item
< 0) || (item
>= m_noItems
))
235 return wxEmptyString
;
238 current
= m_radioButtonCycle
;
241 current
= current
->NextInCycle();
243 return current
->GetLabel();
246 //-------------------------------------------------------------------------------------
248 //-------------------------------------------------------------------------------------
249 // Returns the zero-based position of the selected button
251 int wxRadioBox::GetSelection() const
254 wxRadioButton
*current
;
257 current
=m_radioButtonCycle
;
258 while (!current
->GetValue()) {
260 current
=current
->NextInCycle();
266 //-------------------------------------------------------------------------------------
268 //-------------------------------------------------------------------------------------
269 // Returns the number of buttons in the radiobox
274 //-------------------------------------------------------------------------------------
275 // ¥ SetLabel(const wxString&)
276 //-------------------------------------------------------------------------------------
277 // Sets the radiobox label
279 void wxRadioBox::SetLabel(const wxString
& label
)
281 return wxControl::SetLabel(label
);
284 //-------------------------------------------------------------------------------------
285 // ¥ SetLabel(int, const wxString&)
286 //-------------------------------------------------------------------------------------
287 // Sets the label of a given button
289 void wxRadioBox::SetString(int item
,const wxString
& label
)
292 wxRadioButton
*current
;
294 if ((item
< 0) || (item
>= m_noItems
))
297 current
=m_radioButtonCycle
;
300 current
=current
->NextInCycle();
302 return current
->SetLabel(label
);
305 //-------------------------------------------------------------------------------------
307 //-------------------------------------------------------------------------------------
308 // Sets a button by passing the desired position. This does not cause
309 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
311 void wxRadioBox::SetSelection(int item
)
314 wxRadioButton
*current
;
316 if ((item
< 0) || (item
>= m_noItems
))
319 current
=m_radioButtonCycle
;
322 current
=current
->NextInCycle();
324 current
->SetValue(true);
328 //-------------------------------------------------------------------------------------
330 //-------------------------------------------------------------------------------------
331 // Shows or hides the entire radiobox
333 bool wxRadioBox::Show(bool show
)
336 wxRadioButton
*current
;
338 wxControl::Show(show
);
340 current
=m_radioButtonCycle
;
341 for (i
=0;i
<m_noItems
;i
++) {
343 current
=current
->NextInCycle();
348 //-------------------------------------------------------------------------------------
350 //-------------------------------------------------------------------------------------
351 // Shows or hides the given button
353 void wxRadioBox::Show(int item
, bool show
)
356 wxRadioButton
*current
;
358 if ((item
< 0) || (item
>= m_noItems
))
361 current
=m_radioButtonCycle
;
364 current
=current
->NextInCycle();
369 //-------------------------------------------------------------------------------------
371 //-------------------------------------------------------------------------------------
372 // Simulates the effect of the user issuing a command to the item
374 void wxRadioBox::Command (wxCommandEvent
& event
)
376 SetSelection (event
.GetInt());
377 ProcessCommand (event
);
380 //-------------------------------------------------------------------------------------
382 //-------------------------------------------------------------------------------------
383 // Sets the selected button to receive keyboard input
385 void wxRadioBox::SetFocus()
388 wxRadioButton
*current
;
391 current
=m_radioButtonCycle
;
392 while (!current
->GetValue()) {
394 current
=current
->NextInCycle();
400 //-------------------------------------------------------------------------------------
402 //-------------------------------------------------------------------------------------
403 // Simulates the effect of the user issuing a command to the item
405 #define RADIO_SIZE 20
407 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
410 wxRadioButton
*current
;
412 // define the position
414 int x_current
, y_current
;
415 int x_offset
,y_offset
;
416 int widthOld
, heightOld
;
417 GetSize(&widthOld
, &heightOld
);
421 GetPosition(&x_current
, &y_current
);
422 if ((x
== -1) && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
423 x_offset
= x_current
;
424 if ((y
== -1)&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
425 y_offset
= y_current
;
429 int charWidth
,charHeight
;
430 int maxWidth
,maxHeight
;
431 int eachWidth
[128],eachHeight
[128];
432 int totWidth
,totHeight
;
434 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth
, &charHeight
);
440 for (i
= 0 ; i
< m_noItems
; i
++)
442 GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
]);
443 eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
);
444 eachHeight
[i
] = (int)((3*eachHeight
[i
])/2);
445 if (maxWidth
<eachWidth
[i
]) maxWidth
= eachWidth
[i
];
446 if (maxHeight
<eachHeight
[i
]) maxHeight
= eachHeight
[i
];
449 totHeight
= GetRowCount() * ( maxHeight
) ;
450 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
452 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
454 // only change our width/height if asked for
457 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
465 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
471 wxControl::DoSetSize(x_offset
,y_offset
,width
,height
,wxSIZE_AUTO
);
473 // arrange radiobuttons
484 current
=m_radioButtonCycle
;
485 for ( i
= 0 ; i
< m_noItems
; i
++)
487 if (i
&&((i%m_majorDim
)==0)) // not to do for the zero button!
489 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
491 x_offset
+= maxWidth
+ charWidth
;
497 y_offset
+= maxHeight
; /*+ charHeight/2;*/
501 current
->SetSize(x_offset
,y_offset
,eachWidth
[i
],eachHeight
[i
]);
502 current
=current
->NextInCycle();
504 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
505 y_offset
+= maxHeight
; /*+ charHeight/2;*/
507 x_offset
+= maxWidth
+ charWidth
;
511 wxSize
wxRadioBox::DoGetBestSize() const
513 int charWidth
, charHeight
;
514 int maxWidth
, maxHeight
;
515 int eachWidth
, eachHeight
;
516 int totWidth
, totHeight
;
518 wxFont font
= /*GetParent()->*/GetFont();
519 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
520 &charWidth
, &charHeight
, NULL
, NULL
, &font
);
527 for (int i
= 0 ; i
< m_noItems
; i
++)
529 GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
,NULL
, NULL
, &font
);
530 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) ;
531 eachHeight
= (int)((3 * eachHeight
) / 2);
532 if (maxWidth
< eachWidth
) maxWidth
= eachWidth
;
533 if (maxHeight
< eachHeight
) maxHeight
= eachHeight
;
536 totHeight
= GetRowCount() * (maxHeight
) ;
537 totWidth
= GetColumnCount() * (maxWidth
+ charWidth
) ;
539 wxSize sz
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight
) ) ;
543 // handle radio box title as well
544 GetTextExtent(GetTitle(), &eachWidth
, NULL
);
545 eachWidth
= (int)(eachWidth
+ RADIO_SIZE
) + 3 * charWidth
;
546 if (totWidth
< eachWidth
)
547 totWidth
= eachWidth
;
549 return wxSize(totWidth
, totHeight
);
551 //-------------------------------------------------------------------------------------
553 //-------------------------------------------------------------------------------------
554 // return the number of buttons in the vertical direction
556 int wxRadioBox::GetRowCount() const
558 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
564 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
568 //-------------------------------------------------------------------------------------
570 //-------------------------------------------------------------------------------------
571 // return the number of buttons in the horizontal direction
573 int wxRadioBox::GetColumnCount() const
575 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
577 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;