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 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
211 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
213 // Now that we have items determine what is the best size and set it.
214 SetInitialSize(size
);
219 bool wxRadioBox::Create(wxWindow
*parent
,
221 const wxString
& title
,
224 const wxArrayString
& choices
,
227 const wxValidator
& val
,
228 const wxString
& name
)
230 wxCArrayString
chs(choices
);
231 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
232 chs
.GetStrings(), majorDim
, style
, val
, name
);
235 wxRadioBox::~wxRadioBox()
237 m_isBeingDeleted
= true;
239 delete m_radioButtons
;
240 delete[] m_radioWidth
;
241 delete[] m_radioHeight
;
244 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
245 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
246 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
248 HWND hwndBtn
= (HWND
)hWndBtn
;
250 if ( !s_wndprocRadioBtn
)
251 s_wndprocRadioBtn
= (WXFARPROC
)wxGetWindowProc(hwndBtn
);
253 wxSetWindowProc(hwndBtn
, wxRadioBtnWndProc
);
254 wxSetWindowUserData(hwndBtn
, this);
257 // ----------------------------------------------------------------------------
259 // ----------------------------------------------------------------------------
261 bool wxRadioBox::MSWCommand(WXUINT cmd
, WXWORD id_
)
263 const int id
= (signed short)id_
;
265 if ( cmd
== BN_CLICKED
)
270 int selectedButton
= wxNOT_FOUND
;
272 const unsigned int count
= GetCount();
273 for ( unsigned int i
= 0; i
< count
; i
++ )
275 const HWND hwndBtn
= (*m_radioButtons
)[i
];
276 if ( id
== wxGetWindowId(hwndBtn
) )
278 // we can get BN_CLICKED for a button which just became focused
279 // but it may not be checked, in which case we shouldn't
280 // generate a radiobox selection changed event for it
281 if ( ::SendMessage(hwndBtn
, BM_GETCHECK
, 0, 0) == BST_CHECKED
)
288 if ( selectedButton
== wxNOT_FOUND
)
290 // just ignore it - due to a hack with WM_NCHITTEST handling in our
291 // wnd proc, we can receive dummy click messages when we click near
292 // the radiobox edge (this is ugly but Julian wouldn't let me get
297 if ( selectedButton
!= m_selectedButton
)
299 m_selectedButton
= selectedButton
;
301 SendNotificationEvent();
303 //else: don't generate events when the selection doesn't change
311 void wxRadioBox::Command(wxCommandEvent
& event
)
313 SetSelection (event
.GetInt());
315 ProcessCommand(event
);
318 void wxRadioBox::SendNotificationEvent()
320 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
321 event
.SetInt( m_selectedButton
);
322 event
.SetString(GetString(m_selectedButton
));
323 event
.SetEventObject( this );
324 ProcessCommand(event
);
327 // ----------------------------------------------------------------------------
329 // ----------------------------------------------------------------------------
331 unsigned int wxRadioBox::GetCount() const
333 return m_radioButtons
? m_radioButtons
->GetCount() : 0u;
336 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
338 wxCHECK_RET( IsValid(item
), wxT("invalid radiobox index") );
341 m_radioHeight
[item
] = wxDefaultCoord
;
343 ::SetWindowText((*m_radioButtons
)[item
], label
.c_str());
345 InvalidateBestSize();
348 void wxRadioBox::SetSelection(int N
)
350 wxCHECK_RET( IsValid(N
), wxT("invalid radiobox index") );
352 // unselect the old button
353 if ( m_selectedButton
!= wxNOT_FOUND
)
354 ::SendMessage((*m_radioButtons
)[m_selectedButton
], BM_SETCHECK
, 0, 0L);
356 // and select the new one
357 ::SendMessage((*m_radioButtons
)[N
], BM_SETCHECK
, 1, 0L);
359 m_selectedButton
= N
;
362 // Find string for position
363 wxString
wxRadioBox::GetString(unsigned int item
) const
365 wxCHECK_MSG( IsValid(item
), wxEmptyString
,
366 wxT("invalid radiobox index") );
368 return wxGetWindowText((*m_radioButtons
)[item
]);
371 void wxRadioBox::SetFocus()
373 if ( GetCount() > 0 )
375 ::SetFocus((*m_radioButtons
)[m_selectedButton
== wxNOT_FOUND
377 : m_selectedButton
]);
381 // Enable a specific button
382 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
384 wxCHECK_MSG( IsValid(item
), false,
385 wxT("invalid item in wxRadioBox::Enable()") );
387 BOOL ret
= ::EnableWindow((*m_radioButtons
)[item
], enable
);
389 return (ret
== 0) != enable
;
392 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
394 wxCHECK_MSG( IsValid(item
), false,
395 wxT("invalid item in wxRadioBox::IsItemEnabled()") );
397 return ::IsWindowEnabled((*m_radioButtons
)[item
]) != 0;
400 // Show a specific button
401 bool wxRadioBox::Show(unsigned int item
, bool show
)
403 wxCHECK_MSG( IsValid(item
), false,
404 wxT("invalid item in wxRadioBox::Show()") );
406 BOOL ret
= ::ShowWindow((*m_radioButtons
)[item
], show
? SW_SHOW
: SW_HIDE
);
408 bool changed
= (ret
!= 0) != show
;
411 InvalidateBestSize();
417 bool wxRadioBox::IsItemShown(unsigned int item
) const
419 wxCHECK_MSG( IsValid(item
), false,
420 wxT("invalid item in wxRadioBox::IsItemShown()") );
422 // don't use IsWindowVisible() here because it would return false if the
423 // radiobox itself is hidden while we want to only return false if this
424 // button specifically is hidden
425 return (::GetWindowLong((*m_radioButtons
)[item
],
426 GWL_STYLE
) & WS_VISIBLE
) != 0;
431 bool wxRadioBox::HasToolTips() const
433 return wxStaticBox::HasToolTips() || wxRadioBoxBase::HasItemToolTips();
436 void wxRadioBox::DoSetItemToolTip(unsigned int item
, wxToolTip
*tooltip
)
438 // we have already checked for the item to be valid in wxRadioBoxBase
439 const HWND hwndRbtn
= (*m_radioButtons
)[item
];
440 if ( tooltip
!= NULL
)
441 tooltip
->Add(hwndRbtn
);
442 else // unset the tooltip
443 wxToolTip::Remove(hwndRbtn
);
446 #endif // wxUSE_TOOLTIPS
448 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox
, wxStaticBox
, m_radioButtons
)
450 // ----------------------------------------------------------------------------
452 // ----------------------------------------------------------------------------
454 wxSize
wxRadioBox::GetMaxButtonSize() const
456 // calculate the max button size
459 const unsigned int count
= GetCount();
460 for ( unsigned int i
= 0 ; i
< count
; i
++ )
463 if ( m_radioWidth
[i
] < 0 )
465 GetTextExtent(wxGetWindowText((*m_radioButtons
)[i
]), &width
, &height
);
467 // adjust the size to take into account the radio box itself
468 // FIXME this is totally bogus!
475 width
= m_radioWidth
[i
];
476 height
= m_radioHeight
[i
];
479 if ( widthMax
< width
)
481 if ( heightMax
< height
)
485 return wxSize(widthMax
, heightMax
);
488 wxSize
wxRadioBox::GetTotalButtonSize(const wxSize
& sizeBtn
) const
490 // the radiobox should be big enough for its buttons
492 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
494 int extraHeight
= cy1
;
496 int height
= GetRowCount() * sizeBtn
.y
+ cy1
/2 + extraHeight
;
497 int width
= GetColumnCount() * (sizeBtn
.x
+ cx1
) + cx1
;
499 // Add extra space under the label, if it exists.
500 if (!wxControl::GetLabel().empty())
503 // and also wide enough for its label
505 GetTextExtent(GetLabelText(), &widthLabel
, NULL
);
506 widthLabel
+= RADIO_SIZE
; // FIXME this is bogus too
507 if ( widthLabel
> width
)
510 return wxSize(width
, height
);
513 wxSize
wxRadioBox::DoGetBestSize() const
515 if ( !m_radioButtons
)
517 // if we're not fully initialized yet, we can't meaningfully compute
518 // our best size, we'll do it later
522 wxSize best
= GetTotalButtonSize(GetMaxButtonSize());
527 // Restored old code.
528 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
530 int currentX
, currentY
;
531 GetPosition(¤tX
, ¤tY
);
532 int widthOld
, heightOld
;
533 GetSize(&widthOld
, &heightOld
);
538 if (x
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
540 if (y
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
547 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
549 // Attempt to have a look coherent with other platforms: We compute the
550 // biggest toggle dim, then we align all items according this value.
551 wxSize maxSize
= GetMaxButtonSize();
552 int maxWidth
= maxSize
.x
,
553 maxHeight
= maxSize
.y
;
555 wxSize totSize
= GetTotalButtonSize(maxSize
);
556 int totWidth
= totSize
.x
,
557 totHeight
= totSize
.y
;
559 // only change our width/height if asked for
560 if ( width
== wxDefaultCoord
)
562 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
568 if ( height
== wxDefaultCoord
)
570 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
576 DoMoveWindow(xx
, yy
, width
, height
);
578 // Now position all the buttons: the current button will be put at
579 // wxPoint(x_offset, y_offset) and the new row/column will start at
580 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
581 // maxHeight) except for the buttons in the last column which should extend
582 // to the right border of radiobox and thus can be wider than this.
584 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
585 // left to right order and GetMajorDim() is the number of columns while
586 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
587 // GetMajorDim() is the number of rows.
592 // Add extra space under the label, if it exists.
593 if (!wxControl::GetLabel().empty())
596 int startX
= x_offset
;
597 int startY
= y_offset
;
599 const unsigned int count
= GetCount();
600 for (unsigned int i
= 0; i
< count
; i
++)
602 // the last button in the row may be wider than the other ones as the
603 // radiobox may be wider than the sum of the button widths (as it
604 // happens, for example, when the radiobox label is very long)
606 if ( m_windowStyle
& wxRA_SPECIFY_COLS
)
608 // item is the last in its row if it is a multiple of the number of
609 // columns or if it is just the last item
610 unsigned int n
= i
+ 1;
611 isLastInTheRow
= ((n
% GetMajorDim()) == 0) || (n
== count
);
613 else // wxRA_SPECIFY_ROWS
615 // item is the last in the row if it is in the last columns
616 isLastInTheRow
= i
>= (count
/GetMajorDim())*GetMajorDim();
619 // is this the start of new row/column?
620 if ( i
&& (i
% GetMajorDim() == 0) )
622 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
624 // start of new column
626 x_offset
+= maxWidth
+ cx1
;
628 else // start of new row
631 y_offset
+= maxHeight
;
632 if (m_radioWidth
[0]>0)
638 if ( isLastInTheRow
)
640 // make the button go to the end of radio box
641 widthBtn
= startX
+ width
- x_offset
- 2*cx1
;
642 if ( widthBtn
< maxWidth
)
647 // normal button, always of the same size
651 // make all buttons of the same, maximal size - like this they cover
652 // the radiobox entirely and the radiobox tooltips are always shown
653 // (otherwise they are not when the mouse pointer is in the radiobox
654 // part not belonging to any radiobutton)
655 DoMoveSibling((*m_radioButtons
)[i
], 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 int wxRadioBox::GetItemFromPoint(const wxPoint
& pt
) const
675 const unsigned int count
= GetCount();
676 for ( unsigned int i
= 0; i
< count
; i
++ )
678 RECT rect
= wxGetWindowRect((*m_radioButtons
)[i
]);
680 if ( rect
.left
<= pt
.x
&& pt
.x
< rect
.right
&&
681 rect
.top
<= pt
.y
&& pt
.y
< rect
.bottom
)
690 // ----------------------------------------------------------------------------
692 // ----------------------------------------------------------------------------
696 WXHRGN
wxRadioBox::MSWGetRegionWithoutChildren()
699 ::GetWindowRect(GetHwnd(), &rc
);
700 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
702 const unsigned int count
= GetCount();
703 for ( unsigned int i
= 0; i
< count
; ++i
)
705 // don't clip out hidden children
706 if ( !IsItemShown(i
) )
709 ::GetWindowRect((*m_radioButtons
)[i
], &rc
);
710 AutoHRGN
hrgnchild(::CreateRectRgnIndirect(&rc
));
711 ::CombineRgn(hrgn
, hrgn
, hrgnchild
, RGN_DIFF
);
717 #endif // __WXWINCE__
719 // ---------------------------------------------------------------------------
720 // window proc for radio buttons
721 // ---------------------------------------------------------------------------
723 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
731 // we must tell IsDialogMessage()/our kbd processing code that we
732 // want to process arrows ourselves because neither of them is
733 // smart enough to handle arrows properly for us
735 long lDlgCode
= ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
,
736 message
, wParam
, lParam
);
738 return lDlgCode
| DLGC_WANTARROWS
;
743 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
745 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
747 bool processed
= true;
771 // just to suppress the compiler warning
777 int selOld
= radiobox
->GetSelection();
778 int selNew
= radiobox
->GetNextItem
782 radiobox
->GetWindowStyle()
785 if ( selNew
!= selOld
)
787 radiobox
->SetSelection(selNew
);
788 radiobox
->SetFocus();
790 // emulate the button click
791 radiobox
->SendNotificationEvent();
802 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
804 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
806 // if we don't do this, no focus events are generated for the
807 // radiobox and, besides, we need to notify the parent about
808 // the focus change, otherwise the focus handling logic in
809 // wxControlContainer doesn't work
810 if ( message
== WM_SETFOCUS
)
811 radiobox
->HandleSetFocus((WXHWND
)wParam
);
813 radiobox
->HandleKillFocus((WXHWND
)wParam
);
820 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
822 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
824 bool processed
= false;
826 wxEvtHandler
* const handler
= radiobox
->GetEventHandler();
828 HELPINFO
* info
= (HELPINFO
*) lParam
;
829 if ( info
->iContextType
== HELPINFO_WINDOW
)
831 for ( wxWindow
* subjectOfHelp
= radiobox
;
833 subjectOfHelp
= subjectOfHelp
->GetParent() )
835 wxHelpEvent
helpEvent(wxEVT_HELP
,
836 subjectOfHelp
->GetId(),
837 wxPoint(info
->MousePos
.x
,
839 helpEvent
.SetEventObject(radiobox
);
840 if ( handler
->ProcessEvent(helpEvent
) )
847 else if (info
->iContextType
== HELPINFO_MENUITEM
)
849 wxHelpEvent
helpEvent(wxEVT_HELP
, info
->iCtrlId
);
850 helpEvent
.SetEventObject(radiobox
);
851 processed
= handler
->ProcessEvent(helpEvent
);
858 #endif // !__WXWINCE__
861 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, message
, wParam
, lParam
);
864 #endif // wxUSE_RADIOBOX