📄 textctrl.cpp
字号:
if ( ! (style & wxNO_BORDER) )
style = (style & ~wxBORDER_MASK) | wxSUNKEN_BORDER ;
if ( !wxTextCtrlBase::Create( parent, id, pos, size, style & ~(wxHSCROLL | wxVSCROLL), validator, name ) )
return false;
if ( m_windowStyle & wxTE_MULTILINE )
{
wxASSERT_MSG(
!(m_windowStyle & wxTE_PROCESS_ENTER),
wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
m_windowStyle |= wxTE_PROCESS_ENTER;
style |= wxTE_PROCESS_ENTER ;
}
bool forceMLTE = false ;
#if wxUSE_SYSTEM_OPTIONS
if (wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_MLTE ) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_MLTE ) == 1))
{
forceMLTE = true ;
}
#endif
#ifdef __WXMAC_OSX__
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
if ( UMAGetSystemVersion() >= 0x1030 && !forceMLTE )
{
if ( m_windowStyle & wxTE_MULTILINE )
m_peer = new wxMacMLTEHIViewControl( this , str , pos , size , style ) ;
}
#endif
if ( !m_peer )
{
if ( !(m_windowStyle & wxTE_MULTILINE) && !forceMLTE )
m_peer = new wxMacUnicodeTextControl( this , str , pos , size , style ) ;
}
#endif
if ( !m_peer )
m_peer = new wxMacMLTEClassicControl( this , str , pos , size , style ) ;
MacPostControlCreate(pos, size) ;
// only now the embedding is correct and we can do a positioning update
MacSuperChangedPosition() ;
if ( m_windowStyle & wxTE_READONLY)
SetEditable( false ) ;
SetCursor( wxCursor( wxCURSOR_IBEAM ) ) ;
return true;
}
void wxTextCtrl::MacSuperChangedPosition()
{
wxWindow::MacSuperChangedPosition() ;
GetPeer()->SuperChangedPosition() ;
}
void wxTextCtrl::MacVisibilityChanged()
{
GetPeer()->VisibilityChanged( MacIsReallyShown() ) ;
}
void wxTextCtrl::MacEnabledStateChanged()
{
}
wxString wxTextCtrl::GetValue() const
{
return GetPeer()->GetStringValue() ;
}
void wxTextCtrl::GetSelection(long* from, long* to) const
{
GetPeer()->GetSelection( from , to ) ;
}
void wxTextCtrl::SetValue(const wxString& str)
{
// optimize redraws
if ( GetValue() == str )
return ;
GetPeer()->SetStringValue( str ) ;
if ( m_triggerOnSetValue )
{
wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
event.SetString( GetValue() );
event.SetEventObject( this );
GetEventHandler()->ProcessEvent( event );
}
}
void wxTextCtrl::SetMaxLength(unsigned long len)
{
m_maxLength = len ;
}
bool wxTextCtrl::SetFont( const wxFont& font )
{
if ( !wxTextCtrlBase::SetFont( font ) )
return false ;
GetPeer()->SetFont( font , GetForegroundColour() , GetWindowStyle() ) ;
return true ;
}
bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
{
GetPeer()->SetStyle( start , end , style ) ;
return true ;
}
bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style)
{
wxTextCtrlBase::SetDefaultStyle( style ) ;
SetStyle( kTXNUseCurrentSelection , kTXNUseCurrentSelection , GetDefaultStyle() ) ;
return true ;
}
// Clipboard operations
void wxTextCtrl::Copy()
{
if (CanCopy())
GetPeer()->Copy() ;
}
void wxTextCtrl::Cut()
{
if (CanCut())
{
GetPeer()->Cut() ;
wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
event.SetEventObject( this );
GetEventHandler()->ProcessEvent( event );
}
}
void wxTextCtrl::Paste()
{
if (CanPaste())
{
GetPeer()->Paste() ;
// TODO: eventually we should add setting the default style again
wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
event.SetEventObject( this );
GetEventHandler()->ProcessEvent( event );
}
}
bool wxTextCtrl::CanCopy() const
{
// Can copy if there's a selection
long from, to;
GetSelection( &from, &to );
return (from != to);
}
bool wxTextCtrl::CanCut() const
{
if ( !IsEditable() )
return false;
// Can cut if there's a selection
long from, to;
GetSelection( &from, &to );
return (from != to);
}
bool wxTextCtrl::CanPaste() const
{
if (!IsEditable())
return false;
return GetPeer()->CanPaste() ;
}
void wxTextCtrl::SetEditable(bool editable)
{
if ( editable != m_editable )
{
m_editable = editable ;
GetPeer()->SetEditable( editable ) ;
}
}
void wxTextCtrl::SetInsertionPoint(long pos)
{
SetSelection( pos , pos ) ;
}
void wxTextCtrl::SetInsertionPointEnd()
{
wxTextPos pos = GetLastPosition();
SetInsertionPoint( pos );
}
long wxTextCtrl::GetInsertionPoint() const
{
long begin, end ;
GetSelection( &begin , &end ) ;
return begin ;
}
wxTextPos wxTextCtrl::GetLastPosition() const
{
return GetPeer()->GetLastPosition() ;
}
void wxTextCtrl::Replace(long from, long to, const wxString& str)
{
GetPeer()->Replace( from , to , str ) ;
}
void wxTextCtrl::Remove(long from, long to)
{
GetPeer()->Remove( from , to ) ;
}
void wxTextCtrl::SetSelection(long from, long to)
{
GetPeer()->SetSelection( from , to ) ;
}
bool wxTextCtrl::LoadFile(const wxString& file)
{
return wxTextCtrlBase::LoadFile( file );
}
void wxTextCtrl::WriteText(const wxString& str)
{
// TODO: this MPRemoting will be moved into a remoting peer proxy for any command
if ( !wxIsMainThread() )
{
// unfortunately CW 8 is not able to correctly deduce the template types,
// so we have to instantiate explicitly
wxMacMPRemoteGUICall<wxTextCtrl,wxString>( this , &wxTextCtrl::WriteText , str ) ;
return ;
}
GetPeer()->WriteText( str ) ;
}
void wxTextCtrl::AppendText(const wxString& text)
{
SetInsertionPointEnd();
WriteText( text );
}
void wxTextCtrl::Clear()
{
GetPeer()->Clear() ;
}
bool wxTextCtrl::IsModified() const
{
return m_dirty;
}
bool wxTextCtrl::IsEditable() const
{
return IsEnabled() && m_editable ;
}
bool wxTextCtrl::AcceptsFocus() const
{
// we don't want focus if we can't be edited
return /*IsEditable() && */ wxControl::AcceptsFocus();
}
wxSize wxTextCtrl::DoGetBestSize() const
{
int wText, hText;
// these are the numbers from the HIG:
// we reduce them by the borders first
wText = 100 ;
switch ( m_windowVariant )
{
case wxWINDOW_VARIANT_NORMAL :
hText = 22 - 6 ;
break ;
case wxWINDOW_VARIANT_SMALL :
hText = 19 - 6 ;
break ;
case wxWINDOW_VARIANT_MINI :
hText = 15 - 6 ;
break ;
default :
hText = 22 - 6;
break ;
}
// as the above numbers have some free space around the text
// we get 5 lines like this anyway
if ( m_windowStyle & wxTE_MULTILINE )
hText *= 5 ;
if ( !HasFlag(wxNO_BORDER) )
hText += 6 ;
return wxSize(wText, hText);
}
// ----------------------------------------------------------------------------
// Undo/redo
// ----------------------------------------------------------------------------
void wxTextCtrl::Undo()
{
if (CanUndo())
GetPeer()->Undo() ;
}
void wxTextCtrl::Redo()
{
if (CanRedo())
GetPeer()->Redo() ;
}
bool wxTextCtrl::CanUndo() const
{
if ( !IsEditable() )
return false ;
return GetPeer()->CanUndo() ;
}
bool wxTextCtrl::CanRedo() const
{
if ( !IsEditable() )
return false ;
return GetPeer()->CanRedo() ;
}
void wxTextCtrl::MarkDirty()
{
m_dirty = true;
}
void wxTextCtrl::DiscardEdits()
{
m_dirty = false;
}
int wxTextCtrl::GetNumberOfLines() const
{
return GetPeer()->GetNumberOfLines() ;
}
long wxTextCtrl::XYToPosition(long x, long y) const
{
return GetPeer()->XYToPosition( x , y ) ;
}
bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
{
return GetPeer()->PositionToXY( pos , x , y ) ;
}
void wxTextCtrl::ShowPosition(long pos)
{
return GetPeer()->ShowPosition(pos) ;
}
int wxTextCtrl::GetLineLength(long lineNo) const
{
return GetPeer()->GetLineLength(lineNo) ;
}
wxString wxTextCtrl::GetLineText(long lineNo) const
{
return GetPeer()->GetLineText(lineNo) ;
}
void wxTextCtrl::Command(wxCommandEvent & event)
{
SetValue(event.GetString());
ProcessCommand(event);
}
void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
{
// By default, load the first file into the text window.
if (event.GetNumberOfFiles() > 0)
LoadFile( event.GetFiles()[0] );
}
void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
{
// all erasing should be done by the real mac control implementation
// while this is true for MLTE under classic, the HITextView is somehow
// transparent but background erase is not working correctly, so intercept
// things while we can...
event.Skip() ;
}
void wxTextCtrl::OnChar(wxKeyEvent& event)
{
int key = event.GetKeyCode() ;
bool eat_key = false ;
if ( key == 'a' && event.MetaDown() )
{
SelectAll() ;
return ;
}
if ( key == 'c' && event.MetaDown() )
{
if ( CanCopy() )
Copy() ;
return ;
}
if ( !IsEditable() && key != WXK_LEFT && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_UP && key != WXK_TAB &&
!( key == WXK_RETURN && ( (m_windowStyle & wxTE_PROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) )
// && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
)
{
// eat it
return ;
}
// Check if we have reached the max # of chars (if it is set), but still
// allow navigation and deletion
if ( !IsMultiLine() && m_maxLength && GetValue().length() >= m_maxLength &&
key != WXK_LEFT && key != WXK_RIGHT && key != WXK_TAB &&
key != WXK_BACK && !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) )
)
{
// eat it, we don't want to add more than allowed # of characters
// TODO: generate EVT_TEXT_MAXLEN()
return;
}
// assume that any key not processed yet is going to modify the control
m_dirty = true;
if ( key == 'v' && event.MetaDown() )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -