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