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