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
->GetBackgroundColour() ;
102 m_foregroundColour
= parent
->GetForegroundColour() ;
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.
129 if ( want3D || wxStyleHasBorder(m_windowStyle) )
130 msStyle |= WS_BORDER;
134 HWND the_handle
= (HWND
) parent
->GetHWND() ;
136 m_hWnd
= (WXHWND
)::CreateWindowEx
152 Ctl3dSubclassCtl((HWND
)m_hWnd
);
157 SetFont(* parent
->GetFont());
159 SubclassWin((WXHWND
)m_hWnd
);
161 // Some radio boxes test consecutive id.
162 (void)NewControlId() ;
163 m_radioButtons
= new WXHWND
[n
];
164 m_radioWidth
= new int[n
] ;
165 m_radioHeight
= new int[n
] ;
167 for (i
= 0; i
< n
; i
++)
169 m_radioWidth
[i
] = m_radioHeight
[i
] = -1 ;
171 if (i
== 0 && style
==0)
172 groupStyle
= WS_GROUP
;
173 long newId
= NewControlId();
174 long msStyle
= groupStyle
| RADIO_FLAGS
;
176 m_radioButtons
[i
] = (WXHWND
) CreateWindowEx(exStyle
, RADIO_CLASS
, choices
[i
],
178 the_handle
, (HMENU
)newId
, wxGetInstance(), NULL
);
182 Ctl3dSubclassCtl((HWND
) m_hWnd
);
188 SendMessage((HWND
)m_radioButtons
[i
],WM_SETFONT
,
189 (WPARAM
)GetFont()->GetResourceHandle(),0L);
191 m_subControls
.Append((wxObject
*)newId
);
194 // Create a dummy radio control to end the group.
195 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
|RADIO_FLAGS
, 0,0,0,0, the_handle
, (HMENU
)NewControlId(), wxGetInstance(), NULL
);
199 SetSize(x
, y
, width
, height
);
205 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
206 const wxPoint
& pos
, const wxSize
& size
,
207 int n
, const wxBitmap
*choices
[],
208 int majorDim
, long style
,
209 const wxValidator
& val
, const wxString
& name
)
211 m_selectedButton
= -1;
218 parent
->AddChild(this);
219 m_backgroundColour
= parent
->GetBackgroundColour() ;
220 m_foregroundColour
= parent
->GetForegroundColour() ;
222 m_windowStyle
= (long&)style
;
230 m_windowId
= NewControlId();
235 m_noRowsOrCols
= majorDim
;
238 m_majorDim
= majorDim
;
241 long msStyle
= GROUP_FLAGS
;
244 WXDWORD exStyle
= Determine3DEffects(0, &want3D
) ;
245 // Even with extended styles, need to combine with WS_BORDER
246 // for them to look right.
247 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
248 msStyle
|= WS_BORDER
;
250 m_hWnd
= (WXHWND
) CreateWindowEx((DWORD
) exStyle
, GROUP_CLASS
, (title
== "" ? NULL
: (const char *)title
),
253 (HWND
) parent
->GetHWND(), (HMENU
) m_windowId
, wxGetInstance(), NULL
) ;
255 the_handle
= (HWND
) parent
->GetHWND();
260 Ctl3dSubclassCtl((HWND
) m_hWnd
);
265 SetFont(* parent
->GetFont());
267 // Subclass again for purposes of dialog editing mode
268 SubclassWin((WXHWND
)m_hWnd
);
270 (void)NewControlId() ;
271 m_radioButtons
= new WXHWND
[n
];
272 m_radioWidth
= new int[n
] ;
273 m_radioHeight
= new int[n
] ;
276 for (i
= 0; i
< n
; i
++)
279 if (i
== 0 && style
==0)
280 groupStyle
= WS_GROUP
;
281 long newId
= NewControlId();
282 m_radioWidth
[i
] = ((wxBitmap
*)choices
[i
])->GetWidth();
283 m_radioHeight
[i
] = ((wxBitmap
*)choices
[i
])->GetHeight();
285 sprintf(tmp
,"Toggle%d",i
) ;
286 long msStyle
= groupStyle
| RADIO_FLAGS
;
287 m_radioButtons
[i
] = (WXHWND
) CreateWindowEx(exStyle
, RADIO_CLASS
, tmp
,
289 the_handle
, (HMENU
)newId
, wxhInstance
, NULL
);
293 Ctl3dSubclassCtl((HWND
) m_hWnd
);
297 m_subControls
.Append((wxObject
*)newId
);
299 // Create a dummy radio control to end the group.
300 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
|RADIO_FLAGS
, 0,0,0,0, the_handle
, (HMENU
)NewControlId(), wxGetInstance(), NULL
);
304 SetSize(x
, y
, width
, height
);
310 wxRadioBox::~wxRadioBox(void)
312 m_isBeingDeleted
= TRUE
;
317 for (i
= 0; i
< m_noItems
; i
++)
318 DestroyWindow((HWND
) m_radioButtons
[i
]);
319 delete[] m_radioButtons
;
322 delete[] m_radioWidth
;
324 delete[] m_radioHeight
;
326 ::DestroyWindow((HWND
) m_hWnd
) ;
331 wxString
wxRadioBox::GetLabel(int item
) const
333 GetWindowText((HWND
)m_radioButtons
[item
], wxBuffer
, 300);
334 return wxString(wxBuffer
);
337 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
339 m_radioWidth
[item
] = m_radioHeight
[item
] = -1 ;
340 SetWindowText((HWND
)m_radioButtons
[item
], (const char *)label
);
343 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
346 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN ;
347 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN ;
351 int wxRadioBox::FindString(const wxString
& s
) const
354 for (i
= 0; i
< m_noItems
; i
++)
356 GetWindowText((HWND
) m_radioButtons
[i
], wxBuffer
, 1000);
363 void wxRadioBox::SetSelection(int N
)
365 if ((N
< 0) || (N
>= m_noItems
))
368 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
369 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
370 SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
372 SendMessage((HWND
) m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
373 m_selectedButton
= N
;
376 // Get single selection, for single choice list items
377 int wxRadioBox::GetSelection(void) const
379 return m_selectedButton
;
382 // Find string for position
383 wxString
wxRadioBox::GetString(int N
) const
385 GetWindowText((HWND
) m_radioButtons
[N
], wxBuffer
, 1000);
386 return wxString(wxBuffer
);
389 void wxRadioBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
391 int currentX
, currentY
;
392 GetPosition(¤tX
, ¤tY
);
396 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
398 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
401 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
407 int current_width
, cyf
;
410 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
411 // Attempt to have a look coherent with other platforms:
412 // We compute the biggest toggle dim, then we align all
413 // items according this value.
418 for (i
= 0 ; i
< m_noItems
; i
++)
422 if (m_radioWidth
[i
]<0)
424 // It's a labelled toggle
425 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
426 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, GetFont());
427 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
428 eachHeight
= (int)((3*cyf
)/2);
432 eachWidth
= m_radioWidth
[i
] ;
433 eachHeight
= m_radioHeight
[i
] ;
435 if (maxWidth
<eachWidth
) maxWidth
= eachWidth
;
436 if (maxHeight
<eachHeight
) maxHeight
= eachHeight
;
446 if (m_windowStyle
& wxRA_VERTICAL
)
449 nbHor
= (m_noItems
+m_majorDim
-1)/m_majorDim
;
454 nbVer
= (m_noItems
+m_majorDim
-1)/m_majorDim
;
457 // this formula works, but I don't know why.
458 // Please, be sure what you do if you modify it!!
459 if (m_radioWidth
[0]<0)
460 totHeight
= (nbVer
* maxHeight
) + cy1
/2 ;
462 totHeight
= nbVer
* (maxHeight
+cy1
/2) ;
463 totWidth
= nbHor
* (maxWidth
+cx1
) ;
466 // Requires a bigger group box in plain Windows
467 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+(3*cy1
)/2,TRUE
) ;
469 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+cy1
,TRUE
) ;
476 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
477 // JACS 2/12/93. CTL3D draws group label quite high.
479 int startX
= x_offset
;
480 int startY
= y_offset
;
482 for ( i
= 0 ; i
< m_noItems
; i
++)
484 // Bidimensional radio adjustment
485 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
487 if (m_windowStyle
& wxRA_VERTICAL
)
490 x_offset
+= maxWidth
+ cx1
;
495 y_offset
+= maxHeight
;
496 if (m_radioWidth
[0]>0)
502 if (m_radioWidth
[i
]<0)
504 // It's a labeled item
505 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
506 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
,GetFont());
508 // How do we find out radio button bitmap size!!
509 // By adjusting them carefully, manually :-)
510 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
511 eachHeight
= (int)((3*cyf
)/2);
515 eachWidth
= m_radioWidth
[i
] ;
516 eachHeight
= m_radioHeight
[i
] ;
519 MoveWindow((HWND
) m_radioButtons
[i
],x_offset
,y_offset
,eachWidth
,eachHeight
,TRUE
);
520 if (m_windowStyle
& wxRA_VERTICAL
)
522 y_offset
+= maxHeight
;
523 if (m_radioWidth
[0]>0)
527 x_offset
+= maxWidth
+ cx1
;
531 void wxRadioBox::GetSize(int *width
, int *height
) const
534 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
537 wxFindMaxSize(m_hWnd
, &rect
);
540 for (i
= 0; i
< m_noItems
; i
++)
541 wxFindMaxSize(m_radioButtons
[i
], &rect
);
543 *width
= rect
.right
- rect
.left
;
544 *height
= rect
.bottom
- rect
.top
;
547 void wxRadioBox::GetPosition(int *x
, int *y
) const
549 wxWindow
*parent
= GetParent();
551 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
554 for (i
= 0; i
< m_noItems
; i
++)
555 wxFindMaxSize(m_radioButtons
[i
], &rect
);
558 wxFindMaxSize(m_hWnd
, &rect
);
560 // Since we now have the absolute screen coords,
561 // if there's a parent we must subtract its top left corner
567 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
569 // We may be faking the client origin.
570 // So a window that's really at (0, 30) may appear
571 // (to wxWin apps) to be at (0, 0).
574 wxPoint
pt(GetParent()->GetClientAreaOrigin());
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 if (m_selectedButton
== -1)
604 ::SetFocus((HWND
) m_radioButtons
[0]);
606 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
611 bool wxRadioBox::Show(bool show
)
619 ShowWindow((HWND
) m_hWnd
, cshow
);
621 for (i
= 0; i
< m_noItems
; i
++)
622 ShowWindow((HWND
) m_radioButtons
[i
], cshow
);
626 // Enable a specific button
627 void wxRadioBox::Enable(int item
, bool enable
)
630 wxWindow::Enable(enable
) ;
631 else if (item
< m_noItems
)
632 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
635 // Enable all controls
636 void wxRadioBox::Enable(bool enable
)
638 wxControl::Enable(enable
);
641 for (i
= 0; i
< m_noItems
; i
++)
642 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
645 // Show a specific button
646 void wxRadioBox::Show(int item
, bool show
)
649 wxRadioBox::Show(show
) ;
650 else if (item
< m_noItems
)
657 ShowWindow((HWND
) m_radioButtons
[item
], cshow
);
661 WXHBRUSH
wxRadioBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
662 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
667 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
668 return (WXHBRUSH
) hbrush
;
672 if (GetParent()->GetTransparentBackground())
673 SetBkMode((HDC
) pDC
, TRANSPARENT
);
675 SetBkMode((HDC
) pDC
, OPAQUE
);
677 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
678 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
680 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
682 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
683 // has a zero usage count.
684 // backgroundBrush->RealizeResource();
685 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
688 // For single selection items only
689 wxString
wxRadioBox::GetStringSelection (void) const
691 int sel
= GetSelection ();
693 return this->GetString (sel
);
698 bool wxRadioBox::SetStringSelection (const wxString
& s
)
700 int sel
= FindString (s
);
710 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
713 for (i
= 0; i
< Number(); i
++)
714 if (GetRadioButtons()[i
] == hWnd
)
719 void wxRadioBox::Command (wxCommandEvent
& event
)
721 SetSelection (event
.m_commandInt
);
722 ProcessCommand (event
);
725 long wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
727 if (nMsg
== WM_NCHITTEST
)
729 int xPos
= LOWORD(lParam
); // horizontal position of cursor
730 int yPos
= HIWORD(lParam
); // vertical position of cursor
732 ScreenToClient(&xPos
, &yPos
);
734 // Make sure you can drag by the top of the groupbox, but let
735 // other (enclosed) controls get mouse events also
737 return (long)HTCLIENT
;
740 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);