1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "radiobox.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/bitmap.h"
34 #include "wx/radiobox.h"
38 #include "wx/msw/private.h"
45 #include "wx/tooltip.h"
46 #endif // wxUSE_TOOLTIPS
48 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
50 // there are two possible ways to create the radio buttons: either as children
51 // of the radiobox or as siblings of it - allow playing with both variants for
52 // now, eventually we will choose the best one for our purposes
54 // two main problems are the keyboard navigation inside the radiobox (arrows
55 // should switch between buttons, not pass focus to the next control) and the
56 // tooltips - a tooltip is associated with the radiobox itself, not the
59 // the problems with setting this to 1:
60 // a) Alt-<mnemonic of radiobox> isn't handled properly by IsDialogMessage()
61 // because it sets focus to the next control accepting it which is not a
62 // radio button but a radiobox sibling in this case - the only solution to
63 // this would be to handle Alt-<mnemonic> ourselves
64 // b) the problems with setting radiobox colours under Win98/2K were reported
65 // but I couldn't reproduce it so I have no idea about what causes it
67 // the problems with setting this to 0:
68 // a) the tooltips are not shown for the radiobox - possible solution: make
69 // TTM_WINDOWFROMPOS handling code in msw/tooltip.cpp work (easier said than
70 // done because I don't know why it doesn't work)
71 #define RADIOBTN_PARENT_IS_RADIOBOX 0
73 // ---------------------------------------------------------------------------
75 // ---------------------------------------------------------------------------
77 // wnd proc for radio buttons
79 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
84 // ---------------------------------------------------------------------------
86 // ---------------------------------------------------------------------------
88 // the pointer to standard radio button wnd proc
89 static WNDPROC s_wndprocRadioBtn
= (WNDPROC
)NULL
;
93 // ===========================================================================
95 // ===========================================================================
97 // ---------------------------------------------------------------------------
99 // ---------------------------------------------------------------------------
101 int wxRadioBox
::GetNumVer() const
103 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
109 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
113 int wxRadioBox
::GetNumHor() const
115 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
117 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
125 bool wxRadioBox
::MSWCommand(WXUINT cmd
, WXWORD id
)
127 if ( cmd
== BN_CLICKED
)
129 int selectedButton
= -1;
131 for ( int i
= 0; i
< m_noItems
; i
++ )
133 if ( id
== wxGetWindowId(m_radioButtons
[i
]) )
141 wxASSERT_MSG( selectedButton
!= -1, wxT("click from alien button?") );
143 if ( selectedButton
!= m_selectedButton
)
145 m_selectedButton
= selectedButton
;
147 SendNotificationEvent();
149 //else: don't generate events when the selection doesn't change
157 #if WXWIN_COMPATIBILITY
158 wxRadioBox
::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
159 int x
, int y
, int width
, int height
,
160 int n
, char **choices
,
161 int majorDim
, long style
, const char *name
)
163 wxString
*choices2
= new wxString
[n
];
164 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
165 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
166 wxDefaultValidator
, name
);
171 #endif // WXWIN_COMPATIBILITY
174 wxRadioBox
::wxRadioBox()
176 m_selectedButton
= -1;
179 m_radioButtons
= NULL
;
182 m_radioHeight
= NULL
;
185 bool wxRadioBox
::Create(wxWindow
*parent
,
187 const wxString
& title
,
191 const wxString choices
[],
194 const wxValidator
& val
,
195 const wxString
& name
)
197 // initialize members
198 m_selectedButton
= -1;
201 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
202 m_noRowsOrCols
= majorDim
;
204 // common initialization
205 if ( !CreateControl(parent
, id
, pos
, size
, style
, val
, name
) )
208 // create the static box
209 if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX
| WS_GROUP
,
210 pos
, size
, title
, 0) )
213 // and now create the buttons
215 #if RADIOBTN_PARENT_IS_RADIOBOX
216 HWND hwndParent
= GetHwnd();
218 HWND hwndParent
= GetHwndOf(parent
);
221 // Some radio boxes test consecutive id.
222 (void)NewControlId();
223 m_radioButtons
= new WXHWND
[n
];
224 m_radioWidth
= new int[n
];
225 m_radioHeight
= new int[n
];
228 wxFont
& font
= GetFont();
231 hfont
= font
.GetResourceHandle();
234 for ( int i
= 0; i
< n
; i
++ )
237 m_radioHeight
[i
] = -1;
238 long styleBtn
= BS_AUTORADIOBUTTON
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
239 if ( i
== 0 && style
== 0 )
240 styleBtn
|= WS_GROUP
;
242 long newId
= NewControlId();
244 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
247 0, 0, 0, 0, // will be set in SetSize()
255 wxLogLastError("CreateWindow(radio btn)");
260 m_radioButtons
[i
] = (WXHWND
)hwndBtn
;
262 SubclassRadioButton((WXHWND
)hwndBtn
);
266 ::SendMessage(hwndBtn
, WM_SETFONT
, (WPARAM
)hfont
, 0L);
269 m_subControls
.Add(newId
);
272 // Create a dummy radio control to end the group.
273 (void)::CreateWindow(_T("BUTTON"),
275 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
276 0, 0, 0, 0, hwndParent
,
277 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
281 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
286 wxRadioBox
::~wxRadioBox()
288 m_isBeingDeleted
= TRUE
;
293 for (i
= 0; i
< m_noItems
; i
++)
294 ::DestroyWindow((HWND
)m_radioButtons
[i
]);
295 delete[] m_radioButtons
;
299 delete[] m_radioWidth
;
301 delete[] m_radioHeight
;
305 wxString wxRadioBox
::GetLabel(int item
) const
307 wxCHECK_MSG( item
>= 0 && item
< m_noItems
, wxT(""), wxT("invalid radiobox index") );
309 return wxGetWindowText(m_radioButtons
[item
]);
312 void wxRadioBox
::SetLabel(int item
, const wxString
& label
)
314 wxCHECK_RET( item
>= 0 && item
< m_noItems
, wxT("invalid radiobox index") );
316 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
317 SetWindowText((HWND
)m_radioButtons
[item
], label
.c_str());
320 void wxRadioBox
::SetLabel(int item
, wxBitmap
*bitmap
)
323 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
324 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
326 wxFAIL_MSG(wxT("not implemented"));
329 int wxRadioBox
::FindString(const wxString
& s
) const
331 for (int i
= 0; i
< m_noItems
; i
++)
333 if ( s
== wxGetWindowText(m_radioButtons
[i
]) )
340 void wxRadioBox
::SetSelection(int N
)
342 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), wxT("invalid radiobox index") );
344 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
345 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
346 ::SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
348 ::SendMessage((HWND
)m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
349 ::SetFocus((HWND
)m_radioButtons
[N
]);
351 m_selectedButton
= N
;
354 // Get single selection, for single choice list items
355 int wxRadioBox
::GetSelection() const
357 return m_selectedButton
;
360 // Find string for position
361 wxString wxRadioBox
::GetString(int N
) const
363 return wxGetWindowText(m_radioButtons
[N
]);
366 // Restored old code.
367 void wxRadioBox
::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
369 int currentX
, currentY
;
370 GetPosition(¤tX
, ¤tY
);
371 int widthOld
, heightOld
;
372 GetSize(&widthOld
, &heightOld
);
377 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
379 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
382 #if RADIOBTN_PARENT_IS_RADIOBOX
390 int current_width
, cyf
;
393 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
395 // Attempt to have a look coherent with other platforms: We compute the
396 // biggest toggle dim, then we align all items according this value.
401 for (i
= 0 ; i
< m_noItems
; i
++)
405 if (m_radioWidth
[i
]<0)
407 // It's a labelled toggle
408 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
409 ¤t_width
, &cyf
);
410 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
411 eachHeight
= (int)((3*cyf
)/2);
415 eachWidth
= m_radioWidth
[i
];
416 eachHeight
= m_radioHeight
[i
];
419 if (maxWidth
<eachWidth
)
420 maxWidth
= eachWidth
;
421 if (maxHeight
<eachHeight
)
422 maxHeight
= eachHeight
;
430 int nbHor
= GetNumHor(),
433 // this formula works, but I don't know why.
434 // Please, be sure what you do if you modify it!!
435 if (m_radioWidth
[0]<0)
436 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
438 totHeight
= nbVer
* (maxHeight
+cy1
/2);
439 totWidth
= nbHor
* (maxWidth
+cx1
);
441 int extraHeight
= cy1
;
443 #if defined(CTL3D) && !CTL3D
444 // Requires a bigger group box in plain Windows
449 // only change our width/height if asked for
452 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
453 width
= totWidth
+ cx1
;
460 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
461 height
= totHeight
+ extraHeight
;
466 ::MoveWindow(GetHwnd(), xx
, yy
, width
, height
, TRUE
);
472 #if defined(CTL3D) && (!CTL3D)
473 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
474 // JACS 2/12/93. CTL3D draws group label quite high.
476 int startX
= x_offset
;
477 int startY
= y_offset
;
479 for ( i
= 0 ; i
< m_noItems
; i
++)
481 // Bidimensional radio adjustment
482 if (i
&&((i
%m_majorDim
)==0)) // Why is this omitted for i = 0?
484 if (m_windowStyle
& wxRA_VERTICAL
)
487 x_offset
+= maxWidth
+ cx1
;
492 y_offset
+= maxHeight
;
493 if (m_radioWidth
[0]>0)
499 if (m_radioWidth
[i
]<0)
501 // It's a labeled item
502 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
503 ¤t_width
, &cyf
);
505 // How do we find out radio button bitmap size!!
506 // By adjusting them carefully, manually :-)
507 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
508 eachHeight
= (int)((3*cyf
)/2);
512 eachWidth
= m_radioWidth
[i
];
513 eachHeight
= m_radioHeight
[i
];
516 // VZ: make all buttons of the same, maximal size - like this they
517 // cover the radiobox entirely and the radiobox tooltips are always
518 // shown (otherwise they are not when the mouse pointer is in the
519 // radiobox part not belonging to any radiobutton)
520 ::MoveWindow((HWND
)m_radioButtons
[i
],
521 x_offset
, y_offset
, maxWidth
, maxHeight
,
524 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
526 y_offset
+= maxHeight
;
527 if (m_radioWidth
[0]>0)
531 x_offset
+= maxWidth
+ cx1
;
535 void wxRadioBox
::GetSize(int *width
, int *height
) const
538 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
541 wxFindMaxSize(m_hWnd
, &rect
);
544 for (i
= 0; i
< m_noItems
; i
++)
545 wxFindMaxSize(m_radioButtons
[i
], &rect
);
547 *width
= rect
.right
- rect
.left
;
548 *height
= rect
.bottom
- rect
.top
;
551 void wxRadioBox
::GetPosition(int *x
, int *y
) const
553 wxWindow
*parent
= GetParent();
554 RECT rect
= { -1, -1, -1, -1 };
557 for (i
= 0; i
< m_noItems
; i
++)
558 wxFindMaxSize(m_radioButtons
[i
], &rect
);
561 wxFindMaxSize(m_hWnd
, &rect
);
563 // Since we now have the absolute screen coords, if there's a parent we
564 // must subtract its top left corner
570 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
573 // We may be faking the client origin. So a window that's really at (0, 30)
574 // may appear (to wxWin apps) to be at (0, 0).
577 wxPoint
pt(GetParent()->GetClientAreaOrigin());
586 void wxRadioBox
::SetFocus()
590 if (m_selectedButton
== -1)
591 ::SetFocus((HWND
) m_radioButtons
[0]);
593 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
598 bool wxRadioBox
::Show(bool show
)
600 if ( !wxControl
::Show(show
) )
603 int nCmdShow
= show ? SW_SHOW
: SW_HIDE
;
604 for ( int i
= 0; i
< m_noItems
; i
++ )
606 ::ShowWindow((HWND
)m_radioButtons
[i
], nCmdShow
);
612 // Enable a specific button
613 void wxRadioBox
::Enable(int item
, bool enable
)
615 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
616 wxT("invalid item in wxRadioBox::Enable()") );
618 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
621 // Enable all controls
622 bool wxRadioBox
::Enable(bool enable
)
624 if ( !wxControl
::Enable(enable
) )
627 for (int i
= 0; i
< m_noItems
; i
++)
628 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
633 // Show a specific button
634 void wxRadioBox
::Show(int item
, bool show
)
636 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
637 wxT("invalid item in wxRadioBox::Show()") );
639 ::ShowWindow((HWND
)m_radioButtons
[item
], show ? SW_SHOW
: SW_HIDE
);
642 // For single selection items only
643 wxString wxRadioBox
::GetStringSelection() const
646 int sel
= GetSelection();
648 result
= GetString(sel
);
653 bool wxRadioBox
::SetStringSelection(const wxString
& s
)
655 int sel
= FindString (s
);
665 bool wxRadioBox
::ContainsHWND(WXHWND hWnd
) const
668 for (i
= 0; i
< Number(); i
++)
670 if (GetRadioButtons()[i
] == hWnd
)
677 void wxRadioBox
::Command(wxCommandEvent
& event
)
679 SetSelection (event
.m_commandInt
);
680 ProcessCommand (event
);
683 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
684 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
685 void wxRadioBox
::SubclassRadioButton(WXHWND hWndBtn
)
687 // No GWL_USERDATA in Win16, so omit this subclassing.
689 HWND hwndBtn
= (HWND
)hWndBtn
;
691 if ( !s_wndprocRadioBtn
)
692 s_wndprocRadioBtn
= (WNDPROC
)::GetWindowLong(hwndBtn
, GWL_WNDPROC
);
694 ::SetWindowLong(hwndBtn
, GWL_WNDPROC
, (long)wxRadioBtnWndProc
);
695 ::SetWindowLong(hwndBtn
, GWL_USERDATA
, (long)this);
699 void wxRadioBox
::SendNotificationEvent()
701 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
702 event
.SetInt( m_selectedButton
);
703 event
.SetString( GetString(m_selectedButton
) );
704 event
.SetEventObject( this );
705 ProcessCommand(event
);
708 bool wxRadioBox
::SetFont(const wxFont
& font
)
710 if ( !wxControl
::SetFont(font
) )
716 // also set the font of our radio buttons
717 WXHFONT hfont
= wxFont(font
).GetResourceHandle();
718 for ( int n
= 0; n
< m_noItems
; n
++ )
720 ::SendMessage((HWND
)m_radioButtons
[n
], WM_SETFONT
, (WPARAM
)hfont
, 0L);
723 // this is needed because otherwise the buttons are not redrawn correctly
729 // ----------------------------------------------------------------------------
731 // ----------------------------------------------------------------------------
733 long wxRadioBox
::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
738 case WM_CTLCOLORSTATIC
:
739 // set the colour of the radio buttons to be the same as ours
741 HDC hdc
= (HDC
)wParam
;
743 const wxColour
& colBack
= GetBackgroundColour();
744 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
745 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
747 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
749 return (WXHBRUSH
)brush
->GetResourceHandle();
753 // This is required for the radiobox to be sensitive to mouse input,
754 // e.g. for Dialog Editor.
757 int xPos
= LOWORD(lParam
); // horizontal position of cursor
758 int yPos
= HIWORD(lParam
); // vertical position of cursor
760 ScreenToClient(&xPos
, &yPos
);
762 // Make sure you can drag by the top of the groupbox, but let
763 // other (enclosed) controls get mouse events also
765 return (long)HTCLIENT
;
770 return wxControl
::MSWWindowProc(nMsg
, wParam
, lParam
);
773 // ---------------------------------------------------------------------------
774 // window proc for radio buttons
775 // ---------------------------------------------------------------------------
779 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
787 // we must tell IsDialogMessage()/our kbd processing code that we
788 // want to process arrows ourselves because neither of them is
789 // smart enough to handle arrows properly for us
791 long lDlgCode
= ::CallWindowProc(s_wndprocRadioBtn
, hwnd
,
792 message
, wParam
, lParam
);
793 return lDlgCode
| DLGC_WANTARROWS
;
799 NMHDR
* hdr
= (NMHDR
*)lParam
;
800 if ( (int)hdr
->code
== TTN_NEEDTEXT
)
802 wxRadioBox
*radiobox
= (wxRadioBox
*)
803 ::GetWindowLong(hwnd
, GWL_USERDATA
);
805 wxCHECK_MSG( radiobox
, 0,
806 wxT("radio button without radio box?") );
808 wxToolTip
*tooltip
= radiobox
->GetToolTip();
811 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
812 ttt
->lpszText
= (wxChar
*)tooltip
->GetTip().c_str();
820 #endif // wxUSE_TOOLTIPS
824 wxRadioBox
*radiobox
= (wxRadioBox
*)
825 ::GetWindowLong(hwnd
, GWL_USERDATA
);
827 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
829 bool processed
= TRUE
;
831 int selOld
= radiobox
->GetSelection();
841 selNew
-= radiobox
->GetNumVer();
849 selNew
+= radiobox
->GetNumVer();
858 // ensure that selNew is in range [0..num)
859 int num
= radiobox
->Number();
863 if ( selNew
!= selOld
)
865 radiobox
->SetSelection(selNew
);
867 // emulate the button click
868 radiobox
->SendNotificationEvent();
876 return ::CallWindowProc(s_wndprocRadioBtn
, hwnd
, message
, wParam
, lParam
);