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