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 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
27 // ---------------------------------------------------------------------------
29 // ---------------------------------------------------------------------------
31 // wnd proc for radio buttons
32 MRESULT
wxRadioBtnWndProc(HWND hWnd
,
37 // ---------------------------------------------------------------------------
39 // ---------------------------------------------------------------------------
41 // the pointer to standard radio button wnd proc
42 static s_wndprocRadioBtn
= NULL
;
44 // ===========================================================================
46 // ===========================================================================
48 // ---------------------------------------------------------------------------
50 // ---------------------------------------------------------------------------
52 int wxRadioBox::GetNumVer() const
54 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
60 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
64 int wxRadioBox::GetNumHor() const
66 if ( m_windowStyle
& wxRA_SPECIFY_ROWS
)
68 return (m_noItems
+ m_majorDim
- 1)/m_majorDim
;
76 bool wxRadioBox::OS2Command(WXUINT cmd
, WXWORD id
)
80 if ( cmd == BN_CLICKED )
82 int selectedButton = -1;
84 for ( int i = 0; i < m_noItems; i++ )
86 if ( id == wxGetWindowId(m_radioButtons[i]) )
94 wxASSERT_MSG( selectedButton != -1, wxT("click from alien button?") );
96 if ( selectedButton != m_selectedButton )
98 m_selectedButton = selectedButton;
100 SendNotificationEvent();
102 //else: don't generate events when the selection doesn't change
112 #if WXWIN_COMPATIBILITY
113 wxRadioBox::wxRadioBox(wxWindow
*parent
, wxFunction func
, const char *title
,
114 int x
, int y
, int width
, int height
,
115 int n
, char **choices
,
116 int majorDim
, long style
, const char *name
)
118 wxString
*choices2
= new wxString
[n
];
119 for ( int i
= 0; i
< n
; i
++) choices2
[i
] = choices
[i
];
120 Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), n
, choices2
, majorDim
, style
,
121 wxDefaultValidator
, name
);
129 wxRadioBox::wxRadioBox()
131 m_selectedButton
= -1;
134 m_radioButtons
= NULL
;
137 m_radioHeight
= NULL
;
140 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
141 const wxPoint
& pos
, const wxSize
& size
,
142 int n
, const wxString choices
[],
143 int majorDim
, long style
,
145 const wxValidator
& val
, const wxString
& name
)
148 m_selectedButton
= -1;
155 parent
->AddChild(this);
156 m_backgroundColour
= parent
->GetBackgroundColour();
157 m_foregroundColour
= parent
->GetForegroundColour();
159 m_windowStyle
= (long&)style
;
167 m_windowId
= NewControlId();
174 m_majorDim
= majorDim
;
175 m_noRowsOrCols
= majorDim
;
177 long msStyle
= 0; // TODO: GROUP_FLAGS;
180 WXDWORD exStyle
= Determine3DEffects(0, &want3D
);
182 HWND hwndParent
= (HWND
)parent
->GetHWND();
184 m_hWnd = (WXHWND)::CreateWindowEx
200 Ctl3dSubclassCtl((HWND)m_hWnd);
203 #endif // wxUSE_CTL3D
205 SetFont(parent
->GetFont());
209 // Some radio boxes test consecutive id.
210 (void)NewControlId();
211 m_radioButtons
= new WXHWND
[n
];
212 m_radioWidth
= new int[n
];
213 m_radioHeight
= new int[n
];
215 for (i
= 0; i
< n
; i
++)
219 m_radioWidth[i] = m_radioHeight[i] = -1;
221 if ( i == 0 && style == 0 )
222 groupStyle = WS_GROUP;
223 long newId = NewControlId();
224 long msStyle = groupStyle | RADIO_FLAGS;
226 HWND hwndBtn = CreateWindowEx(exStyle, RADIO_CLASS,
230 (HMENU)newId, wxGetInstance(),
233 m_radioButtons[i] = (WXHWND)hwndBtn;
235 SubclassRadioButton((WXHWND)hwndBtn);
237 wxFont& font = GetFont();
240 SendMessage(hwndBtn, WM_SETFONT,
241 (WPARAM)font.GetResourceHandle(), 0L);
244 m_subControls.Append((wxObject *)(WXDWORD)(WXWORD)newId);
248 // Create a dummy radio control to end the group.
249 // (void)CreateWindowEx(0, RADIO_CLASS, wxT(""), WS_GROUP | RADIO_FLAGS,
250 // 0, 0, 0, 0, hwndParent,
251 // (HMENU)NewControlId(), wxGetInstance(), NULL);
255 SetSize(x
, y
, width
, height
);
260 wxRadioBox::~wxRadioBox()
262 m_isBeingDeleted
= TRUE
;
269 for (i = 0; i < m_noItems; i++)
270 ::DestroyWindow((HWND)m_radioButtons[i]);
271 delete[] m_radioButtons;
276 delete[] m_radioWidth
;
278 delete[] m_radioHeight
;
282 wxString
wxRadioBox::GetLabel(int item
) const
284 wxCHECK_MSG( item
>= 0 && item
< m_noItems
, wxT(""), wxT("invalid radiobox index") );
286 return wxGetWindowText(m_radioButtons
[item
]);
289 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
291 wxCHECK_RET( item
>= 0 && item
< m_noItems
, wxT("invalid radiobox index") );
293 m_radioWidth
[item
] = m_radioHeight
[item
] = -1;
294 // TODO: SetWindowText((HWND)m_radioButtons[item], label.c_str());
297 void wxRadioBox::SetLabel(int item
, wxBitmap
*bitmap
)
300 m_radioWidth[item] = bitmap->GetWidth() + FB_MARGIN;
301 m_radioHeight[item] = bitmap->GetHeight() + FB_MARGIN;
303 wxFAIL_MSG(wxT("not implemented"));
306 int wxRadioBox::FindString(const wxString
& s
) const
308 for (int i
= 0; i
< m_noItems
; i
++)
310 if ( s
== wxGetWindowText(m_radioButtons
[i
]) )
317 void wxRadioBox::SetSelection(int N
)
319 wxCHECK_RET( (N
>= 0) && (N
< m_noItems
), wxT("invalid radiobox index") );
321 // Following necessary for Win32s, because Win32s translate BM_SETCHECK
324 if (m_selectedButton >= 0 && m_selectedButton < m_noItems)
325 ::SendMessage((HWND) m_radioButtons[m_selectedButton], BM_SETCHECK, 0, 0L);
327 ::SendMessage((HWND)m_radioButtons[N], BM_SETCHECK, 1, 0L);
328 ::SetFocus((HWND)m_radioButtons[N]);
330 m_selectedButton
= N
;
333 // Get single selection, for single choice list items
334 int wxRadioBox::GetSelection() const
336 return m_selectedButton
;
339 // Find string for position
340 wxString
wxRadioBox::GetString(int N
) const
342 return wxGetWindowText(m_radioButtons
[N
]);
345 // Restored old code.
346 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
348 int currentX
, currentY
;
349 GetPosition(¤tX
, ¤tY
);
350 int widthOld
, heightOld
;
351 GetSize(&widthOld
, &heightOld
);
356 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
358 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
365 int current_width
, cyf
;
368 wxGetCharSize(m_hWnd
, &cx1
, &cy1
, & GetFont());
370 // Attempt to have a look coherent with other platforms: We compute the
371 // biggest toggle dim, then we align all items according this value.
376 for (i
= 0 ; i
< m_noItems
; i
++)
380 if (m_radioWidth
[i
]<0)
382 // It's a labelled toggle
383 buf
= wxGetWindowText(m_radioButtons
[i
]);
384 GetTextExtent(buf
, ¤t_width
, &cyf
);
385 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
386 eachHeight
= (int)((3*cyf
)/2);
390 eachWidth
= m_radioWidth
[i
];
391 eachHeight
= m_radioHeight
[i
];
394 if (maxWidth
<eachWidth
)
395 maxWidth
= eachWidth
;
396 if (maxHeight
<eachHeight
)
397 maxHeight
= eachHeight
;
405 int nbHor
= GetNumHor(),
408 // this formula works, but I don't know why.
409 // Please, be sure what you do if you modify it!!
410 if (m_radioWidth
[0]<0)
411 totHeight
= (nbVer
* maxHeight
) + cy1
/2;
413 totHeight
= nbVer
* (maxHeight
+cy1
/2);
414 totWidth
= nbHor
* (maxWidth
+cx1
);
416 int extraHeight
= cy1
;
418 // only change our width/height if asked for
421 if ( sizeFlags
& wxSIZE_AUTO_WIDTH
)
422 width
= totWidth
+ cx1
;
429 if ( sizeFlags
& wxSIZE_AUTO_HEIGHT
)
430 height
= totHeight
+ extraHeight
;
435 // TODO: MoveWindow(GetHwnd(), x_offset, y_offset, width, height, TRUE);
441 int startX
= x_offset
;
442 int startY
= y_offset
;
444 for ( i
= 0 ; i
< m_noItems
; i
++)
446 // Bidimensional radio adjustment
447 if (i
&&((i%m_majorDim
)==0)) // Why is this omitted for i = 0?
449 if (m_windowStyle
& wxRA_VERTICAL
)
452 x_offset
+= maxWidth
+ cx1
;
457 y_offset
+= maxHeight
;
458 if (m_radioWidth
[0]>0)
464 if (m_radioWidth
[i
]<0)
466 // It's a labeled item
467 buf
= wxGetWindowText(m_radioButtons
[i
]);
468 GetTextExtent(buf
, ¤t_width
, &cyf
);
470 // How do we find out radio button bitmap size!!
471 // By adjusting them carefully, manually :-)
472 eachWidth
= (int)(current_width
+ RADIO_SIZE
);
473 eachHeight
= (int)((3*cyf
)/2);
477 eachWidth
= m_radioWidth
[i
];
478 eachHeight
= m_radioHeight
[i
];
483 MoveWindow((HWND)m_radioButtons[i], x_offset, y_offset,
484 eachWidth, eachHeight,
487 if (m_windowStyle
& wxRA_SPECIFY_ROWS
)
489 y_offset
+= maxHeight
;
490 if (m_radioWidth
[0]>0)
494 x_offset
+= maxWidth
+ cx1
;
498 void wxRadioBox::GetSize(int *width
, int *height
) const
501 rect
.xLeft
= -1; rect
.xRight
= -1; rect
.yTop
= -1; rect
.yBottom
= -1;
504 wxFindMaxSize(m_hWnd
, &rect
);
507 for (i
= 0; i
< m_noItems
; i
++)
508 wxFindMaxSize(m_radioButtons
[i
], &rect
);
510 *width
= rect
.xRight
- rect
.xLeft
;
511 *height
= rect
.yBottom
- rect
.yTop
;
514 void wxRadioBox::GetPosition(int *x
, int *y
) const
516 wxWindow
*parent
= GetParent();
518 rect
.xLeft
= -1; rect
.xRight
= -1; rect
.yTop
= -1; rect
.yBottom
= -1;
521 for (i
= 0; i
< m_noItems
; i
++)
522 wxFindMaxSize(m_radioButtons
[i
], &rect
);
525 wxFindMaxSize(m_hWnd
, &rect
);
527 // Since we now have the absolute screen coords,
528 // if there's a parent we must subtract its top left corner
530 point
.x
= rect
.xLeft
;
536 ::ScreenToClient((HWND) parent->GetHWND(), &point);
539 // We may be faking the client origin.
540 // So a window that's really at (0, 30) may appear
541 // (to wxWin apps) to be at (0, 0).
544 wxPoint
pt(GetParent()->GetClientAreaOrigin());
553 void wxRadioBox::SetFocus()
559 if (m_selectedButton == -1)
560 ::SetFocus((HWND) m_radioButtons[0]);
562 ::SetFocus((HWND) m_radioButtons[m_selectedButton]);
567 bool wxRadioBox::Show(bool show
)
569 if ( !wxControl::Show(show
) )
572 int nCmdShow
= 0; // TODO: show ? SW_SHOW : SW_HIDE;
573 for ( int i
= 0; i
< m_noItems
; i
++ )
575 // TODO: ::ShowWindow((HWND)m_radioButtons[i], nCmdShow);
581 // Enable a specific button
582 void wxRadioBox::Enable(int item
, bool enable
)
584 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
585 wxT("invalid item in wxRadioBox::Enable()") );
587 // TODO: ::EnableWindow((HWND) m_radioButtons[item], enable);
590 // Enable all controls
591 bool wxRadioBox::Enable(bool enable
)
593 if ( !wxControl::Enable(enable
) )
598 for (int i = 0; i < m_noItems; i++)
599 ::EnableWindow((HWND) m_radioButtons[i], enable);
604 // Show a specific button
605 void wxRadioBox::Show(int item
, bool show
)
607 wxCHECK_RET( item
>= 0 && item
< m_noItems
,
608 wxT("invalid item in wxRadioBox::Show()") );
610 // TODO: ::ShowWindow((HWND)m_radioButtons[item], show ? SW_SHOW : SW_HIDE);
613 WXHBRUSH
wxRadioBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
614 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
618 if (GetParent()->GetTransparentBackground())
619 SetBkMode((HDC) pDC, TRANSPARENT);
621 SetBkMode((HDC) pDC, OPAQUE);
623 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
624 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
626 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
628 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
631 // For single selection items only
632 wxString
wxRadioBox::GetStringSelection() const
635 int sel
= GetSelection();
637 result
= GetString(sel
);
642 bool wxRadioBox::SetStringSelection(const wxString
& s
)
644 int sel
= FindString (s
);
654 bool wxRadioBox::ContainsHWND(WXHWND hWnd
) const
657 for (i
= 0; i
< Number(); i
++)
659 if (GetRadioButtons()[i
] == hWnd
)
666 void wxRadioBox::Command (wxCommandEvent
& event
)
668 SetSelection (event
.m_commandInt
);
669 ProcessCommand (event
);
672 void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn
)
674 HWND hwndBtn
= (HWND
)hWndBtn
;
678 if ( !s_wndprocRadioBtn )
679 s_wndprocRadioBtn = (WXFARPROC)::GetWindowLong(hwndBtn, GWL_WNDPROC);
681 // No GWL_USERDATA in Win16, so omit this subclassing.
682 ::SetWindowLong(hwndBtn, GWL_WNDPROC, (long)wxRadioBtnWndProc);
683 ::SetWindowLong(hwndBtn, GWL_USERDATA, (long)this);
687 void wxRadioBox::SendNotificationEvent()
689 wxCommandEvent
event(wxEVT_COMMAND_RADIOBOX_SELECTED
, m_windowId
);
690 event
.SetInt( m_selectedButton
);
691 event
.SetString( GetString(m_selectedButton
) );
692 event
.SetEventObject( this );
693 ProcessCommand(event
);
696 // ---------------------------------------------------------------------------
697 // window proc for radio buttons
698 // ---------------------------------------------------------------------------
700 MRESULT
wxRadioBtnWndProc(HWND hwnd
,
705 bool processed
= TRUE
;
706 // if ( msg != WM_KEYDOWN )
707 // processed = FALSE;
711 wxRadioBox
*radiobox
= NULL
; // TODO: (wxRadioBox *)::GetWindowLong(hwnd, GWL_USERDATA);
713 wxCHECK_MSG( radiobox
, 0, wxT("radio button without radio box?") );
715 int sel
= radiobox
->GetSelection();
726 sel -= radiobox->GetNumVer();
734 sel += radiobox->GetNumVer();
739 wxNavigationKeyEvent event;
740 event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100));
741 event.SetWindowChange(FALSE);
742 event.SetEventObject(radiobox);
744 if ( radiobox->GetEventHandler()->ProcessEvent(event) )
755 if ( sel
>= 0 && sel
< radiobox
->Number() )
757 radiobox
->SetSelection(sel
);
759 // emulate the button click
760 radiobox
->SendNotificationEvent();
768 return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, msg, wParam, lParam);