1 /////////////////////////////////////////////////////////////////////////////
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
18 #include "wx/bitmap.h"
20 #include "wx/radiobox.h"
23 #include "wx/os2/private.h"
25 #if !USE_SHARED_LIBRARY
26 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
29 // ---------------------------------------------------------------------------
31 // ---------------------------------------------------------------------------
33 // wnd proc for radio buttons
34 MRESULT
wxRadioBtnWndProc(HWND hWnd
,
39 // ---------------------------------------------------------------------------
41 // ---------------------------------------------------------------------------
43 // the pointer to standard radio button wnd proc
44 static s_wndprocRadioBtn
= NULL
;
46 // ===========================================================================
48 // ===========================================================================
50 // ---------------------------------------------------------------------------
52 // ---------------------------------------------------------------------------
54 int wxRadioBox::GetNumVer() const
56 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
62 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
66 int wxRadioBox::GetNumHor() const
68 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
70 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
78 bool wxRadioBox::OS2Command(WXUINT cmd
, WXWORD id
)
82 if ( cmd == BN_CLICKED )
84 int selectedButton = -1;
86 for ( int i = 0; i < m_noItems; i++ )
88 if ( id == wxGetWindowId(m_radioButtons[i]) )
96 wxASSERT_MSG( selectedButton != -1, wxT("click from alien button?") );
98 if ( selectedButton != m_selectedButton )
100 m_selectedButton = selectedButton;
102 SendNotificationEvent();
104 //else: don't generate events when the selection doesn't change
114 #if WXWIN_COMPATIBILITY
115 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
116 int x
, int y
, int width
, int height
,
117 int n
, char **choices
,
118 int majorDim
, long style
, const char *name
)
120 wxString
*choices2
= new wxString
[n
];
121 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
122 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
123 wxDefaultValidator
, name
);
131 wxRadioBox::wxRadioBox()
133 m_selectedButton
= -1;
136 m_radioButtons
= NULL
;
139 m_radioHeight
= NULL
;
142 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
143 const wxPoint
& pos
, const wxSize
& size
,
144 int n
, const wxString choices
[],
145 int majorDim
, long style
,
146 const wxValidator
& val
, const wxString
& name
)
148 m_selectedButton
= -1;
154 parent
->AddChild(this);
155 m_backgroundColour
= parent
->GetBackgroundColour();
156 m_foregroundColour
= parent
->GetForegroundColour();
158 m_windowStyle
= (long&)style
;
166 m_windowId
= NewControlId();
173 m_majorDim
= majorDim
;
174 m_noRowsOrCols
= majorDim
;
176 long msStyle
= 0; // TODO: GROUP_FLAGS;
179 WXDWORD exStyle
= Determine3DEffects(0, &want3D
);
181 HWND hwndParent
= (HWND
)parent
->GetHWND();
183 m_hWnd = (WXHWND)::CreateWindowEx
199 Ctl3dSubclassCtl((HWND)m_hWnd);
202 #endif // wxUSE_CTL3D
204 SetFont(parent
->GetFont());
208 // Some radio boxes test consecutive id.
209 (void)NewControlId();
210 m_radioButtons
= new WXHWND
[n
];
211 m_radioWidth
= new int[n
];
212 m_radioHeight
= new int[n
];
214 for (i
= 0; i
< n
; i
++)
218 m_radioWidth[i] = m_radioHeight[i] = -1;
220 if ( i == 0 && style == 0 )
221 groupStyle = WS_GROUP;
222 long newId = NewControlId();
223 long msStyle = groupStyle | RADIO_FLAGS;
225 HWND hwndBtn = CreateWindowEx(exStyle, RADIO_CLASS,
229 (HMENU)newId, wxGetInstance(),
232 m_radioButtons[i] = (WXHWND)hwndBtn;
234 SubclassRadioButton((WXHWND)hwndBtn);
236 wxFont& font = GetFont();
239 SendMessage(hwndBtn, WM_SETFONT,
240 (WPARAM)font.GetResourceHandle(), 0L);
243 m_subControls.Append((wxObject *)(WXDWORD)(WXWORD)newId);
247 // Create a dummy radio control to end the group.
248 // (void)CreateWindowEx(0, RADIO_CLASS, wxT(""), WS_GROUP | RADIO_FLAGS,
249 // 0, 0, 0, 0, hwndParent,
250 // (HMENU)NewControlId(), wxGetInstance(), NULL);
254 SetSize(x
, y
, width
, height
);
259 wxRadioBox::~wxRadioBox()
261 m_isBeingDeleted
= TRUE
;
268 for (i = 0; i < m_noItems; i++)
269 ::DestroyWindow((HWND)m_radioButtons[i]);
270 delete[] m_radioButtons;
275 delete[] m_radioWidth
;
277 delete[] m_radioHeight
;
281 wxString
wxRadioBox::GetLabel(int item
) const
283 wxCHECK_MSG( item
>= 0 && item
< m_noItems
, wxT(""), wxT("invalid radiobox index") );
285 return wxGetWindowText(m_radioButtons
[item
]);
288 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
290 wxCHECK_RET( item
>= 0 && item
< m_noItems
, wxT("invalid radiobox index") );
292 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
293 // TODO: SetWindowText((HWND)m_radioButtons[item], label.c_str());
296 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
299 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
300 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
302 wxFAIL_MSG(wxT("not implemented"));
305 int wxRadioBox::FindString(const wxString
& s
) const
307 for (int i
= 0; i
< m_noItems
; i
++)
309 if ( s
== wxGetWindowText(m_radioButtons
[i
]) )
316 void wxRadioBox::SetSelection(int N
)
318 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), wxT("invalid radiobox index") );
320 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
323 if (m_selectedButton >= 0 && m_selectedButton < m_noItems)
324 ::SendMessage((HWND) m_radioButtons[m_selectedButton], BM_SETCHECK, 0, 0L);
326 ::SendMessage((HWND)m_radioButtons[N], BM_SETCHECK, 1, 0L);
327 ::SetFocus((HWND)m_radioButtons[N]);
329 m_selectedButton
= N
;
332 // Get single selection, for single choice list items
333 int wxRadioBox::GetSelection() const
335 return m_selectedButton
;
338 // Find string for position
339 wxString
wxRadioBox::GetString(int N
) const
341 return wxGetWindowText(m_radioButtons
[N
]);
344 // Restored old code.
345 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
347 int currentX
, currentY
;
348 GetPosition(¤tX
, ¤tY
);
349 int widthOld
, heightOld
;
350 GetSize(&widthOld
, &heightOld
);
355 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
357 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
364 int current_width
, cyf
;
367 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
369 // Attempt to have a look coherent with other platforms: We compute the
370 // biggest toggle dim, then we align all items according this value.
375 for (i
= 0 ; i
< m_noItems
; i
++)
379 if (m_radioWidth
[i
]<0)
381 // It's a labelled toggle
382 buf
= wxGetWindowText(m_radioButtons
[i
]);
383 GetTextExtent(buf
, ¤t_width
, &cyf
);
384 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
385 eachHeight
= (int)((3*cyf
)/2);
389 eachWidth
= m_radioWidth
[i
];
390 eachHeight
= m_radioHeight
[i
];
393 if (maxWidth
<eachWidth
)
394 maxWidth
= eachWidth
;
395 if (maxHeight
<eachHeight
)
396 maxHeight
= eachHeight
;
404 int nbHor
= GetNumHor(),
407 // this formula works, but I don't know why.
408 // Please, be sure what you do if you modify it!!
409 if (m_radioWidth
[0]<0)
410 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
412 totHeight
= nbVer
* (maxHeight
+cy1
/2);
413 totWidth
= nbHor
* (maxWidth
+cx1
);
415 int extraHeight
= cy1
;
417 // only change our width/height if asked for
420 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
421 width
= totWidth
+ cx1
;
428 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
429 height
= totHeight
+ extraHeight
;
434 // TODO: MoveWindow(GetHwnd(), x_offset, y_offset, width, height, TRUE);
440 int startX
= x_offset
;
441 int startY
= y_offset
;
443 for ( i
= 0 ; i
< m_noItems
; i
++)
445 // Bidimensional radio adjustment
446 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
448 if (m_windowStyle
& wxRA_VERTICAL
)
451 x_offset
+= maxWidth
+ cx1
;
456 y_offset
+= maxHeight
;
457 if (m_radioWidth
[0]>0)
463 if (m_radioWidth
[i
]<0)
465 // It's a labeled item
466 buf
= wxGetWindowText(m_radioButtons
[i
]);
467 GetTextExtent(buf
, ¤t_width
, &cyf
);
469 // How do we find out radio button bitmap size!!
470 // By adjusting them carefully, manually :-)
471 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
472 eachHeight
= (int)((3*cyf
)/2);
476 eachWidth
= m_radioWidth
[i
];
477 eachHeight
= m_radioHeight
[i
];
482 MoveWindow((HWND)m_radioButtons[i], x_offset, y_offset,
483 eachWidth, eachHeight,
486 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
488 y_offset
+= maxHeight
;
489 if (m_radioWidth
[0]>0)
493 x_offset
+= maxWidth
+ cx1
;
497 void wxRadioBox::GetSize(int *width
, int *height
) const
500 rect
.xLeft
= -1; rect
.xRight
= -1; rect
.yTop
= -1; rect
.yBottom
= -1;
503 wxFindMaxSize(m_hWnd
, &rect
);
506 for (i
= 0; i
< m_noItems
; i
++)
507 wxFindMaxSize(m_radioButtons
[i
], &rect
);
509 *width
= rect
.xRight
- rect
.xLeft
;
510 *height
= rect
.yBottom
- rect
.yTop
;
513 void wxRadioBox::GetPosition(int *x
, int *y
) const
515 wxWindow
*parent
= GetParent();
517 rect
.xLeft
= -1; rect
.xRight
= -1; rect
.yTop
= -1; rect
.yBottom
= -1;
520 for (i
= 0; i
< m_noItems
; i
++)
521 wxFindMaxSize(m_radioButtons
[i
], &rect
);
524 wxFindMaxSize(m_hWnd
, &rect
);
526 // Since we now have the absolute screen coords,
527 // if there's a parent we must subtract its top left corner
529 point
.x
= rect
.xLeft
;
535 ::ScreenToClient((HWND) parent->GetHWND(), &point);
538 // We may be faking the client origin.
539 // So a window that's really at (0, 30) may appear
540 // (to wxWin apps) to be at (0, 0).
543 wxPoint
pt(GetParent()->GetClientAreaOrigin());
552 void wxRadioBox::SetFocus()
558 if (m_selectedButton == -1)
559 ::SetFocus((HWND) m_radioButtons[0]);
561 ::SetFocus((HWND) m_radioButtons[m_selectedButton]);
566 bool wxRadioBox::Show(bool show
)
568 if ( !wxControl::Show(show
) )
571 int nCmdShow
= 0; // TODO: show ? SW_SHOW : SW_HIDE;
572 for ( int i
= 0; i
< m_noItems
; i
++ )
574 // TODO: ::ShowWindow((HWND)m_radioButtons[i], nCmdShow);
580 // Enable a specific button
581 void wxRadioBox::Enable(int item
, bool enable
)
583 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
584 wxT("invalid item in wxRadioBox::Enable()") );
586 // TODO: ::EnableWindow((HWND) m_radioButtons[item], enable);
589 // Enable all controls
590 bool wxRadioBox::Enable(bool enable
)
592 if ( !wxControl::Enable(enable
) )
597 for (int i = 0; i < m_noItems; i++)
598 ::EnableWindow((HWND) m_radioButtons[i], enable);
603 // Show a specific button
604 void wxRadioBox::Show(int item
, bool show
)
606 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
607 wxT("invalid item in wxRadioBox::Show()") );
609 // TODO: ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE);
612 WXHBRUSH
wxRadioBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
613 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
617 if (GetParent()->GetTransparentBackground())
618 SetBkMode((HDC) pDC, TRANSPARENT);
620 SetBkMode((HDC) pDC, OPAQUE);
622 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
623 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
625 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
627 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
630 // For single selection items only
631 wxString
wxRadioBox::GetStringSelection() const
634 int sel
= GetSelection();
636 result
= GetString(sel
);
641 bool wxRadioBox::SetStringSelection(const wxString
& s
)
643 int sel
= FindString (s
);
653 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
656 for (i
= 0; i
< Number(); i
++)
658 if (GetRadioButtons()[i
] == hWnd
)
665 void wxRadioBox::Command (wxCommandEvent
& event
)
667 SetSelection (event
.m_commandInt
);
668 ProcessCommand (event
);
671 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
673 HWND hwndBtn
= (HWND
)hWndBtn
;
677 if ( !s_wndprocRadioBtn )
678 s_wndprocRadioBtn = (WXFARPROC)::GetWindowLong(hwndBtn, GWL_WNDPROC);
680 // No GWL_USERDATA in Win16, so omit this subclassing.
681 ::SetWindowLong(hwndBtn, GWL_WNDPROC, (long)wxRadioBtnWndProc);
682 ::SetWindowLong(hwndBtn, GWL_USERDATA, (long)this);
686 void wxRadioBox::SendNotificationEvent()
688 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
689 event
.SetInt( m_selectedButton
);
690 event
.SetString( GetString(m_selectedButton
) );
691 event
.SetEventObject( this );
692 ProcessCommand(event
);
695 // ---------------------------------------------------------------------------
696 // window proc for radio buttons
697 // ---------------------------------------------------------------------------
699 MRESULT
wxRadioBtnWndProc(HWND hwnd
,
704 bool processed
= TRUE
;
705 // if ( msg != WM_KEYDOWN )
706 // processed = FALSE;
710 wxRadioBox
*radiobox
= NULL
; // TODO: (wxRadioBox *)::GetWindowLong(hwnd, GWL_USERDATA);
712 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
714 int sel
= radiobox
->GetSelection();
725 sel -= radiobox->GetNumVer();
733 sel += radiobox->GetNumVer();
738 wxNavigationKeyEvent event;
739 event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100));
740 event.SetWindowChange(FALSE);
741 event.SetEventObject(radiobox);
743 if ( radiobox->GetEventHandler()->ProcessEvent(event) )
754 if ( sel
>= 0 && sel
< radiobox
->Number() )
756 radiobox
->SetSelection(sel
);
758 // emulate the button click
759 radiobox
->SendNotificationEvent();
767 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, msg, wParam, lParam);