]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/radiobox.cpp
cleanup - reformatting
[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/wxprec.h"
17
18 #if wxUSE_RADIOBOX
19
20 #include "wx/arrstr.h"
21 #include "wx/radiobox.h"
22 #include "wx/radiobut.h"
23 #include "wx/mac/uma.h"
24
25 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
26
27 //-------------------------------------------------------------------------------------
28 // ¥ wxRadioBox()
29 //-------------------------------------------------------------------------------------
30 // Default constructor
31 BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
32 EVT_RADIOBUTTON( wxID_ANY , wxRadioBox::OnRadioButton )
33 END_EVENT_TABLE()
34
35 void wxRadioBox::OnRadioButton( wxCommandEvent &outer )
36 {
37 if ( outer.IsChecked() )
38 {
39 wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId);
40 int i = GetSelection() ;
41 event.SetInt( i );
42 event.SetString( GetString( i ) );
43 event.SetEventObject( this );
44 ProcessCommand(event);
45 }
46 }
47
48 wxRadioBox::wxRadioBox()
49 {
50 m_noItems = 0;
51 m_noRowsOrCols = 0;
52 m_radioButtonCycle = NULL;
53 }
54
55 //-------------------------------------------------------------------------------------
56 // ¥ wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
57 // const wxSize&, int, const wxString[], int, long,
58 // const wxValidator&, const wxString&)
59 //-------------------------------------------------------------------------------------
60 // Contructor, creating and showing a radiobox
61 //
62 // inline defined
63 //
64
65 //-------------------------------------------------------------------------------------
66 // ¥ ~wxRadioBox
67 //-------------------------------------------------------------------------------------
68 // Destructor, destroying the radiobox item
69
70 wxRadioBox::~wxRadioBox()
71 {
72 m_isBeingDeleted = true;
73
74 wxRadioButton *next,*current;
75
76 current=m_radioButtonCycle->NextInCycle();
77 next=current->NextInCycle();
78 while (current!=m_radioButtonCycle) {
79 delete current;
80 current=next;
81 next=current->NextInCycle();
82 }
83 delete current;
84 }
85
86 //-------------------------------------------------------------------------------------
87 // ¥ Create
88 //-------------------------------------------------------------------------------------
89 // Create the radiobox for two-step construction
90
91 bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& label,
92 const wxPoint& pos, const wxSize& size,
93 const wxArrayString& choices,
94 int majorDim, long style,
95 const wxValidator& val, const wxString& name)
96 {
97 wxCArrayString chs(choices);
98
99 return Create(parent, id, label, pos, size, chs.GetCount(),
100 chs.GetStrings(), majorDim, style, val, name);
101 }
102
103 bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& label,
104 const wxPoint& pos, const wxSize& size,
105 int n, const wxString choices[],
106 int majorDim, long style,
107 const wxValidator& val, const wxString& name)
108 {
109 if ( !wxControl::Create(parent, id, pos, size, style, val, name) )
110 return false;
111
112 int i;
113
114 m_noItems = (size_t)n;
115 m_noRowsOrCols = majorDim;
116 m_radioButtonCycle = NULL;
117
118 SetMajorDim(majorDim == 0 ? n : majorDim, style);
119
120 Rect bounds ;
121 Str255 title ;
122
123 MacPreControlCreate( parent , id , wxStripMenuCodes(label) , pos , size ,style, val , name , &bounds , title ) ;
124
125 m_macControl = (WXWidget) ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , 0 , 1,
126 kControlGroupBoxTextTitleProc , (long) this ) ;
127
128 for (i = 0; i < n; i++)
129 {
130 wxRadioButton *radBtn = new wxRadioButton
131 (
132 this,
133 wxID_ANY,
134 wxStripMenuCodes(choices[i]),
135 wxPoint(5,20*i+10),
136 wxDefaultSize,
137 i == 0 ? wxRB_GROUP : 0
138 );
139 if ( i == 0 )
140 m_radioButtonCycle = radBtn ;
141 // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
142 }
143
144 SetSelection(0);
145 MacPostControlCreate() ;
146
147 return true;
148 }
149
150
151 //-------------------------------------------------------------------------------------
152 // ¥ Enable(bool)
153 //-------------------------------------------------------------------------------------
154 // Enables or disables the entire radiobox
155
156 bool wxRadioBox::Enable(bool enable)
157 {
158 wxRadioButton *current;
159
160 if (!wxControl::Enable(enable))
161 return false;
162
163 current = m_radioButtonCycle;
164 for (size_t i = 0; i < m_noItems; i++) {
165 current->Enable(enable);
166 current = current->NextInCycle();
167 }
168 return true;
169 }
170
171 //-------------------------------------------------------------------------------------
172 // ¥ Enable(int, bool)
173 //-------------------------------------------------------------------------------------
174 // Enables or disables an given button
175
176 bool wxRadioBox::Enable(int item, bool enable)
177 {
178 int i;
179 wxRadioButton *current;
180
181 if (!IsValid(item))
182 return false;
183
184 i = 0;
185 current = m_radioButtonCycle;
186 while (i != item) {
187 i++;
188 current = current->NextInCycle();
189 }
190 return current->Enable(enable);
191 }
192
193 //-------------------------------------------------------------------------------------
194 // ¥ GetLabel()
195 //-------------------------------------------------------------------------------------
196 // Returns the radiobox label
197
198 wxString wxRadioBox::GetLabel() const
199 {
200 return wxControl::GetLabel();
201 }
202
203 //-------------------------------------------------------------------------------------
204 // ¥ GetLabel(int)
205 //-------------------------------------------------------------------------------------
206 // Returns the label for the given button
207
208 wxString wxRadioBox::GetString(int item) const
209 {
210 int i;
211 wxRadioButton *current;
212
213 if (!IsValid(item))
214 return wxEmptyString;
215
216 i = 0;
217 current = m_radioButtonCycle;
218 while (i != item) {
219 i++;
220 current = current->NextInCycle();
221 }
222 return current->GetLabel();
223 }
224
225 //-------------------------------------------------------------------------------------
226 // ¥ GetSelection
227 //-------------------------------------------------------------------------------------
228 // Returns the zero-based position of the selected button
229
230 int wxRadioBox::GetSelection() const
231 {
232 int i;
233 wxRadioButton *current;
234
235 i=0;
236 current=m_radioButtonCycle;
237 while (!current->GetValue()) {
238 i++;
239 current=current->NextInCycle();
240 }
241
242 return i;
243 }
244
245 //-------------------------------------------------------------------------------------
246 // ¥ Number
247 //-------------------------------------------------------------------------------------
248 // Returns the number of buttons in the radiobox
249 //
250 // inline defined
251 //
252
253 //-------------------------------------------------------------------------------------
254 // ¥ SetLabel(const wxString&)
255 //-------------------------------------------------------------------------------------
256 // Sets the radiobox label
257
258 void wxRadioBox::SetLabel(const wxString& label)
259 {
260 return wxControl::SetLabel(label);
261 }
262
263 //-------------------------------------------------------------------------------------
264 // ¥ SetLabel(int, const wxString&)
265 //-------------------------------------------------------------------------------------
266 // Sets the label of a given button
267
268 void wxRadioBox::SetString(int item,const wxString& label)
269 {
270 int i;
271 wxRadioButton *current;
272
273 if (!IsValid(item))
274 return;
275 i=0;
276 current=m_radioButtonCycle;
277 while (i!=item) {
278 i++;
279 current=current->NextInCycle();
280 }
281 return current->SetLabel(label);
282 }
283
284 //-------------------------------------------------------------------------------------
285 // ¥ SetSelection
286 //-------------------------------------------------------------------------------------
287 // Sets a button by passing the desired position. This does not cause
288 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
289
290 void wxRadioBox::SetSelection(int item)
291 {
292 int i;
293 wxRadioButton *current;
294
295 if (!IsValid(item))
296 return;
297 i=0;
298 current=m_radioButtonCycle;
299 while (i!=item) {
300 i++;
301 current=current->NextInCycle();
302 }
303 current->SetValue(true);
304
305 }
306
307 //-------------------------------------------------------------------------------------
308 // ¥ Show(bool)
309 //-------------------------------------------------------------------------------------
310 // Shows or hides the entire radiobox
311
312 bool wxRadioBox::Show(bool show)
313 {
314 wxRadioButton *current;
315
316 wxControl::Show(show);
317
318 current=m_radioButtonCycle;
319 for (size_t 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 size_t 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 (size_t 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 }
532
533 #endif // wxUSE_RADIOBOX