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 // and now create the buttons
169 HWND hwndParent
= GetHwndOf(parent
);
171 m_radioButtons
= new wxSubwindows(n
);
172 m_radioWidth
= new int[n
];
173 m_radioHeight
= new int[n
];
175 for ( int i
= 0; i
< n
; i
++ )
178 m_radioHeight
[i
] = wxDefaultCoord
;
179 long styleBtn
= BS_AUTORADIOBUTTON
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
181 styleBtn
|= WS_GROUP
;
183 long newId
= NewControlId();
185 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
188 0, 0, 0, 0, // will be set in SetSize()
196 wxLogLastError(wxT("CreateWindow(radio btn)"));
201 (*m_radioButtons
)[i
] = hwndBtn
;
203 SubclassRadioButton((WXHWND
)hwndBtn
);
205 m_subControls
.Add(newId
);
208 // Create a dummy radio control to end the group.
209 (void)::CreateWindow(_T("BUTTON"),
211 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
212 0, 0, 0, 0, hwndParent
,
213 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
215 m_radioButtons
->SetFont(GetFont());
218 // Set the z-order correctly
219 SetWindowPos(GetHwnd(), HWND_BOTTOM
, 0, 0, 0, 0, SWP_NOMOVE
|SWP_NOSIZE
);
223 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
225 // Now that we have items determine what is the best size and set it.
231 bool wxRadioBox::Create(wxWindow
*parent
,
233 const wxString
& title
,
236 const wxArrayString
& choices
,
239 const wxValidator
& val
,
240 const wxString
& name
)
242 wxCArrayString
chs(choices
);
243 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
244 chs
.GetStrings(), majorDim
, style
, val
, name
);
247 wxRadioBox::~wxRadioBox()
249 m_isBeingDeleted
= true;
251 delete m_radioButtons
;
252 delete[] m_radioWidth
;
253 delete[] m_radioHeight
;
256 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
257 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
258 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
260 HWND hwndBtn
= (HWND
)hWndBtn
;
262 if ( !s_wndprocRadioBtn
)
263 s_wndprocRadioBtn
= (WXFARPROC
)wxGetWindowProc(hwndBtn
);
265 wxSetWindowProc(hwndBtn
, wxRadioBtnWndProc
);
266 wxSetWindowUserData(hwndBtn
, this);
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 bool wxRadioBox::MSWCommand(WXUINT cmd
, WXWORD id
)
275 if ( cmd
== BN_CLICKED
)
280 int selectedButton
= wxNOT_FOUND
;
282 int count
= GetCount();
283 for ( int i
= 0; i
< count
; i
++ )
285 if ( id
== wxGetWindowId((*m_radioButtons
)[i
]) )
293 if ( selectedButton
== wxNOT_FOUND
)
295 // just ignore it - due to a hack with WM_NCHITTEST handling in our
296 // wnd proc, we can receive dummy click messages when we click near
297 // the radiobox edge (this is ugly but Julian wouldn't let me get
302 if ( selectedButton
!= m_selectedButton
)
304 m_selectedButton
= selectedButton
;
306 SendNotificationEvent();
308 //else: don't generate events when the selection doesn't change
316 void wxRadioBox::Command(wxCommandEvent
& event
)
318 SetSelection (event
.GetInt());
320 ProcessCommand(event
);
323 void wxRadioBox::SendNotificationEvent()
325 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
326 event
.SetInt( m_selectedButton
);
327 event
.SetString( GetString(m_selectedButton
) );
328 event
.SetEventObject( this );
329 ProcessCommand(event
);
332 // ----------------------------------------------------------------------------
334 // ----------------------------------------------------------------------------
336 int wxRadioBox::GetCount() const
338 return m_radioButtons
->GetCount();
341 // returns the number of rows
342 int wxRadioBox::GetNumVer() const
344 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
350 return (GetCount() + m_majorDim
- 1)/m_majorDim
;
354 // returns the number of columns
355 int wxRadioBox::GetNumHor() const
357 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
359 return (GetCount() + m_majorDim
- 1)/m_majorDim
;
367 void wxRadioBox::SetString(int item
, const wxString
& label
)
369 wxCHECK_RET( IsValid(item
), wxT("invalid radiobox index") );
372 m_radioHeight
[item
] = wxDefaultCoord
;
374 ::SetWindowText((*m_radioButtons
)[item
], label
.c_str());
377 void wxRadioBox::SetSelection(int N
)
379 wxCHECK_RET( IsValid(N
), wxT("invalid radiobox index") );
381 // unselect the old button
382 if ( m_selectedButton
!= wxNOT_FOUND
)
383 ::SendMessage((*m_radioButtons
)[m_selectedButton
], BM_SETCHECK
, 0, 0L);
385 // and select the new one
386 ::SendMessage((*m_radioButtons
)[N
], BM_SETCHECK
, 1, 0L);
388 m_selectedButton
= N
;
391 // Find string for position
392 wxString
wxRadioBox::GetString(int item
) const
394 wxCHECK_MSG( IsValid(item
), wxEmptyString
,
395 wxT("invalid radiobox index") );
397 return wxGetWindowText((*m_radioButtons
)[item
]);
400 void wxRadioBox::SetFocus()
402 if ( GetCount() > 0 )
404 ::SetFocus((*m_radioButtons
)[m_selectedButton
== wxNOT_FOUND
406 : m_selectedButton
]);
410 // Enable a specific button
411 bool wxRadioBox::Enable(int item
, bool enable
)
413 wxCHECK_MSG( IsValid(item
), false,
414 wxT("invalid item in wxRadioBox::Enable()") );
416 BOOL ret
= ::EnableWindow((*m_radioButtons
)[item
], enable
);
418 return (ret
== 0) == enable
;
421 // Show a specific button
422 bool wxRadioBox::Show(int item
, bool show
)
424 wxCHECK_MSG( IsValid(item
), false,
425 wxT("invalid item in wxRadioBox::Show()") );
427 BOOL ret
= ::ShowWindow((*m_radioButtons
)[item
], show
? SW_SHOW
: SW_HIDE
);
429 return (ret
!= 0) == show
;
432 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxRadioBox
, wxStaticBox
, m_radioButtons
)
434 // ----------------------------------------------------------------------------
436 // ----------------------------------------------------------------------------
438 wxSize
wxRadioBox::GetMaxButtonSize() const
440 // calculate the max button size
443 const int count
= GetCount();
444 for ( int i
= 0 ; i
< count
; i
++ )
447 if ( m_radioWidth
[i
] < 0 )
449 GetTextExtent(wxGetWindowText((*m_radioButtons
)[i
]), &width
, &height
);
451 // adjust the size to take into account the radio box itself
452 // FIXME this is totally bogus!
459 width
= m_radioWidth
[i
];
460 height
= m_radioHeight
[i
];
463 if ( widthMax
< width
)
465 if ( heightMax
< height
)
469 return wxSize(widthMax
, heightMax
);
472 wxSize
wxRadioBox::GetTotalButtonSize(const wxSize
& sizeBtn
) const
474 // the radiobox should be big enough for its buttons
476 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
478 int extraHeight
= cy1
;
480 int height
= GetNumVer() * sizeBtn
.y
+ cy1
/2 + extraHeight
;
481 int width
= GetNumHor() * (sizeBtn
.x
+ cx1
) + cx1
;
483 // Add extra space under the label, if it exists.
484 if (!wxControl::GetLabel().empty())
487 // and also wide enough for its label
489 GetTextExtent(GetTitle(), &widthLabel
, NULL
);
490 widthLabel
+= RADIO_SIZE
; // FIXME this is bogus too
491 if ( widthLabel
> width
)
494 return wxSize(width
, height
);
497 wxSize
wxRadioBox::DoGetBestSize() const
499 return GetTotalButtonSize(GetMaxButtonSize());
502 // Restored old code.
503 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
505 int currentX
, currentY
;
506 GetPosition(¤tX
, ¤tY
);
507 int widthOld
, heightOld
;
508 GetSize(&widthOld
, &heightOld
);
513 if (x
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
515 if (y
== wxDefaultCoord
&& !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
522 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
524 // Attempt to have a look coherent with other platforms: We compute the
525 // biggest toggle dim, then we align all items according this value.
526 wxSize maxSize
= GetMaxButtonSize();
527 int maxWidth
= maxSize
.x
,
528 maxHeight
= maxSize
.y
;
530 wxSize totSize
= GetTotalButtonSize(maxSize
);
531 int totWidth
= totSize
.x
,
532 totHeight
= totSize
.y
;
534 // only change our width/height if asked for
535 if ( width
== wxDefaultCoord
)
537 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
543 if ( height
== wxDefaultCoord
)
545 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
551 ::MoveWindow(GetHwnd(), xx
, yy
, width
, height
, TRUE
);
553 // Now position all the buttons: the current button will be put at
554 // wxPoint(x_offset, y_offset) and the new row/column will start at
555 // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
556 // maxHeight) except for the buttons in the last column which should extend
557 // to the right border of radiobox and thus can be wider than this.
559 // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
560 // left to right order and m_majorDim is the number of columns while
561 // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
562 // m_majorDim is the number of rows.
567 // Add extra space under the label, if it exists.
568 if (!wxControl::GetLabel().empty())
571 int startX
= x_offset
;
572 int startY
= y_offset
;
574 const int count
= GetCount();
575 for ( int i
= 0; i
< count
; i
++ )
577 // the last button in the row may be wider than the other ones as the
578 // radiobox may be wider than the sum of the button widths (as it
579 // happens, for example, when the radiobox label is very long)
581 if ( m_windowStyle
& wxRA_SPECIFY_COLS
)
583 // item is the last in its row if it is a multiple of the number of
584 // columns or if it is just the last item
586 isLastInTheRow
= ((n
% m_majorDim
) == 0) || (n
== count
);
588 else // wxRA_SPECIFY_ROWS
590 // item is the last in the row if it is in the last columns
591 isLastInTheRow
= i
>= (count
/m_majorDim
)*m_majorDim
;
594 // is this the start of new row/column?
595 if ( i
&& (i
% m_majorDim
== 0) )
597 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
599 // start of new column
601 x_offset
+= maxWidth
+ cx1
;
603 else // start of new row
606 y_offset
+= maxHeight
;
607 if (m_radioWidth
[0]>0)
613 if ( isLastInTheRow
)
615 // make the button go to the end of radio box
616 widthBtn
= startX
+ width
- x_offset
- 2*cx1
;
617 if ( widthBtn
< maxWidth
)
622 // normal button, always of the same size
626 // make all buttons of the same, maximal size - like this they cover
627 // the radiobox entirely and the radiobox tooltips are always shown
628 // (otherwise they are not when the mouse pointer is in the radiobox
629 // part not belonging to any radiobutton)
630 ::MoveWindow((*m_radioButtons
)[i
],
631 x_offset
, y_offset
, widthBtn
, maxHeight
,
634 // where do we put the next button?
635 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
638 y_offset
+= maxHeight
;
639 if (m_radioWidth
[0]>0)
644 // to the right of this one
645 x_offset
+= widthBtn
+ cx1
;
650 // ---------------------------------------------------------------------------
651 // window proc for radio buttons
652 // ---------------------------------------------------------------------------
654 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
662 // we must tell IsDialogMessage()/our kbd processing code that we
663 // want to process arrows ourselves because neither of them is
664 // smart enough to handle arrows properly for us
666 long lDlgCode
= ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
,
667 message
, wParam
, lParam
);
669 return lDlgCode
| DLGC_WANTARROWS
;
675 NMHDR
* hdr
= (NMHDR
*)lParam
;
676 if ( hdr
->code
== TTN_NEEDTEXT
)
679 radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
681 wxCHECK_MSG( radiobox
, 0,
682 wxT("radio button without radio box?") );
684 wxToolTip
*tooltip
= radiobox
->GetToolTip();
687 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
688 ttt
->lpszText
= (wxChar
*)tooltip
->GetTip().c_str();
696 #endif // wxUSE_TOOLTIPS
700 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
702 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
704 bool processed
= true;
728 // just to suppress the compiler warning
734 int selOld
= radiobox
->GetSelection();
735 int selNew
= radiobox
->GetNextItem
739 radiobox
->GetWindowStyle()
742 if ( selNew
!= selOld
)
744 radiobox
->SetSelection(selNew
);
745 radiobox
->SetFocus();
747 // emulate the button click
748 radiobox
->SendNotificationEvent();
759 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
761 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
763 // if we don't do this, no focus events are generated for the
764 // radiobox and, besides, we need to notify the parent about
765 // the focus change, otherwise the focus handling logic in
766 // wxControlContainer doesn't work
767 if ( message
== WM_SETFOCUS
)
768 radiobox
->HandleSetFocus((WXHWND
)wParam
);
770 radiobox
->HandleKillFocus((WXHWND
)wParam
);
777 wxRadioBox
*radiobox
= (wxRadioBox
*)wxGetWindowUserData(hwnd
);
779 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
781 bool processed
= false;
783 wxEvtHandler
* const handler
= radiobox
->GetEventHandler();
785 HELPINFO
* info
= (HELPINFO
*) lParam
;
786 if ( info
->iContextType
== HELPINFO_WINDOW
)
788 for ( wxWindow
* subjectOfHelp
= radiobox
;
790 subjectOfHelp
= subjectOfHelp
->GetParent() )
792 wxHelpEvent
helpEvent(wxEVT_HELP
,
793 subjectOfHelp
->GetId(),
794 wxPoint(info
->MousePos
.x
,
796 helpEvent
.SetEventObject(radiobox
);
797 if ( handler
->ProcessEvent(helpEvent
) )
804 else if (info
->iContextType
== HELPINFO_MENUITEM
)
806 wxHelpEvent
helpEvent(wxEVT_HELP
, info
->iCtrlId
);
807 helpEvent
.SetEventObject(radiobox
);
808 processed
= handler
->ProcessEvent(helpEvent
);
816 #endif // !__WXWINCE__
819 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, message
, wParam
, lParam
);
822 #endif // wxUSE_RADIOBOX