OSDN Git Service

行更新処理を最適化した
[fooeditengine/FooEditEngine.git] / WPF / Test / MainWindow.xaml.cs
1 /*
2  * Copyright (C) 2013 FooProject
3  * * 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
4  * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
5
6  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
7  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8
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/>.
10  */
11 using System.Text;
12 using System.Windows;
13 using System.Windows.Controls;
14 using System.Windows.Documents;
15 using System.Windows.Media;
16 using System.Windows.Threading;
17 using FooEditEngine;
18 using FooEditEngine.WPF;
19 using Microsoft.Win32;
20
21 namespace Test
22 {
23     /// <summary>
24     /// MainWindow.xaml の相互作用ロジック
25     /// </summary>
26     public partial class MainWindow : Window
27     {
28         System.Threading.CancellationTokenSource cancleTokenSrc = new System.Threading.CancellationTokenSource();
29         public MainWindow()
30         {
31             InitializeComponent();
32             this.fooTextBox.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(fooTextBox_MouseDoubleClick);
33             this.fooTextBox.CaretMoved += fooTextBox_CaretMoved;
34             this.fooTextBox.ShowTab = true;
35             this.fooTextBox.ShowFullSpace = true;
36             this.fooTextBox.ShowLineBreak = true;
37             this.fooTextBox.FoldingStrategy = new CharFoldingMethod('{', '}');
38             this.Enable.IsChecked = true;
39             this.URLMark.IsChecked = true;
40             
41             this.Closed += MainWindow_Closed;
42         }
43
44         void fooTextBox_CaretMoved(object sender, System.EventArgs e)
45         {
46             this.CaretIndex.Text = string.Format("Index:{0}", this.fooTextBox.SelectionStart);
47             this.SelectLength.Text = string.Format("Select Length:{0}", this.fooTextBox.SelectionLength);
48         }
49
50         void MainWindow_Closed(object sender, System.EventArgs e)
51         {
52             this.cancleTokenSrc.Cancel();
53             this.fooTextBox.Dispose();
54         }
55
56         void fooTextBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
57         {
58             FooMouseButtonEventArgs fe = (FooMouseButtonEventArgs)e;
59             foreach (Marker m in this.fooTextBox.Document.GetMarkers(fe.Index))
60             {
61                 if (m.hilight == HilightType.Url)
62                 {
63                     MessageBox.Show(this.fooTextBox.Document.ToString(m.start, m.length));
64
65                     fe.Handled = true;
66                 }
67             }
68         }
69
70         private void MenuItem_Click(object sender, RoutedEventArgs e)
71         {
72             if (this.fooTextBox.IsEnabled)
73                 this.fooTextBox.IsEnabled = false;
74             else
75                 this.fooTextBox.IsEnabled = true;
76             this.Enable.IsChecked = this.fooTextBox.IsEnabled;
77             this.fooTextBox.Refresh();
78         }
79
80         private void MenuItem_Click_1(object sender, RoutedEventArgs e)
81         {
82             if (this.fooTextBox.DrawLineNumber)
83                 this.fooTextBox.DrawLineNumber = false;
84             else
85                 this.fooTextBox.DrawLineNumber = true;
86             this.ShowLineNumber.IsChecked = this.fooTextBox.DrawLineNumber;
87             this.fooTextBox.Refresh();
88         }
89
90         private void MenuItem_Click_2(object sender, RoutedEventArgs e)
91         {
92             PrintDialog pd = new PrintDialog();
93             pd.PageRangeSelection = PageRangeSelection.AllPages;
94             pd.UserPageRangeEnabled = true;
95             if (pd.ShowDialog() == false)
96                 return;
97             FooPrintText printtext = new FooPrintText();
98             printtext.Document = this.fooTextBox.Document;
99             printtext.Font = this.fooTextBox.FontFamily;
100             printtext.FontSize = this.fooTextBox.FontSize;
101             printtext.DrawLineNumber = this.fooTextBox.DrawLineNumber;
102             printtext.Header = "header";
103             printtext.Footer = "footter";
104             printtext.LineBreakMethod = this.fooTextBox.LineBreakMethod;
105             printtext.LineBreakCharCount = this.fooTextBox.LineBreakCharCount;
106             printtext.MarkURL = true;
107             printtext.Hilighter = this.fooTextBox.Hilighter;
108             printtext.Foreground = this.fooTextBox.Foreground;
109             printtext.URL = this.fooTextBox.URL;
110             printtext.Comment = this.fooTextBox.Comment;
111             printtext.Keyword1 = this.fooTextBox.Keyword1;
112             printtext.Keyword2 = this.fooTextBox.Keyword2;
113             printtext.Litral = this.fooTextBox.Literal;
114             printtext.FlowDirection = this.fooTextBox.FlowDirection;
115             if (pd.PageRangeSelection == PageRangeSelection.AllPages)
116             {
117                 printtext.StartPage = -1;
118                 printtext.EndPage = -1;
119             }
120             else
121             {
122                 printtext.StartPage = pd.PageRange.PageFrom;
123                 printtext.EndPage = pd.PageRange.PageTo;
124             }
125             printtext.PageRect = new Rect(0,0,pd.PrintableAreaWidth, pd.PrintableAreaHeight);
126             printtext.Print(pd);
127         }
128
129         private async void MenuItem_Click_3(object sender, RoutedEventArgs e)
130         {
131             OpenFileDialog ofd = new OpenFileDialog();
132             bool result = (bool)ofd.ShowDialog(this);
133             if (result == true)
134             {
135                 await this.fooTextBox.Document.LoadAsync(ofd.FileName, Encoding.Default,this.cancleTokenSrc);
136                 this.fooTextBox.Refresh();
137             }
138         }
139
140         private void MenuItem_Click_4(object sender, RoutedEventArgs e)
141         {
142             if (this.fooTextBox.LineBreakMethod == LineBreakMethod.None)
143                 this.fooTextBox.LineBreakMethod = LineBreakMethod.CharUnit;
144             else
145                 this.fooTextBox.LineBreakMethod = LineBreakMethod.None;
146             this.fooTextBox.LineBreakCharCount = 10;
147             this.fooTextBox.PerfomLayouts();
148             this.fooTextBox.Refresh();
149         }
150
151         private void MenuItem_Click_5(object sender, RoutedEventArgs e)
152         {
153             Document doc = this.fooTextBox.Document;
154             doc.Insert(0,"this is a pen");
155             doc.SetMarker(Marker.Create(0, 4, HilightType.Sold));
156             doc.SetMarker(Marker.Create(8, 1, HilightType.Select, 255, 128, 128, 128));
157             doc.SetMarker(Marker.Create(10, 3, HilightType.Squiggle, 255, 255, 0, 0));
158             this.fooTextBox.Refresh();
159         }
160
161         private void MenuItem_Click_6(object sender, RoutedEventArgs e)
162         {
163             this.fooTextBox.FlowDirection = this.RTL.IsChecked ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
164             this.fooTextBox.Refresh();
165         }
166
167         private void MenuItem_Click_7(object sender, RoutedEventArgs e)
168         {
169             if (this.fooTextBox.Hilighter != null)
170             {
171                 this.fooTextBox.Hilighter = null;
172                 this.fooTextBox.LayoutLineCollection.ClearHilight();
173                 this.XMLHilight.IsChecked = false;
174             }
175             else
176             {
177                 this.fooTextBox.Hilighter = new XmlHilighter();
178                 this.fooTextBox.LayoutLineCollection.HilightAll();
179                 this.XMLHilight.IsChecked = true;
180             }
181             this.fooTextBox.Refresh();
182         }
183
184         private void MenuItem_Click_9(object sender, RoutedEventArgs e)
185         {
186             this.fooTextBox.LayoutLineCollection.GenerateFolding();
187             this.fooTextBox.Refresh();
188         }
189
190         private void MenuItem_Click_8(object sender, RoutedEventArgs e)
191         {
192             if (this.fooTextBox.DrawRuler)
193                 this.fooTextBox.DrawRuler = false;
194             else
195                 this.fooTextBox.DrawRuler = true;
196             this.ShowRuler.IsChecked = this.fooTextBox.DrawRuler;
197             this.fooTextBox.Refresh();
198         }
199
200         private async void MenuItem_Click_10(object sender, RoutedEventArgs e)
201         {
202             SaveFileDialog sfd = new SaveFileDialog();
203             bool result = (bool)sfd.ShowDialog(this);
204             if (result == true)
205             {
206                 await this.fooTextBox.Document.SaveAsync(sfd.FileName,Encoding.Default,"\r\n",cancleTokenSrc);
207                 MessageBox.Show("complete");
208             }
209         }
210
211         private void ReplaceAll_Click(object sender, RoutedEventArgs e)
212         {
213             System.Diagnostics.Stopwatch time = new System.Diagnostics.Stopwatch();
214             time.Start();
215             this.fooTextBox.Document.FireUpdateEvent = false;
216             this.fooTextBox.Document.ReplaceAll2(this.FindPattern.Text, this.ReplacePattern.Text,true);
217             this.fooTextBox.Document.FireUpdateEvent = true;
218             time.Stop();
219             this.fooTextBox.Refresh();
220             MessageBox.Show(string.Format("complete elpased time:{0}s",time.ElapsedMilliseconds/1000.0f));
221         }
222
223         private void URLMark_Click(object sender, RoutedEventArgs e)
224         {
225             if (this.fooTextBox.MarkURL)
226             {
227                 this.fooTextBox.MarkURL = false;
228                 this.URLMark.IsChecked = false;
229             }
230             else
231             {
232                 this.fooTextBox.MarkURL = true;
233                 this.URLMark.IsChecked = true;
234             }
235             this.fooTextBox.Refresh();
236         }
237     }
238 }