Permalink
Browse files

Added Modification event.

  • Loading branch information...
1 parent 40fed1a commit 5c3df8a0d1495f5ed660c0b14ebe7ee382929e3b @cmrazek committed Jul 28, 2012
View
4 NppSharp/Main.cpp
@@ -269,6 +269,10 @@ namespace NppSharp
(pNotify->modifiers & SCMOD_ALT) != 0,
(pNotify->modifiers & SCMOD_SHIFT) != 0);
break;
+
+ case SCN_MODIFIED:
+ g.npp->OnModified(pNotify);
+ break;
}
}
catch (Exception^ ex)
View
25 NppSharp/NppInterface.cpp
@@ -18,6 +18,7 @@
#include "NppInterface.h"
#include <vcclr.h>
#include "OutputWindow.h"
+#include "TextPtrA.h"
namespace NppSharp
{
@@ -215,6 +216,30 @@ namespace NppSharp
DoubleClick(this, gcnew DoubleClickEventArgs(pos, ctrl, alt, shift));
}
+ void NppInterface::OnModified(npp::SCNotification *pNotify)
+ {
+ if (pNotify->modificationType & (SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT))
+ {
+ int codePage;
+ if (pNotify->nmhdr.hwndFrom == NULL) codePage = 0;
+ else codePage = ::SendMessageA((HWND)pNotify->nmhdr.hwndFrom, SCI_GETCODEPAGE, 0, 0);
+
+ TextPtrA^ textPtr = gcnew TextPtrA(pNotify->text, pNotify->length, codePage);
+
+ // ModifiedEventArgs(ModificationType type, string text, TextLocation location, bool userAction, bool undo, bool redo, int linesAdded)
+ ModifiedEventArgs^ e = gcnew ModifiedEventArgs(
+ (pNotify->modificationType & SC_MOD_INSERTTEXT) ? ModificationType::Insert : ModificationType::Delete,
+ textPtr,
+ TextLocation::FromByteOffset(pNotify->position),
+ (pNotify->modificationType & SC_PERFORMED_USER) != 0,
+ (pNotify->modificationType & SC_PERFORMED_UNDO) != 0,
+ (pNotify->modificationType & SC_PERFORMED_REDO) != 0,
+ pNotify->linesAdded);
+
+ Modification(this, e);
+ }
+ }
+
void NppInterface::ExecuteCommandByIndex(int cmdIndex)
{
if (cmdIndex < 0 || cmdIndex >= _commands->Count) return;
View
2 NppSharp/NppInterface.h
@@ -132,6 +132,7 @@ namespace NppSharp
void OnFileOrderChanged(unsigned int bufferId);
void OnCharAdded(int ch);
void OnDoubleClick(int pos, bool ctrl, bool alt, bool shift);
+ void OnModified(npp::SCNotification *pNotify);
virtual event NppEventHandler^ GetCommands;
virtual event NppEventHandler^ RegisterToolbarIcons;
@@ -152,6 +153,7 @@ namespace NppSharp
virtual event ExecuteCommandEventHandler^ CommandExecuted;
virtual event CharAddedEventHandler^ CharAdded;
virtual event DoubleClickEventHandler^ DoubleClick;
+ virtual event ModifiedEventHandler^ Modification;
virtual void ShowOutputWindow();
virtual void HideOutputWindow();
View
2 NppSharp/NppSharp.vcxproj
@@ -141,6 +141,7 @@
<ClInclude Include="LexerLine.h" />
<ClInclude Include="LexerWrapper.h" />
<ClInclude Include="OutputWindow.h" />
+ <ClInclude Include="TextPtrA.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="AssemblyInfo.cpp" />
@@ -161,6 +162,7 @@
<ClCompile Include="Npp_Lexer.cpp" />
<ClCompile Include="Npp_OutputWindow.cpp" />
<ClCompile Include="OutputWindow.cpp" />
+ <ClCompile Include="TextPtrA.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
View
6 NppSharp/NppSharp.vcxproj.filters
@@ -96,6 +96,9 @@
<ClInclude Include="OutputWindow.h">
<Filter>OutputWindow</Filter>
</ClInclude>
+ <ClInclude Include="TextPtrA.h">
+ <Filter>Core</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="AssemblyInfo.cpp">
@@ -143,5 +146,8 @@
<ClCompile Include="Npp_AutoCompletion.cpp">
<Filter>AutoCompletion</Filter>
</ClCompile>
+ <ClCompile Include="TextPtrA.cpp">
+ <Filter>Core</Filter>
+ </ClCompile>
</ItemGroup>
</Project>
View
47 NppSharp/TextPtrA.cpp
@@ -0,0 +1,47 @@
+// NppSharp - C#/.NET Scripting Plugin for Notepad++
+// Copyright (C) 2012 Chris Mrazek
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#include "Stdafx.h"
+#include "TextPtrA.h"
+#include "ClrUtil.h"
+
+using namespace System::Runtime::InteropServices;
+
+namespace NppSharp
+{
+ TextPtrA::TextPtrA(const char* ptr, int length, int codePage)
+ : _ptr(ptr)
+ , _length(length)
+ , _codePage(codePage)
+ , _str(nullptr)
+ {
+ }
+
+ String^ TextPtrA::Text::get()
+ {
+ if (_str != nullptr) return _str;
+
+ if (_codePage == SC_CP_UTF8)
+ {
+ _str = NativeUtf8ToClrString(_ptr, _length);
+ }
+ else
+ {
+ _str = Marshal::PtrToStringAnsi((IntPtr)(void*)_ptr, _length);
+ }
+ return _str;
+ }
+}
View
34 NppSharp/TextPtrA.h
@@ -0,0 +1,34 @@
+// NppSharp - C#/.NET Scripting Plugin for Notepad++
+// Copyright (C) 2012 Chris Mrazek
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#pragma once
+
+namespace NppSharp
+{
+ public ref class TextPtrA : public TextPtr
+ {
+ public:
+ TextPtrA(const char* ptr, int length, int codePage);
+
+ virtual property String^ Text { String^ get() override; }
+
+ private:
+ const char* _ptr;
+ int _length;
+ int _codePage;
+ String^ _str;
+ };
+}
View
90 NppSharpCS/INpp.cs
@@ -187,6 +187,11 @@ public interface INpp
/// Triggered when the user double-clicks inside a document.
/// </summary>
event DoubleClickEventHandler DoubleClick;
+
+ /// <summary>
+ /// Triggered when the active document is modified.
+ /// </summary>
+ event ModifiedEventHandler Modification;
#endregion
#region Editor
@@ -975,6 +980,91 @@ public DoubleClickEventArgs(int pos, bool ctrl, bool alt, bool shift)
/// </summary>
public bool Shift { get; private set; }
}
+
+ /// <summary>
+ /// A type of document modification performed.
+ /// </summary>
+ public enum ModificationType
+ {
+ /// <summary>
+ /// Text was inserted into the document.
+ /// </summary>
+ Insert,
+
+ /// <summary>
+ /// Text was deleted from the document.
+ /// </summary>
+ Delete
+ }
+
+ /// <summary>
+ /// Handler for modified events.
+ /// </summary>
+ /// <param name="sender">Object that sends the event.</param>
+ /// <param name="e">Arguments provided for this event.</param>
+ public delegate void ModifiedEventHandler(object sender, ModifiedEventArgs e);
+
+ /// <summary>
+ /// Arguments for modified events.
+ /// </summary>
+ public class ModifiedEventArgs : EventArgs
+ {
+ /// <summary>
+ /// Gets the type of modification that occurred.
+ /// </summary>
+ public ModificationType ModificationType { get; private set; }
+
+ /// <summary>
+ /// Gets the text inserted or deleted.
+ /// </summary>
+ public TextPtr Text { get; private set; }
+
+ /// <summary>
+ /// Gets the location of the modification.
+ /// </summary>
+ public TextLocation Location { get; private set; }
+
+ /// <summary>
+ /// Gets a value indicating if this action was caused by the user.
+ /// </summary>
+ public bool UserAction { get; private set; }
+
+ /// <summary>
+ /// Gets a value indicating if this action is part of an undo process.
+ /// </summary>
+ public bool Undo { get; private set; }
+
+ /// <summary>
+ /// Gets a value indicating if this action is part of a redo process.
+ /// </summary>
+ public bool Redo { get; private set; }
+
+ /// <summary>
+ /// Gets the number of lines added by this modification.
+ /// </summary>
+ public int LinesAdded { get; private set; }
+
+ /// <summary>
+ /// Creates the event arguments object.
+ /// </summary>
+ /// <param name="type">The type of modification that occurred.</param>
+ /// <param name="text">The text inserted or deleted.</param>
+ /// <param name="location">The location of the modification.</param>
+ /// <param name="userAction">A flag indicating if this action was caused by the user.</param>
+ /// <param name="undo">A flag indicating if this action is part of an undo process.</param>
+ /// <param name="redo">A flag indicating if this action is part of a redo process.</param>
+ /// <param name="linesAdded">The number of lines added by this modification.</param>
+ public ModifiedEventArgs(ModificationType type, TextPtr text, TextLocation location, bool userAction, bool undo, bool redo, int linesAdded)
+ {
+ ModificationType = type;
+ Text = text;
+ Location = location;
+ UserAction = userAction;
+ Undo = undo;
+ Redo = redo;
+ LinesAdded = linesAdded;
+ }
+ }
#endregion
/// <summary>
View
12 NppSharpCS/NppScript.cs
@@ -885,6 +885,11 @@ public void MoveSelectedLinesDown()
/// </summary>
public event DoubleClickEventHandler DoubleClick;
+ /// <summary>
+ /// Triggered when the active document is modified.
+ /// </summary>
+ public event ModifiedEventHandler Modification;
+
internal void InitEvents()
{
INpp npp = Plugin.NppIntf;
@@ -905,6 +910,7 @@ internal void InitEvents()
npp.FileOrderChanged += OnFileOrderChanged;
npp.CharAdded += OnCharAdded;
npp.DoubleClick += OnDoubleClick;
+ npp.Modification += OnModification;
}
/// <summary>
@@ -1010,6 +1016,12 @@ internal void OnDoubleClick(object sender, DoubleClickEventArgs e)
DoubleClickEventHandler ev = DoubleClick;
if (ev != null) ev(this, e);
}
+
+ internal void OnModification(object sender, ModifiedEventArgs e)
+ {
+ ModifiedEventHandler ev = Modification;
+ if (ev != null) ev(this, e);
+ }
#endregion
#region Dock Window
View
1 NppSharpCS/NppSharpCS.csproj
@@ -76,6 +76,7 @@
<Compile Include="IDockWindow.cs" />
<Compile Include="ILexer.cs" />
<Compile Include="ILexerLine.cs" />
+ <Compile Include="TextPtr.cs" />
<Compile Include="LexerCommentsAttribute.cs" />
<Compile Include="LexerException.cs" />
<Compile Include="LexerStyle.cs" />
View
28 NppSharpCS/TextPtr.cs
@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace NppSharp
+{
+ /// <summary>
+ /// Represents text stored using a native ptr.
+ /// </summary>
+ /// <remarks>This text will only be converted to a CLR string upon request, improving performance when its not needed.</remarks>
+ public abstract class TextPtr
+ {
+ /// <summary>
+ /// Gets the text at the pointer.
+ /// </summary>
+ public abstract string Text { get; }
+
+ /// <summary>
+ /// Gets the text at the pointer.
+ /// </summary>
+ /// <returns>The text at the pointer.</returns>
+ public override string ToString()
+ {
+ return this.Text;
+ }
+ }
+}

0 comments on commit 5c3df8a

Please sign in to comment.