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
;
71 // ===========================================================================
73 // ===========================================================================
75 // ---------------------------------------------------------------------------
77 // ---------------------------------------------------------------------------
79 int wxRadioBox::GetNumVer() const
81 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
87 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
91 int wxRadioBox::GetNumHor() const
93 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
95 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
103 bool wxRadioBox::MSWCommand(WXUINT param
, WXWORD id
)
105 if ( param
== BN_CLICKED
)
107 m_selectedButton
= -1;
109 for ( int i
= 0; i
< m_noItems
; i
++ )
111 if ( id
== GET_WIN_ID(m_radioButtons
[i
]) )
113 m_selectedButton
= i
;
119 wxASSERT_MSG( m_selectedButton
!= -1, "click from alien button?" );
121 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
122 event
.SetInt( m_selectedButton
);
123 event
.SetEventObject( this );
124 ProcessCommand(event
);
132 #if WXWIN_COMPATIBILITY
133 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
134 int x
, int y
, int width
, int height
,
135 int n
, char **choices
,
136 int majorDim
, long style
, const char *name
)
138 wxString
*choices2
= new wxString
[n
];
139 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
140 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
141 wxDefaultValidator
, name
);
149 wxRadioBox::wxRadioBox()
151 m_selectedButton
= -1;
154 m_radioButtons
= NULL
;
157 m_radioHeight
= NULL
;
160 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
161 const wxPoint
& pos
, const wxSize
& size
,
162 int n
, const wxString choices
[],
163 int majorDim
, long style
,
164 const wxValidator
& val
, const wxString
& name
)
166 m_selectedButton
= -1;
172 parent
->AddChild(this);
173 m_backgroundColour
= parent
->GetBackgroundColour();
174 m_foregroundColour
= parent
->GetForegroundColour();
176 m_windowStyle
= (long&)style
;
184 m_windowId
= NewControlId();
191 m_majorDim
= majorDim
;
192 m_noRowsOrCols
= majorDim
;
194 long msStyle
= GROUP_FLAGS
;
197 WXDWORD exStyle
= Determine3DEffects(0, &want3D
);
198 // Even with extended styles, need to combine with WS_BORDER
199 // for them to look right.
201 if ( want3D || wxStyleHasBorder(m_windowStyle) )
202 msStyle |= WS_BORDER;
205 HWND hwndParent
= (HWND
)parent
->GetHWND();
207 m_hWnd
= (WXHWND
)::CreateWindowEx
223 Ctl3dSubclassCtl((HWND
)m_hWnd
);
226 #endif // wxUSE_CTL3D
228 SetFont(parent
->GetFont());
232 // Some radio boxes test consecutive id.
233 (void)NewControlId();
234 m_radioButtons
= new WXHWND
[n
];
235 m_radioWidth
= new int[n
];
236 m_radioHeight
= new int[n
];
238 for (i
= 0; i
< n
; i
++)
240 m_radioWidth
[i
] = m_radioHeight
[i
] = -1;
242 if ( i
== 0 && style
== 0 )
243 groupStyle
= WS_GROUP
;
244 long newId
= NewControlId();
245 long msStyle
= groupStyle
| RADIO_FLAGS
;
247 HWND hwndBtn
= CreateWindowEx(exStyle
, RADIO_CLASS
,
251 (HMENU
)newId
, wxGetInstance(),
254 m_radioButtons
[i
] = (WXHWND
)hwndBtn
;
255 SubclassRadioButton((WXHWND
)hwndBtn
);
257 wxFont
& font
= GetFont();
260 SendMessage(hwndBtn
, WM_SETFONT
,
261 (WPARAM
)font
.GetResourceHandle(), 0L);
264 m_subControls
.Append((wxObject
*)newId
);
267 // Create a dummy radio control to end the group.
268 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
| RADIO_FLAGS
,
269 0, 0, 0, 0, hwndParent
,
270 (HMENU
)NewControlId(), wxGetInstance(), NULL
);
274 SetSize(x
, y
, width
, height
);
279 wxRadioBox::~wxRadioBox()
281 m_isBeingDeleted
= TRUE
;
286 for (i
= 0; i
< m_noItems
; i
++)
287 DestroyWindow((HWND
) m_radioButtons
[i
]);
288 delete[] m_radioButtons
;
291 delete[] m_radioWidth
;
293 delete[] m_radioHeight
;
295 ::DestroyWindow((HWND
) m_hWnd
);
300 wxString
wxRadioBox::GetLabel(int item
) const
302 GetWindowText((HWND
)m_radioButtons
[item
], wxBuffer
, 300);
303 return wxString(wxBuffer
);
306 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
308 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
309 SetWindowText((HWND
)m_radioButtons
[item
], (const char *)label
);
312 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
315 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
316 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
320 int wxRadioBox::FindString(const wxString
& s
) const
323 for (i
= 0; i
< m_noItems
; i
++)
325 GetWindowText((HWND
) m_radioButtons
[i
], wxBuffer
, 1000);
332 void wxRadioBox::SetSelection(int N
)
334 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), "invalid radiobox index" );
336 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
337 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
338 ::SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
340 ::SendMessage((HWND
)m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
341 ::SetFocus((HWND
)m_radioButtons
[N
]);
343 m_selectedButton
= N
;
346 // Get single selection, for single choice list items
347 int wxRadioBox::GetSelection() const
349 return m_selectedButton
;
352 // Find string for position
353 wxString
wxRadioBox::GetString(int N
) const
355 return wxGetWindowText(m_radioButtons
[N
]);
358 // Restored old code.
359 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
361 int currentX
, currentY
;
362 GetPosition(¤tX
, ¤tY
);
366 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
368 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
375 int current_width
, cyf
;
378 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
379 // Attempt to have a look coherent with other platforms:
380 // We compute the biggest toggle dim, then we align all
381 // items according this value.
386 for (i
= 0 ; i
< m_noItems
; i
++)
390 if (m_radioWidth
[i
]<0)
392 // It's a labelled toggle
393 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
394 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, & GetFont());
395 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
396 eachHeight
= (int)((3*cyf
)/2);
400 eachWidth
= m_radioWidth
[i
];
401 eachHeight
= m_radioHeight
[i
];
403 if (maxWidth
<eachWidth
) maxWidth
= eachWidth
;
404 if (maxHeight
<eachHeight
) maxHeight
= eachHeight
;
412 int nbHor
= GetNumHor(),
415 // this formula works, but I don't know why.
416 // Please, be sure what you do if you modify it!!
417 if (m_radioWidth
[0]<0)
418 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
420 totHeight
= nbVer
* (maxHeight
+cy1
/2);
421 totWidth
= nbHor
* (maxWidth
+cx1
);
424 // Requires a bigger group box in plain Windows
425 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+(3*cy1
)/2,TRUE
);
427 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+cy1
,TRUE
);
434 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
435 // JACS 2/12/93. CTL3D draws group label quite high.
437 int startX
= x_offset
;
438 int startY
= y_offset
;
440 for ( i
= 0 ; i
< m_noItems
; i
++)
442 // Bidimensional radio adjustment
443 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
445 if (m_windowStyle
& wxRA_VERTICAL
)
448 x_offset
+= maxWidth
+ cx1
;
453 y_offset
+= maxHeight
;
454 if (m_radioWidth
[0]>0)
460 if (m_radioWidth
[i
]<0)
462 // It's a labeled item
463 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
464 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, & GetFont());
466 // How do we find out radio button bitmap size!!
467 // By adjusting them carefully, manually :-)
468 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
469 eachHeight
= (int)((3*cyf
)/2);
473 eachWidth
= m_radioWidth
[i
];
474 eachHeight
= m_radioHeight
[i
];
477 MoveWindow((HWND
) m_radioButtons
[i
],x_offset
,y_offset
,eachWidth
,eachHeight
,TRUE
);
478 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
480 y_offset
+= maxHeight
;
481 if (m_radioWidth
[0]>0)
485 x_offset
+= maxWidth
+ cx1
;
490 void wxRadioBox::GetSize(int *width
, int *height
) const
493 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
496 wxFindMaxSize(m_hWnd
, &rect
);
499 for (i
= 0; i
< m_noItems
; i
++)
500 wxFindMaxSize(m_radioButtons
[i
], &rect
);
502 *width
= rect
.right
- rect
.left
;
503 *height
= rect
.bottom
- rect
.top
;
506 void wxRadioBox::GetPosition(int *x
, int *y
) const
508 wxWindow
*parent
= GetParent();
510 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
513 for (i
= 0; i
< m_noItems
; i
++)
514 wxFindMaxSize(m_radioButtons
[i
], &rect
);
517 wxFindMaxSize(m_hWnd
, &rect
);
519 // Since we now have the absolute screen coords,
520 // if there's a parent we must subtract its top left corner
526 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
528 // We may be faking the client origin.
529 // So a window that's really at (0, 30) may appear
530 // (to wxWin apps) to be at (0, 0).
533 wxPoint
pt(GetParent()->GetClientAreaOrigin());
542 wxString
wxRadioBox::GetLabel() const
546 GetWindowText((HWND
) m_hWnd
, wxBuffer
, 300);
547 return wxString(wxBuffer
);
549 else return wxString("");
552 void wxRadioBox::SetLabel(const wxString
& label
)
555 SetWindowText((HWND
) m_hWnd
, label
);
558 void wxRadioBox::SetFocus()
562 if (m_selectedButton
== -1)
563 ::SetFocus((HWND
) m_radioButtons
[0]);
565 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
570 bool wxRadioBox::Show(bool show
)
579 ShowWindow((HWND
) m_hWnd
, cshow
);
581 for (i
= 0; i
< m_noItems
; i
++)
582 ShowWindow((HWND
) m_radioButtons
[i
], cshow
);
586 // Enable a specific button
587 void wxRadioBox::Enable(int item
, bool enable
)
590 wxWindow::Enable(enable
);
591 else if (item
< m_noItems
)
592 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
595 // Enable all controls
596 void wxRadioBox::Enable(bool enable
)
598 wxControl::Enable(enable
);
601 for (i
= 0; i
< m_noItems
; i
++)
602 ::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
= (WNDPROC
)::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 // ---------------------------------------------------------------------------
719 // window proc for radio buttons
720 // ---------------------------------------------------------------------------
724 LRESULT APIENTRY _EXPORT
wxRadioBtnWndProc(HWND hwnd
,
729 bool processed
= TRUE
;
730 if ( msg
!= WM_KEYDOWN
)
735 wxRadioBox
*radiobox
= (wxRadioBox
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
737 wxCHECK_MSG( radiobox
, 0, "radio button without radio box?" );
739 int sel
= radiobox
->GetSelection();
748 sel
-= radiobox
->GetNumVer();
756 sel
+= radiobox
->GetNumVer();
761 wxNavigationKeyEvent event
;
762 event
.SetDirection(!(::GetKeyState(VK_SHIFT
) & 0x100));
763 event
.SetWindowChange(FALSE
);
764 event
.SetEventObject(radiobox
);
766 if ( radiobox
->GetEventHandler()->ProcessEvent(event
) )
777 if ( sel
>= 0 && sel
< radiobox
->Number() )
778 radiobox
->SetSelection(sel
);
783 return ::CallWindowProc((WNDPROC
) s_wndprocRadioBtn
, hwnd
, msg
, wParam
, lParam
);