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