1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "radiobox.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/bitmap.h"
36 #include "wx/radiobox.h"
37 #include "wx/settings.h"
41 #include "wx/msw/subwin.h"
43 // This is switched off because in some situations, the radiobox
44 // buttons simply don't appear when deferred sizing is on.
45 // Instead, refreshing on WM_MOVE seems to at least cure the droppings.
46 #define USE_DEFERRED_SIZING 0
49 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
52 #include "wx/tooltip.h"
53 #endif // wxUSE_TOOLTIPS
55 // TODO: wxCONSTRUCTOR
56 #if 0 // wxUSE_EXTENDED_RTTI
57 WX_DEFINE_FLAGS( wxRadioBoxStyle
)
59 wxBEGIN_FLAGS( wxRadioBoxStyle
)
60 // new style border flags, we put them first to
61 // use them for streaming out
62 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
63 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
64 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
65 wxFLAGS_MEMBER(wxBORDER_RAISED
)
66 wxFLAGS_MEMBER(wxBORDER_STATIC
)
67 wxFLAGS_MEMBER(wxBORDER_NONE
)
69 // old style border flags
70 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
71 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
72 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
73 wxFLAGS_MEMBER(wxRAISED_BORDER
)
74 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
75 wxFLAGS_MEMBER(wxBORDER
)
77 // standard window styles
78 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
79 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
80 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
81 wxFLAGS_MEMBER(wxWANTS_CHARS
)
82 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
83 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
84 wxFLAGS_MEMBER(wxVSCROLL
)
85 wxFLAGS_MEMBER(wxHSCROLL
)
87 wxFLAGS_MEMBER(wxRA_SPECIFY_COLS
)
88 wxFLAGS_MEMBER(wxRA_HORIZONTAL
)
89 wxFLAGS_MEMBER(wxRA_SPECIFY_ROWS
)
90 wxFLAGS_MEMBER(wxRA_VERTICAL
)
92 wxEND_FLAGS( wxRadioBoxStyle
)
94 IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioBox
, wxControl
,"wx/radiobox.h")
96 wxBEGIN_PROPERTIES_TABLE(wxRadioBox
)
97 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_RADIOBOX_SELECTED
, wxCommandEvent
)
98 wxPROPERTY_FLAGS( WindowStyle
, wxRadioBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
99 wxEND_PROPERTIES_TABLE()
102 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
113 // ---------------------------------------------------------------------------
115 // ---------------------------------------------------------------------------
117 // wnd proc for radio buttons
118 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
123 // ---------------------------------------------------------------------------
125 // ---------------------------------------------------------------------------
127 // the pointer to standard radio button wnd proc
128 static WXFARPROC s_wndprocRadioBtn
= (WXFARPROC
)NULL
;
130 // ===========================================================================
132 // ===========================================================================
134 // ---------------------------------------------------------------------------
135 // wxRadioBox creation
136 // ---------------------------------------------------------------------------
139 void wxRadioBox::Init()
141 m_selectedButton
= wxNOT_FOUND
;
142 m_radioButtons
= NULL
;
145 m_radioHeight
= NULL
;
148 bool wxRadioBox::Create(wxWindow
*parent
,
150 const wxString
& title
,
154 const wxString choices
[],
157 const wxValidator
& val
,
158 const wxString
& name
)
160 // initialize members
161 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
163 // common initialization
164 if ( !wxStaticBox::Create(parent
, id
, title
, pos
, size
, style
, name
) )
171 #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS
173 m_radioButtons
= new wxSubwindows(n
);
174 m_radioWidth
= new int[n
];
175 m_radioHeight
= new int[n
];
177 for ( int i
= 0; i
< n
; i
++ )
180 m_radioHeight
[i
] = wxDefaultCoord
;
181 long styleBtn
= BS_AUTORADIOBUTTON
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
183 styleBtn
|= WS_GROUP
;
185 long newId
= NewControlId();
187 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
190 0, 0, 0, 0, // will be set in SetSize()
198 wxLogLastError(wxT("CreateWindow(radio btn)"));
203 (*m_radioButtons
)[i
] = hwndBtn
;
205 SubclassRadioButton((WXHWND
)hwndBtn
);
207 m_subControls
.Add(newId
);
210 // Create a dummy radio control to end the group.
211 (void)::CreateWindow(_T("BUTTON"),
213 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
214 0, 0, 0, 0, GetHwnd(),
215 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
217 m_radioButtons
->SetFont(GetFont());
220 // Set the z-order correctly
221 SetWindowPos(GetHwnd(), HWND_BOTTOM
, 0, 0, 0, 0, SWP_NOMOVE
|SWP_NOSIZE
);
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.
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
;
254 delete[] m_radioWidth
;
255 delete[] m_radioHeight
;
258 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
259 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
260 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
262 HWND hwndBtn
= (HWND
)hWndBtn
;
264 if ( !s_wndprocRadioBtn
)
265 s_wndprocRadioBtn
= (WXFARPROC
)wxGetWindowProc(hwndBtn
);
267 wxSetWindowProc(hwndBtn
, wxRadioBtnWndProc
);
268 wxSetWindowUserData(hwndBtn
, this);
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 bool wxRadioBox::MSWCommand(WXUINT cmd
, WXWORD id
)
277 if ( cmd
== BN_CLICKED
)
282 int selectedButton
= wxNOT_FOUND
;
284 int count
= GetCount();
285 for ( int i
= 0; i
< count
; i
++ )
287 if ( id
== wxGetWindowId((*m_radioButtons
)[i
]) )
295 if ( selectedButton
== wxNOT_FOUND
)
297 // just ignore it - due to a hack with WM_NCHITTEST handling in our
298 // wnd proc, we can receive dummy click messages when we click near
299 // the radiobox edge (this is ugly but Julian wouldn't let me get
304 if ( selectedButton
!= m_selectedButton
)
306 m_selectedButton
= selectedButton
;
308 SendNotificationEvent();
310 //else: don't generate events when the selection doesn't change
318 void wxRadioBox::Command(wxCommandEvent
& event
)
320 SetSelection (event
.GetInt());
322 ProcessCommand(event
);
325 void wxRadioBox::SendNotificationEvent()
327 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
328 event
.SetInt( m_selectedButton
);
329 event
.SetString( GetString(m_selectedButton
) );
330 event
.SetEventObject( this );
331 ProcessCommand(event
);
334 // ----------------------------------------------------------------------------
336 // ----------------------------------------------------------------------------
338 int wxRadioBox::GetCount() const
340 return m_radioButtons
->GetCount();
343 // returns the number of rows
344 int wxRadioBox::GetNumVer() const
346 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
352 return (GetCount() + m_majorDim
- 1)/m_majorDim
;
356 // returns the number of columns
357 int wxRadioBox::GetNumHor() const
359 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
361 return (GetCount() + m_majorDim
- 1)/m_majorDim
;
369 void wxRadioBox::SetString(int item
, const wxString
& label
)
371 wxCHECK_RET( IsValid(item
), wxT("invalid radiobox index") );
374 m_radioHeight
[item
] = wxDefaultCoord
;
376 ::SetWindowText((*m_radioButtons
)[item
], label
.c_str());
378 InvalidateBestSize();
381 void wxRadioBox::SetSelection(int N
)
383 wxCHECK_RET( IsValid(N
), wxT("invalid radiobox index") );
385 // unselect the old button
386 if ( m_selectedButton
!= wxNOT_FOUND
)
387 ::SendMessage((*m_radioButtons
)[m_selectedButton
], BM_SETCHECK
, 0, 0L);
389 // and select the new one
390 ::SendMessage((*m_radioButtons
)[N
], BM_SETCHECK
, 1, 0L);
392 m_selectedButton
= N
;
395 // Find string for position
396 wxString
wxRadioBox::GetString(int item
) const
398 wxCHECK_MSG( IsValid(item
), wxEmptyString
,
399 wxT("invalid radiobox index") );
401 return wxGetWindowText((*m_radioButtons
)[item
]);
404 void wxRadioBox::SetFocus()
406 if ( GetCount() > 0 )
408 ::SetFocus((*m_radioButtons
)[m_selectedButton
== wxNOT_FOUND
410 : m_selectedButton
]);
414 // Enable a specific button
415 bool wxRadioBox::Enable(int item
, bool enable
)
417 wxCHECK_MSG( IsValid(item
), false,
418 wxT("invalid item in wxRadioBox::Enable()") );
420 BOOL ret
= ::EnableWindow((*m_radioButtons
)[item
], enable
);
422 return (ret
== 0) == enable
;
425 // Show a specific button
426 bool wxRadioBox::Show(int item
, bool show
)
428 wxCHECK_MSG( IsValid(item
), false,
429 wxT("invalid item in wxRadioBox::Show()") );
431 BOOL ret
= ::ShowWindow((*m_radioButtons
)[item
], show
? SW_SHOW
: SW_HIDE
);
433 bool changed
= (ret
!= 0) == show
;
435 InvalidateBestSize();
439 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox
, wxStaticBox
, m_radioButtons
)
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
445 wxSize
wxRadioBox::GetMaxButtonSize() const
447 // calculate the max button size
450 const int count
= GetCount();
451 for ( int i
= 0 ; i
< count
; i
++ )
454 if ( m_radioWidth
[i
] < 0 )
456 GetTextExtent(wxGetWindowText((*m_radioButtons
)[i
]), &width
, &height
);
458 // adjust the size to take into account the radio box itself
459 // FIXME this is totally bogus!
466 width
= m_radioWidth
[i
];
467 height
= m_radioHeight
[i
];
470 if ( widthMax
< width
)
472 if ( heightMax
< height
)
476 return wxSize(widthMax
, heightMax
);
479 wxSize
wxRadioBox::GetTotalButtonSize(const wxSize
& sizeBtn
) const
481 // the radiobox should be big enough for its buttons
483 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
485 int extraHeight
= cy1
;
487 int height
= GetNumVer() * sizeBtn
.y
+ cy1
/2 + extraHeight
;
488 int width
= GetNumHor() * (sizeBtn
.x
+ cx1
) + cx1
;
490 // Add extra space under the label, if it exists.
491 if (!wxControl::GetLabel().empty())
494 // and also wide enough for its label
496 GetTextExtent(GetTitle(), &widthLabel
, NULL
);
497 widthLabel
+= RADIO_SIZE
; // FIXME this is bogus too
498 if ( widthLabel
> width
)
501 return wxSize(width
, height
);
504 wxSize
wxRadioBox::DoGetBestSize() const
506 wxSize best
= GetTotalButtonSize(GetMaxButtonSize());
511 // Restored old code.
512 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
514 int currentX
, currentY
;
515 GetPosition(¤tX
, ¤tY
);
516 int widthOld
, heightOld
;
517 GetSize(&widthOld
, &heightOld
);
522 if (x
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
524 if (y
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
531 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
533 // Attempt to have a look coherent with other platforms: We compute the
534 // biggest toggle dim, then we align all items according this value.
535 wxSize maxSize
= GetMaxButtonSize();
536 int maxWidth
= maxSize
.x
,
537 maxHeight
= maxSize
.y
;
539 wxSize totSize
= GetTotalButtonSize(maxSize
);
540 int totWidth
= totSize
.x
,
541 totHeight
= totSize
.y
;
543 // only change our width/height if asked for
544 if ( width
== wxDefaultCoord
)
546 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
552 if ( height
== wxDefaultCoord
)
554 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
560 // if our parent had prepared a defer window handle for us, use it (unless
561 // we are a top level window)
563 #if USE_DEFERRED_SIZING
564 wxWindowMSW
*parent
= GetParent();
565 HDWP hdwp
= parent
&& !IsTopLevel() ? (HDWP
)parent
->m_hDWP
: NULL
;
570 wxMoveWindowDeferred(hdwp
, this, GetHwnd(), xx
, yy
, width
, height
);
572 // Now position all the buttons: the current button will be put at
573 // wxPoint(x_offset, y_offset) and the new row/column will start at
574 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
575 // maxHeight) except for the buttons in the last column which should extend
576 // to the right border of radiobox and thus can be wider than this.
578 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
579 // left to right order and m_majorDim is the number of columns while
580 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
581 // m_majorDim is the number of rows.
586 // Add extra space under the label, if it exists.
587 if (!wxControl::GetLabel().empty())
590 int startX
= x_offset
;
591 int startY
= y_offset
;
593 const int count
= GetCount();
594 for ( int i
= 0; i
< count
; i
++ )
596 // the last button in the row may be wider than the other ones as the
597 // radiobox may be wider than the sum of the button widths (as it
598 // happens, for example, when the radiobox label is very long)
600 if ( m_windowStyle
& wxRA_SPECIFY_COLS
)
602 // item is the last in its row if it is a multiple of the number of
603 // columns or if it is just the last item
605 isLastInTheRow
= ((n
% m_majorDim
) == 0) || (n
== count
);
607 else // wxRA_SPECIFY_ROWS
609 // item is the last in the row if it is in the last columns
610 isLastInTheRow
= i
>= (count
/m_majorDim
)*m_majorDim
;
613 // is this the start of new row/column?
614 if ( i
&& (i
% m_majorDim
== 0) )
616 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
618 // start of new column
620 x_offset
+= maxWidth
+ cx1
;
622 else // start of new row
625 y_offset
+= maxHeight
;
626 if (m_radioWidth
[0]>0)
632 if ( isLastInTheRow
)
634 // make the button go to the end of radio box
635 widthBtn
= startX
+ width
- x_offset
- 2*cx1
;
636 if ( widthBtn
< maxWidth
)
641 // normal button, always of the same size
645 // make all buttons of the same, maximal size - like this they cover
646 // the radiobox entirely and the radiobox tooltips are always shown
647 // (otherwise they are not when the mouse pointer is in the radiobox
648 // part not belonging to any radiobutton)
649 ::MoveWindow((*m_radioButtons
)[i
],
650 x_offset
, y_offset
, widthBtn
, maxHeight
,
653 // where do we put the next button?
654 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
657 y_offset
+= maxHeight
;
658 if (m_radioWidth
[0]>0)
663 // to the right of this one
664 x_offset
+= widthBtn
+ cx1
;
668 #if USE_DEFERRED_SIZING
671 // hdwp must be updated as it may have been changed
672 parent
->m_hDWP
= (WXHANDLE
)hdwp
;
677 // ----------------------------------------------------------------------------
679 // ----------------------------------------------------------------------------
683 WXHRGN
wxRadioBox::MSWGetRegionWithoutChildren()
686 ::GetWindowRect(GetHwnd(), &rc
);
687 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
689 const size_t count
= GetCount();
690 for ( size_t i
= 0; i
< count
; ++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