]> git.saurik.com Git - wxWidgets.git/blob - src/mac/radiobox.cpp
Reordered includes to have OS2EMX_PLAIN_CHAR defined prior to including os2.h
[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 if ( UMAGetSystemVersion() >= 0x1030 )
456 {
457 //need to add a few more pixels for the top border on panther
458 y_start = y_start + 5; //how many exactly should this be to meet the HIG?
459 }
460 x_offset = x_start;
461 y_offset = y_start;
462
463 current=m_radioButtonCycle;
464 for ( i = 0 ; i < m_noItems; i++)
465 {
466 if (i&&((i%m_majorDim)==0)) // not to do for the zero button!
467 {
468 if (m_windowStyle & wxRA_VERTICAL)
469 {
470 x_offset += maxWidth + charWidth;
471 y_offset = y_start;
472 }
473 else
474 {
475 x_offset = x_start;
476 y_offset += maxHeight ; /*+ charHeight/2;*/
477 }
478 }
479
480 current->SetSize(x_offset,y_offset,eachWidth[i],eachHeight[i]);
481 current=current->NextInCycle();
482
483 if (m_windowStyle & wxRA_SPECIFY_ROWS)
484 y_offset += maxHeight ; /*+ charHeight/2;*/
485 else
486 x_offset += maxWidth + charWidth;
487 }
488 }
489
490 wxSize wxRadioBox::DoGetBestSize() const
491 {
492 int charWidth, charHeight;
493 int maxWidth, maxHeight;
494 int eachWidth, eachHeight;
495 int totWidth, totHeight;
496
497 wxFont font = GetParent()->GetFont();
498 GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
499 &charWidth, &charHeight, NULL, NULL, &font);
500 charWidth /= 52;
501
502 maxWidth = -1;
503 maxHeight = -1;
504
505 for (int i = 0 ; i < m_noItems; i++)
506 {
507 GetTextExtent(GetString(i), &eachWidth, &eachHeight);
508 eachWidth = (int)(eachWidth + RADIO_SIZE) ;
509 eachHeight = (int)((3 * eachHeight) / 2);
510 if (maxWidth < eachWidth) maxWidth = eachWidth;
511 if (maxHeight < eachHeight) maxHeight = eachHeight;
512 }
513
514 totHeight = GetRowCount() * (maxHeight + charHeight/2) + charHeight ;
515 totWidth = GetColumnCount() * (maxWidth + charWidth) + charWidth;
516
517 if ( UMAGetSystemVersion() >= 0x1030 )
518 {
519 //need to add a few more pixels for the static boxborder on panther
520 totHeight = totHeight + 10; //how many exactly should this be to meet the HIG?
521 }
522 // handle radio box title as well
523 GetTextExtent(GetTitle(), &eachWidth, NULL);
524 eachWidth = (int)(eachWidth + RADIO_SIZE) + 3 * charWidth ;
525 if (totWidth < eachWidth)
526 totWidth = eachWidth;
527
528 return wxSize(totWidth, totHeight);
529 }
530 //-------------------------------------------------------------------------------------
531 // ¥ GetNumVer
532 //-------------------------------------------------------------------------------------
533 // return the number of buttons in the vertical direction
534
535 int wxRadioBox::GetRowCount() const
536 {
537 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
538 {
539 return m_majorDim;
540 }
541 else
542 {
543 return (m_noItems + m_majorDim - 1)/m_majorDim;
544 }
545 }
546
547 //-------------------------------------------------------------------------------------
548 // ¥ GetNumHor
549 //-------------------------------------------------------------------------------------
550 // return the number of buttons in the horizontal direction
551
552 int wxRadioBox::GetColumnCount() const
553 {
554 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
555 {
556 return (m_noItems + m_majorDim - 1)/m_majorDim;
557 }
558 else
559 {
560 return m_majorDim;
561 }
562 }
563
564
565
566
567