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.
128 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
129 msStyle
|= WS_BORDER
;
132 HWND the_handle
= (HWND
) parent
->GetHWND() ;
134 m_hWnd
= (WXHWND
)::CreateWindowEx
150 Ctl3dSubclassCtl((HWND
)m_hWnd
);
155 SetFont(* parent
->GetFont());
157 SubclassWin((WXHWND
)m_hWnd
);
159 // Some radio boxes test consecutive id.
160 (void)NewControlId() ;
161 m_radioButtons
= new WXHWND
[n
];
162 m_radioWidth
= new int[n
] ;
163 m_radioHeight
= new int[n
] ;
165 for (i
= 0; i
< n
; i
++)
167 m_radioWidth
[i
] = m_radioHeight
[i
] = -1 ;
169 if (i
== 0 && style
==0)
170 groupStyle
= WS_GROUP
;
171 long newId
= NewControlId();
172 long msStyle
= groupStyle
| RADIO_FLAGS
;
174 m_radioButtons
[i
] = (WXHWND
) CreateWindowEx(exStyle
, RADIO_CLASS
, choices
[i
],
176 the_handle
, (HMENU
)newId
, wxGetInstance(), NULL
);
180 Ctl3dSubclassCtl((HWND
) m_hWnd
);
186 SendMessage((HWND
)m_radioButtons
[i
],WM_SETFONT
,
187 (WPARAM
)GetFont()->GetResourceHandle(),0L);
189 m_subControls
.Append((wxObject
*)newId
);
192 // Create a dummy radio control to end the group.
193 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
|RADIO_FLAGS
, 0,0,0,0, the_handle
, (HMENU
)NewControlId(), wxGetInstance(), NULL
);
197 SetSize(x
, y
, width
, height
);
203 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
204 const wxPoint
& pos
, const wxSize
& size
,
205 int n
, const wxBitmap
*choices
[],
206 int majorDim
, long style
,
207 const wxValidator
& val
, const wxString
& name
)
209 m_selectedButton
= -1;
216 parent
->AddChild(this);
217 m_backgroundColour
= parent
->GetBackgroundColour() ;
218 m_foregroundColour
= parent
->GetForegroundColour() ;
220 m_windowStyle
= (long&)style
;
228 m_windowId
= NewControlId();
233 m_noRowsOrCols
= majorDim
;
236 m_majorDim
= majorDim
;
239 long msStyle
= GROUP_FLAGS
;
242 WXDWORD exStyle
= Determine3DEffects(0, &want3D
) ;
243 // Even with extended styles, need to combine with WS_BORDER
244 // for them to look right.
245 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
246 msStyle
|= WS_BORDER
;
248 m_hWnd
= (WXHWND
) CreateWindowEx((DWORD
) exStyle
, GROUP_CLASS
, (title
== "" ? NULL
: (const char *)title
),
251 (HWND
) parent
->GetHWND(), (HMENU
) m_windowId
, wxGetInstance(), NULL
) ;
253 the_handle
= (HWND
) parent
->GetHWND();
258 Ctl3dSubclassCtl((HWND
) m_hWnd
);
263 SetFont(* parent
->GetFont());
265 // Subclass again for purposes of dialog editing mode
266 SubclassWin((WXHWND
)m_hWnd
);
268 (void)NewControlId() ;
269 m_radioButtons
= new WXHWND
[n
];
270 m_radioWidth
= new int[n
] ;
271 m_radioHeight
= new int[n
] ;
274 for (i
= 0; i
< n
; i
++)
277 if (i
== 0 && style
==0)
278 groupStyle
= WS_GROUP
;
279 long newId
= NewControlId();
280 m_radioWidth
[i
] = ((wxBitmap
*)choices
[i
])->GetWidth();
281 m_radioHeight
[i
] = ((wxBitmap
*)choices
[i
])->GetHeight();
283 sprintf(tmp
,"Toggle%d",i
) ;
284 long msStyle
= groupStyle
| RADIO_FLAGS
;
285 m_radioButtons
[i
] = (WXHWND
) CreateWindowEx(exStyle
, RADIO_CLASS
, tmp
,
287 the_handle
, (HMENU
)newId
, wxhInstance
, NULL
);
291 Ctl3dSubclassCtl((HWND
) m_hWnd
);
295 m_subControls
.Append((wxObject
*)newId
);
297 // Create a dummy radio control to end the group.
298 (void)CreateWindowEx(0, RADIO_CLASS
, "", WS_GROUP
|RADIO_FLAGS
, 0,0,0,0, the_handle
, (HMENU
)NewControlId(), wxGetInstance(), NULL
);
302 SetSize(x
, y
, width
, height
);
308 wxRadioBox::~wxRadioBox(void)
310 m_isBeingDeleted
= TRUE
;
315 for (i
= 0; i
< m_noItems
; i
++)
316 DestroyWindow((HWND
) m_radioButtons
[i
]);
317 delete[] m_radioButtons
;
320 delete[] m_radioWidth
;
322 delete[] m_radioHeight
;
324 ::DestroyWindow((HWND
) m_hWnd
) ;
329 wxString
wxRadioBox::GetLabel(int item
) const
331 GetWindowText((HWND
)m_radioButtons
[item
], wxBuffer
, 300);
332 return wxString(wxBuffer
);
335 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
337 m_radioWidth
[item
] = m_radioHeight
[item
] = -1 ;
338 SetWindowText((HWND
)m_radioButtons
[item
], (const char *)label
);
341 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
344 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN ;
345 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN ;
349 int wxRadioBox::FindString(const wxString
& s
) const
352 for (i
= 0; i
< m_noItems
; i
++)
354 GetWindowText((HWND
) m_radioButtons
[i
], wxBuffer
, 1000);
361 void wxRadioBox::SetSelection(int N
)
363 if ((N
< 0) || (N
>= m_noItems
))
366 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
367 if (m_selectedButton
>= 0 && m_selectedButton
< m_noItems
)
368 SendMessage((HWND
) m_radioButtons
[m_selectedButton
], BM_SETCHECK
, 0, 0L);
370 SendMessage((HWND
) m_radioButtons
[N
], BM_SETCHECK
, 1, 0L);
371 m_selectedButton
= N
;
374 // Get single selection, for single choice list items
375 int wxRadioBox::GetSelection(void) const
377 return m_selectedButton
;
380 // Find string for position
381 wxString
wxRadioBox::GetString(int N
) const
383 GetWindowText((HWND
) m_radioButtons
[N
], wxBuffer
, 1000);
384 return wxString(wxBuffer
);
387 void wxRadioBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
389 int currentX
, currentY
;
390 GetPosition(¤tX
, ¤tY
);
394 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
396 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
399 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
405 int current_width
, cyf
;
408 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, GetFont());
409 // Attempt to have a look coherent with other platforms:
410 // We compute the biggest toggle dim, then we align all
411 // items according this value.
416 for (i
= 0 ; i
< m_noItems
; i
++)
420 if (m_radioWidth
[i
]<0)
422 // It's a labelled toggle
423 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
424 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
, GetFont());
425 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
426 eachHeight
= (int)((3*cyf
)/2);
430 eachWidth
= m_radioWidth
[i
] ;
431 eachHeight
= m_radioHeight
[i
] ;
433 if (maxWidth
<eachWidth
) maxWidth
= eachWidth
;
434 if (maxHeight
<eachHeight
) maxHeight
= eachHeight
;
444 if (m_windowStyle
& wxRA_VERTICAL
)
447 nbHor
= (m_noItems
+m_majorDim
-1)/m_majorDim
;
452 nbVer
= (m_noItems
+m_majorDim
-1)/m_majorDim
;
455 // this formula works, but I don't know why.
456 // Please, be sure what you do if you modify it!!
457 if (m_radioWidth
[0]<0)
458 totHeight
= (nbVer
* maxHeight
) + cy1
/2 ;
460 totHeight
= nbVer
* (maxHeight
+cy1
/2) ;
461 totWidth
= nbHor
* (maxWidth
+cx1
) ;
464 // Requires a bigger group box in plain Windows
465 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+(3*cy1
)/2,TRUE
) ;
467 MoveWindow((HWND
) m_hWnd
,x_offset
,y_offset
,totWidth
+cx1
,totHeight
+cy1
,TRUE
) ;
474 y_offset
+= (int)(cy1
/2); // Fudge factor since buttons overlapped label
475 // JACS 2/12/93. CTL3D draws group label quite high.
477 int startX
= x_offset
;
478 int startY
= y_offset
;
480 for ( i
= 0 ; i
< m_noItems
; i
++)
482 // Bidimensional radio adjustment
483 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
485 if (m_windowStyle
& wxRA_VERTICAL
)
488 x_offset
+= maxWidth
+ cx1
;
493 y_offset
+= maxHeight
;
494 if (m_radioWidth
[0]>0)
500 if (m_radioWidth
[i
]<0)
502 // It's a labeled item
503 GetWindowText((HWND
) m_radioButtons
[i
], buf
, 300);
504 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
,GetFont());
506 // How do we find out radio button bitmap size!!
507 // By adjusting them carefully, manually :-)
508 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
509 eachHeight
= (int)((3*cyf
)/2);
513 eachWidth
= m_radioWidth
[i
] ;
514 eachHeight
= m_radioHeight
[i
] ;
517 MoveWindow((HWND
) m_radioButtons
[i
],x_offset
,y_offset
,eachWidth
,eachHeight
,TRUE
);
518 if (m_windowStyle
& wxRA_VERTICAL
)
520 y_offset
+= maxHeight
;
521 if (m_radioWidth
[0]>0)
525 x_offset
+= maxWidth
+ cx1
;
529 void wxRadioBox::GetSize(int *width
, int *height
) const
532 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
535 wxFindMaxSize(m_hWnd
, &rect
);
538 for (i
= 0; i
< m_noItems
; i
++)
539 wxFindMaxSize(m_radioButtons
[i
], &rect
);
541 *width
= rect
.right
- rect
.left
;
542 *height
= rect
.bottom
- rect
.top
;
545 void wxRadioBox::GetPosition(int *x
, int *y
) const
547 wxWindow
*parent
= GetParent();
549 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
552 for (i
= 0; i
< m_noItems
; i
++)
553 wxFindMaxSize(m_radioButtons
[i
], &rect
);
556 wxFindMaxSize(m_hWnd
, &rect
);
558 // Since we now have the absolute screen coords,
559 // if there's a parent we must subtract its top left corner
565 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
567 // We may be faking the client origin.
568 // So a window that's really at (0, 30) may appear
569 // (to wxWin apps) to be at (0, 0).
572 wxPoint
pt(GetParent()->GetClientAreaOrigin());
581 wxString
wxRadioBox::GetLabel(void) const
585 GetWindowText((HWND
) m_hWnd
, wxBuffer
, 300);
586 return wxString(wxBuffer
);
588 else return wxString("");
591 void wxRadioBox::SetLabel(const wxString
& label
)
594 SetWindowText((HWND
) m_hWnd
, label
);
597 void wxRadioBox::SetFocus(void)
601 if (m_selectedButton
== -1)
602 ::SetFocus((HWND
) m_radioButtons
[0]);
604 ::SetFocus((HWND
) m_radioButtons
[m_selectedButton
]);
609 bool wxRadioBox::Show(bool show
)
617 ShowWindow((HWND
) m_hWnd
, cshow
);
619 for (i
= 0; i
< m_noItems
; i
++)
620 ShowWindow((HWND
) m_radioButtons
[i
], cshow
);
624 // Enable a specific button
625 void wxRadioBox::Enable(int item
, bool enable
)
628 wxWindow::Enable(enable
) ;
629 else if (item
< m_noItems
)
630 ::EnableWindow((HWND
) m_radioButtons
[item
], enable
);
633 // Enable all controls
634 void wxRadioBox::Enable(bool enable
)
636 wxControl::Enable(enable
);
639 for (i
= 0; i
< m_noItems
; i
++)
640 ::EnableWindow((HWND
) m_radioButtons
[i
], enable
);
643 // Show a specific button
644 void wxRadioBox::Show(int item
, bool show
)
647 wxRadioBox::Show(show
) ;
648 else if (item
< m_noItems
)
655 ShowWindow((HWND
) m_radioButtons
[item
], cshow
);
659 WXHBRUSH
wxRadioBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
660 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
665 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
666 return (WXHBRUSH
) hbrush
;
670 if (GetParent()->GetTransparentBackground())
671 SetBkMode((HDC
) pDC
, TRANSPARENT
);
673 SetBkMode((HDC
) pDC
, OPAQUE
);
675 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
676 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
678 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
680 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
681 // has a zero usage count.
682 // backgroundBrush->RealizeResource();
683 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
686 // For single selection items only
687 wxString
wxRadioBox::GetStringSelection (void) const
689 int sel
= GetSelection ();
691 return this->GetString (sel
);
696 bool wxRadioBox::SetStringSelection (const wxString
& s
)
698 int sel
= FindString (s
);
708 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
711 for (i
= 0; i
< Number(); i
++)
712 if (GetRadioButtons()[i
] == hWnd
)
717 void wxRadioBox::Command (wxCommandEvent
& event
)
719 SetSelection (event
.m_commandInt
);
720 ProcessCommand (event
);
723 long wxRadioBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
725 if (nMsg
== WM_NCHITTEST
)
727 int xPos
= LOWORD(lParam
); // horizontal position of cursor
728 int yPos
= HIWORD(lParam
); // vertical position of cursor
730 ScreenToClient(&xPos
, &yPos
);
732 // Make sure you can drag by the top of the groupbox, but let
733 // other (enclosed) controls get mouse events also
735 return (long)HTCLIENT
;
738 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);