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 #if !USE_SHARED_LIBRARY
47 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
50 // VZ: the new behaviour is to create the radio buttons as children of the
51 // radiobox instead of creating them as children of the radiobox' parent.
53 // This seems more logical, more consistent with what other frameworks do
54 // and allows tooltips to work with radioboxes, so there should be no
55 // reason to revert to the backward compatible behaviour - but I still
56 // leave this possibility just in case.
57 #define RADIOBTN_PARENT_IS_RADIOBOX 1
59 // ---------------------------------------------------------------------------
61 // ---------------------------------------------------------------------------
63 // wnd proc for radio buttons
65 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
70 // ---------------------------------------------------------------------------
72 // ---------------------------------------------------------------------------
74 // the pointer to standard radio button wnd proc
75 static WXFARPROC s_wndprocRadioBtn
= (WXFARPROC
)NULL
;
79 // ===========================================================================
81 // ===========================================================================
83 // ---------------------------------------------------------------------------
85 // ---------------------------------------------------------------------------
87 int wxRadioBox::GetNumVer() const
89 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
95 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
99 int wxRadioBox::GetNumHor() const
101 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
103 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
111 bool wxRadioBox::MSWCommand(WXUINT cmd
, WXWORD id
)
113 if ( cmd
== BN_CLICKED
)
115 int selectedButton
= -1;
117 for ( int i
= 0; i
< m_noItems
; i
++ )
119 if ( id
== wxGetWindowId(m_radioButtons
[i
]) )
127 wxASSERT_MSG( selectedButton
!= -1, wxT("click from alien button?") );
129 if ( selectedButton
!= m_selectedButton
)
131 m_selectedButton
= selectedButton
;
133 SendNotificationEvent();
135 //else: don't generate events when the selection doesn't change
143 #if WXWIN_COMPATIBILITY
144 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
145 int x
, int y
, int width
, int height
,
146 int n
, char **choices
,
147 int majorDim
, long style
, const char *name
)
149 wxString
*choices2
= new wxString
[n
];
150 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
151 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
152 wxDefaultValidator
, name
);
160 wxRadioBox::wxRadioBox()
162 m_selectedButton
= -1;
165 m_radioButtons
= NULL
;
168 m_radioHeight
= NULL
;
171 bool wxRadioBox::Create(wxWindow
*parent
,
173 const wxString
& title
,
177 const wxString choices
[],
180 const wxValidator
& val
,
181 const wxString
& name
)
183 // initialize members
184 m_selectedButton
= -1;
187 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
188 m_noRowsOrCols
= majorDim
;
190 // common initialization
191 if ( !CreateControl(parent
, id
, pos
, size
, style
, val
, name
) )
194 // create the static box
195 if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX
, pos
, size
, title
, 0) )
198 // and now create the buttons
200 #if RADIOBTN_PARENT_IS_RADIOBOX
201 HWND hwndParent
= GetHwnd();
203 HWND hwndParent
= GetHwndOf(parent
);
206 // Some radio boxes test consecutive id.
207 (void)NewControlId();
208 m_radioButtons
= new WXHWND
[n
];
209 m_radioWidth
= new int[n
];
210 m_radioHeight
= new int[n
];
213 wxFont
& font
= GetFont();
216 hfont
= font
.GetResourceHandle();
219 for ( int i
= 0; i
< n
; i
++ )
222 m_radioHeight
[i
] = -1;
223 long styleBtn
= BS_AUTORADIOBUTTON
| WS_CHILD
| WS_VISIBLE
;
224 if ( i
== 0 && style
== 0 )
225 styleBtn
|= WS_GROUP
;
227 long newId
= NewControlId();
229 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
232 0, 0, 0, 0, // will be set in SetSize()
240 wxLogLastError("CreateWindow(radio btn)");
245 m_radioButtons
[i
] = (WXHWND
)hwndBtn
;
247 SubclassRadioButton((WXHWND
)hwndBtn
);
251 ::SendMessage(hwndBtn
, WM_SETFONT
, (WPARAM
)hfont
, 0L);
254 m_subControls
.Add(newId
);
257 // Create a dummy radio control to end the group.
258 (void)::CreateWindow(_T("BUTTON"),
260 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
261 0, 0, 0, 0, hwndParent
,
262 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
266 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
271 wxRadioBox::~wxRadioBox()
273 m_isBeingDeleted
= TRUE
;
278 for (i
= 0; i
< m_noItems
; i
++)
279 ::DestroyWindow((HWND
)m_radioButtons
[i
]);
280 delete[] m_radioButtons
;
284 delete[] m_radioWidth
;
286 delete[] m_radioHeight
;
290 wxString
wxRadioBox::GetLabel(int item
) const
292 wxCHECK_MSG( item
>= 0 && item
< m_noItems
, wxT(""), wxT("invalid radiobox index") );
294 return wxGetWindowText(m_radioButtons
[item
]);
297 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
299 wxCHECK_RET( item
>= 0 && item
< m_noItems
, wxT("invalid radiobox index") );
301 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
302 SetWindowText((HWND
)m_radioButtons
[item
], label
.c_str());
305 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
308 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
309 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
311 wxFAIL_MSG(wxT("not implemented"));
314 int wxRadioBox::FindString(const wxString
& s
) const
316 for (int i
= 0; i
< m_noItems
; i
++)
318 if ( s
== wxGetWindowText(m_radioButtons
[i
]) )
325 void wxRadioBox::SetSelection(int N
)
327 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), wxT("invalid radiobox index") );
329 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
330 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
331 ::SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
333 ::SendMessage((HWND
)m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
334 ::SetFocus((HWND
)m_radioButtons
[N
]);
336 m_selectedButton
= N
;
339 // Get single selection, for single choice list items
340 int wxRadioBox::GetSelection() const
342 return m_selectedButton
;
345 // Find string for position
346 wxString
wxRadioBox::GetString(int N
) const
348 return wxGetWindowText(m_radioButtons
[N
]);
351 // Restored old code.
352 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
354 int currentX
, currentY
;
355 GetPosition(¤tX
, ¤tY
);
356 int widthOld
, heightOld
;
357 GetSize(&widthOld
, &heightOld
);
362 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
364 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
367 #if RADIOBTN_PARENT_IS_RADIOBOX
375 int current_width
, cyf
;
378 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
380 // Attempt to have a look coherent with other platforms: We compute the
381 // biggest toggle dim, then we align all items according this value.
386 for (i
= 0 ; i
< m_noItems
; i
++)
390 if (m_radioWidth
[i
]<0)
392 // It's a labelled toggle
393 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
394 ¤t_width
, &cyf
);
395 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
396 eachHeight
= (int)((3*cyf
)/2);
400 eachWidth
= m_radioWidth
[i
];
401 eachHeight
= m_radioHeight
[i
];
404 if (maxWidth
<eachWidth
)
405 maxWidth
= eachWidth
;
406 if (maxHeight
<eachHeight
)
407 maxHeight
= eachHeight
;
415 int nbHor
= GetNumHor(),
418 // this formula works, but I don't know why.
419 // Please, be sure what you do if you modify it!!
420 if (m_radioWidth
[0]<0)
421 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
423 totHeight
= nbVer
* (maxHeight
+cy1
/2);
424 totWidth
= nbHor
* (maxWidth
+cx1
);
426 int extraHeight
= cy1
;
428 #if defined(CTL3D) && !CTL3D
429 // Requires a bigger group box in plain Windows
434 // only change our width/height if asked for
437 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
438 width
= totWidth
+ cx1
;
445 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
446 height
= totHeight
+ extraHeight
;
451 ::MoveWindow(GetHwnd(), xx
, yy
, width
, height
, TRUE
);
457 #if defined(CTL3D) && (!CTL3D)
458 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
459 // JACS 2/12/93. CTL3D draws group label quite high.
461 int startX
= x_offset
;
462 int startY
= y_offset
;
464 for ( i
= 0 ; i
< m_noItems
; i
++)
466 // Bidimensional radio adjustment
467 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
469 if (m_windowStyle
& wxRA_VERTICAL
)
472 x_offset
+= maxWidth
+ cx1
;
477 y_offset
+= maxHeight
;
478 if (m_radioWidth
[0]>0)
484 if (m_radioWidth
[i
]<0)
486 // It's a labeled item
487 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
488 ¤t_width
, &cyf
);
490 // How do we find out radio button bitmap size!!
491 // By adjusting them carefully, manually :-)
492 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
493 eachHeight
= (int)((3*cyf
)/2);
497 eachWidth
= m_radioWidth
[i
];
498 eachHeight
= m_radioHeight
[i
];
501 // VZ: make all buttons of the same, maximal size - like this they
502 // cover the radiobox entirely and the radiobox tooltips are always
503 // shown (otherwise they are not when the mouse pointer is in the
504 // radiobox part not belonging to any radiobutton)
505 ::MoveWindow((HWND
)m_radioButtons
[i
],
506 x_offset
, y_offset
, maxWidth
, maxHeight
,
509 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
511 y_offset
+= maxHeight
;
512 if (m_radioWidth
[0]>0)
516 x_offset
+= maxWidth
+ cx1
;
520 void wxRadioBox::GetSize(int *width
, int *height
) const
523 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
526 wxFindMaxSize(m_hWnd
, &rect
);
529 for (i
= 0; i
< m_noItems
; i
++)
530 wxFindMaxSize(m_radioButtons
[i
], &rect
);
532 *width
= rect
.right
- rect
.left
;
533 *height
= rect
.bottom
- rect
.top
;
536 void wxRadioBox::GetPosition(int *x
, int *y
) const
538 wxWindow
*parent
= GetParent();
539 RECT rect
= { -1, -1, -1, -1 };
542 for (i
= 0; i
< m_noItems
; i
++)
543 wxFindMaxSize(m_radioButtons
[i
], &rect
);
546 wxFindMaxSize(m_hWnd
, &rect
);
548 // Since we now have the absolute screen coords, if there's a parent we
549 // must subtract its top left corner
555 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
558 // We may be faking the client origin. So a window that's really at (0, 30)
559 // may appear (to wxWin apps) to be at (0, 0).
562 wxPoint
pt(GetParent()->GetClientAreaOrigin());
571 void wxRadioBox::SetFocus()
575 if (m_selectedButton
== -1)
576 ::SetFocus((HWND
) m_radioButtons
[0]);
578 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
583 bool wxRadioBox::Show(bool show
)
585 if ( !wxControl::Show(show
) )
588 int nCmdShow
= show
? SW_SHOW
: SW_HIDE
;
589 for ( int i
= 0; i
< m_noItems
; i
++ )
591 ::ShowWindow((HWND
)m_radioButtons
[i
], nCmdShow
);
597 // Enable a specific button
598 void wxRadioBox::Enable(int item
, bool enable
)
600 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
601 wxT("invalid item in wxRadioBox::Enable()") );
603 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
606 // Enable all controls
607 bool wxRadioBox::Enable(bool enable
)
609 if ( !wxControl::Enable(enable
) )
612 for (int i
= 0; i
< m_noItems
; i
++)
613 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
618 // Show a specific button
619 void wxRadioBox::Show(int item
, bool show
)
621 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
622 wxT("invalid item in wxRadioBox::Show()") );
624 ::ShowWindow((HWND
)m_radioButtons
[item
], show
? SW_SHOW
: SW_HIDE
);
627 // For single selection items only
628 wxString
wxRadioBox::GetStringSelection() const
631 int sel
= GetSelection();
633 result
= GetString(sel
);
638 bool wxRadioBox::SetStringSelection(const wxString
& s
)
640 int sel
= FindString (s
);
650 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
653 for (i
= 0; i
< Number(); i
++)
655 if (GetRadioButtons()[i
] == hWnd
)
662 void wxRadioBox::Command(wxCommandEvent
& event
)
664 SetSelection (event
.m_commandInt
);
665 ProcessCommand (event
);
668 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
671 HWND hwndBtn
= (HWND
)hWndBtn
;
673 if ( !s_wndprocRadioBtn
)
674 s_wndprocRadioBtn
= (WXFARPROC
)::GetWindowLong(hwndBtn
, GWL_WNDPROC
);
676 // No GWL_USERDATA in Win16, so omit this subclassing.
677 ::SetWindowLong(hwndBtn
, GWL_WNDPROC
, (long)wxRadioBtnWndProc
);
678 ::SetWindowLong(hwndBtn
, GWL_USERDATA
, (long)this);
682 void wxRadioBox::SendNotificationEvent()
684 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
685 event
.SetInt( m_selectedButton
);
686 event
.SetString( GetString(m_selectedButton
) );
687 event
.SetEventObject( this );
688 ProcessCommand(event
);
691 bool wxRadioBox::SetFont(const wxFont
& font
)
693 if ( !wxControl::SetFont(font
) )
699 // also set the font of our radio buttons
700 WXHFONT hfont
= wxFont(font
).GetResourceHandle();
701 for ( int n
= 0; n
< m_noItems
; n
++ )
703 ::SendMessage((HWND
)m_radioButtons
[n
], WM_SETFONT
, (WPARAM
)hfont
, 0L);
709 long wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
711 // This is required for the radiobox to be sensitive to mouse input,
712 // e.g. for Dialog Editor.
713 if (nMsg
== WM_NCHITTEST
)
715 int xPos
= LOWORD(lParam
); // horizontal position of cursor
716 int yPos
= HIWORD(lParam
); // vertical position of cursor
718 ScreenToClient(&xPos
, &yPos
);
720 // Make sure you can drag by the top of the groupbox, but let
721 // other (enclosed) controls get mouse events also
723 return (long)HTCLIENT
;
726 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
729 // ---------------------------------------------------------------------------
730 // window proc for radio buttons
731 // ---------------------------------------------------------------------------
735 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
740 bool processed
= FALSE
;
741 if ( msg
== WM_KEYDOWN
744 #endif // wxUSE_TOOLTIPS
747 wxRadioBox
*radiobox
= (wxRadioBox
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
749 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
752 if ( msg
== WM_NOTIFY
)
754 NMHDR
* hdr
= (NMHDR
*)lParam
;
755 if ( (int)hdr
->code
== TTN_NEEDTEXT
)
757 wxToolTip
*tt
= radiobox
->GetToolTip();
760 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
761 ttt
->lpszText
= (wxChar
*)tt
->GetTip().c_str();
767 else // msg == WM_KEYDOWN
768 #endif // wxUSE_TOOLTIPS
772 int sel
= radiobox
->GetSelection();
781 sel
-= radiobox
->GetNumVer();
789 sel
+= radiobox
->GetNumVer();
794 wxNavigationKeyEvent event
;
795 event
.SetDirection(!(::GetKeyState(VK_SHIFT
) & 0x100));
796 event
.SetWindowChange(FALSE
);
797 event
.SetEventObject(radiobox
);
799 if ( radiobox
->GetEventHandler()->ProcessEvent(event
) )
810 if ( sel
>= 0 && sel
< radiobox
->Number() )
812 radiobox
->SetSelection(sel
);
814 // emulate the button click
815 radiobox
->SendNotificationEvent();
824 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, msg
, wParam
, lParam
);