From cd8ba0d9e4f7f3a650cffff4dd99c11b75a86ceb Mon Sep 17 00:00:00 2001
From: Julian Smart <julian@anthemion.co.uk>
Date: Fri, 1 Feb 2008 14:14:44 +0000
Subject: [PATCH] Added a reset-buffer event to give an opportunity to set the
 default initial style

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@51494 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/richtextevent.tex    |  1 +
 include/wx/richtext/richtextctrl.h |  2 ++
 src/richtext/richtextbuffer.cpp    |  9 +++++++++
 src/richtext/richtextctrl.cpp      | 13 +++++++------
 4 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/docs/latex/wx/richtextevent.tex b/docs/latex/wx/richtextevent.tex
index 50b7b5736b..00bdc9e759 100644
--- a/docs/latex/wx/richtextevent.tex
+++ b/docs/latex/wx/richtextevent.tex
@@ -24,6 +24,7 @@ function that takes a wxRichTextEvent argument.
 \twocolitem{{\bf EVT\_RICHTEXT\_STYLESHEET\_REPLACED(id, func)}}{Process a wxEVT\_COMMAND\_RICHTEXT\_STYLESHEET\_REPLACED event, generated when the control's stylesheet has been replaced, for example when a file is loaded into the control. Valid event functions: GetOldStyleSheet, GetNewStyleSheet.}
 \twocolitem{{\bf EVT\_RICHTEXT\_CONTENT\_INSERTED(id, func)}}{Process a wxEVT\_COMMAND\_RICHTEXT\_CONTENT\_INSERTED event, generated when content has been inserted into the control. Valid event functions: GetPosition, GetRange.}
 \twocolitem{{\bf EVT\_RICHTEXT\_CONTENT\_DELETED(id, func)}}{Process a wxEVT\_COMMAND\_RICHTEXT\_CONTENT\_DELETED event, generated when content has been deleted from the control. Valid event functions: GetPosition, GetRange.}
+\twocolitem{{\bf EVT\_RICHTEXT\_BUFFER\_RESET(id, func)}}{Process a wxEVT\_COMMAND\_RICHTEXT\_BUFFER\_RESET event, generated when the buffer has been reset by deleting all content. You can use this to set a default style for the first new paragraph.}
 \end{twocollist}%
  
 \wxheading{Derived from}
diff --git a/include/wx/richtext/richtextctrl.h b/include/wx/richtext/richtextctrl.h
index 3786368845..aa17c9f7d9 100644
--- a/include/wx/richtext/richtextctrl.h
+++ b/include/wx/richtext/richtextctrl.h
@@ -894,6 +894,7 @@ extern WXDLLIMPEXP_RICHTEXT const wxEventType wxEVT_COMMAND_RICHTEXT_CONTENT_INS
 extern WXDLLIMPEXP_RICHTEXT const wxEventType wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED;
 extern WXDLLIMPEXP_RICHTEXT const wxEventType wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED;
 extern WXDLLIMPEXP_RICHTEXT const wxEventType wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED;
+extern WXDLLIMPEXP_RICHTEXT const wxEventType wxEVT_COMMAND_RICHTEXT_BUFFER_RESET;
 
 typedef void (wxEvtHandler::*wxRichTextEventFunction)(wxRichTextEvent&);
 
@@ -917,6 +918,7 @@ typedef void (wxEvtHandler::*wxRichTextEventFunction)(wxRichTextEvent&);
 #define EVT_RICHTEXT_CONTENT_DELETED(id, fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED, id, -1, (wxObjectEventFunction) (wxEventFunction)  wxStaticCastEvent( wxRichTextEventFunction, & fn ), NULL ),
 #define EVT_RICHTEXT_STYLE_CHANGED(id, fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction)  wxStaticCastEvent( wxRichTextEventFunction, & fn ), NULL ),
 #define EVT_RICHTEXT_SELECTION_CHANGED(id, fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction)  wxStaticCastEvent( wxRichTextEventFunction, & fn ), NULL ),
+#define EVT_RICHTEXT_BUFFER_RESET(id, fn) DECLARE_EVENT_TABLE_ENTRY( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET, id, -1, (wxObjectEventFunction) (wxEventFunction)  wxStaticCastEvent( wxRichTextEventFunction, & fn ), NULL ),
 
 #endif
     // wxUSE_RICHTEXT
diff --git a/src/richtext/richtextbuffer.cpp b/src/richtext/richtextbuffer.cpp
index 651b5ac107..1936bb107a 100644
--- a/src/richtext/richtextbuffer.cpp
+++ b/src/richtext/richtextbuffer.cpp
@@ -2419,6 +2419,15 @@ void wxRichTextParagraphLayoutBox::Reset()
 {
     Clear();
 
+    wxRichTextBuffer* buffer = wxDynamicCast(this, wxRichTextBuffer);
+    if (buffer && GetRichTextCtrl())
+    {
+        wxRichTextEvent event(wxEVT_COMMAND_RICHTEXT_BUFFER_RESET, GetRichTextCtrl()->GetId());
+        event.SetEventObject(GetRichTextCtrl());
+
+        buffer->SendEvent(event, true);
+    }
+
     AddParagraph(wxEmptyString);
 
     Invalidate(wxRICHTEXT_ALL);
diff --git a/src/richtext/richtextctrl.cpp b/src/richtext/richtextctrl.cpp
index c5ac3408e9..f577276abf 100644
--- a/src/richtext/richtextctrl.cpp
+++ b/src/richtext/richtextctrl.cpp
@@ -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);
 
-- 
2.50.0

