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