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