OSDN Git Service

DocumentクラスをPCLでも使えるようにした。これに伴い、ファイル操作はIStreamWriter,IStreamReaderインターフェイスを通じて行うようにした...
authorkonekoneko <test2214@hotmail.co.jp>
Sat, 8 Feb 2014 15:45:16 +0000 (00:45 +0900)
committerkonekoneko <test2214@hotmail.co.jp>
Sat, 8 Feb 2014 15:45:16 +0000 (00:45 +0900)
12 files changed:
Common/Document.cs
Common/DocumentExtend.cs [deleted file]
Common/WinFileStream.cs [new file with mode: 0644]
Metro/FooEditEngine/FooEditEngine.csproj
Metro/FooEditEngine/FooTextBox.cs
Metro/Test/MainPage.xaml.cs
WPF/FooEditEngine/FooEditEngine.csproj
WPF/FooEditEngine/FooTextBox.cs
WPF/Test/MainWindow.xaml.cs
Windows/FooEditEngine/FooEditEngine.csproj
Windows/FooEditEngine/FooTextBox.cs
Windows/Test/Form1.cs

index 8420015..28756a7 100644 (file)
@@ -9,6 +9,8 @@
 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.\r
  */\r
 \r
+//#define TEST_ASYNC\r
+\r
 using System;\r
 using System.IO;\r
 using System.Collections.Generic;\r
@@ -447,6 +449,88 @@ namespace FooEditEngine
         }\r
 \r
         /// <summary>\r
+        /// ストリームからドキュメントを非同期的に構築します\r
+        /// </summary>\r
+        /// <param name="fs">IStreamReaderオブジェクト</param>\r
+        /// <param name="tokenSource">キャンセルトークン</param>\r
+        /// <returns>Taskオブジェクト</returns>\r
+        /// <remarks>\r
+        /// 読み取り操作は別スレッドで行われます。\r
+        /// また、非同期操作中はこのメソッドを実行することはできません。\r
+        /// </remarks>\r
+        internal async Task LoadAsync(IStreamReader fs, CancellationTokenSource tokenSource = null)\r
+        {\r
+            if (fs.IsEnd())\r
+                return;\r
+\r
+            try\r
+            {\r
+                await this.LockAsync().ConfigureAwait(false);\r
+                this.Clear();\r
+                this.FireUpdateEvent = false;\r
+                this.UndoManager.BeginLock();\r
+                string str;\r
+                for (int i = 0; (str = await fs.ReadLineAsync().ConfigureAwait(false)) != null; i++)\r
+                {\r
+                    int index = this.Length;\r
+                    if (index < 0)\r
+                        index = 0;\r
+\r
+                    this.Replace(index, 0, str + Document.NewLine);\r
+\r
+                    if (tokenSource != null)\r
+                        tokenSource.Token.ThrowIfCancellationRequested();\r
+#if TEST_ASYNC\r
+                    System.Threading.Thread.Sleep(10);\r
+#endif\r
+                }\r
+            }\r
+            finally\r
+            {\r
+                this.FireUpdateEvent = true;\r
+                this.UndoManager.EndLock();\r
+                this.UnLock();\r
+            }\r
+        }\r
+\r
+        /// <summary>\r
+        /// ストリームに非同期モードで保存します\r
+        /// </summary>\r
+        /// <param name="fs">IStreamWriterオブジェクト</param>\r
+        /// <param name="tokenSource">キャンセルトークン</param>\r
+        /// <returns>Taskオブジェクト</returns>\r
+        /// <remarks>非同期操作中はこのメソッドを実行することはできません</remarks>\r
+        internal async Task SaveAsync(IStreamWriter fs, CancellationTokenSource tokenSource = null)\r
+        {\r
+            try\r
+            {\r
+                await this.LockAsync().ConfigureAwait(false);\r
+                StringBuilder line = new StringBuilder();\r
+                for (int i = 0; i < this.Length; i++)\r
+                {\r
+                    char c = this[i];\r
+                    line.Append(c);\r
+                    if (c == Document.NewLine || i == this.Length - 1)\r
+                    {\r
+                        string str = line.ToString();\r
+                        str = str.Replace(Document.NewLine.ToString(), fs.NewLine);\r
+                        await fs.WriteAsync(str).ConfigureAwait(false);\r
+                        line.Clear();\r
+                        if (tokenSource != null)\r
+                            tokenSource.Token.ThrowIfCancellationRequested();\r
+#if TEST_ASYNC\r
+                    System.Threading.Thread.Sleep(10);\r
+#endif\r
+                    }\r
+                }\r
+            }\r
+            finally\r
+            {\r
+                this.UnLock();\r
+            }\r
+        }\r
+\r
+        /// <summary>\r
         /// Find()およびReplaceAll()で使用するパラメーターをセットします\r
         /// </summary>\r
         /// <param name="pattern">検索したい文字列</param>\r
@@ -595,6 +679,36 @@ namespace FooEditEngine
         }\r
     }\r
 \r
+    public interface IStreamReader\r
+    {\r
+        /// <summary>\r
+        /// ストリームが空かどうかを返す\r
+        /// </summary>\r
+        bool IsEnd();\r
+\r
+        /// <summary>\r
+        /// ストリームから行を読み取った物を返す。LoadAsyncを呼び出す場合は必ず実装してください\r
+        /// </summary>\r
+        Task<string> ReadLineAsync();\r
+    }\r
+\r
+    public interface IStreamWriter\r
+    {\r
+        /// <summary>\r
+        /// ストリームに書き込む。SaveAsyncを呼び出す場合は必ず実装してください\r
+        /// </summary>\r
+        Task WriteAsync(string str);\r
+\r
+        /// <summary>\r
+        /// 書き込む際に使用する改行コード\r
+        /// </summary>\r
+        string NewLine\r
+        {\r
+            get;\r
+            set;\r
+        }\r
+    }\r
+\r
     /// <summary>\r
     /// 検索結果を表す\r
     /// </summary>\r
diff --git a/Common/DocumentExtend.cs b/Common/DocumentExtend.cs
deleted file mode 100644 (file)
index 69c8b98..0000000
+++ /dev/null
@@ -1,225 +0,0 @@
-/*\r
- * Copyright (C) 2013 FooProject\r
- * * 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\r
- * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.\r
-\r
- * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of \r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.\r
- */\r
-\r
-//#define TEST_ASYNC\r
-\r
-using System;\r
-using System.IO;\r
-using System.Text;\r
-using System.Threading;\r
-using System.Threading.Tasks;\r
-\r
-namespace FooEditEngine\r
-{\r
-    /// <summary>\r
-    /// Documentの拡張クラス\r
-    /// </summary>\r
-    public static class DocumentExtend\r
-    {\r
-        /// <summary>\r
-        /// 進捗処理を表す\r
-        /// </summary>\r
-        public static event EventHandler<ProgressEventArgs> Progress;\r
-\r
-\r
-#if !METRO\r
-        /// <summary>\r
-        /// ファイルからドキュメントを構築します\r
-        /// </summary>\r
-        /// <param name="doc">Documentオブジェクト</param>\r
-        /// <param name="filepath">読み取り先のファイル</param>\r
-        /// <param name="enc">エンコーディング</param>\r
-        /// <remarks>\r
-        /// 非同期操作中はこのメソッドを実行することはできません。\r
-        /// </remarks>\r
-        public static void Load(this Document doc, string filepath, Encoding enc)\r
-        {\r
-            using (StreamReader sr = new StreamReader(filepath, enc))\r
-            {\r
-                Load(doc,sr);\r
-            }\r
-        }\r
-\r
-        /// <summary>\r
-        /// ファイルからドキュメントを非同期的に構築します\r
-        /// </summary>\r
-        /// <param name="doc">Documentオブジェクト</param>\r
-        /// <param name="filepath">読み取り先のファイル</param>\r
-        /// <param name="enc">エンコーディング</param>\r
-        /// <param name="token">キャンセル用のトークン</param>\r
-        /// <remarks>\r
-        /// 読み取り操作は別スレッドで行われます。\r
-        /// また、非同期操作中にこのメソッドを読みだすことはできません\r
-        /// </remarks>\r
-        public static async Task LoadAsync(this Document doc, string filepath, Encoding enc, CancellationTokenSource token)\r
-        {\r
-            using (StreamReader sr = new StreamReader(filepath, enc))\r
-            {\r
-                await LoadAsync(doc,sr, token);\r
-            }\r
-        }\r
-#endif\r
-\r
-        /// <summary>\r
-        /// ストリームからドキュメントを構築します\r
-        /// </summary>\r
-        /// <param name="doc">Documentオブジェクト</param>\r
-        /// <param name="sr">読み取り先のストリーム</param>\r
-        /// <remarks>\r
-        /// 非同期操作中はこのメソッドを実行することはできません\r
-        /// </remarks>\r
-        public static void Load(this Document doc, TextReader sr)\r
-        {\r
-            Task t = LoadAsync(doc,sr, null);\r
-            t.Wait();\r
-        }\r
-\r
-        /// <summary>\r
-        /// ストリームからドキュメントを非同期的に構築します\r
-        /// </summary>\r
-        /// <param name="doc">Documentオブジェクト</param>\r
-        /// <param name="sr">読み取り先のストリーム</param>\r
-        /// <param name="tokenSource">キャンセルトークン</param>\r
-        /// <returns>Taskオブジェクト</returns>\r
-        /// <remarks>\r
-        /// 読み取り操作は別スレッドで行われます。\r
-        /// また、非同期操作中はこのメソッドを実行することはできません。\r
-        /// </remarks>\r
-        public static async Task LoadAsync(this Document doc, TextReader sr, CancellationTokenSource tokenSource = null)\r
-        {\r
-            if (sr.Peek() == -1)\r
-            {\r
-                return;\r
-            }\r
-\r
-            try\r
-            {\r
-                await doc.LockAsync().ConfigureAwait(false);\r
-                Progress(doc,new ProgressEventArgs(ProgressState.Start));\r
-                doc.Clear();\r
-                doc.FireUpdateEvent = false;\r
-                doc.UndoManager.BeginLock();\r
-                string str;\r
-                for (int i = 0; (str = await sr.ReadLineAsync().ConfigureAwait(false)) != null; i++)\r
-                {\r
-                    int index = doc.Length;\r
-                    if (index < 0)\r
-                        index = 0;\r
-\r
-                    doc.Replace(index,0,str + Document.NewLine);\r
-\r
-                    if (tokenSource != null)\r
-                        tokenSource.Token.ThrowIfCancellationRequested();\r
-#if TEST_ASYNC\r
-                    System.Threading.Thread.Sleep(10);\r
-#endif\r
-                }\r
-            }\r
-            finally\r
-            {\r
-                doc.FireUpdateEvent = true;\r
-                doc.UndoManager.EndLock();\r
-                Progress(doc, new ProgressEventArgs(ProgressState.Complete));\r
-                doc.UnLock();\r
-            }\r
-        }\r
-\r
-#if !METRO\r
-        /// <summary>\r
-        /// ドキュメントをファイルに保存します\r
-        /// </summary>\r
-        /// <param name="doc">Documentオブジェクト</param>\r
-        /// <param name="filepath">保存先のファイル</param>\r
-        /// <param name="enc">保存したいエンコード</param>\r
-        /// <param name="newline">改行を表す文字列</param>\r
-        /// <remarks>非同期操作中はこのメソッドを実行することはできません</remarks>\r
-        public static void Save(this Document doc, string filepath, Encoding enc, string newline)\r
-        {\r
-            using (StreamWriter sw = new StreamWriter(filepath, false, enc))\r
-            {\r
-                sw.NewLine = newline;\r
-                Save(doc,sw);\r
-            }\r
-        }\r
-\r
-        /// <summary>\r
-        /// ドキュメントをファイルに保存します\r
-        /// </summary>\r
-        /// <param name="doc">Documentオブジェクト</param>\r
-        /// <param name="filepath">保存先のファイル</param>\r
-        /// <param name="enc">保存したいエンコード</param>\r
-        /// <param name="newline">改行を表す文字列</param>\r
-        /// <param name="token">CancellationTokenSourceオブジェクト</param>\r
-        /// <remarks>非同期操作中はこのメソッドを実行することはできません</remarks>\r
-        public static async Task SaveAsync(this Document doc, string filepath, Encoding enc, string newline,CancellationTokenSource token)\r
-        {\r
-            using (StreamWriter sw = new StreamWriter(filepath, false, enc))\r
-            {\r
-                sw.NewLine = newline;\r
-                await SaveAsync(doc,sw, token);\r
-            }\r
-        }\r
-#endif\r
-\r
-        /// <summary>\r
-        /// ストリームに保存します\r
-        /// </summary>\r
-        /// <param name="doc">Documentオブジェクト</param>\r
-        /// <param name="sw">保存先のストリーム</param>\r
-        /// <returns>Taskオブジェクト</returns>\r
-        /// <remarks>非同期操作中はこのメソッドを実行することはできません</remarks>\r
-        public static void Save(this Document doc, StreamWriter sw)\r
-        {\r
-            Task t = SaveAsync(doc,sw, null);\r
-            t.Wait();\r
-        }\r
-\r
-        /// <summary>\r
-        /// ストリームに非同期モードで保存します\r
-        /// </summary>\r
-        /// <param name="doc">Documentオブジェクト</param>\r
-        /// <param name="sw">保存先のストリーム</param>\r
-        /// <param name="tokenSource">キャンセルトークン</param>\r
-        /// <returns>Taskオブジェクト</returns>\r
-        /// <remarks>非同期操作中はこのメソッドを実行することはできません</remarks>\r
-        public static async Task SaveAsync(this Document doc,StreamWriter sw, CancellationTokenSource tokenSource = null)\r
-        {\r
-            try\r
-            {\r
-                await doc.LockAsync().ConfigureAwait(false);\r
-                Progress(doc, new ProgressEventArgs(ProgressState.Start));\r
-                StringBuilder line = new StringBuilder();\r
-                for (int i = 0; i < doc.Length; i++)\r
-                {\r
-                    char c = doc[i];\r
-                    line.Append(c);\r
-                    if (c == Document.NewLine || i == doc.Length - 1)\r
-                    {\r
-                        string str = line.ToString();\r
-                        str = str.Replace(Document.NewLine.ToString(), sw.NewLine);\r
-                        await sw.WriteAsync(str).ConfigureAwait(false);\r
-                        line.Clear();\r
-                        if (tokenSource != null)\r
-                            tokenSource.Token.ThrowIfCancellationRequested();\r
-#if TEST_ASYNC\r
-                    System.Threading.Thread.Sleep(10);\r
-#endif\r
-                    }\r
-                }\r
-            }\r
-            finally\r
-            {\r
-                Progress(doc, new ProgressEventArgs(ProgressState.Complete));\r
-                doc.UnLock();\r
-            }\r
-        }\r
-    }\r
-}\r
diff --git a/Common/WinFileStream.cs b/Common/WinFileStream.cs
new file mode 100644 (file)
index 0000000..b867261
--- /dev/null
@@ -0,0 +1,82 @@
+using System;
+using System.Text;
+using System.IO;
+using System.Threading.Tasks;
+
+namespace FooEditEngine
+{
+    class WinFileWriter : IStreamWriter
+    {
+        StreamWriter sw;
+
+#if !METRO
+        public WinFileWriter(string filepath,Encoding enc)
+        {
+            this.sw = new StreamWriter(filepath,false, enc);
+        }
+#else
+        public WinFileWriter(StreamWriter sw)
+        {
+            this.sw = sw;
+        }
+#endif
+        
+        public Task WriteAsync(string str)
+        {
+            return this.sw.WriteAsync(str);
+        }
+
+        public string NewLine
+        {
+            get
+            {
+                return this.sw.NewLine;
+            }
+            set
+            {
+                this.sw.NewLine = value;
+            }
+        }
+
+        public void Close()
+        {
+#if !METRO
+            this.sw.Dispose();
+#else
+#endif
+        }
+    }
+    class WinFileReader : IStreamReader
+    {
+        StreamReader sr;
+#if !METRO
+        public WinFileReader(string filepath, Encoding enc)
+        {
+            this.sr = new StreamReader(filepath, enc);
+        }
+#else
+        public WinFileReader(StreamReader sr)
+        {
+            this.sr = sr;
+        }
+#endif
+
+        public bool IsEnd()
+        {
+            return this.sr.Peek() == -1;
+        }
+
+        public Task<string> ReadLineAsync()
+        {
+            return this.sr.ReadLineAsync();
+        }
+
+        public void Close()
+        {
+#if !METRO
+            this.sr.Dispose();
+#else
+#endif
+        }
+    }
+}
index 8eb694d..28583e0 100644 (file)
     <Compile Include="..\..\Common\Document.cs">
       <Link>Document.cs</Link>
     </Compile>
-    <Compile Include="..\..\Common\DocumentExtend.cs">
-      <Link>DocumentExtend.cs</Link>
-    </Compile>
     <Compile Include="..\..\Common\EditView.cs">
       <Link>EditView.cs</Link>
     </Compile>
     <Compile Include="..\..\Common\WatchDogPattern.cs">
       <Link>WatchDogPattern.cs</Link>
     </Compile>
+    <Compile Include="..\..\Common\WinFileStream.cs">
+      <Link>WinFileStream.cs</Link>
+    </Compile>
     <Compile Include="Direct2D\D2DRender.cs" />
     <Compile Include="Direct2D\D2DRenderBase.cs" />
     <Compile Include="FooPrintText.cs" />
index 63b4af7..d29790b 100644 (file)
@@ -9,6 +9,7 @@
 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.\r
  */\r
 using System;\r
+using System.Text;\r
 using System.ComponentModel;\r
 using System.Threading.Tasks;\r
 using Windows.ApplicationModel.Resources.Core;\r
@@ -72,7 +73,6 @@ namespace FooEditEngine.Metro
             this.textStore.CompositionEnded += textStore_CompositionEnded;\r
 \r
             this.Document = new Document();\r
-            DocumentExtend.Progress += Document_Progress;\r
 \r
             this.rectangle = new Windows.UI.Xaml.Shapes.Rectangle();\r
             this.rectangle.Margin = this.Padding;\r
@@ -145,7 +145,6 @@ namespace FooEditEngine.Metro
                 this.View.Dispose();\r
                 this.Render.Dispose();\r
             }\r
-            DocumentExtend.Progress -= this.Document_Progress;\r
         }\r
 \r
         /// <summary>\r
@@ -319,6 +318,35 @@ namespace FooEditEngine.Metro
             this.View.PerfomLayouts();\r
         }\r
 \r
+        /// <summary>\r
+        /// ファイルからドキュメントを構築する\r
+        /// </summary>\r
+        /// <param name="sr">StremReader</param>\r
+        /// <returns>Taskオブジェクト</returns>\r
+        public async Task LoadFileAsync(System.IO.StreamReader sr, System.Threading.CancellationTokenSource token)\r
+        {\r
+            this.View.LayoutLines.IsFrozneDirtyFlag = true;\r
+            WinFileReader fs = new WinFileReader(sr);\r
+            await this.Document.LoadAsync(fs, token);\r
+            this.View.LayoutLines.IsFrozneDirtyFlag = false;\r
+            TextStoreHelper.NotifyTextChanged(this.textStore, 0, 0, this.Document.Length);\r
+            if (this.verticalScrollBar != null)\r
+                this.verticalScrollBar.Maximum = this.View.LayoutLines.Count;\r
+            this.View.CalculateLineCountOnScreen();\r
+        }\r
+\r
+        /// <summary>\r
+        /// ドキュメントの内容をファイルに保存する\r
+        /// </summary>\r
+        /// <param name="sw">StreamWriter</param>\r
+        /// <param name="enc">エンコード</param>\r
+        /// <returns>Taskオブジェクト</returns>\r
+        public async Task SaveFile(System.IO.StreamWriter sw, System.Threading.CancellationTokenSource token)\r
+        {\r
+            WinFileWriter fs = new WinFileWriter(sw);\r
+            await this.Document.SaveAsync(fs, token);\r
+        }\r
+\r
         #region command\r
         void CopyCommand()\r
         {\r
@@ -1037,26 +1065,6 @@ namespace FooEditEngine.Metro
             TextStoreHelper.NotifyTextChanged(this.textStore, e.startIndex, e.removeLength, e.insertLength);\r
         }\r
 \r
-        async void Document_Progress(object sender, ProgressEventArgs e)\r
-        {\r
-            switch (e.state)\r
-            {\r
-                case ProgressState.Start:\r
-                    this.View.LayoutLines.IsFrozneDirtyFlag = true;\r
-                    break;\r
-                case ProgressState.Complete:\r
-                    this.View.LayoutLines.IsFrozneDirtyFlag = false;\r
-                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>\r
-                    {\r
-                        TextStoreHelper.NotifyTextChanged(this.textStore, 0, 0, this.Document.Length);\r
-                        if (this.verticalScrollBar != null)\r
-                            this.verticalScrollBar.Maximum = this.View.LayoutLines.Count;\r
-                    });\r
-                    this.View.CalculateLineCountOnScreen();\r
-                    break;\r
-            }\r
-        }\r
-\r
         void FooTextBox_Loaded(object sender, RoutedEventArgs e)\r
         {\r
             this.Focus(FocusState.Programmatic);\r
index 1c9329e..1ceeeed 100644 (file)
@@ -114,7 +114,7 @@ namespace Test
                 using (Stream stream = await file.OpenStreamForReadAsync())\r
                 using(StreamReader reader = new StreamReader(stream))\r
                 {\r
-                    await this.fooTextBox.Document.LoadAsync(reader);\r
+                    await this.fooTextBox.LoadFileAsync(reader,null);\r
                     this.fooTextBox.FoldingStrategy = new CharFoldingMethod('{', '}');\r
                     this.fooTextBox.LayoutLineCollection.GenerateFolding();\r
                     this.fooTextBox.IsEnabled = true;\r
@@ -135,7 +135,7 @@ namespace Test
                 using (Stream stream = await file.OpenStreamForWriteAsync())\r
                 using (StreamWriter writer = new StreamWriter(stream))\r
                 {\r
-                    await this.fooTextBox.Document.SaveAsync(writer);\r
+                    await this.fooTextBox.SaveFile(writer,null);\r
                 }\r
             }\r
         }\r
index c5f2d26..15ca30d 100644 (file)
     <Compile Include="..\..\Common\Document.cs">
       <Link>Document.cs</Link>
     </Compile>
-    <Compile Include="..\..\Common\DocumentExtend.cs">
-      <Link>DocumentExtend.cs</Link>
-    </Compile>
     <Compile Include="..\..\Common\EditView.cs">
       <Link>EditView.cs</Link>
     </Compile>
     <Compile Include="..\..\Common\WatchDogPattern.cs">
       <Link>WatchDogPattern.cs</Link>
     </Compile>
+    <Compile Include="..\..\Common\WinFileStream.cs">
+      <Link>WinFileStream.cs</Link>
+    </Compile>
     <Compile Include="Direct2D\D2DRender.cs" />
     <Compile Include="Direct2D\NativeMethods.cs" />
     <Compile Include="FooPrintText.cs" />
index add244d..697d6c0 100644 (file)
@@ -9,6 +9,8 @@
 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.\r
  */\r
 using System;\r
+using System.Text;\r
+using System.Threading.Tasks;\r
 using System.Runtime.InteropServices;\r
 using System.Windows;\r
 using System.Windows.Input;\r
@@ -85,7 +87,6 @@ namespace FooEditEngine.WPF
             this.textStore.CompositionEnded += textStore_CompositionEnded;\r
 \r
             this.Document = new Document();\r
-            DocumentExtend.Progress += new EventHandler<ProgressEventArgs>(Document_Progress);\r
 \r
             this.Render = new D2DRender(this, 200, 200,this.image);\r
             this.Render.ShowFullSpace = this.ShowFullSpace;\r
@@ -346,6 +347,38 @@ namespace FooEditEngine.WPF
         }\r
 \r
         /// <summary>\r
+        /// ファイルからドキュメントを構築する\r
+        /// </summary>\r
+        /// <param name="filepath">ファイルパス</param>\r
+        /// <param name="enc">エンコード</param>\r
+        /// <returns>Taskオブジェクト</returns>\r
+        public async Task LoadFileAsync(string filepath, Encoding enc,System.Threading.CancellationTokenSource token)\r
+        {\r
+            this.View.LayoutLines.IsFrozneDirtyFlag = true;\r
+            WinFileReader fs = new WinFileReader(filepath, enc);\r
+            await this.Document.LoadAsync(fs, token);\r
+            this.View.LayoutLines.IsFrozneDirtyFlag = false;\r
+            TextStoreHelper.NotifyTextChanged(this.textStore, 0, 0, this.Document.Length);\r
+            if (this.verticalScrollBar != null)\r
+                this.verticalScrollBar.Maximum = this.View.LayoutLines.Count;\r
+            this.View.CalculateLineCountOnScreen();\r
+        }\r
+\r
+        /// <summary>\r
+        /// ドキュメントの内容をファイルに保存する\r
+        /// </summary>\r
+        /// <param name="filepath">ファイルパス</param>\r
+        /// <param name="newLine">改行コード</param>\r
+        /// <param name="enc">エンコード</param>\r
+        /// <returns>Taskオブジェクト</returns>\r
+        public async Task SaveFile(string filepath, Encoding enc,string newLine, System.Threading.CancellationTokenSource token)\r
+        {\r
+            WinFileWriter fs = new WinFileWriter(filepath, enc);\r
+            fs.NewLine = newLine;\r
+            await this.Document.SaveAsync(fs, token);\r
+        }\r
+\r
+        /// <summary>\r
         /// アンマネージドリソースを開放する\r
         /// </summary>\r
         public void Dispose()\r
@@ -371,7 +404,6 @@ namespace FooEditEngine.WPF
                 this.Render.Dispose();\r
             }\r
             SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);\r
-            DocumentExtend.Progress -= this.Document_Progress;\r
         }\r
         \r
         void Refresh(Rectangle updateRect)\r
@@ -916,25 +948,6 @@ namespace FooEditEngine.WPF
             TextStoreHelper.NotifyTextChanged(this.textStore, e.startIndex, e.removeLength, e.insertLength);\r
         }\r
 \r
-        void Document_Progress(object sender, ProgressEventArgs e)\r
-        {\r
-            switch (e.state)\r
-            {\r
-                case ProgressState.Start:\r
-                    this.View.LayoutLines.IsFrozneDirtyFlag = true;\r
-                    break;\r
-                case ProgressState.Complete:\r
-                    this.View.LayoutLines.IsFrozneDirtyFlag = false;\r
-                    this.Dispatcher.BeginInvoke(new Action(()=>{\r
-                        TextStoreHelper.NotifyTextChanged(this.textStore, 0, 0, this.Document.Length);\r
-                        if (this.verticalScrollBar != null)\r
-                            this.verticalScrollBar.Maximum = this.View.LayoutLines.Count;\r
-                    }), null);\r
-                    this.View.CalculateLineCountOnScreen();\r
-                    break;\r
-            }\r
-        }\r
-\r
         void timer_Tick(object sender, EventArgs e)\r
         {\r
             if (this.image.ActualWidth == 0 || this.image.ActualHeight == 0)\r
index 0f9544b..cd61df2 100644 (file)
@@ -127,7 +127,7 @@ namespace Test
             if (result == true)
             {
                 this.fooTextBox.IsEnabled = false;
-                await this.fooTextBox.Document.LoadAsync(ofd.FileName, Encoding.Default,this.cancleTokenSrc);
+                await this.fooTextBox.LoadFileAsync(ofd.FileName, Encoding.Default,this.cancleTokenSrc);
                 this.fooTextBox.IsEnabled = true;
                 this.fooTextBox.Refresh();
             }
@@ -199,7 +199,7 @@ namespace Test
             bool result = (bool)sfd.ShowDialog(this);
             if (result == true)
             {
-                await this.fooTextBox.Document.SaveAsync(sfd.FileName,Encoding.Default,"\r\n",cancleTokenSrc);
+                await this.fooTextBox.SaveFile(sfd.FileName,Encoding.Default,"\r\n",cancleTokenSrc);
                 MessageBox.Show("complete");
             }
         }
index bd39102..fd5b28f 100644 (file)
@@ -82,9 +82,6 @@
     <Compile Include="..\..\Common\Document.cs">
       <Link>Document.cs</Link>
     </Compile>
-    <Compile Include="..\..\Common\DocumentExtend.cs">
-      <Link>DocumentExtend.cs</Link>
-    </Compile>
     <Compile Include="..\..\Common\EditView.cs">
       <Link>EditView.cs</Link>
     </Compile>
     <Compile Include="..\..\Common\WatchDogPattern.cs">
       <Link>WatchDogPattern.cs</Link>
     </Compile>
+    <Compile Include="..\..\Common\WinFileStream.cs">
+      <Link>WinFileStream.cs</Link>
+    </Compile>
     <Compile Include="D2DTextRender.cs" />
     <Compile Include="FooPrintText.cs" />
     <Compile Include="FooTextBox.cs">
index a3d562b..81b2b29 100644 (file)
@@ -9,7 +9,9 @@
 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.\r
  */\r
 using System;\r
+using System.Text;\r
 using System.Threading;\r
+using System.Threading.Tasks;\r
 using System.Windows.Forms;\r
 using System.Drawing;\r
 using System.ComponentModel;\r
@@ -70,7 +72,6 @@ namespace FooEditEngine.Windows
             this.SetStyle(ControlStyles.Opaque, true);\r
 \r
             this.Document = new Document();\r
-            DocumentExtend.Progress += Document_Progress;\r
 \r
             this.render = new D2DTextRender(this);\r
             this.View = new EditView(this.Document,this.render,new FooEditEngine.Padding(5,5,5,5));\r
@@ -830,6 +831,37 @@ namespace FooEditEngine.Windows
         }\r
 \r
         /// <summary>\r
+        /// ファイルからドキュメントを構築する\r
+        /// </summary>\r
+        /// <param name="filepath">ファイルパス</param>\r
+        /// <param name="enc">エンコード</param>\r
+        /// <returns>Taskオブジェクト</returns>\r
+        public async Task LoadFileAsync(string filepath, Encoding enc, System.Threading.CancellationTokenSource token)\r
+        {\r
+            this.View.LayoutLines.IsFrozneDirtyFlag = true;\r
+            WinFileReader fs = new WinFileReader(filepath, enc);\r
+            await this.Document.LoadAsync(fs, token);\r
+            this.View.LayoutLines.IsFrozneDirtyFlag = false;\r
+            this.initScrollBars();\r
+            this.OnMouseMove(new MouseEventArgs(MouseButtons.None, 0, MousePosition.X, MousePosition.Y, 0));\r
+            this.View.CalculateLineCountOnScreen();\r
+        }\r
+\r
+        /// <summary>\r
+        /// ドキュメントの内容をファイルに保存する\r
+        /// </summary>\r
+        /// <param name="filepath">ファイルパス</param>\r
+        /// <param name="newLine">改行コード</param>\r
+        /// <param name="enc">エンコード</param>\r
+        /// <returns>Taskオブジェクト</returns>\r
+        public async Task SaveFile(string filepath, Encoding enc, string newLine, System.Threading.CancellationTokenSource token)\r
+        {\r
+            WinFileWriter fs = new WinFileWriter(filepath, enc);\r
+            fs.NewLine = newLine;\r
+            await this.Document.SaveAsync(fs, token);\r
+        }\r
+\r
+        /// <summary>\r
         /// マウスカーソルを指定します\r
         /// </summary>\r
         public override Cursor Cursor\r
@@ -920,7 +952,6 @@ namespace FooEditEngine.Windows
         protected override void Dispose(bool disposing)\r
         {\r
             SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler(this.SystemEvents_UserPreferenceChanged);\r
-            DocumentExtend.Progress -= this.Document_Progress;\r
             this.render.Dispose();\r
             this.Timer.Dispose();\r
             base.Dispose(disposing);\r
@@ -1380,21 +1411,6 @@ namespace FooEditEngine.Windows
             this.SelectionChanged(this, null);\r
         }\r
 \r
-        void Document_Progress(object sender, ProgressEventArgs e)\r
-        {\r
-            if (e.state == ProgressState.Start)\r
-                this.LayoutLines.IsFrozneDirtyFlag = true;\r
-            if (e.state == ProgressState.Complete)\r
-            {\r
-                this.LayoutLines.IsFrozneDirtyFlag = false;\r
-                this.BeginInvoke(new Action(() => {\r
-                    this.initScrollBars();\r
-                    this.OnMouseMove(new MouseEventArgs(MouseButtons.None, 0, MousePosition.X, MousePosition.Y, 0));\r
-                }), null);\r
-                this.View.CalculateLineCountOnScreen();\r
-            }\r
-        }\r
-\r
         void initScrollBars()\r
         {\r
             this.VScrollBar.SmallChange = 1;\r
index 7600ade..b82345a 100644 (file)
@@ -63,7 +63,7 @@ namespace Test.Windows
             OpenFileDialog ofd = new OpenFileDialog();
             if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
-                await this.fooTextBox1.Document.LoadAsync(ofd.FileName, Encoding.Default,null);
+                await this.fooTextBox1.LoadFileAsync(ofd.FileName, Encoding.Default,null);
                 this.fooTextBox1.Refresh();
             }
         }