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 #define RADIOBTN_PARENT_IS_RADIOBOX 1
61 // ---------------------------------------------------------------------------
63 // ---------------------------------------------------------------------------
65 // wnd proc for radio buttons
67 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
72 // ---------------------------------------------------------------------------
74 // ---------------------------------------------------------------------------
76 // the pointer to standard radio button wnd proc
77 static WXFARPROC s_wndprocRadioBtn
= (WXFARPROC
)NULL
;
81 // ===========================================================================
83 // ===========================================================================
85 // ---------------------------------------------------------------------------
87 // ---------------------------------------------------------------------------
89 int wxRadioBox::GetNumVer() const
91 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
97 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
101 int wxRadioBox::GetNumHor() const
103 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
105 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
113 bool wxRadioBox::MSWCommand(WXUINT cmd
, WXWORD id
)
115 if ( cmd
== BN_CLICKED
)
117 int selectedButton
= -1;
119 for ( int i
= 0; i
< m_noItems
; i
++ )
121 if ( id
== wxGetWindowId(m_radioButtons
[i
]) )
129 wxASSERT_MSG( selectedButton
!= -1, wxT("click from alien button?") );
131 if ( selectedButton
!= m_selectedButton
)
133 m_selectedButton
= selectedButton
;
135 SendNotificationEvent();
137 //else: don't generate events when the selection doesn't change
145 #if WXWIN_COMPATIBILITY
146 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
147 int x
, int y
, int width
, int height
,
148 int n
, char **choices
,
149 int majorDim
, long style
, const char *name
)
151 wxString
*choices2
= new wxString
[n
];
152 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
153 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
154 wxDefaultValidator
, name
);
162 wxRadioBox::wxRadioBox()
164 m_selectedButton
= -1;
167 m_radioButtons
= NULL
;
170 m_radioHeight
= NULL
;
173 bool wxRadioBox::Create(wxWindow
*parent
,
175 const wxString
& title
,
179 const wxString choices
[],
182 const wxValidator
& val
,
183 const wxString
& name
)
185 // initialize members
186 m_selectedButton
= -1;
189 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
190 m_noRowsOrCols
= majorDim
;
192 // common initialization
193 if ( !CreateControl(parent
, id
, pos
, size
, style
, val
, name
) )
196 // create the static box
197 if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX
, pos
, size
, title
, 0) )
200 // and now create the buttons
202 #if RADIOBTN_PARENT_IS_RADIOBOX
203 HWND hwndParent
= GetHwnd();
205 HWND hwndParent
= GetHwndOf(parent
);
208 // Some radio boxes test consecutive id.
209 (void)NewControlId();
210 m_radioButtons
= new WXHWND
[n
];
211 m_radioWidth
= new int[n
];
212 m_radioHeight
= new int[n
];
215 wxFont
& font
= GetFont();
218 hfont
= font
.GetResourceHandle();
221 for ( int i
= 0; i
< n
; i
++ )
224 m_radioHeight
[i
] = -1;
225 long styleBtn
= BS_AUTORADIOBUTTON
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
226 if ( i
== 0 && style
== 0 )
227 styleBtn
|= WS_GROUP
;
229 long newId
= NewControlId();
231 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
234 0, 0, 0, 0, // will be set in SetSize()
242 wxLogLastError("CreateWindow(radio btn)");
247 m_radioButtons
[i
] = (WXHWND
)hwndBtn
;
249 SubclassRadioButton((WXHWND
)hwndBtn
);
253 ::SendMessage(hwndBtn
, WM_SETFONT
, (WPARAM
)hfont
, 0L);
256 m_subControls
.Add(newId
);
259 // Create a dummy radio control to end the group.
260 (void)::CreateWindow(_T("BUTTON"),
262 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
263 0, 0, 0, 0, hwndParent
,
264 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
268 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
273 wxRadioBox::~wxRadioBox()
275 m_isBeingDeleted
= TRUE
;
280 for (i
= 0; i
< m_noItems
; i
++)
281 ::DestroyWindow((HWND
)m_radioButtons
[i
]);
282 delete[] m_radioButtons
;
286 delete[] m_radioWidth
;
288 delete[] m_radioHeight
;
292 wxString
wxRadioBox::GetLabel(int item
) const
294 wxCHECK_MSG( item
>= 0 && item
< m_noItems
, wxT(""), wxT("invalid radiobox index") );
296 return wxGetWindowText(m_radioButtons
[item
]);
299 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
301 wxCHECK_RET( item
>= 0 && item
< m_noItems
, wxT("invalid radiobox index") );
303 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
304 SetWindowText((HWND
)m_radioButtons
[item
], label
.c_str());
307 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
310 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
311 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
313 wxFAIL_MSG(wxT("not implemented"));
316 int wxRadioBox::FindString(const wxString
& s
) const
318 for (int i
= 0; i
< m_noItems
; i
++)
320 if ( s
== wxGetWindowText(m_radioButtons
[i
]) )
327 void wxRadioBox::SetSelection(int N
)
329 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), wxT("invalid radiobox index") );
331 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
332 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
333 ::SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
335 ::SendMessage((HWND
)m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
336 ::SetFocus((HWND
)m_radioButtons
[N
]);
338 m_selectedButton
= N
;
341 // Get single selection, for single choice list items
342 int wxRadioBox::GetSelection() const
344 return m_selectedButton
;
347 // Find string for position
348 wxString
wxRadioBox::GetString(int N
) const
350 return wxGetWindowText(m_radioButtons
[N
]);
353 // Restored old code.
354 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
356 int currentX
, currentY
;
357 GetPosition(¤tX
, ¤tY
);
358 int widthOld
, heightOld
;
359 GetSize(&widthOld
, &heightOld
);
364 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
366 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
369 #if RADIOBTN_PARENT_IS_RADIOBOX
377 int current_width
, cyf
;
380 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
382 // Attempt to have a look coherent with other platforms: We compute the
383 // biggest toggle dim, then we align all items according this value.
388 for (i
= 0 ; i
< m_noItems
; i
++)
392 if (m_radioWidth
[i
]<0)
394 // It's a labelled toggle
395 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
396 ¤t_width
, &cyf
);
397 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
398 eachHeight
= (int)((3*cyf
)/2);
402 eachWidth
= m_radioWidth
[i
];
403 eachHeight
= m_radioHeight
[i
];
406 if (maxWidth
<eachWidth
)
407 maxWidth
= eachWidth
;
408 if (maxHeight
<eachHeight
)
409 maxHeight
= eachHeight
;
417 int nbHor
= GetNumHor(),
420 // this formula works, but I don't know why.
421 // Please, be sure what you do if you modify it!!
422 if (m_radioWidth
[0]<0)
423 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
425 totHeight
= nbVer
* (maxHeight
+cy1
/2);
426 totWidth
= nbHor
* (maxWidth
+cx1
);
428 int extraHeight
= cy1
;
430 #if defined(CTL3D) && !CTL3D
431 // Requires a bigger group box in plain Windows
436 // only change our width/height if asked for
439 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
440 width
= totWidth
+ cx1
;
447 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
448 height
= totHeight
+ extraHeight
;
453 ::MoveWindow(GetHwnd(), xx
, yy
, width
, height
, TRUE
);
459 #if defined(CTL3D) && (!CTL3D)
460 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
461 // JACS 2/12/93. CTL3D draws group label quite high.
463 int startX
= x_offset
;
464 int startY
= y_offset
;
466 for ( i
= 0 ; i
< m_noItems
; i
++)
468 // Bidimensional radio adjustment
469 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
471 if (m_windowStyle
& wxRA_VERTICAL
)
474 x_offset
+= maxWidth
+ cx1
;
479 y_offset
+= maxHeight
;
480 if (m_radioWidth
[0]>0)
486 if (m_radioWidth
[i
]<0)
488 // It's a labeled item
489 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
490 ¤t_width
, &cyf
);
492 // How do we find out radio button bitmap size!!
493 // By adjusting them carefully, manually :-)
494 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
495 eachHeight
= (int)((3*cyf
)/2);
499 eachWidth
= m_radioWidth
[i
];
500 eachHeight
= m_radioHeight
[i
];
503 // VZ: make all buttons of the same, maximal size - like this they
504 // cover the radiobox entirely and the radiobox tooltips are always
505 // shown (otherwise they are not when the mouse pointer is in the
506 // radiobox part not belonging to any radiobutton)
507 ::MoveWindow((HWND
)m_radioButtons
[i
],
508 x_offset
, y_offset
, maxWidth
, maxHeight
,
511 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
513 y_offset
+= maxHeight
;
514 if (m_radioWidth
[0]>0)
518 x_offset
+= maxWidth
+ cx1
;
522 void wxRadioBox::GetSize(int *width
, int *height
) const
525 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
528 wxFindMaxSize(m_hWnd
, &rect
);
531 for (i
= 0; i
< m_noItems
; i
++)
532 wxFindMaxSize(m_radioButtons
[i
], &rect
);
534 *width
= rect
.right
- rect
.left
;
535 *height
= rect
.bottom
- rect
.top
;
538 void wxRadioBox::GetPosition(int *x
, int *y
) const
540 wxWindow
*parent
= GetParent();
541 RECT rect
= { -1, -1, -1, -1 };
544 for (i
= 0; i
< m_noItems
; i
++)
545 wxFindMaxSize(m_radioButtons
[i
], &rect
);
548 wxFindMaxSize(m_hWnd
, &rect
);
550 // Since we now have the absolute screen coords, if there's a parent we
551 // must subtract its top left corner
557 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
560 // We may be faking the client origin. So a window that's really at (0, 30)
561 // may appear (to wxWin apps) to be at (0, 0).
564 wxPoint
pt(GetParent()->GetClientAreaOrigin());
573 void wxRadioBox::SetFocus()
577 if (m_selectedButton
== -1)
578 ::SetFocus((HWND
) m_radioButtons
[0]);
580 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
585 bool wxRadioBox::Show(bool show
)
587 if ( !wxControl::Show(show
) )
590 int nCmdShow
= show
? SW_SHOW
: SW_HIDE
;
591 for ( int i
= 0; i
< m_noItems
; i
++ )
593 ::ShowWindow((HWND
)m_radioButtons
[i
], nCmdShow
);
599 // Enable a specific button
600 void wxRadioBox::Enable(int item
, bool enable
)
602 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
603 wxT("invalid item in wxRadioBox::Enable()") );
605 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
608 // Enable all controls
609 bool wxRadioBox::Enable(bool enable
)
611 if ( !wxControl::Enable(enable
) )
614 for (int i
= 0; i
< m_noItems
; i
++)
615 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
620 // Show a specific button
621 void wxRadioBox::Show(int item
, bool show
)
623 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
624 wxT("invalid item in wxRadioBox::Show()") );
626 ::ShowWindow((HWND
)m_radioButtons
[item
], show
? SW_SHOW
: SW_HIDE
);
629 // For single selection items only
630 wxString
wxRadioBox::GetStringSelection() const
633 int sel
= GetSelection();
635 result
= GetString(sel
);
640 bool wxRadioBox::SetStringSelection(const wxString
& s
)
642 int sel
= FindString (s
);
652 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
655 for (i
= 0; i
< Number(); i
++)
657 if (GetRadioButtons()[i
] == hWnd
)
664 void wxRadioBox::Command(wxCommandEvent
& event
)
666 SetSelection (event
.m_commandInt
);
667 ProcessCommand (event
);
670 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
673 HWND hwndBtn
= (HWND
)hWndBtn
;
675 if ( !s_wndprocRadioBtn
)
676 s_wndprocRadioBtn
= (WXFARPROC
)::GetWindowLong(hwndBtn
, GWL_WNDPROC
);
678 // No GWL_USERDATA in Win16, so omit this subclassing.
679 ::SetWindowLong(hwndBtn
, GWL_WNDPROC
, (long)wxRadioBtnWndProc
);
680 ::SetWindowLong(hwndBtn
, GWL_USERDATA
, (long)this);
684 void wxRadioBox::SendNotificationEvent()
686 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
687 event
.SetInt( m_selectedButton
);
688 event
.SetString( GetString(m_selectedButton
) );
689 event
.SetEventObject( this );
690 ProcessCommand(event
);
693 bool wxRadioBox::SetFont(const wxFont
& font
)
695 if ( !wxControl::SetFont(font
) )
701 // also set the font of our radio buttons
702 WXHFONT hfont
= wxFont(font
).GetResourceHandle();
703 for ( int n
= 0; n
< m_noItems
; n
++ )
705 ::SendMessage((HWND
)m_radioButtons
[n
], WM_SETFONT
, (WPARAM
)hfont
, 0L);
711 long wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
715 case WM_CTLCOLORSTATIC
:
716 // set the colour of the radio buttons to be the same as ours
718 HDC hdc
= (HDC
)wParam
;
720 const wxColour
& colBack
= GetBackgroundColour();
721 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
722 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
724 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
726 return (WXHBRUSH
)brush
->GetResourceHandle();
729 // This is required for the radiobox to be sensitive to mouse input,
730 // e.g. for Dialog Editor.
733 int xPos
= LOWORD(lParam
); // horizontal position of cursor
734 int yPos
= HIWORD(lParam
); // vertical position of cursor
736 ScreenToClient(&xPos
, &yPos
);
738 // Make sure you can drag by the top of the groupbox, but let
739 // other (enclosed) controls get mouse events also
741 return (long)HTCLIENT
;
746 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
750 // ---------------------------------------------------------------------------
751 // window proc for radio buttons
752 // ---------------------------------------------------------------------------
756 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
761 bool processed
= FALSE
;
762 if ( msg
== WM_KEYDOWN
765 #endif // wxUSE_TOOLTIPS
768 wxRadioBox
*radiobox
= (wxRadioBox
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
770 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
772 #if wxUSE_TOOLTIPS && !defined(__GNUWIN32__)
773 if ( msg
== WM_NOTIFY
)
775 NMHDR
* hdr
= (NMHDR
*)lParam
;
776 if ( (int)hdr
->code
== TTN_NEEDTEXT
)
778 wxToolTip
*tt
= radiobox
->GetToolTip();
781 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
782 ttt
->lpszText
= (wxChar
*)tt
->GetTip().c_str();
788 else // msg == WM_KEYDOWN
789 #endif // wxUSE_TOOLTIPS
793 int sel
= radiobox
->GetSelection();
802 sel
-= radiobox
->GetNumVer();
810 sel
+= radiobox
->GetNumVer();
815 wxNavigationKeyEvent event
;
816 event
.SetDirection(!(::GetKeyState(VK_SHIFT
) & 0x100));
817 event
.SetWindowChange(FALSE
);
818 event
.SetEventObject(radiobox
);
820 if ( radiobox
->GetEventHandler()->ProcessEvent(event
) )
831 if ( sel
>= 0 && sel
< radiobox
->Number() )
833 radiobox
->SetSelection(sel
);
835 // emulate the button click
836 radiobox
->SendNotificationEvent();
845 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, msg
, wParam
, lParam
);