]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/radiobox.cpp
Line-up interfaces to use size_t for GetCount()s (and count related api).
[wxWidgets.git] / src / mac / carbon / radiobox.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
fb5246be 2// Name: src/mac/carbon/radiobox.cpp
e9576ca5 3// Purpose: wxRadioBox
a31a5f85 4// Author: Stefan Csomor
1395ff56 5// Modified by: JS Lair (99/11/15) first implementation
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
8228b893 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
eb22f2a6 12//-------------------------------------------------------------------------------------
e40298d5 13// headers
eb22f2a6
GD
14//-------------------------------------------------------------------------------------
15
3d1a4878
SC
16#include "wx/wxprec.h"
17
179e085f
RN
18#if wxUSE_RADIOBOX
19
584ad2a3 20#include "wx/arrstr.h"
e9576ca5 21#include "wx/radiobox.h"
03e11df5
GD
22#include "wx/radiobut.h"
23#include "wx/mac/uma.h"
e9576ca5 24
e9576ca5 25IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
e9576ca5 26
43524b15 27
cf1a9b45 28BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
6cb0bfdf 29EVT_RADIOBUTTON( wxID_ANY , wxRadioBox::OnRadioButton )
cf1a9b45
SC
30END_EVENT_TABLE()
31
43524b15 32
cf1a9b45
SC
33void wxRadioBox::OnRadioButton( wxCommandEvent &outer )
34{
e40298d5
JS
35 if ( outer.IsChecked() )
36 {
43524b15 37 wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId );
e40298d5
JS
38 int i = GetSelection() ;
39 event.SetInt( i );
40 event.SetString( GetString( i ) );
41 event.SetEventObject( this );
43524b15 42 ProcessCommand( event );
e40298d5 43 }
cf1a9b45 44}
1395ff56 45
e9576ca5
SC
46wxRadioBox::wxRadioBox()
47{
e9576ca5
SC
48 m_noItems = 0;
49 m_noRowsOrCols = 0;
1395ff56
UJ
50 m_radioButtonCycle = NULL;
51}
52
1395ff56
UJ
53wxRadioBox::~wxRadioBox()
54{
1a87edf2 55 m_isBeingDeleted = true;
140f2012 56
43524b15
DS
57 wxRadioButton *next, *current;
58
59 current = m_radioButtonCycle->NextInCycle();
60 if (current != NULL)
61 {
62 while (current != m_radioButtonCycle)
63 {
64 next = current->NextInCycle();
65 delete current;
66
67 current = next;
68 }
1a87edf2 69
e40298d5 70 delete current;
e40298d5 71 }
e9576ca5
SC
72}
73
1395ff56
UJ
74// Create the radiobox for two-step construction
75
43524b15
DS
76bool 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 )
584ad2a3
MB
82{
83 wxCArrayString chs(choices);
84
43524b15
DS
85 return Create(
86 parent, id, label, pos, size, chs.GetCount(),
87 chs.GetStrings(), majorDim, style, val, name);
584ad2a3
MB
88}
89
43524b15
DS
90bool 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 )
e9576ca5 96{
1a87edf2
WS
97 m_macIsUserPane = false ;
98
43524b15 99 if ( !wxControl::Create( parent, id, pos, size, style, val, name ) )
b45ed7a2
VZ
100 return false;
101
1395ff56 102 int i;
1a87edf2 103
8228b893 104 m_noItems = (size_t)n;
e9576ca5 105 m_noRowsOrCols = majorDim;
1395ff56 106 m_radioButtonCycle = NULL;
1a87edf2 107
43524b15 108 SetMajorDim( majorDim == 0 ? n : majorDim, style );
1a87edf2 109
facd6764
SC
110 m_label = label ;
111
43524b15
DS
112 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
113 if ( bounds.right <= bounds.left )
114 bounds.right = bounds.left + 100;
facd6764 115 if ( bounds.bottom <= bounds.top )
43524b15 116 bounds.bottom = bounds.top + 100;
1a87edf2 117
43524b15 118 m_peer = new wxMacControl( this );
1a87edf2 119
43524b15
DS
120 OSStatus err = CreateGroupBoxControl(
121 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
122 &bounds, CFSTR("") , true /*primary*/,
123 m_peer->GetControlRefAddr() );
124 verify_noerr( err );
1a87edf2 125
1395ff56
UJ
126 for (i = 0; i < n; i++)
127 {
43524b15
DS
128 wxRadioButton *radBtn = new wxRadioButton(
129 this,
130 wxID_ANY,
131 wxStripMenuCodes(choices[i]),
132 wxPoint( 5, 20 * i + 10 ),
133 wxDefaultSize,
134 i == 0 ? wxRB_GROUP : 0 );
135
0a67a93b 136 if ( i == 0 )
43524b15
DS
137 m_radioButtonCycle = radBtn;
138// m_radioButtonCycle = radBtn->AddInCycle( m_radioButtonCycle );
1395ff56 139 }
1a87edf2 140
43524b15
DS
141 SetSelection( 0 );
142 MacPostControlCreate( pos, size );
1a87edf2
WS
143
144 return true;
e9576ca5
SC
145}
146
1395ff56 147// Enables or disables the entire radiobox
43524b15 148//
5008c64c 149bool wxRadioBox::Enable(bool enable)
1395ff56 150{
1395ff56 151 wxRadioButton *current;
1a87edf2 152
43524b15 153 if (!wxControl::Enable( enable ))
e40298d5 154 return false;
1a87edf2 155
371fd015 156 current = m_radioButtonCycle;
8228b893 157 for (size_t i = 0; i < m_noItems; i++)
43524b15
DS
158 {
159 current->Enable( enable );
e40298d5 160 current = current->NextInCycle();
371fd015 161 }
43524b15 162
371fd015 163 return true;
1395ff56
UJ
164}
165
1395ff56 166// Enables or disables an given button
43524b15 167//
1a87edf2 168bool wxRadioBox::Enable(int item, bool enable)
e9576ca5 169{
371fd015 170 int i;
1395ff56 171 wxRadioButton *current;
1a87edf2 172
43524b15 173 if (!IsValid( item ))
1a87edf2
WS
174 return false;
175
371fd015
GD
176 i = 0;
177 current = m_radioButtonCycle;
43524b15
DS
178 while (i != item)
179 {
e40298d5
JS
180 i++;
181 current = current->NextInCycle();
371fd015 182 }
43524b15
DS
183
184 return current->Enable( enable );
e9576ca5
SC
185}
186
1395ff56 187// Returns the radiobox label
43524b15 188//
1395ff56 189wxString wxRadioBox::GetLabel() const
e9576ca5 190{
1395ff56
UJ
191 return wxControl::GetLabel();
192}
e9576ca5 193
1395ff56 194// Returns the label for the given button
43524b15 195//
90b959ae 196wxString wxRadioBox::GetString(int item) const
1395ff56 197{
371fd015 198 int i;
1395ff56 199 wxRadioButton *current;
1a87edf2 200
43524b15 201 if (!IsValid( item ))
427ff662 202 return wxEmptyString;
1a87edf2 203
371fd015
GD
204 i = 0;
205 current = m_radioButtonCycle;
43524b15
DS
206 while (i != item)
207 {
e40298d5
JS
208 i++;
209 current = current->NextInCycle();
371fd015 210 }
43524b15 211
1395ff56 212 return current->GetLabel();
e9576ca5
SC
213}
214
1395ff56 215// Returns the zero-based position of the selected button
43524b15 216//
e9576ca5
SC
217int wxRadioBox::GetSelection() const
218{
1395ff56
UJ
219 int i;
220 wxRadioButton *current;
1a87edf2 221
43524b15
DS
222 i = 0;
223 current = m_radioButtonCycle;
224 while (!current->GetValue())
225 {
e40298d5 226 i++;
43524b15 227 current = current->NextInCycle();
e40298d5 228 }
1a87edf2 229
1395ff56 230 return i;
e9576ca5
SC
231}
232
1395ff56 233// Sets the radiobox label
43524b15 234//
1395ff56 235void wxRadioBox::SetLabel(const wxString& label)
e9576ca5 236{
43524b15 237 return wxControl::SetLabel( label );
e9576ca5
SC
238}
239
1395ff56 240// Sets the label of a given button
43524b15 241//
90b959ae 242void wxRadioBox::SetString(int item,const wxString& label)
e9576ca5 243{
43524b15 244 int i;
1395ff56 245 wxRadioButton *current;
1a87edf2 246
43524b15 247 if (!IsValid( item ))
1395ff56 248 return;
43524b15
DS
249
250 i = 0;
251 current = m_radioButtonCycle;
252 while (i != item)
253 {
e40298d5 254 i++;
43524b15 255 current = current->NextInCycle();
e40298d5 256 }
43524b15
DS
257
258 return current->SetLabel( label );
e9576ca5
SC
259}
260
1a87edf2 261// Sets a button by passing the desired position. This does not cause
1395ff56 262// wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
43524b15 263//
1395ff56 264void wxRadioBox::SetSelection(int item)
e9576ca5 265{
1395ff56
UJ
266 int i;
267 wxRadioButton *current;
1a87edf2 268
43524b15 269 if (!IsValid( item ))
1395ff56 270 return;
43524b15
DS
271
272 i = 0;
273 current = m_radioButtonCycle;
274 while (i != item)
275 {
e40298d5 276 i++;
43524b15 277 current = current->NextInCycle();
e40298d5 278 }
1a87edf2 279
43524b15 280 current->SetValue( true );
e9576ca5
SC
281}
282
1a87edf2 283// Shows or hides the entire radiobox
43524b15 284//
e9576ca5
SC
285bool wxRadioBox::Show(bool show)
286{
1395ff56 287 wxRadioButton *current;
1a87edf2 288
43524b15 289 current = m_radioButtonCycle;
8228b893 290 for (size_t i=0; i<m_noItems; i++)
6cb0bfdf 291 {
43524b15
DS
292 current->Show( show );
293 current = current->NextInCycle();
e40298d5 294 }
43524b15
DS
295
296 wxControl::Show( show );
297
1395ff56 298 return true;
e9576ca5
SC
299}
300
1a87edf2 301// Shows or hides the given button
43524b15 302//
6cb0bfdf 303bool wxRadioBox::Show(int item, bool show)
e9576ca5 304{
6cb0bfdf 305 int i;
1395ff56 306 wxRadioButton *current;
1a87edf2 307
43524b15 308 if (!IsValid( item ))
6cb0bfdf 309 return false;
43524b15
DS
310
311 i = 0;
312 current = m_radioButtonCycle;
313 while (i != item)
314 {
e40298d5 315 i++;
43524b15 316 current = current->NextInCycle();
e40298d5 317 }
43524b15
DS
318
319 return current->Show( show );
e9576ca5
SC
320}
321
1395ff56 322// Simulates the effect of the user issuing a command to the item
43524b15
DS
323//
324void wxRadioBox::Command( wxCommandEvent& event )
e9576ca5 325{
43524b15
DS
326 SetSelection( event.GetInt() );
327 ProcessCommand( event );
e9576ca5
SC
328}
329
1395ff56 330// Sets the selected button to receive keyboard input
43524b15 331//
1395ff56 332void wxRadioBox::SetFocus()
e9576ca5 333{
1395ff56
UJ
334 int i;
335 wxRadioButton *current;
1a87edf2 336
43524b15
DS
337 i = 0;
338 current = m_radioButtonCycle;
339 while (!current->GetValue())
340 {
e40298d5 341 i++;
43524b15 342 current = current->NextInCycle();
e40298d5 343 }
43524b15 344
e40298d5 345 current->SetFocus();
e9576ca5
SC
346}
347
1395ff56 348// Simulates the effect of the user issuing a command to the item
43524b15 349//
baf34314 350#define RADIO_SIZE 20
1395ff56
UJ
351
352void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
e9576ca5 353{
e40298d5
JS
354 int i;
355 wxRadioButton *current;
1a87edf2 356
e40298d5 357 // define the position
1a87edf2 358
e40298d5 359 int x_current, y_current;
43524b15 360 int x_offset, y_offset;
8208e181 361 int widthOld, heightOld;
43524b15
DS
362
363 GetSize( &widthOld, &heightOld );
364 GetPosition( &x_current, &y_current );
1a87edf2 365
e40298d5
JS
366 x_offset = x;
367 y_offset = y;
43524b15
DS
368 if (!(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
369 {
370 if (x == wxDefaultCoord)
371 x_offset = x_current;
372 if (y == wxDefaultCoord)
373 y_offset = y_current;
374 }
1a87edf2 375
e40298d5 376 // define size
43524b15
DS
377 int charWidth, charHeight;
378 int maxWidth, maxHeight;
379 int eachWidth[128], eachHeight[128];
380 int totWidth, totHeight;
1a87edf2 381
43524b15
DS
382 GetTextExtent(
383 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
384 &charWidth, &charHeight );
facd6764 385
43524b15 386 charWidth /= 52;
1a87edf2 387
43524b15
DS
388 maxWidth = -1;
389 maxHeight = -1;
8228b893 390 for (size_t i = 0 ; i < m_noItems; i++)
e40298d5 391 {
43524b15 392 GetTextExtent( GetString( i ), &eachWidth[i], &eachHeight[i] );
e40298d5 393 eachWidth[i] = (int)(eachWidth[i] + RADIO_SIZE);
43524b15
DS
394 eachHeight[i] = (int)((3 * eachHeight[i]) / 2);
395
396 if (maxWidth < eachWidth[i])
397 maxWidth = eachWidth[i];
398 if (maxHeight < eachHeight[i])
399 maxHeight = eachHeight[i];
6cb0bfdf 400 }
1a87edf2 401
43524b15
DS
402 totHeight = GetRowCount() * maxHeight;
403 totWidth = GetColumnCount() * (maxWidth + charWidth);
facd6764 404
43524b15 405 wxSize sz = DoGetSizeFromClientSize( wxSize( totWidth, totHeight ) ) ;
1a87edf2 406
43524b15 407 // change the width / height only when specified
6cb0bfdf 408 if ( width == wxDefaultCoord )
8208e181
SC
409 {
410 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
43524b15 411 width = sz.x;
8208e181
SC
412 else
413 width = widthOld;
414 }
1a87edf2 415
6cb0bfdf 416 if ( height == wxDefaultCoord )
8208e181
SC
417 {
418 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
43524b15 419 height = sz.y;
8208e181
SC
420 else
421 height = heightOld;
422 }
1a87edf2 423
43524b15 424 wxControl::DoSetSize( x_offset, y_offset, width, height, wxSIZE_AUTO );
1a87edf2 425
43524b15
DS
426 // arrange radio buttons
427 int x_start, y_start;
1a87edf2 428
facd6764 429 x_start = 0;
43524b15 430 y_start = 0;
facd6764 431
e40298d5
JS
432 x_offset = x_start;
433 y_offset = y_start;
1a87edf2 434
43524b15 435 current = m_radioButtonCycle;
e40298d5
JS
436 for ( i = 0 ; i < m_noItems; i++)
437 {
43524b15
DS
438 // not to do for the zero button!
439 if ((i > 0) && ((i % GetMajorDim()) == 0))
e40298d5 440 {
3413cead 441 if (m_windowStyle & wxRA_SPECIFY_ROWS)
e40298d5
JS
442 {
443 x_offset += maxWidth + charWidth;
444 y_offset = y_start;
445 }
446 else
447 {
448 x_offset = x_start;
43524b15 449 y_offset += maxHeight ; //+ charHeight / 2
e40298d5
JS
450 }
451 }
1a87edf2 452
43524b15
DS
453 current->SetSize( x_offset, y_offset, eachWidth[i], eachHeight[i]);
454 current = current->NextInCycle();
1a87edf2 455
e40298d5 456 if (m_windowStyle & wxRA_SPECIFY_ROWS)
43524b15 457 y_offset += maxHeight ; // + charHeight / 2
e40298d5
JS
458 else
459 x_offset += maxWidth + charWidth;
460 }
e9576ca5
SC
461}
462
9453cf2b
SC
463wxSize wxRadioBox::DoGetBestSize() const
464{
465 int charWidth, charHeight;
466 int maxWidth, maxHeight;
467 int eachWidth, eachHeight;
468 int totWidth, totHeight;
1a87edf2 469
43524b15
DS
470 wxFont font = GetFont(); // GetParent()->GetFont()
471 GetTextExtent(
472 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
473 &charWidth, &charHeight, NULL, NULL, &font );
facd6764 474
9453cf2b 475 charWidth /= 52;
1a87edf2 476
e40298d5
JS
477 maxWidth = -1;
478 maxHeight = -1;
1a87edf2 479
8228b893 480 for (size_t i = 0 ; i < m_noItems; i++)
e40298d5 481 {
43524b15
DS
482 GetTextExtent( GetString( i ), &eachWidth, &eachHeight, NULL, NULL, &font );
483 eachWidth = (int)(eachWidth + RADIO_SIZE);
9453cf2b 484 eachHeight = (int)((3 * eachHeight) / 2);
43524b15
DS
485 if (maxWidth < eachWidth)
486 maxWidth = eachWidth;
487 if (maxHeight < eachHeight)
488 maxHeight = eachHeight;
9453cf2b 489 }
1a87edf2 490
43524b15
DS
491 totHeight = GetRowCount() * maxHeight;
492 totWidth = GetColumnCount() * (maxWidth + charWidth);
1a87edf2 493
43524b15
DS
494 wxSize sz = DoGetSizeFromClientSize( wxSize( totWidth, totHeight ) );
495 totWidth = sz.x;
496 totHeight = sz.y;
1a87edf2 497
c5bdc14f 498 // handle radio box title as well
43524b15
DS
499 GetTextExtent( GetLabel(), &eachWidth, NULL );
500 eachWidth = (int)(eachWidth + RADIO_SIZE) + 3 * charWidth;
1a87edf2 501 if (totWidth < eachWidth)
c5bdc14f 502 totWidth = eachWidth;
1a87edf2 503
43524b15 504 return wxSize( totWidth, totHeight );
9453cf2b 505}
ec7a8d02 506
8228b893 507#endif // wxUSE_RADIOBOX