]> git.saurik.com Git - wxWidgets.git/blobdiff - src/richtext/richtextctrl.cpp
Removed #include <typeinfo> (since C++ RTTI is no longer used)
[wxWidgets.git] / src / richtext / richtextctrl.cpp
index c0ba3cbd98cc508c820db48a0875fa7d6682edfb..3a6432a531aea0d46864c62087893b9f079fdd08 100644 (file)
@@ -32,6 +32,7 @@
 #include "wx/dcbuffer.h"
 #include "wx/arrimpl.cpp"
 #include "wx/fontenum.h"
+#include "wx/accel.h"
 
 // DLL options compatibility check:
 #include "wx/app.h"
@@ -135,6 +136,7 @@ BEGIN_EVENT_TABLE( wxRichTextCtrl, wxControl )
     EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick)
     EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick)
     EVT_CHAR(wxRichTextCtrl::OnChar)
+    EVT_KEY_DOWN(wxRichTextCtrl::OnChar)
     EVT_SIZE(wxRichTextCtrl::OnSize)
     EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus)
     EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus)
@@ -215,8 +217,6 @@ bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& va
     attributes.SetLineSpacing(10);
     attributes.SetParagraphSpacingAfter(10);
     attributes.SetParagraphSpacingBefore(0);
-    attributes.SetTextEffects(0);
-    attributes.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH|wxTEXT_ATTR_EFFECT_CAPITALS);
 
     SetBasicStyle(attributes);
 
@@ -255,6 +255,17 @@ bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& va
 
     GetBuffer().AddEventHandler(this);
 
+    // Accelerators
+    wxAcceleratorEntry entries[4];
+
+    entries[0].Set(wxACCEL_CMD,   (int) 'C',       wxID_COPY);
+    entries[1].Set(wxACCEL_CMD,   (int) 'X',       wxID_CUT);
+    entries[2].Set(wxACCEL_CMD,   (int) 'V',       wxID_PASTE);
+    entries[3].Set(wxACCEL_CMD,   (int) 'A',       wxID_SELECTALL);
+
+    wxAcceleratorTable accel(4, entries);
+    SetAcceleratorTable(accel);
+
     return true;
 }
 
@@ -289,7 +300,8 @@ void wxRichTextCtrl::DoThaw()
         LayoutContent();
     else
         SetupScrollbars();
-    Refresh(false);
+    
+    wxWindow::DoThaw();
 }
 
 /// Clear all text
@@ -644,6 +656,65 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event)
     if (event.AltDown())
         flags |= wxRICHTEXT_ALT_DOWN;
 
+    if (event.GetEventType() == wxEVT_KEY_DOWN)
+    {
+        // Must process this before translation, otherwise it's translated into a WXK_DELETE event.
+        if (event.CmdDown() && event.GetKeyCode() == WXK_BACK)
+        {
+            BeginBatchUndo(_("Delete Text"));
+
+            long newPos = m_caretPosition;
+
+            DeleteSelectedContent(& newPos);
+
+            // Submit range in character positions, which are greater than caret positions,
+            // so subtract 1 for deleted character and add 1 for conversion to character position.
+            if (newPos > -1)
+            {
+                bool processed = false;
+                if (event.CmdDown())
+                {
+                    long pos = wxRichTextCtrl::FindNextWordPosition(-1);
+                    if (pos != -1 && (pos < newPos))
+                    {
+                        GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos+1, newPos), this);
+                        processed = true;
+                    }
+                }
+
+                if (!processed)
+                    GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos, newPos), this);
+            }
+
+            EndBatchUndo();
+
+            if (GetLastPosition() == -1)
+            {
+                GetBuffer().Reset();
+
+                m_caretPosition = -1;
+                PositionCaret();
+                SetDefaultStyleToCursorStyle();
+            }
+
+            ScrollIntoView(m_caretPosition, WXK_LEFT);
+
+            wxRichTextEvent cmdEvent(
+                wxEVT_COMMAND_RICHTEXT_DELETE,
+                GetId());
+            cmdEvent.SetEventObject(this);
+            cmdEvent.SetFlags(flags);
+            cmdEvent.SetPosition(m_caretPosition+1);
+            GetEventHandler()->ProcessEvent(cmdEvent);
+
+            Update();
+        }
+        else
+            event.Skip();
+
+        return;
+    }
+
     if (event.GetKeyCode() == WXK_LEFT ||
         event.GetKeyCode() == WXK_RIGHT ||
         event.GetKeyCode() == WXK_UP ||
@@ -687,6 +758,8 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event)
             wxString text;
             text = wxRichTextLineBreakChar;
             GetBuffer().InsertTextWithUndo(newPos+1, text, this);
+            m_caretAtLineStart = true;
+            PositionCaret();
         }
         else
             GetBuffer().InsertNewlineWithUndo(newPos+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE|wxRICHTEXT_INSERT_INTERACTIVE);
@@ -717,26 +790,28 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event)
     {
         BeginBatchUndo(_("Delete Text"));
 
+        long newPos = m_caretPosition;
+
+        DeleteSelectedContent(& newPos);
+
         // Submit range in character positions, which are greater than caret positions,
         // so subtract 1 for deleted character and add 1 for conversion to character position.
-        if (m_caretPosition > -1 && !HasSelection())
+        if (newPos > -1)
         {
             bool processed = false;
             if (event.CmdDown())
             {
                 long pos = wxRichTextCtrl::FindNextWordPosition(-1);
-                if (pos != -1 && (pos < m_caretPosition))
+                if (pos != -1 && (pos < newPos))
                 {
-                    GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos+1, m_caretPosition), this);
+                    GetBuffer().DeleteRangeWithUndo(wxRichTextRange(pos+1, newPos), this);
                     processed = true;
                 }
             }
 
             if (!processed)
-                GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition, m_caretPosition), this);
+                GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos, newPos), this);
         }
-        else
-            DeleteSelectedContent();
 
         EndBatchUndo();
 
@@ -765,13 +840,27 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event)
     {
         BeginBatchUndo(_("Delete Text"));
 
+        long newPos = m_caretPosition;
+
+        DeleteSelectedContent(& newPos);
+
         // Submit range in character positions, which are greater than caret positions,
-        if (m_caretPosition < GetBuffer().GetRange().GetEnd()+1 && !HasSelection())
+        if (newPos < GetBuffer().GetRange().GetEnd()+1)
         {
-            GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition+1, m_caretPosition+1), this);
+            bool processed = false;
+            if (event.CmdDown())
+            {
+                long pos = wxRichTextCtrl::FindNextWordPosition(1);
+                if (pos != -1 && (pos > newPos))
+                {
+                    GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos+1, pos), this);
+                    processed = true;
+                }
+            }
+
+            if (!processed)
+                GetBuffer().DeleteRangeWithUndo(wxRichTextRange(newPos+1, newPos+1), this);
         }
-        else
-            DeleteSelectedContent();
 
         EndBatchUndo();
 
@@ -1085,6 +1174,9 @@ bool wxRichTextCtrl::ExtendSelection(long oldPos, long newPos, int flags)
 {
     if (flags & wxRICHTEXT_SHIFT_DOWN)
     {
+        if (oldPos == newPos)
+            return false;
+
         wxRichTextRange oldSelection = m_selectionRange;
 
         // If not currently selecting, start selecting
@@ -1103,6 +1195,8 @@ bool wxRichTextCtrl::ExtendSelection(long oldPos, long newPos, int flags)
             // the end.
             if (newPos > m_selectionAnchor)
                 m_selectionRange.SetRange(m_selectionAnchor+1, newPos);
+            else if (newPos == m_selectionAnchor)
+                m_selectionRange = wxRichTextRange(-2, -2);
             else
                 m_selectionRange.SetRange(newPos+1, m_selectionAnchor);
         }
@@ -1978,7 +2072,7 @@ bool wxRichTextCtrl::RecreateBuffer(const wxSize& size)
 
 bool wxRichTextCtrl::DoLoadFile(const wxString& filename, int fileType)
 {
-    bool success = GetBuffer().LoadFile(filename, fileType);
+    bool success = GetBuffer().LoadFile(filename, (wxRichTextFileType)fileType);
     if (success)
         m_filename = filename;
 
@@ -2002,7 +2096,7 @@ bool wxRichTextCtrl::DoLoadFile(const wxString& filename, int fileType)
 
 bool wxRichTextCtrl::DoSaveFile(const wxString& filename, int fileType)
 {
-    if (GetBuffer().SaveFile(filename, fileType))
+    if (GetBuffer().SaveFile(filename, (wxRichTextFileType)fileType))
     {
         m_filename = filename;
 
@@ -2245,7 +2339,7 @@ void wxRichTextCtrl::AppendText(const wxString& text)
 }
 
 /// Write an image at the current insertion point
-bool wxRichTextCtrl::WriteImage(const wxImage& image, int bitmapType)
+bool wxRichTextCtrl::WriteImage(const wxImage& image, wxBitmapType bitmapType)
 {
     wxRichTextImageBlock imageBlock;
 
@@ -2256,7 +2350,7 @@ bool wxRichTextCtrl::WriteImage(const wxImage& image, int bitmapType)
     return false;
 }
 
-bool wxRichTextCtrl::WriteImage(const wxString& filename, int bitmapType)
+bool wxRichTextCtrl::WriteImage(const wxString& filename, wxBitmapType bitmapType)
 {
     wxRichTextImageBlock imageBlock;
 
@@ -2272,7 +2366,7 @@ bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock)
     return GetBuffer().InsertImageWithUndo(m_caretPosition+1, imageBlock, this);
 }
 
-bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, int bitmapType)
+bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, wxBitmapType bitmapType)
 {
     if (bitmap.Ok())
     {
@@ -2289,7 +2383,7 @@ bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, int bitmapType)
 /// Insert a newline (actually paragraph) at the current insertion point.
 bool wxRichTextCtrl::Newline()
 {
-    return GetBuffer().InsertNewlineWithUndo(m_caretPosition+1, this);
+    return GetBuffer().InsertNewlineWithUndo(m_caretPosition+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE);
 }
 
 /// Insert a line break at the current insertion point.
@@ -2443,7 +2537,6 @@ void wxRichTextCtrl::SetSelection(long from, long to)
     }
 
     DoSetSelection(from, to);
-    SetDefaultStyleToCursorStyle();
 }
 
 void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret))