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"
44 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
47 #include "wx/tooltip.h"
48 #endif // wxUSE_TOOLTIPS
50 // TODO: wxCONSTRUCTOR
51 #if 0 // wxUSE_EXTENDED_RTTI
52 WX_DEFINE_FLAGS( wxRadioBoxStyle
)
54 wxBEGIN_FLAGS( wxRadioBoxStyle
)
55 // new style border flags, we put them first to
56 // use them for streaming out
57 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
58 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
59 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
60 wxFLAGS_MEMBER(wxBORDER_RAISED
)
61 wxFLAGS_MEMBER(wxBORDER_STATIC
)
62 wxFLAGS_MEMBER(wxBORDER_NONE
)
64 // old style border flags
65 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
66 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
67 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
68 wxFLAGS_MEMBER(wxRAISED_BORDER
)
69 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
70 wxFLAGS_MEMBER(wxBORDER
)
72 // standard window styles
73 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
74 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
75 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
76 wxFLAGS_MEMBER(wxWANTS_CHARS
)
77 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
78 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
79 wxFLAGS_MEMBER(wxVSCROLL
)
80 wxFLAGS_MEMBER(wxHSCROLL
)
82 wxFLAGS_MEMBER(wxRA_SPECIFY_COLS
)
83 wxFLAGS_MEMBER(wxRA_HORIZONTAL
)
84 wxFLAGS_MEMBER(wxRA_SPECIFY_ROWS
)
85 wxFLAGS_MEMBER(wxRA_VERTICAL
)
87 wxEND_FLAGS( wxRadioBoxStyle
)
89 IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioBox
, wxControl
,"wx/radiobox.h")
91 wxBEGIN_PROPERTIES_TABLE(wxRadioBox
)
92 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_RADIOBOX_SELECTED
, wxCommandEvent
)
93 wxPROPERTY_FLAGS( WindowStyle
, wxRadioBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
94 wxEND_PROPERTIES_TABLE()
97 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
108 // ---------------------------------------------------------------------------
110 // ---------------------------------------------------------------------------
112 // wnd proc for radio buttons
113 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
118 // ---------------------------------------------------------------------------
120 // ---------------------------------------------------------------------------
122 // the pointer to standard radio button wnd proc
123 static WXFARPROC s_wndprocRadioBtn
= (WXFARPROC
)NULL
;
125 // ===========================================================================
127 // ===========================================================================
129 // ---------------------------------------------------------------------------
130 // wxRadioBox creation
131 // ---------------------------------------------------------------------------
134 void wxRadioBox::Init()
136 m_selectedButton
= wxNOT_FOUND
;
137 m_radioButtons
= NULL
;
140 m_radioHeight
= NULL
;
143 bool wxRadioBox::Create(wxWindow
*parent
,
145 const wxString
& title
,
149 const wxString choices
[],
152 const wxValidator
& val
,
153 const wxString
& name
)
155 // initialize members
156 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
158 // common initialization
159 if ( !wxStaticBox::Create(parent
, id
, title
, pos
, size
, style
, name
) )
166 #endif // wxUSE_VALIDATORS/!wxUSE_VALIDATORS
168 m_radioButtons
= new wxSubwindows(n
);
169 m_radioWidth
= new int[n
];
170 m_radioHeight
= new int[n
];
172 for ( int i
= 0; i
< n
; i
++ )
175 m_radioHeight
[i
] = wxDefaultCoord
;
176 long styleBtn
= BS_AUTORADIOBUTTON
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
178 styleBtn
|= WS_GROUP
;
180 long newId
= NewControlId();
182 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
185 0, 0, 0, 0, // will be set in SetSize()
193 wxLogLastError(wxT("CreateWindow(radio btn)"));
198 (*m_radioButtons
)[i
] = hwndBtn
;
200 SubclassRadioButton((WXHWND
)hwndBtn
);
202 m_subControls
.Add(newId
);
205 // Create a dummy radio control to end the group.
206 (void)::CreateWindow(_T("BUTTON"),
208 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
209 0, 0, 0, 0, GetHwnd(),
210 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
212 m_radioButtons
->SetFont(GetFont());
215 // Set the z-order correctly
216 SetWindowPos(GetHwnd(), HWND_BOTTOM
, 0, 0, 0, 0, SWP_NOMOVE
|SWP_NOSIZE
);
220 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
222 // Now that we have items determine what is the best size and set it.
228 bool wxRadioBox::Create(wxWindow
*parent
,
230 const wxString
& title
,
233 const wxArrayString
& choices
,
236 const wxValidator
& val
,
237 const wxString
& name
)
239 wxCArrayString
chs(choices
);
240 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
241 chs
.GetStrings(), majorDim
, style
, val
, name
);
244 wxRadioBox::~wxRadioBox()
246 m_isBeingDeleted
= true;
248 delete m_radioButtons
;
249 delete[] m_radioWidth
;
250 delete[] m_radioHeight
;
253 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
254 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
255 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
257 HWND hwndBtn
= (HWND
)hWndBtn
;
259 if ( !s_wndprocRadioBtn
)
260 s_wndprocRadioBtn
= (WXFARPROC
)wxGetWindowProc(hwndBtn
);
262 wxSetWindowProc(hwndBtn
, wxRadioBtnWndProc
);
263 wxSetWindowUserData(hwndBtn
, this);
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
270 bool wxRadioBox::MSWCommand(WXUINT cmd
, WXWORD id
)
272 if ( cmd
== BN_CLICKED
)
277 int selectedButton
= wxNOT_FOUND
;
279 int count
= GetCount();
280 for ( int i
= 0; i
< count
; i
++ )
282 if ( id
== wxGetWindowId((*m_radioButtons
)[i
]) )
290 if ( selectedButton
== wxNOT_FOUND
)
292 // just ignore it - due to a hack with WM_NCHITTEST handling in our
293 // wnd proc, we can receive dummy click messages when we click near
294 // the radiobox edge (this is ugly but Julian wouldn't let me get
299 if ( selectedButton
!= m_selectedButton
)
301 m_selectedButton
= selectedButton
;
303 SendNotificationEvent();
305 //else: don't generate events when the selection doesn't change
313 void wxRadioBox::Command(wxCommandEvent
& event
)
315 SetSelection (event
.GetInt());
317 ProcessCommand(event
);
320 void wxRadioBox::SendNotificationEvent()
322 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
323 event
.SetInt( m_selectedButton
);
324 event
.SetString( GetString(m_selectedButton
) );
325 event
.SetEventObject( this );
326 ProcessCommand(event
);
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
333 int wxRadioBox::GetCount() const
335 return m_radioButtons
->GetCount();
338 // returns the number of rows
339 int wxRadioBox::GetNumVer() const
341 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
347 return (GetCount() + m_majorDim
- 1)/m_majorDim
;
351 // returns the number of columns
352 int wxRadioBox::GetNumHor() const
354 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
356 return (GetCount() + m_majorDim
- 1)/m_majorDim
;
364 void wxRadioBox::SetString(int item
, const wxString
& label
)
366 wxCHECK_RET( IsValid(item
), wxT("invalid radiobox index") );
369 m_radioHeight
[item
] = wxDefaultCoord
;
371 ::SetWindowText((*m_radioButtons
)[item
], label
.c_str());
374 void wxRadioBox::SetSelection(int N
)
376 wxCHECK_RET( IsValid(N
), wxT("invalid radiobox index") );
378 // unselect the old button
379 if ( m_selectedButton
!= wxNOT_FOUND
)
380 ::SendMessage((*m_radioButtons
)[m_selectedButton
], BM_SETCHECK
, 0, 0L);
382 // and select the new one
383 ::SendMessage((*m_radioButtons
)[N
], BM_SETCHECK
, 1, 0L);
385 m_selectedButton
= N
;
388 // Find string for position
389 wxString
wxRadioBox::GetString(int item
) const
391 wxCHECK_MSG( IsValid(item
), wxEmptyString
,
392 wxT("invalid radiobox index") );
394 return wxGetWindowText((*m_radioButtons
)[item
]);
397 void wxRadioBox::SetFocus()
399 if ( GetCount() > 0 )
401 ::SetFocus((*m_radioButtons
)[m_selectedButton
== wxNOT_FOUND
403 : m_selectedButton
]);
407 // Enable a specific button
408 bool wxRadioBox::Enable(int item
, bool enable
)
410 wxCHECK_MSG( IsValid(item
), false,
411 wxT("invalid item in wxRadioBox::Enable()") );
413 BOOL ret
= ::EnableWindow((*m_radioButtons
)[item
], enable
);
415 return (ret
== 0) == enable
;
418 // Show a specific button
419 bool wxRadioBox::Show(int item
, bool show
)
421 wxCHECK_MSG( IsValid(item
), false,
422 wxT("invalid item in wxRadioBox::Show()") );
424 BOOL ret
= ::ShowWindow((*m_radioButtons
)[item
], show
? SW_SHOW
: SW_HIDE
);
426 return (ret
!= 0) == show
;
429 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox
, wxStaticBox
, m_radioButtons
)
431 // ----------------------------------------------------------------------------
433 // ----------------------------------------------------------------------------
435 wxSize
wxRadioBox::GetMaxButtonSize() const
437 // calculate the max button size
440 const int count
= GetCount();
441 for ( int i
= 0 ; i
< count
; i
++ )
444 if ( m_radioWidth
[i
] < 0 )
446 GetTextExtent(wxGetWindowText((*m_radioButtons
)[i
]), &width
, &height
);
448 // adjust the size to take into account the radio box itself
449 // FIXME this is totally bogus!
456 width
= m_radioWidth
[i
];
457 height
= m_radioHeight
[i
];
460 if ( widthMax
< width
)
462 if ( heightMax
< height
)
466 return wxSize(widthMax
, heightMax
);
469 wxSize
wxRadioBox::GetTotalButtonSize(const wxSize
& sizeBtn
) const
471 // the radiobox should be big enough for its buttons
473 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
475 int extraHeight
= cy1
;
477 int height
= GetNumVer() * sizeBtn
.y
+ cy1
/2 + extraHeight
;
478 int width
= GetNumHor() * (sizeBtn
.x
+ cx1
) + cx1
;
480 // Add extra space under the label, if it exists.
481 if (!wxControl::GetLabel().empty())
484 // and also wide enough for its label
486 GetTextExtent(GetTitle(), &widthLabel
, NULL
);
487 widthLabel
+= RADIO_SIZE
; // FIXME this is bogus too
488 if ( widthLabel
> width
)
491 return wxSize(width
, height
);
494 wxSize
wxRadioBox::DoGetBestSize() const
496 return GetTotalButtonSize(GetMaxButtonSize());
499 // Restored old code.
500 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
502 int currentX
, currentY
;
503 GetPosition(¤tX
, ¤tY
);
504 int widthOld
, heightOld
;
505 GetSize(&widthOld
, &heightOld
);
510 if (x
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
512 if (y
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
519 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
521 // Attempt to have a look coherent with other platforms: We compute the
522 // biggest toggle dim, then we align all items according this value.
523 wxSize maxSize
= GetMaxButtonSize();
524 int maxWidth
= maxSize
.x
,
525 maxHeight
= maxSize
.y
;
527 wxSize totSize
= GetTotalButtonSize(maxSize
);
528 int totWidth
= totSize
.x
,
529 totHeight
= totSize
.y
;
531 // only change our width/height if asked for
532 if ( width
== wxDefaultCoord
)
534 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
540 if ( height
== wxDefaultCoord
)
542 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
548 ::MoveWindow(GetHwnd(), xx
, yy
, width
, height
, TRUE
);
550 // Now position all the buttons: the current button will be put at
551 // wxPoint(x_offset, y_offset) and the new row/column will start at
552 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
553 // maxHeight) except for the buttons in the last column which should extend
554 // to the right border of radiobox and thus can be wider than this.
556 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
557 // left to right order and m_majorDim is the number of columns while
558 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
559 // m_majorDim is the number of rows.
564 // Add extra space under the label, if it exists.
565 if (!wxControl::GetLabel().empty())
568 int startX
= x_offset
;
569 int startY
= y_offset
;
571 const int count
= GetCount();
572 for ( int i
= 0; i
< count
; i
++ )
574 // the last button in the row may be wider than the other ones as the
575 // radiobox may be wider than the sum of the button widths (as it
576 // happens, for example, when the radiobox label is very long)
578 if ( m_windowStyle
& wxRA_SPECIFY_COLS
)
580 // item is the last in its row if it is a multiple of the number of
581 // columns or if it is just the last item
583 isLastInTheRow
= ((n
% m_majorDim
) == 0) || (n
== count
);
585 else // wxRA_SPECIFY_ROWS
587 // item is the last in the row if it is in the last columns
588 isLastInTheRow
= i
>= (count
/m_majorDim
)*m_majorDim
;
591 // is this the start of new row/column?
592 if ( i
&& (i
% m_majorDim
== 0) )
594 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
596 // start of new column
598 x_offset
+= maxWidth
+ cx1
;
600 else // start of new row
603 y_offset
+= maxHeight
;
604 if (m_radioWidth
[0]>0)
610 if ( isLastInTheRow
)
612 // make the button go to the end of radio box
613 widthBtn
= startX
+ width
- x_offset
- 2*cx1
;
614 if ( widthBtn
< maxWidth
)
619 // normal button, always of the same size
623 // make all buttons of the same, maximal size - like this they cover
624 // the radiobox entirely and the radiobox tooltips are always shown
625 // (otherwise they are not when the mouse pointer is in the radiobox
626 // part not belonging to any radiobutton)
627 ::MoveWindow((*m_radioButtons
)[i
],
628 x_offset
, y_offset
, widthBtn
, maxHeight
,
631 // where do we put the next button?
632 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
635 y_offset
+= maxHeight
;
636 if (m_radioWidth
[0]>0)
641 // to the right of this one
642 x_offset
+= widthBtn
+ cx1
;
647 // ---------------------------------------------------------------------------
648 // window proc for radio buttons
649 // ---------------------------------------------------------------------------
651 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
659 // we must tell IsDialogMessage()/our kbd processing code that we
660 // want to process arrows ourselves because neither of them is
661 // smart enough to handle arrows properly for us
663 long lDlgCode
= ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
,
664 message
, wParam
, lParam
);
666 return lDlgCode
| DLGC_WANTARROWS
;
672 NMHDR
* hdr
= (NMHDR
*)lParam
;
673 if ( hdr
->code
== TTN_NEEDTEXT
)
676 radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
678 wxCHECK_MSG( radiobox
, 0,
679 wxT("radio button without radio box?") );
681 wxToolTip
*tooltip
= radiobox
->GetToolTip();
684 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
685 ttt
->lpszText
= (wxChar
*)tooltip
->GetTip().c_str();
693 #endif // wxUSE_TOOLTIPS
697 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
699 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
701 bool processed
= true;
725 // just to suppress the compiler warning
731 int selOld
= radiobox
->GetSelection();
732 int selNew
= radiobox
->GetNextItem
736 radiobox
->GetWindowStyle()
739 if ( selNew
!= selOld
)
741 radiobox
->SetSelection(selNew
);
742 radiobox
->SetFocus();
744 // emulate the button click
745 radiobox
->SendNotificationEvent();
756 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
758 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
760 // if we don't do this, no focus events are generated for the
761 // radiobox and, besides, we need to notify the parent about
762 // the focus change, otherwise the focus handling logic in
763 // wxControlContainer doesn't work
764 if ( message
== WM_SETFOCUS
)
765 radiobox
->HandleSetFocus((WXHWND
)wParam
);
767 radiobox
->HandleKillFocus((WXHWND
)wParam
);
774 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
776 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
778 bool processed
= false;
780 wxEvtHandler
* const handler
= radiobox
->GetEventHandler();
782 HELPINFO
* info
= (HELPINFO
*) lParam
;
783 if ( info
->iContextType
== HELPINFO_WINDOW
)
785 for ( wxWindow
* subjectOfHelp
= radiobox
;
787 subjectOfHelp
= subjectOfHelp
->GetParent() )
789 wxHelpEvent
helpEvent(wxEVT_HELP
,
790 subjectOfHelp
->GetId(),
791 wxPoint(info
->MousePos
.x
,
793 helpEvent
.SetEventObject(radiobox
);
794 if ( handler
->ProcessEvent(helpEvent
) )
801 else if (info
->iContextType
== HELPINFO_MENUITEM
)
803 wxHelpEvent
helpEvent(wxEVT_HELP
, info
->iCtrlId
);
804 helpEvent
.SetEventObject(radiobox
);
805 processed
= handler
->ProcessEvent(helpEvent
);
813 #endif // !__WXWINCE__
816 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, message
, wParam
, lParam
);
819 WXHRGN
wxRadioBox::MSWGetRegionWithoutChildren()
822 ::GetWindowRect(GetHwnd(), &rc
);
823 HRGN hrgn
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right
+ 1, rc
.bottom
+ 1);
825 const size_t count
= GetCount();
826 for ( size_t i
= 0; i
< count
; ++i
)
828 ::GetWindowRect((*m_radioButtons
)[i
], &rc
);
829 AutoHRGN
hrgnchild(::CreateRectRgnIndirect(&rc
));
830 ::CombineRgn(hrgn
, hrgn
, hrgnchild
, RGN_DIFF
);
836 WXLRESULT
wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
839 if ( nMsg
== WM_PRINTCLIENT
)
841 // first check to see if a parent window knows how to paint us better
842 for ( wxWindow
*win
= GetParent(); win
; win
= win
->GetParent() )
843 if ( win
->MSWPrintChild(this, wParam
, lParam
) )
846 // nope, so lets do it ourselves
848 WXHBRUSH hbr
= DoMSWControlColor((HDC
)wParam
, wxNullColour
);
851 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
852 hbr
= (WXHBRUSH
)brush
->GetResourceHandle();
855 ::GetClientRect(GetHwnd(), &rc
);
856 ::FillRect((HDC
)wParam
, &rc
, (HBRUSH
)hbr
);
863 return wxStaticBox::MSWWindowProc(nMsg
, wParam
, lParam
);
865 #endif // wxUSE_RADIOBOX