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