-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
MathControl.xaml.cs
247 lines (214 loc) · 7.62 KB
/
MathControl.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2012-2017 Dmitry Kischenko
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using xFunc.Maths;
using xFunc.Maths.Analyzers;
using xFunc.Maths.Expressions;
using xFunc.Maths.Expressions.Collections;
using xFunc.Presenters;
using xFunc.Resources;
using xFunc.ViewModels;
namespace xFunc.Views
{
public partial class MathControl : UserControl, IMathView
{
public static readonly DependencyProperty StatusProperty = DependencyProperty.Register(nameof(Status), typeof(string), typeof(MathControl));
public static RoutedCommand CopyExpToClipCommand = new RoutedCommand();
public static RoutedCommand CopyAnswerToClipCommand = new RoutedCommand();
public static RoutedCommand CopyExpToInputCommand = new RoutedCommand();
public static RoutedCommand CopyAnswerToInputCommand = new RoutedCommand();
public static RoutedCommand DeleteExpCommand = new RoutedCommand();
private MathPresenter presenter;
private int index;
private Key prevKey;
public MathControl()
{
InitializeComponent();
}
public MathControl(MathPresenter presenter)
{
this.presenter = presenter;
InitializeComponent();
}
internal void MathExpEnter()
{
try
{
presenter.Add(mathExpressionBox.Text);
var count = mathExpsListBox.Items.Count;
if (count > 0)
mathExpsListBox.ScrollIntoView(mathExpsListBox.Items[count - 1]);
Status = string.Empty;
}
catch (LexerException mle)
{
Status = mle.Message;
}
catch (ParserException mpe)
{
Status = mpe.Message;
}
catch (ParameterIsReadOnlyException mpiroe)
{
Status = mpiroe.Message;
}
catch (ParameterTypeMismatchException ptme)
{
Status = ptme.Message;
}
catch (ResultIsNotSupportedException rinse)
{
Status = rinse.Message;
}
catch (DivideByZeroException dbze)
{
Status = dbze.Message;
}
catch (ArgumentNullException ane)
{
Status = ane.Message;
}
catch (ArgumentException ae)
{
Status = ae.Message;
}
catch (FormatException fe)
{
Status = fe.Message;
}
catch (OverflowException oe)
{
Status = oe.Message;
}
catch (KeyNotFoundException knfe)
{
Status = knfe.Message;
}
catch (IndexOutOfRangeException)
{
Status = Resource.IndexOutOfRangeExceptionError;
}
catch (InvalidOperationException ioe)
{
Status = ioe.Message;
}
catch (NotSupportedException)
{
Status = Resource.NotSupportedOperationError;
}
mathExpressionBox.Text = string.Empty;
}
private void mathExpressionBox_KeyUp(object o, KeyEventArgs args)
{
if (args.Key == Key.Enter && !string.IsNullOrWhiteSpace(mathExpressionBox.Text))
{
MathExpEnter();
}
else if (args.Key == Key.Up && presenter.Workspace.Count > 0)
{
if (prevKey != Key.None && prevKey != args.Key)
index++;
var newIndex = presenter.Workspace.Count - 1 - index;
if (newIndex >= 0 && newIndex < presenter.Workspace.Count)
{
this.mathExpressionBox.Text = presenter.Workspace[newIndex].StringExpression;
this.mathExpressionBox.SelectionStart = this.mathExpressionBox.Text.Length;
if (index < presenter.Workspace.Count - 1)
index++;
}
prevKey = args.Key;
}
else if (args.Key == Key.Down && presenter.Workspace.Count > 0)
{
if (index > 0 || (prevKey != Key.None && prevKey != args.Key))
index--;
var newIndex = presenter.Workspace.Count - 1 - index;
if (newIndex >= 0 && newIndex < presenter.Workspace.Count)
{
this.mathExpressionBox.Text = presenter.Workspace[newIndex].StringExpression;
this.mathExpressionBox.SelectionStart = this.mathExpressionBox.Text.Length;
}
prevKey = args.Key;
}
}
private void removeMath_Click(object o, RoutedEventArgs args)
{
var item = ((Button)o).Tag as MathWorkspaceItemViewModel;
presenter.Remove(item);
}
private void Copy_CanExecute(object o, CanExecuteRoutedEventArgs args)
{
args.CanExecute = mathExpsListBox.SelectedItem != null;
}
private void CopyExpToClip_Execute(object o, ExecutedRoutedEventArgs args)
{
Clipboard.SetText(((MathWorkspaceItemViewModel)mathExpsListBox.SelectedItem).StringExpression);
}
private void CopyAnswerToClip_Execute(object o, ExecutedRoutedEventArgs args)
{
Clipboard.SetText(((MathWorkspaceItemViewModel)mathExpsListBox.SelectedItem).Answer);
}
private void CopyExpToInput_Execute(object o, ExecutedRoutedEventArgs args)
{
mathExpressionBox.Text = ((MathWorkspaceItemViewModel)mathExpsListBox.SelectedItem).StringExpression;
}
private void CopyAnswerToInput_Execute(object o, ExecutedRoutedEventArgs args)
{
mathExpressionBox.Text = ((MathWorkspaceItemViewModel)mathExpsListBox.SelectedItem).Answer;
}
private void DeleteExp_Execute(object o, ExecutedRoutedEventArgs args)
{
var item = (MathWorkspaceItemViewModel)mathExpsListBox.SelectedItem;
presenter.Remove(item);
}
private void DeleteExp_CanExecute(object o, CanExecuteRoutedEventArgs args)
{
args.CanExecute = mathExpsListBox.SelectedItem != null;
}
public string Status
{
get
{
return (string)GetValue(StatusProperty);
}
set
{
SetValue(StatusProperty, value);
}
}
public MathPresenter Presenter
{
get
{
return presenter;
}
set
{
presenter = value;
}
}
public IEnumerable<MathWorkspaceItemViewModel> MathExpressions
{
set
{
mathExpsListBox.ItemsSource = value;
}
}
}
}