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
199 #if RADIOBTN_PARENT_IS_RADIOBOX
200 HWND hwndParent
= GetHwnd();
202 HWND hwndParent
= GetHwndOf(parent
);
205 // Some radio boxes test consecutive id.
206 (void)NewControlId();
207 m_radioButtons
= new WXHWND
[n
];
208 m_radioWidth
= new int[n
];
209 m_radioHeight
= new int[n
];
212 wxFont
& font
= GetFont();
215 hfont
= font
.GetResourceHandle();
218 for ( int i
= 0; i
< n
; i
++ )
221 m_radioHeight
[i
] = -1;
222 long styleBtn
= BS_AUTORADIOBUTTON
| WS_CHILD
| WS_VISIBLE
;
223 if ( i
== 0 && style
== 0 )
224 styleBtn
|= WS_GROUP
;
226 long newId
= NewControlId();
228 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
231 0, 0, 0, 0, // will be set in SetSize()
239 wxLogLastError("CreateWindow(radio btn)");
244 m_radioButtons
[i
] = (WXHWND
)hwndBtn
;
246 SubclassRadioButton((WXHWND
)hwndBtn
);
250 ::SendMessage(hwndBtn
, WM_SETFONT
, (WPARAM
)hfont
, 0L);
253 m_subControls
.Add(newId
);
256 // Create a dummy radio control to end the group.
257 (void)::CreateWindow(_T("BUTTON"),
259 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
260 0, 0, 0, 0, hwndParent
,
261 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
265 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
270 wxRadioBox::~wxRadioBox()
272 m_isBeingDeleted
= TRUE
;
277 for (i
= 0; i
< m_noItems
; i
++)
278 ::DestroyWindow((HWND
)m_radioButtons
[i
]);
279 delete[] m_radioButtons
;
283 delete[] m_radioWidth
;
285 delete[] m_radioHeight
;
289 wxString
wxRadioBox::GetLabel(int item
) const
291 wxCHECK_MSG( item
>= 0 && item
< m_noItems
, wxT(""), wxT("invalid radiobox index") );
293 return wxGetWindowText(m_radioButtons
[item
]);
296 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
298 wxCHECK_RET( item
>= 0 && item
< m_noItems
, wxT("invalid radiobox index") );
300 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
301 SetWindowText((HWND
)m_radioButtons
[item
], label
.c_str());
304 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
307 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
308 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
310 wxFAIL_MSG(wxT("not implemented"));
313 int wxRadioBox::FindString(const wxString
& s
) const
315 for (int i
= 0; i
< m_noItems
; i
++)
317 if ( s
== wxGetWindowText(m_radioButtons
[i
]) )
324 void wxRadioBox::SetSelection(int N
)
326 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), wxT("invalid radiobox index") );
328 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
329 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
330 ::SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
332 ::SendMessage((HWND
)m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
333 ::SetFocus((HWND
)m_radioButtons
[N
]);
335 m_selectedButton
= N
;
338 // Get single selection, for single choice list items
339 int wxRadioBox::GetSelection() const
341 return m_selectedButton
;
344 // Find string for position
345 wxString
wxRadioBox::GetString(int N
) const
347 return wxGetWindowText(m_radioButtons
[N
]);
350 // Restored old code.
351 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
353 int currentX
, currentY
;
354 GetPosition(¤tX
, ¤tY
);
355 int widthOld
, heightOld
;
356 GetSize(&widthOld
, &heightOld
);
361 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
363 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
366 #if RADIOBTN_PARENT_IS_RADIOBOX
374 int current_width
, cyf
;
377 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
379 // Attempt to have a look coherent with other platforms: We compute the
380 // biggest toggle dim, then we align all items according this value.
385 for (i
= 0 ; i
< m_noItems
; i
++)
389 if (m_radioWidth
[i
]<0)
391 // It's a labelled toggle
392 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
393 ¤t_width
, &cyf
);
394 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
395 eachHeight
= (int)((3*cyf
)/2);
399 eachWidth
= m_radioWidth
[i
];
400 eachHeight
= m_radioHeight
[i
];
403 if (maxWidth
<eachWidth
)
404 maxWidth
= eachWidth
;
405 if (maxHeight
<eachHeight
)
406 maxHeight
= eachHeight
;
414 int nbHor
= GetNumHor(),
417 // this formula works, but I don't know why.
418 // Please, be sure what you do if you modify it!!
419 if (m_radioWidth
[0]<0)
420 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
422 totHeight
= nbVer
* (maxHeight
+cy1
/2);
423 totWidth
= nbHor
* (maxWidth
+cx1
);
425 int extraHeight
= cy1
;
427 #if defined(CTL3D) && !CTL3D
428 // Requires a bigger group box in plain Windows
433 // only change our width/height if asked for
436 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
437 width
= totWidth
+ cx1
;
444 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
445 height
= totHeight
+ extraHeight
;
450 ::MoveWindow(GetHwnd(), xx
, yy
, width
, height
, TRUE
);
456 #if defined(CTL3D) && (!CTL3D)
457 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
458 // JACS 2/12/93. CTL3D draws group label quite high.
460 int startX
= x_offset
;
461 int startY
= y_offset
;
463 for ( i
= 0 ; i
< m_noItems
; i
++)
465 // Bidimensional radio adjustment
466 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
468 if (m_windowStyle
& wxRA_VERTICAL
)
471 x_offset
+= maxWidth
+ cx1
;
476 y_offset
+= maxHeight
;
477 if (m_radioWidth
[0]>0)
483 if (m_radioWidth
[i
]<0)
485 // It's a labeled item
486 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
487 ¤t_width
, &cyf
);
489 // How do we find out radio button bitmap size!!
490 // By adjusting them carefully, manually :-)
491 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
492 eachHeight
= (int)((3*cyf
)/2);
496 eachWidth
= m_radioWidth
[i
];
497 eachHeight
= m_radioHeight
[i
];
500 // VZ: make all buttons of the same, maximal size - like this they
501 // cover the radiobox entirely and the radiobox tooltips are always
502 // shown (otherwise they are not when the mouse pointer is in the
503 // radiobox part not belonging to any radiobutton)
504 ::MoveWindow((HWND
)m_radioButtons
[i
],
505 x_offset
, y_offset
, maxWidth
, maxHeight
,
508 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
510 y_offset
+= maxHeight
;
511 if (m_radioWidth
[0]>0)
515 x_offset
+= maxWidth
+ cx1
;
519 void wxRadioBox::GetSize(int *width
, int *height
) const
522 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
525 wxFindMaxSize(m_hWnd
, &rect
);
528 for (i
= 0; i
< m_noItems
; i
++)
529 wxFindMaxSize(m_radioButtons
[i
], &rect
);
531 *width
= rect
.right
- rect
.left
;
532 *height
= rect
.bottom
- rect
.top
;
535 void wxRadioBox::GetPosition(int *x
, int *y
) const
537 wxWindow
*parent
= GetParent();
538 RECT rect
= { -1, -1, -1, -1 };
541 for (i
= 0; i
< m_noItems
; i
++)
542 wxFindMaxSize(m_radioButtons
[i
], &rect
);
545 wxFindMaxSize(m_hWnd
, &rect
);
547 // Since we now have the absolute screen coords, if there's a parent we
548 // must subtract its top left corner
554 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
557 // We may be faking the client origin. So a window that's really at (0, 30)
558 // may appear (to wxWin apps) to be at (0, 0).
561 wxPoint
pt(GetParent()->GetClientAreaOrigin());
570 void wxRadioBox::SetFocus()
574 if (m_selectedButton
== -1)
575 ::SetFocus((HWND
) m_radioButtons
[0]);
577 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
582 bool wxRadioBox::Show(bool show
)
584 if ( !wxControl::Show(show
) )
587 int nCmdShow
= show
? SW_SHOW
: SW_HIDE
;
588 for ( int i
= 0; i
< m_noItems
; i
++ )
590 ::ShowWindow((HWND
)m_radioButtons
[i
], nCmdShow
);
596 // Enable a specific button
597 void wxRadioBox::Enable(int item
, bool enable
)
599 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
600 wxT("invalid item in wxRadioBox::Enable()") );
602 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
605 // Enable all controls
606 bool wxRadioBox::Enable(bool enable
)
608 if ( !wxControl::Enable(enable
) )
611 for (int i
= 0; i
< m_noItems
; i
++)
612 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
617 // Show a specific button
618 void wxRadioBox::Show(int item
, bool show
)
620 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
621 wxT("invalid item in wxRadioBox::Show()") );
623 ::ShowWindow((HWND
)m_radioButtons
[item
], show
? SW_SHOW
: SW_HIDE
);
626 // For single selection items only
627 wxString
wxRadioBox::GetStringSelection() const
630 int sel
= GetSelection();
632 result
= GetString(sel
);
637 bool wxRadioBox::SetStringSelection(const wxString
& s
)
639 int sel
= FindString (s
);
649 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
652 for (i
= 0; i
< Number(); i
++)
654 if (GetRadioButtons()[i
] == hWnd
)
661 void wxRadioBox::Command (wxCommandEvent
& event
)
663 SetSelection (event
.m_commandInt
);
664 ProcessCommand (event
);
667 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
670 HWND hwndBtn
= (HWND
)hWndBtn
;
672 if ( !s_wndprocRadioBtn
)
673 s_wndprocRadioBtn
= (WXFARPROC
)::GetWindowLong(hwndBtn
, GWL_WNDPROC
);
675 // No GWL_USERDATA in Win16, so omit this subclassing.
676 ::SetWindowLong(hwndBtn
, GWL_WNDPROC
, (long)wxRadioBtnWndProc
);
677 ::SetWindowLong(hwndBtn
, GWL_USERDATA
, (long)this);
681 void wxRadioBox::SendNotificationEvent()
683 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
684 event
.SetInt( m_selectedButton
);
685 event
.SetString( GetString(m_selectedButton
) );
686 event
.SetEventObject( this );
687 ProcessCommand(event
);
690 // ---------------------------------------------------------------------------
691 // window proc for radio buttons
692 // ---------------------------------------------------------------------------
696 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
701 bool processed
= FALSE
;
702 if ( msg
== WM_KEYDOWN
705 #endif // wxUSE_TOOLTIPS
708 wxRadioBox
*radiobox
= (wxRadioBox
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
710 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
713 if ( msg
== WM_NOTIFY
)
715 NMHDR
* hdr
= (NMHDR
*)lParam
;
716 if ( (int)hdr
->code
== TTN_NEEDTEXT
)
718 wxToolTip
*tt
= radiobox
->GetToolTip();
721 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
722 ttt
->lpszText
= (wxChar
*)tt
->GetTip().c_str();
728 else // msg == WM_KEYDOWN
729 #endif // wxUSE_TOOLTIPS
733 int sel
= radiobox
->GetSelection();
742 sel
-= radiobox
->GetNumVer();
750 sel
+= radiobox
->GetNumVer();
755 wxNavigationKeyEvent event
;
756 event
.SetDirection(!(::GetKeyState(VK_SHIFT
) & 0x100));
757 event
.SetWindowChange(FALSE
);
758 event
.SetEventObject(radiobox
);
760 if ( radiobox
->GetEventHandler()->ProcessEvent(event
) )
771 if ( sel
>= 0 && sel
< radiobox
->Number() )
773 radiobox
->SetSelection(sel
);
775 // emulate the button click
776 radiobox
->SendNotificationEvent();
785 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, msg
, wParam
, lParam
);