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 0
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
);
159 #endif // WXWIN_COMPATIBILITY
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
| WS_GROUP
,
198 pos
, size
, title
, 0) )
201 // and now create the buttons
203 #if RADIOBTN_PARENT_IS_RADIOBOX
204 HWND hwndParent
= GetHwnd();
206 HWND hwndParent
= GetHwndOf(parent
);
209 // Some radio boxes test consecutive id.
210 (void)NewControlId();
211 m_radioButtons
= new WXHWND
[n
];
212 m_radioWidth
= new int[n
];
213 m_radioHeight
= new int[n
];
216 wxFont
& font
= GetFont();
219 hfont
= font
.GetResourceHandle();
222 for ( int i
= 0; i
< n
; i
++ )
225 m_radioHeight
[i
] = -1;
226 long styleBtn
= BS_AUTORADIOBUTTON
| WS_TABSTOP
| WS_CHILD
| WS_VISIBLE
;
227 if ( i
== 0 && style
== 0 )
228 styleBtn
|= WS_GROUP
;
230 long newId
= NewControlId();
232 HWND hwndBtn
= ::CreateWindow(_T("BUTTON"),
235 0, 0, 0, 0, // will be set in SetSize()
243 wxLogLastError("CreateWindow(radio btn)");
248 m_radioButtons
[i
] = (WXHWND
)hwndBtn
;
250 SubclassRadioButton((WXHWND
)hwndBtn
);
254 ::SendMessage(hwndBtn
, WM_SETFONT
, (WPARAM
)hfont
, 0L);
257 m_subControls
.Add(newId
);
260 // Create a dummy radio control to end the group.
261 (void)::CreateWindow(_T("BUTTON"),
263 WS_GROUP
| BS_AUTORADIOBUTTON
| WS_CHILD
,
264 0, 0, 0, 0, hwndParent
,
265 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
269 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
274 wxRadioBox::~wxRadioBox()
276 m_isBeingDeleted
= TRUE
;
281 for (i
= 0; i
< m_noItems
; i
++)
282 ::DestroyWindow((HWND
)m_radioButtons
[i
]);
283 delete[] m_radioButtons
;
287 delete[] m_radioWidth
;
289 delete[] m_radioHeight
;
293 wxString
wxRadioBox::GetLabel(int item
) const
295 wxCHECK_MSG( item
>= 0 && item
< m_noItems
, wxT(""), wxT("invalid radiobox index") );
297 return wxGetWindowText(m_radioButtons
[item
]);
300 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
302 wxCHECK_RET( item
>= 0 && item
< m_noItems
, wxT("invalid radiobox index") );
304 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
305 SetWindowText((HWND
)m_radioButtons
[item
], label
.c_str());
308 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
311 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
312 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
314 wxFAIL_MSG(wxT("not implemented"));
317 int wxRadioBox::FindString(const wxString
& s
) const
319 for (int i
= 0; i
< m_noItems
; i
++)
321 if ( s
== wxGetWindowText(m_radioButtons
[i
]) )
328 void wxRadioBox::SetSelection(int N
)
330 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), wxT("invalid radiobox index") );
332 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
333 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
334 ::SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
336 ::SendMessage((HWND
)m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
337 ::SetFocus((HWND
)m_radioButtons
[N
]);
339 m_selectedButton
= N
;
342 // Get single selection, for single choice list items
343 int wxRadioBox::GetSelection() const
345 return m_selectedButton
;
348 // Find string for position
349 wxString
wxRadioBox::GetString(int N
) const
351 return wxGetWindowText(m_radioButtons
[N
]);
354 // Restored old code.
355 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
357 int currentX
, currentY
;
358 GetPosition(¤tX
, ¤tY
);
359 int widthOld
, heightOld
;
360 GetSize(&widthOld
, &heightOld
);
365 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
367 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
370 #if RADIOBTN_PARENT_IS_RADIOBOX
378 int current_width
, cyf
;
381 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
383 // Attempt to have a look coherent with other platforms: We compute the
384 // biggest toggle dim, then we align all items according this value.
389 for (i
= 0 ; i
< m_noItems
; i
++)
393 if (m_radioWidth
[i
]<0)
395 // It's a labelled toggle
396 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
397 ¤t_width
, &cyf
);
398 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
399 eachHeight
= (int)((3*cyf
)/2);
403 eachWidth
= m_radioWidth
[i
];
404 eachHeight
= m_radioHeight
[i
];
407 if (maxWidth
<eachWidth
)
408 maxWidth
= eachWidth
;
409 if (maxHeight
<eachHeight
)
410 maxHeight
= eachHeight
;
418 int nbHor
= GetNumHor(),
421 // this formula works, but I don't know why.
422 // Please, be sure what you do if you modify it!!
423 if (m_radioWidth
[0]<0)
424 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
426 totHeight
= nbVer
* (maxHeight
+cy1
/2);
427 totWidth
= nbHor
* (maxWidth
+cx1
);
429 int extraHeight
= cy1
;
431 #if defined(CTL3D) && !CTL3D
432 // Requires a bigger group box in plain Windows
437 // only change our width/height if asked for
440 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
441 width
= totWidth
+ cx1
;
448 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
449 height
= totHeight
+ extraHeight
;
454 ::MoveWindow(GetHwnd(), xx
, yy
, width
, height
, TRUE
);
460 #if defined(CTL3D) && (!CTL3D)
461 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
462 // JACS 2/12/93. CTL3D draws group label quite high.
464 int startX
= x_offset
;
465 int startY
= y_offset
;
467 for ( i
= 0 ; i
< m_noItems
; i
++)
469 // Bidimensional radio adjustment
470 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
472 if (m_windowStyle
& wxRA_VERTICAL
)
475 x_offset
+= maxWidth
+ cx1
;
480 y_offset
+= maxHeight
;
481 if (m_radioWidth
[0]>0)
487 if (m_radioWidth
[i
]<0)
489 // It's a labeled item
490 GetTextExtent(wxGetWindowText(m_radioButtons
[i
]),
491 ¤t_width
, &cyf
);
493 // How do we find out radio button bitmap size!!
494 // By adjusting them carefully, manually :-)
495 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
496 eachHeight
= (int)((3*cyf
)/2);
500 eachWidth
= m_radioWidth
[i
];
501 eachHeight
= m_radioHeight
[i
];
504 // VZ: make all buttons of the same, maximal size - like this they
505 // cover the radiobox entirely and the radiobox tooltips are always
506 // shown (otherwise they are not when the mouse pointer is in the
507 // radiobox part not belonging to any radiobutton)
508 ::MoveWindow((HWND
)m_radioButtons
[i
],
509 x_offset
, y_offset
, maxWidth
, maxHeight
,
512 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
514 y_offset
+= maxHeight
;
515 if (m_radioWidth
[0]>0)
519 x_offset
+= maxWidth
+ cx1
;
523 void wxRadioBox::GetSize(int *width
, int *height
) const
526 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
529 wxFindMaxSize(m_hWnd
, &rect
);
532 for (i
= 0; i
< m_noItems
; i
++)
533 wxFindMaxSize(m_radioButtons
[i
], &rect
);
535 *width
= rect
.right
- rect
.left
;
536 *height
= rect
.bottom
- rect
.top
;
539 void wxRadioBox::GetPosition(int *x
, int *y
) const
541 wxWindow
*parent
= GetParent();
542 RECT rect
= { -1, -1, -1, -1 };
545 for (i
= 0; i
< m_noItems
; i
++)
546 wxFindMaxSize(m_radioButtons
[i
], &rect
);
549 wxFindMaxSize(m_hWnd
, &rect
);
551 // Since we now have the absolute screen coords, if there's a parent we
552 // must subtract its top left corner
558 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
561 // We may be faking the client origin. So a window that's really at (0, 30)
562 // may appear (to wxWin apps) to be at (0, 0).
565 wxPoint
pt(GetParent()->GetClientAreaOrigin());
574 void wxRadioBox::SetFocus()
578 if (m_selectedButton
== -1)
579 ::SetFocus((HWND
) m_radioButtons
[0]);
581 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
586 bool wxRadioBox::Show(bool show
)
588 if ( !wxControl::Show(show
) )
591 int nCmdShow
= show
? SW_SHOW
: SW_HIDE
;
592 for ( int i
= 0; i
< m_noItems
; i
++ )
594 ::ShowWindow((HWND
)m_radioButtons
[i
], nCmdShow
);
600 // Enable a specific button
601 void wxRadioBox::Enable(int item
, bool enable
)
603 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
604 wxT("invalid item in wxRadioBox::Enable()") );
606 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
609 // Enable all controls
610 bool wxRadioBox::Enable(bool enable
)
612 if ( !wxControl::Enable(enable
) )
615 for (int i
= 0; i
< m_noItems
; i
++)
616 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
621 // Show a specific button
622 void wxRadioBox::Show(int item
, bool show
)
624 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
625 wxT("invalid item in wxRadioBox::Show()") );
627 ::ShowWindow((HWND
)m_radioButtons
[item
], show
? SW_SHOW
: SW_HIDE
);
630 // For single selection items only
631 wxString
wxRadioBox::GetStringSelection() const
634 int sel
= GetSelection();
636 result
= GetString(sel
);
641 bool wxRadioBox::SetStringSelection(const wxString
& s
)
643 int sel
= FindString (s
);
653 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
656 for (i
= 0; i
< Number(); i
++)
658 if (GetRadioButtons()[i
] == hWnd
)
665 void wxRadioBox::Command(wxCommandEvent
& event
)
667 SetSelection (event
.m_commandInt
);
668 ProcessCommand (event
);
671 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
674 HWND hwndBtn
= (HWND
)hWndBtn
;
676 if ( !s_wndprocRadioBtn
)
677 s_wndprocRadioBtn
= (WXFARPROC
)::GetWindowLong(hwndBtn
, GWL_WNDPROC
);
679 // No GWL_USERDATA in Win16, so omit this subclassing.
680 ::SetWindowLong(hwndBtn
, GWL_WNDPROC
, (long)wxRadioBtnWndProc
);
681 ::SetWindowLong(hwndBtn
, GWL_USERDATA
, (long)this);
685 void wxRadioBox::SendNotificationEvent()
687 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
688 event
.SetInt( m_selectedButton
);
689 event
.SetString( GetString(m_selectedButton
) );
690 event
.SetEventObject( this );
691 ProcessCommand(event
);
694 bool wxRadioBox::SetFont(const wxFont
& font
)
696 if ( !wxControl::SetFont(font
) )
702 // also set the font of our radio buttons
703 WXHFONT hfont
= wxFont(font
).GetResourceHandle();
704 for ( int n
= 0; n
< m_noItems
; n
++ )
706 ::SendMessage((HWND
)m_radioButtons
[n
], WM_SETFONT
, (WPARAM
)hfont
, 0L);
712 long wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
717 case WM_CTLCOLORSTATIC
:
718 // set the colour of the radio buttons to be the same as ours
720 HDC hdc
= (HDC
)wParam
;
722 const wxColour
& colBack
= GetBackgroundColour();
723 ::SetBkColor(hdc
, wxColourToRGB(colBack
));
724 ::SetTextColor(hdc
, wxColourToRGB(GetForegroundColour()));
726 wxBrush
*brush
= wxTheBrushList
->FindOrCreateBrush(colBack
, wxSOLID
);
728 return (WXHBRUSH
)brush
->GetResourceHandle();
732 // This is required for the radiobox to be sensitive to mouse input,
733 // e.g. for Dialog Editor.
736 int xPos
= LOWORD(lParam
); // horizontal position of cursor
737 int yPos
= HIWORD(lParam
); // vertical position of cursor
739 ScreenToClient(&xPos
, &yPos
);
741 // Make sure you can drag by the top of the groupbox, but let
742 // other (enclosed) controls get mouse events also
744 return (long)HTCLIENT
;
749 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
753 // ---------------------------------------------------------------------------
754 // window proc for radio buttons
755 // ---------------------------------------------------------------------------
759 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
764 bool processed
= FALSE
;
765 if ( msg
== WM_KEYDOWN
768 #endif // wxUSE_TOOLTIPS
771 wxRadioBox
*radiobox
= (wxRadioBox
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
773 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
775 #if wxUSE_TOOLTIPS && !defined(__GNUWIN32__)
776 if ( msg
== WM_NOTIFY
)
778 NMHDR
* hdr
= (NMHDR
*)lParam
;
779 if ( (int)hdr
->code
== TTN_NEEDTEXT
)
781 wxToolTip
*tt
= radiobox
->GetToolTip();
784 TOOLTIPTEXT
*ttt
= (TOOLTIPTEXT
*)lParam
;
785 ttt
->lpszText
= (wxChar
*)tt
->GetTip().c_str();
791 else // msg == WM_KEYDOWN
792 #endif // wxUSE_TOOLTIPS
796 int sel
= radiobox
->GetSelection();
805 sel
-= radiobox
->GetNumVer();
813 sel
+= radiobox
->GetNumVer();
818 wxNavigationKeyEvent event
;
819 event
.SetDirection(!(::GetKeyState(VK_SHIFT
) & 0x100));
820 event
.SetWindowChange(FALSE
);
821 event
.SetEventObject(radiobox
);
823 if ( radiobox
->GetEventHandler()->ProcessEvent(event
) )
834 if ( sel
>= 0 && sel
< radiobox
->Number() )
836 radiobox
->SetSelection(sel
);
838 // emulate the button click
839 radiobox
->SendNotificationEvent();
848 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, msg
, wParam
, lParam
);