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
;
135 m_radioHeight
= NULL
;
138 bool wxRadioBox::Create(wxWindow
*parent
,
140 const wxString
& title
,
144 const wxString choices
[],
147 const wxValidator
& val
,
148 const wxString
& name
)
150 // common initialization
151 if ( !wxStaticBox::Create(parent
, id
, title
, pos
, size
, style
, name
) )
158 #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS
160 m_radioButtons
= new wxSubwindows(n
);
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 long newId
= NewControlId();
174 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
177 0, 0, 0, 0, // will be set in SetSize()
185 wxLogLastError(wxT("CreateWindow(radio btn)"));
190 (*m_radioButtons
)[i
] = hwndBtn
;
192 SubclassRadioButton((WXHWND
)hwndBtn
);
194 m_subControls
.Add(newId
);
197 // Create a dummy radio control to end the group.
198 (void)::CreateWindow(_T("BUTTON"),
200 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
201 0, 0, 0, 0, GetHwndOf(parent
),
202 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
204 m_radioButtons
->SetFont(GetFont());
207 // Set the z-order correctly
208 SetWindowPos(GetHwnd(), HWND_BOTTOM
, 0, 0, 0, 0, SWP_NOMOVE
|SWP_NOSIZE
);
211 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
213 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
215 // Now that we have items determine what is the best size and set it.
221 bool wxRadioBox::Create(wxWindow
*parent
,
223 const wxString
& title
,
226 const wxArrayString
& choices
,
229 const wxValidator
& val
,
230 const wxString
& name
)
232 wxCArrayString
chs(choices
);
233 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
234 chs
.GetStrings(), majorDim
, style
, val
, name
);
237 wxRadioBox::~wxRadioBox()
239 m_isBeingDeleted
= true;
241 delete m_radioButtons
;
242 delete[] m_radioWidth
;
243 delete[] m_radioHeight
;
246 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
247 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
248 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
250 HWND hwndBtn
= (HWND
)hWndBtn
;
252 if ( !s_wndprocRadioBtn
)
253 s_wndprocRadioBtn
= (WXFARPROC
)wxGetWindowProc(hwndBtn
);
255 wxSetWindowProc(hwndBtn
, wxRadioBtnWndProc
);
256 wxSetWindowUserData(hwndBtn
, this);
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
263 bool wxRadioBox::MSWCommand(WXUINT cmd
, WXWORD id
)
265 if ( cmd
== BN_CLICKED
)
270 int selectedButton
= wxNOT_FOUND
;
272 int count
= GetCount();
273 for ( int i
= 0; i
< count
; i
++ )
275 if ( id
== wxGetWindowId((*m_radioButtons
)[i
]) )
283 if ( selectedButton
== wxNOT_FOUND
)
285 // just ignore it - due to a hack with WM_NCHITTEST handling in our
286 // wnd proc, we can receive dummy click messages when we click near
287 // the radiobox edge (this is ugly but Julian wouldn't let me get
292 if ( selectedButton
!= m_selectedButton
)
294 m_selectedButton
= selectedButton
;
296 SendNotificationEvent();
298 //else: don't generate events when the selection doesn't change
306 void wxRadioBox::Command(wxCommandEvent
& event
)
308 SetSelection (event
.GetInt());
310 ProcessCommand(event
);
313 void wxRadioBox::SendNotificationEvent()
315 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
316 event
.SetInt( m_selectedButton
);
317 event
.SetString( GetString(m_selectedButton
) );
318 event
.SetEventObject( this );
319 ProcessCommand(event
);
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
326 int wxRadioBox::GetCount() const
328 return m_radioButtons
->GetCount();
331 void wxRadioBox::SetString(int item
, const wxString
& label
)
333 wxCHECK_RET( IsValid(item
), wxT("invalid radiobox index") );
336 m_radioHeight
[item
] = wxDefaultCoord
;
338 ::SetWindowText((*m_radioButtons
)[item
], label
.c_str());
340 InvalidateBestSize();
343 void wxRadioBox::SetSelection(int N
)
345 wxCHECK_RET( IsValid(N
), wxT("invalid radiobox index") );
347 // unselect the old button
348 if ( m_selectedButton
!= wxNOT_FOUND
)
349 ::SendMessage((*m_radioButtons
)[m_selectedButton
], BM_SETCHECK
, 0, 0L);
351 // and select the new one
352 ::SendMessage((*m_radioButtons
)[N
], BM_SETCHECK
, 1, 0L);
354 m_selectedButton
= N
;
357 // Find string for position
358 wxString
wxRadioBox::GetString(int item
) const
360 wxCHECK_MSG( IsValid(item
), wxEmptyString
,
361 wxT("invalid radiobox index") );
363 return wxGetWindowText((*m_radioButtons
)[item
]);
366 void wxRadioBox::SetFocus()
368 if ( GetCount() > 0 )
370 ::SetFocus((*m_radioButtons
)[m_selectedButton
== wxNOT_FOUND
372 : m_selectedButton
]);
376 // Enable a specific button
377 bool wxRadioBox::Enable(int item
, bool enable
)
379 wxCHECK_MSG( IsValid(item
), false,
380 wxT("invalid item in wxRadioBox::Enable()") );
382 BOOL ret
= ::EnableWindow((*m_radioButtons
)[item
], enable
);
384 return (ret
== 0) != enable
;
387 bool wxRadioBox::IsItemEnabled(int item
) const
389 wxCHECK_MSG( IsValid(item
), false,
390 wxT("invalid item in wxRadioBox::IsItemEnabled()") );
392 return ::IsWindowEnabled((*m_radioButtons
)[item
]) != 0;
395 // Show a specific button
396 bool wxRadioBox::Show(int item
, bool show
)
398 wxCHECK_MSG( IsValid(item
), false,
399 wxT("invalid item in wxRadioBox::Show()") );
401 BOOL ret
= ::ShowWindow((*m_radioButtons
)[item
], show
? SW_SHOW
: SW_HIDE
);
403 bool changed
= (ret
!= 0) != show
;
406 InvalidateBestSize();
412 bool wxRadioBox::IsItemShown(int item
) const
414 wxCHECK_MSG( IsValid(item
), false,
415 wxT("invalid item in wxRadioBox::IsItemShown()") );
417 // don't use IsWindowVisible() here because it would return false if the
418 // radiobox itself is hidden while we want to only return false if this
419 // button specifically is hidden
420 return (::GetWindowLong((*m_radioButtons
)[item
],
421 GWL_STYLE
) & WS_VISIBLE
) != 0;
424 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox
, wxStaticBox
, m_radioButtons
)
426 // ----------------------------------------------------------------------------
428 // ----------------------------------------------------------------------------
430 wxSize
wxRadioBox::GetMaxButtonSize() const
432 // calculate the max button size
435 const int count
= GetCount();
436 for ( int i
= 0 ; i
< count
; i
++ )
439 if ( m_radioWidth
[i
] < 0 )
441 GetTextExtent(wxGetWindowText((*m_radioButtons
)[i
]), &width
, &height
);
443 // adjust the size to take into account the radio box itself
444 // FIXME this is totally bogus!
451 width
= m_radioWidth
[i
];
452 height
= m_radioHeight
[i
];
455 if ( widthMax
< width
)
457 if ( heightMax
< height
)
461 return wxSize(widthMax
, heightMax
);
464 wxSize
wxRadioBox::GetTotalButtonSize(const wxSize
& sizeBtn
) const
466 // the radiobox should be big enough for its buttons
468 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
470 int extraHeight
= cy1
;
472 int height
= GetRowCount() * sizeBtn
.y
+ cy1
/2 + extraHeight
;
473 int width
= GetColumnCount() * (sizeBtn
.x
+ cx1
) + cx1
;
475 // Add extra space under the label, if it exists.
476 if (!wxControl::GetLabel().empty())
479 // and also wide enough for its label
481 GetTextExtent(GetLabel(), &widthLabel
, NULL
);
482 widthLabel
+= RADIO_SIZE
; // FIXME this is bogus too
483 if ( widthLabel
> width
)
486 return wxSize(width
, height
);
489 wxSize
wxRadioBox::DoGetBestSize() const
491 wxSize best
= GetTotalButtonSize(GetMaxButtonSize());
496 // Restored old code.
497 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
499 int currentX
, currentY
;
500 GetPosition(¤tX
, ¤tY
);
501 int widthOld
, heightOld
;
502 GetSize(&widthOld
, &heightOld
);
507 if (x
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
509 if (y
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
516 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
518 // Attempt to have a look coherent with other platforms: We compute the
519 // biggest toggle dim, then we align all items according this value.
520 wxSize maxSize
= GetMaxButtonSize();
521 int maxWidth
= maxSize
.x
,
522 maxHeight
= maxSize
.y
;
524 wxSize totSize
= GetTotalButtonSize(maxSize
);
525 int totWidth
= totSize
.x
,
526 totHeight
= totSize
.y
;
528 // only change our width/height if asked for
529 if ( width
== wxDefaultCoord
)
531 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
537 if ( height
== wxDefaultCoord
)
539 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
545 DoMoveWindow(xx
, yy
, width
, height
);
547 // Now position all the buttons: the current button will be put at
548 // wxPoint(x_offset, y_offset) and the new row/column will start at
549 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
550 // maxHeight) except for the buttons in the last column which should extend
551 // to the right border of radiobox and thus can be wider than this.
553 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
554 // left to right order and GetMajorDim() is the number of columns while
555 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
556 // GetMajorDim() is the number of rows.
561 // Add extra space under the label, if it exists.
562 if (!wxControl::GetLabel().empty())
565 int startX
= x_offset
;
566 int startY
= y_offset
;
568 const int count
= GetCount();
569 for ( int i
= 0; i
< count
; i
++ )
571 // the last button in the row may be wider than the other ones as the
572 // radiobox may be wider than the sum of the button widths (as it
573 // happens, for example, when the radiobox label is very long)
575 if ( m_windowStyle
& wxRA_SPECIFY_COLS
)
577 // item is the last in its row if it is a multiple of the number of
578 // columns or if it is just the last item
580 isLastInTheRow
= ((n
% GetMajorDim()) == 0) || (n
== count
);
582 else // wxRA_SPECIFY_ROWS
584 // item is the last in the row if it is in the last columns
585 isLastInTheRow
= i
>= (count
/GetMajorDim())*GetMajorDim();
588 // is this the start of new row/column?
589 if ( i
&& (i
% GetMajorDim() == 0) )
591 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
593 // start of new column
595 x_offset
+= maxWidth
+ cx1
;
597 else // start of new row
600 y_offset
+= maxHeight
;
601 if (m_radioWidth
[0]>0)
607 if ( isLastInTheRow
)
609 // make the button go to the end of radio box
610 widthBtn
= startX
+ width
- x_offset
- 2*cx1
;
611 if ( widthBtn
< maxWidth
)
616 // normal button, always of the same size
620 // make all buttons of the same, maximal size - like this they cover
621 // the radiobox entirely and the radiobox tooltips are always shown
622 // (otherwise they are not when the mouse pointer is in the radiobox
623 // part not belonging to any radiobutton)
624 DoMoveSibling((*m_radioButtons
)[i
], x_offset
, y_offset
, widthBtn
, maxHeight
);
626 // where do we put the next button?
627 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
630 y_offset
+= maxHeight
;
631 if (m_radioWidth
[0]>0)
636 // to the right of this one
637 x_offset
+= widthBtn
+ cx1
;
642 // ----------------------------------------------------------------------------
644 // ----------------------------------------------------------------------------
648 WXHRGN
wxRadioBox::MSWGetRegionWithoutChildren()
651 ::GetWindowRect(GetHwnd(), &rc
);
652 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
654 const size_t count
= GetCount();
655 for ( size_t i
= 0; i
< count
; ++i
)
657 // don't clip out hidden children
658 if ( !IsItemShown(i
) )
661 ::GetWindowRect((*m_radioButtons
)[i
], &rc
);
662 AutoHRGN
hrgnchild(::CreateRectRgnIndirect(&rc
));
663 ::CombineRgn(hrgn
, hrgn
, hrgnchild
, RGN_DIFF
);
669 #endif // __WXWINCE__
671 // ---------------------------------------------------------------------------
672 // window proc for radio buttons
673 // ---------------------------------------------------------------------------
675 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
683 // we must tell IsDialogMessage()/our kbd processing code that we
684 // want to process arrows ourselves because neither of them is
685 // smart enough to handle arrows properly for us
687 long lDlgCode
= ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
,
688 message
, wParam
, lParam
);
690 return lDlgCode
| DLGC_WANTARROWS
;
696 NMHDR
* hdr
= (NMHDR
*)lParam
;
697 if ( hdr
->code
== TTN_NEEDTEXT
)
700 radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
702 wxCHECK_MSG( radiobox
, 0,
703 wxT("radio button without radio box?") );
705 wxToolTip
*tooltip
= radiobox
->GetToolTip();
708 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
709 ttt
->lpszText
= (wxChar
*)tooltip
->GetTip().c_str();
717 #endif // wxUSE_TOOLTIPS
721 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
723 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
725 bool processed
= true;
749 // just to suppress the compiler warning
755 int selOld
= radiobox
->GetSelection();
756 int selNew
= radiobox
->GetNextItem
760 radiobox
->GetWindowStyle()
763 if ( selNew
!= selOld
)
765 radiobox
->SetSelection(selNew
);
766 radiobox
->SetFocus();
768 // emulate the button click
769 radiobox
->SendNotificationEvent();
780 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
782 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
784 // if we don't do this, no focus events are generated for the
785 // radiobox and, besides, we need to notify the parent about
786 // the focus change, otherwise the focus handling logic in
787 // wxControlContainer doesn't work
788 if ( message
== WM_SETFOCUS
)
789 radiobox
->HandleSetFocus((WXHWND
)wParam
);
791 radiobox
->HandleKillFocus((WXHWND
)wParam
);
798 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
800 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
802 bool processed
= false;
804 wxEvtHandler
* const handler
= radiobox
->GetEventHandler();
806 HELPINFO
* info
= (HELPINFO
*) lParam
;
807 if ( info
->iContextType
== HELPINFO_WINDOW
)
809 for ( wxWindow
* subjectOfHelp
= radiobox
;
811 subjectOfHelp
= subjectOfHelp
->GetParent() )
813 wxHelpEvent
helpEvent(wxEVT_HELP
,
814 subjectOfHelp
->GetId(),
815 wxPoint(info
->MousePos
.x
,
817 helpEvent
.SetEventObject(radiobox
);
818 if ( handler
->ProcessEvent(helpEvent
) )
825 else if (info
->iContextType
== HELPINFO_MENUITEM
)
827 wxHelpEvent
helpEvent(wxEVT_HELP
, info
->iCtrlId
);
828 helpEvent
.SetEventObject(radiobox
);
829 processed
= handler
->ProcessEvent(helpEvent
);
836 #endif // !__WXWINCE__
839 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, message
, wParam
, lParam
);
842 #endif // wxUSE_RADIOBOX