1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/mac/carbon/radiobox.cpp 
   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 #include "wx/wxprec.h" 
  16 #include "wx/radiobox.h" 
  19     #include "wx/radiobut.h" 
  20     #include "wx/arrstr.h" 
  23 #include "wx/mac/uma.h" 
  25 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
) 
  28 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
) 
  29     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() 
  50     m_radioButtonCycle 
= NULL
; 
  53 wxRadioBox::~wxRadioBox() 
  55     m_isBeingDeleted 
= true; 
  57     wxRadioButton 
*next
, *current
; 
  59     current 
= m_radioButtonCycle
->NextInCycle(); 
  62         while (current 
!= m_radioButtonCycle
) 
  64             next 
= current
->NextInCycle(); 
  74 // Create the radiobox for two-step construction 
  76 bool wxRadioBox::Create( wxWindow 
*parent
, 
  77     wxWindowID id
, const wxString
& label
, 
  78     const wxPoint
& pos
, const wxSize
& size
, 
  79     const wxArrayString
& choices
, 
  80     int majorDim
, long style
, 
  81     const wxValidator
& val
, const wxString
& name 
) 
  83     wxCArrayString 
chs(choices
); 
  86         parent
, id
, label
, pos
, size
, chs
.GetCount(), 
  87         chs
.GetStrings(), majorDim
, style
, val
, name
); 
  90 bool wxRadioBox::Create( wxWindow 
*parent
, 
  91     wxWindowID id
, const wxString
& label
, 
  92     const wxPoint
& pos
, const wxSize
& size
, 
  93     int n
, const wxString choices
[], 
  94     int majorDim
, long style
, 
  95     const wxValidator
& val
, const wxString
& name 
) 
  97     m_macIsUserPane 
= false ; 
  99     if ( !wxControl::Create( parent
, id
, pos
, size
, style
, val
, name 
) ) 
 104     m_noItems 
= (unsigned int)n
; 
 105     m_noRowsOrCols 
= majorDim
; 
 106     m_radioButtonCycle 
= NULL
; 
 108     SetMajorDim( majorDim 
== 0 ? n 
: majorDim
, style 
); 
 112     Rect bounds 
= wxMacGetBoundsForControl( this, pos
, size 
); 
 113     if ( bounds
.right 
<= bounds
.left 
) 
 114         bounds
.right 
= bounds
.left 
+ 100; 
 115     if ( bounds
.bottom 
<= bounds
.top 
) 
 116         bounds
.bottom 
= bounds
.top 
+ 100; 
 118     m_peer 
= new wxMacControl( this ); 
 120     OSStatus err 
= CreateGroupBoxControl( 
 121         MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), 
 122         &bounds
, CFSTR("") , true /*primary*/, 
 123         m_peer
->GetControlRefAddr() ); 
 126     for (i 
= 0; i 
< n
; i
++) 
 128         wxRadioButton 
*radBtn 
= new wxRadioButton( 
 131             GetLabelText(choices
[i
]), 
 132             wxPoint( 5, 20 * i 
+ 10 ), 
 134             i 
== 0 ? wxRB_GROUP 
: 0 ); 
 137             m_radioButtonCycle 
= radBtn
; 
 138 //        m_radioButtonCycle = radBtn->AddInCycle( m_radioButtonCycle ); 
 142     MacPostControlCreate( pos
, size 
); 
 147 // Enables or disables the entire radiobox 
 149 bool wxRadioBox::Enable(bool enable
) 
 151     wxRadioButton 
*current
; 
 153     if (!wxControl::Enable( enable 
)) 
 156     current 
= m_radioButtonCycle
; 
 157     for (unsigned int i 
= 0; i 
< m_noItems
; i
++) 
 159         current
->Enable( enable 
); 
 160         current 
= current
->NextInCycle(); 
 166 // Enables or disables an given button 
 168 bool wxRadioBox::Enable(unsigned int item
, bool enable
) 
 170     if (!IsValid( item 
)) 
 174     wxRadioButton 
*current 
= m_radioButtonCycle
; 
 178         current 
= current
->NextInCycle(); 
 181     return current
->Enable( enable 
); 
 184 // Returns the radiobox label 
 186 wxString 
wxRadioBox::GetLabel() const 
 188     return wxControl::GetLabel(); 
 191 // Returns the label for the given button 
 193 wxString 
wxRadioBox::GetString(unsigned int item
) const 
 195     wxRadioButton 
*current
; 
 197     if (!IsValid( item 
)) 
 198         return wxEmptyString
; 
 201     current 
= m_radioButtonCycle
; 
 205         current 
= current
->NextInCycle(); 
 208     return current
->GetLabel(); 
 211 // Returns the zero-based position of the selected button 
 213 int wxRadioBox::GetSelection() const 
 216     wxRadioButton 
*current
; 
 219     current 
= m_radioButtonCycle
; 
 220     while (!current
->GetValue()) 
 223         current 
= current
->NextInCycle(); 
 229 // Sets the radiobox label 
 231 void wxRadioBox::SetLabel(const wxString
& label
) 
 233     return wxControl::SetLabel( label 
); 
 236 // Sets the label of a given button 
 238 void wxRadioBox::SetString(unsigned int item
,const wxString
& label
) 
 240     if (!IsValid( item 
)) 
 244     wxRadioButton 
*current 
= m_radioButtonCycle
; 
 248         current 
= current
->NextInCycle(); 
 251     return current
->SetLabel( label 
); 
 254 // Sets a button by passing the desired position. This does not cause 
 255 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted 
 257 void wxRadioBox::SetSelection(int item
) 
 260     wxRadioButton 
*current
; 
 262     if (!IsValid( item 
)) 
 266     current 
= m_radioButtonCycle
; 
 270         current 
= current
->NextInCycle(); 
 273     current
->SetValue( true ); 
 276 // Shows or hides the entire radiobox 
 278 bool wxRadioBox::Show(bool show
) 
 280     wxRadioButton 
*current
; 
 282     current 
= m_radioButtonCycle
; 
 283     for (unsigned int i
=0; i
<m_noItems
; i
++) 
 285         current
->Show( show 
); 
 286         current 
= current
->NextInCycle(); 
 289     wxControl::Show( show 
); 
 294 // Shows or hides the given button 
 296 bool wxRadioBox::Show(unsigned int item
, bool show
) 
 298     if (!IsValid( item 
)) 
 302     wxRadioButton 
*current 
= m_radioButtonCycle
; 
 306         current 
= current
->NextInCycle(); 
 309     return current
->Show( show 
); 
 312 // Simulates the effect of the user issuing a command to the item 
 314 void wxRadioBox::Command( wxCommandEvent
& event 
) 
 316     SetSelection( event
.GetInt() ); 
 317     ProcessCommand( event 
); 
 320 // Sets the selected button to receive keyboard input 
 322 void wxRadioBox::SetFocus() 
 324     wxRadioButton 
*current
; 
 326     current 
= m_radioButtonCycle
; 
 327     while (!current
->GetValue()) 
 329         current 
= current
->NextInCycle(); 
 335 // Simulates the effect of the user issuing a command to the item 
 337 #define RADIO_SIZE 20 
 339 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
) 
 342     wxRadioButton 
*current
; 
 344     // define the position 
 346     int x_current
, y_current
; 
 347     int x_offset
, y_offset
; 
 348     int widthOld
, heightOld
; 
 350     GetSize( &widthOld
, &heightOld 
); 
 351     GetPosition( &x_current
, &y_current 
); 
 355     if (!(sizeFlags 
& wxSIZE_ALLOW_MINUS_ONE
)) 
 357         if (x 
== wxDefaultCoord
) 
 358             x_offset 
= x_current
; 
 359         if (y 
== wxDefaultCoord
) 
 360             y_offset 
= y_current
; 
 364     int charWidth
, charHeight
; 
 365     int maxWidth
, maxHeight
; 
 366     int eachWidth
[128], eachHeight
[128]; 
 367     int totWidth
, totHeight
; 
 370         wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 
 371         &charWidth
, &charHeight 
); 
 377     for (unsigned int i 
= 0 ; i 
< m_noItems
; i
++) 
 379         GetTextExtent(GetString(i
), &eachWidth
[i
], &eachHeight
[i
] ); 
 380         eachWidth
[i
] = (int)(eachWidth
[i
] + RADIO_SIZE
); 
 381         eachHeight
[i
] = (int)((3 * eachHeight
[i
]) / 2); 
 383         if (maxWidth 
< eachWidth
[i
]) 
 384             maxWidth 
= eachWidth
[i
]; 
 385         if (maxHeight 
< eachHeight
[i
]) 
 386             maxHeight 
= eachHeight
[i
]; 
 389     totHeight 
= GetRowCount() * maxHeight
; 
 390     totWidth  
= GetColumnCount() * (maxWidth 
+ charWidth
); 
 392     wxSize sz 
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight 
) ) ; 
 394     // change the width / height only when specified 
 395     if ( width 
== wxDefaultCoord 
) 
 397         if ( sizeFlags 
& wxSIZE_AUTO_WIDTH 
) 
 403     if ( height 
== wxDefaultCoord 
) 
 405         if ( sizeFlags 
& wxSIZE_AUTO_HEIGHT 
) 
 411     wxControl::DoSetSize( x_offset
, y_offset
, width
, height
, wxSIZE_AUTO 
); 
 413     // arrange radio buttons 
 414     int x_start
, y_start
; 
 422     current 
= m_radioButtonCycle
; 
 423     for (i 
= 0 ; i 
< (int)m_noItems
; i
++) 
 425         // not to do for the zero button! 
 426         if ((i 
> 0) && ((i 
% GetMajorDim()) == 0)) 
 428             if (m_windowStyle 
& wxRA_SPECIFY_ROWS
) 
 430                 x_offset 
+= maxWidth 
+ charWidth
; 
 436                 y_offset 
+= maxHeight 
; //+ charHeight / 2 
 440         current
->SetSize( x_offset
, y_offset
, eachWidth
[i
], eachHeight
[i
]); 
 441         current 
= current
->NextInCycle(); 
 443         if (m_windowStyle 
& wxRA_SPECIFY_ROWS
) 
 444             y_offset 
+= maxHeight 
; // + charHeight / 2 
 446             x_offset 
+= maxWidth 
+ charWidth
; 
 450 wxSize 
wxRadioBox::DoGetBestSize() const 
 452     int charWidth
, charHeight
; 
 453     int maxWidth
, maxHeight
; 
 454     int eachWidth
, eachHeight
; 
 455     int totWidth
, totHeight
; 
 457     wxFont font 
= GetFont(); // GetParent()->GetFont() 
 459         wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 
 460         &charWidth
, &charHeight
, NULL
, NULL
, &font 
); 
 467     for (unsigned int i 
= 0 ; i 
< m_noItems
; i
++) 
 469         GetTextExtent(GetString(i
), &eachWidth
, &eachHeight
, NULL
, NULL
, &font 
); 
 470         eachWidth  
= (int)(eachWidth 
+ RADIO_SIZE
); 
 471         eachHeight 
= (int)((3 * eachHeight
) / 2); 
 472         if (maxWidth 
< eachWidth
) 
 473             maxWidth 
= eachWidth
; 
 474         if (maxHeight 
< eachHeight
) 
 475             maxHeight 
= eachHeight
; 
 478     totHeight 
= GetRowCount() * maxHeight
; 
 479     totWidth  
= GetColumnCount() * (maxWidth 
+ charWidth
); 
 481     wxSize sz 
= DoGetSizeFromClientSize( wxSize( totWidth
, totHeight 
) ); 
 485     // handle radio box title as well 
 486     GetTextExtent( GetLabel(), &eachWidth
, NULL 
); 
 487     eachWidth  
= (int)(eachWidth 
+ RADIO_SIZE
) +  3 * charWidth
; 
 488     if (totWidth 
< eachWidth
) 
 489         totWidth 
= eachWidth
; 
 491     return wxSize( totWidth
, totHeight 
); 
 494 #endif // wxUSE_RADIOBOX