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