📄 textctrlce.cpp
字号:
DoWriteText(value, false);
// for compatibility, don't move the cursor when doing SetValue()
SetInsertionPoint(0);
}
else // same text
{
// still send an event for consistency
SendUpdateEvent();
}
// we should reset the modified flag even if the value didn't really change
// mark the control as being not dirty - we changed its text, not the
// user
DiscardEdits();
}
void wxTextCtrl::WriteText(const wxString& value)
{
DoWriteText(value);
}
void wxTextCtrl::DoWriteText(const wxString& value, bool selectionOnly)
{
wxString valueDos;
if ( m_windowStyle & wxTE_MULTILINE )
valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
else
valueDos = value;
// in some cases we get 2 EN_CHANGE notifications after the SendMessage
// call below which is confusing for the client code and so should be
// avoided
//
if ( ( selectionOnly && HasSelection() ) )
{
m_suppressNextUpdate = true;
}
::SendMessage(GetBuddyHwnd(), selectionOnly ? EM_REPLACESEL : WM_SETTEXT,
0, (LPARAM)valueDos.c_str());
if ( !selectionOnly )
{
// Windows already sends an update event for single-line
// controls.
if ( m_windowStyle & wxTE_MULTILINE )
SendUpdateEvent();
}
AdjustSpaceLimit();
}
void wxTextCtrl::AppendText(const wxString& text)
{
SetInsertionPointEnd();
WriteText(text);
}
void wxTextCtrl::Clear()
{
::SetWindowText(GetBuddyHwnd(), wxEmptyString);
// Windows already sends an update event for single-line
// controls.
if ( m_windowStyle & wxTE_MULTILINE )
SendUpdateEvent();
}
// ----------------------------------------------------------------------------
// Clipboard operations
// ----------------------------------------------------------------------------
void wxTextCtrl::Copy()
{
if (CanCopy())
{
::SendMessage(GetBuddyHwnd(), WM_COPY, 0, 0L);
}
}
void wxTextCtrl::Cut()
{
if (CanCut())
{
::SendMessage(GetBuddyHwnd(), WM_CUT, 0, 0L);
}
}
void wxTextCtrl::Paste()
{
if (CanPaste())
{
::SendMessage(GetBuddyHwnd(), WM_PASTE, 0, 0L);
}
}
bool wxTextCtrl::HasSelection() const
{
long from, to;
GetSelection(&from, &to);
return from != to;
}
bool wxTextCtrl::CanCopy() const
{
// Can copy if there's a selection
return HasSelection();
}
bool wxTextCtrl::CanCut() const
{
return CanCopy() && IsEditable();
}
bool wxTextCtrl::CanPaste() const
{
if ( !IsEditable() )
return false;
// Standard edit control: check for straight text on clipboard
if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
return false;
bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0;
::CloseClipboard();
return isTextAvailable;
}
// ----------------------------------------------------------------------------
// Accessors
// ----------------------------------------------------------------------------
void wxTextCtrl::SetEditable(bool editable)
{
::SendMessage(GetBuddyHwnd(), EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
}
void wxTextCtrl::SetInsertionPoint(long pos)
{
DoSetSelection(pos, pos);
}
void wxTextCtrl::SetInsertionPointEnd()
{
if ( GetInsertionPoint() != GetLastPosition() )
SetInsertionPoint(GetLastPosition());
}
long wxTextCtrl::GetInsertionPoint() const
{
DWORD Pos = (DWORD)::SendMessage(GetBuddyHwnd(), EM_GETSEL, 0, 0L);
return Pos & 0xFFFF;
}
wxTextPos wxTextCtrl::GetLastPosition() const
{
int numLines = GetNumberOfLines();
long posStartLastLine = XYToPosition(0, numLines - 1);
long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine);
return posStartLastLine + lenLastLine;
}
void wxTextCtrl::GetSelection(long* from, long* to) const
{
DWORD dwStart, dwEnd;
::SendMessage(GetBuddyHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
*from = dwStart;
*to = dwEnd;
}
bool wxTextCtrl::IsEditable() const
{
if ( !GetBuddyHwnd() )
return true;
long style = ::GetWindowLong(GetBuddyHwnd(), GWL_STYLE);
return (style & ES_READONLY) == 0;
}
// ----------------------------------------------------------------------------
// selection
// ----------------------------------------------------------------------------
void wxTextCtrl::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;
to = -1;
}
DoSetSelection(from, to);
}
void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret)
{
::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to);
if ( scrollCaret )
{
::SendMessage(GetBuddyHwnd(), EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
}
}
// ----------------------------------------------------------------------------
// Working with files
// ----------------------------------------------------------------------------
bool wxTextCtrl::LoadFile(const wxString& file)
{
if ( wxTextCtrlBase::LoadFile(file) )
{
// update the size limit if needed
AdjustSpaceLimit();
return true;
}
return false;
}
// ----------------------------------------------------------------------------
// Editing
// ----------------------------------------------------------------------------
void wxTextCtrl::Replace(long from, long to, const wxString& value)
{
// Set selection and remove it
DoSetSelection(from, to, false);
DoWriteText(value, true);
}
void wxTextCtrl::Remove(long from, long to)
{
Replace(from, to, wxEmptyString);
}
bool wxTextCtrl::IsModified() const
{
return ::SendMessage(GetBuddyHwnd(), EM_GETMODIFY, 0, 0) != 0;
}
void wxTextCtrl::MarkDirty()
{
::SendMessage(GetBuddyHwnd(), EM_SETMODIFY, TRUE, 0L);
}
void wxTextCtrl::DiscardEdits()
{
::SendMessage(GetBuddyHwnd(), EM_SETMODIFY, FALSE, 0L);
}
int wxTextCtrl::GetNumberOfLines() const
{
return (int)::SendMessage(GetBuddyHwnd(), EM_GETLINECOUNT, 0, 0L);
}
// ----------------------------------------------------------------------------
// Positions <-> coords
// ----------------------------------------------------------------------------
long wxTextCtrl::XYToPosition(long x, long y) const
{
// This gets the char index for the _beginning_ of this line
long charIndex = ::SendMessage(GetBuddyHwnd(), EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
return charIndex + x;
}
bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
{
// This gets the line number containing the character
long lineNo = ::SendMessage(GetBuddyHwnd(), EM_LINEFROMCHAR, (WPARAM)pos, 0);
if ( lineNo == -1 )
{
// no such line
return false;
}
// This gets the char index for the _beginning_ of this line
long charIndex = ::SendMessage(GetBuddyHwnd(), EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
if ( charIndex == -1 )
{
return false;
}
// The X position must therefore be the different between pos and charIndex
if ( x )
*x = pos - charIndex;
if ( y )
*y = lineNo;
return true;
}
wxTextCtrlHitTestResult
wxTextCtrl::HitTest(const wxPoint& pt, long *posOut) const
{
// first get the position from Windows
// for the plain ones, we are limited to 16 bit positions which are
// combined in a single 32 bit value
LPARAM lParam = MAKELPARAM(pt.x, pt.y);
LRESULT pos = ::SendMessage(GetBuddyHwnd(), EM_CHARFROMPOS, 0, lParam);
if ( pos == -1 )
{
// this seems to indicate an error...
return wxTE_HT_UNKNOWN;
}
// for plain EDIT controls the higher word contains something else
pos = LOWORD(pos);
// next determine where it is relatively to our point: EM_CHARFROMPOS
// always returns the closest character but we need to be more precise, so
// double check that we really are where it pretends
POINTL ptReal;
LRESULT lRc = ::SendMessage(GetBuddyHwnd(), EM_POSFROMCHAR, pos, 0);
if ( lRc == -1 )
{
// this is apparently returned when pos corresponds to the last
// position
ptReal.x =
ptReal.y = 0;
}
else
{
ptReal.x = LOWORD(lRc);
ptReal.y = HIWORD(lRc);
}
wxTextCtrlHitTestResult rc;
if ( pt.y > ptReal.y + GetCharHeight() )
rc = wxTE_HT_BELOW;
else if ( pt.x > ptReal.x + GetCharWidth() )
rc = wxTE_HT_BEYOND;
else
rc = wxTE_HT_ON_TEXT;
if ( posOut )
*posOut = pos;
return rc;
}
void wxTextCtrl::ShowPosition(long pos)
{
int currentLineLineNo = (int)::SendMessage(GetBuddyHwnd(), EM_GETFIRSTVISIBLELINE, 0, 0L);
int specifiedLineLineNo = (int)::SendMessage(GetBuddyHwnd(), EM_LINEFROMCHAR, (WPARAM)pos, 0L);
int linesToScroll = specifiedLineLineNo - currentLineLineNo;
if (linesToScroll != 0)
(void)::SendMessage(GetBuddyHwnd(), EM_LINESCROLL, 0, (LPARAM)linesToScroll);
}
long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const
{
return ::SendMessage(GetBuddyHwnd(), EM_LINELENGTH, (WPARAM)pos, 0L);
}
int wxTextCtrl::GetLineLength(long lineNo) const
{
long pos = XYToPosition(0, lineNo);
return GetLengthOfLineContainingPos(pos);
}
wxString wxTextCtrl::GetLineText(long lineNo) const
{
size_t len = (size_t)GetLineLength(lineNo) + 1;
// there must be at least enough place for the length WORD in the
// buffer
len += sizeof(WORD);
wxString str;
{
wxStringBufferLength tmp(str, len);
wxChar *buf = tmp;
*(WORD *)buf = (WORD)len;
len = (size_t)::SendMessage(GetBuddyHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
// remove the '\n' at the end, if any (this is how this function is
// supposed to work according to the docs)
if ( buf[len - 1] == _T('\n') )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -