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.ShowTab = true;
34             this.fooTextBox.ShowFullSpace = true;
35             this.fooTextBox.ShowLineBreak = true;
36             this.fooTextBox.FoldingStrategy = new CharFoldingMethod('{', '}');
37             this.Enable.IsChecked = true;
38             this.URLMark.IsChecked = true;
39             
40             this.Closed += MainWindow_Closed;
41         }
42
43         void MainWindow_Closed(object sender, System.EventArgs e)
44         {
45             this.cancleTokenSrc.Cancel();
46             this.fooTextBox.Dispose();
47         }
48
49         void fooTextBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
50         {
51             FooMouseButtonEventArgs fe = (FooMouseButtonEventArgs)e;
52             foreach (Marker m in this.fooTextBox.Document.GetMarkers(fe.Index))
53             {
54                 if (m.hilight == HilightType.Url)
55                 {
56                     MessageBox.Show(this.fooTextBox.Document.ToString(m.start, m.length));
57
58                     fe.Handled = true;
59                 }
60             }
61         }
62
63         private void MenuItem_Click(object sender, RoutedEventArgs e)
64         {
65             if (this.fooTextBox.IsEnabled)
66                 this.fooTextBox.IsEnabled = false;
67             else
68                 this.fooTextBox.IsEnabled = true;
69             this.Enable.IsChecked = this.fooTextBox.IsEnabled;
70             this.fooTextBox.Refresh();
71         }
72
73         private void MenuItem_Click_1(object sender, RoutedEventArgs e)
74         {
75             if (this.fooTextBox.DrawLineNumber)
76                 this.fooTextBox.DrawLineNumber = false;
77             else
78                 this.fooTextBox.DrawLineNumber = true;
79             this.ShowLineNumber.IsChecked = this.fooTextBox.DrawLineNumber;
80             this.fooTextBox.Refresh();
81         }
82
83         private void MenuItem_Click_2(object sender, RoutedEventArgs e)
84         {
85             PrintDialog pd = new PrintDialog();
86             pd.PageRangeSelection = PageRangeSelection.AllPages;
87             pd.UserPageRangeEnabled = true;
88             if (pd.ShowDialog() == false)
89                 return;
90             FooPrintText printtext = new FooPrintText();
91             printtext.Document = this.fooTextBox.Document;
92             printtext.Font = this.fooTextBox.FontFamily;
93             printtext.FontSize = this.fooTextBox.FontSize;
94             printtext.DrawLineNumber = this.fooTextBox.DrawLineNumber;
95             printtext.Header = "header";
96             printtext.Footer = "footter";
97             printtext.LineBreakMethod = this.fooTextBox.LineBreakMethod;
98             printtext.LineBreakCharCount = this.fooTextBox.LineBreakCharCount;
99             printtext.MarkURL = true;
100             printtext.Hilighter = this.fooTextBox.Hilighter;
101             printtext.Foreground = this.fooTextBox.Foreground;
102             printtext.URL = this.fooTextBox.URL;
103             printtext.Comment = this.fooTextBox.Comment;
104             printtext.Keyword1 = this.fooTextBox.Keyword1;
105             printtext.Keyword2 = this.fooTextBox.Keyword2;
106             printtext.Litral = this.fooTextBox.Literal;
107             printtext.FlowDirection = this.fooTextBox.FlowDirection;
108             if (pd.PageRangeSelection == PageRangeSelection.AllPages)
109             {
110                 printtext.StartPage = -1;
111                 printtext.EndPage = -1;
112             }
113             else
114             {
115                 printtext.StartPage = pd.PageRange.PageFrom;
116                 printtext.EndPage = pd.PageRange.PageTo;
117             }
118             printtext.PageRect = new Rect(0,0,pd.PrintableAreaWidth, pd.PrintableAreaHeight);
119             printtext.Print(pd);
120         }
121
122         private async void MenuItem_Click_3(object sender, RoutedEventArgs e)
123         {
124             OpenFileDialog ofd = new OpenFileDialog();
125             bool result = (bool)ofd.ShowDialog(this);
126             if (result == true)
127             {
128                 await this.fooTextBox.Document.LoadAsync(ofd.FileName, Encoding.Default,this.cancleTokenSrc);
129                 this.fooTextBox.Refresh();
130             }
131         }
132
133         private void MenuItem_Click_4(object sender, RoutedEventArgs e)
134         {
135             if (this.fooTextBox.LineBreakMethod == LineBreakMethod.None)
136                 this.fooTextBox.LineBreakMethod = LineBreakMethod.CharUnit;
137             else
138                 this.fooTextBox.LineBreakMethod = LineBreakMethod.None;
139             this.fooTextBox.LineBreakCharCount = 10;
140             this.fooTextBox.PerfomLayouts();
141             this.fooTextBox.Refresh();
142         }
143
144         private void MenuItem_Click_5(object sender, RoutedEventArgs e)
145         {
146             Document doc = this.fooTextBox.Document;
147             doc.Insert(0,"this is a pen");
148             doc.SetMarker(Marker.Create(0, 4, HilightType.Sold));
149             doc.SetMarker(Marker.Create(8, 1, HilightType.Select, 255, 128, 128, 128));
150             doc.SetMarker(Marker.Create(10, 3, HilightType.Squiggle, 255, 255, 0, 0));
151             this.fooTextBox.Refresh();
152         }
153
154         private void MenuItem_Click_6(object sender, RoutedEventArgs e)
155         {
156             this.fooTextBox.FlowDirection = this.RTL.IsChecked ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
157             this.fooTextBox.Refresh();
158         }
159
160         private void MenuItem_Click_7(object sender, RoutedEventArgs e)
161         {
162             if (this.fooTextBox.Hilighter != null)
163             {
164                 this.fooTextBox.Hilighter = null;
165                 this.fooTextBox.LayoutLineCollection.ClearHilight();
166                 this.XMLHilight.IsChecked = false;
167             }
168             else
169             {
170                 this.fooTextBox.Hilighter = new XmlHilighter();
171                 this.fooTextBox.LayoutLineCollection.HilightAll();
172                 this.XMLHilight.IsChecked = true;
173             }
174             this.fooTextBox.Refresh();
175         }
176
177         private void MenuItem_Click_9(object sender, RoutedEventArgs e)
178         {
179             this.fooTextBox.LayoutLineCollection.GenerateFolding();
180             this.fooTextBox.Refresh();
181         }
182
183         private void MenuItem_Click_8(object sender, RoutedEventArgs e)
184         {
185             if (this.fooTextBox.DrawRuler)
186                 this.fooTextBox.DrawRuler = false;
187             else
188                 this.fooTextBox.DrawRuler = true;
189             this.ShowRuler.IsChecked = this.fooTextBox.DrawRuler;
190             this.fooTextBox.Refresh();
191         }
192
193         private async void MenuItem_Click_10(object sender, RoutedEventArgs e)
194         {
195             SaveFileDialog sfd = new SaveFileDialog();
196             bool result = (bool)sfd.ShowDialog(this);
197             if (result == true)
198             {
199                 await this.fooTextBox.Document.SaveAsync(sfd.FileName,Encoding.Default,"\r\n",cancleTokenSrc);
200                 MessageBox.Show("complete");
201             }
202         }
203
204         private void ReplaceAll_Click(object sender, RoutedEventArgs e)
205         {
206             System.Diagnostics.Stopwatch time = new System.Diagnostics.Stopwatch();
207             time.Start();
208             this.fooTextBox.Document.FireUpdateEvent = false;
209             this.fooTextBox.Document.ReplaceAll2(this.FindPattern.Text, this.ReplacePattern.Text,true);
210             this.fooTextBox.Document.FireUpdateEvent = true;
211             time.Stop();
212             this.fooTextBox.Refresh();
213             MessageBox.Show(string.Format("complete elpased time:{0}s",time.ElapsedMilliseconds/1000.0f));
214         }
215
216         private void URLMark_Click(object sender, RoutedEventArgs e)
217         {
218             if (this.fooTextBox.MarkURL)
219             {
220                 this.fooTextBox.MarkURL = false;
221                 this.URLMark.IsChecked = false;
222             }
223             else
224             {
225                 this.fooTextBox.MarkURL = true;
226                 this.URLMark.IsChecked = true;
227             }
228             this.fooTextBox.Refresh();
229         }
230
231         private void ShowLineMarker_Click(object sender, RoutedEventArgs e)
232         {
233             if (this.fooTextBox.DrawCaretLine)
234             {
235                 this.fooTextBox.DrawCaretLine = false;
236                 this.ShowLineMarker.IsEnabled = false;
237             }
238             else
239             {
240                 this.fooTextBox.DrawCaretLine = true;
241                 this.ShowLineMarker.IsEnabled = true;
242             }
243             this.fooTextBox.Refresh();
244         }
245     }
246     public class TextRangeConveter : System.Windows.Data.IValueConverter
247     {
248         public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
249         {
250             FooEditEngine.TextRange range = (FooEditEngine.TextRange)value;
251             return string.Format("Index:{0} Length:{1}", range.Index, range.Length);
252         }
253
254         public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
255         {
256             throw new System.NotImplementedException();
257         }
258     }
259 }