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"
43 #include "wx/tooltip.h"
44 #endif // wxUSE_TOOLTIPS
46 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
48 // VZ: the new behaviour is to create the radio buttons as children of the
49 // radiobox instead of creating them as children of the radiobox' parent.
51 // This seems more logical, more consistent with what other frameworks do
52 // and allows tooltips to work with radioboxes, so there should be no
53 // reason to revert to the backward compatible behaviour - but I still
54 // leave this possibility just in case.
55 #define RADIOBTN_PARENT_IS_RADIOBOX 1
57 // ---------------------------------------------------------------------------
59 // ---------------------------------------------------------------------------
61 // wnd proc for radio buttons
63 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
68 // ---------------------------------------------------------------------------
70 // ---------------------------------------------------------------------------
72 // the pointer to standard radio button wnd proc
73 static WXFARPROC s_wndprocRadioBtn
= (WXFARPROC
)NULL
;
77 // ===========================================================================
79 // ===========================================================================
81 // ---------------------------------------------------------------------------
83 // ---------------------------------------------------------------------------
85 int wxRadioBox::GetNumVer() const
87 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
93 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
97 int wxRadioBox::GetNumHor() const
99 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
101 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
109 bool wxRadioBox::MSWCommand(WXUINT cmd
, WXWORD id
)
111 if ( cmd
== BN_CLICKED
)
113 int selectedButton
= -1;
115 for ( int i
= 0; i
< m_noItems
; i
++ )
117 if ( id
== wxGetWindowId(m_radioButtons
[i
]) )
125 wxASSERT_MSG( selectedButton
!= -1, wxT("click from alien button?") );
127 if ( selectedButton
!= m_selectedButton
)
129 m_selectedButton
= selectedButton
;
131 SendNotificationEvent();
133 //else: don't generate events when the selection doesn't change
141 #if WXWIN_COMPATIBILITY
142 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
143 int x
, int y
, int width
, int height
,
144 int n
, char **choices
,
145 int majorDim
, long style
, const char *name
)
147 wxString
*choices2
= new wxString
[n
];
148 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
149 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
150 wxDefaultValidator
, name
);
158 wxRadioBox::wxRadioBox()
160 m_selectedButton
= -1;
163 m_radioButtons
= NULL
;
166 m_radioHeight
= NULL
;
169 bool wxRadioBox::Create(wxWindow
*parent
,
171 const wxString
& title
,
175 const wxString choices
[],
178 const wxValidator
& val
,
179 const wxString
& name
)
181 // initialize members
182 m_selectedButton
= -1;
185 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
186 m_noRowsOrCols
= majorDim
;
188 // common initialization
189 if ( !CreateControl(parent
, id
, pos
, size
, style
, val
, name
) )
192 // create the static box
193 if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX
, pos
, size
, title
, 0) )
196 // and now create the buttons
198 #if RADIOBTN_PARENT_IS_RADIOBOX
199 HWND hwndParent
= GetHwnd();
201 HWND hwndParent
= GetHwndOf(parent
);
204 // Some radio boxes test consecutive id.
205 (void)NewControlId();
206 m_radioButtons
= new WXHWND
[n
];
207 m_radioWidth
= new int[n
];
208 m_radioHeight
= new int[n
];
211 wxFont
& font
= GetFont();
214 hfont
= font
.GetResourceHandle();
217 for ( int i
= 0; i
< n
; i
++ )
220 m_radioHeight
[i
] = -1;
221 long styleBtn
= BS_AUTORADIOBUTTON
| WS_CHILD
| WS_VISIBLE
;
222 if ( i
== 0 && style
== 0 )
223 styleBtn
|= WS_GROUP
;
225 long newId
= NewControlId();
227 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
230 0, 0, 0, 0, // will be set in SetSize()
238 wxLogLastError("CreateWindow(radio btn)");
243 m_radioButtons
[i
] = (WXHWND
)hwndBtn
;
245 SubclassRadioButton((WXHWND
)hwndBtn
);
249 ::SendMessage(hwndBtn
, WM_SETFONT
, (WPARAM
)hfont
, 0L);
252 m_subControls
.Add(newId
);
255 // Create a dummy radio control to end the group.
256 (void)::CreateWindow(_T("BUTTON"),
258 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
259 0, 0, 0, 0, hwndParent
,
260 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
264 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
269 wxRadioBox::~wxRadioBox()
271 m_isBeingDeleted
= TRUE
;
276 for (i
= 0; i
< m_noItems
; i
++)
277 ::DestroyWindow((HWND
)m_radioButtons
[i
]);
278 delete[] m_radioButtons
;
282 delete[] m_radioWidth
;
284 delete[] m_radioHeight
;
288 wxString
wxRadioBox::GetLabel(int item
) const
290 wxCHECK_MSG( item
>= 0 && item
< m_noItems
, wxT(""), wxT("invalid radiobox index") );
292 return wxGetWindowText(m_radioButtons
[item
]);
295 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
297 wxCHECK_RET( item
>= 0 && item
< m_noItems
, wxT("invalid radiobox index") );
299 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
300 SetWindowText((HWND
)m_radioButtons
[item
], label
.c_str());
303 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
306 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
307 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
309 wxFAIL_MSG(wxT("not implemented"));
312 int wxRadioBox::FindString(const wxString
& s
) const
314 for (int i
= 0; i
< m_noItems
; i
++)
316 if ( s
== wxGetWindowText(m_radioButtons
[i
]) )
323 void wxRadioBox::SetSelection(int N
)
325 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), wxT("invalid radiobox index") );
327 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
328 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
329 ::SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
331 ::SendMessage((HWND
)m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
332 ::SetFocus((HWND
)m_radioButtons
[N
]);
334 m_selectedButton
= N
;
337 // Get single selection, for single choice list items
338 int wxRadioBox::GetSelection() const
340 return m_selectedButton
;
343 // Find string for position
344 wxString
wxRadioBox::GetString(int N
) const
346 return wxGetWindowText(m_radioButtons
[N
]);
349 // Restored old code.
350 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
352 int currentX
, currentY
;
353 GetPosition(¤tX
, ¤tY
);
354 int widthOld
, heightOld
;
355 GetSize(&widthOld
, &heightOld
);
360 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
362 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
365 #if RADIOBTN_PARENT_IS_RADIOBOX
373 int current_width
, cyf
;
376 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
378 // Attempt to have a look coherent with other platforms: We compute the
379 // biggest toggle dim, then we align all items according this value.
384 for (i
= 0 ; i
< m_noItems
; i
++)
388 if (m_radioWidth
[i
]<0)
390 // It's a labelled toggle
391 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
392 ¤t_width
, &cyf
);
393 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
394 eachHeight
= (int)((3*cyf
)/2);
398 eachWidth
= m_radioWidth
[i
];
399 eachHeight
= m_radioHeight
[i
];
402 if (maxWidth
<eachWidth
)
403 maxWidth
= eachWidth
;
404 if (maxHeight
<eachHeight
)
405 maxHeight
= eachHeight
;
413 int nbHor
= GetNumHor(),
416 // this formula works, but I don't know why.
417 // Please, be sure what you do if you modify it!!
418 if (m_radioWidth
[0]<0)
419 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
421 totHeight
= nbVer
* (maxHeight
+cy1
/2);
422 totWidth
= nbHor
* (maxWidth
+cx1
);
424 int extraHeight
= cy1
;
426 #if defined(CTL3D) && !CTL3D
427 // Requires a bigger group box in plain Windows
432 // only change our width/height if asked for
435 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
436 width
= totWidth
+ cx1
;
443 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
444 height
= totHeight
+ extraHeight
;
449 ::MoveWindow(GetHwnd(), xx
, yy
, width
, height
, TRUE
);
455 #if defined(CTL3D) && (!CTL3D)
456 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
457 // JACS 2/12/93. CTL3D draws group label quite high.
459 int startX
= x_offset
;
460 int startY
= y_offset
;
462 for ( i
= 0 ; i
< m_noItems
; i
++)
464 // Bidimensional radio adjustment
465 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
467 if (m_windowStyle
& wxRA_VERTICAL
)
470 x_offset
+= maxWidth
+ cx1
;
475 y_offset
+= maxHeight
;
476 if (m_radioWidth
[0]>0)
482 if (m_radioWidth
[i
]<0)
484 // It's a labeled item
485 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
486 ¤t_width
, &cyf
);
488 // How do we find out radio button bitmap size!!
489 // By adjusting them carefully, manually :-)
490 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
491 eachHeight
= (int)((3*cyf
)/2);
495 eachWidth
= m_radioWidth
[i
];
496 eachHeight
= m_radioHeight
[i
];
499 // VZ: make all buttons of the same, maximal size - like this they
500 // cover the radiobox entirely and the radiobox tooltips are always
501 // shown (otherwise they are not when the mouse pointer is in the
502 // radiobox part not belonging to any radiobutton)
503 ::MoveWindow((HWND
)m_radioButtons
[i
],
504 x_offset
, y_offset
, maxWidth
, maxHeight
,
507 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
509 y_offset
+= maxHeight
;
510 if (m_radioWidth
[0]>0)
514 x_offset
+= maxWidth
+ cx1
;
518 void wxRadioBox::GetSize(int *width
, int *height
) const
521 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
524 wxFindMaxSize(m_hWnd
, &rect
);
527 for (i
= 0; i
< m_noItems
; i
++)
528 wxFindMaxSize(m_radioButtons
[i
], &rect
);
530 *width
= rect
.right
- rect
.left
;
531 *height
= rect
.bottom
- rect
.top
;
534 void wxRadioBox::GetPosition(int *x
, int *y
) const
536 wxWindow
*parent
= GetParent();
537 RECT rect
= { -1, -1, -1, -1 };
540 for (i
= 0; i
< m_noItems
; i
++)
541 wxFindMaxSize(m_radioButtons
[i
], &rect
);
544 wxFindMaxSize(m_hWnd
, &rect
);
546 // Since we now have the absolute screen coords, if there's a parent we
547 // must subtract its top left corner
553 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
556 // We may be faking the client origin. So a window that's really at (0, 30)
557 // may appear (to wxWin apps) to be at (0, 0).
560 wxPoint
pt(GetParent()->GetClientAreaOrigin());
569 void wxRadioBox::SetFocus()
573 if (m_selectedButton
== -1)
574 ::SetFocus((HWND
) m_radioButtons
[0]);
576 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
581 bool wxRadioBox::Show(bool show
)
583 if ( !wxControl::Show(show
) )
586 int nCmdShow
= show
? SW_SHOW
: SW_HIDE
;
587 for ( int i
= 0; i
< m_noItems
; i
++ )
589 ::ShowWindow((HWND
)m_radioButtons
[i
], nCmdShow
);
595 // Enable a specific button
596 void wxRadioBox::Enable(int item
, bool enable
)
598 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
599 wxT("invalid item in wxRadioBox::Enable()") );
601 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
604 // Enable all controls
605 bool wxRadioBox::Enable(bool enable
)
607 if ( !wxControl::Enable(enable
) )
610 for (int i
= 0; i
< m_noItems
; i
++)
611 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
616 // Show a specific button
617 void wxRadioBox::Show(int item
, bool show
)
619 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
620 wxT("invalid item in wxRadioBox::Show()") );
622 ::ShowWindow((HWND
)m_radioButtons
[item
], show
? SW_SHOW
: SW_HIDE
);
625 // For single selection items only
626 wxString
wxRadioBox::GetStringSelection() const
629 int sel
= GetSelection();
631 result
= GetString(sel
);
636 bool wxRadioBox::SetStringSelection(const wxString
& s
)
638 int sel
= FindString (s
);
648 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
651 for (i
= 0; i
< Number(); i
++)
653 if (GetRadioButtons()[i
] == hWnd
)
660 void wxRadioBox::Command(wxCommandEvent
& event
)
662 SetSelection (event
.m_commandInt
);
663 ProcessCommand (event
);
666 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
669 HWND hwndBtn
= (HWND
)hWndBtn
;
671 if ( !s_wndprocRadioBtn
)
672 s_wndprocRadioBtn
= (WXFARPROC
)::GetWindowLong(hwndBtn
, GWL_WNDPROC
);
674 // No GWL_USERDATA in Win16, so omit this subclassing.
675 ::SetWindowLong(hwndBtn
, GWL_WNDPROC
, (long)wxRadioBtnWndProc
);
676 ::SetWindowLong(hwndBtn
, GWL_USERDATA
, (long)this);
680 void wxRadioBox::SendNotificationEvent()
682 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
683 event
.SetInt( m_selectedButton
);
684 event
.SetString( GetString(m_selectedButton
) );
685 event
.SetEventObject( this );
686 ProcessCommand(event
);
689 bool wxRadioBox::SetFont(const wxFont
& font
)
691 if ( !wxControl::SetFont(font
) )
697 // also set the font of our radio buttons
698 WXHFONT hfont
= wxFont(font
).GetResourceHandle();
699 for ( int n
= 0; n
< m_noItems
; n
++ )
701 ::SendMessage((HWND
)m_radioButtons
[n
], WM_SETFONT
, (WPARAM
)hfont
, 0L);
707 long wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
709 // This is required for the radiobox to be sensitive to mouse input,
710 // e.g. for Dialog Editor.
711 if (nMsg
== WM_NCHITTEST
)
713 int xPos
= LOWORD(lParam
); // horizontal position of cursor
714 int yPos
= HIWORD(lParam
); // vertical position of cursor
716 ScreenToClient(&xPos
, &yPos
);
718 // Make sure you can drag by the top of the groupbox, but let
719 // other (enclosed) controls get mouse events also
721 return (long)HTCLIENT
;
724 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
727 // ---------------------------------------------------------------------------
728 // window proc for radio buttons
729 // ---------------------------------------------------------------------------
733 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
738 bool processed
= FALSE
;
739 if ( msg
== WM_KEYDOWN
742 #endif // wxUSE_TOOLTIPS
745 wxRadioBox
*radiobox
= (wxRadioBox
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
747 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
750 if ( msg
== WM_NOTIFY
)
752 NMHDR
* hdr
= (NMHDR
*)lParam
;
753 if ( (int)hdr
->code
== TTN_NEEDTEXT
)
755 wxToolTip
*tt
= radiobox
->GetToolTip();
758 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
759 ttt
->lpszText
= (wxChar
*)tt
->GetTip().c_str();
765 else // msg == WM_KEYDOWN
766 #endif // wxUSE_TOOLTIPS
770 int sel
= radiobox
->GetSelection();
779 sel
-= radiobox
->GetNumVer();
787 sel
+= radiobox
->GetNumVer();
792 wxNavigationKeyEvent event
;
793 event
.SetDirection(!(::GetKeyState(VK_SHIFT
) & 0x100));
794 event
.SetWindowChange(FALSE
);
795 event
.SetEventObject(radiobox
);
797 if ( radiobox
->GetEventHandler()->ProcessEvent(event
) )
808 if ( sel
>= 0 && sel
< radiobox
->Number() )
810 radiobox
->SetSelection(sel
);
812 // emulate the button click
813 radiobox
->SendNotificationEvent();
822 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, msg
, wParam
, lParam
);