OSDN Git Service

URLに下線を引くかどうか選択できるようにした
[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         }
47
48         void fooTextBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
49         {
50             FooMouseButtonEventArgs fe = (FooMouseButtonEventArgs)e;
51             foreach (Marker m in this.fooTextBox.Document.GetMarkers(fe.Index))
52             {
53                 if (m.hilight == HilightType.Url)
54                 {
55                     MessageBox.Show(this.fooTextBox.Document.ToString(m.start, m.length));
56
57                     fe.Handled = true;
58                 }
59             }
60         }
61
62         private void MenuItem_Click(object sender, RoutedEventArgs e)
63         {
64             if (this.fooTextBox.IsEnabled)
65                 this.fooTextBox.IsEnabled = false;
66             else
67                 this.fooTextBox.IsEnabled = true;
68             this.Enable.IsChecked = this.fooTextBox.IsEnabled;
69             this.fooTextBox.Refresh();
70         }
71
72         private void MenuItem_Click_1(object sender, RoutedEventArgs e)
73         {
74             if (this.fooTextBox.DrawLineNumber)
75                 this.fooTextBox.DrawLineNumber = false;
76             else
77                 this.fooTextBox.DrawLineNumber = true;
78             this.ShowLineNumber.IsChecked = this.fooTextBox.DrawLineNumber;
79             this.fooTextBox.Refresh();
80         }
81
82         private void MenuItem_Click_2(object sender, RoutedEventArgs e)
83         {
84             PrintDialog pd = new PrintDialog();
85             pd.PageRangeSelection = PageRangeSelection.AllPages;
86             pd.UserPageRangeEnabled = true;
87             if (pd.ShowDialog() == false)
88                 return;
89             FooPrintText printtext = new FooPrintText();
90             printtext.Document = this.fooTextBox.Document;
91             printtext.Font = this.fooTextBox.FontFamily;
92             printtext.FontSize = this.fooTextBox.FontSize;
93             printtext.DrawLineNumber = this.fooTextBox.DrawLineNumber;
94             printtext.Header = "header";
95             printtext.Footer = "footter";
96             printtext.LineBreakMethod = this.fooTextBox.LineBreakMethod;
97             printtext.LineBreakCharCount = this.fooTextBox.LineBreakCharCount;
98             printtext.MarkURL = true;
99             printtext.Hilighter = this.fooTextBox.Hilighter;
100             printtext.Foreground = this.fooTextBox.Foreground;
101             printtext.URL = this.fooTextBox.URL;
102             printtext.Comment = this.fooTextBox.Comment;
103             printtext.Keyword1 = this.fooTextBox.Keyword1;
104             printtext.Keyword2 = this.fooTextBox.Keyword2;
105             printtext.Litral = this.fooTextBox.Literal;
106             printtext.FlowDirection = this.fooTextBox.FlowDirection;
107             if (pd.PageRangeSelection == PageRangeSelection.AllPages)
108             {
109                 printtext.StartPage = -1;
110                 printtext.EndPage = -1;
111             }
112             else
113             {
114                 printtext.StartPage = pd.PageRange.PageFrom;
115                 printtext.EndPage = pd.PageRange.PageTo;
116             }
117             printtext.PageRect = new Rect(0,0,pd.PrintableAreaWidth, pd.PrintableAreaHeight);
118             printtext.Print(pd);
119         }
120
121         private async void MenuItem_Click_3(object sender, RoutedEventArgs e)
122         {
123             OpenFileDialog ofd = new OpenFileDialog();
124             bool result = (bool)ofd.ShowDialog(this);
125             if (result == true)
126             {
127                 await this.fooTextBox.Document.LoadAsync(ofd.FileName, Encoding.Default,this.cancleTokenSrc);
128                 this.fooTextBox.Refresh();
129             }
130         }
131
132         private void MenuItem_Click_4(object sender, RoutedEventArgs e)
133         {
134             if (this.fooTextBox.LineBreakMethod == LineBreakMethod.None)
135                 this.fooTextBox.LineBreakMethod = LineBreakMethod.PageBound;
136             else
137                 this.fooTextBox.LineBreakMethod = LineBreakMethod.None;
138             this.fooTextBox.PerfomLayouts();
139             this.fooTextBox.Refresh();
140         }
141
142         private void MenuItem_Click_5(object sender, RoutedEventArgs e)
143         {
144             Document doc = this.fooTextBox.Document;
145             doc.Insert(0,"this is a pen");
146             doc.SetMarker(Marker.Create(0, 4, HilightType.Sold));
147             doc.SetMarker(Marker.Create(8, 1, HilightType.Select, 255, 128, 128, 128));
148             doc.SetMarker(Marker.Create(10, 3, HilightType.Squiggle, 255, 255, 0, 0));
149             this.fooTextBox.Refresh();
150         }
151
152         private void MenuItem_Click_6(object sender, RoutedEventArgs e)
153         {
154             this.fooTextBox.FlowDirection = this.RTL.IsChecked ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
155             this.fooTextBox.Refresh();
156         }
157
158         private void MenuItem_Click_7(object sender, RoutedEventArgs e)
159         {
160             if (this.fooTextBox.Hilighter != null)
161             {
162                 this.fooTextBox.Hilighter = null;
163                 this.fooTextBox.LayoutLineCollection.ClearHilight();
164                 this.XMLHilight.IsChecked = false;
165             }
166             else
167             {
168                 this.fooTextBox.Hilighter = new XmlHilighter();
169                 this.fooTextBox.LayoutLineCollection.HilightAll();
170                 this.XMLHilight.IsChecked = true;
171             }
172             this.fooTextBox.Refresh();
173         }
174
175         private void MenuItem_Click_9(object sender, RoutedEventArgs e)
176         {
177             this.fooTextBox.LayoutLineCollection.GenerateFolding();
178             this.fooTextBox.Refresh();
179         }
180
181         private void MenuItem_Click_8(object sender, RoutedEventArgs e)
182         {
183             if (this.fooTextBox.DrawRuler)
184                 this.fooTextBox.DrawRuler = false;
185             else
186                 this.fooTextBox.DrawRuler = true;
187             this.ShowRuler.IsChecked = this.fooTextBox.DrawRuler;
188             this.fooTextBox.Refresh();
189         }
190
191         private async void MenuItem_Click_10(object sender, RoutedEventArgs e)
192         {
193             SaveFileDialog sfd = new SaveFileDialog();
194             bool result = (bool)sfd.ShowDialog(this);
195             if (result == true)
196             {
197                 await this.fooTextBox.Document.SaveAsync(sfd.FileName,Encoding.Default,"\r\n",cancleTokenSrc);
198                 MessageBox.Show("complete");
199             }
200         }
201
202         private void ReplaceAll_Click(object sender, RoutedEventArgs e)
203         {
204             System.Diagnostics.Stopwatch time = new System.Diagnostics.Stopwatch();
205             time.Start();
206             this.fooTextBox.Document.SetFindParam(this.FindPattern.Text, false, System.Text.RegularExpressions.RegexOptions.None);
207             this.fooTextBox.Document.ReplaceAll(this.ReplacePattern.Text, false);
208             time.Stop();
209             this.fooTextBox.Refresh();
210             MessageBox.Show(string.Format("complete elpased time:{0}s",time.ElapsedMilliseconds/1000.0f));
211         }
212
213         private void URLMark_Click(object sender, RoutedEventArgs e)
214         {
215             if (this.fooTextBox.MarkURL)
216             {
217                 this.fooTextBox.MarkURL = false;
218                 this.URLMark.IsChecked = false;
219             }
220             else
221             {
222                 this.fooTextBox.MarkURL = true;
223                 this.URLMark.IsChecked = true;
224             }
225             this.fooTextBox.Refresh();
226         }
227     }
228 }