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.
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 if ( cmd
== BN_CLICKED
)
268 int selectedButton
= wxNOT_FOUND
;
270 const unsigned int count
= GetCount();
271 for ( unsigned int i
= 0; i
< count
; i
++ )
273 if ( id
== wxGetWindowId((*m_radioButtons
)[i
]) )
281 if ( selectedButton
== wxNOT_FOUND
)
283 // just ignore it - due to a hack with WM_NCHITTEST handling in our
284 // wnd proc, we can receive dummy click messages when we click near
285 // the radiobox edge (this is ugly but Julian wouldn't let me get
290 if ( selectedButton
!= m_selectedButton
)
292 m_selectedButton
= selectedButton
;
294 SendNotificationEvent();
296 //else: don't generate events when the selection doesn't change
304 void wxRadioBox::Command(wxCommandEvent
& event
)
306 SetSelection (event
.GetInt());
308 ProcessCommand(event
);
311 void wxRadioBox::SendNotificationEvent()
313 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
314 event
.SetInt( m_selectedButton
);
315 event
.SetString(GetString(m_selectedButton
));
316 event
.SetEventObject( this );
317 ProcessCommand(event
);
320 // ----------------------------------------------------------------------------
322 // ----------------------------------------------------------------------------
324 unsigned int wxRadioBox::GetCount() const
326 return m_radioButtons
->GetCount();
329 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
331 wxCHECK_RET( IsValid(item
), wxT("invalid radiobox index") );
334 m_radioHeight
[item
] = wxDefaultCoord
;
336 ::SetWindowText((*m_radioButtons
)[item
], label
.c_str());
338 InvalidateBestSize();
341 void wxRadioBox::SetSelection(int N
)
343 wxCHECK_RET( IsValid(N
), wxT("invalid radiobox index") );
345 // unselect the old button
346 if ( m_selectedButton
!= wxNOT_FOUND
)
347 ::SendMessage((*m_radioButtons
)[m_selectedButton
], BM_SETCHECK
, 0, 0L);
349 // and select the new one
350 ::SendMessage((*m_radioButtons
)[N
], BM_SETCHECK
, 1, 0L);
352 m_selectedButton
= N
;
355 // Find string for position
356 wxString
wxRadioBox::GetString(unsigned int item
) const
358 wxCHECK_MSG( IsValid(item
), wxEmptyString
,
359 wxT("invalid radiobox index") );
361 return wxGetWindowText((*m_radioButtons
)[item
]);
364 void wxRadioBox::SetFocus()
366 if ( GetCount() > 0 )
368 ::SetFocus((*m_radioButtons
)[m_selectedButton
== wxNOT_FOUND
370 : m_selectedButton
]);
374 // Enable a specific button
375 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
377 wxCHECK_MSG( IsValid(item
), false,
378 wxT("invalid item in wxRadioBox::Enable()") );
380 BOOL ret
= ::EnableWindow((*m_radioButtons
)[item
], enable
);
382 return (ret
== 0) != enable
;
385 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
387 wxCHECK_MSG( IsValid(item
), false,
388 wxT("invalid item in wxRadioBox::IsItemEnabled()") );
390 return ::IsWindowEnabled((*m_radioButtons
)[item
]) != 0;
393 // Show a specific button
394 bool wxRadioBox::Show(unsigned int item
, bool show
)
396 wxCHECK_MSG( IsValid(item
), false,
397 wxT("invalid item in wxRadioBox::Show()") );
399 BOOL ret
= ::ShowWindow((*m_radioButtons
)[item
], show
? SW_SHOW
: SW_HIDE
);
401 bool changed
= (ret
!= 0) != show
;
404 InvalidateBestSize();
410 bool wxRadioBox::IsItemShown(unsigned int item
) const
412 wxCHECK_MSG( IsValid(item
), false,
413 wxT("invalid item in wxRadioBox::IsItemShown()") );
415 // don't use IsWindowVisible() here because it would return false if the
416 // radiobox itself is hidden while we want to only return false if this
417 // button specifically is hidden
418 return (::GetWindowLong((*m_radioButtons
)[item
],
419 GWL_STYLE
) & WS_VISIBLE
) != 0;
424 bool wxRadioBox::HasToolTips() const
426 return wxStaticBox::HasToolTips() || wxRadioBoxBase::HasItemToolTips();
429 void wxRadioBox::DoSetItemToolTip(unsigned int item
, wxToolTip
*tooltip
)
431 // we have already checked for the item to be valid in wxRadioBoxBase
432 const HWND hwndRbtn
= (*m_radioButtons
)[item
];
433 if ( tooltip
!= NULL
)
434 tooltip
->Add(hwndRbtn
);
435 else // unset the tooltip
436 wxToolTip::Remove(hwndRbtn
);
439 #endif // wxUSE_TOOLTIPS
441 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox
, wxStaticBox
, m_radioButtons
)
443 // ----------------------------------------------------------------------------
445 // ----------------------------------------------------------------------------
447 wxSize
wxRadioBox::GetMaxButtonSize() const
449 // calculate the max button size
452 const unsigned int count
= GetCount();
453 for ( unsigned int i
= 0 ; i
< count
; i
++ )
456 if ( m_radioWidth
[i
] < 0 )
458 GetTextExtent(wxGetWindowText((*m_radioButtons
)[i
]), &width
, &height
);
460 // adjust the size to take into account the radio box itself
461 // FIXME this is totally bogus!
468 width
= m_radioWidth
[i
];
469 height
= m_radioHeight
[i
];
472 if ( widthMax
< width
)
474 if ( heightMax
< height
)
478 return wxSize(widthMax
, heightMax
);
481 wxSize
wxRadioBox::GetTotalButtonSize(const wxSize
& sizeBtn
) const
483 // the radiobox should be big enough for its buttons
485 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
487 int extraHeight
= cy1
;
489 int height
= GetRowCount() * sizeBtn
.y
+ cy1
/2 + extraHeight
;
490 int width
= GetColumnCount() * (sizeBtn
.x
+ cx1
) + cx1
;
492 // Add extra space under the label, if it exists.
493 if (!wxControl::GetLabel().empty())
496 // and also wide enough for its label
498 GetTextExtent(GetLabelText(), &widthLabel
, NULL
);
499 widthLabel
+= RADIO_SIZE
; // FIXME this is bogus too
500 if ( widthLabel
> width
)
503 return wxSize(width
, height
);
506 wxSize
wxRadioBox::DoGetBestSize() const
508 wxSize best
= GetTotalButtonSize(GetMaxButtonSize());
513 // Restored old code.
514 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
516 int currentX
, currentY
;
517 GetPosition(¤tX
, ¤tY
);
518 int widthOld
, heightOld
;
519 GetSize(&widthOld
, &heightOld
);
524 if (x
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
526 if (y
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
533 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
535 // Attempt to have a look coherent with other platforms: We compute the
536 // biggest toggle dim, then we align all items according this value.
537 wxSize maxSize
= GetMaxButtonSize();
538 int maxWidth
= maxSize
.x
,
539 maxHeight
= maxSize
.y
;
541 wxSize totSize
= GetTotalButtonSize(maxSize
);
542 int totWidth
= totSize
.x
,
543 totHeight
= totSize
.y
;
545 // only change our width/height if asked for
546 if ( width
== wxDefaultCoord
)
548 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
554 if ( height
== wxDefaultCoord
)
556 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
562 DoMoveWindow(xx
, yy
, width
, height
);
564 // Now position all the buttons: the current button will be put at
565 // wxPoint(x_offset, y_offset) and the new row/column will start at
566 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
567 // maxHeight) except for the buttons in the last column which should extend
568 // to the right border of radiobox and thus can be wider than this.
570 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
571 // left to right order and GetMajorDim() is the number of columns while
572 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
573 // GetMajorDim() is the number of rows.
578 // Add extra space under the label, if it exists.
579 if (!wxControl::GetLabel().empty())
582 int startX
= x_offset
;
583 int startY
= y_offset
;
585 const unsigned int count
= GetCount();
586 for (unsigned int i
= 0; i
< count
; i
++)
588 // the last button in the row may be wider than the other ones as the
589 // radiobox may be wider than the sum of the button widths (as it
590 // happens, for example, when the radiobox label is very long)
592 if ( m_windowStyle
& wxRA_SPECIFY_COLS
)
594 // item is the last in its row if it is a multiple of the number of
595 // columns or if it is just the last item
596 unsigned int n
= i
+ 1;
597 isLastInTheRow
= ((n
% GetMajorDim()) == 0) || (n
== count
);
599 else // wxRA_SPECIFY_ROWS
601 // item is the last in the row if it is in the last columns
602 isLastInTheRow
= i
>= (count
/GetMajorDim())*GetMajorDim();
605 // is this the start of new row/column?
606 if ( i
&& (i
% GetMajorDim() == 0) )
608 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
610 // start of new column
612 x_offset
+= maxWidth
+ cx1
;
614 else // start of new row
617 y_offset
+= maxHeight
;
618 if (m_radioWidth
[0]>0)
624 if ( isLastInTheRow
)
626 // make the button go to the end of radio box
627 widthBtn
= startX
+ width
- x_offset
- 2*cx1
;
628 if ( widthBtn
< maxWidth
)
633 // normal button, always of the same size
637 // make all buttons of the same, maximal size - like this they cover
638 // the radiobox entirely and the radiobox tooltips are always shown
639 // (otherwise they are not when the mouse pointer is in the radiobox
640 // part not belonging to any radiobutton)
641 DoMoveSibling((*m_radioButtons
)[i
], x_offset
, y_offset
, widthBtn
, maxHeight
);
643 // where do we put the next button?
644 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
647 y_offset
+= maxHeight
;
648 if (m_radioWidth
[0]>0)
653 // to the right of this one
654 x_offset
+= widthBtn
+ cx1
;
659 int wxRadioBox::GetItemFromPoint(const wxPoint
& pt
) const
661 const unsigned int count
= GetCount();
662 for ( unsigned int i
= 0; i
< count
; i
++ )
664 RECT rect
= wxGetWindowRect((*m_radioButtons
)[i
]);
666 if ( rect
.left
<= pt
.x
&& pt
.x
< rect
.right
&&
667 rect
.top
<= pt
.y
&& pt
.y
< rect
.bottom
)
676 // ----------------------------------------------------------------------------
678 // ----------------------------------------------------------------------------
682 WXHRGN
wxRadioBox::MSWGetRegionWithoutChildren()
685 ::GetWindowRect(GetHwnd(), &rc
);
686 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
688 const unsigned int count
= GetCount();
689 for ( unsigned int i
= 0; i
< count
; ++i
)
691 // don't clip out hidden children
692 if ( !IsItemShown(i
) )
695 ::GetWindowRect((*m_radioButtons
)[i
], &rc
);
696 AutoHRGN
hrgnchild(::CreateRectRgnIndirect(&rc
));
697 ::CombineRgn(hrgn
, hrgn
, hrgnchild
, RGN_DIFF
);
703 #endif // __WXWINCE__
705 // ---------------------------------------------------------------------------
706 // window proc for radio buttons
707 // ---------------------------------------------------------------------------
709 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
717 // we must tell IsDialogMessage()/our kbd processing code that we
718 // want to process arrows ourselves because neither of them is
719 // smart enough to handle arrows properly for us
721 long lDlgCode
= ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
,
722 message
, wParam
, lParam
);
724 return lDlgCode
| DLGC_WANTARROWS
;
729 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
731 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
733 bool processed
= true;
757 // just to suppress the compiler warning
763 int selOld
= radiobox
->GetSelection();
764 int selNew
= radiobox
->GetNextItem
768 radiobox
->GetWindowStyle()
771 if ( selNew
!= selOld
)
773 radiobox
->SetSelection(selNew
);
774 radiobox
->SetFocus();
776 // emulate the button click
777 radiobox
->SendNotificationEvent();
788 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
790 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
792 // if we don't do this, no focus events are generated for the
793 // radiobox and, besides, we need to notify the parent about
794 // the focus change, otherwise the focus handling logic in
795 // wxControlContainer doesn't work
796 if ( message
== WM_SETFOCUS
)
797 radiobox
->HandleSetFocus((WXHWND
)wParam
);
799 radiobox
->HandleKillFocus((WXHWND
)wParam
);
806 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
808 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
810 bool processed
= false;
812 wxEvtHandler
* const handler
= radiobox
->GetEventHandler();
814 HELPINFO
* info
= (HELPINFO
*) lParam
;
815 if ( info
->iContextType
== HELPINFO_WINDOW
)
817 for ( wxWindow
* subjectOfHelp
= radiobox
;
819 subjectOfHelp
= subjectOfHelp
->GetParent() )
821 wxHelpEvent
helpEvent(wxEVT_HELP
,
822 subjectOfHelp
->GetId(),
823 wxPoint(info
->MousePos
.x
,
825 helpEvent
.SetEventObject(radiobox
);
826 if ( handler
->ProcessEvent(helpEvent
) )
833 else if (info
->iContextType
== HELPINFO_MENUITEM
)
835 wxHelpEvent
helpEvent(wxEVT_HELP
, info
->iCtrlId
);
836 helpEvent
.SetEventObject(radiobox
);
837 processed
= handler
->ProcessEvent(helpEvent
);
844 #endif // !__WXWINCE__
847 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, message
, wParam
, lParam
);
850 #endif // wxUSE_RADIOBOX