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