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 // Subclass again for purposes of dialog editing mode
151 SubclassWin((WXHWND
)m_hWnd
);
153 /* Label will be the same as button font now.
156 labelFont->RealizeResource();
157 if (labelFont->GetResourceHandle())
158 SendMessage(ms_handle,WM_SETFONT,
159 (WPARAM)labelFont->GetResourceHandle(),0L);
163 // Some radio boxes test consecutive id.
164 (void)NewControlId() ;
165 m_radioButtons
= new WXHWND
[n
];
166 m_radioWidth
= new int[n
] ;
167 m_radioHeight
= new int[n
] ;
169 for (i
= 0; i
< n
; i
++)
171 m_radioWidth
[i
] = m_radioHeight
[i
] = -1 ;
173 if (i
== 0 && style
==0)
174 groupStyle
= WS_GROUP
;
175 long newId
= NewControlId();
176 long msStyle
= groupStyle
| RADIO_FLAGS
;
178 m_radioButtons
[i
] = (WXHWND
) CreateWindowEx(exStyle
, RADIO_CLASS
, choices
[i
],
180 the_handle
, (HMENU
)newId
, wxGetInstance(), NULL
);
184 Ctl3dSubclassCtl((HWND
) m_hWnd
);
190 SendMessage((HWND
)m_radioButtons
[i
],WM_SETFONT
,
191 (WPARAM
)GetFont()->GetResourceHandle(),0L);
193 m_subControls
.Append((wxObject
*)newId
);
196 // Create a dummy radio control to end the group.
197 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
|RADIO_FLAGS
, 0,0,0,0, the_handle
, (HMENU
)NewControlId(), wxGetInstance(), NULL
);
201 SetSize(x
, y
, width
, height
);
207 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
208 const wxPoint
& pos
, const wxSize
& size
,
209 int n
, const wxBitmap
*choices
[],
210 int majorDim
, long style
,
211 const wxValidator
& val
, const wxString
& name
)
213 m_selectedButton
= -1;
220 parent
->AddChild(this);
221 m_backgroundColour
= parent
->GetDefaultBackgroundColour() ;
222 m_foregroundColour
= parent
->GetDefaultForegroundColour() ;
224 m_windowStyle
= (long&)style
;
232 m_windowId
= NewControlId();
237 m_noRowsOrCols
= majorDim
;
240 m_majorDim
= majorDim
;
243 long msStyle
= GROUP_FLAGS
;
246 WXDWORD exStyle
= Determine3DEffects(0, &want3D
) ;
247 // Even with extended styles, need to combine with WS_BORDER
248 // for them to look right.
249 if (want3D
&& ((m_windowStyle
& wxSIMPLE_BORDER
) || (m_windowStyle
& wxRAISED_BORDER
) ||
250 (m_windowStyle
& wxSUNKEN_BORDER
) || (m_windowStyle
& wxDOUBLE_BORDER
)))
251 msStyle
|= WS_BORDER
;
253 m_hWnd
= (WXHWND
) CreateWindowEx((DWORD
) exStyle
, GROUP_CLASS
, (title
== "" ? NULL
: (const char *)title
),
256 (HWND
) parent
->GetHWND(), (HMENU
) m_windowId
, wxGetInstance(), NULL
) ;
261 labelFont->RealizeResource();
262 if (labelFont->GetResourceHandle())
263 SendMessage(ms_handle,WM_SETFONT,
264 (WPARAM)labelFont->GetResourceHandle(),0L);
267 the_handle
= (HWND
) parent
->GetHWND();
272 Ctl3dSubclassCtl((HWND
) m_hWnd
);
277 SetFont(* parent
->GetFont());
279 // Subclass again for purposes of dialog editing mode
280 SubclassWin((WXHWND
)m_hWnd
);
282 (void)NewControlId() ;
283 m_radioButtons
= new WXHWND
[n
];
284 m_radioWidth
= new int[n
] ;
285 m_radioHeight
= new int[n
] ;
288 for (i
= 0; i
< n
; i
++)
291 if (i
== 0 && style
==0)
292 groupStyle
= WS_GROUP
;
293 long newId
= NewControlId();
294 m_radioWidth
[i
] = ((wxBitmap
*)choices
[i
])->GetWidth();
295 m_radioHeight
[i
] = ((wxBitmap
*)choices
[i
])->GetHeight();
297 sprintf(tmp
,"Toggle%d",i
) ;
298 long msStyle
= groupStyle
| RADIO_FLAGS
;
299 m_radioButtons
[i
] = (WXHWND
) CreateWindowEx(exStyle
, RADIO_CLASS
, tmp
,
301 the_handle
, (HMENU
)newId
, wxhInstance
, NULL
);
305 Ctl3dSubclassCtl((HWND
) m_hWnd
);
309 m_subControls
.Append((wxObject
*)newId
);
311 // Create a dummy radio control to end the group.
312 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
|RADIO_FLAGS
, 0,0,0,0, the_handle
, (HMENU
)NewControlId(), wxGetInstance(), NULL
);
316 SetSize(x
, y
, width
, height
);
322 wxRadioBox::~wxRadioBox(void)
324 m_isBeingDeleted
= TRUE
;
329 for (i
= 0; i
< m_noItems
; i
++)
330 DestroyWindow((HWND
) m_radioButtons
[i
]);
331 delete[] m_radioButtons
;
334 delete[] m_radioWidth
;
336 delete[] m_radioHeight
;
338 ::DestroyWindow((HWND
) m_hWnd
) ;
343 wxString
wxRadioBox::GetLabel(int item
) const
345 GetWindowText((HWND
)m_radioButtons
[item
], wxBuffer
, 300);
346 return wxString(wxBuffer
);
349 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
351 m_radioWidth
[item
] = m_radioHeight
[item
] = -1 ;
352 SetWindowText((HWND
)m_radioButtons
[item
], (const char *)label
);
355 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
358 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN ;
359 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN ;
363 int wxRadioBox::FindString(const wxString
& s
) const
366 for (i
= 0; i
< m_noItems
; i
++)
368 GetWindowText((HWND
) m_radioButtons
[i
], wxBuffer
, 1000);
375 void wxRadioBox::SetSelection(int N
)
377 if ((N
< 0) || (N
>= m_noItems
))
380 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
381 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
382 SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
384 SendMessage((HWND
) m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
385 m_selectedButton
= N
;
388 // Get single selection, for single choice list items
389 int wxRadioBox::GetSelection(void) const
391 return m_selectedButton
;
394 // Find string for position
395 wxString
wxRadioBox::GetString(int N
) const
397 GetWindowText((HWND
) m_radioButtons
[N
], wxBuffer
, 1000);
398 return wxString(wxBuffer
);
401 void wxRadioBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
403 int currentX
, currentY
;
404 GetPosition(¤tX
, ¤tY
);
408 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
410 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
417 int current_width
, cyf
;
420 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
421 // Attempt to have a look coherent with other platforms:
422 // We compute the biggest toggle dim, then we align all
423 // items according this value.
428 for (i
= 0 ; i
< m_noItems
; i
++)
432 if (m_radioWidth
[i
]<0)
434 // It's a labelled toggle
435 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
436 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, GetFont());
437 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
438 eachHeight
= (int)((3*cyf
)/2);
442 eachWidth
= m_radioWidth
[i
] ;
443 eachHeight
= m_radioHeight
[i
] ;
445 if (maxWidth
<eachWidth
) maxWidth
= eachWidth
;
446 if (maxHeight
<eachHeight
) maxHeight
= eachHeight
;
456 if (m_windowStyle
& wxRA_VERTICAL
)
459 nbHor
= (m_noItems
+m_majorDim
-1)/m_majorDim
;
464 nbVer
= (m_noItems
+m_majorDim
-1)/m_majorDim
;
467 // this formula works, but I don't know why.
468 // Please, be sure what you do if you modify it!!
469 if (m_radioWidth
[0]<0)
470 totHeight
= (nbVer
* maxHeight
) + cy1
/2 ;
472 totHeight
= nbVer
* (maxHeight
+cy1
/2) ;
473 totWidth
= nbHor
* (maxWidth
+cx1
) ;
476 // Requires a bigger group box in plain Windows
477 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+(3*cy1
)/2,TRUE
) ;
479 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+cy1
,TRUE
) ;
486 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
487 // JACS 2/12/93. CTL3D draws group label quite high.
489 int startX
= x_offset
;
490 int startY
= y_offset
;
492 for ( i
= 0 ; i
< m_noItems
; i
++)
494 // Bidimensional radio adjustment
495 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
497 if (m_windowStyle
& wxRA_VERTICAL
)
500 x_offset
+= maxWidth
+ cx1
;
505 y_offset
+= maxHeight
;
506 if (m_radioWidth
[0]>0)
512 if (m_radioWidth
[i
]<0)
514 // It's a labeled item
515 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
516 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
,GetFont());
518 // How do we find out radio button bitmap size!!
519 // By adjusting them carefully, manually :-)
520 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
521 eachHeight
= (int)((3*cyf
)/2);
525 eachWidth
= m_radioWidth
[i
] ;
526 eachHeight
= m_radioHeight
[i
] ;
529 MoveWindow((HWND
) m_radioButtons
[i
],x_offset
,y_offset
,eachWidth
,eachHeight
,TRUE
);
530 if (m_windowStyle
& wxRA_VERTICAL
)
532 y_offset
+= maxHeight
;
533 if (m_radioWidth
[0]>0)
537 x_offset
+= maxWidth
+ cx1
;
541 void wxRadioBox::GetSize(int *width
, int *height
) const
544 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
547 wxFindMaxSize(m_hWnd
, &rect
);
550 for (i
= 0; i
< m_noItems
; i
++)
551 wxFindMaxSize(m_radioButtons
[i
], &rect
);
553 *width
= rect
.right
- rect
.left
;
554 *height
= rect
.bottom
- rect
.top
;
557 void wxRadioBox::GetPosition(int *x
, int *y
) const
559 wxWindow
*parent
= GetParent();
561 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
564 for (i
= 0; i
< m_noItems
; i
++)
565 wxFindMaxSize(m_radioButtons
[i
], &rect
);
568 wxFindMaxSize(m_hWnd
, &rect
);
570 // Since we now have the absolute screen coords,
571 // if there's a parent we must subtract its top left corner
577 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
584 wxString
wxRadioBox::GetLabel(void) const
588 GetWindowText((HWND
) m_hWnd
, wxBuffer
, 300);
589 return wxString(wxBuffer
);
591 else return wxString("");
594 void wxRadioBox::SetLabel(const wxString
& label
)
597 SetWindowText((HWND
) m_hWnd
, label
);
600 void wxRadioBox::SetFocus(void)
604 ::SetFocus((HWND) m_radioButtons[0]);
606 /* Begin Alberts Patch 26. 5. 1997*/
609 if (m_selectedButton
== -1)
610 ::SetFocus((HWND
) m_radioButtons
[0]);
612 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
614 /* Ende Alberts Patch*/
618 bool wxRadioBox::Show(bool show
)
626 ShowWindow((HWND
) m_hWnd
, cshow
);
628 for (i
= 0; i
< m_noItems
; i
++)
629 ShowWindow((HWND
) m_radioButtons
[i
], cshow
);
633 // Enable a specific button
634 void wxRadioBox::Enable(int item
, bool enable
)
637 wxWindow::Enable(enable
) ;
638 else if (item
< m_noItems
)
639 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
642 // Enable all controls
643 void wxRadioBox::Enable(bool enable
)
645 wxControl::Enable(enable
);
648 for (i
= 0; i
< m_noItems
; i
++)
649 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
652 // Show a specific button
653 void wxRadioBox::Show(int item
, bool show
)
656 wxRadioBox::Show(show
) ;
657 else if (item
< m_noItems
)
664 ShowWindow((HWND
) m_radioButtons
[item
], cshow
);
668 WXHBRUSH
wxRadioBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
669 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
674 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
675 return (WXHBRUSH
) hbrush
;
679 if (GetParent()->GetTransparentBackground())
680 SetBkMode((HDC
) pDC
, TRANSPARENT
);
682 SetBkMode((HDC
) pDC
, OPAQUE
);
684 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
685 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
687 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
689 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
690 // has a zero usage count.
691 // backgroundBrush->RealizeResource();
692 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
695 // For single selection items only
696 wxString
wxRadioBox::GetStringSelection (void) const
698 int sel
= GetSelection ();
700 return this->GetString (sel
);
705 bool wxRadioBox::SetStringSelection (const wxString
& s
)
707 int sel
= FindString (s
);
718 void wxRadioBox::SetLabelFont(wxFont *font)
720 // Decrement the usage count of the old label font
721 // (we may be able to free it up)
723 labelFont->ReleaseResource();
727 // Increment usage count
731 HWND hWnd = GetHWND();
736 font->RealizeResource();
738 if (font->GetResourceHandle())
739 SendMessage(hWnd, WM_SETFONT,
740 (WPARAM)font->GetResourceHandle(),TRUE);
747 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
750 for (i
= 0; i
< Number(); i
++)
751 if (GetRadioButtons()[i
] == hWnd
)
756 void wxRadioBox::Command (wxCommandEvent
& event
)
758 SetSelection (event
.m_commandInt
);
759 ProcessCommand (event
);