]> git.saurik.com Git - wxWidgets.git/blobdiff - src/richtext/richtextctrl.cpp
added wxSpinCtrlDouble (slightly modified patch 1835864)
[wxWidgets.git] / src / richtext / richtextctrl.cpp
index c5ac3408e95ad18305c18afd480db10eafea23bc..01cbd9387a36a2b5c190c7b5bdf2ebe44e83ad4a 100644 (file)
@@ -54,6 +54,7 @@ DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED)
 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED)
 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED)
 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET)
 
 IMPLEMENT_CLASS( wxRichTextCtrl, wxControl )
 
@@ -140,12 +141,6 @@ bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& va
         SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
     }
 
-    GetBuffer().Reset();
-    GetBuffer().SetRichTextCtrl(this);
-
-    SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16));
-    GetCaret()->Show();
-
     if (style & wxTE_READONLY)
         SetEditable(false);
 
@@ -170,6 +165,12 @@ bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& va
     SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
     SetBackgroundStyle(wxBG_STYLE_CUSTOM);
 
+    GetBuffer().Reset();
+    GetBuffer().SetRichTextCtrl(this);
+
+    SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16));
+    GetCaret()->Show();
+
     // Tell the sizers to use the given or best size
     SetInitialSize(size);
 
@@ -235,6 +236,8 @@ void wxRichTextCtrl::Clear()
     m_caretAtLineStart = false;
     m_selectionRange.SetRange(-2, -2);
 
+    Scroll(0,0);
+
     if (!IsFrozen())
     {
         LayoutContent();
@@ -339,8 +342,6 @@ void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event)
         m_dragging = true;
         CaptureMouse();
 
-        SelectNone();
-
         bool caretAtLineStart = false;
 
         if (hit & wxRICHTEXT_HITTEST_BEFORE)
@@ -356,8 +357,24 @@ void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event)
             position --;
         }
 
+        long oldCaretPos = m_caretPosition;
+
         MoveCaret(position, caretAtLineStart);
         SetDefaultStyleToCursorStyle();
+
+        if (event.ShiftDown())
+        {
+            bool extendSel = false;
+            if (m_selectionRange.GetStart() == -2)
+                extendSel = ExtendSelection(oldCaretPos, m_caretPosition, wxRICHTEXT_SHIFT_DOWN);
+            else
+                extendSel = ExtendSelection(m_caretPosition, m_caretPosition, wxRICHTEXT_SHIFT_DOWN);
+
+            if (extendSel)
+                Refresh(false);
+        }
+        else
+            SelectNone();
     }
 
     event.Skip();
@@ -381,7 +398,7 @@ void wxRichTextCtrl::OnLeftUp(wxMouseEvent& event)
         wxPoint logicalPt = event.GetLogicalPosition(dc);
         int hit = GetBuffer().HitTest(dc, logicalPt, position);
 
-        if (hit != wxRICHTEXT_HITTEST_NONE)
+        if ((hit != wxRICHTEXT_HITTEST_NONE) && !(hit & wxRICHTEXT_HITTEST_OUTSIDE))
         {
             wxRichTextEvent cmdEvent(
                 wxEVT_COMMAND_RICHTEXT_LEFT_CLICK,
@@ -593,7 +610,7 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event)
             GetBuffer().InsertTextWithUndo(newPos+1, text, this);
         }
         else
-            GetBuffer().InsertNewlineWithUndo(newPos+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
+            GetBuffer().InsertNewlineWithUndo(newPos+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE|wxRICHTEXT_INSERT_INTERACTIVE);
 
         EndBatchUndo();
         SetDefaultStyleToCursorStyle();
@@ -625,7 +642,19 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event)
         // so subtract 1 for deleted character and add 1 for conversion to character position.
         if (m_caretPosition > -1 && !HasSelection())
         {
-            GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition, m_caretPosition), this);
+            bool processed = false;
+            if (event.CmdDown())
+            {
+                long pos = wxRichTextCtrl::FindNextWordPosition(-1);
+                if (pos != -1 && (pos < m_caretPosition))
+                {
+                    GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos+1, m_caretPosition), this);
+                    processed = true;
+                }
+            }
+
+            if (!processed)
+                GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition, m_caretPosition), this);
         }
         else
             DeleteSelectedContent();
@@ -1898,13 +1927,17 @@ bool wxRichTextCtrl::DoSaveFile(const wxString& filename, int fileType)
 /// Add a new paragraph of text to the end of the buffer
 wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text)
 {
-    return GetBuffer().AddParagraph(text);
+    wxRichTextRange range = GetBuffer().AddParagraph(text);
+    LayoutContent();
+    return range;
 }
 
 /// Add an image
 wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image)
 {
-    return GetBuffer().AddImage(image);
+    wxRichTextRange range = GetBuffer().AddImage(image);
+    LayoutContent();
+    return range;
 }
 
 // ----------------------------------------------------------------------------
@@ -1940,6 +1973,9 @@ bool wxRichTextCtrl::SelectWord(long position)
     if (!para)
         return false;
 
+    if (position == para->GetRange().GetEnd())
+        position --;
+
     long positionStart = position;
     long positionEnd = position;
 
@@ -1967,6 +2003,9 @@ bool wxRichTextCtrl::SelectWord(long position)
     if (positionEnd >= para->GetRange().GetEnd())
         positionEnd = para->GetRange().GetEnd();
 
+    if (positionEnd < positionStart)
+        return false;
+
     SetSelection(positionStart, positionEnd+1);
 
     if (positionStart >= 0)
@@ -2288,6 +2327,7 @@ void wxRichTextCtrl::SetSelection(long from, long to)
     }
 
     DoSetSelection(from, to);
+    SetDefaultStyleToCursorStyle();
 }
 
 void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret))
@@ -2321,7 +2361,7 @@ void wxRichTextCtrl::Remove(long from, long to)
 {
     SelectNone();
 
-    GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from, to), this);
+    GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from, to-1), this);
 
     LayoutContent();
     if (!IsFrozen())
@@ -2840,7 +2880,11 @@ bool wxRichTextCtrl::ApplyBoldToSelection()
     if (HasSelection())
         return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
     else
-        SetAndShowDefaultStyle(attr);
+    {
+        wxRichTextAttr current = GetDefaultStyleEx();
+        current.Apply(attr);
+        SetAndShowDefaultStyle(current);
+    }
     return true;
 }
 
@@ -2854,7 +2898,11 @@ bool wxRichTextCtrl::ApplyItalicToSelection()
     if (HasSelection())
         return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
     else
-        SetAndShowDefaultStyle(attr);
+    {
+        wxRichTextAttr current = GetDefaultStyleEx();
+        current.Apply(attr);
+        SetAndShowDefaultStyle(current);
+    }
     return true;
 }
 
@@ -2868,7 +2916,11 @@ bool wxRichTextCtrl::ApplyUnderlineToSelection()
     if (HasSelection())
         return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
     else
-        SetAndShowDefaultStyle(attr);
+    {
+        wxRichTextAttr current = GetDefaultStyleEx();
+        current.Apply(attr);
+        SetAndShowDefaultStyle(current);
+    }
     return true;
 }
 
@@ -2910,7 +2962,7 @@ bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def)
     // attributes are applied.
     wxTextAttr attr(GetStyleSheet() ? def->GetStyleMergedWithBase(GetStyleSheet()) : def->GetStyle());
 
-    int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE;
+    int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_RESET;
 
     if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition)))
     {
@@ -2946,7 +2998,9 @@ bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def)
         return SetStyleEx(GetSelectionRange(), attr, flags);
     else
     {
-        SetAndShowDefaultStyle(attr);
+        wxRichTextAttr current = GetDefaultStyleEx();
+        current.Apply(attr);
+        SetAndShowDefaultStyle(current);
         return true;
     }
 }