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"
41 #ifndef __GNUWIN32_OLD__
44 #include "wx/tooltip.h"
45 #endif // wxUSE_TOOLTIPS
47 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
49 // there are two possible ways to create the radio buttons: either as children
50 // of the radiobox or as siblings of it - allow playing with both variants for
51 // now, eventually we will choose the best one for our purposes
53 // two main problems are the keyboard navigation inside the radiobox (arrows
54 // should switch between buttons, not pass focus to the next control) and the
55 // tooltips - a tooltip is associated with the radiobox itself, not the
58 // the problems with setting this to 1:
59 // a) Alt-<mnemonic of radiobox> isn't handled properly by IsDialogMessage()
60 // because it sets focus to the next control accepting it which is not a
61 // radio button but a radiobox sibling in this case - the only solution to
62 // this would be to handle Alt-<mnemonic> ourselves
63 // b) the problems with setting radiobox colours under Win98/2K were reported
64 // but I couldn't reproduce it so I have no idea about what causes it
66 // the problems with setting this to 0:
67 // a) the tooltips are not shown for the radiobox - possible solution: make
68 // TTM_WINDOWFROMPOS handling code in msw/tooltip.cpp work (easier said than
69 // done because I don't know why it doesn't work)
70 #define RADIOBTN_PARENT_IS_RADIOBOX 0
72 // ---------------------------------------------------------------------------
74 // ---------------------------------------------------------------------------
76 // wnd proc for radio buttons
78 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
83 // ---------------------------------------------------------------------------
85 // ---------------------------------------------------------------------------
87 // the pointer to standard radio button wnd proc
88 static WNDPROC s_wndprocRadioBtn
= (WNDPROC
)NULL
;
92 // ===========================================================================
94 // ===========================================================================
96 // ---------------------------------------------------------------------------
98 // ---------------------------------------------------------------------------
100 int wxRadioBox::GetNumVer() const
102 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
108 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
112 int wxRadioBox::GetNumHor() const
114 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
116 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
124 bool wxRadioBox::MSWCommand(WXUINT cmd
, WXWORD id
)
126 if ( cmd
== BN_CLICKED
)
128 int selectedButton
= -1;
130 for ( int i
= 0; i
< m_noItems
; i
++ )
132 if ( id
== wxGetWindowId(m_radioButtons
[i
]) )
140 wxASSERT_MSG( selectedButton
!= -1, wxT("click from alien button?") );
142 if ( selectedButton
!= m_selectedButton
)
144 m_selectedButton
= selectedButton
;
146 SendNotificationEvent();
148 //else: don't generate events when the selection doesn't change
156 #if WXWIN_COMPATIBILITY
157 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
158 int x
, int y
, int width
, int height
,
159 int n
, char **choices
,
160 int majorDim
, long style
, const char *name
)
162 wxString
*choices2
= new wxString
[n
];
163 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
164 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
165 wxDefaultValidator
, name
);
170 #endif // WXWIN_COMPATIBILITY
173 wxRadioBox::wxRadioBox()
175 m_selectedButton
= -1;
178 m_radioButtons
= NULL
;
181 m_radioHeight
= NULL
;
184 bool wxRadioBox::Create(wxWindow
*parent
,
186 const wxString
& title
,
190 const wxString choices
[],
193 const wxValidator
& val
,
194 const wxString
& name
)
196 // initialize members
197 m_selectedButton
= -1;
200 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
201 m_noRowsOrCols
= majorDim
;
203 // common initialization
204 if ( !CreateControl(parent
, id
, pos
, size
, style
, val
, name
) )
207 // create the static box
208 if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX
| WS_GROUP
,
209 pos
, size
, title
, 0) )
212 // and now create the buttons
214 #if RADIOBTN_PARENT_IS_RADIOBOX
215 HWND hwndParent
= GetHwnd();
217 HWND hwndParent
= GetHwndOf(parent
);
220 // Some radio boxes test consecutive id.
221 (void)NewControlId();
222 m_radioButtons
= new WXHWND
[n
];
223 m_radioWidth
= new int[n
];
224 m_radioHeight
= new int[n
];
227 wxFont
& font
= GetFont();
230 hfont
= font
.GetResourceHandle();
233 for ( int i
= 0; i
< n
; i
++ )
236 m_radioHeight
[i
] = -1;
237 long styleBtn
= BS_AUTORADIOBUTTON
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
238 if ( i
== 0 && style
== 0 )
239 styleBtn
|= WS_GROUP
;
241 long newId
= NewControlId();
243 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
246 0, 0, 0, 0, // will be set in SetSize()
254 wxLogLastError("CreateWindow(radio btn)");
259 m_radioButtons
[i
] = (WXHWND
)hwndBtn
;
261 SubclassRadioButton((WXHWND
)hwndBtn
);
265 ::SendMessage(hwndBtn
, WM_SETFONT
, (WPARAM
)hfont
, 0L);
268 m_subControls
.Add(newId
);
271 // Create a dummy radio control to end the group.
272 (void)::CreateWindow(_T("BUTTON"),
274 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
275 0, 0, 0, 0, hwndParent
,
276 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
280 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
285 wxRadioBox::~wxRadioBox()
287 m_isBeingDeleted
= TRUE
;
292 for (i
= 0; i
< m_noItems
; i
++)
293 ::DestroyWindow((HWND
)m_radioButtons
[i
]);
294 delete[] m_radioButtons
;
298 delete[] m_radioWidth
;
300 delete[] m_radioHeight
;
304 wxString
wxRadioBox::GetLabel(int item
) const
306 wxCHECK_MSG( item
>= 0 && item
< m_noItems
, wxT(""), wxT("invalid radiobox index") );
308 return wxGetWindowText(m_radioButtons
[item
]);
311 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
313 wxCHECK_RET( item
>= 0 && item
< m_noItems
, wxT("invalid radiobox index") );
315 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
316 SetWindowText((HWND
)m_radioButtons
[item
], label
.c_str());
319 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
322 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
323 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
325 wxFAIL_MSG(wxT("not implemented"));
328 int wxRadioBox::FindString(const wxString
& s
) const
330 for (int i
= 0; i
< m_noItems
; i
++)
332 if ( s
== wxGetWindowText(m_radioButtons
[i
]) )
339 void wxRadioBox::SetSelection(int N
)
341 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), wxT("invalid radiobox index") );
343 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
344 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
345 ::SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
347 ::SendMessage((HWND
)m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
348 ::SetFocus((HWND
)m_radioButtons
[N
]);
350 m_selectedButton
= N
;
353 // Get single selection, for single choice list items
354 int wxRadioBox::GetSelection() const
356 return m_selectedButton
;
359 // Find string for position
360 wxString
wxRadioBox::GetString(int N
) const
362 return wxGetWindowText(m_radioButtons
[N
]);
365 // Restored old code.
366 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
368 int currentX
, currentY
;
369 GetPosition(¤tX
, ¤tY
);
370 int widthOld
, heightOld
;
371 GetSize(&widthOld
, &heightOld
);
376 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
378 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
381 #if RADIOBTN_PARENT_IS_RADIOBOX
389 int current_width
, cyf
;
392 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
394 // Attempt to have a look coherent with other platforms: We compute the
395 // biggest toggle dim, then we align all items according this value.
400 for (i
= 0 ; i
< m_noItems
; i
++)
404 if (m_radioWidth
[i
]<0)
406 // It's a labelled toggle
407 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
408 ¤t_width
, &cyf
);
409 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
410 eachHeight
= (int)((3*cyf
)/2);
414 eachWidth
= m_radioWidth
[i
];
415 eachHeight
= m_radioHeight
[i
];
418 if (maxWidth
<eachWidth
)
419 maxWidth
= eachWidth
;
420 if (maxHeight
<eachHeight
)
421 maxHeight
= eachHeight
;
429 int nbHor
= GetNumHor(),
432 // this formula works, but I don't know why.
433 // Please, be sure what you do if you modify it!!
434 if (m_radioWidth
[0]<0)
435 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
437 totHeight
= nbVer
* (maxHeight
+cy1
/2);
438 totWidth
= nbHor
* (maxWidth
+cx1
);
440 int extraHeight
= cy1
;
442 #if defined(CTL3D) && !CTL3D
443 // Requires a bigger group box in plain Windows
448 // only change our width/height if asked for
451 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
452 width
= totWidth
+ cx1
;
459 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
460 height
= totHeight
+ extraHeight
;
465 ::MoveWindow(GetHwnd(), xx
, yy
, width
, height
, TRUE
);
471 #if defined(CTL3D) && (!CTL3D)
472 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
473 // JACS 2/12/93. CTL3D draws group label quite high.
475 int startX
= x_offset
;
476 int startY
= y_offset
;
478 for ( i
= 0 ; i
< m_noItems
; i
++)
480 // Bidimensional radio adjustment
481 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
483 if (m_windowStyle
& wxRA_VERTICAL
)
486 x_offset
+= maxWidth
+ cx1
;
491 y_offset
+= maxHeight
;
492 if (m_radioWidth
[0]>0)
498 if (m_radioWidth
[i
]<0)
500 // It's a labeled item
501 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
502 ¤t_width
, &cyf
);
504 // How do we find out radio button bitmap size!!
505 // By adjusting them carefully, manually :-)
506 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
507 eachHeight
= (int)((3*cyf
)/2);
511 eachWidth
= m_radioWidth
[i
];
512 eachHeight
= m_radioHeight
[i
];
515 // VZ: make all buttons of the same, maximal size - like this they
516 // cover the radiobox entirely and the radiobox tooltips are always
517 // shown (otherwise they are not when the mouse pointer is in the
518 // radiobox part not belonging to any radiobutton)
519 ::MoveWindow((HWND
)m_radioButtons
[i
],
520 x_offset
, y_offset
, maxWidth
, maxHeight
,
523 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
525 y_offset
+= maxHeight
;
526 if (m_radioWidth
[0]>0)
530 x_offset
+= maxWidth
+ cx1
;
534 void wxRadioBox::GetSize(int *width
, int *height
) const
537 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
540 wxFindMaxSize(m_hWnd
, &rect
);
543 for (i
= 0; i
< m_noItems
; i
++)
544 wxFindMaxSize(m_radioButtons
[i
], &rect
);
546 *width
= rect
.right
- rect
.left
;
547 *height
= rect
.bottom
- rect
.top
;
550 void wxRadioBox::GetPosition(int *x
, int *y
) const
552 wxWindow
*parent
= GetParent();
553 RECT rect
= { -1, -1, -1, -1 };
556 for (i
= 0; i
< m_noItems
; i
++)
557 wxFindMaxSize(m_radioButtons
[i
], &rect
);
560 wxFindMaxSize(m_hWnd
, &rect
);
562 // Since we now have the absolute screen coords, if there's a parent we
563 // must subtract its top left corner
569 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
572 // We may be faking the client origin. So a window that's really at (0, 30)
573 // may appear (to wxWin apps) to be at (0, 0).
576 wxPoint
pt(GetParent()->GetClientAreaOrigin());
585 void wxRadioBox::SetFocus()
589 if (m_selectedButton
== -1)
590 ::SetFocus((HWND
) m_radioButtons
[0]);
592 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
597 bool wxRadioBox::Show(bool show
)
599 if ( !wxControl::Show(show
) )
602 int nCmdShow
= show
? SW_SHOW
: SW_HIDE
;
603 for ( int i
= 0; i
< m_noItems
; i
++ )
605 ::ShowWindow((HWND
)m_radioButtons
[i
], nCmdShow
);
611 // Enable a specific button
612 void wxRadioBox::Enable(int item
, bool enable
)
614 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
615 wxT("invalid item in wxRadioBox::Enable()") );
617 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
620 // Enable all controls
621 bool wxRadioBox::Enable(bool enable
)
623 if ( !wxControl::Enable(enable
) )
626 for (int i
= 0; i
< m_noItems
; i
++)
627 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
632 // Show a specific button
633 void wxRadioBox::Show(int item
, bool show
)
635 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
636 wxT("invalid item in wxRadioBox::Show()") );
638 ::ShowWindow((HWND
)m_radioButtons
[item
], show
? SW_SHOW
: SW_HIDE
);
641 // For single selection items only
642 wxString
wxRadioBox::GetStringSelection() const
645 int sel
= GetSelection();
647 result
= GetString(sel
);
652 bool wxRadioBox::SetStringSelection(const wxString
& s
)
654 int sel
= FindString (s
);
664 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
667 for (i
= 0; i
< Number(); i
++)
669 if (GetRadioButtons()[i
] == hWnd
)
676 void wxRadioBox::Command(wxCommandEvent
& event
)
678 SetSelection (event
.m_commandInt
);
679 ProcessCommand (event
);
682 // NB: if this code is changed, wxGetWindowForHWND() which relies on having the
683 // radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
684 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
686 // No GWL_USERDATA in Win16, so omit this subclassing.
688 HWND hwndBtn
= (HWND
)hWndBtn
;
690 if ( !s_wndprocRadioBtn
)
691 s_wndprocRadioBtn
= (WNDPROC
)::GetWindowLong(hwndBtn
, GWL_WNDPROC
);
693 ::SetWindowLong(hwndBtn
, GWL_WNDPROC
, (long)wxRadioBtnWndProc
);
694 ::SetWindowLong(hwndBtn
, GWL_USERDATA
, (long)this);
698 void wxRadioBox::SendNotificationEvent()
700 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
701 event
.SetInt( m_selectedButton
);
702 event
.SetString( GetString(m_selectedButton
) );
703 event
.SetEventObject( this );
704 ProcessCommand(event
);
707 bool wxRadioBox::SetFont(const wxFont
& font
)
709 if ( !wxControl::SetFont(font
) )
715 // also set the font of our radio buttons
716 WXHFONT hfont
= wxFont(font
).GetResourceHandle();
717 for ( int n
= 0; n
< m_noItems
; n
++ )
719 ::SendMessage((HWND
)m_radioButtons
[n
], WM_SETFONT
, (WPARAM
)hfont
, 0L);
722 // this is needed because otherwise the buttons are not redrawn correctly
728 // ----------------------------------------------------------------------------
730 // ----------------------------------------------------------------------------
732 long wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
737 case WM_CTLCOLORSTATIC
:
738 // set the colour of the radio buttons to be the same as ours
740 HDC hdc
= (HDC
)wParam
;
742 const wxColour
& colBack
= GetBackgroundColour();
743 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
744 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
746 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
748 return (WXHBRUSH
)brush
->GetResourceHandle();
752 // This is required for the radiobox to be sensitive to mouse input,
753 // e.g. for Dialog Editor.
756 int xPos
= LOWORD(lParam
); // horizontal position of cursor
757 int yPos
= HIWORD(lParam
); // vertical position of cursor
759 ScreenToClient(&xPos
, &yPos
);
761 // Make sure you can drag by the top of the groupbox, but let
762 // other (enclosed) controls get mouse events also
764 return (long)HTCLIENT
;
769 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
772 // ---------------------------------------------------------------------------
773 // window proc for radio buttons
774 // ---------------------------------------------------------------------------
778 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
786 // we must tell IsDialogMessage()/our kbd processing code that we
787 // want to process arrows ourselves because neither of them is
788 // smart enough to handle arrows properly for us
790 long lDlgCode
= ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
,
791 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(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, message
, wParam
, lParam
);