]> git.saurik.com Git - wxWidgets.git/commitdiff
Corrections to take into account that range in the API has an end position
authorJulian Smart <julian@anthemion.co.uk>
Tue, 19 Sep 2006 13:47:32 +0000 (13:47 +0000)
committerJulian Smart <julian@anthemion.co.uk>
Tue, 19 Sep 2006 13:47:32 +0000 (13:47 +0000)
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

include/wx/richtext/richtextbuffer.h
include/wx/richtext/richtextctrl.h
src/richtext/richtextctrl.cpp

index 0e90377f236e9e298e56ac3c65ec7afa9b63e969..28a4fb98326ff5e7aee2a58e942ecd66a9409e30 100644 (file)
@@ -214,6 +214,7 @@ public:
 
     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); }
     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
     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:
     long m_start;
     long m_end;
index b008d5d984e382ea177b2f474ff8944ec548d08e..beef94f24d628ae451791e828347b63027d5e0e2 100644 (file)
@@ -380,8 +380,16 @@ public:
     virtual void SelectNone();
 
     /// Get/set the selection range in character positions. -1, -1 means no selection.
-    const wxRichTextRange& GetSelectionRange() const { return m_selectionRange; }
-    void SetSelectionRange(const wxRichTextRange& range) { m_selectionRange = range; }
+    /// The range is in API convention, i.e. a single character selection is denoted
+    /// 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
     virtual wxRichTextRange AddParagraph(const wxString& text);
index 32d4a691e85bf52259e6e88d547abd0f58b94275..bd1b6c6e8f2b88a12a44e0482582745cf6fc67b8 100644 (file)
@@ -259,7 +259,7 @@ void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
             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())
@@ -1597,7 +1597,7 @@ wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image)
 
 void wxRichTextCtrl::SelectAll()
 {
-    SetSelection(0, GetLastPosition());
+    SetSelection(0, GetLastPosition()+1);
     m_selectionAnchor = -1;
 }
 
@@ -1693,7 +1693,8 @@ wxString wxRichTextCtrl::GetValue() 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)
@@ -1907,6 +1908,8 @@ void wxRichTextCtrl::GetSelection(long* from, long* to) const
 {
     *from = m_selectionRange.GetStart();
     *to = m_selectionRange.GetEnd();
+    if ((*to) != -1 && (*to) != -2)
+        (*to) ++;
 }
 
 bool wxRichTextCtrl::IsEditable() const
@@ -1925,7 +1928,7 @@ void wxRichTextCtrl::SetSelection(long from, long to)
     if ( (from == -1) && (to == -1) )
     {
         from = 0;
-        to = GetLastPosition();
+        to = GetLastPosition()+1;
     }
 
     DoSetSelection(from, to);
@@ -1934,7 +1937,7 @@ void wxRichTextCtrl::SetSelection(long from, long to)
 void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret))
 {
     m_selectionAnchor = from;
-    m_selectionRange.SetRange(from, to);
+    m_selectionRange.SetRange(from, to-1);
     Refresh(false);
     PositionCaret();
 }
@@ -2208,17 +2211,17 @@ void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& WXUNUSED(event))
 
 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)
 {
-    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)
 {
-    return GetBuffer().SetStyle(range, style);
+    return GetBuffer().SetStyle(range.ToInternal(), style);
 }
 
 bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx& style)
@@ -2421,7 +2424,7 @@ bool wxRichTextCtrl::IsSelectionBold() const
     if (HasSelection())
     {
         wxRichTextAttr attr;
-        wxRichTextRange range = GetSelectionRange();
+        wxRichTextRange range = GetInternalSelectionRange();
         attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT);
         attr.SetFontWeight(wxBOLD);
 
@@ -2450,7 +2453,7 @@ bool wxRichTextCtrl::IsSelectionItalics() const
 {
     if (HasSelection())
     {
-        wxRichTextRange range = GetSelectionRange();
+        wxRichTextRange range = GetInternalSelectionRange();
         wxRichTextAttr attr;
         attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC);
         attr.SetFontStyle(wxITALIC);
@@ -2480,7 +2483,7 @@ bool wxRichTextCtrl::IsSelectionUnderlined() const
 {
     if (HasSelection())
     {
-        wxRichTextRange range = GetSelectionRange();
+        wxRichTextRange range = GetInternalSelectionRange();
         wxRichTextAttr attr;
         attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE);
         attr.SetFontUnderlined(true);
@@ -2552,7 +2555,7 @@ bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment) const
 {
     if (HasSelection())
     {
-        wxRichTextRange range = GetSelectionRange();
+        wxRichTextRange range = GetInternalSelectionRange();
         wxRichTextAttr attr;
         attr.SetAlignment(alignment);
 
@@ -2579,7 +2582,7 @@ bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment)
     {
         wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1);
         if (para)
-            return SetStyle(para->GetRange(), attr);
+            return SetStyle(para->GetRange().FromInternal(), attr);
     }
     return true;
 }
@@ -2643,5 +2646,27 @@ long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const
     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
     // wxUSE_RICHTEXT