]> git.saurik.com Git - wxWidgets.git/blob - src/msw/radiobox.cpp
No changes, just use wxRecursionGuard instead of manual boolean flag.
[wxWidgets.git] / src / msw / radiobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/radiobox.cpp
3 // Purpose: wxRadioBox implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ===========================================================================
12 // declarations
13 // ===========================================================================
14
15 // ---------------------------------------------------------------------------
16 // headers
17 // ---------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_RADIOBOX
27
28 #include "wx/radiobox.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/hashmap.h"
32 #include "wx/bitmap.h"
33 #include "wx/brush.h"
34 #include "wx/settings.h"
35 #include "wx/log.h"
36 #endif
37
38 #include "wx/msw/subwin.h"
39
40 #if wxUSE_TOOLTIPS
41 #include "wx/tooltip.h"
42 #endif // wxUSE_TOOLTIPS
43
44 // TODO: wxCONSTRUCTOR
45 #if 0 // wxUSE_EXTENDED_RTTI
46 WX_DEFINE_FLAGS( wxRadioBoxStyle )
47
48 wxBEGIN_FLAGS( wxRadioBoxStyle )
49 // new style border flags, we put them first to
50 // use them for streaming out
51 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
52 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
53 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
54 wxFLAGS_MEMBER(wxBORDER_RAISED)
55 wxFLAGS_MEMBER(wxBORDER_STATIC)
56 wxFLAGS_MEMBER(wxBORDER_NONE)
57
58 // old style border flags
59 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
60 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
61 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
62 wxFLAGS_MEMBER(wxRAISED_BORDER)
63 wxFLAGS_MEMBER(wxSTATIC_BORDER)
64 wxFLAGS_MEMBER(wxBORDER)
65
66 // standard window styles
67 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
68 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
69 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
70 wxFLAGS_MEMBER(wxWANTS_CHARS)
71 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
72 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
73 wxFLAGS_MEMBER(wxVSCROLL)
74 wxFLAGS_MEMBER(wxHSCROLL)
75
76 wxFLAGS_MEMBER(wxRA_SPECIFY_COLS)
77 wxFLAGS_MEMBER(wxRA_SPECIFY_ROWS)
78 wxEND_FLAGS( wxRadioBoxStyle )
79
80 IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioBox, wxControl,"wx/radiobox.h")
81
82 wxBEGIN_PROPERTIES_TABLE(wxRadioBox)
83 wxEVENT_PROPERTY( Select , wxEVT_RADIOBOX , wxCommandEvent )
84 wxPROPERTY_FLAGS( WindowStyle , wxRadioBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
85 wxEND_PROPERTIES_TABLE()
86
87 #else
88 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
89 #endif
90
91 /*
92 selection
93 content
94 label
95 dimension
96 item
97 */
98
99 // ---------------------------------------------------------------------------
100 // private functions
101 // ---------------------------------------------------------------------------
102
103 // wnd proc for radio buttons
104 LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hWnd,
105 UINT message,
106 WPARAM wParam,
107 LPARAM lParam);
108
109 // ---------------------------------------------------------------------------
110 // global vars
111 // ---------------------------------------------------------------------------
112
113 namespace
114 {
115
116 // the pointer to standard radio button wnd proc
117 WXFARPROC s_wndprocRadioBtn = (WXFARPROC)NULL;
118
119 // Hash allowing to find wxRadioBox containing the given radio button by its
120 // HWND. This is used by (subclassed) radio button window proc to find the
121 // radio box it belongs to.
122 WX_DECLARE_HASH_MAP(HWND, wxRadioBox *,
123 wxPointerHash, wxPointerEqual,
124 RadioBoxFromButton);
125
126 RadioBoxFromButton gs_boxFromButton;
127
128 } // anonymous namespace
129
130 // ===========================================================================
131 // implementation
132 // ===========================================================================
133
134 /* static */
135 wxRadioBox* wxRadioBox::GetFromRadioButtonHWND(WXHWND hwnd)
136 {
137 const RadioBoxFromButton::const_iterator it = gs_boxFromButton.find(hwnd);
138 return it == gs_boxFromButton.end() ? NULL : it->second;
139 }
140
141 // ---------------------------------------------------------------------------
142 // wxRadioBox creation
143 // ---------------------------------------------------------------------------
144
145 // Radio box item
146 void wxRadioBox::Init()
147 {
148 m_selectedButton = wxNOT_FOUND;
149 m_radioButtons = NULL;
150 m_dummyHwnd = NULL;
151 m_radioWidth = NULL;
152 m_radioHeight = NULL;
153 }
154
155 bool wxRadioBox::Create(wxWindow *parent,
156 wxWindowID id,
157 const wxString& title,
158 const wxPoint& pos,
159 const wxSize& size,
160 int n,
161 const wxString choices[],
162 int majorDim,
163 long style,
164 const wxValidator& val,
165 const wxString& name)
166 {
167 // common initialization
168 if ( !wxStaticBox::Create(parent, id, title, pos, size, style, name) )
169 return false;
170
171 // the code elsewhere in this file supposes that either wxRA_SPECIFY_COLS
172 // or wxRA_SPECIFY_ROWS is set, ensure that this is indeed the case
173 if ( !(style & (wxRA_SPECIFY_ROWS | wxRA_SPECIFY_COLS)) )
174 style |= wxRA_SPECIFY_COLS;
175
176 #if wxUSE_VALIDATORS
177 SetValidator(val);
178 #else
179 wxUnusedVar(val);
180 #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS
181
182 // We need an extra one to keep track of the 'dummy' item we
183 // create to end the radio group, so it will be destroyed and
184 // it's id will be released. But we want it separate from the
185 // other buttons since the wxSubwindows will operate on it as
186 // well and we just want to ignore it until destroying it.
187 // For instance, we don't want the bounding box of the radio
188 // buttons to include the dummy button
189 m_radioButtons = new wxSubwindows(n);
190
191 m_radioWidth = new int[n];
192 m_radioHeight = new int[n];
193
194 for ( int i = 0; i < n; i++ )
195 {
196 m_radioWidth[i] =
197 m_radioHeight[i] = wxDefaultCoord;
198 long styleBtn = BS_AUTORADIOBUTTON | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
199 if ( i == 0 )
200 styleBtn |= WS_GROUP;
201
202 wxWindowIDRef subid = NewControlId();
203
204 HWND hwndBtn = ::CreateWindow(wxT("BUTTON"),
205 choices[i].t_str(),
206 styleBtn,
207 0, 0, 0, 0, // will be set in SetSize()
208 GetHwndOf(parent),
209 (HMENU)wxUIntToPtr(subid.GetValue()),
210 wxGetInstance(),
211 NULL);
212
213 if ( !hwndBtn )
214 {
215 wxLogLastError(wxT("CreateWindow(radio btn)"));
216
217 return false;
218 }
219
220 // Keep track of the subwindow
221 m_radioButtons->Set(i, hwndBtn, subid);
222
223 SubclassRadioButton((WXHWND)hwndBtn);
224
225 // Also, make it a subcontrol of this control
226 m_subControls.Add(subid);
227 }
228
229 // Create a dummy radio control to end the group.
230 m_dummyId = NewControlId();
231
232 m_dummyHwnd = (WXHWND)::CreateWindow(wxT("BUTTON"),
233 wxEmptyString,
234 WS_GROUP | BS_AUTORADIOBUTTON | WS_CHILD,
235 0, 0, 0, 0, GetHwndOf(parent),
236 (HMENU)wxUIntToPtr(m_dummyId.GetValue()),
237 wxGetInstance(), NULL);
238
239
240 m_radioButtons->SetFont(GetFont());
241
242 #ifdef __WXWINCE__
243 // Set the z-order correctly
244 SetWindowPos(GetHwnd(), HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
245 #endif
246
247 SetMajorDim(majorDim == 0 ? n : majorDim, style);
248 SetSelection(0);
249 SetSize(pos.x, pos.y, size.x, size.y);
250
251 // Now that we have items determine what is the best size and set it.
252 SetInitialSize(size);
253
254 // And update all the buttons positions to match it.
255 const wxSize actualSize = GetSize();
256 PositionAllButtons(pos.x, pos.y, actualSize.x, actualSize.y);
257
258 // The base wxStaticBox class never accepts focus, but we do because giving
259 // focus to a wxRadioBox actually gives it to one of its buttons, which are
260 // not visible at wx level and hence are not taken into account by the
261 // logic in wxControlContainer code.
262 m_container.EnableSelfFocus();
263
264 return true;
265 }
266
267 bool wxRadioBox::Create(wxWindow *parent,
268 wxWindowID id,
269 const wxString& title,
270 const wxPoint& pos,
271 const wxSize& size,
272 const wxArrayString& choices,
273 int majorDim,
274 long style,
275 const wxValidator& val,
276 const wxString& name)
277 {
278 wxCArrayString chs(choices);
279 return Create(parent, id, title, pos, size, chs.GetCount(),
280 chs.GetStrings(), majorDim, style, val, name);
281 }
282
283 wxRadioBox::~wxRadioBox()
284 {
285 SendDestroyEvent();
286
287 // Unsubclass all the radio buttons and remove their soon-to-be-invalid
288 // HWNDs from the global map. Notice that we need to unsubclass because
289 // otherwise we'd need the entries in gs_boxFromButton for the buttons
290 // being deleted to handle the messages generated during their destruction.
291 for ( size_t item = 0; item < m_radioButtons->GetCount(); item++ )
292 {
293 HWND hwnd = m_radioButtons->Get(item);
294
295 wxSetWindowProc(hwnd, reinterpret_cast<WNDPROC>(s_wndprocRadioBtn));
296 gs_boxFromButton.erase(hwnd);
297 }
298
299 delete m_radioButtons;
300
301 if ( m_dummyHwnd )
302 DestroyWindow((HWND)m_dummyHwnd);
303
304 delete[] m_radioWidth;
305 delete[] m_radioHeight;
306 }
307
308 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
309 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
310 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn)
311 {
312 HWND hwndBtn = (HWND)hWndBtn;
313
314 if ( !s_wndprocRadioBtn )
315 s_wndprocRadioBtn = (WXFARPROC)wxGetWindowProc(hwndBtn);
316
317 wxSetWindowProc(hwndBtn, wxRadioBtnWndProc);
318
319 gs_boxFromButton[hwndBtn] = this;
320 }
321
322 // ----------------------------------------------------------------------------
323 // events generation
324 // ----------------------------------------------------------------------------
325
326 bool wxRadioBox::MSWCommand(WXUINT cmd, WXWORD id_)
327 {
328 const int id = (signed short)id_;
329
330 if ( cmd == BN_CLICKED )
331 {
332 if (id == GetId())
333 return true;
334
335 int selectedButton = wxNOT_FOUND;
336
337 const unsigned int count = GetCount();
338 for ( unsigned int i = 0; i < count; i++ )
339 {
340 const HWND hwndBtn = (*m_radioButtons)[i];
341 if ( id == wxGetWindowId(hwndBtn) )
342 {
343 // we can get BN_CLICKED for a button which just became focused
344 // but it may not be checked, in which case we shouldn't
345 // generate a radiobox selection changed event for it
346 if ( ::SendMessage(hwndBtn, BM_GETCHECK, 0, 0) == BST_CHECKED )
347 selectedButton = i;
348
349 break;
350 }
351 }
352
353 if ( selectedButton == wxNOT_FOUND )
354 {
355 // just ignore it - due to a hack with WM_NCHITTEST handling in our
356 // wnd proc, we can receive dummy click messages when we click near
357 // the radiobox edge (this is ugly but Julian wouldn't let me get
358 // rid of this...)
359 return false;
360 }
361
362 if ( selectedButton != m_selectedButton )
363 {
364 m_selectedButton = selectedButton;
365
366 SendNotificationEvent();
367 }
368 //else: don't generate events when the selection doesn't change
369
370 return true;
371 }
372 else
373 return false;
374 }
375
376 void wxRadioBox::Command(wxCommandEvent & event)
377 {
378 SetSelection (event.GetInt());
379 SetFocus();
380 ProcessCommand(event);
381 }
382
383 void wxRadioBox::SendNotificationEvent()
384 {
385 wxCommandEvent event(wxEVT_RADIOBOX, m_windowId);
386 event.SetInt( m_selectedButton );
387 event.SetString(GetString(m_selectedButton));
388 event.SetEventObject( this );
389 ProcessCommand(event);
390 }
391
392 // ----------------------------------------------------------------------------
393 // simple accessors
394 // ----------------------------------------------------------------------------
395
396 unsigned int wxRadioBox::GetCount() const
397 {
398 return m_radioButtons ? m_radioButtons->GetCount() : 0u;
399 }
400
401 void wxRadioBox::SetString(unsigned int item, const wxString& label)
402 {
403 wxCHECK_RET( IsValid(item), wxT("invalid radiobox index") );
404
405 m_radioWidth[item] =
406 m_radioHeight[item] = wxDefaultCoord;
407
408 ::SetWindowText((*m_radioButtons)[item], label.c_str());
409
410 InvalidateBestSize();
411 }
412
413 void wxRadioBox::SetSelection(int N)
414 {
415 wxCHECK_RET( IsValid(N), wxT("invalid radiobox index") );
416
417 // unselect the old button
418 if ( m_selectedButton != wxNOT_FOUND )
419 ::SendMessage((*m_radioButtons)[m_selectedButton], BM_SETCHECK, 0, 0L);
420
421 // and select the new one
422 ::SendMessage((*m_radioButtons)[N], BM_SETCHECK, 1, 0L);
423
424 m_selectedButton = N;
425 }
426
427 // Find string for position
428 wxString wxRadioBox::GetString(unsigned int item) const
429 {
430 wxCHECK_MSG( IsValid(item), wxEmptyString,
431 wxT("invalid radiobox index") );
432
433 return wxGetWindowText((*m_radioButtons)[item]);
434 }
435
436 void wxRadioBox::SetFocus()
437 {
438 if ( GetCount() > 0 )
439 {
440 ::SetFocus((*m_radioButtons)[m_selectedButton == wxNOT_FOUND
441 ? 0
442 : m_selectedButton]);
443 }
444 }
445
446 bool wxRadioBox::CanBeFocused() const
447 {
448 // If the control itself is hidden or disabled, no need to check anything
449 // else.
450 if ( !wxStaticBox::CanBeFocused() )
451 return false;
452
453 // Otherwise, check if we have any buttons that can be focused.
454 for ( size_t item = 0; item < m_radioButtons->GetCount(); item++ )
455 {
456 if ( IsItemEnabled(item) && IsItemShown(item) )
457 return true;
458 }
459
460 // We didn't find any items that can accept focus, so neither can we as a
461 // whole accept it.
462 return false;
463 }
464
465 // Enable a specific button
466 bool wxRadioBox::Enable(unsigned int item, bool enable)
467 {
468 wxCHECK_MSG( IsValid(item), false,
469 wxT("invalid item in wxRadioBox::Enable()") );
470
471 BOOL ret = MSWEnableHWND((*m_radioButtons)[item], enable);
472
473 return (ret == 0) != enable;
474 }
475
476 bool wxRadioBox::IsItemEnabled(unsigned int item) const
477 {
478 wxCHECK_MSG( IsValid(item), false,
479 wxT("invalid item in wxRadioBox::IsItemEnabled()") );
480
481 return ::IsWindowEnabled((*m_radioButtons)[item]) != 0;
482 }
483
484 // Show a specific button
485 bool wxRadioBox::Show(unsigned int item, bool show)
486 {
487 wxCHECK_MSG( IsValid(item), false,
488 wxT("invalid item in wxRadioBox::Show()") );
489
490 BOOL ret = ::ShowWindow((*m_radioButtons)[item], show ? SW_SHOW : SW_HIDE);
491
492 bool changed = (ret != 0) != show;
493 if ( changed )
494 {
495 InvalidateBestSize();
496 }
497
498 return changed;
499 }
500
501 bool wxRadioBox::IsItemShown(unsigned int item) const
502 {
503 wxCHECK_MSG( IsValid(item), false,
504 wxT("invalid item in wxRadioBox::IsItemShown()") );
505
506 // don't use IsWindowVisible() here because it would return false if the
507 // radiobox itself is hidden while we want to only return false if this
508 // button specifically is hidden
509 return (::GetWindowLong((*m_radioButtons)[item],
510 GWL_STYLE) & WS_VISIBLE) != 0;
511 }
512
513 #if wxUSE_TOOLTIPS
514
515 bool wxRadioBox::HasToolTips() const
516 {
517 return wxStaticBox::HasToolTips() || wxRadioBoxBase::HasItemToolTips();
518 }
519
520 void wxRadioBox::DoSetItemToolTip(unsigned int item, wxToolTip *tooltip)
521 {
522 // we have already checked for the item to be valid in wxRadioBoxBase
523 const HWND hwndRbtn = (*m_radioButtons)[item];
524 if ( tooltip != NULL )
525 tooltip->AddOtherWindow(hwndRbtn);
526 else // unset the tooltip
527 wxToolTip::Remove(hwndRbtn, 0, wxRect(0,0,0,0));
528 // the second parameter can be zero since it's ignored by Remove()
529 // as we pass a rect for which wxRect::IsEmpty()==true...
530 }
531
532 #endif // wxUSE_TOOLTIPS
533
534 bool wxRadioBox::Reparent(wxWindowBase *newParent)
535 {
536 if ( !wxStaticBox::Reparent(newParent) )
537 {
538 return false;
539 }
540
541 HWND hwndParent = GetHwndOf(GetParent());
542 for ( size_t item = 0; item < m_radioButtons->GetCount(); item++ )
543 {
544 ::SetParent((*m_radioButtons)[item], hwndParent);
545 }
546 #ifdef __WXWINCE__
547 // put static box under the buttons in the Z-order
548 SetWindowPos(GetHwnd(), HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
549 #endif
550 return true;
551 }
552
553 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox, wxStaticBox, m_radioButtons)
554
555 // ----------------------------------------------------------------------------
556 // size calculations
557 // ----------------------------------------------------------------------------
558
559 wxSize wxRadioBox::GetMaxButtonSize() const
560 {
561 // calculate the max button size
562 int widthMax = 0,
563 heightMax = 0;
564 const unsigned int count = GetCount();
565 for ( unsigned int i = 0 ; i < count; i++ )
566 {
567 int width, height;
568 if ( m_radioWidth[i] < 0 )
569 {
570 GetTextExtent(wxGetWindowText((*m_radioButtons)[i]), &width, &height);
571
572 // adjust the size to take into account the radio box itself
573 // FIXME this is totally bogus!
574 width += RADIO_SIZE;
575 height *= 3;
576 height /= 2;
577 }
578 else
579 {
580 width = m_radioWidth[i];
581 height = m_radioHeight[i];
582 }
583
584 if ( widthMax < width )
585 widthMax = width;
586 if ( heightMax < height )
587 heightMax = height;
588 }
589
590 return wxSize(widthMax, heightMax);
591 }
592
593 wxSize wxRadioBox::GetTotalButtonSize(const wxSize& sizeBtn) const
594 {
595 // the radiobox should be big enough for its buttons
596 int cx1, cy1;
597 wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont());
598
599 int extraHeight = cy1;
600
601 int height = GetRowCount() * sizeBtn.y + cy1/2 + extraHeight;
602 int width = GetColumnCount() * (sizeBtn.x + cx1) + cx1;
603
604 // Add extra space under the label, if it exists.
605 if (!wxControl::GetLabel().empty())
606 height += cy1/2;
607
608 // and also wide enough for its label
609 int widthLabel;
610 GetTextExtent(GetLabelText(), &widthLabel, NULL);
611 widthLabel += RADIO_SIZE; // FIXME this is bogus too
612 if ( widthLabel > width )
613 width = widthLabel;
614
615 return wxSize(width, height);
616 }
617
618 wxSize wxRadioBox::DoGetBestSize() const
619 {
620 if ( !m_radioButtons )
621 {
622 // if we're not fully initialized yet, we can't meaningfully compute
623 // our best size, we'll do it later
624 return wxSize(1, 1);
625 }
626
627 wxSize best = GetTotalButtonSize(GetMaxButtonSize());
628 CacheBestSize(best);
629 return best;
630 }
631
632 void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
633 {
634 if ( (width == wxDefaultCoord && (sizeFlags & wxSIZE_AUTO_WIDTH)) ||
635 (height == wxDefaultCoord && (sizeFlags & wxSIZE_AUTO_HEIGHT)) )
636 {
637 // Attempt to have a look coherent with other platforms: We compute the
638 // biggest toggle dim, then we align all items according this value.
639 const wxSize totSize = GetTotalButtonSize(GetMaxButtonSize());
640
641 // only change our width/height if asked for
642 if ( width == wxDefaultCoord && (sizeFlags & wxSIZE_AUTO_WIDTH) )
643 width = totSize.x;
644
645 if ( height == wxDefaultCoord && (sizeFlags & wxSIZE_AUTO_HEIGHT) )
646 height = totSize.y;
647 }
648
649 wxStaticBox::DoSetSize(x, y, width, height);
650 }
651
652 void wxRadioBox::DoMoveWindow(int x, int y, int width, int height)
653 {
654 wxStaticBox::DoMoveWindow(x, y, width, height);
655
656 PositionAllButtons(x, y, width, height);
657 }
658
659 void
660 wxRadioBox::PositionAllButtons(int x, int y, int width, int WXUNUSED(height))
661 {
662 wxSize maxSize = GetMaxButtonSize();
663 int maxWidth = maxSize.x,
664 maxHeight = maxSize.y;
665
666 // Now position all the buttons: the current button will be put at
667 // wxPoint(x_offset, y_offset) and the new row/column will start at
668 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
669 // maxHeight) except for the buttons in the last column which should extend
670 // to the right border of radiobox and thus can be wider than this.
671
672 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
673 // left to right order and GetMajorDim() is the number of columns while
674 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
675 // GetMajorDim() is the number of rows.
676
677 int cx1, cy1;
678 wxGetCharSize(m_hWnd, &cx1, &cy1, GetFont());
679
680 int x_offset = x + cx1;
681 int y_offset = y + cy1;
682
683 // Add extra space under the label, if it exists.
684 if (!wxControl::GetLabel().empty())
685 y_offset += cy1/2;
686
687 int startX = x_offset;
688 int startY = y_offset;
689
690 const unsigned int count = GetCount();
691 for (unsigned int i = 0; i < count; i++)
692 {
693 // the last button in the row may be wider than the other ones as the
694 // radiobox may be wider than the sum of the button widths (as it
695 // happens, for example, when the radiobox label is very long)
696 bool isLastInTheRow;
697 if ( m_windowStyle & wxRA_SPECIFY_COLS )
698 {
699 // item is the last in its row if it is a multiple of the number of
700 // columns or if it is just the last item
701 unsigned int n = i + 1;
702 isLastInTheRow = ((n % GetMajorDim()) == 0) || (n == count);
703 }
704 else // wxRA_SPECIFY_ROWS
705 {
706 // item is the last in the row if it is in the last columns
707 isLastInTheRow = i >= (count/GetMajorDim())*GetMajorDim();
708 }
709
710 // is this the start of new row/column?
711 if ( i && (i % GetMajorDim() == 0) )
712 {
713 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
714 {
715 // start of new column
716 y_offset = startY;
717 x_offset += maxWidth + cx1;
718 }
719 else // start of new row
720 {
721 x_offset = startX;
722 y_offset += maxHeight;
723 if (m_radioWidth[0]>0)
724 y_offset += cy1/2;
725 }
726 }
727
728 int widthBtn;
729 if ( isLastInTheRow )
730 {
731 // make the button go to the end of radio box
732 widthBtn = startX + width - x_offset - 2*cx1;
733 if ( widthBtn < maxWidth )
734 widthBtn = maxWidth;
735 }
736 else
737 {
738 // normal button, always of the same size
739 widthBtn = maxWidth;
740 }
741
742 // make all buttons of the same, maximal size - like this they cover
743 // the radiobox entirely and the radiobox tooltips are always shown
744 // (otherwise they are not when the mouse pointer is in the radiobox
745 // part not belonging to any radiobutton)
746 DoMoveSibling((*m_radioButtons)[i], x_offset, y_offset, widthBtn, maxHeight);
747
748 // where do we put the next button?
749 if ( m_windowStyle & wxRA_SPECIFY_ROWS )
750 {
751 // below this one
752 y_offset += maxHeight;
753 if (m_radioWidth[0]>0)
754 y_offset += cy1/2;
755 }
756 else
757 {
758 // to the right of this one
759 x_offset += widthBtn + cx1;
760 }
761 }
762 }
763
764 int wxRadioBox::GetItemFromPoint(const wxPoint& pt) const
765 {
766 const unsigned int count = GetCount();
767 for ( unsigned int i = 0; i < count; i++ )
768 {
769 RECT rect = wxGetWindowRect((*m_radioButtons)[i]);
770
771 if ( rect.left <= pt.x && pt.x < rect.right &&
772 rect.top <= pt.y && pt.y < rect.bottom )
773 {
774 return i;
775 }
776 }
777
778 return wxNOT_FOUND;
779 }
780
781 // ----------------------------------------------------------------------------
782 // radio box drawing
783 // ----------------------------------------------------------------------------
784
785 #ifndef __WXWINCE__
786
787 WXHRGN wxRadioBox::MSWGetRegionWithoutChildren()
788 {
789 RECT rc;
790 ::GetWindowRect(GetHwnd(), &rc);
791 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
792
793 const unsigned int count = GetCount();
794 for ( unsigned int i = 0; i < count; ++i )
795 {
796 // don't clip out hidden children
797 if ( !IsItemShown(i) )
798 continue;
799
800 ::GetWindowRect((*m_radioButtons)[i], &rc);
801 AutoHRGN hrgnchild(::CreateRectRgnIndirect(&rc));
802 ::CombineRgn(hrgn, hrgn, hrgnchild, RGN_DIFF);
803 }
804
805 return (WXHRGN)hrgn;
806 }
807
808 #endif // __WXWINCE__
809
810 // ---------------------------------------------------------------------------
811 // window proc for radio buttons
812 // ---------------------------------------------------------------------------
813
814 LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd,
815 UINT message,
816 WPARAM wParam,
817 LPARAM lParam)
818 {
819
820 wxRadioBox * const radiobox = wxRadioBox::GetFromRadioButtonHWND(hwnd);
821 wxCHECK_MSG( radiobox, 0, wxT("Should have the associated radio box") );
822
823 switch ( message )
824 {
825 case WM_GETDLGCODE:
826 // we must tell IsDialogMessage()/our kbd processing code that we
827 // want to process arrows ourselves because neither of them is
828 // smart enough to handle arrows properly for us
829 {
830 long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd,
831 message, wParam, lParam);
832
833 return lDlgCode | DLGC_WANTARROWS;
834 }
835
836 case WM_KEYDOWN:
837 {
838 bool processed = true;
839
840 wxDirection dir;
841 switch ( wParam )
842 {
843 case VK_UP:
844 dir = wxUP;
845 break;
846
847 case VK_LEFT:
848 dir = wxLEFT;
849 break;
850
851 case VK_DOWN:
852 dir = wxDOWN;
853 break;
854
855 case VK_RIGHT:
856 dir = wxRIGHT;
857 break;
858
859 default:
860 processed = false;
861
862 // just to suppress the compiler warning
863 dir = wxALL;
864 }
865
866 if ( processed )
867 {
868 int selOld = radiobox->GetSelection();
869 int selNew = radiobox->GetNextItem
870 (
871 selOld,
872 dir,
873 radiobox->GetWindowStyle()
874 );
875
876 if ( selNew != selOld )
877 {
878 radiobox->SetSelection(selNew);
879 radiobox->SetFocus();
880
881 // emulate the button click
882 radiobox->SendNotificationEvent();
883
884 return 0;
885 }
886 }
887 }
888 break;
889
890 case WM_SETFOCUS:
891 case WM_KILLFOCUS:
892 {
893 // if we don't do this, no focus events are generated for the
894 // radiobox and, besides, we need to notify the parent about
895 // the focus change, otherwise the focus handling logic in
896 // wxControlContainer doesn't work
897 if ( message == WM_SETFOCUS )
898 radiobox->HandleSetFocus((WXHWND)wParam);
899 else
900 radiobox->HandleKillFocus((WXHWND)wParam);
901 }
902 break;
903
904 #ifndef __WXWINCE__
905 case WM_HELP:
906 {
907 bool processed = false;
908
909 wxEvtHandler * const handler = radiobox->GetEventHandler();
910
911 HELPINFO* info = (HELPINFO*) lParam;
912 if ( info->iContextType == HELPINFO_WINDOW )
913 {
914 for ( wxWindow* subjectOfHelp = radiobox;
915 subjectOfHelp;
916 subjectOfHelp = subjectOfHelp->GetParent() )
917 {
918 wxHelpEvent helpEvent(wxEVT_HELP,
919 subjectOfHelp->GetId(),
920 wxPoint(info->MousePos.x,
921 info->MousePos.y));
922 helpEvent.SetEventObject(radiobox);
923 if ( handler->ProcessEvent(helpEvent) )
924 {
925 processed = true;
926 break;
927 }
928 }
929 }
930 else if (info->iContextType == HELPINFO_MENUITEM)
931 {
932 wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId);
933 helpEvent.SetEventObject(radiobox);
934 processed = handler->ProcessEvent(helpEvent);
935 }
936
937 if ( processed )
938 return 0;
939 }
940 break;
941 #endif // !__WXWINCE__
942 }
943
944 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam);
945 }
946
947 #endif // wxUSE_RADIOBOX