]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/radiobox.cpp
oops, typo leading to compilation problem fixed
[wxWidgets.git] / src / mac / carbon / radiobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: radiobox.cpp
3 // Purpose: wxRadioBox
4 // Author: AUTHOR
5 // Modified by: JS Lair (99/11/15) first implementation
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 //-------------------------------------------------------------------------------------
13 // headers
14 //-------------------------------------------------------------------------------------
15
16 #ifdef __GNUG__
17 #pragma implementation "radiobox.h"
18 #endif
19
20 #include "wx/radiobox.h"
21 #include "wx/radiobut.h"
22 #include "wx/mac/uma.h"
23
24 #if !USE_SHARED_LIBRARY
25 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
26 #endif
27
28 #pragma mark -
29 #pragma mark ### Constructors & destructor ###
30
31 //-------------------------------------------------------------------------------------
32 // ¥ wxRadioBox()
33 //-------------------------------------------------------------------------------------
34 // Default constructor
35 BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
36 EVT_RADIOBUTTON( -1 , wxRadioBox::OnRadioButton )
37 END_EVENT_TABLE()
38
39 void wxRadioBox::OnRadioButton( wxCommandEvent &outer )
40 {
41 wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId);
42 int i = GetSelection() ;
43 event.SetInt( i );
44 event.SetString( GetString( i ) );
45 event.SetEventObject( this );
46 ProcessCommand(event);
47 }
48
49 wxRadioBox::wxRadioBox()
50 {
51 m_noItems = 0;
52 m_noRowsOrCols = 0;
53 m_majorDim = 0 ;
54 m_radioButtonCycle = NULL;
55 }
56
57 //-------------------------------------------------------------------------------------
58 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
59 // const wxSize&, int, const wxString[], int, long,
60 // const wxValidator&, const wxString&)
61 //-------------------------------------------------------------------------------------
62 // Contructor, creating and showing a radiobox
63 //
64 // inline defined
65 //
66
67 //-------------------------------------------------------------------------------------
68 // ¥ ~wxRadioBox
69 //-------------------------------------------------------------------------------------
70 // Destructor, destroying the radiobox item
71
72 wxRadioBox::~wxRadioBox()
73 {
74 wxRadioButton *next,*current;
75
76 current=m_radioButtonCycle->NextInCycle();
77 next=current->NextInCycle();
78 while (current!=m_radioButtonCycle) {
79 delete current;
80 current=next;
81 next=current->NextInCycle();
82 }
83 delete current;
84 }
85
86 //-------------------------------------------------------------------------------------
87 // ¥ Create
88 //-------------------------------------------------------------------------------------
89 // Create the radiobox for two-step construction
90
91 bool wxRadioBox::Create(wxWindow *parent, 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)
96 {
97 int i;
98
99 m_noItems = n;
100 m_noRowsOrCols = majorDim;
101 m_radioButtonCycle = NULL;
102
103 if (majorDim==0)
104 m_majorDim = n ;
105 else
106 m_majorDim = majorDim ;
107
108
109 Rect bounds ;
110 Str255 title ;
111
112 MacPreControlCreate( parent , id , label , pos , size ,style, val , name , &bounds , title ) ;
113
114 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , title , true , 0 , 0 , 1,
115 kControlGroupBoxTextTitleProc , (long) this ) ;
116
117 MacPostControlCreate() ;
118
119 for (i = 0; i < n; i++)
120 {
121 wxRadioButton *radBtn = new wxRadioButton(this, NewControlId(),choices[i],wxPoint(5,20*i+10),
122 wxDefaultSize , i == 0 ? wxRB_GROUP : 0 ) ;
123 if ( i == 0 )
124 m_radioButtonCycle = radBtn ;
125 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
126 }
127
128 SetSelection(0);
129 SetSize(pos.x,pos.y,size.x,size.y);
130
131 return TRUE;
132 }
133
134
135 #pragma mark -
136 #pragma mark ### Specific functions (reference v2) ###
137
138 //-------------------------------------------------------------------------------------
139 // ¥ Enable(bool)
140 //-------------------------------------------------------------------------------------
141 // Enables or disables the entire radiobox
142
143 bool wxRadioBox::Enable(bool enable)
144 {
145 int i;
146 wxRadioButton *current;
147
148 if (!wxControl::Enable(enable))
149 return (false);
150
151 current=m_radioButtonCycle;
152 for (i=0;i<m_noItems;i++) {
153 current->Enable(enable);
154 current=current->NextInCycle();
155 }
156 return (true);
157 }
158
159 //-------------------------------------------------------------------------------------
160 // ¥ Enable(int, bool)
161 //-------------------------------------------------------------------------------------
162 // Enables or disables an given button
163
164 void wxRadioBox::Enable(int item, bool enable)
165 {
166 int i;
167 wxRadioButton *current;
168
169 if ((item < 0) || (item >= m_noItems))
170 return;
171 i=0;
172 current=m_radioButtonCycle;
173 while (i!=item) {
174 i++;
175 current=current->NextInCycle();
176 }
177 }
178
179
180 //-------------------------------------------------------------------------------------
181 // ¥ FindString
182 //-------------------------------------------------------------------------------------
183 // Finds a button matching the given string, returning the position if found
184 // or -1 if not found
185
186 int wxRadioBox::FindString(const wxString& s) const
187 {
188 int i;
189 wxRadioButton *current;
190
191 current=m_radioButtonCycle;
192 for (i = 0; i < m_noItems; i++)
193 {
194 if (s == current->GetLabel())
195 return i;
196 current=current->NextInCycle();
197 }
198 return -1;
199 }
200
201 //-------------------------------------------------------------------------------------
202 // ¥ GetLabel()
203 //-------------------------------------------------------------------------------------
204 // Returns the radiobox label
205
206 wxString wxRadioBox::GetLabel() const
207 {
208 return wxControl::GetLabel();
209 }
210
211 //-------------------------------------------------------------------------------------
212 // ¥ GetLabel(int)
213 //-------------------------------------------------------------------------------------
214 // Returns the label for the given button
215
216 wxString wxRadioBox::GetLabel(int item) const
217 {
218 int i;
219 wxRadioButton *current;
220
221 if ((item < 0) || (item >= m_noItems))
222 return wxString("");
223 i=0;
224 current=m_radioButtonCycle;
225 while (i!=item) {
226 i++;
227 current=current->NextInCycle();
228 }
229 return current->GetLabel();
230 }
231
232 //-------------------------------------------------------------------------------------
233 // ¥ GetSelection
234 //-------------------------------------------------------------------------------------
235 // Returns the zero-based position of the selected button
236
237 int wxRadioBox::GetSelection() const
238 {
239 int i;
240 wxRadioButton *current;
241
242 i=0;
243 current=m_radioButtonCycle;
244 while (!current->GetValue()) {
245 i++;
246 current=current->NextInCycle();
247 }
248
249 return i;
250 }
251
252 //-------------------------------------------------------------------------------------
253 // ¥ GetString
254 //-------------------------------------------------------------------------------------
255 // Find string for position
256
257 wxString wxRadioBox::GetString(int item) const
258 {
259
260 return GetLabel(item);
261 }
262
263 //-------------------------------------------------------------------------------------
264 // ¥ GetStringSelection
265 //-------------------------------------------------------------------------------------
266 // Returns the selected string
267
268 wxString wxRadioBox::GetStringSelection () const
269 {
270 int sel = GetSelection ();
271 if (sel > -1)
272 return this->GetString (sel);
273 else
274 return wxString("");
275 }
276
277 //-------------------------------------------------------------------------------------
278 // ¥ Number
279 //-------------------------------------------------------------------------------------
280 // Returns the number of buttons in the radiobox
281 //
282 // inline defined
283 //
284
285 //-------------------------------------------------------------------------------------
286 // ¥ SetLabel(const wxString&)
287 //-------------------------------------------------------------------------------------
288 // Sets the radiobox label
289
290 void wxRadioBox::SetLabel(const wxString& label)
291 {
292 return wxControl::SetLabel(label);
293 }
294
295 //-------------------------------------------------------------------------------------
296 // ¥ SetLabel(int, const wxString&)
297 //-------------------------------------------------------------------------------------
298 // Sets the label of a given button
299
300 void wxRadioBox::SetLabel(int item,const wxString& label)
301 {
302 int i;
303 wxRadioButton *current;
304
305 if ((item < 0) || (item >= m_noItems))
306 return;
307 i=0;
308 current=m_radioButtonCycle;
309 while (i!=item) {
310 i++;
311 current=current->NextInCycle();
312 }
313 return current->SetLabel(label);
314 }
315
316 //-------------------------------------------------------------------------------------
317 // ¥ SetSelection
318 //-------------------------------------------------------------------------------------
319 // Sets a button by passing the desired position. This does not cause
320 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
321
322 void wxRadioBox::SetSelection(int item)
323 {
324 int i;
325 wxRadioButton *current;
326
327 if ((item < 0) || (item >= m_noItems))
328 return;
329 i=0;
330 current=m_radioButtonCycle;
331 while (i!=item) {
332 i++;
333 current=current->NextInCycle();
334 }
335 current->SetValue(true);
336
337 }
338
339 //-------------------------------------------------------------------------------------
340 // ¥ SetStringSelection
341 //-------------------------------------------------------------------------------------
342 // Sets a button by passing the desired string. This does not cause
343 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
344
345 bool wxRadioBox::SetStringSelection (const wxString& s)
346 {
347 int sel = FindString (s);
348 if (sel > -1)
349 {
350 SetSelection (sel);
351 return TRUE;
352 }
353 else
354 return FALSE;
355 }
356
357 //-------------------------------------------------------------------------------------
358 // ¥ Show(bool)
359 //-------------------------------------------------------------------------------------
360 // Shows or hides the entire radiobox
361
362 bool wxRadioBox::Show(bool show)
363 {
364 int i;
365 wxRadioButton *current;
366
367 wxControl::Show(show);
368
369 current=m_radioButtonCycle;
370 for (i=0;i<m_noItems;i++) {
371 current->Show(show);
372 current=current->NextInCycle();
373 }
374 return true;
375 }
376
377 //-------------------------------------------------------------------------------------
378 // ¥ Show(int, bool)
379 //-------------------------------------------------------------------------------------
380 // Shows or hides the given button
381
382 void wxRadioBox::Show(int item, bool show)
383 {
384 int i;
385 wxRadioButton *current;
386
387 if ((item < 0) || (item >= m_noItems))
388 return;
389 i=0;
390 current=m_radioButtonCycle;
391 while (i!=item) {
392 i++;
393 current=current->NextInCycle();
394 }
395 current->Show(show);
396 }
397
398 #pragma mark -
399 #pragma mark ### Other external functions ###
400
401 //-------------------------------------------------------------------------------------
402 // ¥ Command
403 //-------------------------------------------------------------------------------------
404 // Simulates the effect of the user issuing a command to the item
405
406 void wxRadioBox::Command (wxCommandEvent & event)
407 {
408 SetSelection (event.GetInt());
409 ProcessCommand (event);
410 }
411
412 //-------------------------------------------------------------------------------------
413 // ¥ SetFocus
414 //-------------------------------------------------------------------------------------
415 // Sets the selected button to receive keyboard input
416
417 void wxRadioBox::SetFocus()
418 {
419 int i;
420 wxRadioButton *current;
421
422 i=0;
423 current=m_radioButtonCycle;
424 while (!current->GetValue()) {
425 i++;
426 current=current->NextInCycle();
427 }
428 current->SetFocus();
429 }
430
431
432 #pragma mark -
433 #pragma mark ### Internal functions ###
434
435 //-------------------------------------------------------------------------------------
436 // ¥ DoSetSize
437 //-------------------------------------------------------------------------------------
438 // Simulates the effect of the user issuing a command to the item
439
440 #define RADIO_SIZE 40
441
442 void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
443 {
444 int i;
445 wxRadioButton *current;
446
447 // define the position
448
449 int x_current, y_current;
450 int x_offset,y_offset;
451 int widthOld, heightOld;
452 GetSize(&widthOld, &heightOld);
453
454 x_offset = x;
455 y_offset = y;
456 GetPosition(&x_current, &y_current);
457 if ((x == -1) && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
458 x_offset = x_current;
459 if ((y == -1)&& !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
460 y_offset = y_current;
461
462 // define size
463
464 int charWidth,charHeight;
465 int maxWidth,maxHeight;
466 int eachWidth[128],eachHeight[128];
467 int totWidth,totHeight;
468
469 SetFont(GetParent()->GetFont());
470 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth, &charHeight);
471 charWidth/=52;
472
473 maxWidth=-1;
474 maxHeight=-1;
475 for (i = 0 ; i < m_noItems; i++)
476 {
477 GetTextExtent(GetLabel(i), &eachWidth[i], &eachHeight[i]);
478 eachWidth[i] = (int)(eachWidth[i] + RADIO_SIZE);
479 eachHeight[i] = (int)((3*eachHeight[i])/2);
480 if (maxWidth<eachWidth[i]) maxWidth = eachWidth[i];
481 if (maxHeight<eachHeight[i]) maxHeight = eachHeight[i];
482 }
483
484 totHeight = GetNumVer() * (maxHeight + charHeight/2) + charHeight*3/2;
485 totWidth = GetNumHor() * (maxWidth + charWidth) + charWidth;
486
487 // only change our width/height if asked for
488 if ( width == -1 )
489 {
490 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
491 width = totWidth ;
492 else
493 width = widthOld;
494 }
495
496 if ( height == -1 )
497 {
498 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
499 height = totHeight ;
500 else
501 height = heightOld;
502 }
503
504 wxControl::DoSetSize(x_offset,y_offset,width,height,wxSIZE_AUTO);
505
506 // arrange radiobuttons
507
508 int x_start,y_start;
509
510
511 x_start = charWidth;
512 y_start = 15 ;
513 x_offset = x_start;
514 y_offset = y_start;
515
516 current=m_radioButtonCycle;
517 for ( i = 0 ; i < m_noItems; i++)
518 {
519 if (i&&((i%m_majorDim)==0)) // not to do for the zero button!
520 {
521 if (m_windowStyle & wxRA_VERTICAL)
522 {
523 x_offset += maxWidth + charWidth;
524 y_offset = y_start;
525 }
526 else
527 {
528 x_offset = x_start;
529 y_offset += maxHeight ; /*+ charHeight/2;*/
530 }
531 }
532
533 current->SetSize(x_offset,y_offset,eachWidth[i],eachHeight[i]);
534 current=current->NextInCycle();
535
536 if (m_windowStyle & wxRA_SPECIFY_ROWS)
537 y_offset += maxHeight ; /*+ charHeight/2;*/
538 else
539 x_offset += maxWidth + charWidth;
540 }
541 }
542
543 //-------------------------------------------------------------------------------------
544 // ¥ GetNumVer
545 //-------------------------------------------------------------------------------------
546 // return the number of buttons in the vertical direction
547
548 int wxRadioBox::GetNumVer() const
549 {
550 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
551 {
552 return m_majorDim;
553 }
554 else
555 {
556 return (m_noItems + m_majorDim - 1)/m_majorDim;
557 }
558 }
559
560 //-------------------------------------------------------------------------------------
561 // ¥ GetNumHor
562 //-------------------------------------------------------------------------------------
563 // return the number of buttons in the horizontal direction
564
565 int wxRadioBox::GetNumHor() const
566 {
567 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
568 {
569 return (m_noItems + m_majorDim - 1)/m_majorDim;
570 }
571 else
572 {
573 return m_majorDim;
574 }
575 }
576
577
578
579
580