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
;
132 m_radioButtonIds
= 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 m_radioButtons
= new wxSubwindows(n
);
160 m_radioButtonIds
= new wxWindowIDRef
[n
+ 1];
161 m_radioWidth
= new int[n
];
162 m_radioHeight
= new int[n
];
164 for ( int i
= 0; i
< n
; i
++ )
167 m_radioHeight
[i
] = wxDefaultCoord
;
168 long styleBtn
= BS_AUTORADIOBUTTON
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
170 styleBtn
|= WS_GROUP
;
172 m_radioButtonIds
[i
] = NewControlId();
174 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
177 0, 0, 0, 0, // will be set in SetSize()
179 (HMENU
)(wxWindowID
)m_radioButtonIds
[i
],
185 wxLogLastError(wxT("CreateWindow(radio btn)"));
190 (*m_radioButtons
)[i
] = hwndBtn
;
192 SubclassRadioButton((WXHWND
)hwndBtn
);
194 m_subControls
.Add(m_radioButtonIds
[i
]);
197 // Create a dummy radio control to end the group.
198 m_radioButtonIds
[n
] = NewControlId();
200 (void)::CreateWindow(_T("BUTTON"),
202 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
203 0, 0, 0, 0, GetHwndOf(parent
),
204 (HMENU
)(wxWindowID
)m_radioButtonIds
[n
], wxGetInstance(), NULL
);
206 m_radioButtons
->SetFont(GetFont());
209 // Set the z-order correctly
210 SetWindowPos(GetHwnd(), HWND_BOTTOM
, 0, 0, 0, 0, SWP_NOMOVE
|SWP_NOSIZE
);
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_radioButtonIds
;
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 const int id
= (signed short)id_
;
270 if ( cmd
== BN_CLICKED
)
275 int selectedButton
= wxNOT_FOUND
;
277 const unsigned int count
= GetCount();
278 for ( unsigned int i
= 0; i
< count
; i
++ )
280 const HWND hwndBtn
= (*m_radioButtons
)[i
];
281 if ( id
== wxGetWindowId(hwndBtn
) )
283 // we can get BN_CLICKED for a button which just became focused
284 // but it may not be checked, in which case we shouldn't
285 // generate a radiobox selection changed event for it
286 if ( ::SendMessage(hwndBtn
, BM_GETCHECK
, 0, 0) == BST_CHECKED
)
293 if ( selectedButton
== wxNOT_FOUND
)
295 // just ignore it - due to a hack with WM_NCHITTEST handling in our
296 // wnd proc, we can receive dummy click messages when we click near
297 // the radiobox edge (this is ugly but Julian wouldn't let me get
302 if ( selectedButton
!= m_selectedButton
)
304 m_selectedButton
= selectedButton
;
306 SendNotificationEvent();
308 //else: don't generate events when the selection doesn't change
316 void wxRadioBox::Command(wxCommandEvent
& event
)
318 SetSelection (event
.GetInt());
320 ProcessCommand(event
);
323 void wxRadioBox::SendNotificationEvent()
325 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
326 event
.SetInt( m_selectedButton
);
327 event
.SetString(GetString(m_selectedButton
));
328 event
.SetEventObject( this );
329 ProcessCommand(event
);
332 // ----------------------------------------------------------------------------
334 // ----------------------------------------------------------------------------
336 unsigned int wxRadioBox::GetCount() const
338 return m_radioButtons
? m_radioButtons
->GetCount() : 0u;
341 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
343 wxCHECK_RET( IsValid(item
), wxT("invalid radiobox index") );
346 m_radioHeight
[item
] = wxDefaultCoord
;
348 ::SetWindowText((*m_radioButtons
)[item
], label
.c_str());
350 InvalidateBestSize();
353 void wxRadioBox::SetSelection(int N
)
355 wxCHECK_RET( IsValid(N
), wxT("invalid radiobox index") );
357 // unselect the old button
358 if ( m_selectedButton
!= wxNOT_FOUND
)
359 ::SendMessage((*m_radioButtons
)[m_selectedButton
], BM_SETCHECK
, 0, 0L);
361 // and select the new one
362 ::SendMessage((*m_radioButtons
)[N
], BM_SETCHECK
, 1, 0L);
364 m_selectedButton
= N
;
367 // Find string for position
368 wxString
wxRadioBox::GetString(unsigned int item
) const
370 wxCHECK_MSG( IsValid(item
), wxEmptyString
,
371 wxT("invalid radiobox index") );
373 return wxGetWindowText((*m_radioButtons
)[item
]);
376 void wxRadioBox::SetFocus()
378 if ( GetCount() > 0 )
380 ::SetFocus((*m_radioButtons
)[m_selectedButton
== wxNOT_FOUND
382 : m_selectedButton
]);
386 // Enable a specific button
387 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
389 wxCHECK_MSG( IsValid(item
), false,
390 wxT("invalid item in wxRadioBox::Enable()") );
392 BOOL ret
= ::EnableWindow((*m_radioButtons
)[item
], enable
);
394 return (ret
== 0) != enable
;
397 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
399 wxCHECK_MSG( IsValid(item
), false,
400 wxT("invalid item in wxRadioBox::IsItemEnabled()") );
402 return ::IsWindowEnabled((*m_radioButtons
)[item
]) != 0;
405 // Show a specific button
406 bool wxRadioBox::Show(unsigned int item
, bool show
)
408 wxCHECK_MSG( IsValid(item
), false,
409 wxT("invalid item in wxRadioBox::Show()") );
411 BOOL ret
= ::ShowWindow((*m_radioButtons
)[item
], show
? SW_SHOW
: SW_HIDE
);
413 bool changed
= (ret
!= 0) != show
;
416 InvalidateBestSize();
422 bool wxRadioBox::IsItemShown(unsigned int item
) const
424 wxCHECK_MSG( IsValid(item
), false,
425 wxT("invalid item in wxRadioBox::IsItemShown()") );
427 // don't use IsWindowVisible() here because it would return false if the
428 // radiobox itself is hidden while we want to only return false if this
429 // button specifically is hidden
430 return (::GetWindowLong((*m_radioButtons
)[item
],
431 GWL_STYLE
) & WS_VISIBLE
) != 0;
436 bool wxRadioBox::HasToolTips() const
438 return wxStaticBox::HasToolTips() || wxRadioBoxBase::HasItemToolTips();
441 void wxRadioBox::DoSetItemToolTip(unsigned int item
, wxToolTip
*tooltip
)
443 // we have already checked for the item to be valid in wxRadioBoxBase
444 const HWND hwndRbtn
= (*m_radioButtons
)[item
];
445 if ( tooltip
!= NULL
)
446 tooltip
->Add(hwndRbtn
);
447 else // unset the tooltip
448 wxToolTip::Remove(hwndRbtn
);
451 #endif // wxUSE_TOOLTIPS
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 unsigned int count
= GetCount();
465 for ( unsigned 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
= GetRowCount() * sizeBtn
.y
+ cy1
/2 + extraHeight
;
502 int width
= GetColumnCount() * (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(GetLabelText(), &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 if ( !m_radioButtons
)
522 // if we're not fully initialized yet, we can't meaningfully compute
523 // our best size, we'll do it later
527 wxSize best
= GetTotalButtonSize(GetMaxButtonSize());
532 // Restored old code.
533 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
535 int currentX
, currentY
;
536 GetPosition(¤tX
, ¤tY
);
537 int widthOld
, heightOld
;
538 GetSize(&widthOld
, &heightOld
);
543 if (x
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
545 if (y
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
552 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
554 // Attempt to have a look coherent with other platforms: We compute the
555 // biggest toggle dim, then we align all items according this value.
556 wxSize maxSize
= GetMaxButtonSize();
557 int maxWidth
= maxSize
.x
,
558 maxHeight
= maxSize
.y
;
560 wxSize totSize
= GetTotalButtonSize(maxSize
);
561 int totWidth
= totSize
.x
,
562 totHeight
= totSize
.y
;
564 // only change our width/height if asked for
565 if ( width
== wxDefaultCoord
)
567 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
573 if ( height
== wxDefaultCoord
)
575 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
581 DoMoveWindow(xx
, yy
, width
, height
);
583 // Now position all the buttons: the current button will be put at
584 // wxPoint(x_offset, y_offset) and the new row/column will start at
585 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
586 // maxHeight) except for the buttons in the last column which should extend
587 // to the right border of radiobox and thus can be wider than this.
589 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
590 // left to right order and GetMajorDim() is the number of columns while
591 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
592 // GetMajorDim() is the number of rows.
597 // Add extra space under the label, if it exists.
598 if (!wxControl::GetLabel().empty())
601 int startX
= x_offset
;
602 int startY
= y_offset
;
604 const unsigned int count
= GetCount();
605 for (unsigned int i
= 0; i
< count
; i
++)
607 // the last button in the row may be wider than the other ones as the
608 // radiobox may be wider than the sum of the button widths (as it
609 // happens, for example, when the radiobox label is very long)
611 if ( m_windowStyle
& wxRA_SPECIFY_COLS
)
613 // item is the last in its row if it is a multiple of the number of
614 // columns or if it is just the last item
615 unsigned int n
= i
+ 1;
616 isLastInTheRow
= ((n
% GetMajorDim()) == 0) || (n
== count
);
618 else // wxRA_SPECIFY_ROWS
620 // item is the last in the row if it is in the last columns
621 isLastInTheRow
= i
>= (count
/GetMajorDim())*GetMajorDim();
624 // is this the start of new row/column?
625 if ( i
&& (i
% GetMajorDim() == 0) )
627 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
629 // start of new column
631 x_offset
+= maxWidth
+ cx1
;
633 else // start of new row
636 y_offset
+= maxHeight
;
637 if (m_radioWidth
[0]>0)
643 if ( isLastInTheRow
)
645 // make the button go to the end of radio box
646 widthBtn
= startX
+ width
- x_offset
- 2*cx1
;
647 if ( widthBtn
< maxWidth
)
652 // normal button, always of the same size
656 // make all buttons of the same, maximal size - like this they cover
657 // the radiobox entirely and the radiobox tooltips are always shown
658 // (otherwise they are not when the mouse pointer is in the radiobox
659 // part not belonging to any radiobutton)
660 DoMoveSibling((*m_radioButtons
)[i
], x_offset
, y_offset
, widthBtn
, maxHeight
);
662 // where do we put the next button?
663 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
666 y_offset
+= maxHeight
;
667 if (m_radioWidth
[0]>0)
672 // to the right of this one
673 x_offset
+= widthBtn
+ cx1
;
678 int wxRadioBox::GetItemFromPoint(const wxPoint
& pt
) const
680 const unsigned int count
= GetCount();
681 for ( unsigned int i
= 0; i
< count
; i
++ )
683 RECT rect
= wxGetWindowRect((*m_radioButtons
)[i
]);
685 if ( rect
.left
<= pt
.x
&& pt
.x
< rect
.right
&&
686 rect
.top
<= pt
.y
&& pt
.y
< rect
.bottom
)
695 // ----------------------------------------------------------------------------
697 // ----------------------------------------------------------------------------
701 WXHRGN
wxRadioBox::MSWGetRegionWithoutChildren()
704 ::GetWindowRect(GetHwnd(), &rc
);
705 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
707 const unsigned int count
= GetCount();
708 for ( unsigned int i
= 0; i
< count
; ++i
)
710 // don't clip out hidden children
711 if ( !IsItemShown(i
) )
714 ::GetWindowRect((*m_radioButtons
)[i
], &rc
);
715 AutoHRGN
hrgnchild(::CreateRectRgnIndirect(&rc
));
716 ::CombineRgn(hrgn
, hrgn
, hrgnchild
, RGN_DIFF
);
722 #endif // __WXWINCE__
724 // ---------------------------------------------------------------------------
725 // window proc for radio buttons
726 // ---------------------------------------------------------------------------
728 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
736 // we must tell IsDialogMessage()/our kbd processing code that we
737 // want to process arrows ourselves because neither of them is
738 // smart enough to handle arrows properly for us
740 long lDlgCode
= ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
,
741 message
, wParam
, lParam
);
743 return lDlgCode
| DLGC_WANTARROWS
;
748 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
750 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
752 bool processed
= true;
776 // just to suppress the compiler warning
782 int selOld
= radiobox
->GetSelection();
783 int selNew
= radiobox
->GetNextItem
787 radiobox
->GetWindowStyle()
790 if ( selNew
!= selOld
)
792 radiobox
->SetSelection(selNew
);
793 radiobox
->SetFocus();
795 // emulate the button click
796 radiobox
->SendNotificationEvent();
807 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
809 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
811 // if we don't do this, no focus events are generated for the
812 // radiobox and, besides, we need to notify the parent about
813 // the focus change, otherwise the focus handling logic in
814 // wxControlContainer doesn't work
815 if ( message
== WM_SETFOCUS
)
816 radiobox
->HandleSetFocus((WXHWND
)wParam
);
818 radiobox
->HandleKillFocus((WXHWND
)wParam
);
825 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
827 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
829 bool processed
= false;
831 wxEvtHandler
* const handler
= radiobox
->GetEventHandler();
833 HELPINFO
* info
= (HELPINFO
*) lParam
;
834 if ( info
->iContextType
== HELPINFO_WINDOW
)
836 for ( wxWindow
* subjectOfHelp
= radiobox
;
838 subjectOfHelp
= subjectOfHelp
->GetParent() )
840 wxHelpEvent
helpEvent(wxEVT_HELP
,
841 subjectOfHelp
->GetId(),
842 wxPoint(info
->MousePos
.x
,
844 helpEvent
.SetEventObject(radiobox
);
845 if ( handler
->ProcessEvent(helpEvent
) )
852 else if (info
->iContextType
== HELPINFO_MENUITEM
)
854 wxHelpEvent
helpEvent(wxEVT_HELP
, info
->iCtrlId
);
855 helpEvent
.SetEventObject(radiobox
);
856 processed
= handler
->ProcessEvent(helpEvent
);
863 #endif // !__WXWINCE__
866 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, message
, wParam
, lParam
);
869 #endif // wxUSE_RADIOBOX