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"
34 #include "wx/bitmap.h"
36 #include "wx/radiobox.h"
39 #include "wx/msw/private.h"
41 #if !USE_SHARED_LIBRARY
42 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
45 // ---------------------------------------------------------------------------
47 // ---------------------------------------------------------------------------
49 // wnd proc for radio buttons
51 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
57 // ---------------------------------------------------------------------------
59 // ---------------------------------------------------------------------------
61 // the pointer to standard radio button wnd proc
62 // static WNDPROC s_wndprocRadioBtn = (WNDPROC)NULL;
63 static WXFARPROC s_wndprocRadioBtn
= (WXFARPROC
)NULL
;
65 // ===========================================================================
67 // ===========================================================================
69 // ---------------------------------------------------------------------------
71 // ---------------------------------------------------------------------------
73 int wxRadioBox::GetNumVer() const
75 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
81 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
85 int wxRadioBox::GetNumHor() const
87 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
89 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
97 bool wxRadioBox::MSWCommand(WXUINT param
, WXWORD id
)
99 if ( param
== BN_CLICKED
)
101 int selectedButton
= -1;
103 for ( int i
= 0; i
< m_noItems
; i
++ )
105 if ( id
== wxGetWindowId(m_radioButtons
[i
]) )
113 wxASSERT_MSG( selectedButton
!= -1, "click from alien button?" );
115 if ( selectedButton
!= m_selectedButton
)
117 m_selectedButton
= selectedButton
;
119 SendNotificationEvent();
121 //else: don't generate events when the selection doesn't change
129 #if WXWIN_COMPATIBILITY
130 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
131 int x
, int y
, int width
, int height
,
132 int n
, char **choices
,
133 int majorDim
, long style
, const char *name
)
135 wxString
*choices2
= new wxString
[n
];
136 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
137 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
138 wxDefaultValidator
, name
);
146 wxRadioBox::wxRadioBox()
148 m_selectedButton
= -1;
151 m_radioButtons
= NULL
;
154 m_radioHeight
= NULL
;
157 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
158 const wxPoint
& pos
, const wxSize
& size
,
159 int n
, const wxString choices
[],
160 int majorDim
, long style
,
161 const wxValidator
& val
, const wxString
& name
)
163 m_selectedButton
= -1;
169 parent
->AddChild(this);
170 m_backgroundColour
= parent
->GetBackgroundColour();
171 m_foregroundColour
= parent
->GetForegroundColour();
173 m_windowStyle
= (long&)style
;
181 m_windowId
= NewControlId();
188 m_majorDim
= majorDim
;
189 m_noRowsOrCols
= majorDim
;
191 long msStyle
= GROUP_FLAGS
;
194 WXDWORD exStyle
= Determine3DEffects(0, &want3D
);
195 // Even with extended styles, need to combine with WS_BORDER
196 // for them to look right.
198 if ( want3D || wxStyleHasBorder(m_windowStyle) )
199 msStyle |= WS_BORDER;
202 HWND hwndParent
= (HWND
)parent
->GetHWND();
204 m_hWnd
= (WXHWND
)::CreateWindowEx
220 Ctl3dSubclassCtl((HWND
)m_hWnd
);
223 #endif // wxUSE_CTL3D
225 SetFont(parent
->GetFont());
229 // Some radio boxes test consecutive id.
230 (void)NewControlId();
231 m_radioButtons
= new WXHWND
[n
];
232 m_radioWidth
= new int[n
];
233 m_radioHeight
= new int[n
];
235 for (i
= 0; i
< n
; i
++)
237 m_radioWidth
[i
] = m_radioHeight
[i
] = -1;
239 if ( i
== 0 && style
== 0 )
240 groupStyle
= WS_GROUP
;
241 long newId
= NewControlId();
242 long msStyle
= groupStyle
| RADIO_FLAGS
;
244 HWND hwndBtn
= CreateWindowEx(exStyle
, RADIO_CLASS
,
248 (HMENU
)newId
, wxGetInstance(),
251 m_radioButtons
[i
] = (WXHWND
)hwndBtn
;
252 SubclassRadioButton((WXHWND
)hwndBtn
);
254 wxFont
& font
= GetFont();
257 SendMessage(hwndBtn
, WM_SETFONT
,
258 (WPARAM
)font
.GetResourceHandle(), 0L);
261 m_subControls
.Append((wxObject
*)newId
);
264 // Create a dummy radio control to end the group.
265 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
| RADIO_FLAGS
,
266 0, 0, 0, 0, hwndParent
,
267 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
271 SetSize(x
, y
, width
, height
);
276 wxRadioBox::~wxRadioBox()
278 m_isBeingDeleted
= TRUE
;
283 for (i
= 0; i
< m_noItems
; i
++)
284 DestroyWindow((HWND
) m_radioButtons
[i
]);
285 delete[] m_radioButtons
;
288 delete[] m_radioWidth
;
290 delete[] m_radioHeight
;
292 ::DestroyWindow((HWND
) m_hWnd
);
297 wxString
wxRadioBox::GetLabel(int item
) const
299 GetWindowText((HWND
)m_radioButtons
[item
], wxBuffer
, 300);
300 return wxString(wxBuffer
);
303 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
305 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
306 SetWindowText((HWND
)m_radioButtons
[item
], (const char *)label
);
309 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
312 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
313 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
317 int wxRadioBox::FindString(const wxString
& s
) const
320 for (i
= 0; i
< m_noItems
; i
++)
322 GetWindowText((HWND
) m_radioButtons
[i
], wxBuffer
, 1000);
329 void wxRadioBox::SetSelection(int N
)
331 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), "invalid radiobox index" );
333 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
334 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
335 ::SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
337 ::SendMessage((HWND
)m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
338 ::SetFocus((HWND
)m_radioButtons
[N
]);
340 m_selectedButton
= N
;
343 // Get single selection, for single choice list items
344 int wxRadioBox::GetSelection() const
346 return m_selectedButton
;
349 // Find string for position
350 wxString
wxRadioBox::GetString(int N
) const
352 return wxGetWindowText(m_radioButtons
[N
]);
355 // Restored old code.
356 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
358 int currentX
, currentY
;
359 GetPosition(¤tX
, ¤tY
);
363 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
365 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
372 int current_width
, cyf
;
375 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
376 // Attempt to have a look coherent with other platforms:
377 // We compute the biggest toggle dim, then we align all
378 // items according this value.
383 for (i
= 0 ; i
< m_noItems
; i
++)
387 if (m_radioWidth
[i
]<0)
389 // It's a labelled toggle
390 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
391 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, & GetFont());
392 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
393 eachHeight
= (int)((3*cyf
)/2);
397 eachWidth
= m_radioWidth
[i
];
398 eachHeight
= m_radioHeight
[i
];
400 if (maxWidth
<eachWidth
) maxWidth
= eachWidth
;
401 if (maxHeight
<eachHeight
) maxHeight
= eachHeight
;
409 int nbHor
= GetNumHor(),
412 // this formula works, but I don't know why.
413 // Please, be sure what you do if you modify it!!
414 if (m_radioWidth
[0]<0)
415 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
417 totHeight
= nbVer
* (maxHeight
+cy1
/2);
418 totWidth
= nbHor
* (maxWidth
+cx1
);
421 // Requires a bigger group box in plain Windows
422 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+(3*cy1
)/2,TRUE
);
424 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+cy1
,TRUE
);
431 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
432 // JACS 2/12/93. CTL3D draws group label quite high.
434 int startX
= x_offset
;
435 int startY
= y_offset
;
437 for ( i
= 0 ; i
< m_noItems
; i
++)
439 // Bidimensional radio adjustment
440 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
442 if (m_windowStyle
& wxRA_VERTICAL
)
445 x_offset
+= maxWidth
+ cx1
;
450 y_offset
+= maxHeight
;
451 if (m_radioWidth
[0]>0)
457 if (m_radioWidth
[i
]<0)
459 // It's a labeled item
460 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
461 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, & GetFont());
463 // How do we find out radio button bitmap size!!
464 // By adjusting them carefully, manually :-)
465 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
466 eachHeight
= (int)((3*cyf
)/2);
470 eachWidth
= m_radioWidth
[i
];
471 eachHeight
= m_radioHeight
[i
];
474 MoveWindow((HWND
) m_radioButtons
[i
],x_offset
,y_offset
,eachWidth
,eachHeight
,TRUE
);
475 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
477 y_offset
+= maxHeight
;
478 if (m_radioWidth
[0]>0)
482 x_offset
+= maxWidth
+ cx1
;
487 void wxRadioBox::GetSize(int *width
, int *height
) const
490 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
493 wxFindMaxSize(m_hWnd
, &rect
);
496 for (i
= 0; i
< m_noItems
; i
++)
497 wxFindMaxSize(m_radioButtons
[i
], &rect
);
499 *width
= rect
.right
- rect
.left
;
500 *height
= rect
.bottom
- rect
.top
;
503 void wxRadioBox::GetPosition(int *x
, int *y
) const
505 wxWindow
*parent
= GetParent();
507 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
510 for (i
= 0; i
< m_noItems
; i
++)
511 wxFindMaxSize(m_radioButtons
[i
], &rect
);
514 wxFindMaxSize(m_hWnd
, &rect
);
516 // Since we now have the absolute screen coords,
517 // if there's a parent we must subtract its top left corner
523 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
525 // We may be faking the client origin.
526 // So a window that's really at (0, 30) may appear
527 // (to wxWin apps) to be at (0, 0).
530 wxPoint
pt(GetParent()->GetClientAreaOrigin());
539 wxString
wxRadioBox::GetLabel() const
543 GetWindowText((HWND
) m_hWnd
, wxBuffer
, 300);
544 return wxString(wxBuffer
);
546 else return wxString("");
549 void wxRadioBox::SetLabel(const wxString
& label
)
552 SetWindowText((HWND
) m_hWnd
, label
);
555 void wxRadioBox::SetFocus()
559 if (m_selectedButton
== -1)
560 ::SetFocus((HWND
) m_radioButtons
[0]);
562 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
567 bool wxRadioBox::Show(bool show
)
576 ShowWindow((HWND
) m_hWnd
, cshow
);
578 for (i
= 0; i
< m_noItems
; i
++)
579 ShowWindow((HWND
) m_radioButtons
[i
], cshow
);
583 // Enable a specific button
584 void wxRadioBox::Enable(int item
, bool enable
)
587 wxWindow::Enable(enable
);
588 else if (item
< m_noItems
)
589 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
592 // Enable all controls
593 bool wxRadioBox::Enable(bool enable
)
595 if ( !wxControl::Enable(enable
) )
599 for (i
= 0; i
< m_noItems
; i
++)
600 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
605 // Show a specific button
606 void wxRadioBox::Show(int item
, bool show
)
609 wxRadioBox::Show(show
);
610 else if (item
< m_noItems
)
617 ShowWindow((HWND
) m_radioButtons
[item
], cshow
);
621 WXHBRUSH
wxRadioBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
622 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
627 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
628 return (WXHBRUSH
) hbrush
;
632 if (GetParent()->GetTransparentBackground())
633 SetBkMode((HDC
) pDC
, TRANSPARENT
);
635 SetBkMode((HDC
) pDC
, OPAQUE
);
637 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
638 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
640 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
642 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
643 // has a zero usage count.
644 // backgroundBrush->RealizeResource();
645 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
648 // For single selection items only
649 wxString
wxRadioBox::GetStringSelection() const
652 int sel
= GetSelection();
654 result
= GetString(sel
);
659 bool wxRadioBox::SetStringSelection(const wxString
& s
)
661 int sel
= FindString (s
);
671 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
674 for (i
= 0; i
< Number(); i
++)
675 if (GetRadioButtons()[i
] == hWnd
)
680 void wxRadioBox::Command (wxCommandEvent
& event
)
682 SetSelection (event
.m_commandInt
);
683 ProcessCommand (event
);
686 long wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
688 if (nMsg
== WM_NCHITTEST
)
690 int xPos
= LOWORD(lParam
); // horizontal position of cursor
691 int yPos
= HIWORD(lParam
); // vertical position of cursor
693 ScreenToClient(&xPos
, &yPos
);
695 // Make sure you can drag by the top of the groupbox, but let
696 // other (enclosed) controls get mouse events also
698 return (long)HTCLIENT
;
701 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
704 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
706 HWND hwndBtn
= (HWND
)hWndBtn
;
708 if ( !s_wndprocRadioBtn
)
709 s_wndprocRadioBtn
= (WXFARPROC
)::GetWindowLong(hwndBtn
, GWL_WNDPROC
);
711 // No GWL_USERDATA in Win16, so omit this subclassing.
713 ::SetWindowLong(hwndBtn
, GWL_WNDPROC
, (long)wxRadioBtnWndProc
);
714 ::SetWindowLong(hwndBtn
, GWL_USERDATA
, (long)this);
718 void wxRadioBox::SendNotificationEvent()
720 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
721 event
.SetInt( m_selectedButton
);
722 event
.SetEventObject( this );
723 ProcessCommand(event
);
726 // ---------------------------------------------------------------------------
727 // window proc for radio buttons
728 // ---------------------------------------------------------------------------
732 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
737 bool processed
= TRUE
;
738 if ( msg
!= WM_KEYDOWN
)
743 wxRadioBox
*radiobox
= (wxRadioBox
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
745 wxCHECK_MSG( radiobox
, 0, "radio button without radio box?" );
747 int sel
= radiobox
->GetSelection();
756 sel
-= radiobox
->GetNumVer();
764 sel
+= radiobox
->GetNumVer();
769 wxNavigationKeyEvent event
;
770 event
.SetDirection(!(::GetKeyState(VK_SHIFT
) & 0x100));
771 event
.SetWindowChange(FALSE
);
772 event
.SetEventObject(radiobox
);
774 if ( radiobox
->GetEventHandler()->ProcessEvent(event
) )
785 if ( sel
>= 0 && sel
< radiobox
->Number() )
787 radiobox
->SetSelection(sel
);
789 // emulate the button click
790 radiobox
->SendNotificationEvent();
796 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, msg
, wParam
, lParam
);