1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/radiobox.cpp
3 // Purpose: wxRadioBox implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/radiobox.h"
32 #include "wx/bitmap.h"
34 #include "wx/settings.h"
38 #include "wx/msw/subwin.h"
41 #include "wx/tooltip.h"
42 #endif // wxUSE_TOOLTIPS
44 // TODO: wxCONSTRUCTOR
45 #if 0 // wxUSE_EXTENDED_RTTI
46 WX_DEFINE_FLAGS( wxRadioBoxStyle
)
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
)
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
)
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
)
76 wxFLAGS_MEMBER(wxRA_SPECIFY_COLS
)
77 wxFLAGS_MEMBER(wxRA_HORIZONTAL
)
78 wxFLAGS_MEMBER(wxRA_SPECIFY_ROWS
)
79 wxFLAGS_MEMBER(wxRA_VERTICAL
)
81 wxEND_FLAGS( wxRadioBoxStyle
)
83 IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioBox
, wxControl
,"wx/radiobox.h")
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()
91 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
102 // ---------------------------------------------------------------------------
104 // ---------------------------------------------------------------------------
106 // wnd proc for radio buttons
107 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
112 // ---------------------------------------------------------------------------
114 // ---------------------------------------------------------------------------
116 // the pointer to standard radio button wnd proc
117 static WXFARPROC s_wndprocRadioBtn
= (WXFARPROC
)NULL
;
119 // ===========================================================================
121 // ===========================================================================
123 // ---------------------------------------------------------------------------
124 // wxRadioBox creation
125 // ---------------------------------------------------------------------------
128 void wxRadioBox::Init()
130 m_selectedButton
= wxNOT_FOUND
;
131 m_radioButtons
= NULL
;
134 m_radioHeight
= NULL
;
137 bool wxRadioBox::Create(wxWindow
*parent
,
139 const wxString
& title
,
143 const wxString choices
[],
146 const wxValidator
& val
,
147 const wxString
& name
)
149 // common initialization
150 if ( !wxStaticBox::Create(parent
, id
, title
, pos
, size
, style
, name
) )
157 #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS
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
);
168 m_radioWidth
= new int[n
];
169 m_radioHeight
= new int[n
];
171 for ( int i
= 0; i
< n
; i
++ )
174 m_radioHeight
[i
] = wxDefaultCoord
;
175 long styleBtn
= BS_AUTORADIOBUTTON
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
177 styleBtn
|= WS_GROUP
;
179 wxWindowIDRef subid
= NewControlId();
181 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
184 0, 0, 0, 0, // will be set in SetSize()
186 (HMENU
)subid
.GetValue(),
192 wxLogLastError(wxT("CreateWindow(radio btn)"));
197 // Keep track of the subwindow
198 m_radioButtons
->Set(i
, hwndBtn
, subid
);
200 SubclassRadioButton((WXHWND
)hwndBtn
);
202 // Also, make it a subcontrol of this control
203 m_subControls
.Add(subid
);
206 // Create a dummy radio control to end the group.
207 m_dummyId
= NewControlId();
209 m_dummyHwnd
= (WXHWND
)::CreateWindow(_T("BUTTON"),
211 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
212 0, 0, 0, 0, GetHwndOf(parent
),
213 (HMENU
)m_dummyId
.GetValue(), wxGetInstance(), NULL
);
216 m_radioButtons
->SetFont(GetFont());
219 // Set the z-order correctly
220 SetWindowPos(GetHwnd(), HWND_BOTTOM
, 0, 0, 0, 0, SWP_NOMOVE
|SWP_NOSIZE
);
223 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
225 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
227 // Now that we have items determine what is the best size and set it.
228 SetInitialSize(size
);
233 bool wxRadioBox::Create(wxWindow
*parent
,
235 const wxString
& title
,
238 const wxArrayString
& choices
,
241 const wxValidator
& val
,
242 const wxString
& name
)
244 wxCArrayString
chs(choices
);
245 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
246 chs
.GetStrings(), majorDim
, style
, val
, name
);
249 wxRadioBox::~wxRadioBox()
251 m_isBeingDeleted
= true;
253 delete m_radioButtons
;
255 DestroyWindow((HWND
)m_dummyHwnd
);
256 delete[] m_radioWidth
;
257 delete[] m_radioHeight
;
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
)
264 HWND hwndBtn
= (HWND
)hWndBtn
;
266 if ( !s_wndprocRadioBtn
)
267 s_wndprocRadioBtn
= (WXFARPROC
)wxGetWindowProc(hwndBtn
);
269 wxSetWindowProc(hwndBtn
, wxRadioBtnWndProc
);
270 wxSetWindowUserData(hwndBtn
, this);
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 bool wxRadioBox::MSWCommand(WXUINT cmd
, WXWORD id_
)
279 const int id
= (signed short)id_
;
281 if ( cmd
== BN_CLICKED
)
286 int selectedButton
= wxNOT_FOUND
;
288 const unsigned int count
= GetCount();
289 for ( unsigned int i
= 0; i
< count
; i
++ )
291 const HWND hwndBtn
= (*m_radioButtons
)[i
];
292 if ( id
== wxGetWindowId(hwndBtn
) )
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
)
304 if ( selectedButton
== wxNOT_FOUND
)
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
313 if ( selectedButton
!= m_selectedButton
)
315 m_selectedButton
= selectedButton
;
317 SendNotificationEvent();
319 //else: don't generate events when the selection doesn't change
327 void wxRadioBox::Command(wxCommandEvent
& event
)
329 SetSelection (event
.GetInt());
331 ProcessCommand(event
);
334 void wxRadioBox::SendNotificationEvent()
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
);
343 // ----------------------------------------------------------------------------
345 // ----------------------------------------------------------------------------
347 unsigned int wxRadioBox::GetCount() const
349 return m_radioButtons
? m_radioButtons
->GetCount() : 0u;
352 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
354 wxCHECK_RET( IsValid(item
), wxT("invalid radiobox index") );
357 m_radioHeight
[item
] = wxDefaultCoord
;
359 ::SetWindowText((*m_radioButtons
)[item
], label
.c_str());
361 InvalidateBestSize();
364 void wxRadioBox::SetSelection(int N
)
366 wxCHECK_RET( IsValid(N
), wxT("invalid radiobox index") );
368 // unselect the old button
369 if ( m_selectedButton
!= wxNOT_FOUND
)
370 ::SendMessage((*m_radioButtons
)[m_selectedButton
], BM_SETCHECK
, 0, 0L);
372 // and select the new one
373 ::SendMessage((*m_radioButtons
)[N
], BM_SETCHECK
, 1, 0L);
375 m_selectedButton
= N
;
378 // Find string for position
379 wxString
wxRadioBox::GetString(unsigned int item
) const
381 wxCHECK_MSG( IsValid(item
), wxEmptyString
,
382 wxT("invalid radiobox index") );
384 return wxGetWindowText((*m_radioButtons
)[item
]);
387 void wxRadioBox::SetFocus()
389 if ( GetCount() > 0 )
391 ::SetFocus((*m_radioButtons
)[m_selectedButton
== wxNOT_FOUND
393 : m_selectedButton
]);
397 // Enable a specific button
398 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
400 wxCHECK_MSG( IsValid(item
), false,
401 wxT("invalid item in wxRadioBox::Enable()") );
403 BOOL ret
= ::EnableWindow((*m_radioButtons
)[item
], enable
);
405 return (ret
== 0) != enable
;
408 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
410 wxCHECK_MSG( IsValid(item
), false,
411 wxT("invalid item in wxRadioBox::IsItemEnabled()") );
413 return ::IsWindowEnabled((*m_radioButtons
)[item
]) != 0;
416 // Show a specific button
417 bool wxRadioBox::Show(unsigned int item
, bool show
)
419 wxCHECK_MSG( IsValid(item
), false,
420 wxT("invalid item in wxRadioBox::Show()") );
422 BOOL ret
= ::ShowWindow((*m_radioButtons
)[item
], show
? SW_SHOW
: SW_HIDE
);
424 bool changed
= (ret
!= 0) != show
;
427 InvalidateBestSize();
433 bool wxRadioBox::IsItemShown(unsigned int item
) const
435 wxCHECK_MSG( IsValid(item
), false,
436 wxT("invalid item in wxRadioBox::IsItemShown()") );
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;
447 bool wxRadioBox::HasToolTips() const
449 return wxStaticBox::HasToolTips() || wxRadioBoxBase::HasItemToolTips();
452 void wxRadioBox::DoSetItemToolTip(unsigned int item
, wxToolTip
*tooltip
)
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
);
462 #endif // wxUSE_TOOLTIPS
464 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox
, wxStaticBox
, m_radioButtons
)
466 // ----------------------------------------------------------------------------
468 // ----------------------------------------------------------------------------
470 wxSize
wxRadioBox::GetMaxButtonSize() const
472 // calculate the max button size
475 const unsigned int count
= GetCount();
476 for ( unsigned int i
= 0 ; i
< count
; i
++ )
479 if ( m_radioWidth
[i
] < 0 )
481 GetTextExtent(wxGetWindowText((*m_radioButtons
)[i
]), &width
, &height
);
483 // adjust the size to take into account the radio box itself
484 // FIXME this is totally bogus!
491 width
= m_radioWidth
[i
];
492 height
= m_radioHeight
[i
];
495 if ( widthMax
< width
)
497 if ( heightMax
< height
)
501 return wxSize(widthMax
, heightMax
);
504 wxSize
wxRadioBox::GetTotalButtonSize(const wxSize
& sizeBtn
) const
506 // the radiobox should be big enough for its buttons
508 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
510 int extraHeight
= cy1
;
512 int height
= GetRowCount() * sizeBtn
.y
+ cy1
/2 + extraHeight
;
513 int width
= GetColumnCount() * (sizeBtn
.x
+ cx1
) + cx1
;
515 // Add extra space under the label, if it exists.
516 if (!wxControl::GetLabel().empty())
519 // and also wide enough for its label
521 GetTextExtent(GetLabelText(), &widthLabel
, NULL
);
522 widthLabel
+= RADIO_SIZE
; // FIXME this is bogus too
523 if ( widthLabel
> width
)
526 return wxSize(width
, height
);
529 wxSize
wxRadioBox::DoGetBestSize() const
531 if ( !m_radioButtons
)
533 // if we're not fully initialized yet, we can't meaningfully compute
534 // our best size, we'll do it later
538 wxSize best
= GetTotalButtonSize(GetMaxButtonSize());
543 // Restored old code.
544 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
546 int currentX
, currentY
;
547 GetPosition(¤tX
, ¤tY
);
548 int widthOld
, heightOld
;
549 GetSize(&widthOld
, &heightOld
);
554 if (x
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
556 if (y
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
563 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
565 // Attempt to have a look coherent with other platforms: We compute the
566 // biggest toggle dim, then we align all items according this value.
567 wxSize maxSize
= GetMaxButtonSize();
568 int maxWidth
= maxSize
.x
,
569 maxHeight
= maxSize
.y
;
571 wxSize totSize
= GetTotalButtonSize(maxSize
);
572 int totWidth
= totSize
.x
,
573 totHeight
= totSize
.y
;
575 // only change our width/height if asked for
576 if ( width
== wxDefaultCoord
)
578 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
584 if ( height
== wxDefaultCoord
)
586 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
592 DoMoveWindow(xx
, yy
, width
, height
);
594 // Now position all the buttons: the current button will be put at
595 // wxPoint(x_offset, y_offset) and the new row/column will start at
596 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
597 // maxHeight) except for the buttons in the last column which should extend
598 // to the right border of radiobox and thus can be wider than this.
600 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
601 // left to right order and GetMajorDim() is the number of columns while
602 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
603 // GetMajorDim() is the number of rows.
608 // Add extra space under the label, if it exists.
609 if (!wxControl::GetLabel().empty())
612 int startX
= x_offset
;
613 int startY
= y_offset
;
615 const unsigned int count
= GetCount();
616 for (unsigned int i
= 0; i
< count
; i
++)
618 // the last button in the row may be wider than the other ones as the
619 // radiobox may be wider than the sum of the button widths (as it
620 // happens, for example, when the radiobox label is very long)
622 if ( m_windowStyle
& wxRA_SPECIFY_COLS
)
624 // item is the last in its row if it is a multiple of the number of
625 // columns or if it is just the last item
626 unsigned int n
= i
+ 1;
627 isLastInTheRow
= ((n
% GetMajorDim()) == 0) || (n
== count
);
629 else // wxRA_SPECIFY_ROWS
631 // item is the last in the row if it is in the last columns
632 isLastInTheRow
= i
>= (count
/GetMajorDim())*GetMajorDim();
635 // is this the start of new row/column?
636 if ( i
&& (i
% GetMajorDim() == 0) )
638 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
640 // start of new column
642 x_offset
+= maxWidth
+ cx1
;
644 else // start of new row
647 y_offset
+= maxHeight
;
648 if (m_radioWidth
[0]>0)
654 if ( isLastInTheRow
)
656 // make the button go to the end of radio box
657 widthBtn
= startX
+ width
- x_offset
- 2*cx1
;
658 if ( widthBtn
< maxWidth
)
663 // normal button, always of the same size
667 // make all buttons of the same, maximal size - like this they cover
668 // the radiobox entirely and the radiobox tooltips are always shown
669 // (otherwise they are not when the mouse pointer is in the radiobox
670 // part not belonging to any radiobutton)
671 DoMoveSibling((*m_radioButtons
)[i
], x_offset
, y_offset
, widthBtn
, maxHeight
);
673 // where do we put the next button?
674 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
677 y_offset
+= maxHeight
;
678 if (m_radioWidth
[0]>0)
683 // to the right of this one
684 x_offset
+= widthBtn
+ cx1
;
689 int wxRadioBox::GetItemFromPoint(const wxPoint
& pt
) const
691 const unsigned int count
= GetCount();
692 for ( unsigned int i
= 0; i
< count
; i
++ )
694 RECT rect
= wxGetWindowRect((*m_radioButtons
)[i
]);
696 if ( rect
.left
<= pt
.x
&& pt
.x
< rect
.right
&&
697 rect
.top
<= pt
.y
&& pt
.y
< rect
.bottom
)
706 // ----------------------------------------------------------------------------
708 // ----------------------------------------------------------------------------
712 WXHRGN
wxRadioBox::MSWGetRegionWithoutChildren()
715 ::GetWindowRect(GetHwnd(), &rc
);
716 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
718 const unsigned int count
= GetCount();
719 for ( unsigned int i
= 0; i
< count
; ++i
)
721 // don't clip out hidden children
722 if ( !IsItemShown(i
) )
725 ::GetWindowRect((*m_radioButtons
)[i
], &rc
);
726 AutoHRGN
hrgnchild(::CreateRectRgnIndirect(&rc
));
727 ::CombineRgn(hrgn
, hrgn
, hrgnchild
, RGN_DIFF
);
733 #endif // __WXWINCE__
735 // ---------------------------------------------------------------------------
736 // window proc for radio buttons
737 // ---------------------------------------------------------------------------
739 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
747 // we must tell IsDialogMessage()/our kbd processing code that we
748 // want to process arrows ourselves because neither of them is
749 // smart enough to handle arrows properly for us
751 long lDlgCode
= ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
,
752 message
, wParam
, lParam
);
754 return lDlgCode
| DLGC_WANTARROWS
;
759 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
761 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
763 bool processed
= true;
787 // just to suppress the compiler warning
793 int selOld
= radiobox
->GetSelection();
794 int selNew
= radiobox
->GetNextItem
798 radiobox
->GetWindowStyle()
801 if ( selNew
!= selOld
)
803 radiobox
->SetSelection(selNew
);
804 radiobox
->SetFocus();
806 // emulate the button click
807 radiobox
->SendNotificationEvent();
818 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
820 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
822 // if we don't do this, no focus events are generated for the
823 // radiobox and, besides, we need to notify the parent about
824 // the focus change, otherwise the focus handling logic in
825 // wxControlContainer doesn't work
826 if ( message
== WM_SETFOCUS
)
827 radiobox
->HandleSetFocus((WXHWND
)wParam
);
829 radiobox
->HandleKillFocus((WXHWND
)wParam
);
836 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
838 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
840 bool processed
= false;
842 wxEvtHandler
* const handler
= radiobox
->GetEventHandler();
844 HELPINFO
* info
= (HELPINFO
*) lParam
;
845 if ( info
->iContextType
== HELPINFO_WINDOW
)
847 for ( wxWindow
* subjectOfHelp
= radiobox
;
849 subjectOfHelp
= subjectOfHelp
->GetParent() )
851 wxHelpEvent
helpEvent(wxEVT_HELP
,
852 subjectOfHelp
->GetId(),
853 wxPoint(info
->MousePos
.x
,
855 helpEvent
.SetEventObject(radiobox
);
856 if ( handler
->ProcessEvent(helpEvent
) )
863 else if (info
->iContextType
== HELPINFO_MENUITEM
)
865 wxHelpEvent
helpEvent(wxEVT_HELP
, info
->iCtrlId
);
866 helpEvent
.SetEventObject(radiobox
);
867 processed
= handler
->ProcessEvent(helpEvent
);
874 #endif // !__WXWINCE__
877 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, message
, wParam
, lParam
);
880 #endif // wxUSE_RADIOBOX