/
MainWindow.cs
255 lines (231 loc) · 9.15 KB
/
MainWindow.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
248
249
250
251
252
253
254
255
/*
Copyright (c) 2011-2012, Måns Andersson <mail@mansandersson.se>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace Calculator
{
/// <summary>
/// Main Window
/// </summary>
public partial class MainWindow : Form
{
/// <summary>
/// Window size (only textbox visible)
/// </summary>
private Size NullSize = new Size(467, 50);
/// <summary>
/// Window size (textbox + decimal value visible)
/// </summary>
private Size HalfSize = new Size(467, 83);
/// <summary>
/// Window size (textbox + decimal, hex, binary value visible)
/// </summary>
private Size FullSize = new Size(467, 150);
/// <summary>
/// Window size (textbox + decimal, hex, binary (2 lines) value visible)
/// </summary>
private Size FullSizeTwoBinaryLines = new Size(467, 175);
/// <summary>
/// Window size (textbox + decimal, hex, binary (3 lines) value visible)
/// </summary>
private Size FullSizeThreeBinaryLines = new Size(467, 200);
/// <summary>
/// Operational mode for expression evaluation
/// </summary>
private CalculatorMode Mode { get; set; }
/// <summary>
/// Constructor, init form
/// </summary>
public MainWindow()
{
InitializeComponent();
ClearLabels();
this.Mode = CalculatorMode.Mathematics;
}
/// <summary>
/// Clear all labels and set size to null
/// </summary>
private void ClearLabels()
{
this.ClientSize = NullSize;
txtInput.BackColor = Color.Silver;
txtResultDecimal.Text = "";
txtResultHex.Text = "";
txtResultBits.Text = "";
switch (this.Mode)
{
default:
case CalculatorMode.Mathematics:
lblMode.Text = "M";
lblMode.BackColor = Color.Green;
break;
case CalculatorMode.Programming:
lblMode.Text = "P";
lblMode.BackColor = Color.RoyalBlue;
break;
}
}
/// <summary>
/// Text input has changed, re-evaluate and calculate
/// </summary>
/// <param name="sender">sending object</param>
/// <param name="e">event arguments</param>
private void txtInput_TextChanged(object sender, EventArgs e)
{
ClearLabels();
if (String.IsNullOrWhiteSpace(txtInput.Text))
return;
Calculator calc = new Calculator(txtInput.Text);
calc.Mode = this.Mode;
double? result = null;
try
{
result = calc.Calculate();
}
catch (Exception)
{
result = null;
}
if (result.HasValue)
{
Int64 intResult = (Int64)result;
txtResultDecimal.Text = result.ToString();
if (result == ((double)intResult))
{
// This is a whole number
txtResultHex.Text = String.Join(" ", intResult.ToString("X").Reverse().SplitEveryNth(2)).Reverse();
txtResultBits.Text = String.Join(" ", Convert.ToString(intResult, 2).Reverse().SplitEveryNth(4)).Reverse();
if (txtResultBits.Text.Length > 100)
this.ClientSize = FullSizeThreeBinaryLines;
else if (txtResultBits.Text.Length > 50)
this.ClientSize = FullSizeTwoBinaryLines;
else
this.ClientSize = FullSize;
}
else
{
this.ClientSize = HalfSize;
}
}
else
{
txtInput.BackColor = Color.Salmon;
}
}
/// <summary>
/// Handle input of commands
/// </summary>
/// <param name="sender">calling object</param>
/// <param name="e">event arguments</param>
private void txtInput_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
{
bool handled = false;
switch (txtInput.Text)
{
// Set to mathematics mode
case "m":
this.Mode = CalculatorMode.Mathematics;
handled = true;
break;
// Set to programming mode
case "p":
this.Mode = CalculatorMode.Programming;
handled = true;
break;
// Enter "save/store" mode
default:
txtInput.Enabled = false;
txtStore.Visible = true;
txtStore.Focus();
break;
}
if (handled)
txtInput.Clear();
}
}
private void txtStore_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
{
string varName = txtStore.Text;
string varCalculation = txtInput.Text;
if (varName.Length > 0)
{
Variables.Instance.AddOrUpdateVariable(varName, varCalculation, this.Mode);
txtInput.Text = varName;
}
else
{
// error...
}
txtInput.Enabled = true;
txtStore.Visible = false;
txtStore.Text = String.Empty;
txtInput.Focus();
}
}
private void AdjustResultBoxToSizes(object sender, EventArgs e)
{
txtResultDecimal.Width = (int)Math.Abs((txtResultDecimal.Text.Length) * (txtResultDecimal.Font.Size+1));
//txtResultBits.Width = (int)Math.Abs((txtResultBits.Text.Length) * (txtResultBits.Font.Size+1));
txtResultHex.Width = (int)Math.Abs((txtResultHex.Text.Length) * (txtResultHex.Font.Size+1));
}
/// <summary>
/// Handle event when mouse is moved on top of the bits textbox and present a tooltip of the hovered value
/// </summary>
/// <param name="sender">calling object</param>
/// <param name="e">event arguments</param>
private void txtResultBits_MouseMove(object sender, MouseEventArgs e)
{
if (!timer_bitTooltip.Enabled)
{
Int32? bit = txtResultBits.GetBit(e.Location, 4);
if (bit.HasValue)
{
tooltip.ToolTipTitle = String.Format("Bit {0}", bit.Value);
Point p = txtResultBits.Location;
tooltip.Show(String.Format("1<<{0}={1}", bit.Value, 1<<bit.Value), this, p.X + e.X, p.Y + e.Y + 32);
timer_bitTooltip.Interval = 50;
timer_bitTooltip.Enabled = true;
}
else
{
tooltip.Hide(this);
}
}
}
/// <summary>
/// Handle event when mouse is mouved off from the bits textbox, hiding the tooltip
/// </summary>
/// <param name="sender">calling object</param>
/// <param name="e">event arguments</param>
private void txtResultBits_MouseLeave(object sender, EventArgs e)
{
tooltip.Hide(this);
}
/// <summary>
/// Handle fire event on tooltip timer. The tooltip timer prevents the tooltip from redrawing too often
/// </summary>
/// <param name="sender">calling object</param>
/// <param name="e">event arguments</param>
private void timer_bitTooltip_Tick(object sender, EventArgs e)
{
timer_bitTooltip.Enabled = false;
}
}
}