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 // get the id of the window
51 #define GET_WIN_ID(hwnd) ::GetWindowLong((HWND)hwnd, GWL_ID)
53 #define GET_WIN_ID(hwnd) ::GetWindowWord((HWND)hwnd, GWW_ID)
56 // wnd proc for radio buttons
58 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
64 // ---------------------------------------------------------------------------
66 // ---------------------------------------------------------------------------
68 // the pointer to standard radio button wnd proc
69 // static WNDPROC s_wndprocRadioBtn = (WNDPROC)NULL;
70 static WXFARPROC s_wndprocRadioBtn
= (WXFARPROC
)NULL
;
72 // ===========================================================================
74 // ===========================================================================
76 // ---------------------------------------------------------------------------
78 // ---------------------------------------------------------------------------
80 int wxRadioBox::GetNumVer() const
82 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
88 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
92 int wxRadioBox::GetNumHor() const
94 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
96 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
104 bool wxRadioBox::MSWCommand(WXUINT param
, WXWORD id
)
106 if ( param
== BN_CLICKED
)
108 int selectedButton
= -1;
110 for ( int i
= 0; i
< m_noItems
; i
++ )
112 if ( id
== GET_WIN_ID(m_radioButtons
[i
]) )
120 wxASSERT_MSG( selectedButton
!= -1, "click from alien button?" );
122 if ( selectedButton
!= m_selectedButton
)
124 m_selectedButton
= selectedButton
;
126 SendNotificationEvent();
128 //else: don't generate events when the selection doesn't change
136 #if WXWIN_COMPATIBILITY
137 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
138 int x
, int y
, int width
, int height
,
139 int n
, char **choices
,
140 int majorDim
, long style
, const char *name
)
142 wxString
*choices2
= new wxString
[n
];
143 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
144 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
145 wxDefaultValidator
, name
);
153 wxRadioBox::wxRadioBox()
155 m_selectedButton
= -1;
158 m_radioButtons
= NULL
;
161 m_radioHeight
= NULL
;
164 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
165 const wxPoint
& pos
, const wxSize
& size
,
166 int n
, const wxString choices
[],
167 int majorDim
, long style
,
168 const wxValidator
& val
, const wxString
& name
)
170 m_selectedButton
= -1;
176 parent
->AddChild(this);
177 m_backgroundColour
= parent
->GetBackgroundColour();
178 m_foregroundColour
= parent
->GetForegroundColour();
180 m_windowStyle
= (long&)style
;
188 m_windowId
= NewControlId();
195 m_majorDim
= majorDim
;
196 m_noRowsOrCols
= majorDim
;
198 long msStyle
= GROUP_FLAGS
;
201 WXDWORD exStyle
= Determine3DEffects(0, &want3D
);
202 // Even with extended styles, need to combine with WS_BORDER
203 // for them to look right.
205 if ( want3D || wxStyleHasBorder(m_windowStyle) )
206 msStyle |= WS_BORDER;
209 HWND hwndParent
= (HWND
)parent
->GetHWND();
211 m_hWnd
= (WXHWND
)::CreateWindowEx
227 Ctl3dSubclassCtl((HWND
)m_hWnd
);
230 #endif // wxUSE_CTL3D
232 SetFont(parent
->GetFont());
236 // Some radio boxes test consecutive id.
237 (void)NewControlId();
238 m_radioButtons
= new WXHWND
[n
];
239 m_radioWidth
= new int[n
];
240 m_radioHeight
= new int[n
];
242 for (i
= 0; i
< n
; i
++)
244 m_radioWidth
[i
] = m_radioHeight
[i
] = -1;
246 if ( i
== 0 && style
== 0 )
247 groupStyle
= WS_GROUP
;
248 long newId
= NewControlId();
249 long msStyle
= groupStyle
| RADIO_FLAGS
;
251 HWND hwndBtn
= CreateWindowEx(exStyle
, RADIO_CLASS
,
255 (HMENU
)newId
, wxGetInstance(),
258 m_radioButtons
[i
] = (WXHWND
)hwndBtn
;
259 SubclassRadioButton((WXHWND
)hwndBtn
);
261 wxFont
& font
= GetFont();
264 SendMessage(hwndBtn
, WM_SETFONT
,
265 (WPARAM
)font
.GetResourceHandle(), 0L);
268 m_subControls
.Append((wxObject
*)newId
);
271 // Create a dummy radio control to end the group.
272 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
| RADIO_FLAGS
,
273 0, 0, 0, 0, hwndParent
,
274 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
278 SetSize(x
, y
, width
, height
);
283 wxRadioBox::~wxRadioBox()
285 m_isBeingDeleted
= TRUE
;
290 for (i
= 0; i
< m_noItems
; i
++)
291 DestroyWindow((HWND
) m_radioButtons
[i
]);
292 delete[] m_radioButtons
;
295 delete[] m_radioWidth
;
297 delete[] m_radioHeight
;
299 ::DestroyWindow((HWND
) m_hWnd
);
304 wxString
wxRadioBox::GetLabel(int item
) const
306 GetWindowText((HWND
)m_radioButtons
[item
], wxBuffer
, 300);
307 return wxString(wxBuffer
);
310 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
312 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
313 SetWindowText((HWND
)m_radioButtons
[item
], (const char *)label
);
316 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
319 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
320 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
324 int wxRadioBox::FindString(const wxString
& s
) const
327 for (i
= 0; i
< m_noItems
; i
++)
329 GetWindowText((HWND
) m_radioButtons
[i
], wxBuffer
, 1000);
336 void wxRadioBox::SetSelection(int N
)
338 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), "invalid radiobox index" );
340 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
341 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
342 ::SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
344 ::SendMessage((HWND
)m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
345 ::SetFocus((HWND
)m_radioButtons
[N
]);
347 m_selectedButton
= N
;
350 // Get single selection, for single choice list items
351 int wxRadioBox::GetSelection() const
353 return m_selectedButton
;
356 // Find string for position
357 wxString
wxRadioBox::GetString(int N
) const
359 return wxGetWindowText(m_radioButtons
[N
]);
362 // Restored old code.
363 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
365 int currentX
, currentY
;
366 GetPosition(¤tX
, ¤tY
);
370 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
372 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
379 int current_width
, cyf
;
382 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
383 // Attempt to have a look coherent with other platforms:
384 // We compute the biggest toggle dim, then we align all
385 // items according this value.
390 for (i
= 0 ; i
< m_noItems
; i
++)
394 if (m_radioWidth
[i
]<0)
396 // It's a labelled toggle
397 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
398 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, & GetFont());
399 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
400 eachHeight
= (int)((3*cyf
)/2);
404 eachWidth
= m_radioWidth
[i
];
405 eachHeight
= m_radioHeight
[i
];
407 if (maxWidth
<eachWidth
) maxWidth
= eachWidth
;
408 if (maxHeight
<eachHeight
) maxHeight
= eachHeight
;
416 int nbHor
= GetNumHor(),
419 // this formula works, but I don't know why.
420 // Please, be sure what you do if you modify it!!
421 if (m_radioWidth
[0]<0)
422 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
424 totHeight
= nbVer
* (maxHeight
+cy1
/2);
425 totWidth
= nbHor
* (maxWidth
+cx1
);
428 // Requires a bigger group box in plain Windows
429 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+(3*cy1
)/2,TRUE
);
431 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+cy1
,TRUE
);
438 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
439 // JACS 2/12/93. CTL3D draws group label quite high.
441 int startX
= x_offset
;
442 int startY
= y_offset
;
444 for ( i
= 0 ; i
< m_noItems
; i
++)
446 // Bidimensional radio adjustment
447 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
449 if (m_windowStyle
& wxRA_VERTICAL
)
452 x_offset
+= maxWidth
+ cx1
;
457 y_offset
+= maxHeight
;
458 if (m_radioWidth
[0]>0)
464 if (m_radioWidth
[i
]<0)
466 // It's a labeled item
467 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
468 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, & GetFont());
470 // How do we find out radio button bitmap size!!
471 // By adjusting them carefully, manually :-)
472 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
473 eachHeight
= (int)((3*cyf
)/2);
477 eachWidth
= m_radioWidth
[i
];
478 eachHeight
= m_radioHeight
[i
];
481 MoveWindow((HWND
) m_radioButtons
[i
],x_offset
,y_offset
,eachWidth
,eachHeight
,TRUE
);
482 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
484 y_offset
+= maxHeight
;
485 if (m_radioWidth
[0]>0)
489 x_offset
+= maxWidth
+ cx1
;
494 void wxRadioBox::GetSize(int *width
, int *height
) const
497 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
500 wxFindMaxSize(m_hWnd
, &rect
);
503 for (i
= 0; i
< m_noItems
; i
++)
504 wxFindMaxSize(m_radioButtons
[i
], &rect
);
506 *width
= rect
.right
- rect
.left
;
507 *height
= rect
.bottom
- rect
.top
;
510 void wxRadioBox::GetPosition(int *x
, int *y
) const
512 wxWindow
*parent
= GetParent();
514 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
517 for (i
= 0; i
< m_noItems
; i
++)
518 wxFindMaxSize(m_radioButtons
[i
], &rect
);
521 wxFindMaxSize(m_hWnd
, &rect
);
523 // Since we now have the absolute screen coords,
524 // if there's a parent we must subtract its top left corner
530 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
532 // We may be faking the client origin.
533 // So a window that's really at (0, 30) may appear
534 // (to wxWin apps) to be at (0, 0).
537 wxPoint
pt(GetParent()->GetClientAreaOrigin());
546 wxString
wxRadioBox::GetLabel() const
550 GetWindowText((HWND
) m_hWnd
, wxBuffer
, 300);
551 return wxString(wxBuffer
);
553 else return wxString("");
556 void wxRadioBox::SetLabel(const wxString
& label
)
559 SetWindowText((HWND
) m_hWnd
, label
);
562 void wxRadioBox::SetFocus()
566 if (m_selectedButton
== -1)
567 ::SetFocus((HWND
) m_radioButtons
[0]);
569 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
574 bool wxRadioBox::Show(bool show
)
583 ShowWindow((HWND
) m_hWnd
, cshow
);
585 for (i
= 0; i
< m_noItems
; i
++)
586 ShowWindow((HWND
) m_radioButtons
[i
], cshow
);
590 // Enable a specific button
591 void wxRadioBox::Enable(int item
, bool enable
)
594 wxWindow::Enable(enable
);
595 else if (item
< m_noItems
)
596 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
599 // Enable all controls
600 void wxRadioBox::Enable(bool enable
)
602 wxControl::Enable(enable
);
605 for (i
= 0; i
< m_noItems
; i
++)
606 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
609 // Show a specific button
610 void wxRadioBox::Show(int item
, bool show
)
613 wxRadioBox::Show(show
);
614 else if (item
< m_noItems
)
621 ShowWindow((HWND
) m_radioButtons
[item
], cshow
);
625 WXHBRUSH
wxRadioBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
626 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
631 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
632 return (WXHBRUSH
) hbrush
;
636 if (GetParent()->GetTransparentBackground())
637 SetBkMode((HDC
) pDC
, TRANSPARENT
);
639 SetBkMode((HDC
) pDC
, OPAQUE
);
641 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
642 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
644 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
646 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
647 // has a zero usage count.
648 // backgroundBrush->RealizeResource();
649 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
652 // For single selection items only
653 wxString
wxRadioBox::GetStringSelection() const
656 int sel
= GetSelection();
658 result
= GetString(sel
);
663 bool wxRadioBox::SetStringSelection(const wxString
& s
)
665 int sel
= FindString (s
);
675 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
678 for (i
= 0; i
< Number(); i
++)
679 if (GetRadioButtons()[i
] == hWnd
)
684 void wxRadioBox::Command (wxCommandEvent
& event
)
686 SetSelection (event
.m_commandInt
);
687 ProcessCommand (event
);
690 long wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
692 if (nMsg
== WM_NCHITTEST
)
694 int xPos
= LOWORD(lParam
); // horizontal position of cursor
695 int yPos
= HIWORD(lParam
); // vertical position of cursor
697 ScreenToClient(&xPos
, &yPos
);
699 // Make sure you can drag by the top of the groupbox, but let
700 // other (enclosed) controls get mouse events also
702 return (long)HTCLIENT
;
705 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
708 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
710 HWND hwndBtn
= (HWND
)hWndBtn
;
712 if ( !s_wndprocRadioBtn
)
713 s_wndprocRadioBtn
= (WXFARPROC
)::GetWindowLong(hwndBtn
, GWL_WNDPROC
);
715 // No GWL_USERDATA in Win16, so omit this subclassing.
717 ::SetWindowLong(hwndBtn
, GWL_WNDPROC
, (long)wxRadioBtnWndProc
);
718 ::SetWindowLong(hwndBtn
, GWL_USERDATA
, (long)this);
722 void wxRadioBox::SendNotificationEvent()
724 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
725 event
.SetInt( m_selectedButton
);
726 event
.SetEventObject( this );
727 ProcessCommand(event
);
730 // ---------------------------------------------------------------------------
731 // window proc for radio buttons
732 // ---------------------------------------------------------------------------
736 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
741 bool processed
= TRUE
;
742 if ( msg
!= WM_KEYDOWN
)
747 wxRadioBox
*radiobox
= (wxRadioBox
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
749 wxCHECK_MSG( radiobox
, 0, "radio button without radio box?" );
751 int sel
= radiobox
->GetSelection();
760 sel
-= radiobox
->GetNumVer();
768 sel
+= radiobox
->GetNumVer();
773 wxNavigationKeyEvent event
;
774 event
.SetDirection(!(::GetKeyState(VK_SHIFT
) & 0x100));
775 event
.SetWindowChange(FALSE
);
776 event
.SetEventObject(radiobox
);
778 if ( radiobox
->GetEventHandler()->ProcessEvent(event
) )
789 if ( sel
>= 0 && sel
< radiobox
->Number() )
791 radiobox
->SetSelection(sel
);
793 // emulate the button click
794 radiobox
->SendNotificationEvent();
800 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn
, hwnd
, msg
, wParam
, lParam
);