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/wxchar.h"
19 #include "wx/string.h"
20 #include "wx/bitmap.h"
22 #include "wx/radiobox.h"
25 #include "wx/os2/private.h"
27 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 const wxValidator
& val
, const wxString
& name
)
150 m_selectedButton
= -1;
157 parent
->AddChild(this);
158 m_backgroundColour
= parent
->GetBackgroundColour();
159 m_foregroundColour
= parent
->GetForegroundColour();
161 m_windowStyle
= (long&)style
;
169 m_windowId
= NewControlId();
176 m_majorDim
= majorDim
;
177 m_noRowsOrCols
= majorDim
;
179 long msStyle
= 0; // TODO: GROUP_FLAGS;
182 WXDWORD exStyle
= Determine3DEffects(0, &want3D
);
184 HWND hwndParent
= (HWND
)parent
->GetHWND();
186 m_hWnd = (WXHWND)::CreateWindowEx
202 Ctl3dSubclassCtl((HWND)m_hWnd);
205 #endif // wxUSE_CTL3D
207 SetFont(parent
->GetFont());
211 // Some radio boxes test consecutive id.
212 (void)NewControlId();
213 m_radioButtons
= new WXHWND
[n
];
214 m_radioWidth
= new int[n
];
215 m_radioHeight
= new int[n
];
217 for (i
= 0; i
< n
; i
++)
221 m_radioWidth[i] = m_radioHeight[i] = -1;
223 if ( i == 0 && style == 0 )
224 groupStyle = WS_GROUP;
225 long newId = NewControlId();
226 long msStyle = groupStyle | RADIO_FLAGS;
228 HWND hwndBtn = CreateWindowEx(exStyle, RADIO_CLASS,
232 (HMENU)newId, wxGetInstance(),
235 m_radioButtons[i] = (WXHWND)hwndBtn;
237 SubclassRadioButton((WXHWND)hwndBtn);
239 wxFont& font = GetFont();
242 SendMessage(hwndBtn, WM_SETFONT,
243 (WPARAM)font.GetResourceHandle(), 0L);
246 m_subControls.Append((wxObject *)(WXDWORD)(WXWORD)newId);
250 // Create a dummy radio control to end the group.
251 // (void)CreateWindowEx(0, RADIO_CLASS, wxT(""), WS_GROUP | RADIO_FLAGS,
252 // 0, 0, 0, 0, hwndParent,
253 // (HMENU)NewControlId(), wxGetInstance(), NULL);
257 SetSize(x
, y
, width
, height
);
262 wxRadioBox::~wxRadioBox()
264 m_isBeingDeleted
= TRUE
;
271 for (i = 0; i < m_noItems; i++)
272 ::DestroyWindow((HWND)m_radioButtons[i]);
273 delete[] m_radioButtons;
278 delete[] m_radioWidth
;
280 delete[] m_radioHeight
;
284 wxString
wxRadioBox::GetLabel(int item
) const
286 wxCHECK_MSG( item
>= 0 && item
< m_noItems
, wxT(""), wxT("invalid radiobox index") );
288 return wxGetWindowText(m_radioButtons
[item
]);
291 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
293 wxCHECK_RET( item
>= 0 && item
< m_noItems
, wxT("invalid radiobox index") );
295 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
296 // TODO: SetWindowText((HWND)m_radioButtons[item], label.c_str());
299 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
302 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
303 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
305 wxFAIL_MSG(wxT("not implemented"));
308 int wxRadioBox::FindString(const wxString
& s
) const
310 for (int i
= 0; i
< m_noItems
; i
++)
312 if ( s
== wxGetWindowText(m_radioButtons
[i
]) )
319 void wxRadioBox::SetSelection(int N
)
321 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), wxT("invalid radiobox index") );
323 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
326 if (m_selectedButton >= 0 && m_selectedButton < m_noItems)
327 ::SendMessage((HWND) m_radioButtons[m_selectedButton], BM_SETCHECK, 0, 0L);
329 ::SendMessage((HWND)m_radioButtons[N], BM_SETCHECK, 1, 0L);
330 ::SetFocus((HWND)m_radioButtons[N]);
332 m_selectedButton
= N
;
335 // Get single selection, for single choice list items
336 int wxRadioBox::GetSelection() const
338 return m_selectedButton
;
341 // Find string for position
342 wxString
wxRadioBox::GetString(int N
) const
344 return wxGetWindowText(m_radioButtons
[N
]);
347 // Restored old code.
348 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
350 int currentX
, currentY
;
351 GetPosition(¤tX
, ¤tY
);
352 int widthOld
, heightOld
;
353 GetSize(&widthOld
, &heightOld
);
358 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
360 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
367 int current_width
, cyf
;
370 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
372 // Attempt to have a look coherent with other platforms: We compute the
373 // biggest toggle dim, then we align all items according this value.
378 for (i
= 0 ; i
< m_noItems
; i
++)
382 if (m_radioWidth
[i
]<0)
384 // It's a labelled toggle
385 buf
= wxGetWindowText(m_radioButtons
[i
]);
386 GetTextExtent(buf
, ¤t_width
, &cyf
);
387 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
388 eachHeight
= (int)((3*cyf
)/2);
392 eachWidth
= m_radioWidth
[i
];
393 eachHeight
= m_radioHeight
[i
];
396 if (maxWidth
<eachWidth
)
397 maxWidth
= eachWidth
;
398 if (maxHeight
<eachHeight
)
399 maxHeight
= eachHeight
;
407 int nbHor
= GetNumHor(),
410 // this formula works, but I don't know why.
411 // Please, be sure what you do if you modify it!!
412 if (m_radioWidth
[0]<0)
413 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
415 totHeight
= nbVer
* (maxHeight
+cy1
/2);
416 totWidth
= nbHor
* (maxWidth
+cx1
);
418 int extraHeight
= cy1
;
420 // only change our width/height if asked for
423 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
424 width
= totWidth
+ cx1
;
431 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
432 height
= totHeight
+ extraHeight
;
437 // TODO: MoveWindow(GetHwnd(), x_offset, y_offset, width, height, TRUE);
443 int startX
= x_offset
;
444 int startY
= y_offset
;
446 for ( i
= 0 ; i
< m_noItems
; i
++)
448 // Bidimensional radio adjustment
449 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
451 if (m_windowStyle
& wxRA_VERTICAL
)
454 x_offset
+= maxWidth
+ cx1
;
459 y_offset
+= maxHeight
;
460 if (m_radioWidth
[0]>0)
466 if (m_radioWidth
[i
]<0)
468 // It's a labeled item
469 buf
= wxGetWindowText(m_radioButtons
[i
]);
470 GetTextExtent(buf
, ¤t_width
, &cyf
);
472 // How do we find out radio button bitmap size!!
473 // By adjusting them carefully, manually :-)
474 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
475 eachHeight
= (int)((3*cyf
)/2);
479 eachWidth
= m_radioWidth
[i
];
480 eachHeight
= m_radioHeight
[i
];
485 MoveWindow((HWND)m_radioButtons[i], x_offset, y_offset,
486 eachWidth, eachHeight,
489 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
491 y_offset
+= maxHeight
;
492 if (m_radioWidth
[0]>0)
496 x_offset
+= maxWidth
+ cx1
;
500 void wxRadioBox::GetSize(int *width
, int *height
) const
503 rect
.xLeft
= -1; rect
.xRight
= -1; rect
.yTop
= -1; rect
.yBottom
= -1;
506 wxFindMaxSize(m_hWnd
, &rect
);
509 for (i
= 0; i
< m_noItems
; i
++)
510 wxFindMaxSize(m_radioButtons
[i
], &rect
);
512 *width
= rect
.xRight
- rect
.xLeft
;
513 *height
= rect
.yBottom
- rect
.yTop
;
516 void wxRadioBox::GetPosition(int *x
, int *y
) const
518 wxWindow
*parent
= GetParent();
520 rect
.xLeft
= -1; rect
.xRight
= -1; rect
.yTop
= -1; rect
.yBottom
= -1;
523 for (i
= 0; i
< m_noItems
; i
++)
524 wxFindMaxSize(m_radioButtons
[i
], &rect
);
527 wxFindMaxSize(m_hWnd
, &rect
);
529 // Since we now have the absolute screen coords,
530 // if there's a parent we must subtract its top left corner
532 point
.x
= rect
.xLeft
;
538 ::ScreenToClient((HWND) parent->GetHWND(), &point);
541 // We may be faking the client origin.
542 // So a window that's really at (0, 30) may appear
543 // (to wxWin apps) to be at (0, 0).
546 wxPoint
pt(GetParent()->GetClientAreaOrigin());
555 void wxRadioBox::SetFocus()
561 if (m_selectedButton == -1)
562 ::SetFocus((HWND) m_radioButtons[0]);
564 ::SetFocus((HWND) m_radioButtons[m_selectedButton]);
569 bool wxRadioBox::Show(bool show
)
571 if ( !wxControl::Show(show
) )
574 int nCmdShow
= 0; // TODO: show ? SW_SHOW : SW_HIDE;
575 for ( int i
= 0; i
< m_noItems
; i
++ )
577 // TODO: ::ShowWindow((HWND)m_radioButtons[i], nCmdShow);
583 // Enable a specific button
584 void wxRadioBox::Enable(int item
, bool enable
)
586 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
587 wxT("invalid item in wxRadioBox::Enable()") );
589 // TODO: ::EnableWindow((HWND) m_radioButtons[item], enable);
592 // Enable all controls
593 bool wxRadioBox::Enable(bool enable
)
595 if ( !wxControl::Enable(enable
) )
600 for (int i = 0; i < m_noItems; i++)
601 ::EnableWindow((HWND) m_radioButtons[i], enable);
606 // Show a specific button
607 void wxRadioBox::Show(int item
, bool show
)
609 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
610 wxT("invalid item in wxRadioBox::Show()") );
612 // TODO: ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE);
615 WXHBRUSH
wxRadioBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
616 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
620 if (GetParent()->GetTransparentBackground())
621 SetBkMode((HDC) pDC, TRANSPARENT);
623 SetBkMode((HDC) pDC, OPAQUE);
625 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
626 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
628 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
630 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
633 // For single selection items only
634 wxString
wxRadioBox::GetStringSelection() const
637 int sel
= GetSelection();
639 result
= GetString(sel
);
644 bool wxRadioBox::SetStringSelection(const wxString
& s
)
646 int sel
= FindString (s
);
656 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
659 for (i
= 0; i
< Number(); i
++)
661 if (GetRadioButtons()[i
] == hWnd
)
668 void wxRadioBox::Command (wxCommandEvent
& event
)
670 SetSelection (event
.m_commandInt
);
671 ProcessCommand (event
);
674 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
676 HWND hwndBtn
= (HWND
)hWndBtn
;
680 if ( !s_wndprocRadioBtn )
681 s_wndprocRadioBtn = (WXFARPROC)::GetWindowLong(hwndBtn, GWL_WNDPROC);
683 // No GWL_USERDATA in Win16, so omit this subclassing.
684 ::SetWindowLong(hwndBtn, GWL_WNDPROC, (long)wxRadioBtnWndProc);
685 ::SetWindowLong(hwndBtn, GWL_USERDATA, (long)this);
689 void wxRadioBox::SendNotificationEvent()
691 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
692 event
.SetInt( m_selectedButton
);
693 event
.SetString( GetString(m_selectedButton
) );
694 event
.SetEventObject( this );
695 ProcessCommand(event
);
698 // ---------------------------------------------------------------------------
699 // window proc for radio buttons
700 // ---------------------------------------------------------------------------
702 MRESULT
wxRadioBtnWndProc(HWND hwnd
,
707 bool processed
= TRUE
;
708 // if ( msg != WM_KEYDOWN )
709 // processed = FALSE;
713 wxRadioBox
*radiobox
= NULL
; // TODO: (wxRadioBox *)::GetWindowLong(hwnd, GWL_USERDATA);
715 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
717 int sel
= radiobox
->GetSelection();
728 sel -= radiobox->GetNumVer();
736 sel += radiobox->GetNumVer();
741 wxNavigationKeyEvent event;
742 event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100));
743 event.SetWindowChange(FALSE);
744 event.SetEventObject(radiobox);
746 if ( radiobox->GetEventHandler()->ProcessEvent(event) )
757 if ( sel
>= 0 && sel
< radiobox
->Number() )
759 radiobox
->SetSelection(sel
);
761 // emulate the button click
762 radiobox
->SendNotificationEvent();
770 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, msg, wParam, lParam);