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