OSDN Git Service

WPF版でDocumentを切り替えられるようにした
authorgdkhd812 <test@nnn.co.jp>
Fri, 20 Nov 2015 15:19:49 +0000 (20:49 +0530)
committergdkhd812 <test@nnn.co.jp>
Fri, 20 Nov 2015 15:19:49 +0000 (20:49 +0530)
WPF/FooEditEngine/FooTextBox.cs
WPF/Test/MainWindow.xaml
WPF/Test/MainWindow.xaml.cs

index 6205f8f..3daf54c 100644 (file)
@@ -52,6 +52,7 @@ namespace FooEditEngine.WPF
         bool disposed = false;
         FooTextBoxAutomationPeer peer;
         bool nowCaretMove = false;
+        Document _Document;
         
         static FooTextBox()
         {
@@ -91,7 +92,6 @@ namespace FooEditEngine.WPF
             this.Render.ShowTab = this.ShowTab;
 
             this.Document = new Document();
-            this.Document.LayoutLines.Render = this.Render;
 
             this.View = new EditView(this.Document, this.Render, new Padding(5, 5, 5, 5));
             this.View.SrcChanged += View_SrcChanged;
@@ -106,9 +106,6 @@ namespace FooEditEngine.WPF
             this._Controller = new Controller(this.Document, this.View);
             this._Controller.SelectionChanged += new EventHandler(Controller_SelectionChanged);
 
-            //Viewを作成した後に追加しないと例外が発生する
-            this.Document.Update += new DocumentUpdateEventHandler(Document_Update);
-
             this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, CopyCommand, CanExecute));
             this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut, CutCommand, CanExecute));
             this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, PasteCommand, CanExecute));
@@ -978,7 +975,8 @@ namespace FooEditEngine.WPF
                 return;
             if(e.type == UpdateType.Replace)
                 TextStoreHelper.NotifyTextChanged(this.textStore, e.startIndex, e.removeLength, e.insertLength);
-            this.peer.OnNotifyTextChanged();
+            if(this.peer != null)
+                this.peer.OnNotifyTextChanged();
         }
 
         void timer_Tick(object sender, EventArgs e)
@@ -1285,8 +1283,32 @@ namespace FooEditEngine.WPF
         /// </summary>
         public Document Document
         {
-            get;
-            private set;
+            get
+            {
+                return this._Document;
+            }
+            set
+            {
+                int oldLength = 0;
+                if(this._Document != null)
+                {
+                    this._Document.Update -= new DocumentUpdateEventHandler(Document_Update);
+                    oldLength = this._Document.Length;
+                }
+
+                this._Document = value;
+                this._Document.LayoutLines.Render = this.Render;
+                this._Document.Update += new DocumentUpdateEventHandler(Document_Update);
+                //初期化が終わっていればすべて存在する
+                if(this.Controller != null && this.View != null && this.textStore != null)
+                {
+                    this.Controller.Document = value;
+                    this.View.Document = value;
+                    this.Controller.AdjustCaret();
+                    this.textStore.NotifyTextChanged(oldLength, value.Length);
+                    this.Refresh();
+                }
+            }
         }
 
         /// <summary>
index 476d01a..77a345d 100644 (file)
                 <MenuItem Header="Set Padding" Click="MenuItem_Click_11"/>
                 <MenuItem Header="Indent As Space" Click="MenuItem_Click_13" IsCheckable="True" Name="SpaceIndent"/>
             </MenuItem>
+            <MenuItem Header="Test">
+                <MenuItem Header="Document1" Click="MenuItem_Click_14"/>
+                <MenuItem Header="Document2" Click="MenuItem_Click_15"/>
+            </MenuItem>
         </Menu>
         <MyNamespace:FooTextBox x:Name="fooTextBox" Grid.Row="1"/>
         <StackPanel Grid.Row="2" Orientation="Horizontal">
index 0868d65..c420438 100644 (file)
@@ -28,6 +28,9 @@ namespace Test
     public partial class MainWindow : Window
     {
         System.Threading.CancellationTokenSource cancleTokenSrc = new System.Threading.CancellationTokenSource();
+
+        List<Document> Documents = new List<Document>();
+
         public MainWindow()
         {
             InitializeComponent();
@@ -38,7 +41,10 @@ namespace Test
             this.fooTextBox.FoldingStrategy = new CharFoldingMethod('{', '}');
             this.fooTextBox.Document.Update += Document_Update;
             this.Enable.IsChecked = true;
-            
+
+            this.Documents.Add(this.fooTextBox.Document);
+            this.Documents.Add(new Document());
+
             this.Closed += MainWindow_Closed;
         }
 
@@ -308,6 +314,18 @@ namespace Test
                 this.fooTextBox.IndentMode = IndentMode.Space;
             }
         }
+
+        private void MenuItem_Click_14(object sender, RoutedEventArgs e)
+        {
+            this.fooTextBox.Document = this.Documents[0];
+            MessageBox.Show("Current Document is Document1");
+        }
+
+        private void MenuItem_Click_15(object sender, RoutedEventArgs e)
+        {
+            this.fooTextBox.Document = this.Documents[1];
+            MessageBox.Show("Current Document is Document2");
+        }
     }
     public class TextRangeConveter : System.Windows.Data.IValueConverter
     {