]>
Commit | Line | Data |
---|---|---|
e9576ca5 | 1 | ///////////////////////////////////////////////////////////////////////////// |
fb5246be | 2 | // Name: src/mac/carbon/radiobox.cpp |
e9576ca5 | 3 | // Purpose: wxRadioBox |
a31a5f85 | 4 | // Author: Stefan Csomor |
1395ff56 | 5 | // Modified by: JS Lair (99/11/15) first implementation |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
8228b893 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
3d1a4878 SC |
12 | #include "wx/wxprec.h" |
13 | ||
179e085f RN |
14 | #if wxUSE_RADIOBOX |
15 | ||
584ad2a3 | 16 | #include "wx/arrstr.h" |
e9576ca5 | 17 | #include "wx/radiobox.h" |
03e11df5 GD |
18 | #include "wx/radiobut.h" |
19 | #include "wx/mac/uma.h" | |
e9576ca5 | 20 | |
e9576ca5 | 21 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl) |
e9576ca5 | 22 | |
43524b15 | 23 | |
cf1a9b45 | 24 | BEGIN_EVENT_TABLE(wxRadioBox, wxControl) |
6cb0bfdf | 25 | EVT_RADIOBUTTON( wxID_ANY , wxRadioBox::OnRadioButton ) |
cf1a9b45 SC |
26 | END_EVENT_TABLE() |
27 | ||
43524b15 | 28 | |
cf1a9b45 SC |
29 | void wxRadioBox::OnRadioButton( wxCommandEvent &outer ) |
30 | { | |
e40298d5 JS |
31 | if ( outer.IsChecked() ) |
32 | { | |
43524b15 | 33 | wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId ); |
e40298d5 JS |
34 | int i = GetSelection() ; |
35 | event.SetInt( i ); | |
36 | event.SetString( GetString( i ) ); | |
37 | event.SetEventObject( this ); | |
43524b15 | 38 | ProcessCommand( event ); |
e40298d5 | 39 | } |
cf1a9b45 | 40 | } |
1395ff56 | 41 | |
e9576ca5 SC |
42 | wxRadioBox::wxRadioBox() |
43 | { | |
e9576ca5 SC |
44 | m_noItems = 0; |
45 | m_noRowsOrCols = 0; | |
1395ff56 UJ |
46 | m_radioButtonCycle = NULL; |
47 | } | |
48 | ||
1395ff56 UJ |
49 | wxRadioBox::~wxRadioBox() |
50 | { | |
1a87edf2 | 51 | m_isBeingDeleted = true; |
140f2012 | 52 | |
43524b15 DS |
53 | wxRadioButton *next, *current; |
54 | ||
55 | current = m_radioButtonCycle->NextInCycle(); | |
56 | if (current != NULL) | |
57 | { | |
58 | while (current != m_radioButtonCycle) | |
59 | { | |
60 | next = current->NextInCycle(); | |
61 | delete current; | |
62 | ||
63 | current = next; | |
64 | } | |
1a87edf2 | 65 | |
e40298d5 | 66 | delete current; |
e40298d5 | 67 | } |
e9576ca5 SC |
68 | } |
69 | ||
1395ff56 UJ |
70 | // Create the radiobox for two-step construction |
71 | ||
43524b15 DS |
72 | bool wxRadioBox::Create( wxWindow *parent, |
73 | wxWindowID id, const wxString& label, | |
74 | const wxPoint& pos, const wxSize& size, | |
75 | const wxArrayString& choices, | |
76 | int majorDim, long style, | |
77 | const wxValidator& val, const wxString& name ) | |
584ad2a3 MB |
78 | { |
79 | wxCArrayString chs(choices); | |
80 | ||
43524b15 DS |
81 | return Create( |
82 | parent, id, label, pos, size, chs.GetCount(), | |
83 | chs.GetStrings(), majorDim, style, val, name); | |
584ad2a3 MB |
84 | } |
85 | ||
43524b15 DS |
86 | bool wxRadioBox::Create( wxWindow *parent, |
87 | wxWindowID id, const wxString& label, | |
88 | const wxPoint& pos, const wxSize& size, | |
89 | int n, const wxString choices[], | |
90 | int majorDim, long style, | |
91 | const wxValidator& val, const wxString& name ) | |
e9576ca5 | 92 | { |
1a87edf2 WS |
93 | m_macIsUserPane = false ; |
94 | ||
43524b15 | 95 | if ( !wxControl::Create( parent, id, pos, size, style, val, name ) ) |
b45ed7a2 VZ |
96 | return false; |
97 | ||
1395ff56 | 98 | int i; |
1a87edf2 | 99 | |
8228b893 | 100 | m_noItems = (size_t)n; |
e9576ca5 | 101 | m_noRowsOrCols = majorDim; |
1395ff56 | 102 | m_radioButtonCycle = NULL; |
1a87edf2 | 103 | |
43524b15 | 104 | SetMajorDim( majorDim == 0 ? n : majorDim, style ); |
1a87edf2 | 105 | |
c18353e5 | 106 | m_label = label; |
facd6764 | 107 | |
43524b15 DS |
108 | Rect bounds = wxMacGetBoundsForControl( this, pos, size ); |
109 | if ( bounds.right <= bounds.left ) | |
110 | bounds.right = bounds.left + 100; | |
facd6764 | 111 | if ( bounds.bottom <= bounds.top ) |
43524b15 | 112 | bounds.bottom = bounds.top + 100; |
1a87edf2 | 113 | |
43524b15 | 114 | m_peer = new wxMacControl( this ); |
1a87edf2 | 115 | |
43524b15 DS |
116 | OSStatus err = CreateGroupBoxControl( |
117 | MAC_WXHWND(parent->MacGetTopLevelWindowRef()), | |
118 | &bounds, CFSTR("") , true /*primary*/, | |
119 | m_peer->GetControlRefAddr() ); | |
120 | verify_noerr( err ); | |
1a87edf2 | 121 | |
1395ff56 UJ |
122 | for (i = 0; i < n; i++) |
123 | { | |
43524b15 DS |
124 | wxRadioButton *radBtn = new wxRadioButton( |
125 | this, | |
126 | wxID_ANY, | |
127 | wxStripMenuCodes(choices[i]), | |
128 | wxPoint( 5, 20 * i + 10 ), | |
129 | wxDefaultSize, | |
130 | i == 0 ? wxRB_GROUP : 0 ); | |
131 | ||
0a67a93b | 132 | if ( i == 0 ) |
43524b15 DS |
133 | m_radioButtonCycle = radBtn; |
134 | // m_radioButtonCycle = radBtn->AddInCycle( m_radioButtonCycle ); | |
1395ff56 | 135 | } |
1a87edf2 | 136 | |
43524b15 DS |
137 | SetSelection( 0 ); |
138 | MacPostControlCreate( pos, size ); | |
1a87edf2 WS |
139 | |
140 | return true; | |
e9576ca5 SC |
141 | } |
142 | ||
1395ff56 | 143 | // Enables or disables the entire radiobox |
43524b15 | 144 | // |
5008c64c | 145 | bool wxRadioBox::Enable(bool enable) |
1395ff56 | 146 | { |
1395ff56 | 147 | wxRadioButton *current; |
1a87edf2 | 148 | |
43524b15 | 149 | if (!wxControl::Enable( enable )) |
e40298d5 | 150 | return false; |
1a87edf2 | 151 | |
371fd015 | 152 | current = m_radioButtonCycle; |
8228b893 | 153 | for (size_t i = 0; i < m_noItems; i++) |
43524b15 DS |
154 | { |
155 | current->Enable( enable ); | |
e40298d5 | 156 | current = current->NextInCycle(); |
371fd015 | 157 | } |
43524b15 | 158 | |
371fd015 | 159 | return true; |
1395ff56 UJ |
160 | } |
161 | ||
1395ff56 | 162 | // Enables or disables an given button |
43524b15 | 163 | // |
1a87edf2 | 164 | bool wxRadioBox::Enable(int item, bool enable) |
e9576ca5 | 165 | { |
371fd015 | 166 | int i; |
1395ff56 | 167 | wxRadioButton *current; |
1a87edf2 | 168 | |
43524b15 | 169 | if (!IsValid( item )) |
1a87edf2 WS |
170 | return false; |
171 | ||
371fd015 GD |
172 | i = 0; |
173 | current = m_radioButtonCycle; | |
43524b15 DS |
174 | while (i != item) |
175 | { | |
e40298d5 JS |
176 | i++; |
177 | current = current->NextInCycle(); | |
371fd015 | 178 | } |
43524b15 DS |
179 | |
180 | return current->Enable( enable ); | |
e9576ca5 SC |
181 | } |
182 | ||
1395ff56 | 183 | // Returns the radiobox label |
43524b15 | 184 | // |
1395ff56 | 185 | wxString wxRadioBox::GetLabel() const |
e9576ca5 | 186 | { |
1395ff56 UJ |
187 | return wxControl::GetLabel(); |
188 | } | |
e9576ca5 | 189 | |
1395ff56 | 190 | // Returns the label for the given button |
43524b15 | 191 | // |
90b959ae | 192 | wxString wxRadioBox::GetString(int item) const |
1395ff56 | 193 | { |
371fd015 | 194 | int i; |
1395ff56 | 195 | wxRadioButton *current; |
1a87edf2 | 196 | |
43524b15 | 197 | if (!IsValid( item )) |
427ff662 | 198 | return wxEmptyString; |
1a87edf2 | 199 | |
371fd015 GD |
200 | i = 0; |
201 | current = m_radioButtonCycle; | |
43524b15 DS |
202 | while (i != item) |
203 | { | |
e40298d5 JS |
204 | i++; |
205 | current = current->NextInCycle(); | |
371fd015 | 206 | } |
43524b15 | 207 | |
1395ff56 | 208 | return current->GetLabel(); |
e9576ca5 SC |
209 | } |
210 | ||
1395ff56 | 211 | // Returns the zero-based position of the selected button |
43524b15 | 212 | // |
e9576ca5 SC |
213 | int wxRadioBox::GetSelection() const |
214 | { | |
1395ff56 UJ |
215 | int i; |
216 | wxRadioButton *current; | |
1a87edf2 | 217 | |
43524b15 DS |
218 | i = 0; |
219 | current = m_radioButtonCycle; | |
220 | while (!current->GetValue()) | |
221 | { | |
e40298d5 | 222 | i++; |
43524b15 | 223 | current = current->NextInCycle(); |
e40298d5 | 224 | } |
1a87edf2 | 225 | |
1395ff56 | 226 | return i; |
e9576ca5 SC |
227 | } |
228 | ||
1395ff56 | 229 | // Sets the radiobox label |
43524b15 | 230 | // |
1395ff56 | 231 | void wxRadioBox::SetLabel(const wxString& label) |
e9576ca5 | 232 | { |
43524b15 | 233 | return wxControl::SetLabel( label ); |
e9576ca5 SC |
234 | } |
235 | ||
1395ff56 | 236 | // Sets the label of a given button |
43524b15 | 237 | // |
90b959ae | 238 | void wxRadioBox::SetString(int item,const wxString& label) |
e9576ca5 | 239 | { |
43524b15 | 240 | int i; |
1395ff56 | 241 | wxRadioButton *current; |
1a87edf2 | 242 | |
43524b15 | 243 | if (!IsValid( item )) |
1395ff56 | 244 | return; |
43524b15 DS |
245 | |
246 | i = 0; | |
247 | current = m_radioButtonCycle; | |
248 | while (i != item) | |
249 | { | |
e40298d5 | 250 | i++; |
43524b15 | 251 | current = current->NextInCycle(); |
e40298d5 | 252 | } |
43524b15 DS |
253 | |
254 | return current->SetLabel( label ); | |
e9576ca5 SC |
255 | } |
256 | ||
1a87edf2 | 257 | // Sets a button by passing the desired position. This does not cause |
1395ff56 | 258 | // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted |
43524b15 | 259 | // |
1395ff56 | 260 | void wxRadioBox::SetSelection(int item) |
e9576ca5 | 261 | { |
1395ff56 UJ |
262 | int i; |
263 | wxRadioButton *current; | |
1a87edf2 | 264 | |
43524b15 | 265 | if (!IsValid( item )) |
1395ff56 | 266 | return; |
43524b15 DS |
267 | |
268 | i = 0; | |
269 | current = m_radioButtonCycle; | |
270 | while (i != item) | |
271 | { | |
e40298d5 | 272 | i++; |
43524b15 | 273 | current = current->NextInCycle(); |
e40298d5 | 274 | } |
1a87edf2 | 275 | |
43524b15 | 276 | current->SetValue( true ); |
e9576ca5 SC |
277 | } |
278 | ||
1a87edf2 | 279 | // Shows or hides the entire radiobox |
43524b15 | 280 | // |
e9576ca5 SC |
281 | bool wxRadioBox::Show(bool show) |
282 | { | |
1395ff56 | 283 | wxRadioButton *current; |
1a87edf2 | 284 | |
43524b15 | 285 | current = m_radioButtonCycle; |
8228b893 | 286 | for (size_t i=0; i<m_noItems; i++) |
6cb0bfdf | 287 | { |
43524b15 DS |
288 | current->Show( show ); |
289 | current = current->NextInCycle(); | |
e40298d5 | 290 | } |
43524b15 DS |
291 | |
292 | wxControl::Show( show ); | |
293 | ||
1395ff56 | 294 | return true; |
e9576ca5 SC |
295 | } |
296 | ||
1a87edf2 | 297 | // Shows or hides the given button |
43524b15 | 298 | // |
6cb0bfdf | 299 | bool wxRadioBox::Show(int item, bool show) |
e9576ca5 | 300 | { |
6cb0bfdf | 301 | int i; |
1395ff56 | 302 | wxRadioButton *current; |
1a87edf2 | 303 | |
43524b15 | 304 | if (!IsValid( item )) |
6cb0bfdf | 305 | return false; |
43524b15 DS |
306 | |
307 | i = 0; | |
308 | current = m_radioButtonCycle; | |
309 | while (i != item) | |
310 | { | |
e40298d5 | 311 | i++; |
43524b15 | 312 | current = current->NextInCycle(); |
e40298d5 | 313 | } |
43524b15 DS |
314 | |
315 | return current->Show( show ); | |
e9576ca5 SC |
316 | } |
317 | ||
1395ff56 | 318 | // Simulates the effect of the user issuing a command to the item |
43524b15 DS |
319 | // |
320 | void wxRadioBox::Command( wxCommandEvent& event ) | |
e9576ca5 | 321 | { |
43524b15 DS |
322 | SetSelection( event.GetInt() ); |
323 | ProcessCommand( event ); | |
e9576ca5 SC |
324 | } |
325 | ||
1395ff56 | 326 | // Sets the selected button to receive keyboard input |
43524b15 | 327 | // |
1395ff56 | 328 | void wxRadioBox::SetFocus() |
e9576ca5 | 329 | { |
1395ff56 UJ |
330 | int i; |
331 | wxRadioButton *current; | |
1a87edf2 | 332 | |
43524b15 DS |
333 | i = 0; |
334 | current = m_radioButtonCycle; | |
335 | while (!current->GetValue()) | |
336 | { | |
e40298d5 | 337 | i++; |
43524b15 | 338 | current = current->NextInCycle(); |
e40298d5 | 339 | } |
43524b15 | 340 | |
e40298d5 | 341 | current->SetFocus(); |
e9576ca5 SC |
342 | } |
343 | ||
1395ff56 | 344 | // Simulates the effect of the user issuing a command to the item |
43524b15 | 345 | // |
baf34314 | 346 | #define RADIO_SIZE 20 |
1395ff56 UJ |
347 | |
348 | void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
e9576ca5 | 349 | { |
e40298d5 JS |
350 | int i; |
351 | wxRadioButton *current; | |
1a87edf2 | 352 | |
e40298d5 | 353 | // define the position |
1a87edf2 | 354 | |
e40298d5 | 355 | int x_current, y_current; |
43524b15 | 356 | int x_offset, y_offset; |
8208e181 | 357 | int widthOld, heightOld; |
43524b15 DS |
358 | |
359 | GetSize( &widthOld, &heightOld ); | |
360 | GetPosition( &x_current, &y_current ); | |
1a87edf2 | 361 | |
e40298d5 JS |
362 | x_offset = x; |
363 | y_offset = y; | |
43524b15 DS |
364 | if (!(sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
365 | { | |
366 | if (x == wxDefaultCoord) | |
367 | x_offset = x_current; | |
368 | if (y == wxDefaultCoord) | |
369 | y_offset = y_current; | |
370 | } | |
1a87edf2 | 371 | |
e40298d5 | 372 | // define size |
43524b15 DS |
373 | int charWidth, charHeight; |
374 | int maxWidth, maxHeight; | |
375 | int eachWidth[128], eachHeight[128]; | |
376 | int totWidth, totHeight; | |
1a87edf2 | 377 | |
43524b15 DS |
378 | GetTextExtent( |
379 | wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), | |
380 | &charWidth, &charHeight ); | |
facd6764 | 381 | |
43524b15 | 382 | charWidth /= 52; |
1a87edf2 | 383 | |
43524b15 DS |
384 | maxWidth = -1; |
385 | maxHeight = -1; | |
8228b893 | 386 | for (size_t i = 0 ; i < m_noItems; i++) |
e40298d5 | 387 | { |
43524b15 | 388 | GetTextExtent( GetString( i ), &eachWidth[i], &eachHeight[i] ); |
e40298d5 | 389 | eachWidth[i] = (int)(eachWidth[i] + RADIO_SIZE); |
43524b15 DS |
390 | eachHeight[i] = (int)((3 * eachHeight[i]) / 2); |
391 | ||
392 | if (maxWidth < eachWidth[i]) | |
393 | maxWidth = eachWidth[i]; | |
394 | if (maxHeight < eachHeight[i]) | |
395 | maxHeight = eachHeight[i]; | |
6cb0bfdf | 396 | } |
1a87edf2 | 397 | |
43524b15 DS |
398 | totHeight = GetRowCount() * maxHeight; |
399 | totWidth = GetColumnCount() * (maxWidth + charWidth); | |
facd6764 | 400 | |
43524b15 | 401 | wxSize sz = DoGetSizeFromClientSize( wxSize( totWidth, totHeight ) ) ; |
1a87edf2 | 402 | |
43524b15 | 403 | // change the width / height only when specified |
6cb0bfdf | 404 | if ( width == wxDefaultCoord ) |
8208e181 SC |
405 | { |
406 | if ( sizeFlags & wxSIZE_AUTO_WIDTH ) | |
43524b15 | 407 | width = sz.x; |
8208e181 SC |
408 | else |
409 | width = widthOld; | |
410 | } | |
1a87edf2 | 411 | |
6cb0bfdf | 412 | if ( height == wxDefaultCoord ) |
8208e181 SC |
413 | { |
414 | if ( sizeFlags & wxSIZE_AUTO_HEIGHT ) | |
43524b15 | 415 | height = sz.y; |
8208e181 SC |
416 | else |
417 | height = heightOld; | |
418 | } | |
1a87edf2 | 419 | |
43524b15 | 420 | wxControl::DoSetSize( x_offset, y_offset, width, height, wxSIZE_AUTO ); |
1a87edf2 | 421 | |
43524b15 DS |
422 | // arrange radio buttons |
423 | int x_start, y_start; | |
1a87edf2 | 424 | |
facd6764 | 425 | x_start = 0; |
43524b15 | 426 | y_start = 0; |
facd6764 | 427 | |
e40298d5 JS |
428 | x_offset = x_start; |
429 | y_offset = y_start; | |
1a87edf2 | 430 | |
43524b15 | 431 | current = m_radioButtonCycle; |
c18353e5 | 432 | for (i = 0 ; i < (int)m_noItems; i++) |
e40298d5 | 433 | { |
43524b15 DS |
434 | // not to do for the zero button! |
435 | if ((i > 0) && ((i % GetMajorDim()) == 0)) | |
e40298d5 | 436 | { |
3413cead | 437 | if (m_windowStyle & wxRA_SPECIFY_ROWS) |
e40298d5 JS |
438 | { |
439 | x_offset += maxWidth + charWidth; | |
440 | y_offset = y_start; | |
441 | } | |
442 | else | |
443 | { | |
444 | x_offset = x_start; | |
43524b15 | 445 | y_offset += maxHeight ; //+ charHeight / 2 |
e40298d5 JS |
446 | } |
447 | } | |
1a87edf2 | 448 | |
43524b15 DS |
449 | current->SetSize( x_offset, y_offset, eachWidth[i], eachHeight[i]); |
450 | current = current->NextInCycle(); | |
1a87edf2 | 451 | |
e40298d5 | 452 | if (m_windowStyle & wxRA_SPECIFY_ROWS) |
43524b15 | 453 | y_offset += maxHeight ; // + charHeight / 2 |
e40298d5 JS |
454 | else |
455 | x_offset += maxWidth + charWidth; | |
456 | } | |
e9576ca5 SC |
457 | } |
458 | ||
9453cf2b SC |
459 | wxSize wxRadioBox::DoGetBestSize() const |
460 | { | |
461 | int charWidth, charHeight; | |
462 | int maxWidth, maxHeight; | |
463 | int eachWidth, eachHeight; | |
464 | int totWidth, totHeight; | |
1a87edf2 | 465 | |
43524b15 DS |
466 | wxFont font = GetFont(); // GetParent()->GetFont() |
467 | GetTextExtent( | |
468 | wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), | |
469 | &charWidth, &charHeight, NULL, NULL, &font ); | |
facd6764 | 470 | |
9453cf2b | 471 | charWidth /= 52; |
1a87edf2 | 472 | |
e40298d5 JS |
473 | maxWidth = -1; |
474 | maxHeight = -1; | |
1a87edf2 | 475 | |
8228b893 | 476 | for (size_t i = 0 ; i < m_noItems; i++) |
e40298d5 | 477 | { |
43524b15 DS |
478 | GetTextExtent( GetString( i ), &eachWidth, &eachHeight, NULL, NULL, &font ); |
479 | eachWidth = (int)(eachWidth + RADIO_SIZE); | |
9453cf2b | 480 | eachHeight = (int)((3 * eachHeight) / 2); |
43524b15 DS |
481 | if (maxWidth < eachWidth) |
482 | maxWidth = eachWidth; | |
483 | if (maxHeight < eachHeight) | |
484 | maxHeight = eachHeight; | |
9453cf2b | 485 | } |
1a87edf2 | 486 | |
43524b15 DS |
487 | totHeight = GetRowCount() * maxHeight; |
488 | totWidth = GetColumnCount() * (maxWidth + charWidth); | |
1a87edf2 | 489 | |
43524b15 DS |
490 | wxSize sz = DoGetSizeFromClientSize( wxSize( totWidth, totHeight ) ); |
491 | totWidth = sz.x; | |
492 | totHeight = sz.y; | |
1a87edf2 | 493 | |
c5bdc14f | 494 | // handle radio box title as well |
43524b15 DS |
495 | GetTextExtent( GetLabel(), &eachWidth, NULL ); |
496 | eachWidth = (int)(eachWidth + RADIO_SIZE) + 3 * charWidth; | |
1a87edf2 | 497 | if (totWidth < eachWidth) |
c5bdc14f | 498 | totWidth = eachWidth; |
1a87edf2 | 499 | |
43524b15 | 500 | return wxSize( totWidth, totHeight ); |
9453cf2b | 501 | } |
ec7a8d02 | 502 | |
8228b893 | 503 | #endif // wxUSE_RADIOBOX |