spinctrl.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 605 行 · 第 1/2 页
CPP
605 行
SetWindowStyle(style);
WXDWORD exStyle = 0;
WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ;
// calculate the sizes: the size given is the toal size for both controls
// and we need to fit them both in the given width (height is the same)
wxSize sizeText(size), sizeBtn(size);
sizeBtn.x = wxSpinButton::DoGetBestSize().x;
if ( sizeText.x <= 0 )
{
// DEFAULT_ITEM_WIDTH is the default width for the text control
sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
}
sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
if ( sizeText.x <= 0 )
{
wxLogDebug(_T("not enough space for wxSpinCtrl!"));
}
wxPoint posBtn(pos);
posBtn.x += sizeText.x + MARGIN_BETWEEN;
// we must create the text control before the spin button for the purpose
// of the dialog navigation: if there is a static text just before the spin
// control, activating it by Alt-letter should give focus to the text
// control, not the spin and the dialog navigation code will give focus to
// the next control (at Windows level), not the one after it
// create the text window
m_hwndBuddy = (WXHWND)::CreateWindowEx
(
exStyle, // sunken border
_T("EDIT"), // window class
NULL, // no window title
msStyle, // style (will be shown later)
pos.x, pos.y, // position
0, 0, // size (will be set later)
GetHwndOf(parent), // parent
(HMENU)-1, // control id
wxGetInstance(), // app instance
NULL // unused client data
);
if ( !m_hwndBuddy )
{
wxLogLastError(wxT("CreateWindow(buddy text window)"));
return false;
}
// create the spin button
if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
{
return false;
}
SetRange(min, max);
SetValue(initial);
// subclass the text ctrl to be able to intercept some events
wxSetWindowUserData(GetBuddyHwnd(), this);
m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(),
wxBuddyTextWndProc);
// set up fonts and colours (This is nomally done in MSWCreateControl)
InheritAttributes();
if (!m_hasFont)
SetFont(GetDefaultAttributes().font);
// set the size of the text window - can do it only now, because we
// couldn't call DoGetBestSize() before as font wasn't set
if ( sizeText.y <= 0 )
{
int cx, cy;
wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
}
SetBestSize(size);
(void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
// associate the text window with the spin button
(void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
if ( !value.empty() )
{
SetValue(value);
}
// do it after finishing with m_hwndBuddy creation to avoid generating
// initial wxEVT_COMMAND_TEXT_UPDATED message
ms_allSpins.Add(this);
return true;
}
wxSpinCtrl::~wxSpinCtrl()
{
ms_allSpins.Remove(this);
// This removes spurious memory leak reporting
if (ms_allSpins.GetCount() == 0)
ms_allSpins.Clear();
// destroy the buddy window because this pointer which wxBuddyTextWndProc
// uses will not soon be valid any more
::DestroyWindow(GetBuddyHwnd());
}
// ----------------------------------------------------------------------------
// wxTextCtrl-like methods
// ----------------------------------------------------------------------------
void wxSpinCtrl::SetValue(const wxString& text)
{
if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) )
{
wxLogLastError(wxT("SetWindowText(buddy)"));
}
}
int wxSpinCtrl::GetValue() const
{
wxString val = wxGetWindowText(m_hwndBuddy);
long n;
if ( (wxSscanf(val, wxT("%lu"), &n) != 1) )
n = INT_MIN;
if (n < m_min) n = m_min;
if (n > m_max) n = m_max;
return n;
}
void wxSpinCtrl::SetSelection(long from, long to)
{
// if from and to are both -1, it means (in wxWidgets) that all text should
// be selected - translate into Windows convention
if ( (from == -1) && (to == -1) )
{
from = 0;
}
::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to);
}
// ----------------------------------------------------------------------------
// forward some methods to subcontrols
// ----------------------------------------------------------------------------
bool wxSpinCtrl::SetFont(const wxFont& font)
{
if ( !wxWindowBase::SetFont(font) )
{
// nothing to do
return false;
}
WXHANDLE hFont = GetFont().GetResourceHandle();
(void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
return true;
}
bool wxSpinCtrl::Show(bool show)
{
if ( !wxControl::Show(show) )
{
return false;
}
::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
return true;
}
bool wxSpinCtrl::Enable(bool enable)
{
if ( !wxControl::Enable(enable) )
{
return false;
}
::EnableWindow(GetBuddyHwnd(), enable);
return true;
}
void wxSpinCtrl::SetFocus()
{
::SetFocus(GetBuddyHwnd());
}
#if wxUSE_TOOLTIPS
void wxSpinCtrl::DoSetToolTip(wxToolTip *tip)
{
wxSpinButton::DoSetToolTip(tip);
if ( tip )
tip->Add(m_hwndBuddy);
}
#endif // wxUSE_TOOLTIPS
// ----------------------------------------------------------------------------
// event processing
// ----------------------------------------------------------------------------
void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin)
{
wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
event.SetEventObject(this);
event.SetInt(eventSpin.GetPosition());
(void)GetEventHandler()->ProcessEvent(event);
if ( eventSpin.GetSkipped() )
{
event.Skip();
}
}
// ----------------------------------------------------------------------------
// size calculations
// ----------------------------------------------------------------------------
wxSize wxSpinCtrl::DoGetBestSize() const
{
wxSize sizeBtn = wxSpinButton::DoGetBestSize();
sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
int y;
wxGetCharSize(GetHWND(), NULL, &y, GetFont());
y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
// JACS: we should always use the height calculated
// from above, because otherwise we'll get a spin control
// that's too big. So never use the height calculated
// from wxSpinButton::DoGetBestSize().
// if ( sizeBtn.y < y )
{
// make the text tall enough
sizeBtn.y = y;
}
return sizeBtn;
}
void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
{
int widthBtn = wxSpinButton::DoGetBestSize().x;
int widthText = width - widthBtn - MARGIN_BETWEEN;
if ( widthText <= 0 )
{
wxLogDebug(_T("not enough space for wxSpinCtrl!"));
}
// 1) The buddy window
DoMoveSibling(m_hwndBuddy, x, y, widthText, height);
// 2) The button window
x += widthText + MARGIN_BETWEEN;
wxSpinButton::DoMoveWindow(x, y, widthBtn, height);
}
// get total size of the control
void wxSpinCtrl::DoGetSize(int *x, int *y) const
{
RECT spinrect, textrect, ctrlrect;
GetWindowRect(GetHwnd(), &spinrect);
GetWindowRect(GetBuddyHwnd(), &textrect);
UnionRect(&ctrlrect,&textrect, &spinrect);
if ( x )
*x = ctrlrect.right - ctrlrect.left;
if ( y )
*y = ctrlrect.bottom - ctrlrect.top;
}
void wxSpinCtrl::DoGetPosition(int *x, int *y) const
{
// hack: pretend that our HWND is the text control just for a moment
WXHWND hWnd = GetHWND();
wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy;
wxSpinButton::DoGetPosition(x, y);
wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd;
}
#endif // wxUSE_SPINCTRL
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?