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"
46 #include "wx/tooltip.h"
47 #endif // wxUSE_TOOLTIPS
49 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
51 // VZ: the new behaviour is to create the radio buttons as children of the
52 // radiobox instead of creating them as children of the radiobox' parent.
54 // This seems more logical, more consistent with what other frameworks do
55 // and allows tooltips to work with radioboxes, so there should be no
56 // reason to revert to the backward compatible behaviour - but I still
57 // leave this possibility just in case.
59 // For some reason, the background colour is set wrongly in WIN16 mode
60 // if we use the new method.
63 #define RADIOBTN_PARENT_IS_RADIOBOX 0
65 #define RADIOBTN_PARENT_IS_RADIOBOX 1
68 // ---------------------------------------------------------------------------
70 // ---------------------------------------------------------------------------
72 // wnd proc for radio buttons
74 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
79 // ---------------------------------------------------------------------------
81 // ---------------------------------------------------------------------------
83 // the pointer to standard radio button wnd proc
84 static WXFARPROC s_wndprocRadioBtn
= (WXFARPROC
)NULL
;
88 // ===========================================================================
90 // ===========================================================================
92 // ---------------------------------------------------------------------------
94 // ---------------------------------------------------------------------------
96 int wxRadioBox::GetNumVer() const
98 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
104 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
108 int wxRadioBox::GetNumHor() const
110 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
112 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
120 bool wxRadioBox::MSWCommand(WXUINT cmd
, WXWORD id
)
122 if ( cmd
== BN_CLICKED
)
124 int selectedButton
= -1;
126 for ( int i
= 0; i
< m_noItems
; i
++ )
128 if ( id
== wxGetWindowId(m_radioButtons
[i
]) )
136 wxASSERT_MSG( selectedButton
!= -1, wxT("click from alien button?") );
138 if ( selectedButton
!= m_selectedButton
)
140 m_selectedButton
= selectedButton
;
142 SendNotificationEvent();
144 //else: don't generate events when the selection doesn't change
152 #if WXWIN_COMPATIBILITY
153 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
154 int x
, int y
, int width
, int height
,
155 int n
, char **choices
,
156 int majorDim
, long style
, const char *name
)
158 wxString
*choices2
= new wxString
[n
];
159 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
160 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
161 wxDefaultValidator
, name
);
169 wxRadioBox::wxRadioBox()
171 m_selectedButton
= -1;
174 m_radioButtons
= NULL
;
177 m_radioHeight
= NULL
;
180 bool wxRadioBox::Create(wxWindow
*parent
,
182 const wxString
& title
,
186 const wxString choices
[],
189 const wxValidator
& val
,
190 const wxString
& name
)
192 // initialize members
193 m_selectedButton
= -1;
196 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
197 m_noRowsOrCols
= majorDim
;
199 // common initialization
200 if ( !CreateControl(parent
, id
, pos
, size
, style
, val
, name
) )
203 // create the static box
204 if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX
, pos
, size
, title
, 0) )
207 // and now create the buttons
209 #if RADIOBTN_PARENT_IS_RADIOBOX
210 HWND hwndParent
= GetHwnd();
212 HWND hwndParent
= GetHwndOf(parent
);
215 // Some radio boxes test consecutive id.
216 (void)NewControlId();
217 m_radioButtons
= new WXHWND
[n
];
218 m_radioWidth
= new int[n
];
219 m_radioHeight
= new int[n
];
222 wxFont
& font
= GetFont();
225 hfont
= font
.GetResourceHandle();
228 for ( int i
= 0; i
< n
; i
++ )
231 m_radioHeight
[i
] = -1;
232 long styleBtn
= BS_AUTORADIOBUTTON
| WS_CHILD
| WS_VISIBLE
;
233 if ( i
== 0 && style
== 0 )
234 styleBtn
|= WS_GROUP
;
236 long newId
= NewControlId();
238 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
241 0, 0, 0, 0, // will be set in SetSize()
249 wxLogLastError("CreateWindow(radio btn)");
254 m_radioButtons
[i
] = (WXHWND
)hwndBtn
;
256 SubclassRadioButton((WXHWND
)hwndBtn
);
260 ::SendMessage(hwndBtn
, WM_SETFONT
, (WPARAM
)hfont
, 0L);
263 m_subControls
.Add(newId
);
266 // Create a dummy radio control to end the group.
267 (void)::CreateWindow(_T("BUTTON"),
269 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
270 0, 0, 0, 0, hwndParent
,
271 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
275 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
280 wxRadioBox::~wxRadioBox()
282 m_isBeingDeleted
= TRUE
;
287 for (i
= 0; i
< m_noItems
; i
++)
288 ::DestroyWindow((HWND
)m_radioButtons
[i
]);
289 delete[] m_radioButtons
;
293 delete[] m_radioWidth
;
295 delete[] m_radioHeight
;
299 wxString
wxRadioBox::GetLabel(int item
) const
301 wxCHECK_MSG( item
>= 0 && item
< m_noItems
, wxT(""), wxT("invalid radiobox index") );
303 return wxGetWindowText(m_radioButtons
[item
]);
306 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
308 wxCHECK_RET( item
>= 0 && item
< m_noItems
, wxT("invalid radiobox index") );
310 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
311 SetWindowText((HWND
)m_radioButtons
[item
], label
.c_str());
314 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
317 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
318 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
320 wxFAIL_MSG(wxT("not implemented"));
323 int wxRadioBox::FindString(const wxString
& s
) const
325 for (int i
= 0; i
< m_noItems
; i
++)
327 if ( s
== wxGetWindowText(m_radioButtons
[i
]) )
334 void wxRadioBox::SetSelection(int N
)
336 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), wxT("invalid radiobox index") );
338 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
339 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
340 ::SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
342 ::SendMessage((HWND
)m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
343 ::SetFocus((HWND
)m_radioButtons
[N
]);
345 m_selectedButton
= N
;
348 // Get single selection, for single choice list items
349 int wxRadioBox::GetSelection() const
351 return m_selectedButton
;
354 // Find string for position
355 wxString
wxRadioBox::GetString(int N
) const
357 return wxGetWindowText(m_radioButtons
[N
]);
360 // Restored old code.
361 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
363 int currentX
, currentY
;
364 GetPosition(¤tX
, ¤tY
);
365 int widthOld
, heightOld
;
366 GetSize(&widthOld
, &heightOld
);
371 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
373 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
376 #if RADIOBTN_PARENT_IS_RADIOBOX
384 int current_width
, cyf
;
387 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
389 // Attempt to have a look coherent with other platforms: We compute the
390 // biggest toggle dim, then we align all items according this value.
395 for (i
= 0 ; i
< m_noItems
; i
++)
399 if (m_radioWidth
[i
]<0)
401 // It's a labelled toggle
402 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
403 ¤t_width
, &cyf
);
404 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
405 eachHeight
= (int)((3*cyf
)/2);
409 eachWidth
= m_radioWidth
[i
];
410 eachHeight
= m_radioHeight
[i
];
413 if (maxWidth
<eachWidth
)
414 maxWidth
= eachWidth
;
415 if (maxHeight
<eachHeight
)
416 maxHeight
= eachHeight
;
424 int nbHor
= GetNumHor(),
427 // this formula works, but I don't know why.
428 // Please, be sure what you do if you modify it!!
429 if (m_radioWidth
[0]<0)
430 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
432 totHeight
= nbVer
* (maxHeight
+cy1
/2);
433 totWidth
= nbHor
* (maxWidth
+cx1
);
435 int extraHeight
= cy1
;
437 #if defined(CTL3D) && !CTL3D
438 // Requires a bigger group box in plain Windows
443 // only change our width/height if asked for
446 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
447 width
= totWidth
+ cx1
;
454 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
455 height
= totHeight
+ extraHeight
;
460 ::MoveWindow(GetHwnd(), xx
, yy
, width
, height
, TRUE
);
466 #if defined(CTL3D) && (!CTL3D)
467 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
468 // JACS 2/12/93. CTL3D draws group label quite high.
470 int startX
= x_offset
;
471 int startY
= y_offset
;
473 for ( i
= 0 ; i
< m_noItems
; i
++)
475 // Bidimensional radio adjustment
476 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
478 if (m_windowStyle
& wxRA_VERTICAL
)
481 x_offset
+= maxWidth
+ cx1
;
486 y_offset
+= maxHeight
;
487 if (m_radioWidth
[0]>0)
493 if (m_radioWidth
[i
]<0)
495 // It's a labeled item
496 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
497 ¤t_width
, &cyf
);
499 // How do we find out radio button bitmap size!!
500 // By adjusting them carefully, manually :-)
501 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
502 eachHeight
= (int)((3*cyf
)/2);
506 eachWidth
= m_radioWidth
[i
];
507 eachHeight
= m_radioHeight
[i
];
510 // VZ: make all buttons of the same, maximal size - like this they
511 // cover the radiobox entirely and the radiobox tooltips are always
512 // shown (otherwise they are not when the mouse pointer is in the
513 // radiobox part not belonging to any radiobutton)
514 ::MoveWindow((HWND
)m_radioButtons
[i
],
515 x_offset
, y_offset
, maxWidth
, maxHeight
,
518 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
520 y_offset
+= maxHeight
;
521 if (m_radioWidth
[0]>0)
525 x_offset
+= maxWidth
+ cx1
;
529 void wxRadioBox::GetSize(int *width
, int *height
) const
532 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
535 wxFindMaxSize(m_hWnd
, &rect
);
538 for (i
= 0; i
< m_noItems
; i
++)
539 wxFindMaxSize(m_radioButtons
[i
], &rect
);
541 *width
= rect
.right
- rect
.left
;
542 *height
= rect
.bottom
- rect
.top
;
545 void wxRadioBox::GetPosition(int *x
, int *y
) const
547 wxWindow
*parent
= GetParent();
548 RECT rect
= { -1, -1, -1, -1 };
551 for (i
= 0; i
< m_noItems
; i
++)
552 wxFindMaxSize(m_radioButtons
[i
], &rect
);
555 wxFindMaxSize(m_hWnd
, &rect
);
557 // Since we now have the absolute screen coords, if there's a parent we
558 // must subtract its top left corner
564 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
567 // We may be faking the client origin. So a window that's really at (0, 30)
568 // may appear (to wxWin apps) to be at (0, 0).
571 wxPoint
pt(GetParent()->GetClientAreaOrigin());
580 void wxRadioBox::SetFocus()
584 if (m_selectedButton
== -1)
585 ::SetFocus((HWND
) m_radioButtons
[0]);
587 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
592 bool wxRadioBox::Show(bool show
)
594 if ( !wxControl::Show(show
) )
597 int nCmdShow
= show
? SW_SHOW
: SW_HIDE
;
598 for ( int i
= 0; i
< m_noItems
; i
++ )
600 ::ShowWindow((HWND
)m_radioButtons
[i
], nCmdShow
);
606 // Enable a specific button
607 void wxRadioBox::Enable(int item
, bool enable
)
609 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
610 wxT("invalid item in wxRadioBox::Enable()") );
612 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
615 // Enable all controls
616 bool wxRadioBox::Enable(bool enable
)
618 if ( !wxControl::Enable(enable
) )
621 for (int i
= 0; i
< m_noItems
; i
++)
622 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
627 // Show a specific button
628 void wxRadioBox::Show(int item
, bool show
)
630 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
631 wxT("invalid item in wxRadioBox::Show()") );
633 ::ShowWindow((HWND
)m_radioButtons
[item
], show
? SW_SHOW
: SW_HIDE
);
636 // For single selection items only
637 wxString
wxRadioBox::GetStringSelection() const
640 int sel
= GetSelection();
642 result
= GetString(sel
);
647 bool wxRadioBox::SetStringSelection(const wxString
& s
)
649 int sel
= FindString (s
);
659 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
662 for (i
= 0; i
< Number(); i
++)
664 if (GetRadioButtons()[i
] == hWnd
)
671 void wxRadioBox::Command(wxCommandEvent
& event
)
673 SetSelection (event
.m_commandInt
);
674 ProcessCommand (event
);
677 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
680 HWND hwndBtn
= (HWND
)hWndBtn
;
682 if ( !s_wndprocRadioBtn
)
683 s_wndprocRadioBtn
= (WXFARPROC
)::GetWindowLong(hwndBtn
, GWL_WNDPROC
);
685 // No GWL_USERDATA in Win16, so omit this subclassing.
686 ::SetWindowLong(hwndBtn
, GWL_WNDPROC
, (long)wxRadioBtnWndProc
);
687 ::SetWindowLong(hwndBtn
, GWL_USERDATA
, (long)this);
691 void wxRadioBox::SendNotificationEvent()
693 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
694 event
.SetInt( m_selectedButton
);
695 event
.SetString( GetString(m_selectedButton
) );
696 event
.SetEventObject( this );
697 ProcessCommand(event
);
700 bool wxRadioBox::SetFont(const wxFont
& font
)
702 if ( !wxControl::SetFont(font
) )
708 // also set the font of our radio buttons
709 WXHFONT hfont
= wxFont(font
).GetResourceHandle();
710 for ( int n
= 0; n
< m_noItems
; n
++ )
712 ::SendMessage((HWND
)m_radioButtons
[n
], WM_SETFONT
, (WPARAM
)hfont
, 0L);
718 long wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
720 // This is required for the radiobox to be sensitive to mouse input,
721 // e.g. for Dialog Editor.
722 if (nMsg
== WM_NCHITTEST
)
724 int xPos
= LOWORD(lParam
); // horizontal position of cursor
725 int yPos
= HIWORD(lParam
); // vertical position of cursor
727 ScreenToClient(&xPos
, &yPos
);
729 // Make sure you can drag by the top of the groupbox, but let
730 // other (enclosed) controls get mouse events also
732 return (long)HTCLIENT
;
735 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
738 // ---------------------------------------------------------------------------
739 // window proc for radio buttons
740 // ---------------------------------------------------------------------------
744 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
749 bool processed
= FALSE
;
750 if ( msg
== WM_KEYDOWN
753 #endif // wxUSE_TOOLTIPS
756 wxRadioBox
*radiobox
= (wxRadioBox
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
758 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
760 #if wxUSE_TOOLTIPS && !defined(__GNUWIN32__)
761 if ( msg
== WM_NOTIFY
)
763 NMHDR
* hdr
= (NMHDR
*)lParam
;
764 if ( (int)hdr
->code
== TTN_NEEDTEXT
)
766 wxToolTip
*tt
= radiobox
->GetToolTip();
769 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
770 ttt
->lpszText
= (wxChar
*)tt
->GetTip().c_str();
776 else // msg == WM_KEYDOWN
777 #endif // wxUSE_TOOLTIPS
781 int sel
= radiobox
->GetSelection();
790 sel
-= radiobox
->GetNumVer();
798 sel
+= radiobox
->GetNumVer();
803 wxNavigationKeyEvent event
;
804 event
.SetDirection(!(::GetKeyState(VK_SHIFT
) & 0x100));
805 event
.SetWindowChange(FALSE
);
806 event
.SetEventObject(radiobox
);
808 if ( radiobox
->GetEventHandler()->ProcessEvent(event
) )
819 if ( sel
>= 0 && sel
< radiobox
->Number() )
821 radiobox
->SetSelection(sel
);
823 // emulate the button click
824 radiobox
->SendNotificationEvent();
833 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, msg
, wParam
, lParam
);