]> git.saurik.com Git - wxWidgets.git/blob - src/mac/radiobox.cpp
added default ctor for wxDirDialog
[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 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 20
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
435 x_offset = x;
436 y_offset = y;
437 GetPosition(&x_current, &y_current);
438 if ((x == -1) || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
439 x_offset = x_current;
440 if ((y == -1) || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
441 y_offset = y_current;
442
443 // define size
444
445 int charWidth,charHeight;
446 int maxWidth,maxHeight;
447 int eachWidth[128],eachHeight[128];
448 int totWidth,totHeight;
449
450 SetFont(GetParent()->GetFont());
451 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth, &charHeight);
452 charWidth/=52;
453
454 maxWidth=-1;
455 maxHeight=-1;
456 for (i = 0 ; i < m_noItems; i++)
457 {
458 GetTextExtent(GetLabel(i), &eachWidth[i], &eachHeight[i]);
459 eachWidth[i] = (int)(eachWidth[i] + RADIO_SIZE);
460 eachHeight[i] = (int)((3*eachHeight[i])/2);
461 if (maxWidth<eachWidth[i]) maxWidth = eachWidth[i];
462 if (maxHeight<eachHeight[i]) maxHeight = eachHeight[i];
463 }
464
465 totHeight = GetNumVer() * (maxHeight + charHeight/2) + charHeight*3/2;
466 totWidth = GetNumHor() * (maxWidth + charWidth) + charWidth;
467
468 wxControl::DoSetSize(x_offset,y_offset,totWidth,totHeight,wxSIZE_AUTO);
469
470 // arrange radiobuttons
471
472 int x_start,y_start;
473
474
475 x_start = charWidth;
476 y_start = charHeight*3/2;
477 x_offset = x_start;
478 y_offset = y_start;
479
480 current=m_radioButtonCycle;
481 for ( i = 0 ; i < m_noItems; i++)
482 {
483 if (i&&((i%m_majorDim)==0)) // not to do for the zero button!
484 {
485 if (m_windowStyle & wxRA_VERTICAL)
486 {
487 x_offset += maxWidth + charWidth;
488 y_offset = y_start;
489 }
490 else
491 {
492 x_offset = x_start;
493 y_offset += maxHeight + charHeight/2;
494 }
495 }
496
497 current->SetSize(x_offset,y_offset,eachWidth[i],eachHeight[i]);
498 current=current->NextInCycle();
499
500 if (m_windowStyle & wxRA_SPECIFY_ROWS)
501 y_offset += maxHeight + charHeight/2;
502 else
503 x_offset += maxWidth + charWidth;
504 }
505 }
506
507 //-------------------------------------------------------------------------------------
508 // ¥ GetNumVer
509 //-------------------------------------------------------------------------------------
510 // return the number of buttons in the vertical direction
511
512 int wxRadioBox::GetNumVer() const
513 {
514 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
515 {
516 return m_majorDim;
517 }
518 else
519 {
520 return (m_noItems + m_majorDim - 1)/m_majorDim;
521 }
522 }
523
524 //-------------------------------------------------------------------------------------
525 // ¥ GetNumHor
526 //-------------------------------------------------------------------------------------
527 // return the number of buttons in the horizontal direction
528
529 int wxRadioBox::GetNumHor() const
530 {
531 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
532 {
533 return (m_noItems + m_majorDim - 1)/m_majorDim;
534 }
535 else
536 {
537 return m_majorDim;
538 }
539 }
540
541
542
543
544