Corrections to take into account that range in the API has an end position

that is 1 more than the last affected position


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@41298 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart
2006-09-19 13:47:32 +00:00
parent 7807a2b499
commit 96c9f0f677
3 changed files with 55 additions and 15 deletions

View File

@@ -214,6 +214,7 @@ public:
void operator =(const wxRichTextRange& range) { m_start = range.m_start; m_end = range.m_end; } void operator =(const wxRichTextRange& range) { m_start = range.m_start; m_end = range.m_end; }
bool operator ==(const wxRichTextRange& range) const { return (m_start == range.m_start && m_end == range.m_end); } bool operator ==(const wxRichTextRange& range) const { return (m_start == range.m_start && m_end == range.m_end); }
bool operator !=(const wxRichTextRange& range) const { return (m_start != range.m_start && m_end != range.m_end); }
wxRichTextRange operator -(const wxRichTextRange& range) const { return wxRichTextRange(m_start - range.m_start, m_end - range.m_end); } wxRichTextRange operator -(const wxRichTextRange& range) const { return wxRichTextRange(m_start - range.m_start, m_end - range.m_end); }
wxRichTextRange operator +(const wxRichTextRange& range) const { return wxRichTextRange(m_start + range.m_start, m_end + range.m_end); } wxRichTextRange operator +(const wxRichTextRange& range) const { return wxRichTextRange(m_start + range.m_start, m_end + range.m_end); }
@@ -246,6 +247,12 @@ public:
/// Swaps the start and end /// Swaps the start and end
void Swap() { long tmp = m_start; m_start = m_end; m_end = tmp; } void Swap() { long tmp = m_start; m_start = m_end; m_end = tmp; }
/// Convert to internal form: (n, n) is the range of a single character.
wxRichTextRange ToInternal() const { return wxRichTextRange(m_start, m_end-1); }
/// Convert from internal to public API form: (n, n+1) is the range of a single character.
wxRichTextRange FromInternal() const { return wxRichTextRange(m_start, m_end+1); }
protected: protected:
long m_start; long m_start;
long m_end; long m_end;

View File

@@ -380,8 +380,16 @@ public:
virtual void SelectNone(); virtual void SelectNone();
/// Get/set the selection range in character positions. -1, -1 means no selection. /// Get/set the selection range in character positions. -1, -1 means no selection.
const wxRichTextRange& GetSelectionRange() const { return m_selectionRange; } /// The range is in API convention, i.e. a single character selection is denoted
void SetSelectionRange(const wxRichTextRange& range) { m_selectionRange = range; } /// by (n, n+1)
wxRichTextRange GetSelectionRange() const;
void SetSelectionRange(const wxRichTextRange& range);
/// Get/set the selection range in character positions. -1, -1 means no selection.
/// The range is in internal format, i.e. a single character selection is denoted
/// by (n, n)
const wxRichTextRange& GetInternalSelectionRange() const { return m_selectionRange; }
void SetInternalSelectionRange(const wxRichTextRange& range) { m_selectionRange = range; }
/// Add a new paragraph of text to the end of the buffer /// Add a new paragraph of text to the end of the buffer
virtual wxRichTextRange AddParagraph(const wxString& text); virtual wxRichTextRange AddParagraph(const wxString& text);

View File

@@ -259,7 +259,7 @@ void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
SetupScrollbars(); SetupScrollbars();
} }
GetBuffer().Draw(dc, GetBuffer().GetRange(), GetSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */); GetBuffer().Draw(dc, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */);
} }
if (GetCaret()) if (GetCaret())
@@ -1597,7 +1597,7 @@ wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image)
void wxRichTextCtrl::SelectAll() void wxRichTextCtrl::SelectAll()
{ {
SetSelection(0, GetLastPosition()); SetSelection(0, GetLastPosition()+1);
m_selectionAnchor = -1; m_selectionAnchor = -1;
} }
@@ -1693,7 +1693,8 @@ wxString wxRichTextCtrl::GetValue() const
wxString wxRichTextCtrl::GetRange(long from, long to) const wxString wxRichTextCtrl::GetRange(long from, long to) const
{ {
return GetBuffer().GetTextForRange(wxRichTextRange(from, to)); // Public API for range is different from internals
return GetBuffer().GetTextForRange(wxRichTextRange(from, to-1));
} }
void wxRichTextCtrl::SetValue(const wxString& value) void wxRichTextCtrl::SetValue(const wxString& value)
@@ -1907,6 +1908,8 @@ void wxRichTextCtrl::GetSelection(long* from, long* to) const
{ {
*from = m_selectionRange.GetStart(); *from = m_selectionRange.GetStart();
*to = m_selectionRange.GetEnd(); *to = m_selectionRange.GetEnd();
if ((*to) != -1 && (*to) != -2)
(*to) ++;
} }
bool wxRichTextCtrl::IsEditable() const bool wxRichTextCtrl::IsEditable() const
@@ -1925,7 +1928,7 @@ void wxRichTextCtrl::SetSelection(long from, long to)
if ( (from == -1) && (to == -1) ) if ( (from == -1) && (to == -1) )
{ {
from = 0; from = 0;
to = GetLastPosition(); to = GetLastPosition()+1;
} }
DoSetSelection(from, to); DoSetSelection(from, to);
@@ -1934,7 +1937,7 @@ void wxRichTextCtrl::SetSelection(long from, long to)
void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret)) void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret))
{ {
m_selectionAnchor = from; m_selectionAnchor = from;
m_selectionRange.SetRange(from, to); m_selectionRange.SetRange(from, to-1);
Refresh(false); Refresh(false);
PositionCaret(); PositionCaret();
} }
@@ -2208,17 +2211,17 @@ void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& WXUNUSED(event))
bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttrEx& style) bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttrEx& style)
{ {
return GetBuffer().SetStyle(wxRichTextRange(start, end), style); return GetBuffer().SetStyle(wxRichTextRange(start, end-1), style);
} }
bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
{ {
return GetBuffer().SetStyle(wxRichTextRange(start, end), wxTextAttrEx(style)); return GetBuffer().SetStyle(wxRichTextRange(start, end-1), wxTextAttrEx(style));
} }
bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style) bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style)
{ {
return GetBuffer().SetStyle(range, style); return GetBuffer().SetStyle(range.ToInternal(), style);
} }
bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx& style) bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx& style)
@@ -2421,7 +2424,7 @@ bool wxRichTextCtrl::IsSelectionBold() const
if (HasSelection()) if (HasSelection())
{ {
wxRichTextAttr attr; wxRichTextAttr attr;
wxRichTextRange range = GetSelectionRange(); wxRichTextRange range = GetInternalSelectionRange();
attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
attr.SetFontWeight(wxBOLD); attr.SetFontWeight(wxBOLD);
@@ -2450,7 +2453,7 @@ bool wxRichTextCtrl::IsSelectionItalics() const
{ {
if (HasSelection()) if (HasSelection())
{ {
wxRichTextRange range = GetSelectionRange(); wxRichTextRange range = GetInternalSelectionRange();
wxRichTextAttr attr; wxRichTextAttr attr;
attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
attr.SetFontStyle(wxITALIC); attr.SetFontStyle(wxITALIC);
@@ -2480,7 +2483,7 @@ bool wxRichTextCtrl::IsSelectionUnderlined() const
{ {
if (HasSelection()) if (HasSelection())
{ {
wxRichTextRange range = GetSelectionRange(); wxRichTextRange range = GetInternalSelectionRange();
wxRichTextAttr attr; wxRichTextAttr attr;
attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
attr.SetFontUnderlined(true); attr.SetFontUnderlined(true);
@@ -2552,7 +2555,7 @@ bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment) const
{ {
if (HasSelection()) if (HasSelection())
{ {
wxRichTextRange range = GetSelectionRange(); wxRichTextRange range = GetInternalSelectionRange();
wxRichTextAttr attr; wxRichTextAttr attr;
attr.SetAlignment(alignment); attr.SetAlignment(alignment);
@@ -2579,7 +2582,7 @@ bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment)
{ {
wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1); wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
if (para) if (para)
return SetStyle(para->GetRange(), attr); return SetStyle(para->GetRange().FromInternal(), attr);
} }
return true; return true;
} }
@@ -2643,5 +2646,27 @@ long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const
return caretPos; return caretPos;
} }
/// Get/set the selection range in character positions. -1, -1 means no selection.
/// The range is in API convention, i.e. a single character selection is denoted
/// by (n, n+1)
wxRichTextRange wxRichTextCtrl::GetSelectionRange() const
{
wxRichTextRange range = GetInternalSelectionRange();
if (range != wxRichTextRange(-2,-2) && range != wxRichTextRange(-1,-1))
range.SetEnd(range.GetEnd() + 1);
return range;
}
void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange& range)
{
wxRichTextRange range1(range);
if (range1 != wxRichTextRange(-2,-2) && range1 != wxRichTextRange(-1,-1) )
range1.SetEnd(range1.GetEnd() - 1);
wxASSERT( range1.GetStart() > range1.GetEnd() );
SetInternalSelectionRange(range1);
}
#endif #endif
// wxUSE_RICHTEXT // wxUSE_RICHTEXT