1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "radiobox.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/radiobox.h"
29 #include "wx/msw/private.h"
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
35 bool wxRadioBox::MSWCommand(WXUINT param
, WXWORD id
)
37 if (param
== BN_CLICKED
)
41 for (i
= 0; i
< m_noItems
; i
++)
42 if (id
== GetWindowLong((HWND
) m_radioButtons
[i
], GWL_ID
))
46 for (i
= 0; i
< m_noItems
; i
++)
47 if (id
== GetWindowWord((HWND
) m_radioButtons
[i
], GWW_ID
))
51 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
52 event
.SetInt( m_selectedButton
);
53 event
.SetEventObject( this );
54 ProcessCommand(event
);
60 #if WXWIN_COMPATIBILITY
61 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
62 int x
, int y
, int width
, int height
,
63 int n
, char **choices
,
64 int majorDim
, long style
, const char *name
)
66 wxString
*choices2
= new wxString
[n
];
67 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
68 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
69 wxDefaultValidator
, name
);
77 wxRadioBox::wxRadioBox(void)
79 m_selectedButton
= -1;
82 m_radioButtons
= NULL
;
85 m_radioHeight
= NULL
;
88 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
89 const wxPoint
& pos
, const wxSize
& size
,
90 int n
, const wxString choices
[],
91 int majorDim
, long style
,
92 const wxValidator
& val
, const wxString
& name
)
94 m_selectedButton
= -1;
100 parent
->AddChild(this);
101 m_backgroundColour
= parent
->GetDefaultBackgroundColour() ;
102 m_foregroundColour
= parent
->GetDefaultForegroundColour() ;
104 m_windowStyle
= (long&)style
;
112 m_windowId
= NewControlId();
116 m_noRowsOrCols
= majorDim
;
119 else // Seemed to make sense to put this 'else' here... (RD)
120 m_majorDim
= majorDim
;
122 long msStyle
= GROUP_FLAGS
;
125 WXDWORD exStyle
= Determine3DEffects(0, &want3D
) ;
126 // Even with extended styles, need to combine with WS_BORDER
127 // for them to look right.
128 if (want3D
&& ((m_windowStyle
& wxSIMPLE_BORDER
) || (m_windowStyle
& wxRAISED_BORDER
) ||
129 (m_windowStyle
& wxSUNKEN_BORDER
) || (m_windowStyle
& wxDOUBLE_BORDER
)))
130 msStyle
|= WS_BORDER
;
133 m_hWnd
= (WXHWND
) CreateWindowEx((DWORD
) exStyle
, GROUP_CLASS
, (title
== "" ? NULL
: (const char *)title
),
136 (HWND
) parent
->GetHWND(), (HMENU
) m_windowId
, wxGetInstance(), NULL
) ;
138 HWND the_handle
= (HWND
) parent
->GetHWND() ;
143 Ctl3dSubclassCtl((HWND
) m_hWnd
);
148 SetFont(* parent
->GetFont());
150 SubclassWin((WXHWND
)m_hWnd
);
152 // Some radio boxes test consecutive id.
153 (void)NewControlId() ;
154 m_radioButtons
= new WXHWND
[n
];
155 m_radioWidth
= new int[n
] ;
156 m_radioHeight
= new int[n
] ;
158 for (i
= 0; i
< n
; i
++)
160 m_radioWidth
[i
] = m_radioHeight
[i
] = -1 ;
162 if (i
== 0 && style
==0)
163 groupStyle
= WS_GROUP
;
164 long newId
= NewControlId();
165 long msStyle
= groupStyle
| RADIO_FLAGS
;
167 m_radioButtons
[i
] = (WXHWND
) CreateWindowEx(exStyle
, RADIO_CLASS
, choices
[i
],
169 the_handle
, (HMENU
)newId
, wxGetInstance(), NULL
);
173 Ctl3dSubclassCtl((HWND
) m_hWnd
);
179 SendMessage((HWND
)m_radioButtons
[i
],WM_SETFONT
,
180 (WPARAM
)GetFont()->GetResourceHandle(),0L);
182 m_subControls
.Append((wxObject
*)newId
);
185 // Create a dummy radio control to end the group.
186 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
|RADIO_FLAGS
, 0,0,0,0, the_handle
, (HMENU
)NewControlId(), wxGetInstance(), NULL
);
190 SetSize(x
, y
, width
, height
);
196 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
197 const wxPoint
& pos
, const wxSize
& size
,
198 int n
, const wxBitmap
*choices
[],
199 int majorDim
, long style
,
200 const wxValidator
& val
, const wxString
& name
)
202 m_selectedButton
= -1;
209 parent
->AddChild(this);
210 m_backgroundColour
= parent
->GetDefaultBackgroundColour() ;
211 m_foregroundColour
= parent
->GetDefaultForegroundColour() ;
213 m_windowStyle
= (long&)style
;
221 m_windowId
= NewControlId();
226 m_noRowsOrCols
= majorDim
;
229 m_majorDim
= majorDim
;
232 long msStyle
= GROUP_FLAGS
;
235 WXDWORD exStyle
= Determine3DEffects(0, &want3D
) ;
236 // Even with extended styles, need to combine with WS_BORDER
237 // for them to look right.
238 if (want3D
&& ((m_windowStyle
& wxSIMPLE_BORDER
) || (m_windowStyle
& wxRAISED_BORDER
) ||
239 (m_windowStyle
& wxSUNKEN_BORDER
) || (m_windowStyle
& wxDOUBLE_BORDER
)))
240 msStyle
|= WS_BORDER
;
242 m_hWnd
= (WXHWND
) CreateWindowEx((DWORD
) exStyle
, GROUP_CLASS
, (title
== "" ? NULL
: (const char *)title
),
245 (HWND
) parent
->GetHWND(), (HMENU
) m_windowId
, wxGetInstance(), NULL
) ;
247 the_handle
= (HWND
) parent
->GetHWND();
252 Ctl3dSubclassCtl((HWND
) m_hWnd
);
257 SetFont(* parent
->GetFont());
259 // Subclass again for purposes of dialog editing mode
260 SubclassWin((WXHWND
)m_hWnd
);
262 (void)NewControlId() ;
263 m_radioButtons
= new WXHWND
[n
];
264 m_radioWidth
= new int[n
] ;
265 m_radioHeight
= new int[n
] ;
268 for (i
= 0; i
< n
; i
++)
271 if (i
== 0 && style
==0)
272 groupStyle
= WS_GROUP
;
273 long newId
= NewControlId();
274 m_radioWidth
[i
] = ((wxBitmap
*)choices
[i
])->GetWidth();
275 m_radioHeight
[i
] = ((wxBitmap
*)choices
[i
])->GetHeight();
277 sprintf(tmp
,"Toggle%d",i
) ;
278 long msStyle
= groupStyle
| RADIO_FLAGS
;
279 m_radioButtons
[i
] = (WXHWND
) CreateWindowEx(exStyle
, RADIO_CLASS
, tmp
,
281 the_handle
, (HMENU
)newId
, wxhInstance
, NULL
);
285 Ctl3dSubclassCtl((HWND
) m_hWnd
);
289 m_subControls
.Append((wxObject
*)newId
);
291 // Create a dummy radio control to end the group.
292 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
|RADIO_FLAGS
, 0,0,0,0, the_handle
, (HMENU
)NewControlId(), wxGetInstance(), NULL
);
296 SetSize(x
, y
, width
, height
);
302 wxRadioBox::~wxRadioBox(void)
304 m_isBeingDeleted
= TRUE
;
309 for (i
= 0; i
< m_noItems
; i
++)
310 DestroyWindow((HWND
) m_radioButtons
[i
]);
311 delete[] m_radioButtons
;
314 delete[] m_radioWidth
;
316 delete[] m_radioHeight
;
318 ::DestroyWindow((HWND
) m_hWnd
) ;
323 wxString
wxRadioBox::GetLabel(int item
) const
325 GetWindowText((HWND
)m_radioButtons
[item
], wxBuffer
, 300);
326 return wxString(wxBuffer
);
329 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
331 m_radioWidth
[item
] = m_radioHeight
[item
] = -1 ;
332 SetWindowText((HWND
)m_radioButtons
[item
], (const char *)label
);
335 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
338 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN ;
339 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN ;
343 int wxRadioBox::FindString(const wxString
& s
) const
346 for (i
= 0; i
< m_noItems
; i
++)
348 GetWindowText((HWND
) m_radioButtons
[i
], wxBuffer
, 1000);
355 void wxRadioBox::SetSelection(int N
)
357 if ((N
< 0) || (N
>= m_noItems
))
360 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
361 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
362 SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
364 SendMessage((HWND
) m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
365 m_selectedButton
= N
;
368 // Get single selection, for single choice list items
369 int wxRadioBox::GetSelection(void) const
371 return m_selectedButton
;
374 // Find string for position
375 wxString
wxRadioBox::GetString(int N
) const
377 GetWindowText((HWND
) m_radioButtons
[N
], wxBuffer
, 1000);
378 return wxString(wxBuffer
);
381 void wxRadioBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
383 int currentX
, currentY
;
384 GetPosition(¤tX
, ¤tY
);
388 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
390 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
393 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
399 int current_width
, cyf
;
402 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
403 // Attempt to have a look coherent with other platforms:
404 // We compute the biggest toggle dim, then we align all
405 // items according this value.
410 for (i
= 0 ; i
< m_noItems
; i
++)
414 if (m_radioWidth
[i
]<0)
416 // It's a labelled toggle
417 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
418 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, GetFont());
419 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
420 eachHeight
= (int)((3*cyf
)/2);
424 eachWidth
= m_radioWidth
[i
] ;
425 eachHeight
= m_radioHeight
[i
] ;
427 if (maxWidth
<eachWidth
) maxWidth
= eachWidth
;
428 if (maxHeight
<eachHeight
) maxHeight
= eachHeight
;
438 if (m_windowStyle
& wxRA_VERTICAL
)
441 nbHor
= (m_noItems
+m_majorDim
-1)/m_majorDim
;
446 nbVer
= (m_noItems
+m_majorDim
-1)/m_majorDim
;
449 // this formula works, but I don't know why.
450 // Please, be sure what you do if you modify it!!
451 if (m_radioWidth
[0]<0)
452 totHeight
= (nbVer
* maxHeight
) + cy1
/2 ;
454 totHeight
= nbVer
* (maxHeight
+cy1
/2) ;
455 totWidth
= nbHor
* (maxWidth
+cx1
) ;
458 // Requires a bigger group box in plain Windows
459 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+(3*cy1
)/2,TRUE
) ;
461 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+cy1
,TRUE
) ;
468 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
469 // JACS 2/12/93. CTL3D draws group label quite high.
471 int startX
= x_offset
;
472 int startY
= y_offset
;
474 for ( i
= 0 ; i
< m_noItems
; i
++)
476 // Bidimensional radio adjustment
477 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
479 if (m_windowStyle
& wxRA_VERTICAL
)
482 x_offset
+= maxWidth
+ cx1
;
487 y_offset
+= maxHeight
;
488 if (m_radioWidth
[0]>0)
494 if (m_radioWidth
[i
]<0)
496 // It's a labeled item
497 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
498 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
,GetFont());
500 // How do we find out radio button bitmap size!!
501 // By adjusting them carefully, manually :-)
502 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
503 eachHeight
= (int)((3*cyf
)/2);
507 eachWidth
= m_radioWidth
[i
] ;
508 eachHeight
= m_radioHeight
[i
] ;
511 MoveWindow((HWND
) m_radioButtons
[i
],x_offset
,y_offset
,eachWidth
,eachHeight
,TRUE
);
512 if (m_windowStyle
& wxRA_VERTICAL
)
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();
543 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
546 for (i
= 0; i
< m_noItems
; i
++)
547 wxFindMaxSize(m_radioButtons
[i
], &rect
);
550 wxFindMaxSize(m_hWnd
, &rect
);
552 // Since we now have the absolute screen coords,
553 // if there's a parent we must subtract its top left corner
559 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
561 // We may be faking the client origin.
562 // So a window that's really at (0, 30) may appear
563 // (to wxWin apps) to be at (0, 0).
566 wxPoint
pt(GetParent()->GetClientAreaOrigin());
575 wxString
wxRadioBox::GetLabel(void) const
579 GetWindowText((HWND
) m_hWnd
, wxBuffer
, 300);
580 return wxString(wxBuffer
);
582 else return wxString("");
585 void wxRadioBox::SetLabel(const wxString
& label
)
588 SetWindowText((HWND
) m_hWnd
, label
);
591 void wxRadioBox::SetFocus(void)
595 if (m_selectedButton
== -1)
596 ::SetFocus((HWND
) m_radioButtons
[0]);
598 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
603 bool wxRadioBox::Show(bool show
)
611 ShowWindow((HWND
) m_hWnd
, cshow
);
613 for (i
= 0; i
< m_noItems
; i
++)
614 ShowWindow((HWND
) m_radioButtons
[i
], cshow
);
618 // Enable a specific button
619 void wxRadioBox::Enable(int item
, bool enable
)
622 wxWindow::Enable(enable
) ;
623 else if (item
< m_noItems
)
624 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
627 // Enable all controls
628 void wxRadioBox::Enable(bool enable
)
630 wxControl::Enable(enable
);
633 for (i
= 0; i
< m_noItems
; i
++)
634 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
637 // Show a specific button
638 void wxRadioBox::Show(int item
, bool show
)
641 wxRadioBox::Show(show
) ;
642 else if (item
< m_noItems
)
649 ShowWindow((HWND
) m_radioButtons
[item
], cshow
);
653 WXHBRUSH
wxRadioBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
654 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
659 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
660 return (WXHBRUSH
) hbrush
;
664 if (GetParent()->GetTransparentBackground())
665 SetBkMode((HDC
) pDC
, TRANSPARENT
);
667 SetBkMode((HDC
) pDC
, OPAQUE
);
669 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
670 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
672 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
674 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
675 // has a zero usage count.
676 // backgroundBrush->RealizeResource();
677 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
680 // For single selection items only
681 wxString
wxRadioBox::GetStringSelection (void) const
683 int sel
= GetSelection ();
685 return this->GetString (sel
);
690 bool wxRadioBox::SetStringSelection (const wxString
& s
)
692 int sel
= FindString (s
);
702 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
705 for (i
= 0; i
< Number(); i
++)
706 if (GetRadioButtons()[i
] == hWnd
)
711 void wxRadioBox::Command (wxCommandEvent
& event
)
713 SetSelection (event
.m_commandInt
);
714 ProcessCommand (event
);
717 long wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
719 if (nMsg
== WM_NCHITTEST
)
721 int xPos
= LOWORD(lParam
); // horizontal position of cursor
722 int yPos
= HIWORD(lParam
); // vertical position of cursor
724 ScreenToClient(&xPos
, &yPos
);
726 // Make sure you can drag by the top of the groupbox, but let
727 // other (enclosed) controls get mouse events also
729 return (long)HTCLIENT
;
732 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);