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 #include "wx/tooltip.h"
41 #endif // wxUSE_TOOLTIPS
43 // TODO: wxCONSTRUCTOR
44 #if 0 // wxUSE_EXTENDED_RTTI
45 WX_DEFINE_FLAGS( wxRadioBoxStyle
)
47 wxBEGIN_FLAGS( wxRadioBoxStyle
)
48 // new style border flags, we put them first to
49 // use them for streaming out
50 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
51 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
52 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
53 wxFLAGS_MEMBER(wxBORDER_RAISED
)
54 wxFLAGS_MEMBER(wxBORDER_STATIC
)
55 wxFLAGS_MEMBER(wxBORDER_NONE
)
57 // old style border flags
58 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
59 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
60 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
61 wxFLAGS_MEMBER(wxRAISED_BORDER
)
62 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
63 wxFLAGS_MEMBER(wxBORDER
)
65 // standard window styles
66 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
67 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
68 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
69 wxFLAGS_MEMBER(wxWANTS_CHARS
)
70 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
71 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
72 wxFLAGS_MEMBER(wxVSCROLL
)
73 wxFLAGS_MEMBER(wxHSCROLL
)
75 wxFLAGS_MEMBER(wxRA_SPECIFY_COLS
)
76 wxFLAGS_MEMBER(wxRA_HORIZONTAL
)
77 wxFLAGS_MEMBER(wxRA_SPECIFY_ROWS
)
78 wxFLAGS_MEMBER(wxRA_VERTICAL
)
80 wxEND_FLAGS( wxRadioBoxStyle
)
82 IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioBox
, wxControl
,"wx/radiobox.h")
84 wxBEGIN_PROPERTIES_TABLE(wxRadioBox
)
85 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_RADIOBOX_SELECTED
, wxCommandEvent
)
86 wxPROPERTY_FLAGS( WindowStyle
, wxRadioBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
87 wxEND_PROPERTIES_TABLE()
90 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
101 // ---------------------------------------------------------------------------
103 // ---------------------------------------------------------------------------
105 // wnd proc for radio buttons
106 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
111 // ---------------------------------------------------------------------------
113 // ---------------------------------------------------------------------------
115 // the pointer to standard radio button wnd proc
116 static WXFARPROC s_wndprocRadioBtn
= (WXFARPROC
)NULL
;
118 // ===========================================================================
120 // ===========================================================================
122 // ---------------------------------------------------------------------------
123 // wxRadioBox creation
124 // ---------------------------------------------------------------------------
127 void wxRadioBox::Init()
129 m_selectedButton
= wxNOT_FOUND
;
130 m_radioButtons
= NULL
;
132 m_radioHeight
= NULL
;
135 bool wxRadioBox::Create(wxWindow
*parent
,
137 const wxString
& title
,
141 const wxString choices
[],
144 const wxValidator
& val
,
145 const wxString
& name
)
147 // common initialization
148 if ( !wxStaticBox::Create(parent
, id
, title
, pos
, size
, style
, name
) )
155 #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS
157 m_radioButtons
= new wxSubwindows(n
);
158 m_radioWidth
= new int[n
];
159 m_radioHeight
= new int[n
];
161 for ( int i
= 0; i
< n
; i
++ )
164 m_radioHeight
[i
] = wxDefaultCoord
;
165 long styleBtn
= BS_AUTORADIOBUTTON
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
167 styleBtn
|= WS_GROUP
;
169 long newId
= NewControlId();
171 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
174 0, 0, 0, 0, // will be set in SetSize()
182 wxLogLastError(wxT("CreateWindow(radio btn)"));
187 (*m_radioButtons
)[i
] = hwndBtn
;
189 SubclassRadioButton((WXHWND
)hwndBtn
);
191 m_subControls
.Add(newId
);
194 // Create a dummy radio control to end the group.
195 (void)::CreateWindow(_T("BUTTON"),
197 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
198 0, 0, 0, 0, GetHwndOf(parent
),
199 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
201 m_radioButtons
->SetFont(GetFont());
204 // Set the z-order correctly
205 SetWindowPos(GetHwnd(), HWND_BOTTOM
, 0, 0, 0, 0, SWP_NOMOVE
|SWP_NOSIZE
);
208 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
210 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
212 // Now that we have items determine what is the best size and set it.
218 bool wxRadioBox::Create(wxWindow
*parent
,
220 const wxString
& title
,
223 const wxArrayString
& choices
,
226 const wxValidator
& val
,
227 const wxString
& name
)
229 wxCArrayString
chs(choices
);
230 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
231 chs
.GetStrings(), majorDim
, style
, val
, name
);
234 wxRadioBox::~wxRadioBox()
236 m_isBeingDeleted
= true;
238 delete m_radioButtons
;
239 delete[] m_radioWidth
;
240 delete[] m_radioHeight
;
243 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
244 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
245 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
247 HWND hwndBtn
= (HWND
)hWndBtn
;
249 if ( !s_wndprocRadioBtn
)
250 s_wndprocRadioBtn
= (WXFARPROC
)wxGetWindowProc(hwndBtn
);
252 wxSetWindowProc(hwndBtn
, wxRadioBtnWndProc
);
253 wxSetWindowUserData(hwndBtn
, this);
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 bool wxRadioBox::MSWCommand(WXUINT cmd
, WXWORD id
)
262 if ( cmd
== BN_CLICKED
)
267 int selectedButton
= wxNOT_FOUND
;
269 const unsigned int count
= GetCount();
270 for ( unsigned int i
= 0; i
< count
; i
++ )
272 if ( id
== wxGetWindowId((*m_radioButtons
)[i
]) )
280 if ( selectedButton
== wxNOT_FOUND
)
282 // just ignore it - due to a hack with WM_NCHITTEST handling in our
283 // wnd proc, we can receive dummy click messages when we click near
284 // the radiobox edge (this is ugly but Julian wouldn't let me get
289 if ( selectedButton
!= m_selectedButton
)
291 m_selectedButton
= selectedButton
;
293 SendNotificationEvent();
295 //else: don't generate events when the selection doesn't change
303 void wxRadioBox::Command(wxCommandEvent
& event
)
305 SetSelection (event
.GetInt());
307 ProcessCommand(event
);
310 void wxRadioBox::SendNotificationEvent()
312 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
313 event
.SetInt( m_selectedButton
);
314 event
.SetString(GetString(m_selectedButton
));
315 event
.SetEventObject( this );
316 ProcessCommand(event
);
319 // ----------------------------------------------------------------------------
321 // ----------------------------------------------------------------------------
323 unsigned int wxRadioBox::GetCount() const
325 return m_radioButtons
->GetCount();
328 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
330 wxCHECK_RET( IsValid(item
), wxT("invalid radiobox index") );
333 m_radioHeight
[item
] = wxDefaultCoord
;
335 ::SetWindowText((*m_radioButtons
)[item
], label
.c_str());
337 InvalidateBestSize();
340 void wxRadioBox::SetSelection(int N
)
342 wxCHECK_RET( IsValid(N
), wxT("invalid radiobox index") );
344 // unselect the old button
345 if ( m_selectedButton
!= wxNOT_FOUND
)
346 ::SendMessage((*m_radioButtons
)[m_selectedButton
], BM_SETCHECK
, 0, 0L);
348 // and select the new one
349 ::SendMessage((*m_radioButtons
)[N
], BM_SETCHECK
, 1, 0L);
351 m_selectedButton
= N
;
354 // Find string for position
355 wxString
wxRadioBox::GetString(unsigned int item
) const
357 wxCHECK_MSG( IsValid(item
), wxEmptyString
,
358 wxT("invalid radiobox index") );
360 return wxGetWindowText((*m_radioButtons
)[item
]);
363 void wxRadioBox::SetFocus()
365 if ( GetCount() > 0 )
367 ::SetFocus((*m_radioButtons
)[m_selectedButton
== wxNOT_FOUND
369 : m_selectedButton
]);
373 // Enable a specific button
374 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
376 wxCHECK_MSG( IsValid(item
), false,
377 wxT("invalid item in wxRadioBox::Enable()") );
379 BOOL ret
= ::EnableWindow((*m_radioButtons
)[item
], enable
);
381 return (ret
== 0) != enable
;
384 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
386 wxCHECK_MSG( IsValid(item
), false,
387 wxT("invalid item in wxRadioBox::IsItemEnabled()") );
389 return ::IsWindowEnabled((*m_radioButtons
)[item
]) != 0;
392 // Show a specific button
393 bool wxRadioBox::Show(unsigned int item
, bool show
)
395 wxCHECK_MSG( IsValid(item
), false,
396 wxT("invalid item in wxRadioBox::Show()") );
398 BOOL ret
= ::ShowWindow((*m_radioButtons
)[item
], show
? SW_SHOW
: SW_HIDE
);
400 bool changed
= (ret
!= 0) != show
;
403 InvalidateBestSize();
409 bool wxRadioBox::IsItemShown(unsigned int item
) const
411 wxCHECK_MSG( IsValid(item
), false,
412 wxT("invalid item in wxRadioBox::IsItemShown()") );
414 // don't use IsWindowVisible() here because it would return false if the
415 // radiobox itself is hidden while we want to only return false if this
416 // button specifically is hidden
417 return (::GetWindowLong((*m_radioButtons
)[item
],
418 GWL_STYLE
) & WS_VISIBLE
) != 0;
423 bool wxRadioBox::HasToolTips() const
425 return wxStaticBox::HasToolTips() || wxRadioBoxBase::HasItemToolTips();
428 void wxRadioBox::DoSetItemToolTip(unsigned int item
, wxToolTip
*tooltip
)
430 // we have already checked for the item to be valid in wxRadioBoxBase
431 const HWND hwndRbtn
= (*m_radioButtons
)[item
];
432 if ( tooltip
!= NULL
)
433 tooltip
->Add(hwndRbtn
);
434 else // unset the tooltip
435 wxToolTip::Remove(hwndRbtn
);
438 #endif // wxUSE_TOOLTIPS
440 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox
, wxStaticBox
, m_radioButtons
)
442 // ----------------------------------------------------------------------------
444 // ----------------------------------------------------------------------------
446 wxSize
wxRadioBox::GetMaxButtonSize() const
448 // calculate the max button size
451 const unsigned int count
= GetCount();
452 for ( unsigned int i
= 0 ; i
< count
; i
++ )
455 if ( m_radioWidth
[i
] < 0 )
457 GetTextExtent(wxGetWindowText((*m_radioButtons
)[i
]), &width
, &height
);
459 // adjust the size to take into account the radio box itself
460 // FIXME this is totally bogus!
467 width
= m_radioWidth
[i
];
468 height
= m_radioHeight
[i
];
471 if ( widthMax
< width
)
473 if ( heightMax
< height
)
477 return wxSize(widthMax
, heightMax
);
480 wxSize
wxRadioBox::GetTotalButtonSize(const wxSize
& sizeBtn
) const
482 // the radiobox should be big enough for its buttons
484 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
486 int extraHeight
= cy1
;
488 int height
= GetRowCount() * sizeBtn
.y
+ cy1
/2 + extraHeight
;
489 int width
= GetColumnCount() * (sizeBtn
.x
+ cx1
) + cx1
;
491 // Add extra space under the label, if it exists.
492 if (!wxControl::GetLabel().empty())
495 // and also wide enough for its label
497 GetTextExtent(wxStripMenuCodes(GetLabel()), &widthLabel
, NULL
);
498 widthLabel
+= RADIO_SIZE
; // FIXME this is bogus too
499 if ( widthLabel
> width
)
502 return wxSize(width
, height
);
505 wxSize
wxRadioBox::DoGetBestSize() const
507 wxSize best
= GetTotalButtonSize(GetMaxButtonSize());
512 // Restored old code.
513 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
515 int currentX
, currentY
;
516 GetPosition(¤tX
, ¤tY
);
517 int widthOld
, heightOld
;
518 GetSize(&widthOld
, &heightOld
);
523 if (x
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
525 if (y
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
532 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
534 // Attempt to have a look coherent with other platforms: We compute the
535 // biggest toggle dim, then we align all items according this value.
536 wxSize maxSize
= GetMaxButtonSize();
537 int maxWidth
= maxSize
.x
,
538 maxHeight
= maxSize
.y
;
540 wxSize totSize
= GetTotalButtonSize(maxSize
);
541 int totWidth
= totSize
.x
,
542 totHeight
= totSize
.y
;
544 // only change our width/height if asked for
545 if ( width
== wxDefaultCoord
)
547 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
553 if ( height
== wxDefaultCoord
)
555 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
561 DoMoveWindow(xx
, yy
, width
, height
);
563 // Now position all the buttons: the current button will be put at
564 // wxPoint(x_offset, y_offset) and the new row/column will start at
565 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
566 // maxHeight) except for the buttons in the last column which should extend
567 // to the right border of radiobox and thus can be wider than this.
569 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
570 // left to right order and GetMajorDim() is the number of columns while
571 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
572 // GetMajorDim() is the number of rows.
577 // Add extra space under the label, if it exists.
578 if (!wxControl::GetLabel().empty())
581 int startX
= x_offset
;
582 int startY
= y_offset
;
584 const unsigned int count
= GetCount();
585 for (unsigned int i
= 0; i
< count
; i
++)
587 // the last button in the row may be wider than the other ones as the
588 // radiobox may be wider than the sum of the button widths (as it
589 // happens, for example, when the radiobox label is very long)
591 if ( m_windowStyle
& wxRA_SPECIFY_COLS
)
593 // item is the last in its row if it is a multiple of the number of
594 // columns or if it is just the last item
595 unsigned int n
= i
+ 1;
596 isLastInTheRow
= ((n
% GetMajorDim()) == 0) || (n
== count
);
598 else // wxRA_SPECIFY_ROWS
600 // item is the last in the row if it is in the last columns
601 isLastInTheRow
= i
>= (count
/GetMajorDim())*GetMajorDim();
604 // is this the start of new row/column?
605 if ( i
&& (i
% GetMajorDim() == 0) )
607 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
609 // start of new column
611 x_offset
+= maxWidth
+ cx1
;
613 else // start of new row
616 y_offset
+= maxHeight
;
617 if (m_radioWidth
[0]>0)
623 if ( isLastInTheRow
)
625 // make the button go to the end of radio box
626 widthBtn
= startX
+ width
- x_offset
- 2*cx1
;
627 if ( widthBtn
< maxWidth
)
632 // normal button, always of the same size
636 // make all buttons of the same, maximal size - like this they cover
637 // the radiobox entirely and the radiobox tooltips are always shown
638 // (otherwise they are not when the mouse pointer is in the radiobox
639 // part not belonging to any radiobutton)
640 DoMoveSibling((*m_radioButtons
)[i
], x_offset
, y_offset
, widthBtn
, maxHeight
);
642 // where do we put the next button?
643 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
646 y_offset
+= maxHeight
;
647 if (m_radioWidth
[0]>0)
652 // to the right of this one
653 x_offset
+= widthBtn
+ cx1
;
658 // ----------------------------------------------------------------------------
660 // ----------------------------------------------------------------------------
664 WXHRGN
wxRadioBox::MSWGetRegionWithoutChildren()
667 ::GetWindowRect(GetHwnd(), &rc
);
668 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
670 const unsigned int count
= GetCount();
671 for ( unsigned int i
= 0; i
< count
; ++i
)
673 // don't clip out hidden children
674 if ( !IsItemShown(i
) )
677 ::GetWindowRect((*m_radioButtons
)[i
], &rc
);
678 AutoHRGN
hrgnchild(::CreateRectRgnIndirect(&rc
));
679 ::CombineRgn(hrgn
, hrgn
, hrgnchild
, RGN_DIFF
);
685 #endif // __WXWINCE__
687 // ---------------------------------------------------------------------------
688 // window proc for radio buttons
689 // ---------------------------------------------------------------------------
691 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
699 // we must tell IsDialogMessage()/our kbd processing code that we
700 // want to process arrows ourselves because neither of them is
701 // smart enough to handle arrows properly for us
703 long lDlgCode
= ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
,
704 message
, wParam
, lParam
);
706 return lDlgCode
| DLGC_WANTARROWS
;
711 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
713 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
715 bool processed
= true;
739 // just to suppress the compiler warning
745 int selOld
= radiobox
->GetSelection();
746 int selNew
= radiobox
->GetNextItem
750 radiobox
->GetWindowStyle()
753 if ( selNew
!= selOld
)
755 radiobox
->SetSelection(selNew
);
756 radiobox
->SetFocus();
758 // emulate the button click
759 radiobox
->SendNotificationEvent();
770 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
772 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
774 // if we don't do this, no focus events are generated for the
775 // radiobox and, besides, we need to notify the parent about
776 // the focus change, otherwise the focus handling logic in
777 // wxControlContainer doesn't work
778 if ( message
== WM_SETFOCUS
)
779 radiobox
->HandleSetFocus((WXHWND
)wParam
);
781 radiobox
->HandleKillFocus((WXHWND
)wParam
);
788 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
790 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
792 bool processed
= false;
794 wxEvtHandler
* const handler
= radiobox
->GetEventHandler();
796 HELPINFO
* info
= (HELPINFO
*) lParam
;
797 if ( info
->iContextType
== HELPINFO_WINDOW
)
799 for ( wxWindow
* subjectOfHelp
= radiobox
;
801 subjectOfHelp
= subjectOfHelp
->GetParent() )
803 wxHelpEvent
helpEvent(wxEVT_HELP
,
804 subjectOfHelp
->GetId(),
805 wxPoint(info
->MousePos
.x
,
807 helpEvent
.SetEventObject(radiobox
);
808 if ( handler
->ProcessEvent(helpEvent
) )
815 else if (info
->iContextType
== HELPINFO_MENUITEM
)
817 wxHelpEvent
helpEvent(wxEVT_HELP
, info
->iCtrlId
);
818 helpEvent
.SetEventObject(radiobox
);
819 processed
= handler
->ProcessEvent(helpEvent
);
826 #endif // !__WXWINCE__
829 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, message
, wParam
, lParam
);
832 #endif // wxUSE_RADIOBOX