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