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 m_majorDim
= majorDim
;
121 long msStyle
= GROUP_FLAGS
;
124 WXDWORD exStyle
= Determine3DEffects(0, &want3D
) ;
125 // Even with extended styles, need to combine with WS_BORDER
126 // for them to look right.
127 if (want3D
&& ((m_windowStyle
& wxSIMPLE_BORDER
) || (m_windowStyle
& wxRAISED_BORDER
) ||
128 (m_windowStyle
& wxSUNKEN_BORDER
) || (m_windowStyle
& wxDOUBLE_BORDER
)))
129 msStyle
|= WS_BORDER
;
132 m_hWnd
= (WXHWND
) CreateWindowEx((DWORD
) exStyle
, GROUP_CLASS
, (title
== "" ? NULL
: (const char *)title
),
135 (HWND
) parent
->GetHWND(), (HMENU
) m_windowId
, wxGetInstance(), NULL
) ;
137 HWND the_handle
= (HWND
) parent
->GetHWND() ;
142 Ctl3dSubclassCtl((HWND
) m_hWnd
);
147 SetFont(* parent
->GetFont());
149 // Subclass again for purposes of dialog editing mode
150 SubclassWin((WXHWND
)m_hWnd
);
152 /* Label will be the same as button font now.
155 labelFont->RealizeResource();
156 if (labelFont->GetResourceHandle())
157 SendMessage(ms_handle,WM_SETFONT,
158 (WPARAM)labelFont->GetResourceHandle(),0L);
162 // Some radio boxes test consecutive id.
163 (void)NewControlId() ;
164 m_radioButtons
= new WXHWND
[n
];
165 m_radioWidth
= new int[n
] ;
166 m_radioHeight
= new int[n
] ;
168 for (i
= 0; i
< n
; i
++)
170 m_radioWidth
[i
] = m_radioHeight
[i
] = -1 ;
172 if (i
== 0 && style
==0)
173 groupStyle
= WS_GROUP
;
174 long newId
= NewControlId();
175 long msStyle
= groupStyle
| RADIO_FLAGS
;
177 m_radioButtons
[i
] = (WXHWND
) CreateWindowEx(exStyle
, RADIO_CLASS
, choices
[i
],
179 the_handle
, (HMENU
)newId
, wxGetInstance(), NULL
);
183 Ctl3dSubclassCtl((HWND
) m_hWnd
);
189 SendMessage((HWND
)m_radioButtons
[i
],WM_SETFONT
,
190 (WPARAM
)GetFont()->GetResourceHandle(),0L);
192 m_subControls
.Append((wxObject
*)newId
);
195 // Create a dummy radio control to end the group.
196 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
|RADIO_FLAGS
, 0,0,0,0, the_handle
, (HMENU
)NewControlId(), wxGetInstance(), NULL
);
200 SetSize(x
, y
, width
, height
);
206 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
207 const wxPoint
& pos
, const wxSize
& size
,
208 int n
, const wxBitmap
*choices
[],
209 int majorDim
, long style
,
210 const wxValidator
& val
, const wxString
& name
)
212 m_selectedButton
= -1;
219 parent
->AddChild(this);
220 m_backgroundColour
= parent
->GetDefaultBackgroundColour() ;
221 m_foregroundColour
= parent
->GetDefaultForegroundColour() ;
223 m_windowStyle
= (long&)style
;
231 m_windowId
= NewControlId();
236 m_noRowsOrCols
= majorDim
;
239 m_majorDim
= majorDim
;
242 long msStyle
= GROUP_FLAGS
;
245 WXDWORD exStyle
= Determine3DEffects(0, &want3D
) ;
246 // Even with extended styles, need to combine with WS_BORDER
247 // for them to look right.
248 if (want3D
&& ((m_windowStyle
& wxSIMPLE_BORDER
) || (m_windowStyle
& wxRAISED_BORDER
) ||
249 (m_windowStyle
& wxSUNKEN_BORDER
) || (m_windowStyle
& wxDOUBLE_BORDER
)))
250 msStyle
|= WS_BORDER
;
252 m_hWnd
= (WXHWND
) CreateWindowEx((DWORD
) exStyle
, GROUP_CLASS
, (title
== "" ? NULL
: (const char *)title
),
255 (HWND
) parent
->GetHWND(), (HMENU
) m_windowId
, wxGetInstance(), NULL
) ;
260 labelFont->RealizeResource();
261 if (labelFont->GetResourceHandle())
262 SendMessage(ms_handle,WM_SETFONT,
263 (WPARAM)labelFont->GetResourceHandle(),0L);
266 the_handle
= (HWND
) parent
->GetHWND();
271 Ctl3dSubclassCtl((HWND
) m_hWnd
);
276 SetFont(* parent
->GetFont());
278 // Subclass again for purposes of dialog editing mode
279 SubclassWin((WXHWND
)m_hWnd
);
281 (void)NewControlId() ;
282 m_radioButtons
= new WXHWND
[n
];
283 m_radioWidth
= new int[n
] ;
284 m_radioHeight
= new int[n
] ;
287 for (i
= 0; i
< n
; i
++)
290 if (i
== 0 && style
==0)
291 groupStyle
= WS_GROUP
;
292 long newId
= NewControlId();
293 m_radioWidth
[i
] = ((wxBitmap
*)choices
[i
])->GetWidth();
294 m_radioHeight
[i
] = ((wxBitmap
*)choices
[i
])->GetHeight();
296 sprintf(tmp
,"Toggle%d",i
) ;
297 long msStyle
= groupStyle
| RADIO_FLAGS
;
298 m_radioButtons
[i
] = (WXHWND
) CreateWindowEx(exStyle
, RADIO_CLASS
, tmp
,
300 the_handle
, (HMENU
)newId
, wxhInstance
, NULL
);
304 Ctl3dSubclassCtl((HWND
) m_hWnd
);
308 m_subControls
.Append((wxObject
*)newId
);
310 // Create a dummy radio control to end the group.
311 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
|RADIO_FLAGS
, 0,0,0,0, the_handle
, (HMENU
)NewControlId(), wxGetInstance(), NULL
);
315 SetSize(x
, y
, width
, height
);
321 wxRadioBox::~wxRadioBox(void)
323 m_isBeingDeleted
= TRUE
;
328 for (i
= 0; i
< m_noItems
; i
++)
329 DestroyWindow((HWND
) m_radioButtons
[i
]);
330 delete[] m_radioButtons
;
333 delete[] m_radioWidth
;
335 delete[] m_radioHeight
;
337 ::DestroyWindow((HWND
) m_hWnd
) ;
342 wxString
wxRadioBox::GetLabel(int item
) const
344 GetWindowText((HWND
)m_radioButtons
[item
], wxBuffer
, 300);
345 return wxString(wxBuffer
);
348 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
350 m_radioWidth
[item
] = m_radioHeight
[item
] = -1 ;
351 SetWindowText((HWND
)m_radioButtons
[item
], (const char *)label
);
354 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
357 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN ;
358 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN ;
362 int wxRadioBox::FindString(const wxString
& s
) const
365 for (i
= 0; i
< m_noItems
; i
++)
367 GetWindowText((HWND
) m_radioButtons
[i
], wxBuffer
, 1000);
374 void wxRadioBox::SetSelection(int N
)
376 if ((N
< 0) || (N
>= m_noItems
))
379 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
380 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
381 SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
383 SendMessage((HWND
) m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
384 m_selectedButton
= N
;
387 // Get single selection, for single choice list items
388 int wxRadioBox::GetSelection(void) const
390 return m_selectedButton
;
393 // Find string for position
394 wxString
wxRadioBox::GetString(int N
) const
396 GetWindowText((HWND
) m_radioButtons
[N
], wxBuffer
, 1000);
397 return wxString(wxBuffer
);
400 void wxRadioBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
402 int currentX
, currentY
;
403 GetPosition(¤tX
, ¤tY
);
407 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
409 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
416 int current_width
, cyf
;
419 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
420 // Attempt to have a look coherent with other platforms:
421 // We compute the biggest toggle dim, then we align all
422 // items according this value.
427 for (i
= 0 ; i
< m_noItems
; i
++)
431 if (m_radioWidth
[i
]<0)
433 // It's a labelled toggle
434 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
435 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, GetFont());
436 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
437 eachHeight
= (int)((3*cyf
)/2);
441 eachWidth
= m_radioWidth
[i
] ;
442 eachHeight
= m_radioHeight
[i
] ;
444 if (maxWidth
<eachWidth
) maxWidth
= eachWidth
;
445 if (maxHeight
<eachHeight
) maxHeight
= eachHeight
;
455 if (m_windowStyle
& wxRA_VERTICAL
)
458 nbHor
= (m_noItems
+m_majorDim
-1)/m_majorDim
;
463 nbVer
= (m_noItems
+m_majorDim
-1)/m_majorDim
;
466 // this formula works, but I don't know why.
467 // Please, be sure what you do if you modify it!!
468 if (m_radioWidth
[0]<0)
469 totHeight
= (nbVer
* maxHeight
) + cy1
/2 ;
471 totHeight
= nbVer
* (maxHeight
+cy1
/2) ;
472 totWidth
= nbHor
* (maxWidth
+cx1
) ;
475 // Requires a bigger group box in plain Windows
476 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+(3*cy1
)/2,TRUE
) ;
478 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+cy1
,TRUE
) ;
485 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
486 // JACS 2/12/93. CTL3D draws group label quite high.
488 int startX
= x_offset
;
489 int startY
= y_offset
;
491 for ( i
= 0 ; i
< m_noItems
; i
++)
493 // Bidimensional radio adjustment
494 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
496 if (m_windowStyle
& wxRA_VERTICAL
)
499 x_offset
+= maxWidth
+ cx1
;
504 y_offset
+= maxHeight
;
505 if (m_radioWidth
[0]>0)
511 if (m_radioWidth
[i
]<0)
513 // It's a labeled item
514 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
515 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
,GetFont());
517 // How do we find out radio button bitmap size!!
518 // By adjusting them carefully, manually :-)
519 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
520 eachHeight
= (int)((3*cyf
)/2);
524 eachWidth
= m_radioWidth
[i
] ;
525 eachHeight
= m_radioHeight
[i
] ;
528 MoveWindow((HWND
) m_radioButtons
[i
],x_offset
,y_offset
,eachWidth
,eachHeight
,TRUE
);
529 if (m_windowStyle
& wxRA_VERTICAL
)
531 y_offset
+= maxHeight
;
532 if (m_radioWidth
[0]>0)
536 x_offset
+= maxWidth
+ cx1
;
540 void wxRadioBox::GetSize(int *width
, int *height
) const
543 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
546 wxFindMaxSize(m_hWnd
, &rect
);
549 for (i
= 0; i
< m_noItems
; i
++)
550 wxFindMaxSize(m_radioButtons
[i
], &rect
);
552 *width
= rect
.right
- rect
.left
;
553 *height
= rect
.bottom
- rect
.top
;
556 void wxRadioBox::GetPosition(int *x
, int *y
) const
558 wxWindow
*parent
= GetParent();
560 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
563 for (i
= 0; i
< m_noItems
; i
++)
564 wxFindMaxSize(m_radioButtons
[i
], &rect
);
567 wxFindMaxSize(m_hWnd
, &rect
);
569 // Since we now have the absolute screen coords,
570 // if there's a parent we must subtract its top left corner
576 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
583 wxString
wxRadioBox::GetLabel(void) const
587 GetWindowText((HWND
) m_hWnd
, wxBuffer
, 300);
588 return wxString(wxBuffer
);
590 else return wxString("");
593 void wxRadioBox::SetLabel(const wxString
& label
)
596 SetWindowText((HWND
) m_hWnd
, label
);
599 void wxRadioBox::SetFocus(void)
603 ::SetFocus((HWND) m_radioButtons[0]);
605 /* Begin Alberts Patch 26. 5. 1997*/
608 if (m_selectedButton
== -1)
609 ::SetFocus((HWND
) m_radioButtons
[0]);
611 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
613 /* Ende Alberts Patch*/
617 bool wxRadioBox::Show(bool show
)
625 ShowWindow((HWND
) m_hWnd
, cshow
);
627 for (i
= 0; i
< m_noItems
; i
++)
628 ShowWindow((HWND
) m_radioButtons
[i
], cshow
);
632 // Enable a specific button
633 void wxRadioBox::Enable(int item
, bool enable
)
636 wxWindow::Enable(enable
) ;
637 else if (item
< m_noItems
)
638 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
641 // Enable all controls
642 void wxRadioBox::Enable(bool enable
)
644 wxControl::Enable(enable
);
647 for (i
= 0; i
< m_noItems
; i
++)
648 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
651 // Show a specific button
652 void wxRadioBox::Show(int item
, bool show
)
655 wxRadioBox::Show(show
) ;
656 else if (item
< m_noItems
)
663 ShowWindow((HWND
) m_radioButtons
[item
], cshow
);
667 WXHBRUSH
wxRadioBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
668 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
673 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
674 return (WXHBRUSH
) hbrush
;
678 if (GetParent()->GetTransparentBackground())
679 SetBkMode((HDC
) pDC
, TRANSPARENT
);
681 SetBkMode((HDC
) pDC
, OPAQUE
);
683 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
684 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
686 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
688 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
689 // has a zero usage count.
690 // backgroundBrush->RealizeResource();
691 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
694 // For single selection items only
695 wxString
wxRadioBox::GetStringSelection (void) const
697 int sel
= GetSelection ();
699 return this->GetString (sel
);
704 bool wxRadioBox::SetStringSelection (const wxString
& s
)
706 int sel
= FindString (s
);
717 void wxRadioBox::SetLabelFont(wxFont *font)
719 // Decrement the usage count of the old label font
720 // (we may be able to free it up)
722 labelFont->ReleaseResource();
726 // Increment usage count
730 HWND hWnd = GetHWND();
735 font->RealizeResource();
737 if (font->GetResourceHandle())
738 SendMessage(hWnd, WM_SETFONT,
739 (WPARAM)font->GetResourceHandle(),TRUE);
746 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
749 for (i
= 0; i
< Number(); i
++)
750 if (GetRadioButtons()[i
] == hWnd
)
755 void wxRadioBox::Command (wxCommandEvent
& event
)
757 SetSelection (event
.m_commandInt
);
758 ProcessCommand (event
);