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"
30 #include "wx/bitmap.h"
32 #include "wx/radiobox.h"
33 #include "wx/settings.h"
37 #include "wx/msw/subwin.h"
40 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
43 #include "wx/tooltip.h"
44 #endif // wxUSE_TOOLTIPS
46 // TODO: wxCONSTRUCTOR
47 #if 0 // wxUSE_EXTENDED_RTTI
48 WX_DEFINE_FLAGS( wxRadioBoxStyle
)
50 wxBEGIN_FLAGS( wxRadioBoxStyle
)
51 // new style border flags, we put them first to
52 // use them for streaming out
53 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
54 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
55 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
56 wxFLAGS_MEMBER(wxBORDER_RAISED
)
57 wxFLAGS_MEMBER(wxBORDER_STATIC
)
58 wxFLAGS_MEMBER(wxBORDER_NONE
)
60 // old style border flags
61 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
62 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
63 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
64 wxFLAGS_MEMBER(wxRAISED_BORDER
)
65 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
66 wxFLAGS_MEMBER(wxBORDER
)
68 // standard window styles
69 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
70 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
71 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
72 wxFLAGS_MEMBER(wxWANTS_CHARS
)
73 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
74 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
75 wxFLAGS_MEMBER(wxVSCROLL
)
76 wxFLAGS_MEMBER(wxHSCROLL
)
78 wxFLAGS_MEMBER(wxRA_SPECIFY_COLS
)
79 wxFLAGS_MEMBER(wxRA_HORIZONTAL
)
80 wxFLAGS_MEMBER(wxRA_SPECIFY_ROWS
)
81 wxFLAGS_MEMBER(wxRA_VERTICAL
)
83 wxEND_FLAGS( wxRadioBoxStyle
)
85 IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioBox
, wxControl
,"wx/radiobox.h")
87 wxBEGIN_PROPERTIES_TABLE(wxRadioBox
)
88 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_RADIOBOX_SELECTED
, wxCommandEvent
)
89 wxPROPERTY_FLAGS( WindowStyle
, wxRadioBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
90 wxEND_PROPERTIES_TABLE()
93 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
104 // ---------------------------------------------------------------------------
106 // ---------------------------------------------------------------------------
108 // wnd proc for radio buttons
109 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
114 // ---------------------------------------------------------------------------
116 // ---------------------------------------------------------------------------
118 // the pointer to standard radio button wnd proc
119 static WXFARPROC s_wndprocRadioBtn
= (WXFARPROC
)NULL
;
121 // ===========================================================================
123 // ===========================================================================
125 // ---------------------------------------------------------------------------
126 // wxRadioBox creation
127 // ---------------------------------------------------------------------------
130 void wxRadioBox::Init()
132 m_selectedButton
= wxNOT_FOUND
;
133 m_radioButtons
= NULL
;
136 m_radioHeight
= NULL
;
139 bool wxRadioBox::Create(wxWindow
*parent
,
141 const wxString
& title
,
145 const wxString choices
[],
148 const wxValidator
& val
,
149 const wxString
& name
)
151 // initialize members
152 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
154 // common initialization
155 if ( !wxStaticBox::Create(parent
, id
, title
, pos
, size
, style
, name
) )
162 #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS
164 m_radioButtons
= new wxSubwindows(n
);
165 m_radioWidth
= new int[n
];
166 m_radioHeight
= new int[n
];
168 for ( int i
= 0; i
< n
; i
++ )
171 m_radioHeight
[i
] = wxDefaultCoord
;
172 long styleBtn
= BS_AUTORADIOBUTTON
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
174 styleBtn
|= WS_GROUP
;
176 long newId
= NewControlId();
178 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
181 0, 0, 0, 0, // will be set in SetSize()
189 wxLogLastError(wxT("CreateWindow(radio btn)"));
194 (*m_radioButtons
)[i
] = hwndBtn
;
196 SubclassRadioButton((WXHWND
)hwndBtn
);
198 m_subControls
.Add(newId
);
201 // Create a dummy radio control to end the group.
202 (void)::CreateWindow(_T("BUTTON"),
204 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
205 0, 0, 0, 0, GetHwnd(),
206 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
208 m_radioButtons
->SetFont(GetFont());
211 // Set the z-order correctly
212 SetWindowPos(GetHwnd(), HWND_BOTTOM
, 0, 0, 0, 0, SWP_NOMOVE
|SWP_NOSIZE
);
216 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
218 // Now that we have items determine what is the best size and set it.
224 bool wxRadioBox::Create(wxWindow
*parent
,
226 const wxString
& title
,
229 const wxArrayString
& choices
,
232 const wxValidator
& val
,
233 const wxString
& name
)
235 wxCArrayString
chs(choices
);
236 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
237 chs
.GetStrings(), majorDim
, style
, val
, name
);
240 wxRadioBox::~wxRadioBox()
242 m_isBeingDeleted
= true;
244 delete m_radioButtons
;
245 delete[] m_radioWidth
;
246 delete[] m_radioHeight
;
249 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
250 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
251 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
253 HWND hwndBtn
= (HWND
)hWndBtn
;
255 if ( !s_wndprocRadioBtn
)
256 s_wndprocRadioBtn
= (WXFARPROC
)wxGetWindowProc(hwndBtn
);
258 wxSetWindowProc(hwndBtn
, wxRadioBtnWndProc
);
259 wxSetWindowUserData(hwndBtn
, this);
262 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
266 bool wxRadioBox::MSWCommand(WXUINT cmd
, WXWORD id
)
268 if ( cmd
== BN_CLICKED
)
273 int selectedButton
= wxNOT_FOUND
;
275 int count
= GetCount();
276 for ( int i
= 0; i
< count
; i
++ )
278 if ( id
== wxGetWindowId((*m_radioButtons
)[i
]) )
286 if ( selectedButton
== wxNOT_FOUND
)
288 // just ignore it - due to a hack with WM_NCHITTEST handling in our
289 // wnd proc, we can receive dummy click messages when we click near
290 // the radiobox edge (this is ugly but Julian wouldn't let me get
295 if ( selectedButton
!= m_selectedButton
)
297 m_selectedButton
= selectedButton
;
299 SendNotificationEvent();
301 //else: don't generate events when the selection doesn't change
309 void wxRadioBox::Command(wxCommandEvent
& event
)
311 SetSelection (event
.GetInt());
313 ProcessCommand(event
);
316 void wxRadioBox::SendNotificationEvent()
318 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
319 event
.SetInt( m_selectedButton
);
320 event
.SetString( GetString(m_selectedButton
) );
321 event
.SetEventObject( this );
322 ProcessCommand(event
);
325 // ----------------------------------------------------------------------------
327 // ----------------------------------------------------------------------------
329 int wxRadioBox::GetCount() const
331 return m_radioButtons
->GetCount();
334 // returns the number of rows
335 int wxRadioBox::GetNumVer() const
337 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
343 return (GetCount() + m_majorDim
- 1)/m_majorDim
;
347 // returns the number of columns
348 int wxRadioBox::GetNumHor() const
350 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
352 return (GetCount() + m_majorDim
- 1)/m_majorDim
;
360 void wxRadioBox::SetString(int item
, const wxString
& label
)
362 wxCHECK_RET( IsValid(item
), wxT("invalid radiobox index") );
365 m_radioHeight
[item
] = wxDefaultCoord
;
367 ::SetWindowText((*m_radioButtons
)[item
], label
.c_str());
369 InvalidateBestSize();
372 void wxRadioBox::SetSelection(int N
)
374 wxCHECK_RET( IsValid(N
), wxT("invalid radiobox index") );
376 // unselect the old button
377 if ( m_selectedButton
!= wxNOT_FOUND
)
378 ::SendMessage((*m_radioButtons
)[m_selectedButton
], BM_SETCHECK
, 0, 0L);
380 // and select the new one
381 ::SendMessage((*m_radioButtons
)[N
], BM_SETCHECK
, 1, 0L);
383 m_selectedButton
= N
;
386 // Find string for position
387 wxString
wxRadioBox::GetString(int item
) const
389 wxCHECK_MSG( IsValid(item
), wxEmptyString
,
390 wxT("invalid radiobox index") );
392 return wxGetWindowText((*m_radioButtons
)[item
]);
395 void wxRadioBox::SetFocus()
397 if ( GetCount() > 0 )
399 ::SetFocus((*m_radioButtons
)[m_selectedButton
== wxNOT_FOUND
401 : m_selectedButton
]);
405 // Enable a specific button
406 bool wxRadioBox::Enable(int item
, bool enable
)
408 wxCHECK_MSG( IsValid(item
), false,
409 wxT("invalid item in wxRadioBox::Enable()") );
411 BOOL ret
= ::EnableWindow((*m_radioButtons
)[item
], enable
);
413 return (ret
== 0) != enable
;
416 bool wxRadioBox::IsItemEnabled(int item
) const
418 wxCHECK_MSG( IsValid(item
), false,
419 wxT("invalid item in wxRadioBox::Enable()") );
421 return ::IsWindowEnabled((*m_radioButtons
)[item
]) != 0;
424 // Show a specific button
425 bool wxRadioBox::Show(int item
, bool show
)
427 wxCHECK_MSG( IsValid(item
), false,
428 wxT("invalid item in wxRadioBox::Show()") );
430 BOOL ret
= ::ShowWindow((*m_radioButtons
)[item
], show
? SW_SHOW
: SW_HIDE
);
432 bool changed
= (ret
!= 0) != show
;
435 InvalidateBestSize();
441 bool wxRadioBox::IsItemShown(int item
) const
443 wxCHECK_MSG( IsValid(item
), false,
444 wxT("invalid item in wxRadioBox::Enable()") );
446 // don't use IsWindowVisible() here because it would return false if the
447 // radiobox itself is hidden while we want to only return false if this
448 // button specifically is hidden
449 return (::GetWindowLong((*m_radioButtons
)[item
],
450 GWL_STYLE
) & WS_VISIBLE
) != 0;
453 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox
, wxStaticBox
, m_radioButtons
)
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
459 wxSize
wxRadioBox::GetMaxButtonSize() const
461 // calculate the max button size
464 const int count
= GetCount();
465 for ( int i
= 0 ; i
< count
; i
++ )
468 if ( m_radioWidth
[i
] < 0 )
470 GetTextExtent(wxGetWindowText((*m_radioButtons
)[i
]), &width
, &height
);
472 // adjust the size to take into account the radio box itself
473 // FIXME this is totally bogus!
480 width
= m_radioWidth
[i
];
481 height
= m_radioHeight
[i
];
484 if ( widthMax
< width
)
486 if ( heightMax
< height
)
490 return wxSize(widthMax
, heightMax
);
493 wxSize
wxRadioBox::GetTotalButtonSize(const wxSize
& sizeBtn
) const
495 // the radiobox should be big enough for its buttons
497 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
499 int extraHeight
= cy1
;
501 int height
= GetNumVer() * sizeBtn
.y
+ cy1
/2 + extraHeight
;
502 int width
= GetNumHor() * (sizeBtn
.x
+ cx1
) + cx1
;
504 // Add extra space under the label, if it exists.
505 if (!wxControl::GetLabel().empty())
508 // and also wide enough for its label
510 GetTextExtent(GetLabel(), &widthLabel
, NULL
);
511 widthLabel
+= RADIO_SIZE
; // FIXME this is bogus too
512 if ( widthLabel
> width
)
515 return wxSize(width
, height
);
518 wxSize
wxRadioBox::DoGetBestSize() const
520 wxSize best
= GetTotalButtonSize(GetMaxButtonSize());
525 // Restored old code.
526 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
528 int currentX
, currentY
;
529 GetPosition(¤tX
, ¤tY
);
530 int widthOld
, heightOld
;
531 GetSize(&widthOld
, &heightOld
);
536 if (x
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
538 if (y
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
545 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
547 // Attempt to have a look coherent with other platforms: We compute the
548 // biggest toggle dim, then we align all items according this value.
549 wxSize maxSize
= GetMaxButtonSize();
550 int maxWidth
= maxSize
.x
,
551 maxHeight
= maxSize
.y
;
553 wxSize totSize
= GetTotalButtonSize(maxSize
);
554 int totWidth
= totSize
.x
,
555 totHeight
= totSize
.y
;
557 // only change our width/height if asked for
558 if ( width
== wxDefaultCoord
)
560 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
566 if ( height
== wxDefaultCoord
)
568 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
574 DoMoveWindow(xx
, yy
, width
, height
);
576 // Now position all the buttons: the current button will be put at
577 // wxPoint(x_offset, y_offset) and the new row/column will start at
578 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
579 // maxHeight) except for the buttons in the last column which should extend
580 // to the right border of radiobox and thus can be wider than this.
582 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
583 // left to right order and m_majorDim is the number of columns while
584 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
585 // m_majorDim is the number of rows.
590 // Add extra space under the label, if it exists.
591 if (!wxControl::GetLabel().empty())
594 int startX
= x_offset
;
595 int startY
= y_offset
;
597 const int count
= GetCount();
598 for ( int i
= 0; i
< count
; i
++ )
600 // the last button in the row may be wider than the other ones as the
601 // radiobox may be wider than the sum of the button widths (as it
602 // happens, for example, when the radiobox label is very long)
604 if ( m_windowStyle
& wxRA_SPECIFY_COLS
)
606 // item is the last in its row if it is a multiple of the number of
607 // columns or if it is just the last item
609 isLastInTheRow
= ((n
% m_majorDim
) == 0) || (n
== count
);
611 else // wxRA_SPECIFY_ROWS
613 // item is the last in the row if it is in the last columns
614 isLastInTheRow
= i
>= (count
/m_majorDim
)*m_majorDim
;
617 // is this the start of new row/column?
618 if ( i
&& (i
% m_majorDim
== 0) )
620 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
622 // start of new column
624 x_offset
+= maxWidth
+ cx1
;
626 else // start of new row
629 y_offset
+= maxHeight
;
630 if (m_radioWidth
[0]>0)
636 if ( isLastInTheRow
)
638 // make the button go to the end of radio box
639 widthBtn
= startX
+ width
- x_offset
- 2*cx1
;
640 if ( widthBtn
< maxWidth
)
645 // normal button, always of the same size
649 // make all buttons of the same, maximal size - like this they cover
650 // the radiobox entirely and the radiobox tooltips are always shown
651 // (otherwise they are not when the mouse pointer is in the radiobox
652 // part not belonging to any radiobutton)
653 ::MoveWindow((*m_radioButtons
)[i
],
654 x_offset
, y_offset
, widthBtn
, maxHeight
,
657 // where do we put the next button?
658 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
661 y_offset
+= maxHeight
;
662 if (m_radioWidth
[0]>0)
667 // to the right of this one
668 x_offset
+= widthBtn
+ cx1
;
673 // ----------------------------------------------------------------------------
675 // ----------------------------------------------------------------------------
679 WXHRGN
wxRadioBox::MSWGetRegionWithoutChildren()
682 ::GetWindowRect(GetHwnd(), &rc
);
683 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
685 const size_t count
= GetCount();
686 for ( size_t i
= 0; i
< count
; ++i
)
688 // don't clip out hidden children
689 if ( !IsItemShown(i
) )
692 ::GetWindowRect((*m_radioButtons
)[i
], &rc
);
693 AutoHRGN
hrgnchild(::CreateRectRgnIndirect(&rc
));
694 ::CombineRgn(hrgn
, hrgn
, hrgnchild
, RGN_DIFF
);
701 wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
703 // FIXME: Without this, the radiobox corrupts other controls as it moves
704 // in a dynamic layout. Refreshing causes flicker, but it's better than
705 // leaving droppings. Note that for some reason, wxStaticBox doesn't need
706 // this (perhaps because it has no real children?)
707 if ( nMsg
== WM_MOVE
)
709 WXLRESULT res
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
710 wxRect rect
= GetRect();
711 GetParent()->Refresh(true, & rect
);
715 return wxStaticBox::MSWWindowProc(nMsg
, wParam
, lParam
);
718 #endif // __WXWINCE__
720 // ---------------------------------------------------------------------------
721 // window proc for radio buttons
722 // ---------------------------------------------------------------------------
724 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
732 // we must tell IsDialogMessage()/our kbd processing code that we
733 // want to process arrows ourselves because neither of them is
734 // smart enough to handle arrows properly for us
736 long lDlgCode
= ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
,
737 message
, wParam
, lParam
);
739 return lDlgCode
| DLGC_WANTARROWS
;
745 NMHDR
* hdr
= (NMHDR
*)lParam
;
746 if ( hdr
->code
== TTN_NEEDTEXT
)
749 radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
751 wxCHECK_MSG( radiobox
, 0,
752 wxT("radio button without radio box?") );
754 wxToolTip
*tooltip
= radiobox
->GetToolTip();
757 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
758 ttt
->lpszText
= (wxChar
*)tooltip
->GetTip().c_str();
766 #endif // wxUSE_TOOLTIPS
770 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
772 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
774 bool processed
= true;
798 // just to suppress the compiler warning
804 int selOld
= radiobox
->GetSelection();
805 int selNew
= radiobox
->GetNextItem
809 radiobox
->GetWindowStyle()
812 if ( selNew
!= selOld
)
814 radiobox
->SetSelection(selNew
);
815 radiobox
->SetFocus();
817 // emulate the button click
818 radiobox
->SendNotificationEvent();
829 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
831 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
833 // if we don't do this, no focus events are generated for the
834 // radiobox and, besides, we need to notify the parent about
835 // the focus change, otherwise the focus handling logic in
836 // wxControlContainer doesn't work
837 if ( message
== WM_SETFOCUS
)
838 radiobox
->HandleSetFocus((WXHWND
)wParam
);
840 radiobox
->HandleKillFocus((WXHWND
)wParam
);
847 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
849 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
851 bool processed
= false;
853 wxEvtHandler
* const handler
= radiobox
->GetEventHandler();
855 HELPINFO
* info
= (HELPINFO
*) lParam
;
856 if ( info
->iContextType
== HELPINFO_WINDOW
)
858 for ( wxWindow
* subjectOfHelp
= radiobox
;
860 subjectOfHelp
= subjectOfHelp
->GetParent() )
862 wxHelpEvent
helpEvent(wxEVT_HELP
,
863 subjectOfHelp
->GetId(),
864 wxPoint(info
->MousePos
.x
,
866 helpEvent
.SetEventObject(radiobox
);
867 if ( handler
->ProcessEvent(helpEvent
) )
874 else if (info
->iContextType
== HELPINFO_MENUITEM
)
876 wxHelpEvent
helpEvent(wxEVT_HELP
, info
->iCtrlId
);
877 helpEvent
.SetEventObject(radiobox
);
878 processed
= handler
->ProcessEvent(helpEvent
);
885 #endif // !__WXWINCE__
888 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, message
, wParam
, lParam
);
891 #endif // wxUSE_RADIOBOX