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
,
147 # if defined(__VISAGECPP__)
148 const wxValidator
* val
, const wxString
& name
)
150 const wxValidator
& val
, const wxString
& name
)
154 m_selectedButton
= -1;
161 parent
->AddChild(this);
162 m_backgroundColour
= parent
->GetBackgroundColour();
163 m_foregroundColour
= parent
->GetForegroundColour();
165 m_windowStyle
= (long&)style
;
173 m_windowId
= NewControlId();
180 m_majorDim
= majorDim
;
181 m_noRowsOrCols
= majorDim
;
183 long msStyle
= 0; // TODO: GROUP_FLAGS;
186 WXDWORD exStyle
= Determine3DEffects(0, &want3D
);
188 HWND hwndParent
= (HWND
)parent
->GetHWND();
190 m_hWnd = (WXHWND)::CreateWindowEx
206 Ctl3dSubclassCtl((HWND)m_hWnd);
209 #endif // wxUSE_CTL3D
211 SetFont(parent
->GetFont());
215 // Some radio boxes test consecutive id.
216 (void)NewControlId();
217 m_radioButtons
= new WXHWND
[n
];
218 m_radioWidth
= new int[n
];
219 m_radioHeight
= new int[n
];
221 for (i
= 0; i
< n
; i
++)
225 m_radioWidth[i] = m_radioHeight[i] = -1;
227 if ( i == 0 && style == 0 )
228 groupStyle = WS_GROUP;
229 long newId = NewControlId();
230 long msStyle = groupStyle | RADIO_FLAGS;
232 HWND hwndBtn = CreateWindowEx(exStyle, RADIO_CLASS,
236 (HMENU)newId, wxGetInstance(),
239 m_radioButtons[i] = (WXHWND)hwndBtn;
241 SubclassRadioButton((WXHWND)hwndBtn);
243 wxFont& font = GetFont();
246 SendMessage(hwndBtn, WM_SETFONT,
247 (WPARAM)font.GetResourceHandle(), 0L);
250 m_subControls.Append((wxObject *)(WXDWORD)(WXWORD)newId);
254 // Create a dummy radio control to end the group.
255 // (void)CreateWindowEx(0, RADIO_CLASS, wxT(""), WS_GROUP | RADIO_FLAGS,
256 // 0, 0, 0, 0, hwndParent,
257 // (HMENU)NewControlId(), wxGetInstance(), NULL);
261 SetSize(x
, y
, width
, height
);
266 wxRadioBox::~wxRadioBox()
268 m_isBeingDeleted
= TRUE
;
275 for (i = 0; i < m_noItems; i++)
276 ::DestroyWindow((HWND)m_radioButtons[i]);
277 delete[] m_radioButtons;
282 delete[] m_radioWidth
;
284 delete[] m_radioHeight
;
288 wxString
wxRadioBox::GetLabel(int item
) const
290 wxCHECK_MSG( item
>= 0 && item
< m_noItems
, wxT(""), wxT("invalid radiobox index") );
292 return wxGetWindowText(m_radioButtons
[item
]);
295 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
297 wxCHECK_RET( item
>= 0 && item
< m_noItems
, wxT("invalid radiobox index") );
299 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
300 // TODO: SetWindowText((HWND)m_radioButtons[item], label.c_str());
303 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
306 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
307 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
309 wxFAIL_MSG(wxT("not implemented"));
312 int wxRadioBox::FindString(const wxString
& s
) const
314 for (int i
= 0; i
< m_noItems
; i
++)
316 if ( s
== wxGetWindowText(m_radioButtons
[i
]) )
323 void wxRadioBox::SetSelection(int N
)
325 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), wxT("invalid radiobox index") );
327 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
330 if (m_selectedButton >= 0 && m_selectedButton < m_noItems)
331 ::SendMessage((HWND) m_radioButtons[m_selectedButton], BM_SETCHECK, 0, 0L);
333 ::SendMessage((HWND)m_radioButtons[N], BM_SETCHECK, 1, 0L);
334 ::SetFocus((HWND)m_radioButtons[N]);
336 m_selectedButton
= N
;
339 // Get single selection, for single choice list items
340 int wxRadioBox::GetSelection() const
342 return m_selectedButton
;
345 // Find string for position
346 wxString
wxRadioBox::GetString(int N
) const
348 return wxGetWindowText(m_radioButtons
[N
]);
351 // Restored old code.
352 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
354 int currentX
, currentY
;
355 GetPosition(¤tX
, ¤tY
);
356 int widthOld
, heightOld
;
357 GetSize(&widthOld
, &heightOld
);
362 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
364 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
371 int current_width
, cyf
;
374 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
376 // Attempt to have a look coherent with other platforms: We compute the
377 // biggest toggle dim, then we align all items according this value.
382 for (i
= 0 ; i
< m_noItems
; i
++)
386 if (m_radioWidth
[i
]<0)
388 // It's a labelled toggle
389 buf
= wxGetWindowText(m_radioButtons
[i
]);
390 GetTextExtent(buf
, ¤t_width
, &cyf
);
391 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
392 eachHeight
= (int)((3*cyf
)/2);
396 eachWidth
= m_radioWidth
[i
];
397 eachHeight
= m_radioHeight
[i
];
400 if (maxWidth
<eachWidth
)
401 maxWidth
= eachWidth
;
402 if (maxHeight
<eachHeight
)
403 maxHeight
= eachHeight
;
411 int nbHor
= GetNumHor(),
414 // this formula works, but I don't know why.
415 // Please, be sure what you do if you modify it!!
416 if (m_radioWidth
[0]<0)
417 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
419 totHeight
= nbVer
* (maxHeight
+cy1
/2);
420 totWidth
= nbHor
* (maxWidth
+cx1
);
422 int extraHeight
= cy1
;
424 // only change our width/height if asked for
427 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
428 width
= totWidth
+ cx1
;
435 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
436 height
= totHeight
+ extraHeight
;
441 // TODO: MoveWindow(GetHwnd(), x_offset, y_offset, width, height, TRUE);
447 int startX
= x_offset
;
448 int startY
= y_offset
;
450 for ( i
= 0 ; i
< m_noItems
; i
++)
452 // Bidimensional radio adjustment
453 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
455 if (m_windowStyle
& wxRA_VERTICAL
)
458 x_offset
+= maxWidth
+ cx1
;
463 y_offset
+= maxHeight
;
464 if (m_radioWidth
[0]>0)
470 if (m_radioWidth
[i
]<0)
472 // It's a labeled item
473 buf
= wxGetWindowText(m_radioButtons
[i
]);
474 GetTextExtent(buf
, ¤t_width
, &cyf
);
476 // How do we find out radio button bitmap size!!
477 // By adjusting them carefully, manually :-)
478 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
479 eachHeight
= (int)((3*cyf
)/2);
483 eachWidth
= m_radioWidth
[i
];
484 eachHeight
= m_radioHeight
[i
];
489 MoveWindow((HWND)m_radioButtons[i], x_offset, y_offset,
490 eachWidth, eachHeight,
493 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
495 y_offset
+= maxHeight
;
496 if (m_radioWidth
[0]>0)
500 x_offset
+= maxWidth
+ cx1
;
504 void wxRadioBox::GetSize(int *width
, int *height
) const
507 rect
.xLeft
= -1; rect
.xRight
= -1; rect
.yTop
= -1; rect
.yBottom
= -1;
510 wxFindMaxSize(m_hWnd
, &rect
);
513 for (i
= 0; i
< m_noItems
; i
++)
514 wxFindMaxSize(m_radioButtons
[i
], &rect
);
516 *width
= rect
.xRight
- rect
.xLeft
;
517 *height
= rect
.yBottom
- rect
.yTop
;
520 void wxRadioBox::GetPosition(int *x
, int *y
) const
522 wxWindow
*parent
= GetParent();
524 rect
.xLeft
= -1; rect
.xRight
= -1; rect
.yTop
= -1; rect
.yBottom
= -1;
527 for (i
= 0; i
< m_noItems
; i
++)
528 wxFindMaxSize(m_radioButtons
[i
], &rect
);
531 wxFindMaxSize(m_hWnd
, &rect
);
533 // Since we now have the absolute screen coords,
534 // if there's a parent we must subtract its top left corner
536 point
.x
= rect
.xLeft
;
542 ::ScreenToClient((HWND) parent->GetHWND(), &point);
545 // We may be faking the client origin.
546 // So a window that's really at (0, 30) may appear
547 // (to wxWin apps) to be at (0, 0).
550 wxPoint
pt(GetParent()->GetClientAreaOrigin());
559 void wxRadioBox::SetFocus()
565 if (m_selectedButton == -1)
566 ::SetFocus((HWND) m_radioButtons[0]);
568 ::SetFocus((HWND) m_radioButtons[m_selectedButton]);
573 bool wxRadioBox::Show(bool show
)
575 if ( !wxControl::Show(show
) )
578 int nCmdShow
= 0; // TODO: show ? SW_SHOW : SW_HIDE;
579 for ( int i
= 0; i
< m_noItems
; i
++ )
581 // TODO: ::ShowWindow((HWND)m_radioButtons[i], nCmdShow);
587 // Enable a specific button
588 void wxRadioBox::Enable(int item
, bool enable
)
590 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
591 wxT("invalid item in wxRadioBox::Enable()") );
593 // TODO: ::EnableWindow((HWND) m_radioButtons[item], enable);
596 // Enable all controls
597 bool wxRadioBox::Enable(bool enable
)
599 if ( !wxControl::Enable(enable
) )
604 for (int i = 0; i < m_noItems; i++)
605 ::EnableWindow((HWND) m_radioButtons[i], enable);
610 // Show a specific button
611 void wxRadioBox::Show(int item
, bool show
)
613 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
614 wxT("invalid item in wxRadioBox::Show()") );
616 // TODO: ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE);
619 WXHBRUSH
wxRadioBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
620 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
624 if (GetParent()->GetTransparentBackground())
625 SetBkMode((HDC) pDC, TRANSPARENT);
627 SetBkMode((HDC) pDC, OPAQUE);
629 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
630 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
632 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
634 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
637 // For single selection items only
638 wxString
wxRadioBox::GetStringSelection() const
641 int sel
= GetSelection();
643 result
= GetString(sel
);
648 bool wxRadioBox::SetStringSelection(const wxString
& s
)
650 int sel
= FindString (s
);
660 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
663 for (i
= 0; i
< Number(); i
++)
665 if (GetRadioButtons()[i
] == hWnd
)
672 void wxRadioBox::Command (wxCommandEvent
& event
)
674 SetSelection (event
.m_commandInt
);
675 ProcessCommand (event
);
678 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
680 HWND hwndBtn
= (HWND
)hWndBtn
;
684 if ( !s_wndprocRadioBtn )
685 s_wndprocRadioBtn = (WXFARPROC)::GetWindowLong(hwndBtn, GWL_WNDPROC);
687 // No GWL_USERDATA in Win16, so omit this subclassing.
688 ::SetWindowLong(hwndBtn, GWL_WNDPROC, (long)wxRadioBtnWndProc);
689 ::SetWindowLong(hwndBtn, GWL_USERDATA, (long)this);
693 void wxRadioBox::SendNotificationEvent()
695 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
696 event
.SetInt( m_selectedButton
);
697 event
.SetString( GetString(m_selectedButton
) );
698 event
.SetEventObject( this );
699 ProcessCommand(event
);
702 // ---------------------------------------------------------------------------
703 // window proc for radio buttons
704 // ---------------------------------------------------------------------------
706 MRESULT
wxRadioBtnWndProc(HWND hwnd
,
711 bool processed
= TRUE
;
712 // if ( msg != WM_KEYDOWN )
713 // processed = FALSE;
717 wxRadioBox
*radiobox
= NULL
; // TODO: (wxRadioBox *)::GetWindowLong(hwnd, GWL_USERDATA);
719 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
721 int sel
= radiobox
->GetSelection();
732 sel -= radiobox->GetNumVer();
740 sel += radiobox->GetNumVer();
745 wxNavigationKeyEvent event;
746 event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100));
747 event.SetWindowChange(FALSE);
748 event.SetEventObject(radiobox);
750 if ( radiobox->GetEventHandler()->ProcessEvent(event) )
761 if ( sel
>= 0 && sel
< radiobox
->Number() )
763 radiobox
->SetSelection(sel
);
765 // emulate the button click
766 radiobox
->SendNotificationEvent();
774 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, msg, wParam, lParam);