Split single/multi line behaviour in Qt wxTextCtrl
Introduce wxQtEdit class and wxQtMultiLineEdit and wxQtSingleLineEdit derived classes instead of using ifs in many wxTextCtrl methods, making the code more clear and maintainable. Also fix some wxTextCtrl-related unit test failures with wxQt and disable some other ones which still don't pass. Closes https://github.com/wxWidgets/wxWidgets/pull/1039
This commit is contained in:
committed by
Vadim Zeitlin
parent
3b6fcbab6d
commit
0fbf87d11b
@@ -8,9 +8,8 @@
|
||||
#ifndef _WX_QT_TEXTCTRL_H_
|
||||
#define _WX_QT_TEXTCTRL_H_
|
||||
|
||||
class QLineEdit;
|
||||
class QTextEdit;
|
||||
class QScrollArea;
|
||||
class wxQtEdit;
|
||||
|
||||
class WXDLLIMPEXP_CORE wxTextCtrl : public wxTextCtrlBase
|
||||
{
|
||||
@@ -25,6 +24,8 @@ public:
|
||||
const wxValidator& validator = wxDefaultValidator,
|
||||
const wxString &name = wxTextCtrlNameStr);
|
||||
|
||||
virtual ~wxTextCtrl();
|
||||
|
||||
bool Create(wxWindow *parent,
|
||||
wxWindowID id,
|
||||
const wxString &value = wxEmptyString,
|
||||
@@ -71,8 +72,7 @@ protected:
|
||||
virtual QScrollArea *QtGetScrollBarsContainer() const;
|
||||
|
||||
private:
|
||||
QLineEdit *m_qtLineEdit;
|
||||
QTextEdit *m_qtTextEdit;
|
||||
wxQtEdit *m_qtEdit;
|
||||
|
||||
wxDECLARE_DYNAMIC_CLASS( wxTextCtrl );
|
||||
};
|
||||
|
@@ -17,16 +17,396 @@
|
||||
#include <QtWidgets/QLineEdit>
|
||||
#include <QtWidgets/QTextEdit>
|
||||
|
||||
/*
|
||||
* Abstract base class for wxQtSingleLineEdit and wxQtMultiLineEdit.
|
||||
* This splits the polymorphic behaviour into two separate classes, avoiding
|
||||
* unnecessary branches.
|
||||
*/
|
||||
class wxQtEdit
|
||||
{
|
||||
public:
|
||||
virtual ~wxQtEdit() {}
|
||||
|
||||
virtual bool IsModified() const = 0;
|
||||
virtual int GetNumberOfLines() const = 0;
|
||||
virtual wxString DoGetValue() const = 0;
|
||||
virtual long GetInsertionPoint() const = 0;
|
||||
virtual QWidget *GetHandle() const = 0;
|
||||
virtual int GetLineLength(long lineNo) const = 0;
|
||||
virtual wxString GetLineText(long lineNo) const = 0;
|
||||
virtual bool GetSelection(long *from, long *to) const = 0;
|
||||
virtual long XYToPosition(long x, long y) const = 0;
|
||||
virtual bool PositionToXY(long pos, long *x, long *y) const = 0;
|
||||
virtual QScrollArea *ScrollBarsContainer() const = 0;
|
||||
virtual void WriteText( const wxString &text ) = 0;
|
||||
virtual void MarkDirty() = 0;
|
||||
virtual void DiscardEdits() = 0;
|
||||
virtual void blockSignals(bool block) = 0;
|
||||
virtual void SetValue( const wxString &value ) = 0;
|
||||
virtual void SetSelection( long from, long to ) = 0;
|
||||
virtual void SetInsertionPoint(long pos) = 0;
|
||||
};
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct wxQtLineInfo
|
||||
{
|
||||
wxQtLineInfo(size_t start, size_t end) :
|
||||
startPos(start),
|
||||
endPos(end)
|
||||
{
|
||||
}
|
||||
|
||||
size_t startPos, endPos;
|
||||
};
|
||||
|
||||
class wxQtLineEdit : public wxQtEventSignalHandler< QLineEdit, wxTextCtrl >
|
||||
{
|
||||
public:
|
||||
wxQtLineEdit( wxWindow *parent, wxTextCtrl *handler );
|
||||
|
||||
private:
|
||||
void textChanged(const QString &text);
|
||||
void textChanged();
|
||||
void returnPressed();
|
||||
};
|
||||
|
||||
class wxQtTextEdit : public wxQtEventSignalHandler< QTextEdit, wxTextCtrl >
|
||||
{
|
||||
public:
|
||||
wxQtTextEdit( wxWindow *parent, wxTextCtrl *handler );
|
||||
|
||||
private:
|
||||
void textChanged();
|
||||
};
|
||||
|
||||
class wxQtMultiLineEdit : public wxQtEdit
|
||||
{
|
||||
public:
|
||||
explicit wxQtMultiLineEdit(QTextEdit *edit) : m_edit(edit)
|
||||
{
|
||||
}
|
||||
|
||||
virtual bool IsModified() const wxOVERRIDE
|
||||
{
|
||||
return m_edit->isWindowModified();
|
||||
}
|
||||
|
||||
virtual wxString DoGetValue() const wxOVERRIDE
|
||||
{
|
||||
return wxQtConvertString( m_edit->toPlainText() );
|
||||
}
|
||||
|
||||
virtual long GetInsertionPoint() const wxOVERRIDE
|
||||
{
|
||||
QTextCursor cursor = m_edit->textCursor();
|
||||
return cursor.anchor();
|
||||
}
|
||||
|
||||
virtual QWidget *GetHandle() const wxOVERRIDE
|
||||
{
|
||||
return m_edit;
|
||||
}
|
||||
|
||||
virtual int GetNumberOfLines() const wxOVERRIDE
|
||||
{
|
||||
const wxString &value = DoGetValue();
|
||||
return std::count(value.begin(), value.end(), '\n') + 1;
|
||||
}
|
||||
|
||||
virtual int GetLineLength(long lineNo) const wxOVERRIDE
|
||||
{
|
||||
wxQtLineInfo start = GetLineInfo(lineNo, DoGetValue());
|
||||
if ( start.startPos == wxString::npos )
|
||||
return -1;
|
||||
|
||||
return start.endPos - start.startPos;
|
||||
}
|
||||
|
||||
virtual wxString GetLineText(long lineNo) const wxOVERRIDE
|
||||
{
|
||||
const wxString &value = DoGetValue();
|
||||
|
||||
wxQtLineInfo start = GetLineInfo(lineNo, value);
|
||||
if ( start.startPos == wxString::npos )
|
||||
return wxString();
|
||||
|
||||
return value.Mid(start.startPos, start.endPos - start.startPos);
|
||||
}
|
||||
|
||||
virtual long XYToPosition(long x, long y) const wxOVERRIDE
|
||||
{
|
||||
if ( x < 0 || y < 0 )
|
||||
return -1;
|
||||
|
||||
wxQtLineInfo start = GetLineInfo(y, DoGetValue());
|
||||
if ( start.startPos == wxString::npos )
|
||||
return -1;
|
||||
|
||||
if ( start.endPos - start.startPos < static_cast<size_t>(x) )
|
||||
return -1;
|
||||
|
||||
return start.startPos + x;
|
||||
}
|
||||
|
||||
virtual bool PositionToXY(long pos, long *x, long *y) const wxOVERRIDE
|
||||
{
|
||||
const wxString &value = DoGetValue();
|
||||
|
||||
if ( static_cast<size_t>(pos) > value.length() )
|
||||
return false;
|
||||
|
||||
int cnt = 0;
|
||||
int xval = 0;
|
||||
|
||||
for ( long xx = 0; xx < pos; xx++ )
|
||||
{
|
||||
if ( value[xx] == '\n' )
|
||||
{
|
||||
xval = 0;
|
||||
cnt++;
|
||||
}
|
||||
else xval++;
|
||||
}
|
||||
*y = cnt;
|
||||
*x = xval;
|
||||
|
||||
return true;
|
||||
}
|
||||
virtual void WriteText( const wxString &text ) wxOVERRIDE
|
||||
{
|
||||
m_edit->insertPlainText(wxQtConvertString( text ));
|
||||
// the cursor is moved to the end, ensure it is shown
|
||||
m_edit->ensureCursorVisible();
|
||||
}
|
||||
|
||||
virtual void MarkDirty() wxOVERRIDE
|
||||
{
|
||||
return m_edit->setWindowModified( true );
|
||||
}
|
||||
|
||||
virtual void DiscardEdits() wxOVERRIDE
|
||||
{
|
||||
return m_edit->setWindowModified( false );
|
||||
}
|
||||
|
||||
virtual void blockSignals(bool block) wxOVERRIDE
|
||||
{
|
||||
m_edit->blockSignals(block);
|
||||
}
|
||||
|
||||
virtual void SetValue( const wxString &value ) wxOVERRIDE
|
||||
{
|
||||
m_edit->setPlainText(wxQtConvertString( value ));
|
||||
// the cursor is moved to the end, ensure it is shown
|
||||
m_edit->ensureCursorVisible();
|
||||
}
|
||||
|
||||
virtual void SetSelection( long from, long to ) wxOVERRIDE
|
||||
{
|
||||
QTextCursor cursor = m_edit->textCursor();
|
||||
cursor.setPosition(from);
|
||||
|
||||
cursor.setPosition(to, QTextCursor::KeepAnchor);
|
||||
m_edit->setTextCursor(cursor);
|
||||
}
|
||||
|
||||
virtual bool GetSelection( long *from, long *to ) const wxOVERRIDE
|
||||
{
|
||||
QTextCursor cursor = m_edit->textCursor();
|
||||
*from = cursor.selectionStart();
|
||||
*to = cursor.selectionEnd();
|
||||
return cursor.hasSelection();
|
||||
}
|
||||
|
||||
virtual void SetInsertionPoint(long pos) wxOVERRIDE
|
||||
{
|
||||
QTextCursor::MoveOperation op;
|
||||
|
||||
// check if pos indicates end of text:
|
||||
if ( pos == -1 )
|
||||
{
|
||||
pos = 0;
|
||||
op = QTextCursor::End;
|
||||
}
|
||||
else
|
||||
{
|
||||
op = QTextCursor::Start;
|
||||
}
|
||||
|
||||
QTextCursor cursor = m_edit->textCursor();
|
||||
cursor.movePosition(op, QTextCursor::MoveAnchor, pos);
|
||||
|
||||
if (op != QTextCursor::End )
|
||||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, pos);
|
||||
m_edit->setTextCursor(cursor);
|
||||
m_edit->ensureCursorVisible();
|
||||
}
|
||||
|
||||
QScrollArea *ScrollBarsContainer() const wxOVERRIDE
|
||||
{
|
||||
return (QScrollArea *) m_edit;
|
||||
}
|
||||
|
||||
private:
|
||||
wxQtLineInfo GetLineInfo(long lineNo, const wxString &value) const
|
||||
{
|
||||
size_t pos = 0;
|
||||
long cnt = 0;
|
||||
|
||||
while ( cnt < lineNo )
|
||||
{
|
||||
size_t tpos = value.find('\n', pos);
|
||||
if ( tpos == wxString::npos )
|
||||
return wxQtLineInfo(tpos, tpos);
|
||||
|
||||
pos = tpos + 1;
|
||||
cnt++;
|
||||
}
|
||||
|
||||
size_t end = value.find('\n', pos);
|
||||
if ( end == wxString::npos )
|
||||
end = value.length();
|
||||
|
||||
return wxQtLineInfo(pos, end);
|
||||
}
|
||||
|
||||
QTextEdit* const m_edit;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxQtMultiLineEdit);
|
||||
};
|
||||
|
||||
class wxQtSingleLineEdit : public wxQtEdit
|
||||
{
|
||||
public:
|
||||
explicit wxQtSingleLineEdit(QLineEdit *edit) :
|
||||
m_edit(edit)
|
||||
{
|
||||
}
|
||||
|
||||
virtual bool IsModified() const wxOVERRIDE
|
||||
{
|
||||
return m_edit->isModified();
|
||||
}
|
||||
|
||||
virtual int GetNumberOfLines() const wxOVERRIDE
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
virtual wxString DoGetValue() const wxOVERRIDE
|
||||
{
|
||||
return wxQtConvertString( m_edit->text() );
|
||||
}
|
||||
|
||||
virtual long GetInsertionPoint() const wxOVERRIDE
|
||||
{
|
||||
long selectionStart = m_edit->selectionStart();
|
||||
|
||||
if ( selectionStart >= 0 )
|
||||
return selectionStart;
|
||||
|
||||
return m_edit->cursorPosition();
|
||||
}
|
||||
|
||||
virtual QWidget *GetHandle() const wxOVERRIDE
|
||||
{
|
||||
return m_edit;
|
||||
}
|
||||
|
||||
virtual int GetLineLength(long WXUNUSED(lineNo)) const wxOVERRIDE
|
||||
{
|
||||
return DoGetValue().length();
|
||||
}
|
||||
|
||||
virtual wxString GetLineText(long lineNo) const wxOVERRIDE
|
||||
{
|
||||
return lineNo == 0 ? DoGetValue() : wxString();
|
||||
}
|
||||
|
||||
virtual void WriteText( const wxString &text ) wxOVERRIDE
|
||||
{
|
||||
m_edit->insert(wxQtConvertString( text ));
|
||||
}
|
||||
|
||||
virtual void MarkDirty() wxOVERRIDE
|
||||
{
|
||||
return m_edit->setModified( true );
|
||||
}
|
||||
|
||||
virtual void DiscardEdits() wxOVERRIDE
|
||||
{
|
||||
return m_edit->setModified( false );
|
||||
}
|
||||
|
||||
virtual void blockSignals(bool block) wxOVERRIDE
|
||||
{
|
||||
m_edit->blockSignals(block);
|
||||
}
|
||||
|
||||
virtual void SetValue( const wxString &value ) wxOVERRIDE
|
||||
{
|
||||
m_edit->setText(wxQtConvertString( value ));
|
||||
}
|
||||
|
||||
virtual void SetSelection( long from, long to ) wxOVERRIDE
|
||||
{
|
||||
m_edit->setSelection(from, to - from);
|
||||
}
|
||||
|
||||
virtual bool GetSelection( long *from, long *to ) const wxOVERRIDE
|
||||
{
|
||||
*from = m_edit->selectionStart();
|
||||
if ( *from < 0 )
|
||||
return false;
|
||||
|
||||
*to = *from + m_edit->selectedText().length();
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual void SetInsertionPoint(long pos) wxOVERRIDE
|
||||
{
|
||||
// check if pos indicates end of text:
|
||||
if ( pos == -1 )
|
||||
m_edit->end(false);
|
||||
else
|
||||
m_edit->setCursorPosition(pos);
|
||||
}
|
||||
|
||||
virtual long XYToPosition(long x, long y) const wxOVERRIDE
|
||||
{
|
||||
if ( y == 0 && x >= 0 )
|
||||
{
|
||||
if ( static_cast<size_t>(x) <= DoGetValue().length() )
|
||||
return x;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
virtual bool PositionToXY(long pos, long *x, long *y) const wxOVERRIDE
|
||||
{
|
||||
const wxString &value = DoGetValue();
|
||||
|
||||
if ( static_cast<size_t>(pos) > value.length() )
|
||||
return false;
|
||||
|
||||
*y = 0;
|
||||
*x = pos;
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual QScrollArea *ScrollBarsContainer() const wxOVERRIDE
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
private:
|
||||
QLineEdit *m_edit;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxQtSingleLineEdit);
|
||||
};
|
||||
|
||||
wxQtLineEdit::wxQtLineEdit( wxWindow *parent, wxTextCtrl *handler )
|
||||
: wxQtEventSignalHandler< QLineEdit, wxTextCtrl >( parent, handler )
|
||||
{
|
||||
@@ -36,14 +416,12 @@ wxQtLineEdit::wxQtLineEdit( wxWindow *parent, wxTextCtrl *handler )
|
||||
this, &wxQtLineEdit::returnPressed);
|
||||
}
|
||||
|
||||
void wxQtLineEdit::textChanged(const QString &text)
|
||||
void wxQtLineEdit::textChanged()
|
||||
{
|
||||
wxTextCtrl *handler = GetHandler();
|
||||
wxTextEntryBase *handler = GetHandler();
|
||||
if ( handler )
|
||||
{
|
||||
wxCommandEvent event( wxEVT_TEXT, handler->GetId() );
|
||||
event.SetString( wxQtConvertString( text ) );
|
||||
EmitEvent( event );
|
||||
handler->SendTextUpdatedEventIfAllowed();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,16 +439,6 @@ void wxQtLineEdit::returnPressed()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class wxQtTextEdit : public wxQtEventSignalHandler< QTextEdit, wxTextCtrl >
|
||||
{
|
||||
public:
|
||||
wxQtTextEdit( wxWindow *parent, wxTextCtrl *handler );
|
||||
|
||||
private:
|
||||
void textChanged();
|
||||
};
|
||||
|
||||
wxQtTextEdit::wxQtTextEdit( wxWindow *parent, wxTextCtrl *handler )
|
||||
: wxQtEventSignalHandler< QTextEdit, wxTextCtrl >( parent, handler )
|
||||
{
|
||||
@@ -80,17 +448,18 @@ wxQtTextEdit::wxQtTextEdit( wxWindow *parent, wxTextCtrl *handler )
|
||||
|
||||
void wxQtTextEdit::textChanged()
|
||||
{
|
||||
wxTextCtrl *handler = GetHandler();
|
||||
wxTextEntryBase *handler = GetHandler();
|
||||
if ( handler )
|
||||
{
|
||||
wxCommandEvent event( wxEVT_TEXT, handler->GetId() );
|
||||
event.SetString( handler->GetValue() );
|
||||
EmitEvent( event );
|
||||
handler->SendTextUpdatedEventIfAllowed();
|
||||
}
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
wxTextCtrl::wxTextCtrl()
|
||||
|
||||
wxTextCtrl::wxTextCtrl() :
|
||||
m_qtEdit(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -115,20 +484,19 @@ bool wxTextCtrl::Create(wxWindow *parent,
|
||||
const wxValidator& validator,
|
||||
const wxString &name)
|
||||
{
|
||||
bool multiline = (style & wxTE_MULTILINE) != 0;
|
||||
|
||||
if (!multiline)
|
||||
if ( style & wxTE_MULTILINE )
|
||||
{
|
||||
m_qtLineEdit = new wxQtLineEdit( parent, this );
|
||||
m_qtTextEdit = NULL;
|
||||
if ( style & wxTE_PASSWORD )
|
||||
m_qtLineEdit->setEchoMode( QLineEdit::Password );
|
||||
m_qtEdit = new wxQtMultiLineEdit(new wxQtTextEdit(parent, this));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_qtTextEdit = new wxQtTextEdit( parent, this );
|
||||
m_qtLineEdit = NULL;
|
||||
wxQtLineEdit *lineEdit = new wxQtLineEdit(parent, this);
|
||||
if ( style & wxTE_PASSWORD )
|
||||
lineEdit->setEchoMode( QLineEdit::Password );
|
||||
|
||||
m_qtEdit = new wxQtSingleLineEdit(lineEdit);
|
||||
}
|
||||
|
||||
if ( QtCreateControl( parent, id, pos, size, style, validator, name ) )
|
||||
{
|
||||
// set the initial text value without sending the event:
|
||||
@@ -141,48 +509,44 @@ bool wxTextCtrl::Create(wxWindow *parent,
|
||||
return false;
|
||||
}
|
||||
|
||||
wxTextCtrl::~wxTextCtrl()
|
||||
{
|
||||
delete m_qtEdit;
|
||||
}
|
||||
|
||||
wxSize wxTextCtrl::DoGetBestSize() const
|
||||
{
|
||||
return wxTextCtrlBase::DoGetBestSize();
|
||||
|
||||
}
|
||||
int wxTextCtrl::GetLineLength(long WXUNUSED(lineNo)) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
wxString wxTextCtrl::GetLineText(long WXUNUSED(lineNo)) const
|
||||
int wxTextCtrl::GetLineLength(long lineNo) const
|
||||
{
|
||||
return wxString();
|
||||
return m_qtEdit->GetLineLength(lineNo);
|
||||
}
|
||||
|
||||
wxString wxTextCtrl::GetLineText(long lineNo) const
|
||||
{
|
||||
return m_qtEdit->GetLineText(lineNo);
|
||||
}
|
||||
|
||||
int wxTextCtrl::GetNumberOfLines() const
|
||||
{
|
||||
return 0;
|
||||
return m_qtEdit->GetNumberOfLines();
|
||||
}
|
||||
|
||||
bool wxTextCtrl::IsModified() const
|
||||
{
|
||||
if ( IsSingleLine() )
|
||||
return m_qtLineEdit->isModified();
|
||||
else
|
||||
return m_qtTextEdit->isWindowModified();
|
||||
return m_qtEdit->IsModified();
|
||||
}
|
||||
|
||||
void wxTextCtrl::MarkDirty()
|
||||
{
|
||||
if ( IsSingleLine() )
|
||||
return m_qtLineEdit->setModified( true );
|
||||
else
|
||||
return m_qtTextEdit->setWindowModified( true );
|
||||
m_qtEdit->MarkDirty();
|
||||
}
|
||||
|
||||
void wxTextCtrl::DiscardEdits()
|
||||
{
|
||||
if ( IsSingleLine() )
|
||||
return m_qtLineEdit->setModified( false );
|
||||
else
|
||||
return m_qtTextEdit->setWindowModified( false );
|
||||
m_qtEdit->DiscardEdits();
|
||||
}
|
||||
|
||||
bool wxTextCtrl::SetStyle(long WXUNUSED(start), long WXUNUSED(end), const wxTextAttr& WXUNUSED(style))
|
||||
@@ -200,14 +564,17 @@ bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& WXUNUSED(style))
|
||||
return false;
|
||||
}
|
||||
|
||||
long wxTextCtrl::XYToPosition(long WXUNUSED(x), long WXUNUSED(y)) const
|
||||
long wxTextCtrl::XYToPosition(long x, long y) const
|
||||
{
|
||||
return 0;
|
||||
return m_qtEdit->XYToPosition(x, y);
|
||||
}
|
||||
|
||||
bool wxTextCtrl::PositionToXY(long WXUNUSED(pos), long *WXUNUSED(x), long *WXUNUSED(y)) const
|
||||
bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
|
||||
{
|
||||
return false;
|
||||
if ( x == NULL || y == NULL || pos < 0 )
|
||||
return false;
|
||||
|
||||
return m_qtEdit->PositionToXY(pos, x, y);
|
||||
}
|
||||
|
||||
void wxTextCtrl::ShowPosition(long WXUNUSED(pos))
|
||||
@@ -226,98 +593,35 @@ bool wxTextCtrl::DoSaveFile(const wxString& WXUNUSED(file), int WXUNUSED(fileTyp
|
||||
|
||||
void wxTextCtrl::SetInsertionPoint(long pos)
|
||||
{
|
||||
QTextCursor::MoveOperation op;
|
||||
QTextCursor cursor;
|
||||
|
||||
// check if pos indicates end of text:
|
||||
if ( pos == -1 )
|
||||
{
|
||||
pos = 0;
|
||||
op = QTextCursor::End;
|
||||
}
|
||||
else
|
||||
{
|
||||
op = QTextCursor::Start;
|
||||
}
|
||||
if ( !IsMultiLine() )
|
||||
{
|
||||
if (op == QTextCursor::End)
|
||||
m_qtLineEdit->end(false);
|
||||
else
|
||||
m_qtLineEdit->setCursorPosition(pos);
|
||||
}
|
||||
else
|
||||
{
|
||||
cursor = m_qtTextEdit->textCursor();
|
||||
cursor.movePosition(op, QTextCursor::MoveAnchor, pos);
|
||||
m_qtTextEdit->setTextCursor(cursor);
|
||||
m_qtTextEdit->ensureCursorVisible();
|
||||
}
|
||||
m_qtEdit->SetInsertionPoint(pos);
|
||||
}
|
||||
|
||||
long wxTextCtrl::GetInsertionPoint() const
|
||||
{
|
||||
QTextCursor cursor;
|
||||
|
||||
if ( !IsMultiLine() )
|
||||
{
|
||||
return m_qtLineEdit->cursorPosition();
|
||||
}
|
||||
else
|
||||
{
|
||||
cursor = m_qtTextEdit->textCursor();
|
||||
return cursor.position();
|
||||
}
|
||||
return m_qtEdit->GetInsertionPoint();
|
||||
}
|
||||
|
||||
wxString wxTextCtrl::DoGetValue() const
|
||||
{
|
||||
if ( IsMultiLine() )
|
||||
return wxQtConvertString( m_qtTextEdit->toPlainText() );
|
||||
else
|
||||
return wxQtConvertString( m_qtLineEdit->text() );
|
||||
return m_qtEdit->DoGetValue();
|
||||
}
|
||||
|
||||
void wxTextCtrl::SetSelection( long from, long to )
|
||||
{
|
||||
// SelectAll uses -1 to -1, adjust for qt:
|
||||
if (from == -1 && to == -1)
|
||||
{
|
||||
from = 0;
|
||||
if ( to == -1 )
|
||||
to = GetValue().length();
|
||||
}
|
||||
if ( IsMultiLine() )
|
||||
{
|
||||
QTextCursor cursor = m_qtTextEdit->textCursor();
|
||||
cursor.setPosition(from);
|
||||
cursor.setPosition(to, QTextCursor::KeepAnchor);
|
||||
m_qtTextEdit->setTextCursor(cursor);
|
||||
}
|
||||
else // single line
|
||||
{
|
||||
m_qtLineEdit->setSelection(from, to);
|
||||
}
|
||||
|
||||
if ( from == -1 )
|
||||
from = 0;
|
||||
|
||||
m_qtEdit->SetSelection( from, to );
|
||||
}
|
||||
|
||||
void wxTextCtrl::GetSelection(long* from, long* to) const
|
||||
{
|
||||
if ( IsMultiLine() )
|
||||
{
|
||||
QTextCursor cursor = m_qtTextEdit->textCursor();
|
||||
*from = cursor.selectionStart();
|
||||
*to = cursor.selectionEnd();
|
||||
if ( cursor.hasSelection() )
|
||||
return;
|
||||
}
|
||||
else // single line
|
||||
{
|
||||
*from = m_qtLineEdit->selectionStart();
|
||||
if ( *from >= 0 )
|
||||
{
|
||||
*to = *from + m_qtLineEdit->selectedText().length();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if ( m_qtEdit->GetSelection(from, to) )
|
||||
return;
|
||||
// No selection, call base for default behaviour:
|
||||
wxTextEntry::GetSelection(from, to);
|
||||
}
|
||||
@@ -325,16 +629,7 @@ void wxTextCtrl::GetSelection(long* from, long* to) const
|
||||
void wxTextCtrl::WriteText( const wxString &text )
|
||||
{
|
||||
// Insert the text
|
||||
if ( !IsMultiLine() )
|
||||
{
|
||||
m_qtLineEdit->insert(wxQtConvertString( text ));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_qtTextEdit->insertPlainText(wxQtConvertString( text ));
|
||||
// the cursor is moved to the end, ensure it is shown
|
||||
m_qtTextEdit->ensureCursorVisible();
|
||||
}
|
||||
m_qtEdit->WriteText(text);
|
||||
}
|
||||
|
||||
void wxTextCtrl::DoSetValue( const wxString &text, int flags )
|
||||
@@ -342,46 +637,25 @@ void wxTextCtrl::DoSetValue( const wxString &text, int flags )
|
||||
// do not fire qt signals for certain methods (i.e. ChangeText)
|
||||
if ( !(flags & SetValue_SendEvent) )
|
||||
{
|
||||
if ( !IsMultiLine() )
|
||||
m_qtLineEdit->blockSignals(true);
|
||||
else
|
||||
m_qtTextEdit->blockSignals(true);
|
||||
m_qtEdit->blockSignals(true);
|
||||
}
|
||||
|
||||
// Replace the text int the control
|
||||
if ( !IsMultiLine() )
|
||||
{
|
||||
m_qtLineEdit->setText(wxQtConvertString( text ));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_qtTextEdit->setPlainText(wxQtConvertString( text ));
|
||||
// the cursor is moved to the end, ensure it is shown
|
||||
m_qtTextEdit->ensureCursorVisible();
|
||||
}
|
||||
m_qtEdit->SetValue( text );
|
||||
|
||||
// re-enable qt signals
|
||||
if ( !(flags & SetValue_SendEvent) )
|
||||
{
|
||||
if ( !IsMultiLine() )
|
||||
m_qtLineEdit->blockSignals(false);
|
||||
else
|
||||
m_qtTextEdit->blockSignals(false);
|
||||
m_qtEdit->blockSignals(false);
|
||||
}
|
||||
SetInsertionPoint(0);
|
||||
}
|
||||
|
||||
QWidget *wxTextCtrl::GetHandle() const
|
||||
{
|
||||
if (m_qtLineEdit!=NULL)
|
||||
return (QWidget *) m_qtLineEdit;
|
||||
else
|
||||
return (QWidget *) m_qtTextEdit;
|
||||
return (QWidget *) m_qtEdit->GetHandle();
|
||||
}
|
||||
|
||||
QScrollArea *wxTextCtrl::QtGetScrollBarsContainer() const
|
||||
{
|
||||
if (m_qtTextEdit!=NULL)
|
||||
return (QScrollArea *) m_qtTextEdit;
|
||||
else
|
||||
return NULL;
|
||||
return m_qtEdit->ScrollBarsContainer();
|
||||
}
|
||||
|
@@ -18,8 +18,11 @@ void wxTextEntry::WriteText(const wxString& WXUNUSED(text))
|
||||
{
|
||||
}
|
||||
|
||||
void wxTextEntry::Remove(long WXUNUSED(from), long WXUNUSED(to))
|
||||
void wxTextEntry::Remove(long from, long to)
|
||||
{
|
||||
wxString string = GetValue();
|
||||
string.erase(from, to - from);
|
||||
SetValue(string);
|
||||
}
|
||||
|
||||
void wxTextEntry::Copy()
|
||||
@@ -63,7 +66,7 @@ long wxTextEntry::GetInsertionPoint() const
|
||||
|
||||
long wxTextEntry::GetLastPosition() const
|
||||
{
|
||||
return 0;
|
||||
return GetValue().length();
|
||||
}
|
||||
|
||||
void wxTextEntry::SetSelection(long WXUNUSED(from), long WXUNUSED(to))
|
||||
|
@@ -332,6 +332,9 @@ void TextCtrlTestCase::Redirector()
|
||||
|
||||
void TextCtrlTestCase::HitTestSingleLine()
|
||||
{
|
||||
#ifdef __WXQT__
|
||||
WARN("Does not work under WxQt");
|
||||
#else
|
||||
m_text->ChangeValue("Hit me");
|
||||
|
||||
// We don't know the size of the text borders, so we can't really do any
|
||||
@@ -373,6 +376,7 @@ void TextCtrlTestCase::HitTestSingleLine()
|
||||
REQUIRE( m_text->HitTest(wxPoint(2*sizeChar.x, yMid), &pos) == wxTE_HT_ON_TEXT );
|
||||
CHECK( pos > 3 );
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if 0
|
||||
@@ -433,7 +437,7 @@ void TextCtrlTestCase::Url()
|
||||
|
||||
void TextCtrlTestCase::Style()
|
||||
{
|
||||
#ifndef __WXOSX__
|
||||
#if !defined(__WXOSX__) && !defined(__WXQT__)
|
||||
delete m_text;
|
||||
// We need wxTE_RICH under windows for style support
|
||||
CreateText(wxTE_MULTILINE|wxTE_RICH);
|
||||
@@ -485,11 +489,14 @@ void TextCtrlTestCase::Style()
|
||||
REQUIRE( m_text->GetStyle(17, style) );
|
||||
CHECK( style.GetTextColour() == *wxRED );
|
||||
CHECK( style.GetBackgroundColour() == *wxWHITE );
|
||||
#else
|
||||
WARN("Does not work under WxQt or OSX");
|
||||
#endif
|
||||
}
|
||||
|
||||
void TextCtrlTestCase::FontStyle()
|
||||
{
|
||||
#if !defined(__WXQT__)
|
||||
// We need wxTE_RICH under MSW and wxTE_MULTILINE under GTK for style
|
||||
// support so recreate the control with these styles.
|
||||
delete m_text;
|
||||
@@ -540,6 +547,9 @@ void TextCtrlTestCase::FontStyle()
|
||||
fontOut.SetEncoding(fontIn.GetEncoding());
|
||||
#endif
|
||||
CPPUNIT_ASSERT_EQUAL( fontIn, fontOut );
|
||||
#else
|
||||
WARN("Does not work under WxQt");
|
||||
#endif
|
||||
}
|
||||
|
||||
void TextCtrlTestCase::Lines()
|
||||
@@ -566,7 +576,7 @@ void TextCtrlTestCase::Lines()
|
||||
// #12366, where GetNumberOfLines() always returns the number of logical,
|
||||
// not physical, lines.
|
||||
m_text->AppendText("\n" + wxString(50, '1') + ' ' + wxString(50, '2'));
|
||||
#if defined(__WXGTK__) || defined(__WXOSX_COCOA__) || defined(__WXUNIVERSAL__)
|
||||
#if defined(__WXGTK__) || defined(__WXOSX_COCOA__) || defined(__WXUNIVERSAL__) || defined(__WXQT__)
|
||||
CPPUNIT_ASSERT_EQUAL(6, m_text->GetNumberOfLines());
|
||||
#else
|
||||
CPPUNIT_ASSERT(m_text->GetNumberOfLines() > 6);
|
||||
|
@@ -45,17 +45,25 @@ void TextEntryTestCase::TextChangeEvents()
|
||||
wxTextEntry * const entry = GetTestEntry();
|
||||
|
||||
// notice that SetValue() generates an event even if the text didn't change
|
||||
#ifndef __WXQT__
|
||||
entry->SetValue("");
|
||||
CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
|
||||
updated.Clear();
|
||||
#else
|
||||
WARN("Events are only sent when text changes in WxQt");
|
||||
#endif
|
||||
|
||||
entry->SetValue("foo");
|
||||
CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
|
||||
updated.Clear();
|
||||
|
||||
#ifndef __WXQT__
|
||||
entry->SetValue("foo");
|
||||
CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
|
||||
updated.Clear();
|
||||
#else
|
||||
WARN("Events are only sent when text changes in WxQt");
|
||||
#endif
|
||||
|
||||
entry->SetValue("");
|
||||
CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
|
||||
|
Reference in New Issue
Block a user