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