📄 choicece.cpp
字号:
msStyle |= LBS_NOTIFY;
return msStyle;
}
bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
{
if ( param != LBN_SELCHANGE)
{
// "selection changed" is the only event we're after
return false;
}
int n = GetSelection();
if (n > -1)
{
wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
event.SetInt(n);
event.SetEventObject(this);
event.SetString(GetStringSelection());
if ( HasClientObjectData() )
event.SetClientObject( GetClientObject(n) );
else if ( HasClientUntypedData() )
event.SetClientData( GetClientData(n) );
ProcessCommand(event);
}
return true;
}
wxChoice::~wxChoice()
{
Free();
}
// ----------------------------------------------------------------------------
// adding/deleting items to/from the list
// ----------------------------------------------------------------------------
int wxChoice::DoAppend(const wxString& item)
{
int n = (int)::SendMessage(GetBuddyHwnd(), LB_ADDSTRING, 0, (LPARAM)item.c_str());
if ( n == LB_ERR )
{
wxLogLastError(wxT("SendMessage(LB_ADDSTRING)"));
}
return n;
}
int wxChoice::DoInsert(const wxString& item, unsigned int pos)
{
wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into choice"));
wxCHECK_MSG(IsValidInsert(pos), -1, wxT("invalid index"));
int n = (int)::SendMessage(GetBuddyHwnd(), LB_INSERTSTRING, pos, (LPARAM)item.c_str());
if ( n == LB_ERR )
{
wxLogLastError(wxT("SendMessage(LB_INSERTSTRING)"));
}
return n;
}
void wxChoice::Delete(unsigned int n)
{
wxCHECK_RET( IsValid(n), wxT("invalid item index in wxChoice::Delete") );
if ( HasClientObjectData() )
{
delete GetClientObject(n);
}
::SendMessage(GetBuddyHwnd(), LB_DELETESTRING, n, 0);
}
void wxChoice::Clear()
{
Free();
::SendMessage(GetBuddyHwnd(), LB_RESETCONTENT, 0, 0);
}
void wxChoice::Free()
{
if ( HasClientObjectData() )
{
unsigned int count = GetCount();
for ( unsigned int n = 0; n < count; n++ )
{
delete GetClientObject(n);
}
}
}
// ----------------------------------------------------------------------------
// selection
// ----------------------------------------------------------------------------
int wxChoice::GetSelection() const
{
return (int)::SendMessage(GetBuddyHwnd(), LB_GETCURSEL, 0, 0);
}
void wxChoice::SetSelection(int n)
{
::SendMessage(GetBuddyHwnd(), LB_SETCURSEL, n, 0);
}
// ----------------------------------------------------------------------------
// string list functions
// ----------------------------------------------------------------------------
unsigned int wxChoice::GetCount() const
{
return (unsigned int)::SendMessage(GetBuddyHwnd(), LB_GETCOUNT, 0, 0);
}
int wxChoice::FindString(const wxString& s, bool bCase) const
{
// back to base class search for not native search type
if (bCase)
return wxItemContainerImmutable::FindString( s, bCase );
int pos = (int)::SendMessage(GetBuddyHwnd(), LB_FINDSTRINGEXACT,
(WPARAM)-1, (LPARAM)s.c_str());
return pos == LB_ERR ? wxNOT_FOUND : pos;
}
void wxChoice::SetString(unsigned int n, const wxString& s)
{
wxCHECK_RET( IsValid(n),
wxT("invalid item index in wxChoice::SetString") );
// we have to delete and add back the string as there is no way to change a
// string in place
// we need to preserve the client data
void *data;
if ( m_clientDataItemsType != wxClientData_None )
{
data = DoGetItemClientData(n);
}
else // no client data
{
data = NULL;
}
::SendMessage(GetBuddyHwnd(), LB_DELETESTRING, n, 0);
::SendMessage(GetBuddyHwnd(), LB_INSERTSTRING, n, (LPARAM)s.c_str() );
if ( data )
{
DoSetItemClientData(n, data);
}
//else: it's already NULL by default
}
wxString wxChoice::GetString(unsigned int n) const
{
int len = (int)::SendMessage(GetBuddyHwnd(), LB_GETTEXTLEN, n, 0);
wxString str;
if ( len != LB_ERR && len > 0 )
{
if ( ::SendMessage
(
GetBuddyHwnd(),
LB_GETTEXT,
n,
(LPARAM)(wxChar *)wxStringBuffer(str, len)
) == LB_ERR )
{
wxLogLastError(wxT("SendMessage(LB_GETLBTEXT)"));
}
}
return str;
}
// ----------------------------------------------------------------------------
// client data
// ----------------------------------------------------------------------------
void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
{
if ( ::SendMessage(GetHwnd(), LB_SETITEMDATA,
n, (LPARAM)clientData) == LB_ERR )
{
wxLogLastError(wxT("LB_SETITEMDATA"));
}
}
void* wxChoice::DoGetItemClientData(unsigned int n) const
{
LPARAM rc = ::SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
if ( rc == LB_ERR )
{
wxLogLastError(wxT("LB_GETITEMDATA"));
// unfortunately, there is no way to return an error code to the user
rc = (LPARAM) NULL;
}
return (void *)rc;
}
void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
{
DoSetItemClientData(n, clientData);
}
wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const
{
return (wxClientData *)DoGetItemClientData(n);
}
// ----------------------------------------------------------------------------
// size calculations
// ----------------------------------------------------------------------------
wxSize wxChoice::DoGetBestSize() const
{
wxSize sizeBtn = GetBestSpinnerSize(IsVertical(GetWindowStyle()));
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 wxChoice::DoMoveWindow(int x, int y, int width, int height)
{
int widthBtn = GetBestSpinnerSize(IsVertical(GetWindowStyle())).x;
int widthText = width - widthBtn - MARGIN_BETWEEN;
if ( widthText <= 0 )
{
wxLogDebug(_T("not enough space for wxSpinCtrl!"));
}
if ( !::MoveWindow(GetBuddyHwnd(), x, y, widthText, height, TRUE) )
{
wxLogLastError(wxT("MoveWindow(buddy)"));
}
x += widthText + MARGIN_BETWEEN;
if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
{
wxLogLastError(wxT("MoveWindow"));
}
}
// get total size of the control
void wxChoice::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 wxChoice::DoGetPosition(int *x, int *y) const
{
// hack: pretend that our HWND is the text control just for a moment
WXHWND hWnd = GetHWND();
wxConstCast(this, wxChoice)->m_hWnd = m_hwndBuddy;
wxChoiceBase::DoGetPosition(x, y);
wxConstCast(this, wxChoice)->m_hWnd = hWnd;
}
#endif // wxUSE_CHOICE && __SMARTPHONE__ && __WXWINCE__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -