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