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(const WXUINT param
, const 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
, const wxWindowID id
, const wxString
& title
,
89 const wxPoint
& pos
, const wxSize
& size
,
90 const int n
, const wxString choices
[],
91 const int majorDim
, const 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
, const wxWindowID id
, const wxString
& title
,
207 const wxPoint
& pos
, const wxSize
& size
,
208 const int n
, const wxBitmap
*choices
[],
209 const int majorDim
, const 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(const int item
) const
344 GetWindowText((HWND
)m_radioButtons
[item
], wxBuffer
, 300);
345 return wxString(wxBuffer
);
348 void wxRadioBox::SetLabel(const 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(const 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(const 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(const int N
) const
396 GetWindowText((HWND
) m_radioButtons
[N
], wxBuffer
, 1000);
397 return wxString(wxBuffer
);
400 void wxRadioBox::SetSize(const int x
, const int y
, const int width
, const int height
, const 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
))
421 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
422 // Attempt to have a look coherent with other platforms:
423 // We compute the biggest toggle dim, then we align all
424 // items according this value.
429 for (i
= 0 ; i
< m_noItems
; i
++)
433 if (m_radioWidth
[i
]<0)
435 // It's a labelled toggle
436 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
437 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, GetFont());
438 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
439 eachHeight
= (int)((3*cyf
)/2);
443 eachWidth
= m_radioWidth
[i
] ;
444 eachHeight
= m_radioHeight
[i
] ;
446 if (maxWidth
<eachWidth
) maxWidth
= eachWidth
;
447 if (maxHeight
<eachHeight
) maxHeight
= eachHeight
;
457 if (m_windowStyle
& wxRA_VERTICAL
)
460 nbHor
= (m_noItems
+m_majorDim
-1)/m_majorDim
;
465 nbVer
= (m_noItems
+m_majorDim
-1)/m_majorDim
;
468 // this formula works, but I don't know why.
469 // Please, be sure what you do if you modify it!!
470 if (m_radioWidth
[0]<0)
471 totHeight
= (nbVer
* maxHeight
) + cy1
/2 ;
473 totHeight
= nbVer
* (maxHeight
+cy1
/2) ;
474 totWidth
= nbHor
* (maxWidth
+cx1
) ;
477 // Requires a bigger group box in plain Windows
478 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+(3*cy1
)/2,TRUE
) ;
480 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+cy1
,TRUE
) ;
487 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
488 // JACS 2/12/93. CTL3D draws group label quite high.
490 int startX
= x_offset
;
491 int startY
= y_offset
;
493 for ( i
= 0 ; i
< m_noItems
; i
++)
495 // Bidimensional radio adjustment
496 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
498 if (m_windowStyle
& wxRA_VERTICAL
)
501 x_offset
+= maxWidth
+ cx1
;
506 y_offset
+= maxHeight
;
507 if (m_radioWidth
[0]>0)
513 if (m_radioWidth
[i
]<0)
515 // It's a labeled item
516 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
517 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
,GetFont());
519 // How do we find out radio button bitmap size!!
520 // By adjusting them carefully, manually :-)
521 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
522 eachHeight
= (int)((3*cyf
)/2);
526 eachWidth
= m_radioWidth
[i
] ;
527 eachHeight
= m_radioHeight
[i
] ;
530 MoveWindow((HWND
) m_radioButtons
[i
],x_offset
,y_offset
,eachWidth
,eachHeight
,TRUE
);
531 if (m_windowStyle
& wxRA_VERTICAL
)
533 y_offset
+= maxHeight
;
534 if (m_radioWidth
[0]>0)
538 x_offset
+= maxWidth
+ cx1
;
542 void wxRadioBox::GetSize(int *width
, int *height
) const
545 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
548 wxFindMaxSize(m_hWnd
, &rect
);
551 for (i
= 0; i
< m_noItems
; i
++)
552 wxFindMaxSize(m_radioButtons
[i
], &rect
);
554 *width
= rect
.right
- rect
.left
;
555 *height
= rect
.bottom
- rect
.top
;
558 void wxRadioBox::GetPosition(int *x
, int *y
) const
560 wxWindow
*parent
= GetParent();
562 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
565 for (i
= 0; i
< m_noItems
; i
++)
566 wxFindMaxSize(m_radioButtons
[i
], &rect
);
569 wxFindMaxSize(m_hWnd
, &rect
);
571 // Since we now have the absolute screen coords,
572 // if there's a parent we must subtract its top left corner
578 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
585 wxString
wxRadioBox::GetLabel(void) const
589 GetWindowText((HWND
) m_hWnd
, wxBuffer
, 300);
590 return wxString(wxBuffer
);
592 else return wxString("");
595 void wxRadioBox::SetLabel(const wxString
& label
)
598 SetWindowText((HWND
) m_hWnd
, label
);
601 void wxRadioBox::SetFocus(void)
605 ::SetFocus((HWND) m_radioButtons[0]);
607 /* Begin Alberts Patch 26. 5. 1997*/
610 if (m_selectedButton
== -1)
611 ::SetFocus((HWND
) m_radioButtons
[0]);
613 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
615 /* Ende Alberts Patch*/
619 bool wxRadioBox::Show(const bool show
)
627 ShowWindow((HWND
) m_hWnd
, cshow
);
629 for (i
= 0; i
< m_noItems
; i
++)
630 ShowWindow((HWND
) m_radioButtons
[i
], cshow
);
634 // Enable a specific button
635 void wxRadioBox::Enable(const int item
, const bool enable
)
638 wxWindow::Enable(enable
) ;
639 else if (item
< m_noItems
)
640 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
643 // Enable all controls
644 void wxRadioBox::Enable(const bool enable
)
646 wxControl::Enable(enable
);
649 for (i
= 0; i
< m_noItems
; i
++)
650 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
653 // Show a specific button
654 void wxRadioBox::Show(const int item
, const bool show
)
657 wxRadioBox::Show(show
) ;
658 else if (item
< m_noItems
)
665 ShowWindow((HWND
) m_radioButtons
[item
], cshow
);
669 WXHBRUSH
wxRadioBox::OnCtlColor(const WXHDC pDC
, const WXHWND pWnd
, const WXUINT nCtlColor
,
670 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
675 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
676 return (WXHBRUSH
) hbrush
;
680 if (GetParent()->GetTransparentBackground())
681 SetBkMode((HDC
) pDC
, TRANSPARENT
);
683 SetBkMode((HDC
) pDC
, OPAQUE
);
685 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
686 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
688 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
690 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
691 // has a zero usage count.
692 // backgroundBrush->RealizeResource();
693 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
696 // For single selection items only
697 wxString
wxRadioBox::GetStringSelection (void) const
699 int sel
= GetSelection ();
701 return this->GetString (sel
);
706 bool wxRadioBox::SetStringSelection (const wxString
& s
)
708 int sel
= FindString (s
);
719 void wxRadioBox::SetLabelFont(wxFont *font)
721 // Decrement the usage count of the old label font
722 // (we may be able to free it up)
724 labelFont->ReleaseResource();
728 // Increment usage count
732 HWND hWnd = GetHWND();
737 font->RealizeResource();
739 if (font->GetResourceHandle())
740 SendMessage(hWnd, WM_SETFONT,
741 (WPARAM)font->GetResourceHandle(),TRUE);
748 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
751 for (i
= 0; i
< Number(); i
++)
752 if (GetRadioButtons()[i
] == hWnd
)
757 void wxRadioBox::Command (wxCommandEvent
& event
)
759 SetSelection (event
.m_commandInt
);
760 ProcessCommand (event
);