]> git.saurik.com Git - wxWidgets.git/blobdiff - src/richtext/richtextbuffer.cpp
Don't omit empty text objects
[wxWidgets.git] / src / richtext / richtextbuffer.cpp
index 9a96b42105532bb88c76685e8dd820d6194d6979..039707a80179f360d35a3da5135e71ab05066e7d 100644 (file)
@@ -1159,13 +1159,10 @@ bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextParag
 
             while (objectNode)
             {
-                if (!objectNode->GetData()->IsEmpty())
-                {
-                    wxRichTextObject* newObj = objectNode->GetData()->Clone();
+                wxRichTextObject* newObj = objectNode->GetData()->Clone();
 
-                    // Append
-                    para->AppendChild(newObj);
-                }
+                // Append
+                para->AppendChild(newObj);
 
                 objectNode = objectNode->GetNext();
             }
@@ -1212,7 +1209,8 @@ bool wxRichTextParagraphLayoutBox::InsertFragment(long position, wxRichTextParag
             // 4. Add back the remaining content.
             if (finalPara)
             {
-                finalPara->MoveFromList(savedObjects);
+                if (nextObject)
+                    finalPara->MoveFromList(savedObjects);
 
                 // Ensure there's at least one object
                 if (finalPara->GetChildCount() == 0)
@@ -1470,15 +1468,7 @@ bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange& range)
                     {
                         wxRichTextObject* obj1 = node1->GetData();
 
-                        // If the object is empty, optimise it out
-                        if (obj1->IsEmpty())
-                        {
-                            delete obj1;
-                        }
-                        else
-                        {
-                            firstPara->AppendChild(obj1);
-                        }
+                        firstPara->AppendChild(obj1);
 
                         wxRichTextObjectList::compatibility_iterator next1 = node1->GetNext();
                         nextParagraph->GetChildren().Erase(node1);
@@ -1490,6 +1480,13 @@ bool wxRichTextParagraphLayoutBox::DeleteRange(const wxRichTextRange& range)
                     RemoveChild(nextParagraph, true);
                 }
 
+                // Avoid empty paragraphs
+                if (firstPara && firstPara->GetChildren().GetCount() == 0)
+                {
+                    wxRichTextPlainText* text = new wxRichTextPlainText(wxEmptyString);
+                    firstPara->AppendChild(text);
+                }
+
                 if (applyFinalParagraphStyle)
                     firstPara->SetAttributes(nextParaAttr);
 
@@ -1769,7 +1766,7 @@ bool wxRichTextParagraphLayoutBox::SetStyle(const wxRichTextRange& range, const
                         splitPoint ++;
 
                     // Find last object
-                    if (splitPoint == newPara->GetRange().GetEnd() || splitPoint == (newPara->GetRange().GetEnd() - 1))
+                    if (splitPoint == newPara->GetRange().GetEnd())
                         lastObject = newPara->GetChildren().GetLast()->GetData();
                     else
                         // lastObject is set as a side-effect of splitting. It's
@@ -4272,11 +4269,11 @@ bool wxRichTextPlainText::Draw(wxDC& dc, const wxRichTextRange& range, const wxR
     wxString str = m_text;
     wxString toRemove = wxRichTextLineBreakChar;
     str.Replace(toRemove, wxT(" "));
+    if (textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS))
+        str.MakeUpper();
 
     long len = range.GetLength();
     wxString stringChunk = str.Mid(range.GetStart() - offset, (size_t) len);
-    if (textAttr.HasTextEffects() && (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS))
-        stringChunk.MakeUpper();
 
     int charHeight = dc.GetCharHeight();
 
@@ -4903,17 +4900,45 @@ bool wxRichTextBuffer::InsertNewlineWithUndo(long pos, wxRichTextCtrl* ctrl, int
     action->GetNewParagraphs().AppendChild(newPara);
     action->GetNewParagraphs().UpdateRanges();
     action->GetNewParagraphs().SetPartialParagraph(false);
+    wxRichTextParagraph* para = GetParagraphAtPosition(pos, false);
+    long pos1 = pos;
+
+    if (flags & wxRICHTEXT_INSERT_INTERACTIVE)
+    {
+        if (para && para->GetRange().GetEnd() == pos)
+            pos1 ++;
+    }
+
     action->SetPosition(pos);
 
     if (p)
         newPara->SetAttributes(*p);
 
+    // Use the default character style
     // Use the default character style
     if (!GetDefaultStyle().IsDefault() && newPara->GetChildren().GetFirst())
-        newPara->GetChildren().GetFirst()->GetData()->SetAttributes(GetDefaultStyle());
+    {
+        // Check whether the default style merely reflects the paragraph/basic style,
+        // in which case don't apply it.
+        wxTextAttrEx defaultStyle(GetDefaultStyle());
+        wxTextAttrEx toApply;
+        if (para)
+        {
+            wxRichTextAttr combinedAttr = para->GetCombinedAttributes();
+            wxTextAttrEx newAttr;
+            // This filters out attributes that are accounted for by the current
+            // paragraph/basic style
+            wxRichTextApplyStyle(toApply, defaultStyle, & combinedAttr);
+        }
+        else
+            toApply = defaultStyle;
+
+        if (!toApply.IsDefault())
+            newPara->GetChildren().GetFirst()->GetData()->SetAttributes(toApply);
+    }
 
     // Set the range we'll need to delete in Undo
-    action->SetRange(wxRichTextRange(pos, pos));
+    action->SetRange(wxRichTextRange(pos1, pos1));
 
     SubmitAction(action);
 
@@ -5065,7 +5090,7 @@ bool wxRichTextBuffer::BeginBatchUndo(const wxString& cmdName)
         wxASSERT(m_batchedCommand == NULL);
         if (m_batchedCommand)
         {
-            GetCommandProcessor()->Submit(m_batchedCommand);
+            GetCommandProcessor()->Store(m_batchedCommand);
         }
         m_batchedCommand = new wxRichTextCommand(cmdName);
     }
@@ -5085,7 +5110,7 @@ bool wxRichTextBuffer::EndBatchUndo()
 
     if (m_batchedCommandDepth == 0)
     {
-        GetCommandProcessor()->Submit(m_batchedCommand);
+        GetCommandProcessor()->Store(m_batchedCommand);
         m_batchedCommand = NULL;
     }
 
@@ -5096,7 +5121,15 @@ bool wxRichTextBuffer::EndBatchUndo()
 bool wxRichTextBuffer::SubmitAction(wxRichTextAction* action)
 {
     if (BatchingUndo() && m_batchedCommand && !SuppressingUndo())
+    {
+        wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName());
+        cmd->AddAction(action);
+        cmd->Do();
+        cmd->GetActions().Clear();
+        delete cmd;
+
         m_batchedCommand->AddAction(action);
+    }
     else
     {
         wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName());
@@ -6129,7 +6162,7 @@ bool wxRichTextAction::Do()
                 wxPoint firstVisiblePt = m_ctrl->GetFirstVisiblePoint();
                 int lastY = firstVisiblePt.y + clientSize.y;
 
-                wxRichTextParagraph* para = m_buffer->GetParagraphAtPosition(GetPosition());
+                wxRichTextParagraph* para = m_buffer->GetParagraphAtPosition(GetRange().GetStart());
                 wxRichTextObjectList::compatibility_iterator node = m_buffer->GetChildren().Find(para);
                 while (node)
                 {
@@ -6162,9 +6195,9 @@ bool wxRichTextAction::Do()
             }
 #endif
 
-            m_buffer->InsertFragment(GetPosition(), m_newParagraphs);
+            m_buffer->InsertFragment(GetRange().GetStart(), m_newParagraphs);
             m_buffer->UpdateRanges();
-            m_buffer->Invalidate(GetRange());
+            m_buffer->Invalidate(wxRichTextRange(GetRange().GetStart()-1, GetRange().GetEnd()));
 
             long newCaretPosition = GetPosition() + m_newParagraphs.GetRange().GetLength();