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
;
133 m_radioHeight
= NULL
;
136 bool wxRadioBox
::Create(wxWindow
*parent
,
138 const wxString
& title
,
142 const wxString choices
[],
145 const wxValidator
& val
,
146 const wxString
& name
)
148 // common initialization
149 if ( !wxStaticBox
::Create(parent
, id
, title
, pos
, size
, style
, name
) )
156 #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS
158 m_radioButtons
= new wxSubwindows(n
);
159 m_radioWidth
= new int[n
];
160 m_radioHeight
= new int[n
];
162 for ( int i
= 0; i
< n
; i
++ )
165 m_radioHeight
[i
] = wxDefaultCoord
;
166 long styleBtn
= BS_AUTORADIOBUTTON
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
168 styleBtn
|= WS_GROUP
;
170 long newId
= NewControlId();
172 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
175 0, 0, 0, 0, // will be set in SetSize()
183 wxLogLastError(wxT("CreateWindow(radio btn)"));
188 (*m_radioButtons
)[i
] = hwndBtn
;
190 SubclassRadioButton((WXHWND
)hwndBtn
);
192 m_subControls
.Add(newId
);
195 // Create a dummy radio control to end the group.
196 (void)::CreateWindow(_T("BUTTON"),
198 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
199 0, 0, 0, 0, GetHwndOf(parent
),
200 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
202 m_radioButtons
->SetFont(GetFont());
205 // Set the z-order correctly
206 SetWindowPos(GetHwnd(), HWND_BOTTOM
, 0, 0, 0, 0, SWP_NOMOVE
|SWP_NOSIZE
);
209 // Have to invalidate the best size as it may have been calculated
210 // before the number of buttons was known
211 InvalidateBestSize();
213 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
215 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
217 // Now that we have items determine what is the best size and set it.
218 SetInitialSize(size
);
223 bool wxRadioBox
::Create(wxWindow
*parent
,
225 const wxString
& title
,
228 const wxArrayString
& choices
,
231 const wxValidator
& val
,
232 const wxString
& name
)
234 wxCArrayString
chs(choices
);
235 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
236 chs
.GetStrings(), majorDim
, style
, val
, name
);
239 wxRadioBox
::~wxRadioBox()
241 m_isBeingDeleted
= true;
243 delete m_radioButtons
;
244 delete[] m_radioWidth
;
245 delete[] m_radioHeight
;
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
)
252 HWND hwndBtn
= (HWND
)hWndBtn
;
254 if ( !s_wndprocRadioBtn
)
255 s_wndprocRadioBtn
= (WXFARPROC
)wxGetWindowProc(hwndBtn
);
257 wxSetWindowProc(hwndBtn
, wxRadioBtnWndProc
);
258 wxSetWindowUserData(hwndBtn
, this);
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 bool wxRadioBox
::MSWCommand(WXUINT cmd
, WXWORD id
)
267 if ( cmd
== BN_CLICKED
)
272 int selectedButton
= wxNOT_FOUND
;
274 const unsigned int count
= GetCount();
275 for ( unsigned int i
= 0; i
< count
; i
++ )
277 if ( id
== wxGetWindowId((*m_radioButtons
)[i
]) )
285 if ( selectedButton
== wxNOT_FOUND
)
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
294 if ( selectedButton
!= m_selectedButton
)
296 m_selectedButton
= selectedButton
;
298 SendNotificationEvent();
300 //else: don't generate events when the selection doesn't change
308 void wxRadioBox
::Command(wxCommandEvent
& event
)
310 SetSelection (event
.GetInt());
312 ProcessCommand(event
);
315 void wxRadioBox
::SendNotificationEvent()
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
);
324 // ----------------------------------------------------------------------------
326 // ----------------------------------------------------------------------------
328 unsigned int wxRadioBox
::GetCount() const
330 return m_radioButtons ? m_radioButtons
->GetCount() : 0u;
333 void wxRadioBox
::SetString(unsigned int item
, const wxString
& label
)
335 wxCHECK_RET( IsValid(item
), wxT("invalid radiobox index") );
338 m_radioHeight
[item
] = wxDefaultCoord
;
340 ::SetWindowText((*m_radioButtons
)[item
], label
.c_str());
342 InvalidateBestSize();
345 void wxRadioBox
::SetSelection(int N
)
347 wxCHECK_RET( IsValid(N
), wxT("invalid radiobox index") );
349 // unselect the old button
350 if ( m_selectedButton
!= wxNOT_FOUND
)
351 ::SendMessage((*m_radioButtons
)[m_selectedButton
], BM_SETCHECK
, 0, 0L);
353 // and select the new one
354 ::SendMessage((*m_radioButtons
)[N
], BM_SETCHECK
, 1, 0L);
356 m_selectedButton
= N
;
359 // Find string for position
360 wxString wxRadioBox
::GetString(unsigned int item
) const
362 wxCHECK_MSG( IsValid(item
), wxEmptyString
,
363 wxT("invalid radiobox index") );
365 return wxGetWindowText((*m_radioButtons
)[item
]);
368 void wxRadioBox
::SetFocus()
370 if ( GetCount() > 0 )
372 ::SetFocus((*m_radioButtons
)[m_selectedButton
== wxNOT_FOUND
374 : m_selectedButton
]);
378 // Enable a specific button
379 bool wxRadioBox
::Enable(unsigned int item
, bool enable
)
381 wxCHECK_MSG( IsValid(item
), false,
382 wxT("invalid item in wxRadioBox::Enable()") );
384 BOOL ret
= ::EnableWindow((*m_radioButtons
)[item
], enable
);
386 return (ret
== 0) != enable
;
389 bool wxRadioBox
::IsItemEnabled(unsigned int item
) const
391 wxCHECK_MSG( IsValid(item
), false,
392 wxT("invalid item in wxRadioBox::IsItemEnabled()") );
394 return ::IsWindowEnabled((*m_radioButtons
)[item
]) != 0;
397 // Show a specific button
398 bool wxRadioBox
::Show(unsigned int item
, bool show
)
400 wxCHECK_MSG( IsValid(item
), false,
401 wxT("invalid item in wxRadioBox::Show()") );
403 BOOL ret
= ::ShowWindow((*m_radioButtons
)[item
], show ? SW_SHOW
: SW_HIDE
);
405 bool changed
= (ret
!= 0) != show
;
408 InvalidateBestSize();
414 bool wxRadioBox
::IsItemShown(unsigned int item
) const
416 wxCHECK_MSG( IsValid(item
), false,
417 wxT("invalid item in wxRadioBox::IsItemShown()") );
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;
428 bool wxRadioBox
::HasToolTips() const
430 return wxStaticBox
::HasToolTips() || wxRadioBoxBase
::HasItemToolTips();
433 void wxRadioBox
::DoSetItemToolTip(unsigned int item
, wxToolTip
*tooltip
)
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
);
443 #endif // wxUSE_TOOLTIPS
445 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox
, wxStaticBox
, m_radioButtons
)
447 // ----------------------------------------------------------------------------
449 // ----------------------------------------------------------------------------
451 wxSize wxRadioBox
::GetMaxButtonSize() const
453 // calculate the max button size
456 const unsigned int count
= GetCount();
457 for ( unsigned int i
= 0 ; i
< count
; i
++ )
460 if ( m_radioWidth
[i
] < 0 )
462 GetTextExtent(wxGetWindowText((*m_radioButtons
)[i
]), &width
, &height
);
464 // adjust the size to take into account the radio box itself
465 // FIXME this is totally bogus!
472 width
= m_radioWidth
[i
];
473 height
= m_radioHeight
[i
];
476 if ( widthMax
< width
)
478 if ( heightMax
< height
)
482 return wxSize(widthMax
, heightMax
);
485 wxSize wxRadioBox
::GetTotalButtonSize(const wxSize
& sizeBtn
) const
487 // the radiobox should be big enough for its buttons
489 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
491 int extraHeight
= cy1
;
493 int height
= GetRowCount() * sizeBtn
.y
+ cy1
/2 + extraHeight
;
494 int width
= GetColumnCount() * (sizeBtn
.x
+ cx1
) + cx1
;
496 // Add extra space under the label, if it exists.
497 if (!wxControl
::GetLabel().empty())
500 // and also wide enough for its label
502 GetTextExtent(GetLabelText(), &widthLabel
, NULL
);
503 widthLabel
+= RADIO_SIZE
; // FIXME this is bogus too
504 if ( widthLabel
> width
)
507 return wxSize(width
, height
);
510 wxSize wxRadioBox
::DoGetBestSize() const
512 wxSize best
= GetTotalButtonSize(GetMaxButtonSize());
517 // Restored old code.
518 void wxRadioBox
::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
520 int currentX
, currentY
;
521 GetPosition(¤tX
, ¤tY
);
522 int widthOld
, heightOld
;
523 GetSize(&widthOld
, &heightOld
);
528 if (x
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
530 if (y
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
537 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
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
;
545 wxSize totSize
= GetTotalButtonSize(maxSize
);
546 int totWidth
= totSize
.x
,
547 totHeight
= totSize
.y
;
549 // only change our width/height if asked for
550 if ( width
== wxDefaultCoord
)
552 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
558 if ( height
== wxDefaultCoord
)
560 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
566 DoMoveWindow(xx
, yy
, width
, height
);
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.
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.
582 // Add extra space under the label, if it exists.
583 if (!wxControl
::GetLabel().empty())
586 int startX
= x_offset
;
587 int startY
= y_offset
;
589 const unsigned int count
= GetCount();
590 for (unsigned int i
= 0; i
< count
; i
++)
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)
596 if ( m_windowStyle
& wxRA_SPECIFY_COLS
)
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
);
603 else // wxRA_SPECIFY_ROWS
605 // item is the last in the row if it is in the last columns
606 isLastInTheRow
= i
>= (count
/GetMajorDim())*GetMajorDim();
609 // is this the start of new row/column?
610 if ( i
&& (i
% GetMajorDim() == 0) )
612 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
614 // start of new column
616 x_offset
+= maxWidth
+ cx1
;
618 else // start of new row
621 y_offset
+= maxHeight
;
622 if (m_radioWidth
[0]>0)
628 if ( isLastInTheRow
)
630 // make the button go to the end of radio box
631 widthBtn
= startX
+ width
- x_offset
- 2*cx1
;
632 if ( widthBtn
< maxWidth
)
637 // normal button, always of the same size
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
);
647 // where do we put the next button?
648 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
651 y_offset
+= maxHeight
;
652 if (m_radioWidth
[0]>0)
657 // to the right of this one
658 x_offset
+= widthBtn
+ cx1
;
663 int wxRadioBox
::GetItemFromPoint(const wxPoint
& pt
) const
665 const unsigned int count
= GetCount();
666 for ( unsigned int i
= 0; i
< count
; i
++ )
668 RECT rect
= wxGetWindowRect((*m_radioButtons
)[i
]);
670 if ( rect
.left
<= pt
.x
&& pt
.x
< rect
.right
&&
671 rect
.top
<= pt
.y
&& pt
.y
< rect
.bottom
)
680 // ----------------------------------------------------------------------------
682 // ----------------------------------------------------------------------------
686 WXHRGN wxRadioBox
::MSWGetRegionWithoutChildren()
689 ::GetWindowRect(GetHwnd(), &rc
);
690 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
692 const unsigned int count
= GetCount();
693 for ( unsigned int i
= 0; i
< count
; ++i
)
695 // don't clip out hidden children
696 if ( !IsItemShown(i
) )
699 ::GetWindowRect((*m_radioButtons
)[i
], &rc
);
700 AutoHRGN
hrgnchild(::CreateRectRgnIndirect(&rc
));
701 ::CombineRgn(hrgn
, hrgn
, hrgnchild
, RGN_DIFF
);
707 #endif // __WXWINCE__
709 // ---------------------------------------------------------------------------
710 // window proc for radio buttons
711 // ---------------------------------------------------------------------------
713 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
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
725 long lDlgCode
= ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
,
726 message
, wParam
, lParam
);
728 return lDlgCode
| DLGC_WANTARROWS
;
733 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
735 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
737 bool processed
= true;
761 // just to suppress the compiler warning
767 int selOld
= radiobox
->GetSelection();
768 int selNew
= radiobox
->GetNextItem
772 radiobox
->GetWindowStyle()
775 if ( selNew
!= selOld
)
777 radiobox
->SetSelection(selNew
);
778 radiobox
->SetFocus();
780 // emulate the button click
781 radiobox
->SendNotificationEvent();
792 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
794 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
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
);
803 radiobox
->HandleKillFocus((WXHWND
)wParam
);
810 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
812 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
814 bool processed
= false;
816 wxEvtHandler
* const handler
= radiobox
->GetEventHandler();
818 HELPINFO
* info
= (HELPINFO
*) lParam
;
819 if ( info
->iContextType
== HELPINFO_WINDOW
)
821 for ( wxWindow
* subjectOfHelp
= radiobox
;
823 subjectOfHelp
= subjectOfHelp
->GetParent() )
825 wxHelpEvent
helpEvent(wxEVT_HELP
,
826 subjectOfHelp
->GetId(),
827 wxPoint(info
->MousePos
.x
,
829 helpEvent
.SetEventObject(radiobox
);
830 if ( handler
->ProcessEvent(helpEvent
) )
837 else if (info
->iContextType
== HELPINFO_MENUITEM
)
839 wxHelpEvent
helpEvent(wxEVT_HELP
, info
->iCtrlId
);
840 helpEvent
.SetEventObject(radiobox
);
841 processed
= handler
->ProcessEvent(helpEvent
);
848 #endif // !__WXWINCE__
851 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, message
, wParam
, lParam
);
854 #endif // wxUSE_RADIOBOX