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"
35 #include "wx/settings.h"
39 #include "wx/msw/private.h"
42 #ifndef __GNUWIN32_OLD__
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 WXFARPROC s_wndprocRadioBtn
= (WXFARPROC
)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
)
132 int selectedButton
= -1;
134 for ( int i
= 0; i
< m_noItems
; i
++ )
136 if ( id
== wxGetWindowId(m_radioButtons
[i
]) )
144 wxASSERT_MSG( selectedButton
!= -1, wxT("click from alien button?") );
146 if ( selectedButton
!= m_selectedButton
)
148 m_selectedButton
= selectedButton
;
150 SendNotificationEvent();
152 //else: don't generate events when the selection doesn't change
160 #if WXWIN_COMPATIBILITY
161 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
162 int x
, int y
, int width
, int height
,
163 int n
, char **choices
,
164 int majorDim
, long style
, const char *name
)
166 wxString
*choices2
= new wxString
[n
];
167 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
168 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
169 wxDefaultValidator
, name
);
174 #endif // WXWIN_COMPATIBILITY
177 wxRadioBox::wxRadioBox()
179 m_selectedButton
= -1;
182 m_radioButtons
= NULL
;
185 m_radioHeight
= NULL
;
188 bool wxRadioBox::Create(wxWindow
*parent
,
190 const wxString
& title
,
194 const wxString choices
[],
197 const wxValidator
& val
,
198 const wxString
& name
)
200 // initialize members
201 m_selectedButton
= -1;
204 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
205 m_noRowsOrCols
= majorDim
;
207 // common initialization
208 if ( !CreateControl(parent
, id
, pos
, size
, style
, val
, name
) )
211 // create the static box
212 if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX
| WS_GROUP
,
213 pos
, size
, title
, 0) )
216 // and now create the buttons
218 #if RADIOBTN_PARENT_IS_RADIOBOX
219 HWND hwndParent
= GetHwnd();
221 HWND hwndParent
= GetHwndOf(parent
);
224 // Some radio boxes test consecutive id.
225 (void)NewControlId();
226 m_radioButtons
= new WXHWND
[n
];
227 m_radioWidth
= new int[n
];
228 m_radioHeight
= new int[n
];
231 wxFont
& font
= GetFont();
234 hfont
= font
.GetResourceHandle();
237 for ( int i
= 0; i
< n
; i
++ )
240 m_radioHeight
[i
] = -1;
241 long styleBtn
= BS_AUTORADIOBUTTON
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
242 if ( i
== 0 && style
== 0 )
243 styleBtn
|= WS_GROUP
;
245 long newId
= NewControlId();
247 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
250 0, 0, 0, 0, // will be set in SetSize()
258 wxLogLastError(wxT("CreateWindow(radio btn)"));
263 m_radioButtons
[i
] = (WXHWND
)hwndBtn
;
265 SubclassRadioButton((WXHWND
)hwndBtn
);
269 ::SendMessage(hwndBtn
, WM_SETFONT
, (WPARAM
)hfont
, 0L);
272 m_subControls
.Add(newId
);
275 // Create a dummy radio control to end the group.
276 (void)::CreateWindow(_T("BUTTON"),
278 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
279 0, 0, 0, 0, hwndParent
,
280 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
284 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
289 wxRadioBox::~wxRadioBox()
291 m_isBeingDeleted
= TRUE
;
296 for (i
= 0; i
< m_noItems
; i
++)
297 ::DestroyWindow((HWND
)m_radioButtons
[i
]);
298 delete[] m_radioButtons
;
302 delete[] m_radioWidth
;
304 delete[] m_radioHeight
;
308 wxString
wxRadioBox::GetLabel(int item
) const
310 wxCHECK_MSG( item
>= 0 && item
< m_noItems
, wxT(""), wxT("invalid radiobox index") );
312 return wxGetWindowText(m_radioButtons
[item
]);
315 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
317 wxCHECK_RET( item
>= 0 && item
< m_noItems
, wxT("invalid radiobox index") );
319 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
320 SetWindowText((HWND
)m_radioButtons
[item
], label
.c_str());
323 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
326 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
327 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
329 wxFAIL_MSG(wxT("not implemented"));
332 int wxRadioBox::FindString(const wxString
& s
) const
334 for (int i
= 0; i
< m_noItems
; i
++)
336 if ( s
== wxGetWindowText(m_radioButtons
[i
]) )
343 void wxRadioBox::SetSelection(int N
)
345 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), wxT("invalid radiobox index") );
347 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
348 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
349 ::SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
351 ::SendMessage((HWND
)m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
352 ::SetFocus((HWND
)m_radioButtons
[N
]);
354 m_selectedButton
= N
;
357 // Get single selection, for single choice list items
358 int wxRadioBox::GetSelection() const
360 return m_selectedButton
;
363 // Find string for position
364 wxString
wxRadioBox::GetString(int N
) const
366 return wxGetWindowText(m_radioButtons
[N
]);
369 // Restored old code.
370 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
372 int currentX
, currentY
;
373 GetPosition(¤tX
, ¤tY
);
374 int widthOld
, heightOld
;
375 GetSize(&widthOld
, &heightOld
);
380 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
382 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
385 #if RADIOBTN_PARENT_IS_RADIOBOX
393 int current_width
, cyf
;
396 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
398 // Attempt to have a look coherent with other platforms: We compute the
399 // biggest toggle dim, then we align all items according this value.
404 for (i
= 0 ; i
< m_noItems
; i
++)
408 if (m_radioWidth
[i
]<0)
410 // It's a labelled toggle
411 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
412 ¤t_width
, &cyf
);
413 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
414 eachHeight
= (int)((3*cyf
)/2);
418 eachWidth
= m_radioWidth
[i
];
419 eachHeight
= m_radioHeight
[i
];
422 if (maxWidth
<eachWidth
)
423 maxWidth
= eachWidth
;
424 if (maxHeight
<eachHeight
)
425 maxHeight
= eachHeight
;
433 int nbHor
= GetNumHor(),
436 // this formula works, but I don't know why.
437 // Please, be sure what you do if you modify it!!
438 if (m_radioWidth
[0]<0)
439 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
441 totHeight
= nbVer
* (maxHeight
+cy1
/2);
442 totWidth
= nbHor
* (maxWidth
+cx1
);
444 int extraHeight
= cy1
;
446 #if defined(CTL3D) && !CTL3D
447 // Requires a bigger group box in plain Windows
452 // only change our width/height if asked for
455 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
456 width
= totWidth
+ cx1
;
463 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
464 height
= totHeight
+ extraHeight
;
469 ::MoveWindow(GetHwnd(), xx
, yy
, width
, height
, TRUE
);
475 #if defined(CTL3D) && (!CTL3D)
476 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
477 // JACS 2/12/93. CTL3D draws group label quite high.
479 int startX
= x_offset
;
480 int startY
= y_offset
;
482 for ( i
= 0 ; i
< m_noItems
; i
++)
484 // Bidimensional radio adjustment
485 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
487 if (m_windowStyle
& wxRA_VERTICAL
)
490 x_offset
+= maxWidth
+ cx1
;
495 y_offset
+= maxHeight
;
496 if (m_radioWidth
[0]>0)
502 if (m_radioWidth
[i
]<0)
504 // It's a labeled item
505 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
506 ¤t_width
, &cyf
);
508 // How do we find out radio button bitmap size!!
509 // By adjusting them carefully, manually :-)
510 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
511 eachHeight
= (int)((3*cyf
)/2);
515 eachWidth
= m_radioWidth
[i
];
516 eachHeight
= m_radioHeight
[i
];
519 // VZ: make all buttons of the same, maximal size - like this they
520 // cover the radiobox entirely and the radiobox tooltips are always
521 // shown (otherwise they are not when the mouse pointer is in the
522 // radiobox part not belonging to any radiobutton)
523 ::MoveWindow((HWND
)m_radioButtons
[i
],
524 x_offset
, y_offset
, maxWidth
, maxHeight
,
527 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
529 y_offset
+= maxHeight
;
530 if (m_radioWidth
[0]>0)
534 x_offset
+= maxWidth
+ cx1
;
538 void wxRadioBox::GetSize(int *width
, int *height
) const
541 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
544 wxFindMaxSize(m_hWnd
, &rect
);
547 for (i
= 0; i
< m_noItems
; i
++)
548 wxFindMaxSize(m_radioButtons
[i
], &rect
);
550 *width
= rect
.right
- rect
.left
;
551 *height
= rect
.bottom
- rect
.top
;
554 void wxRadioBox::GetPosition(int *x
, int *y
) const
556 wxWindow
*parent
= GetParent();
557 RECT rect
= { -1, -1, -1, -1 };
560 for (i
= 0; i
< m_noItems
; i
++)
561 wxFindMaxSize(m_radioButtons
[i
], &rect
);
564 wxFindMaxSize(m_hWnd
, &rect
);
566 // Since we now have the absolute screen coords, if there's a parent we
567 // must subtract its top left corner
573 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
576 // We may be faking the client origin. So a window that's really at (0, 30)
577 // may appear (to wxWin apps) to be at (0, 0).
580 wxPoint
pt(GetParent()->GetClientAreaOrigin());
589 void wxRadioBox::SetFocus()
593 if (m_selectedButton
== -1)
594 ::SetFocus((HWND
) m_radioButtons
[0]);
596 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
601 bool wxRadioBox::Show(bool show
)
603 if ( !wxControl::Show(show
) )
606 int nCmdShow
= show
? SW_SHOW
: SW_HIDE
;
607 for ( int i
= 0; i
< m_noItems
; i
++ )
609 ::ShowWindow((HWND
)m_radioButtons
[i
], nCmdShow
);
615 // Enable a specific button
616 void wxRadioBox::Enable(int item
, bool enable
)
618 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
619 wxT("invalid item in wxRadioBox::Enable()") );
621 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
624 // Enable all controls
625 bool wxRadioBox::Enable(bool enable
)
627 if ( !wxControl::Enable(enable
) )
630 for (int i
= 0; i
< m_noItems
; i
++)
631 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
636 // Show a specific button
637 void wxRadioBox::Show(int item
, bool show
)
639 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
640 wxT("invalid item in wxRadioBox::Show()") );
642 ::ShowWindow((HWND
)m_radioButtons
[item
], show
? SW_SHOW
: SW_HIDE
);
645 // For single selection items only
646 wxString
wxRadioBox::GetStringSelection() const
649 int sel
= GetSelection();
651 result
= GetString(sel
);
656 bool wxRadioBox::SetStringSelection(const wxString
& s
)
658 int sel
= FindString (s
);
668 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
671 for (i
= 0; i
< Number(); i
++)
673 if (GetRadioButtons()[i
] == hWnd
)
680 void wxRadioBox::Command(wxCommandEvent
& event
)
682 SetSelection (event
.m_commandInt
);
683 ProcessCommand (event
);
686 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
687 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
688 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
690 // No GWL_USERDATA in Win16, so omit this subclassing.
692 HWND hwndBtn
= (HWND
)hWndBtn
;
694 if ( !s_wndprocRadioBtn
)
695 s_wndprocRadioBtn
= (WXFARPROC
)::GetWindowLong(hwndBtn
, GWL_WNDPROC
);
697 ::SetWindowLong(hwndBtn
, GWL_WNDPROC
, (long)wxRadioBtnWndProc
);
698 ::SetWindowLong(hwndBtn
, GWL_USERDATA
, (long)this);
702 void wxRadioBox::SendNotificationEvent()
704 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
705 event
.SetInt( m_selectedButton
);
706 event
.SetString( GetString(m_selectedButton
) );
707 event
.SetEventObject( this );
708 ProcessCommand(event
);
711 bool wxRadioBox::SetFont(const wxFont
& font
)
713 if ( !wxControl::SetFont(font
) )
719 // also set the font of our radio buttons
720 WXHFONT hfont
= wxFont(font
).GetResourceHandle();
721 for ( int n
= 0; n
< m_noItems
; n
++ )
723 HWND hwndBtn
= (HWND
)m_radioButtons
[n
];
724 ::SendMessage(hwndBtn
, WM_SETFONT
, (WPARAM
)hfont
, 0L);
726 // otherwise the buttons are not redrawn correctly
727 ::InvalidateRect(hwndBtn
, NULL
, FALSE
/* don't erase bg */);
733 // ----------------------------------------------------------------------------
735 // ----------------------------------------------------------------------------
737 long wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
742 case WM_CTLCOLORSTATIC
:
743 // set the colour of the radio buttons to be the same as ours
745 HDC hdc
= (HDC
)wParam
;
747 const wxColour
& colBack
= GetBackgroundColour();
748 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
749 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
751 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
753 return (WXHBRUSH
)brush
->GetResourceHandle();
757 // This is required for the radiobox to be sensitive to mouse input,
758 // e.g. for Dialog Editor.
761 int xPos
= LOWORD(lParam
); // horizontal position of cursor
762 int yPos
= HIWORD(lParam
); // vertical position of cursor
764 ScreenToClient(&xPos
, &yPos
);
766 // Make sure you can drag by the top of the groupbox, but let
767 // other (enclosed) controls get mouse events also
769 return (long)HTCLIENT
;
774 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
777 WXHBRUSH
wxRadioBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
785 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
786 return (WXHBRUSH
) hbrush
;
788 #endif // wxUSE_CTL3D
791 if (GetParent()->GetTransparentBackground())
792 SetBkMode(hdc
, TRANSPARENT
);
794 SetBkMode(hdc
, OPAQUE
);
796 wxColour colBack
= GetBackgroundColour();
799 colBack
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
801 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
802 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
804 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
806 return (WXHBRUSH
)brush
->GetResourceHandle();
810 // ---------------------------------------------------------------------------
811 // window proc for radio buttons
812 // ---------------------------------------------------------------------------
816 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
824 // we must tell IsDialogMessage()/our kbd processing code that we
825 // want to process arrows ourselves because neither of them is
826 // smart enough to handle arrows properly for us
828 long lDlgCode
= ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
,
829 message
, wParam
, lParam
);
831 return lDlgCode
| DLGC_WANTARROWS
;
837 NMHDR
* hdr
= (NMHDR
*)lParam
;
838 if ( (int)hdr
->code
== TTN_NEEDTEXT
)
840 wxRadioBox
*radiobox
= (wxRadioBox
*)
841 ::GetWindowLong(hwnd
, GWL_USERDATA
);
843 wxCHECK_MSG( radiobox
, 0,
844 wxT("radio button without radio box?") );
846 wxToolTip
*tooltip
= radiobox
->GetToolTip();
849 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
850 ttt
->lpszText
= (wxChar
*)tooltip
->GetTip().c_str();
858 #endif // wxUSE_TOOLTIPS
862 wxRadioBox
*radiobox
= (wxRadioBox
*)
863 ::GetWindowLong(hwnd
, GWL_USERDATA
);
865 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
867 bool processed
= TRUE
;
869 int selOld
= radiobox
->GetSelection();
879 selNew
-= radiobox
->GetNumVer();
887 selNew
+= radiobox
->GetNumVer();
896 // ensure that selNew is in range [0..num)
897 int num
= radiobox
->Number();
901 if ( selNew
!= selOld
)
903 radiobox
->SetSelection(selNew
);
905 // emulate the button click
906 radiobox
->SendNotificationEvent();
914 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, message
, wParam
, lParam
);