]> git.saurik.com Git - wxWidgets.git/blob - src/mac/radiobox.cpp
renamed port to session for Carbon printing
[wxWidgets.git] / src / mac / 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 //-------------------------------------------------------------------------------------
186 // ¥ FindString
187 //-------------------------------------------------------------------------------------
188 // Finds a button matching the given string, returning the position if found
189 // or -1 if not found
190
191 int wxRadioBox::FindString(const wxString& s) const
192 {
193 int i;
194 wxRadioButton *current;
195
196 current=m_radioButtonCycle;
197 for (i = 0; i < m_noItems; i++)
198 {
199 if (s == current->GetLabel())
200 return i;
201 current=current->NextInCycle();
202 }
203 return -1;
204 }
205
206 //-------------------------------------------------------------------------------------
207 // ¥ GetLabel()
208 //-------------------------------------------------------------------------------------
209 // Returns the radiobox label
210
211 wxString wxRadioBox::GetLabel() const
212 {
213 return wxControl::GetLabel();
214 }
215
216 //-------------------------------------------------------------------------------------
217 // ¥ GetLabel(int)
218 //-------------------------------------------------------------------------------------
219 // Returns the label for the given button
220
221 wxString wxRadioBox::GetLabel(int item) const
222 {
223 int i;
224 wxRadioButton *current;
225
226 if ((item < 0) || (item >= m_noItems))
227 return wxString("");
228 i=0;
229 current=m_radioButtonCycle;
230 while (i!=item) {
231 i++;
232 current=current->NextInCycle();
233 }
234 return current->GetLabel();
235 }
236
237 //-------------------------------------------------------------------------------------
238 // ¥ GetSelection
239 //-------------------------------------------------------------------------------------
240 // Returns the zero-based position of the selected button
241
242 int wxRadioBox::GetSelection() const
243 {
244 int i;
245 wxRadioButton *current;
246
247 i=0;
248 current=m_radioButtonCycle;
249 while (!current->GetValue()) {
250 i++;
251 current=current->NextInCycle();
252 }
253
254 return i;
255 }
256
257 //-------------------------------------------------------------------------------------
258 // ¥ GetString
259 //-------------------------------------------------------------------------------------
260 // Find string for position
261
262 wxString wxRadioBox::GetString(int item) const
263 {
264
265 return GetLabel(item);
266 }
267
268 //-------------------------------------------------------------------------------------
269 // ¥ GetStringSelection
270 //-------------------------------------------------------------------------------------
271 // Returns the selected string
272
273 wxString wxRadioBox::GetStringSelection () const
274 {
275 int sel = GetSelection ();
276 if (sel > -1)
277 return this->GetString (sel);
278 else
279 return wxString("");
280 }
281
282 //-------------------------------------------------------------------------------------
283 // ¥ Number
284 //-------------------------------------------------------------------------------------
285 // Returns the number of buttons in the radiobox
286 //
287 // inline defined
288 //
289
290 //-------------------------------------------------------------------------------------
291 // ¥ SetLabel(const wxString&)
292 //-------------------------------------------------------------------------------------
293 // Sets the radiobox label
294
295 void wxRadioBox::SetLabel(const wxString& label)
296 {
297 return wxControl::SetLabel(label);
298 }
299
300 //-------------------------------------------------------------------------------------
301 // ¥ SetLabel(int, const wxString&)
302 //-------------------------------------------------------------------------------------
303 // Sets the label of a given button
304
305 void wxRadioBox::SetLabel(int item,const wxString& label)
306 {
307 int i;
308 wxRadioButton *current;
309
310 if ((item < 0) || (item >= m_noItems))
311 return;
312 i=0;
313 current=m_radioButtonCycle;
314 while (i!=item) {
315 i++;
316 current=current->NextInCycle();
317 }
318 return current->SetLabel(label);
319 }
320
321 //-------------------------------------------------------------------------------------
322 // ¥ SetSelection
323 //-------------------------------------------------------------------------------------
324 // Sets a button by passing the desired position. This does not cause
325 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
326
327 void wxRadioBox::SetSelection(int item)
328 {
329 int i;
330 wxRadioButton *current;
331
332 if ((item < 0) || (item >= m_noItems))
333 return;
334 i=0;
335 current=m_radioButtonCycle;
336 while (i!=item) {
337 i++;
338 current=current->NextInCycle();
339 }
340 current->SetValue(true);
341
342 }
343
344 //-------------------------------------------------------------------------------------
345 // ¥ SetStringSelection
346 //-------------------------------------------------------------------------------------
347 // Sets a button by passing the desired string. This does not cause
348 // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
349
350 bool wxRadioBox::SetStringSelection (const wxString& s)
351 {
352 int sel = FindString (s);
353 if (sel > -1)
354 {
355 SetSelection (sel);
356 return TRUE;
357 }
358 else
359 return FALSE;
360 }
361
362 //-------------------------------------------------------------------------------------
363 // ¥ Show(bool)
364 //-------------------------------------------------------------------------------------
365 // Shows or hides the entire radiobox
366
367 bool wxRadioBox::Show(bool show)
368 {
369 int i;
370 wxRadioButton *current;
371
372 wxControl::Show(show);
373
374 current=m_radioButtonCycle;
375 for (i=0;i<m_noItems;i++) {
376 current->Show(show);
377 current=current->NextInCycle();
378 }
379 return true;
380 }
381
382 //-------------------------------------------------------------------------------------
383 // ¥ Show(int, bool)
384 //-------------------------------------------------------------------------------------
385 // Shows or hides the given button
386
387 void wxRadioBox::Show(int item, bool show)
388 {
389 int i;
390 wxRadioButton *current;
391
392 if ((item < 0) || (item >= m_noItems))
393 return;
394 i=0;
395 current=m_radioButtonCycle;
396 while (i!=item) {
397 i++;
398 current=current->NextInCycle();
399 }
400 current->Show(show);
401 }
402
403 #pragma mark -
404 #pragma mark ### Other external functions ###
405
406 //-------------------------------------------------------------------------------------
407 // ¥ Command
408 //-------------------------------------------------------------------------------------
409 // Simulates the effect of the user issuing a command to the item
410
411 void wxRadioBox::Command (wxCommandEvent & event)
412 {
413 SetSelection (event.GetInt());
414 ProcessCommand (event);
415 }
416
417 //-------------------------------------------------------------------------------------
418 // ¥ SetFocus
419 //-------------------------------------------------------------------------------------
420 // Sets the selected button to receive keyboard input
421
422 void wxRadioBox::SetFocus()
423 {
424 int i;
425 wxRadioButton *current;
426
427 i=0;
428 current=m_radioButtonCycle;
429 while (!current->GetValue()) {
430 i++;
431 current=current->NextInCycle();
432 }
433 current->SetFocus();
434 }
435
436
437 #pragma mark -
438 #pragma mark ### Internal functions ###
439
440 //-------------------------------------------------------------------------------------
441 // ¥ DoSetSize
442 //-------------------------------------------------------------------------------------
443 // Simulates the effect of the user issuing a command to the item
444
445 #define RADIO_SIZE 40
446
447 void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
448 {
449 int i;
450 wxRadioButton *current;
451
452 // define the position
453
454 int x_current, y_current;
455 int x_offset,y_offset;
456 int widthOld, heightOld;
457 GetSize(&widthOld, &heightOld);
458
459 x_offset = x;
460 y_offset = y;
461 GetPosition(&x_current, &y_current);
462 if ((x == -1) && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
463 x_offset = x_current;
464 if ((y == -1)&& !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
465 y_offset = y_current;
466
467 // define size
468
469 int charWidth,charHeight;
470 int maxWidth,maxHeight;
471 int eachWidth[128],eachHeight[128];
472 int totWidth,totHeight;
473
474 SetFont(GetParent()->GetFont());
475 GetTextExtent(wxString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth, &charHeight);
476 charWidth/=52;
477
478 maxWidth=-1;
479 maxHeight=-1;
480 for (i = 0 ; i < m_noItems; i++)
481 {
482 GetTextExtent(GetLabel(i), &eachWidth[i], &eachHeight[i]);
483 eachWidth[i] = (int)(eachWidth[i] + RADIO_SIZE);
484 eachHeight[i] = (int)((3*eachHeight[i])/2);
485 if (maxWidth<eachWidth[i]) maxWidth = eachWidth[i];
486 if (maxHeight<eachHeight[i]) maxHeight = eachHeight[i];
487 }
488
489 totHeight = GetNumVer() * (maxHeight + charHeight/2) + charHeight*3/2;
490 totWidth = GetNumHor() * (maxWidth + charWidth) + charWidth;
491
492 // only change our width/height if asked for
493 if ( width == -1 )
494 {
495 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
496 width = totWidth ;
497 else
498 width = widthOld;
499 }
500
501 if ( height == -1 )
502 {
503 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
504 height = totHeight ;
505 else
506 height = heightOld;
507 }
508
509 wxControl::DoSetSize(x_offset,y_offset,width,height,wxSIZE_AUTO);
510
511 // arrange radiobuttons
512
513 int x_start,y_start;
514
515
516 x_start = charWidth;
517 y_start = 15 ;
518 x_offset = x_start;
519 y_offset = y_start;
520
521 current=m_radioButtonCycle;
522 for ( i = 0 ; i < m_noItems; i++)
523 {
524 if (i&&((i%m_majorDim)==0)) // not to do for the zero button!
525 {
526 if (m_windowStyle & wxRA_VERTICAL)
527 {
528 x_offset += maxWidth + charWidth;
529 y_offset = y_start;
530 }
531 else
532 {
533 x_offset = x_start;
534 y_offset += maxHeight ; /*+ charHeight/2;*/
535 }
536 }
537
538 current->SetSize(x_offset,y_offset,eachWidth[i],eachHeight[i]);
539 current=current->NextInCycle();
540
541 if (m_windowStyle & wxRA_SPECIFY_ROWS)
542 y_offset += maxHeight ; /*+ charHeight/2;*/
543 else
544 x_offset += maxWidth + charWidth;
545 }
546 }
547
548 //-------------------------------------------------------------------------------------
549 // ¥ GetNumVer
550 //-------------------------------------------------------------------------------------
551 // return the number of buttons in the vertical direction
552
553 int wxRadioBox::GetNumVer() const
554 {
555 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
556 {
557 return m_majorDim;
558 }
559 else
560 {
561 return (m_noItems + m_majorDim - 1)/m_majorDim;
562 }
563 }
564
565 //-------------------------------------------------------------------------------------
566 // ¥ GetNumHor
567 //-------------------------------------------------------------------------------------
568 // return the number of buttons in the horizontal direction
569
570 int wxRadioBox::GetNumHor() const
571 {
572 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
573 {
574 return (m_noItems + m_majorDim - 1)/m_majorDim;
575 }
576 else
577 {
578 return m_majorDim;
579 }
580 }
581
582
583
584
585