]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/radiobox.cpp
cleanup - reformatting
[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
65571936 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
e9576ca5 104 m_noItems = 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{
371fd015 151 int i;
1395ff56 152 wxRadioButton *current;
1a87edf2 153
43524b15 154 if (!wxControl::Enable( enable ))
e40298d5 155 return false;
1a87edf2 156
371fd015 157 current = m_radioButtonCycle;
43524b15
DS
158 for (i = 0; i < m_noItems; i++)
159 {
160 current->Enable( enable );
e40298d5 161 current = current->NextInCycle();
371fd015 162 }
43524b15 163
371fd015 164 return true;
1395ff56
UJ
165}
166
1395ff56 167// Enables or disables an given button
43524b15 168//
1a87edf2 169bool wxRadioBox::Enable(int item, bool enable)
e9576ca5 170{
371fd015 171 int i;
1395ff56 172 wxRadioButton *current;
1a87edf2 173
43524b15 174 if (!IsValid( item ))
1a87edf2
WS
175 return false;
176
371fd015
GD
177 i = 0;
178 current = m_radioButtonCycle;
43524b15
DS
179 while (i != item)
180 {
e40298d5
JS
181 i++;
182 current = current->NextInCycle();
371fd015 183 }
43524b15
DS
184
185 return current->Enable( enable );
e9576ca5
SC
186}
187
1395ff56 188// Returns the radiobox label
43524b15 189//
1395ff56 190wxString wxRadioBox::GetLabel() const
e9576ca5 191{
1395ff56
UJ
192 return wxControl::GetLabel();
193}
e9576ca5 194
1395ff56 195// Returns the label for the given button
43524b15 196//
90b959ae 197wxString wxRadioBox::GetString(int item) const
1395ff56 198{
371fd015 199 int i;
1395ff56 200 wxRadioButton *current;
1a87edf2 201
43524b15 202 if (!IsValid( item ))
427ff662 203 return wxEmptyString;
1a87edf2 204
371fd015
GD
205 i = 0;
206 current = m_radioButtonCycle;
43524b15
DS
207 while (i != item)
208 {
e40298d5
JS
209 i++;
210 current = current->NextInCycle();
371fd015 211 }
43524b15 212
1395ff56 213 return current->GetLabel();
e9576ca5
SC
214}
215
1395ff56 216// Returns the zero-based position of the selected button
43524b15 217//
e9576ca5
SC
218int wxRadioBox::GetSelection() const
219{
1395ff56
UJ
220 int i;
221 wxRadioButton *current;
1a87edf2 222
43524b15
DS
223 i = 0;
224 current = m_radioButtonCycle;
225 while (!current->GetValue())
226 {
e40298d5 227 i++;
43524b15 228 current = current->NextInCycle();
e40298d5 229 }
1a87edf2 230
1395ff56 231 return i;
e9576ca5
SC
232}
233
1395ff56 234// Sets the radiobox label
43524b15 235//
1395ff56 236void wxRadioBox::SetLabel(const wxString& label)
e9576ca5 237{
43524b15 238 return wxControl::SetLabel( label );
e9576ca5
SC
239}
240
1395ff56 241// Sets the label of a given button
43524b15 242//
90b959ae 243void wxRadioBox::SetString(int item,const wxString& label)
e9576ca5 244{
43524b15 245 int i;
1395ff56 246 wxRadioButton *current;
1a87edf2 247
43524b15 248 if (!IsValid( item ))
1395ff56 249 return;
43524b15
DS
250
251 i = 0;
252 current = m_radioButtonCycle;
253 while (i != item)
254 {
e40298d5 255 i++;
43524b15 256 current = current->NextInCycle();
e40298d5 257 }
43524b15
DS
258
259 return current->SetLabel( label );
e9576ca5
SC
260}
261
1a87edf2 262// Sets a button by passing the desired position. This does not cause
1395ff56 263// wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
43524b15 264//
1395ff56 265void wxRadioBox::SetSelection(int item)
e9576ca5 266{
1395ff56
UJ
267 int i;
268 wxRadioButton *current;
1a87edf2 269
43524b15 270 if (!IsValid( item ))
1395ff56 271 return;
43524b15
DS
272
273 i = 0;
274 current = m_radioButtonCycle;
275 while (i != item)
276 {
e40298d5 277 i++;
43524b15 278 current = current->NextInCycle();
e40298d5 279 }
1a87edf2 280
43524b15 281 current->SetValue( true );
e9576ca5
SC
282}
283
1a87edf2 284// Shows or hides the entire radiobox
43524b15 285//
e9576ca5
SC
286bool wxRadioBox::Show(bool show)
287{
1395ff56
UJ
288 int i;
289 wxRadioButton *current;
1a87edf2 290
43524b15
DS
291 current = m_radioButtonCycle;
292 for (i=0; i<m_noItems; i++)
6cb0bfdf 293 {
43524b15
DS
294 current->Show( show );
295 current = current->NextInCycle();
e40298d5 296 }
43524b15
DS
297
298 wxControl::Show( show );
299
1395ff56 300 return true;
e9576ca5
SC
301}
302
1a87edf2 303// Shows or hides the given button
43524b15 304//
6cb0bfdf 305bool wxRadioBox::Show(int item, bool show)
e9576ca5 306{
6cb0bfdf 307 int i;
1395ff56 308 wxRadioButton *current;
1a87edf2 309
43524b15 310 if (!IsValid( item ))
6cb0bfdf 311 return false;
43524b15
DS
312
313 i = 0;
314 current = m_radioButtonCycle;
315 while (i != item)
316 {
e40298d5 317 i++;
43524b15 318 current = current->NextInCycle();
e40298d5 319 }
43524b15
DS
320
321 return current->Show( show );
e9576ca5
SC
322}
323
1395ff56 324// Simulates the effect of the user issuing a command to the item
43524b15
DS
325//
326void wxRadioBox::Command( wxCommandEvent& event )
e9576ca5 327{
43524b15
DS
328 SetSelection( event.GetInt() );
329 ProcessCommand( event );
e9576ca5
SC
330}
331
1395ff56 332// Sets the selected button to receive keyboard input
43524b15 333//
1395ff56 334void wxRadioBox::SetFocus()
e9576ca5 335{
1395ff56
UJ
336 int i;
337 wxRadioButton *current;
1a87edf2 338
43524b15
DS
339 i = 0;
340 current = m_radioButtonCycle;
341 while (!current->GetValue())
342 {
e40298d5 343 i++;
43524b15 344 current = current->NextInCycle();
e40298d5 345 }
43524b15 346
e40298d5 347 current->SetFocus();
e9576ca5
SC
348}
349
1395ff56 350// Simulates the effect of the user issuing a command to the item
43524b15 351//
baf34314 352#define RADIO_SIZE 20
1395ff56
UJ
353
354void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
e9576ca5 355{
e40298d5
JS
356 int i;
357 wxRadioButton *current;
1a87edf2 358
e40298d5 359 // define the position
1a87edf2 360
e40298d5 361 int x_current, y_current;
43524b15 362 int x_offset, y_offset;
8208e181 363 int widthOld, heightOld;
43524b15
DS
364
365 GetSize( &widthOld, &heightOld );
366 GetPosition( &x_current, &y_current );
1a87edf2 367
e40298d5
JS
368 x_offset = x;
369 y_offset = y;
43524b15
DS
370 if (!(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
371 {
372 if (x == wxDefaultCoord)
373 x_offset = x_current;
374 if (y == wxDefaultCoord)
375 y_offset = y_current;
376 }
1a87edf2 377
e40298d5 378 // define size
43524b15
DS
379 int charWidth, charHeight;
380 int maxWidth, maxHeight;
381 int eachWidth[128], eachHeight[128];
382 int totWidth, totHeight;
1a87edf2 383
43524b15
DS
384 GetTextExtent(
385 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
386 &charWidth, &charHeight );
facd6764 387
43524b15 388 charWidth /= 52;
1a87edf2 389
43524b15
DS
390 maxWidth = -1;
391 maxHeight = -1;
e40298d5
JS
392 for (i = 0 ; i < m_noItems; i++)
393 {
43524b15 394 GetTextExtent( GetString( i ), &eachWidth[i], &eachHeight[i] );
e40298d5 395 eachWidth[i] = (int)(eachWidth[i] + RADIO_SIZE);
43524b15
DS
396 eachHeight[i] = (int)((3 * eachHeight[i]) / 2);
397
398 if (maxWidth < eachWidth[i])
399 maxWidth = eachWidth[i];
400 if (maxHeight < eachHeight[i])
401 maxHeight = eachHeight[i];
6cb0bfdf 402 }
1a87edf2 403
43524b15
DS
404 totHeight = GetRowCount() * maxHeight;
405 totWidth = GetColumnCount() * (maxWidth + charWidth);
facd6764 406
43524b15 407 wxSize sz = DoGetSizeFromClientSize( wxSize( totWidth, totHeight ) ) ;
1a87edf2 408
43524b15 409 // change the width / height only when specified
6cb0bfdf 410 if ( width == wxDefaultCoord )
8208e181
SC
411 {
412 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
43524b15 413 width = sz.x;
8208e181
SC
414 else
415 width = widthOld;
416 }
1a87edf2 417
6cb0bfdf 418 if ( height == wxDefaultCoord )
8208e181
SC
419 {
420 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
43524b15 421 height = sz.y;
8208e181
SC
422 else
423 height = heightOld;
424 }
1a87edf2 425
43524b15 426 wxControl::DoSetSize( x_offset, y_offset, width, height, wxSIZE_AUTO );
1a87edf2 427
43524b15
DS
428 // arrange radio buttons
429 int x_start, y_start;
1a87edf2 430
facd6764 431 x_start = 0;
43524b15 432 y_start = 0;
facd6764 433
e40298d5
JS
434 x_offset = x_start;
435 y_offset = y_start;
1a87edf2 436
43524b15 437 current = m_radioButtonCycle;
e40298d5
JS
438 for ( i = 0 ; i < m_noItems; i++)
439 {
43524b15
DS
440 // not to do for the zero button!
441 if ((i > 0) && ((i % GetMajorDim()) == 0))
e40298d5 442 {
3413cead 443 if (m_windowStyle & wxRA_SPECIFY_ROWS)
e40298d5
JS
444 {
445 x_offset += maxWidth + charWidth;
446 y_offset = y_start;
447 }
448 else
449 {
450 x_offset = x_start;
43524b15 451 y_offset += maxHeight ; //+ charHeight / 2
e40298d5
JS
452 }
453 }
1a87edf2 454
43524b15
DS
455 current->SetSize( x_offset, y_offset, eachWidth[i], eachHeight[i]);
456 current = current->NextInCycle();
1a87edf2 457
e40298d5 458 if (m_windowStyle & wxRA_SPECIFY_ROWS)
43524b15 459 y_offset += maxHeight ; // + charHeight / 2
e40298d5
JS
460 else
461 x_offset += maxWidth + charWidth;
462 }
e9576ca5
SC
463}
464
9453cf2b
SC
465wxSize wxRadioBox::DoGetBestSize() const
466{
467 int charWidth, charHeight;
468 int maxWidth, maxHeight;
469 int eachWidth, eachHeight;
470 int totWidth, totHeight;
1a87edf2 471
43524b15
DS
472 wxFont font = GetFont(); // GetParent()->GetFont()
473 GetTextExtent(
474 wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
475 &charWidth, &charHeight, NULL, NULL, &font );
facd6764 476
9453cf2b 477 charWidth /= 52;
1a87edf2 478
e40298d5
JS
479 maxWidth = -1;
480 maxHeight = -1;
1a87edf2 481
e40298d5
JS
482 for (int i = 0 ; i < m_noItems; i++)
483 {
43524b15
DS
484 GetTextExtent( GetString( i ), &eachWidth, &eachHeight, NULL, NULL, &font );
485 eachWidth = (int)(eachWidth + RADIO_SIZE);
9453cf2b 486 eachHeight = (int)((3 * eachHeight) / 2);
43524b15
DS
487 if (maxWidth < eachWidth)
488 maxWidth = eachWidth;
489 if (maxHeight < eachHeight)
490 maxHeight = eachHeight;
9453cf2b 491 }
1a87edf2 492
43524b15
DS
493 totHeight = GetRowCount() * maxHeight;
494 totWidth = GetColumnCount() * (maxWidth + charWidth);
1a87edf2 495
43524b15
DS
496 wxSize sz = DoGetSizeFromClientSize( wxSize( totWidth, totHeight ) );
497 totWidth = sz.x;
498 totHeight = sz.y;
1a87edf2 499
c5bdc14f 500 // handle radio box title as well
43524b15
DS
501 GetTextExtent( GetLabel(), &eachWidth, NULL );
502 eachWidth = (int)(eachWidth + RADIO_SIZE) + 3 * charWidth;
1a87edf2 503 if (totWidth < eachWidth)
c5bdc14f 504 totWidth = eachWidth;
1a87edf2 505
43524b15 506 return wxSize( totWidth, totHeight );
9453cf2b 507}
ec7a8d02
DS
508
509#endif