OSDN Git Service

ファイル操作関連をDocumentExtendクラスに分離した
[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.Closed += MainWindow_Closed;
38         }
39
40         void MainWindow_Closed(object sender, System.EventArgs e)
41         {
42             this.cancleTokenSrc.Cancel();
43         }
44
45         void fooTextBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
46         {
47             FooMouseButtonEventArgs fe = (FooMouseButtonEventArgs)e;
48             foreach (Marker m in this.fooTextBox.Document.GetMarkers(fe.Index))
49             {
50                 if (m.hilight == HilightType.Url)
51                 {
52                     MessageBox.Show(this.fooTextBox.Document.ToString(m.start, m.length));
53
54                     fe.Handled = true;
55                 }
56             }
57         }
58
59         private void MenuItem_Click(object sender, RoutedEventArgs e)
60         {
61             if (this.fooTextBox.IsEnabled)
62                 this.fooTextBox.IsEnabled = false;
63             else
64                 this.fooTextBox.IsEnabled = true;
65             this.Enable.IsChecked = this.fooTextBox.IsEnabled;
66             this.fooTextBox.Refresh();
67         }
68
69         private void MenuItem_Click_1(object sender, RoutedEventArgs e)
70         {
71             if (this.fooTextBox.DrawLineNumber)
72                 this.fooTextBox.DrawLineNumber = false;
73             else
74                 this.fooTextBox.DrawLineNumber = true;
75             this.ShowLineNumber.IsChecked = this.fooTextBox.DrawLineNumber;
76             this.fooTextBox.Refresh();
77         }
78
79         private void MenuItem_Click_2(object sender, RoutedEventArgs e)
80         {
81             PrintDialog pd = new PrintDialog();
82             pd.PageRangeSelection = PageRangeSelection.AllPages;
83             pd.UserPageRangeEnabled = true;
84             if (pd.ShowDialog() == false)
85                 return;
86             FooPrintText printtext = new FooPrintText();
87             printtext.Document = this.fooTextBox.Document;
88             printtext.Font = this.fooTextBox.FontFamily;
89             printtext.FontSize = this.fooTextBox.FontSize;
90             printtext.DrawLineNumber = this.fooTextBox.DrawLineNumber;
91             printtext.Header = "header";
92             printtext.Footer = "footter";
93             printtext.LineBreakMethod = this.fooTextBox.LineBreakMethod;
94             printtext.LineBreakCharCount = this.fooTextBox.LineBreakCharCount;
95             printtext.MarkURL = true;
96             printtext.Hilighter = this.fooTextBox.Hilighter;
97             printtext.Foreground = this.fooTextBox.Foreground;
98             printtext.URL = this.fooTextBox.URL;
99             printtext.Comment = this.fooTextBox.Comment;
100             printtext.Keyword1 = this.fooTextBox.Keyword1;
101             printtext.Keyword2 = this.fooTextBox.Keyword2;
102             printtext.Litral = this.fooTextBox.Literal;
103             printtext.FlowDirection = this.fooTextBox.FlowDirection;
104             if (pd.PageRangeSelection == PageRangeSelection.AllPages)
105             {
106                 printtext.StartPage = -1;
107                 printtext.EndPage = -1;
108             }
109             else
110             {
111                 printtext.StartPage = pd.PageRange.PageFrom;
112                 printtext.EndPage = pd.PageRange.PageTo;
113             }
114             printtext.PageRect = new Rect(0,0,pd.PrintableAreaWidth, pd.PrintableAreaHeight);
115             printtext.Print(pd);
116         }
117
118         private async void MenuItem_Click_3(object sender, RoutedEventArgs e)
119         {
120             OpenFileDialog ofd = new OpenFileDialog();
121             bool result = (bool)ofd.ShowDialog(this);
122             if (result == true)
123             {
124                 await this.fooTextBox.Document.LoadAsync(ofd.FileName, Encoding.Default,this.cancleTokenSrc);
125                 this.fooTextBox.Refresh();
126             }
127         }
128
129         private void MenuItem_Click_4(object sender, RoutedEventArgs e)
130         {
131             if (this.fooTextBox.LineBreakMethod == LineBreakMethod.None)
132                 this.fooTextBox.LineBreakMethod = LineBreakMethod.PageBound;
133             else
134                 this.fooTextBox.LineBreakMethod = LineBreakMethod.None;
135             this.fooTextBox.PerfomLayouts();
136             this.fooTextBox.Refresh();
137         }
138
139         private void MenuItem_Click_5(object sender, RoutedEventArgs e)
140         {
141             Document doc = this.fooTextBox.Document;
142             doc.Insert(0,"this is a pen");
143             doc.SetMarker(Marker.Create(0, 4, HilightType.Sold));
144             doc.SetMarker(Marker.Create(8, 1, HilightType.Select, 255, 128, 128, 128));
145             doc.SetMarker(Marker.Create(10, 3, HilightType.Squiggle, 255, 255, 0, 0));
146             this.fooTextBox.Refresh();
147         }
148
149         private void MenuItem_Click_6(object sender, RoutedEventArgs e)
150         {
151             this.fooTextBox.FlowDirection = this.RTL.IsChecked ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
152             this.fooTextBox.Refresh();
153         }
154
155         private void MenuItem_Click_7(object sender, RoutedEventArgs e)
156         {
157             if (this.fooTextBox.Hilighter != null)
158             {
159                 this.fooTextBox.Hilighter = null;
160                 this.fooTextBox.LayoutLineCollection.ClearHilight();
161                 this.XMLHilight.IsChecked = false;
162             }
163             else
164             {
165                 this.fooTextBox.Hilighter = new XmlHilighter();
166                 this.fooTextBox.LayoutLineCollection.HilightAll();
167                 this.XMLHilight.IsChecked = true;
168             }
169             this.fooTextBox.Refresh();
170         }
171
172         private void MenuItem_Click_9(object sender, RoutedEventArgs e)
173         {
174             this.fooTextBox.LayoutLineCollection.GenerateFolding();
175             this.fooTextBox.Refresh();
176         }
177
178         private void MenuItem_Click_8(object sender, RoutedEventArgs e)
179         {
180             if (this.fooTextBox.DrawRuler)
181                 this.fooTextBox.DrawRuler = false;
182             else
183                 this.fooTextBox.DrawRuler = true;
184             this.ShowRuler.IsChecked = this.fooTextBox.DrawRuler;
185             this.fooTextBox.Refresh();
186         }
187
188         private async void MenuItem_Click_10(object sender, RoutedEventArgs e)
189         {
190             SaveFileDialog sfd = new SaveFileDialog();
191             bool result = (bool)sfd.ShowDialog(this);
192             if (result == true)
193             {
194                 await this.fooTextBox.Document.SaveAsync(sfd.FileName,Encoding.Default,"\r\n",cancleTokenSrc);
195                 MessageBox.Show("complete");
196             }
197         }
198     }
199 }