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