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
57 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hWnd
,
62 // ---------------------------------------------------------------------------
64 // ---------------------------------------------------------------------------
66 // the pointer to standard radio button wnd proc
67 static WNDPROC s_wndprocRadioBtn
= (WNDPROC
)NULL
;
69 // ===========================================================================
71 // ===========================================================================
73 // ---------------------------------------------------------------------------
75 // ---------------------------------------------------------------------------
77 int wxRadioBox::GetNumVer() const
79 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
85 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
89 int wxRadioBox::GetNumHor() const
91 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
93 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
101 bool wxRadioBox::MSWCommand(WXUINT param
, WXWORD id
)
103 if ( param
== BN_CLICKED
)
105 m_selectedButton
= -1;
107 for ( int i
= 0; i
< m_noItems
; i
++ )
109 if ( id
== GET_WIN_ID(m_radioButtons
[i
]) )
111 m_selectedButton
= i
;
117 wxASSERT_MSG( m_selectedButton
!= -1, "click from alien button?" );
119 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
120 event
.SetInt( m_selectedButton
);
121 event
.SetEventObject( this );
122 ProcessCommand(event
);
130 #if WXWIN_COMPATIBILITY
131 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
132 int x
, int y
, int width
, int height
,
133 int n
, char **choices
,
134 int majorDim
, long style
, const char *name
)
136 wxString
*choices2
= new wxString
[n
];
137 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
138 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
139 wxDefaultValidator
, name
);
147 wxRadioBox::wxRadioBox()
149 m_selectedButton
= -1;
152 m_radioButtons
= NULL
;
155 m_radioHeight
= NULL
;
158 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
159 const wxPoint
& pos
, const wxSize
& size
,
160 int n
, const wxString choices
[],
161 int majorDim
, long style
,
162 const wxValidator
& val
, const wxString
& name
)
164 m_selectedButton
= -1;
170 parent
->AddChild(this);
171 m_backgroundColour
= parent
->GetBackgroundColour();
172 m_foregroundColour
= parent
->GetForegroundColour();
174 m_windowStyle
= (long&)style
;
182 m_windowId
= NewControlId();
189 m_majorDim
= majorDim
;
190 m_noRowsOrCols
= majorDim
;
192 long msStyle
= GROUP_FLAGS
;
195 WXDWORD exStyle
= Determine3DEffects(0, &want3D
);
196 // Even with extended styles, need to combine with WS_BORDER
197 // for them to look right.
199 if ( want3D || wxStyleHasBorder(m_windowStyle) )
200 msStyle |= WS_BORDER;
203 HWND hwndParent
= (HWND
)parent
->GetHWND();
205 m_hWnd
= (WXHWND
)::CreateWindowEx
221 Ctl3dSubclassCtl((HWND
)m_hWnd
);
224 #endif // wxUSE_CTL3D
226 SetFont(parent
->GetFont());
230 // Some radio boxes test consecutive id.
231 (void)NewControlId();
232 m_radioButtons
= new WXHWND
[n
];
233 m_radioWidth
= new int[n
];
234 m_radioHeight
= new int[n
];
236 for (i
= 0; i
< n
; i
++)
238 m_radioWidth
[i
] = m_radioHeight
[i
] = -1;
240 if ( i
== 0 && style
== 0 )
241 groupStyle
= WS_GROUP
;
242 long newId
= NewControlId();
243 long msStyle
= groupStyle
| RADIO_FLAGS
;
245 HWND hwndBtn
= CreateWindowEx(exStyle
, RADIO_CLASS
,
249 (HMENU
)newId
, wxGetInstance(),
252 m_radioButtons
[i
] = (WXHWND
)hwndBtn
;
253 SubclassRadioButton((WXHWND
)hwndBtn
);
255 wxFont
& font
= GetFont();
258 SendMessage(hwndBtn
, WM_SETFONT
,
259 (WPARAM
)font
.GetResourceHandle(), 0L);
262 m_subControls
.Append((wxObject
*)newId
);
265 // Create a dummy radio control to end the group.
266 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
| RADIO_FLAGS
,
267 0, 0, 0, 0, hwndParent
,
268 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
272 SetSize(x
, y
, width
, height
);
277 wxRadioBox::~wxRadioBox()
279 m_isBeingDeleted
= TRUE
;
284 for (i
= 0; i
< m_noItems
; i
++)
285 DestroyWindow((HWND
) m_radioButtons
[i
]);
286 delete[] m_radioButtons
;
289 delete[] m_radioWidth
;
291 delete[] m_radioHeight
;
293 ::DestroyWindow((HWND
) m_hWnd
);
298 wxString
wxRadioBox::GetLabel(int item
) const
300 GetWindowText((HWND
)m_radioButtons
[item
], wxBuffer
, 300);
301 return wxString(wxBuffer
);
304 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
306 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
307 SetWindowText((HWND
)m_radioButtons
[item
], (const char *)label
);
310 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
313 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
314 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
318 int wxRadioBox::FindString(const wxString
& s
) const
321 for (i
= 0; i
< m_noItems
; i
++)
323 GetWindowText((HWND
) m_radioButtons
[i
], wxBuffer
, 1000);
330 void wxRadioBox::SetSelection(int N
)
332 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), "invalid radiobox index" );
334 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
335 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
336 ::SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
338 ::SendMessage((HWND
)m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
339 ::SetFocus((HWND
)m_radioButtons
[N
]);
341 m_selectedButton
= N
;
344 // Get single selection, for single choice list items
345 int wxRadioBox::GetSelection() const
347 return m_selectedButton
;
350 // Find string for position
351 wxString
wxRadioBox::GetString(int N
) const
353 return wxGetWindowText(m_radioButtons
[N
]);
356 // Restored old code.
357 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
359 int currentX
, currentY
;
360 GetPosition(¤tX
, ¤tY
);
364 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
366 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
373 int current_width
, cyf
;
376 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
377 // Attempt to have a look coherent with other platforms:
378 // We compute the biggest toggle dim, then we align all
379 // items according this value.
384 for (i
= 0 ; i
< m_noItems
; i
++)
388 if (m_radioWidth
[i
]<0)
390 // It's a labelled toggle
391 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
392 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, & GetFont());
393 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
394 eachHeight
= (int)((3*cyf
)/2);
398 eachWidth
= m_radioWidth
[i
];
399 eachHeight
= m_radioHeight
[i
];
401 if (maxWidth
<eachWidth
) maxWidth
= eachWidth
;
402 if (maxHeight
<eachHeight
) maxHeight
= eachHeight
;
410 int nbHor
= GetNumHor(),
413 // this formula works, but I don't know why.
414 // Please, be sure what you do if you modify it!!
415 if (m_radioWidth
[0]<0)
416 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
418 totHeight
= nbVer
* (maxHeight
+cy1
/2);
419 totWidth
= nbHor
* (maxWidth
+cx1
);
422 // Requires a bigger group box in plain Windows
423 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+(3*cy1
)/2,TRUE
);
425 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+cy1
,TRUE
);
432 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
433 // JACS 2/12/93. CTL3D draws group label quite high.
435 int startX
= x_offset
;
436 int startY
= y_offset
;
438 for ( i
= 0 ; i
< m_noItems
; i
++)
440 // Bidimensional radio adjustment
441 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
443 if (m_windowStyle
& wxRA_VERTICAL
)
446 x_offset
+= maxWidth
+ cx1
;
451 y_offset
+= maxHeight
;
452 if (m_radioWidth
[0]>0)
458 if (m_radioWidth
[i
]<0)
460 // It's a labeled item
461 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
462 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, & GetFont());
464 // How do we find out radio button bitmap size!!
465 // By adjusting them carefully, manually :-)
466 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
467 eachHeight
= (int)((3*cyf
)/2);
471 eachWidth
= m_radioWidth
[i
];
472 eachHeight
= m_radioHeight
[i
];
475 MoveWindow((HWND
) m_radioButtons
[i
],x_offset
,y_offset
,eachWidth
,eachHeight
,TRUE
);
476 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
478 y_offset
+= maxHeight
;
479 if (m_radioWidth
[0]>0)
483 x_offset
+= maxWidth
+ cx1
;
488 void wxRadioBox::GetSize(int *width
, int *height
) const
491 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
494 wxFindMaxSize(m_hWnd
, &rect
);
497 for (i
= 0; i
< m_noItems
; i
++)
498 wxFindMaxSize(m_radioButtons
[i
], &rect
);
500 *width
= rect
.right
- rect
.left
;
501 *height
= rect
.bottom
- rect
.top
;
504 void wxRadioBox::GetPosition(int *x
, int *y
) const
506 wxWindow
*parent
= GetParent();
508 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
511 for (i
= 0; i
< m_noItems
; i
++)
512 wxFindMaxSize(m_radioButtons
[i
], &rect
);
515 wxFindMaxSize(m_hWnd
, &rect
);
517 // Since we now have the absolute screen coords,
518 // if there's a parent we must subtract its top left corner
524 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
526 // We may be faking the client origin.
527 // So a window that's really at (0, 30) may appear
528 // (to wxWin apps) to be at (0, 0).
531 wxPoint
pt(GetParent()->GetClientAreaOrigin());
540 wxString
wxRadioBox::GetLabel() const
544 GetWindowText((HWND
) m_hWnd
, wxBuffer
, 300);
545 return wxString(wxBuffer
);
547 else return wxString("");
550 void wxRadioBox::SetLabel(const wxString
& label
)
553 SetWindowText((HWND
) m_hWnd
, label
);
556 void wxRadioBox::SetFocus()
560 if (m_selectedButton
== -1)
561 ::SetFocus((HWND
) m_radioButtons
[0]);
563 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
568 bool wxRadioBox::Show(bool show
)
577 ShowWindow((HWND
) m_hWnd
, cshow
);
579 for (i
= 0; i
< m_noItems
; i
++)
580 ShowWindow((HWND
) m_radioButtons
[i
], cshow
);
584 // Enable a specific button
585 void wxRadioBox::Enable(int item
, bool enable
)
588 wxWindow::Enable(enable
);
589 else if (item
< m_noItems
)
590 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
593 // Enable all controls
594 void wxRadioBox::Enable(bool enable
)
596 wxControl::Enable(enable
);
599 for (i
= 0; i
< m_noItems
; i
++)
600 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
603 // Show a specific button
604 void wxRadioBox::Show(int item
, bool show
)
607 wxRadioBox::Show(show
);
608 else if (item
< m_noItems
)
615 ShowWindow((HWND
) m_radioButtons
[item
], cshow
);
619 WXHBRUSH
wxRadioBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
620 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
625 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
626 return (WXHBRUSH
) hbrush
;
630 if (GetParent()->GetTransparentBackground())
631 SetBkMode((HDC
) pDC
, TRANSPARENT
);
633 SetBkMode((HDC
) pDC
, OPAQUE
);
635 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
636 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
638 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
640 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
641 // has a zero usage count.
642 // backgroundBrush->RealizeResource();
643 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
646 // For single selection items only
647 wxString
wxRadioBox::GetStringSelection() const
650 int sel
= GetSelection();
652 result
= GetString(sel
);
657 bool wxRadioBox::SetStringSelection(const wxString
& s
)
659 int sel
= FindString (s
);
669 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
672 for (i
= 0; i
< Number(); i
++)
673 if (GetRadioButtons()[i
] == hWnd
)
678 void wxRadioBox::Command (wxCommandEvent
& event
)
680 SetSelection (event
.m_commandInt
);
681 ProcessCommand (event
);
684 long wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
686 if (nMsg
== WM_NCHITTEST
)
688 int xPos
= LOWORD(lParam
); // horizontal position of cursor
689 int yPos
= HIWORD(lParam
); // vertical position of cursor
691 ScreenToClient(&xPos
, &yPos
);
693 // Make sure you can drag by the top of the groupbox, but let
694 // other (enclosed) controls get mouse events also
696 return (long)HTCLIENT
;
699 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
702 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
704 HWND hwndBtn
= (HWND
)hWndBtn
;
706 if ( !s_wndprocRadioBtn
)
707 s_wndprocRadioBtn
= (WNDPROC
)::GetWindowLong(hwndBtn
, GWL_WNDPROC
);
709 ::SetWindowLong(hwndBtn
, GWL_WNDPROC
, (long)wxRadioBtnWndProc
);
710 ::SetWindowLong(hwndBtn
, GWL_USERDATA
, (long)this);
713 // ---------------------------------------------------------------------------
714 // window proc for radio buttons
715 // ---------------------------------------------------------------------------
717 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
722 bool processed
= TRUE
;
723 if ( msg
!= WM_KEYDOWN
)
728 wxRadioBox
*radiobox
= (wxRadioBox
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
730 wxCHECK_MSG( radiobox
, 0, "radio button without radio box?" );
732 int sel
= radiobox
->GetSelection();
741 sel
-= radiobox
->GetNumVer();
749 sel
+= radiobox
->GetNumVer();
754 wxNavigationKeyEvent event
;
755 event
.SetDirection(!(::GetKeyState(VK_SHIFT
) & 0x100));
756 event
.SetWindowChange(FALSE
);
757 event
.SetEventObject(radiobox
);
759 if ( radiobox
->GetEventHandler()->ProcessEvent(event
) )
770 if ( sel
>= 0 && sel
< radiobox
->Number() )
771 radiobox
->SetSelection(sel
);
776 return ::CallWindowProc(s_wndprocRadioBtn
, hwnd
, msg
, wParam
, lParam
);