/////////////////////////////////////////////////////////////////////////////
-// Name: radiobox.cpp
-// Purpose: wxRadioBox
+// Name: msw/radiobox.cpp
+// Purpose: wxRadioBox implementation
// Author: Julian Smart
// Modified by:
// Created: 04/01/98
#include "wx/bitmap.h"
#include "wx/brush.h"
#include "wx/radiobox.h"
+ #include "wx/settings.h"
#include "wx/log.h"
#endif
#include "wx/msw/private.h"
#if wxUSE_TOOLTIPS
-
-#ifndef __GNUWIN32__
- #include <commctrl.h>
-#endif
-
+ #ifndef __GNUWIN32_OLD__
+ #include <commctrl.h>
+ #endif
#include "wx/tooltip.h"
#endif // wxUSE_TOOLTIPS
- IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
+IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
-// VZ: the new behaviour is to create the radio buttons as children of the
-// radiobox instead of creating them as children of the radiobox' parent.
+// there are two possible ways to create the radio buttons: either as children
+// of the radiobox or as siblings of it - allow playing with both variants for
+// now, eventually we will choose the best one for our purposes
//
-// This seems more logical, more consistent with what other frameworks do
-// and allows tooltips to work with radioboxes, so there should be no
-// reason to revert to the backward compatible behaviour - but I still
-// leave this possibility just in case.
-
-// For some reason, the background colour is set wrongly in WIN16 mode
-// if we use the new method.
-
-#ifdef __WIN16__
+// two main problems are the keyboard navigation inside the radiobox (arrows
+// should switch between buttons, not pass focus to the next control) and the
+// tooltips - a tooltip is associated with the radiobox itself, not the
+// children...
+//
+// the problems with setting this to 1:
+// a) Alt-<mnemonic of radiobox> isn't handled properly by IsDialogMessage()
+// because it sets focus to the next control accepting it which is not a
+// radio button but a radiobox sibling in this case - the only solution to
+// this would be to handle Alt-<mnemonic> ourselves
+// b) the problems with setting radiobox colours under Win98/2K were reported
+// but I couldn't reproduce it so I have no idea about what causes it
+//
+// the problems with setting this to 0:
+// a) the tooltips are not shown for the radiobox - possible solution: make
+// TTM_WINDOWFROMPOS handling code in msw/tooltip.cpp work (easier said than
+// done because I don't know why it doesn't work)
#define RADIOBTN_PARENT_IS_RADIOBOX 0
-#else
-#define RADIOBTN_PARENT_IS_RADIOBOX 1
-#endif
// ---------------------------------------------------------------------------
// private functions
// wxRadioBox
// ---------------------------------------------------------------------------
+// returns the number of rows
int wxRadioBox::GetNumVer() const
{
if ( m_windowStyle & wxRA_SPECIFY_ROWS )
}
}
+// returns the number of columns
int wxRadioBox::GetNumHor() const
{
if ( m_windowStyle & wxRA_SPECIFY_ROWS )
{
if ( cmd == BN_CLICKED )
{
+ if (id == GetId())
+ return TRUE;
+
int selectedButton = -1;
for ( int i = 0; i < m_noItems; i++ )
}
}
- wxASSERT_MSG( selectedButton != -1, wxT("click from alien button?") );
+ if ( selectedButton == -1 )
+ {
+ // just ignore it - due to a hack with WM_NCHITTEST handling in our
+ // wnd proc, we can receive dummy click messages when we click near
+ // the radiobox edge (this is ugly but Julian wouldn't let me get
+ // rid of this...)
+ return FALSE;
+ }
if ( selectedButton != m_selectedButton )
{
delete choices2;
}
-#endif
+#endif // WXWIN_COMPATIBILITY
// Radio box item
wxRadioBox::wxRadioBox()
m_selectedButton = -1;
m_noItems = 0;
- m_majorDim = majorDim == 0 ? n : majorDim;
+ m_majorDim = majorDim == 0 ? n : majorDim;
m_noRowsOrCols = majorDim;
// common initialization
return FALSE;
// create the static box
- if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX, pos, size, title, 0) )
+ if ( !MSWCreateControl(wxT("BUTTON"), BS_GROUPBOX | WS_GROUP,
+ pos, size, title, 0) )
return FALSE;
// and now create the buttons
{
m_radioWidth[i] =
m_radioHeight[i] = -1;
- long styleBtn = BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE;
+ long styleBtn = BS_AUTORADIOBUTTON | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
if ( i == 0 && style == 0 )
styleBtn |= WS_GROUP;
if ( !hwndBtn )
{
- wxLogLastError("CreateWindow(radio btn)");
+ wxLogLastError(wxT("CreateWindow(radio btn)"));
return FALSE;
}
return wxGetWindowText(m_radioButtons[N]);
}
+// ----------------------------------------------------------------------------
+// size calculations
+// ----------------------------------------------------------------------------
+
+wxSize wxRadioBox::GetMaxButtonSize() const
+{
+ // calculate the max button size
+ int widthMax = 0,
+ heightMax = 0;
+ for ( int i = 0 ; i < m_noItems; i++ )
+ {
+ int width, height;
+ if ( m_radioWidth[i] < 0 )
+ {
+ GetTextExtent(wxGetWindowText(m_radioButtons[i]), &width, &height);
+
+ // adjust the size to take into account the radio box itself
+ // FIXME this is totally bogus!
+ width += RADIO_SIZE;
+ height *= 3;
+ height /= 2;
+ }
+ else
+ {
+ width = m_radioWidth[i];
+ height = m_radioHeight[i];
+ }
+
+ if ( widthMax < width )
+ widthMax = width;
+ if ( heightMax < height )
+ heightMax = height;
+ }
+
+ return wxSize(widthMax, heightMax);
+}
+
+wxSize wxRadioBox::GetTotalButtonSize(const wxSize& sizeBtn) const
+{
+ // the radiobox should be big enough for its buttons
+ int cx1, cy1;
+ wxGetCharSize(m_hWnd, &cx1, &cy1, &GetFont());
+
+ int extraHeight = cy1;
+
+#if defined(CTL3D) && !CTL3D
+ // Requires a bigger group box in plain Windows
+ extraHeight *= 3;
+ extraHeight /= 2;
+#endif
+
+ int height = GetNumVer() * sizeBtn.y + cy1/2 + extraHeight;
+ int width = GetNumHor() * (sizeBtn.x + cx1) + cx1;
+
+ // and also wide enough for its label
+ int widthLabel;
+ GetTextExtent(GetTitle(), &widthLabel, NULL);
+ widthLabel += RADIO_SIZE; // FIXME this is bogus too
+ if ( widthLabel > width )
+ width = widthLabel;
+
+ return wxSize(width, height);
+}
+
+wxSize wxRadioBox::DoGetBestSize() const
+{
+ return GetTotalButtonSize(GetMaxButtonSize());
+}
+
// Restored old code.
void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
{
int x_offset = xx;
#endif
- int current_width, cyf;
-
- int cx1,cy1;
+ int cx1, cy1;
wxGetCharSize(m_hWnd, &cx1, &cy1, & GetFont());
// Attempt to have a look coherent with other platforms: We compute the
// biggest toggle dim, then we align all items according this value.
- int maxWidth = -1;
- int maxHeight = -1;
+ wxSize maxSize = GetMaxButtonSize();
+ int maxWidth = maxSize.x,
+ maxHeight = maxSize.y;
- int i;
- for (i = 0 ; i < m_noItems; i++)
+ wxSize totSize = GetTotalButtonSize(maxSize);
+ int totWidth = totSize.x,
+ totHeight = totSize.y;
+
+ // only change our width/height if asked for
+ if ( width == -1 )
{
- int eachWidth;
- int eachHeight;
- if (m_radioWidth[i]<0)
- {
- // It's a labelled toggle
- GetTextExtent(wxGetWindowText(m_radioButtons[i]),
- ¤t_width, &cyf);
- eachWidth = (int)(current_width + RADIO_SIZE);
- eachHeight = (int)((3*cyf)/2);
- }
+ if ( sizeFlags & wxSIZE_AUTO_WIDTH )
+ width = totWidth;
else
- {
- eachWidth = m_radioWidth[i];
- eachHeight = m_radioHeight[i];
- }
-
- if (maxWidth<eachWidth)
- maxWidth = eachWidth;
- if (maxHeight<eachHeight)
- maxHeight = eachHeight;
+ width = widthOld;
}
- if (m_hWnd)
+ if ( height == -1 )
{
- int totWidth;
- int totHeight;
-
- int nbHor = GetNumHor(),
- nbVer = GetNumVer();
-
- // this formula works, but I don't know why.
- // Please, be sure what you do if you modify it!!
- if (m_radioWidth[0]<0)
- totHeight = (nbVer * maxHeight) + cy1/2;
+ if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
+ height = totHeight;
else
- totHeight = nbVer * (maxHeight+cy1/2);
- totWidth = nbHor * (maxWidth+cx1);
-
- int extraHeight = cy1;
-
-#if defined(CTL3D) && !CTL3D
- // Requires a bigger group box in plain Windows
- extraHeight *= 3;
- extraHeight /= 2;
-#endif
+ height = heightOld;
+ }
- // only change our width/height if asked for
- if ( width == -1 )
- {
- if ( sizeFlags & wxSIZE_AUTO_WIDTH )
- width = totWidth + cx1;
- else
- width = widthOld;
- }
+ ::MoveWindow(GetHwnd(), xx, yy, width, height, TRUE);
- if ( height == -1 )
- {
- if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
- height = totHeight + extraHeight;
- else
- height = heightOld;
- }
+ // Now position all the buttons: the current button will be put at
+ // wxPoint(x_offset, y_offset) and the new row/column will start at
+ // startX/startY. The size of all buttons will be the same wxSize(maxWidth,
+ // maxHeight) except for the buttons in the last column which should extend
+ // to the right border of radiobox and thus can be wider than this.
- ::MoveWindow(GetHwnd(), xx, yy, width, height, TRUE);
+ // Also, remember that wxRA_SPECIFY_COLS means that we arrange buttons in
+ // left to right order and m_majorDim is the number of columns while
+ // wxRA_SPECIFY_ROWS means that the buttons are arranged top to bottom and
+ // m_majorDim is the number of rows.
- x_offset += cx1;
- y_offset += cy1;
- }
+ x_offset += cx1;
+ y_offset += cy1;
#if defined(CTL3D) && (!CTL3D)
y_offset += (int)(cy1/2); // Fudge factor since buttons overlapped label
// JACS 2/12/93. CTL3D draws group label quite high.
#endif
+
int startX = x_offset;
int startY = y_offset;
- for ( i = 0 ; i < m_noItems; i++)
+ for ( int i = 0; i < m_noItems; i++ )
{
- // Bidimensional radio adjustment
- if (i&&((i%m_majorDim)==0)) // Why is this omitted for i = 0?
+ // the last button in the row may be wider than the other ones as the
+ // radiobox may be wider than the sum of the button widths (as it
+ // happens, for example, when the radiobox label is very long)
+ bool isLastInTheRow;
+ if ( m_windowStyle & wxRA_SPECIFY_COLS )
+ {
+ // item is the last in its row if it is a multiple of the number of
+ // columns or if it is just the last item
+ int n = i + 1;
+ isLastInTheRow = ((n % m_majorDim) == 0) || (n == m_noItems);
+ }
+ else // wxRA_SPECIFY_ROWS
+ {
+ // item is the last in the row if it is in the last columns
+ isLastInTheRow = i >= (m_noItems/m_majorDim)*m_majorDim;
+ }
+
+ // is this the start of new row/column?
+ if ( i && (i % m_majorDim == 0) )
{
- if (m_windowStyle & wxRA_VERTICAL)
+ if ( m_windowStyle & wxRA_SPECIFY_ROWS )
{
+ // start of new column
y_offset = startY;
x_offset += maxWidth + cx1;
}
- else
+ else // start of new row
{
x_offset = startX;
y_offset += maxHeight;
y_offset += cy1/2;
}
}
- int eachWidth;
- int eachHeight;
- if (m_radioWidth[i]<0)
+
+ int widthBtn;
+ if ( isLastInTheRow )
{
- // It's a labeled item
- GetTextExtent(wxGetWindowText(m_radioButtons[i]),
- ¤t_width, &cyf);
-
- // How do we find out radio button bitmap size!!
- // By adjusting them carefully, manually :-)
- eachWidth = (int)(current_width + RADIO_SIZE);
- eachHeight = (int)((3*cyf)/2);
+ // make the button go to the end of radio box
+ widthBtn = startX + width - x_offset - 2*cx1;
+ if ( widthBtn < maxWidth )
+ widthBtn = maxWidth;
}
else
{
- eachWidth = m_radioWidth[i];
- eachHeight = m_radioHeight[i];
+ // normal button, always of the same size
+ widthBtn = maxWidth;
}
// VZ: make all buttons of the same, maximal size - like this they
// shown (otherwise they are not when the mouse pointer is in the
// radiobox part not belonging to any radiobutton)
::MoveWindow((HWND)m_radioButtons[i],
- x_offset, y_offset, maxWidth, maxHeight,
+ x_offset, y_offset, widthBtn, maxHeight,
TRUE);
- if (m_windowStyle & wxRA_SPECIFY_ROWS)
+ // where do we put the next button?
+ if ( m_windowStyle & wxRA_SPECIFY_ROWS )
{
+ // below this one
y_offset += maxHeight;
if (m_radioWidth[0]>0)
y_offset += cy1/2;
}
else
- x_offset += maxWidth + cx1;
+ {
+ // to the right of this one
+ x_offset += widthBtn + cx1;
+ }
}
}
ProcessCommand (event);
}
+// NB: if this code is changed, wxGetWindowForHWND() which relies on having the
+// radiobox pointer in GWL_USERDATA for radio buttons must be updated too!
void wxRadioBox::SubclassRadioButton(WXHWND hWndBtn)
{
+ // No GWL_USERDATA in Win16, so omit this subclassing.
#ifdef __WIN32__
HWND hwndBtn = (HWND)hWndBtn;
if ( !s_wndprocRadioBtn )
s_wndprocRadioBtn = (WXFARPROC)::GetWindowLong(hwndBtn, GWL_WNDPROC);
- // No GWL_USERDATA in Win16, so omit this subclassing.
::SetWindowLong(hwndBtn, GWL_WNDPROC, (long)wxRadioBtnWndProc);
::SetWindowLong(hwndBtn, GWL_USERDATA, (long)this);
#endif // __WIN32__
WXHFONT hfont = wxFont(font).GetResourceHandle();
for ( int n = 0; n < m_noItems; n++ )
{
- ::SendMessage((HWND)m_radioButtons[n], WM_SETFONT, (WPARAM)hfont, 0L);
+ HWND hwndBtn = (HWND)m_radioButtons[n];
+ ::SendMessage(hwndBtn, WM_SETFONT, (WPARAM)hfont, 0L);
+
+ // otherwise the buttons are not redrawn correctly
+ ::InvalidateRect(hwndBtn, NULL, FALSE /* don't erase bg */);
}
return TRUE;
}
+// ----------------------------------------------------------------------------
+// our window proc
+// ----------------------------------------------------------------------------
+
long wxRadioBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
{
- // This is required for the radiobox to be sensitive to mouse input,
- // e.g. for Dialog Editor.
- if (nMsg == WM_NCHITTEST)
+ switch ( nMsg )
{
- int xPos = LOWORD(lParam); // horizontal position of cursor
- int yPos = HIWORD(lParam); // vertical position of cursor
+#ifdef __WIN32__
+ case WM_CTLCOLORSTATIC:
+ // set the colour of the radio buttons to be the same as ours
+ {
+ HDC hdc = (HDC)wParam;
+
+ const wxColour& colBack = GetBackgroundColour();
+ ::SetBkColor(hdc, wxColourToRGB(colBack));
+ ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
- ScreenToClient(&xPos, &yPos);
+ wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
- // Make sure you can drag by the top of the groupbox, but let
- // other (enclosed) controls get mouse events also
- if (yPos < 10)
- return (long)HTCLIENT;
+ return (WXHBRUSH)brush->GetResourceHandle();
+ }
+#endif // Win32
+
+ // This is required for the radiobox to be sensitive to mouse input,
+ // e.g. for Dialog Editor.
+ case WM_NCHITTEST:
+ {
+ int xPos = LOWORD(lParam); // horizontal position of cursor
+ int yPos = HIWORD(lParam); // vertical position of cursor
+
+ ScreenToClient(&xPos, &yPos);
+
+ // Make sure you can drag by the top of the groupbox, but let
+ // other (enclosed) controls get mouse events also
+ if (yPos < 10)
+ return (long)HTCLIENT;
+ }
+ break;
}
return wxControl::MSWWindowProc(nMsg, wParam, lParam);
}
+WXHBRUSH wxRadioBox::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
+ WXUINT message,
+ WXWPARAM wParam,
+ WXLPARAM lParam)
+{
+#if wxUSE_CTL3D
+ if ( m_useCtl3D )
+ {
+ HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
+ return (WXHBRUSH) hbrush;
+ }
+#endif // wxUSE_CTL3D
+
+ HDC hdc = (HDC)pDC;
+ if (GetParent()->GetTransparentBackground())
+ SetBkMode(hdc, TRANSPARENT);
+ else
+ SetBkMode(hdc, OPAQUE);
+
+ wxColour colBack = GetBackgroundColour();
+
+ if (!IsEnabled())
+ colBack = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
+
+ ::SetBkColor(hdc, wxColourToRGB(colBack));
+ ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
+
+ wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
+
+ return (WXHBRUSH)brush->GetResourceHandle();
+}
+
+
// ---------------------------------------------------------------------------
// window proc for radio buttons
// ---------------------------------------------------------------------------
#ifdef __WIN32__
LRESULT APIENTRY _EXPORT wxRadioBtnWndProc(HWND hwnd,
- UINT msg,
+ UINT message,
WPARAM wParam,
LPARAM lParam)
{
- bool processed = FALSE;
- if ( msg == WM_KEYDOWN
-#if wxUSE_TOOLTIPS
- || msg == WM_NOTIFY
-#endif // wxUSE_TOOLTIPS
- )
+ switch ( message )
{
- wxRadioBox *radiobox = (wxRadioBox *)::GetWindowLong(hwnd, GWL_USERDATA);
+ case WM_GETDLGCODE:
+ // we must tell IsDialogMessage()/our kbd processing code that we
+ // want to process arrows ourselves because neither of them is
+ // smart enough to handle arrows properly for us
+ {
+ long lDlgCode = ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd,
+ message, wParam, lParam);
- wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
+ return lDlgCode | DLGC_WANTARROWS;
+ }
-#if wxUSE_TOOLTIPS && !defined(__GNUWIN32__)
- if ( msg == WM_NOTIFY )
- {
- NMHDR* hdr = (NMHDR *)lParam;
- if ( (int)hdr->code == TTN_NEEDTEXT )
+#if wxUSE_TOOLTIPS
+ case WM_NOTIFY:
{
- wxToolTip *tt = radiobox->GetToolTip();
- if ( tt )
+ NMHDR* hdr = (NMHDR *)lParam;
+ if ( (int)hdr->code == TTN_NEEDTEXT )
{
- TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
- ttt->lpszText = (wxChar *)tt->GetTip().c_str();
+ wxRadioBox *radiobox = (wxRadioBox *)
+ ::GetWindowLong(hwnd, GWL_USERDATA);
+
+ wxCHECK_MSG( radiobox, 0,
+ wxT("radio button without radio box?") );
+
+ wxToolTip *tooltip = radiobox->GetToolTip();
+ if ( tooltip )
+ {
+ TOOLTIPTEXT *ttt = (TOOLTIPTEXT *)lParam;
+ ttt->lpszText = (wxChar *)tooltip->GetTip().c_str();
+ }
- processed = TRUE;
+ // processed
+ return 0;
}
}
- }
- else // msg == WM_KEYDOWN
+ break;
#endif // wxUSE_TOOLTIPS
- {
- processed = TRUE;
- int sel = radiobox->GetSelection();
-
- switch ( wParam )
+ case WM_KEYDOWN:
{
- case VK_UP:
- sel--;
- break;
+ wxRadioBox *radiobox = (wxRadioBox *)
+ ::GetWindowLong(hwnd, GWL_USERDATA);
+
+ wxCHECK_MSG( radiobox, 0, wxT("radio button without radio box?") );
- case VK_LEFT:
- sel -= radiobox->GetNumVer();
- break;
+ bool processed = TRUE;
- case VK_DOWN:
- sel++;
- break;
+ bool horz = (radiobox->GetWindowStyle() & wxRA_SPECIFY_COLS) != 0;
+ int num = radiobox->Number(),
+ rows = radiobox->GetNumVer(),
+ cols = radiobox->GetNumHor();
- case VK_RIGHT:
- sel += radiobox->GetNumVer();
- break;
+ int selOld = radiobox->GetSelection();
+ int selNew = selOld;
+
+ // wrapping will be handled below for the cases when we
+ // add/substract more than 1 but here otherwise as it's simpler
+ switch ( wParam )
+ {
+ case VK_UP:
+ if ( horz )
+ selNew -= cols;
+ else
+ {
+ if ( selNew )
+ selNew--;
+ else
+ selNew = num - 1;
+ }
+ break;
+
+ case VK_LEFT:
+ if ( horz )
+ {
+ if ( selNew )
+ selNew--;
+ else
+ selNew = num - 1;
+ }
+ else
+ selNew -= rows;
+ break;
+
+ case VK_DOWN:
+ if ( horz )
+ selNew += cols;
+ else
+ {
+ if ( ++selNew == num )
+ selNew = 0;
+ }
+ break;
+
+ case VK_RIGHT:
+ if ( horz )
+ {
+ if ( ++selNew == num )
+ selNew = 0;
+ }
+ else
+ selNew += rows;
+ break;
+
+ default:
+ processed = FALSE;
+ }
- case VK_TAB:
+ if ( processed )
+ {
+ // ensure that selNew is in range [0..num)
+ if ( selNew >= num )
{
- wxNavigationKeyEvent event;
- event.SetDirection(!(::GetKeyState(VK_SHIFT) & 0x100));
- event.SetWindowChange(FALSE);
- event.SetEventObject(radiobox);
+ selNew -= num;
- if ( radiobox->GetEventHandler()->ProcessEvent(event) )
- return 0;
+ int dim = horz ? cols : rows;
+ selNew += dim - 1;
+ selNew %= dim;
+ }
+ else if ( selNew < 0 )
+ {
+ selNew += num;
+
+ int dim = horz ? cols : rows;
+ if ( selNew % dim == 0 )
+ {
+ selNew -= dim - 1;
+ }
+ else
+ {
+ selNew++;
+ }
}
- // fall through
- default:
- processed = FALSE;
- }
+ if ( selNew != selOld )
+ {
+ radiobox->SetSelection(selNew);
- if ( processed )
- {
- if ( sel >= 0 && sel < radiobox->Number() )
- {
- radiobox->SetSelection(sel);
+ // emulate the button click
+ radiobox->SendNotificationEvent();
- // emulate the button click
- radiobox->SendNotificationEvent();
+ return 0;
+ }
}
}
- }
}
- if ( processed )
- return 0;
-
- return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, msg, wParam, lParam);
+ return ::CallWindowProc(CASTWNDPROC s_wndprocRadioBtn, hwnd, message, wParam, lParam);
}
#endif // __WIN32__