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