]> git.saurik.com Git - wxWidgets.git/blobdiff - src/richtext/richtextctrl.cpp
[ 1936700 ] wxCAL_SHOW_WEEK_NUMBERS, slightly modified
[wxWidgets.git] / src / richtext / richtextctrl.cpp
index bf1f301f10e6571d7de6c6acfc9cbaa6523d6c96..d18a1b35aeec27702a8bbbca77c0c5bd25209614 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 )
 
@@ -76,6 +77,7 @@ BEGIN_EVENT_TABLE( wxRichTextCtrl, wxControl )
     EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus)
     EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost)
     EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu)
+    EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged)
 
     EVT_MENU(wxID_UNDO, wxRichTextCtrl::OnUndo)
     EVT_UPDATE_UI(wxID_UNDO, wxRichTextCtrl::OnUpdateUndo)
@@ -139,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);
 
@@ -169,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);
 
@@ -200,7 +202,6 @@ wxRichTextCtrl::~wxRichTextCtrl()
 /// Member initialisation
 void wxRichTextCtrl::Init()
 {
-    m_freezeCount = 0;
     m_contextMenu = NULL;
     m_caret = NULL;
     m_caretPosition = -1;
@@ -216,25 +217,13 @@ void wxRichTextCtrl::Init()
     m_caretPositionForDefaultStyle = -2;
 }
 
-/// Call Freeze to prevent refresh
-void wxRichTextCtrl::Freeze()
+void wxRichTextCtrl::DoThaw()
 {
-    m_freezeCount ++;
-}
-
-/// Call Thaw to refresh
-void wxRichTextCtrl::Thaw()
-{
-    m_freezeCount --;
-
-    if (m_freezeCount == 0)
-    {
-        if (GetBuffer().GetDirty())
-            LayoutContent();
-        else
-            SetupScrollbars();
-        Refresh(false);
-    }
+    if (GetBuffer().GetDirty())
+        LayoutContent();
+    else
+        SetupScrollbars();
+    Refresh(false);
 }
 
 /// Clear all text
@@ -247,7 +236,9 @@ void wxRichTextCtrl::Clear()
     m_caretAtLineStart = false;
     m_selectionRange.SetRange(-2, -2);
 
-    if (m_freezeCount == 0)
+    Scroll(0,0);
+
+    if (!IsFrozen())
     {
         LayoutContent();
         Refresh(false);
@@ -268,11 +259,12 @@ void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
 #else
         wxPaintDC dc(this);
 #endif
-        PrepareDC(dc);
 
-        if (m_freezeCount > 0)
+        if (IsFrozen())
             return;
 
+        PrepareDC(dc);
+
         dc.SetFont(GetFont());
 
         // Paint the background
@@ -350,8 +342,6 @@ void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event)
         m_dragging = true;
         CaptureMouse();
 
-        SelectNone();
-
         bool caretAtLineStart = false;
 
         if (hit & wxRICHTEXT_HITTEST_BEFORE)
@@ -367,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();
@@ -392,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,
@@ -506,7 +512,7 @@ void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event)
 }
 
 /// Right-click
-void wxRichTextCtrl::OnRightClick(wxMouseEvent& WXUNUSED(event))
+void wxRichTextCtrl::OnRightClick(wxMouseEvent& event)
 {
     SetFocus();
 
@@ -516,7 +522,8 @@ void wxRichTextCtrl::OnRightClick(wxMouseEvent& WXUNUSED(event))
     cmdEvent.SetEventObject(this);
     cmdEvent.SetPosition(m_caretPosition+1);
 
-    GetEventHandler()->ProcessEvent(cmdEvent);
+    if (!GetEventHandler()->ProcessEvent(cmdEvent))
+        event.Skip();
 }
 
 /// Left-double-click
@@ -603,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();
@@ -635,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();
@@ -1787,7 +1806,7 @@ void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event)
 /// Set up scrollbars, e.g. after a resize
 void wxRichTextCtrl::SetupScrollbars(bool atTop)
 {
-    if (m_freezeCount)
+    if (IsFrozen())
         return;
 
     if (GetBuffer().IsEmpty())
@@ -1908,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;
 }
 
 // ----------------------------------------------------------------------------
@@ -1931,7 +1954,10 @@ void wxRichTextCtrl::SelectAll()
 void wxRichTextCtrl::SelectNone()
 {
     if (!(GetSelectionRange() == wxRichTextRange(-2, -2)))
-        SetSelection(-2, -2);
+    {
+        Refresh(false);
+        m_selectionRange = wxRichTextRange(-2, -2);
+    }
     m_selectionAnchor = -2;
 }
 
@@ -1950,6 +1976,9 @@ bool wxRichTextCtrl::SelectWord(long position)
     if (!para)
         return false;
 
+    if (position == para->GetRange().GetEnd())
+        position --;
+
     long positionStart = position;
     long positionEnd = position;
 
@@ -1977,6 +2006,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)
@@ -2057,13 +2089,27 @@ wxString wxRichTextCtrl::GetRange(long from, long to) const
 
 void wxRichTextCtrl::DoSetValue(const wxString& value, int flags)
 {
-    Clear();
+    // Don't call Clear here, since it always sends a text updated event
+    m_buffer.ResetAndClearCommands();
+    m_buffer.SetDirty(true);
+    m_caretPosition = -1;
+    m_caretPositionForDefaultStyle = -2;
+    m_caretAtLineStart = false;
+    m_selectionRange.SetRange(-2, -2);
+
+    Scroll(0,0);
+
+    if (!IsFrozen())
+    {
+        LayoutContent();
+        Refresh(false);
+    }
 
     if (!value.IsEmpty())
     {
         // Remove empty paragraph
         GetBuffer().Clear();
-        DoWriteText(value);
+        DoWriteText(value, flags);
 
         // for compatibility, don't move the cursor when doing SetValue()
         SetInsertionPoint(0);
@@ -2248,6 +2294,8 @@ void wxRichTextCtrl::SetInsertionPoint(long pos)
     SelectNone();
 
     m_caretPosition = pos - 1;
+
+    PositionCaret();
 }
 
 void wxRichTextCtrl::SetInsertionPointEnd()
@@ -2296,17 +2344,25 @@ void wxRichTextCtrl::SetSelection(long from, long to)
     }
 
     DoSetSelection(from, to);
+    SetDefaultStyleToCursorStyle();
 }
 
 void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret))
 {
-    m_selectionAnchor = from;
-    m_selectionRange.SetRange(from, to-1);
-    if (from > -2)
-        m_caretPosition = from-1;
+    if (from == to)
+    {
+        SelectNone();
+    }
+    else
+    {
+        m_selectionAnchor = from;
+        m_selectionRange.SetRange(from, to-1);
+        if (from > -2)
+            m_caretPosition = from-1;
 
-    Refresh(false);
-    PositionCaret();
+        Refresh(false);
+        PositionCaret();
+    }
 }
 
 // ----------------------------------------------------------------------------
@@ -2329,7 +2385,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())
@@ -2848,7 +2904,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;
 }
 
@@ -2862,7 +2922,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;
 }
 
@@ -2876,7 +2940,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;
 }
 
@@ -2918,7 +2986,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)))
     {
@@ -2954,7 +3022,9 @@ bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def)
         return SetStyleEx(GetSelectionRange(), attr, flags);
     else
     {
-        SetAndShowDefaultStyle(attr);
+        wxRichTextAttr current = GetDefaultStyleEx();
+        current.Apply(attr);
+        SetAndShowDefaultStyle(current);
         return true;
     }
 }
@@ -3111,5 +3181,17 @@ void wxRichTextCtrl::ClearAvailableFontNames()
     sm_availableFontNames.Clear();
 }
 
+void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
+{
+    //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged"));
+
+    wxTextAttrEx basicStyle = GetBasicStyle();
+    basicStyle.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
+    SetBasicStyle(basicStyle);
+    SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
+
+    Refresh();
+}
+
 #endif
     // wxUSE_RICHTEXT