forked from microsoft/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StandardCalculatorViewModel.h
380 lines (346 loc) · 17.3 KB
/
StandardCalculatorViewModel.h
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include "Common/Automation/NarratorAnnouncement.h"
#include "Common/DisplayExpressionToken.h"
#include "Common/CalculatorDisplay.h"
#include "Common/EngineResourceProvider.h"
#include "Common/CalculatorButtonUser.h"
#include "HistoryViewModel.h"
#include "MemoryItemViewModel.h"
#include "Common/BitLength.h"
#include "Common/NumberBase.h"
namespace CalculatorFunctionalTests
{
class HistoryTests;
}
namespace CalculatorUnitTests
{
class MultiWindowUnitTests;
}
namespace CalculatorApp
{
namespace WS = Windows::System;
namespace CM = CalculationManager;
namespace ViewModel
{
#define ASCII_0 48
public delegate void HideMemoryClickedHandler();
public value struct ButtonInfo
{
NumbersAndOperatorsEnum buttonId;
bool canSendNegate;
};
[Windows::UI::Xaml::Data::Bindable] public ref class StandardCalculatorViewModel sealed : public Windows::UI::Xaml::Data::INotifyPropertyChanged
{
public:
StandardCalculatorViewModel();
void UpdateOperand(int pos, Platform::String ^ text);
void UpdateCommandsInRecordingMode();
OBSERVABLE_OBJECT_CALLBACK(OnPropertyChanged);
OBSERVABLE_PROPERTY_RW(Platform::String ^, DisplayValue);
OBSERVABLE_PROPERTY_R(HistoryViewModel ^, HistoryVM);
OBSERVABLE_PROPERTY_RW(bool, IsAlwaysOnTop);
OBSERVABLE_PROPERTY_R(bool, IsBinaryBitFlippingEnabled);
PROPERTY_R(bool, IsOperandUpdatedUsingViewModel);
PROPERTY_R(int, TokenPosition);
PROPERTY_R(bool, IsOperandTextCompletelySelected);
PROPERTY_R(bool, KeyPressed);
PROPERTY_R(Platform::String ^, SelectedExpressionLastData);
OBSERVABLE_NAMED_PROPERTY_R(bool, IsInError);
OBSERVABLE_PROPERTY_R(bool, IsOperatorCommand);
OBSERVABLE_PROPERTY_R(Windows::Foundation::Collections::IObservableVector<Common::DisplayExpressionToken ^> ^, ExpressionTokens);
OBSERVABLE_PROPERTY_R(Platform::String ^, DecimalDisplayValue);
OBSERVABLE_PROPERTY_R(Platform::String ^, HexDisplayValue);
OBSERVABLE_PROPERTY_R(Platform::String ^, OctalDisplayValue);
OBSERVABLE_NAMED_PROPERTY_R(Platform::String ^, BinaryDisplayValue);
OBSERVABLE_NAMED_PROPERTY_R(Windows::Foundation::Collections::IVector<bool> ^, BinaryDigits);
OBSERVABLE_PROPERTY_R(Platform::String ^, HexDisplayValue_AutomationName);
OBSERVABLE_PROPERTY_R(Platform::String ^, DecDisplayValue_AutomationName);
OBSERVABLE_PROPERTY_R(Platform::String ^, OctDisplayValue_AutomationName);
OBSERVABLE_PROPERTY_R(Platform::String ^, BinDisplayValue_AutomationName);
OBSERVABLE_PROPERTY_R(bool, IsBinaryOperatorEnabled);
OBSERVABLE_PROPERTY_R(bool, IsUnaryOperatorEnabled);
OBSERVABLE_PROPERTY_R(bool, IsNegateEnabled);
OBSERVABLE_PROPERTY_RW(bool, IsDecimalEnabled);
OBSERVABLE_PROPERTY_R(Windows::Foundation::Collections::IVector<MemoryItemViewModel ^> ^, MemorizedNumbers);
OBSERVABLE_NAMED_PROPERTY_RW(bool, IsMemoryEmpty);
OBSERVABLE_PROPERTY_R(bool, IsFToEChecked);
OBSERVABLE_PROPERTY_R(bool, IsFToEEnabled);
OBSERVABLE_PROPERTY_R(bool, AreHEXButtonsEnabled);
OBSERVABLE_PROPERTY_R(Platform::String ^, CalculationResultAutomationName);
OBSERVABLE_PROPERTY_R(Platform::String ^, CalculationExpressionAutomationName);
OBSERVABLE_PROPERTY_R(bool, IsShiftProgrammerChecked);
OBSERVABLE_PROPERTY_R(CalculatorApp::Common::NumberBase, CurrentRadixType);
OBSERVABLE_PROPERTY_R(bool, AreTokensUpdated);
OBSERVABLE_PROPERTY_R(bool, AreAlwaysOnTopResultsUpdated);
OBSERVABLE_PROPERTY_RW(bool, AreHistoryShortcutsEnabled);
OBSERVABLE_PROPERTY_R(bool, AreProgrammerRadixOperatorsEnabled);
OBSERVABLE_PROPERTY_R(bool, IsInputEmpty);
OBSERVABLE_PROPERTY_R(CalculatorApp::Common::Automation::NarratorAnnouncement ^, Announcement);
OBSERVABLE_PROPERTY_R(unsigned int, OpenParenthesisCount);
COMMAND_FOR_METHOD(CopyCommand, StandardCalculatorViewModel::OnCopyCommand);
COMMAND_FOR_METHOD(PasteCommand, StandardCalculatorViewModel::OnPasteCommand);
COMMAND_FOR_METHOD(ButtonPressed, StandardCalculatorViewModel::OnButtonPressed);
COMMAND_FOR_METHOD(ClearMemoryCommand, StandardCalculatorViewModel::OnClearMemoryCommand);
COMMAND_FOR_METHOD(MemoryItemPressed, StandardCalculatorViewModel::OnMemoryItemPressed);
COMMAND_FOR_METHOD(MemoryAdd, StandardCalculatorViewModel::OnMemoryAdd);
COMMAND_FOR_METHOD(MemorySubtract, StandardCalculatorViewModel::OnMemorySubtract);
event HideMemoryClickedHandler ^ HideMemoryClicked;
property bool IsBitFlipChecked
{
bool get()
{
return m_isBitFlipChecked;
}
void set(bool value)
{
if (m_isBitFlipChecked != value)
{
m_isBitFlipChecked = value;
IsBinaryBitFlippingEnabled = IsProgrammer && m_isBitFlipChecked;
AreProgrammerRadixOperatorsEnabled = IsProgrammer && !m_isBitFlipChecked;
RaisePropertyChanged(L"IsBitFlipChecked");
}
}
}
static property Platform::String
^ IsBitFlipCheckedPropertyName { Platform::String ^ get() { return Platform::StringReference(L"IsBitFlipChecked"); } }
property CalculatorApp::Common::BitLength ValueBitLength
{
CalculatorApp::Common::BitLength get()
{
return m_valueBitLength;
}
void set(CalculatorApp::Common::BitLength value);
}
property bool IsStandard
{
bool get()
{
return m_isStandard;
}
void set(bool value)
{
if (m_isStandard != value)
{
m_isStandard = value;
if (value)
{
IsScientific = false;
IsProgrammer = false;
}
RaisePropertyChanged(L"IsStandard");
}
}
}
property bool IsScientific
{
bool get()
{
return m_isScientific;
}
void set(bool value)
{
if (m_isScientific != value)
{
m_isScientific = value;
if (value)
{
IsStandard = false;
IsProgrammer = false;
}
RaisePropertyChanged(L"IsScientific");
}
}
}
property bool IsProgrammer
{
bool get()
{
return m_isProgrammer;
}
void set(bool value)
{
if (m_isProgrammer != value)
{
m_isProgrammer = value;
if (!m_isProgrammer)
{
IsBitFlipChecked = false;
}
IsBinaryBitFlippingEnabled = m_isProgrammer && IsBitFlipChecked;
AreProgrammerRadixOperatorsEnabled = m_isProgrammer && !IsBitFlipChecked;
if (value)
{
IsStandard = false;
IsScientific = false;
}
RaisePropertyChanged(L"IsProgrammer");
}
}
}
static property Platform::String
^ IsProgrammerPropertyName { Platform::String ^ get() { return Platform::StringReference(L"IsProgrammer"); } }
property bool IsEditingEnabled
{
bool get()
{
return m_isEditingEnabled;
}
void set(bool value)
{
if (m_isEditingEnabled != value)
{
m_isEditingEnabled = value;
bool currentEditToggleValue = !m_isEditingEnabled;
IsBinaryOperatorEnabled = currentEditToggleValue;
IsUnaryOperatorEnabled = currentEditToggleValue;
IsOperandEnabled = currentEditToggleValue;
IsNegateEnabled = currentEditToggleValue;
IsDecimalEnabled = currentEditToggleValue;
RaisePropertyChanged(L"IsEditingEnabled");
}
}
}
property bool IsEngineRecording
{
bool get()
{
return m_standardCalculatorManager.IsEngineRecording();
}
}
property bool IsOperandEnabled
{
bool get()
{
return m_isOperandEnabled;
}
void set(bool value)
{
if (m_isOperandEnabled != value)
{
m_isOperandEnabled = value;
IsDecimalEnabled = value;
AreHEXButtonsEnabled = IsProgrammer;
IsFToEEnabled = value;
RaisePropertyChanged(L"IsOperandEnabled");
}
}
}
internal :
void OnPaste(Platform::String ^ pastedString);
void OnCopyCommand(Platform::Object ^ parameter);
void OnPasteCommand(Platform::Object ^ parameter);
ButtonInfo MapCharacterToButtonId(char16 ch);
// Memory feature related methods. They are internal because they need to called from the MainPage code-behind
void OnMemoryButtonPressed();
void OnMemoryItemPressed(Platform::Object ^ memoryItemPosition);
void OnMemoryAdd(Platform::Object ^ memoryItemPosition);
void OnMemorySubtract(Platform::Object ^ memoryItemPosition);
void OnMemoryClear(_In_ Platform::Object ^ memoryItemPosition);
void OnInputChanged();
void DisplayPasteError();
void SetParenthesisCount(_In_ unsigned int parenthesisCount);
void SetOpenParenthesisCountNarratorAnnouncement();
void OnNoRightParenAdded();
void SetNoParenAddedNarratorAnnouncement();
void OnMaxDigitsReached();
void OnBinaryOperatorReceived();
void OnMemoryItemChanged(unsigned int indexOfMemory);
Platform::String ^ GetLocalizedStringFormat(Platform::String ^ format, Platform::String ^ displayValue);
void OnPropertyChanged(Platform::String ^ propertyname);
void SetCalculatorType(CalculatorApp::Common::ViewMode targetState);
Platform::String ^ GetRawDisplayValue();
void Recalculate(bool fromHistory = false);
bool IsOperator(CalculationManager::Command cmdenum);
void FtoEButtonToggled();
void SwitchProgrammerModeBase(CalculatorApp::Common::NumberBase calculatorBase);
void SetMemorizedNumbersString();
void SwitchAngleType(NumbersAndOperatorsEnum num);
void ResetDisplay();
void SetPrecision(int32_t precision);
void UpdateMaxIntDigits()
{
m_standardCalculatorManager.UpdateMaxIntDigits();
}
NumbersAndOperatorsEnum GetCurrentAngleType()
{
return m_CurrentAngleType;
}
void SelectHistoryItem(HistoryItemViewModel ^ item);
private:
void SetMemorizedNumbers(const std::vector<std::wstring>& memorizedNumbers);
void UpdateProgrammerPanelDisplay();
void HandleUpdatedOperandData(CalculationManager::Command cmdenum);
void SetPrimaryDisplay(_In_ Platform::String ^ displayStringValue, _In_ bool isError);
void SetExpressionDisplay(
_Inout_ std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& tokens,
_Inout_ std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> const& commands);
void SetHistoryExpressionDisplay(
_Inout_ std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& tokens,
_Inout_ std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> const& commands);
void SetTokens(_Inout_ std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& tokens);
NumbersAndOperatorsEnum ConvertIntegerToNumbersAndOperatorsEnum(unsigned int parameter);
static RADIX_TYPE GetRadixTypeFromNumberBase(CalculatorApp::Common::NumberBase base);
NumbersAndOperatorsEnum m_CurrentAngleType;
wchar_t m_decimalSeparator;
CalculatorDisplay m_calculatorDisplay;
CalculatorApp::EngineResourceProvider m_resourceProvider;
CalculationManager::CalculatorManager m_standardCalculatorManager;
Platform::String ^ m_expressionAutomationNameFormat;
Platform::String ^ m_localizedCalculationResultAutomationFormat;
Platform::String ^ m_localizedCalculationResultDecimalAutomationFormat;
Platform::String ^ m_localizedHexaDecimalAutomationFormat;
Platform::String ^ m_localizedDecimalAutomationFormat;
Platform::String ^ m_localizedOctalAutomationFormat;
Platform::String ^ m_localizedBinaryAutomationFormat;
Platform::String ^ m_localizedMaxDigitsReachedAutomationFormat;
Platform::String ^ m_localizedButtonPressFeedbackAutomationFormat;
Platform::String ^ m_localizedMemorySavedAutomationFormat;
Platform::String ^ m_localizedMemoryItemChangedAutomationFormat;
Platform::String ^ m_localizedMemoryItemClearedAutomationFormat;
Platform::String ^ m_localizedMemoryCleared;
Platform::String ^ m_localizedOpenParenthesisCountChangedAutomationFormat;
Platform::String ^ m_localizedNoRightParenthesisAddedFormat;
bool m_isOperandEnabled;
bool m_isEditingEnabled;
bool m_isStandard;
bool m_isScientific;
bool m_isProgrammer;
bool m_isBitFlipChecked;
bool m_isRtlLanguage;
bool m_operandUpdated;
bool m_isLastOperationHistoryLoad;
CalculatorApp::Common::BitLength m_valueBitLength;
Platform::String ^ m_selectedExpressionLastData;
Common::DisplayExpressionToken ^ m_selectedExpressionToken;
Platform::String ^ LocalizeDisplayValue(_In_ std::wstring const& displayValue, _In_ bool isError);
Platform::String
^ CalculateNarratorDisplayValue(_In_ std::wstring const& displayValue, _In_ Platform::String ^ localizedDisplayValue, _In_ bool isError);
CalculatorApp::Common::Automation::NarratorAnnouncement ^ GetDisplayUpdatedNarratorAnnouncement();
Platform::String ^ GetCalculatorExpressionAutomationName();
Platform::String ^ GetNarratorStringReadRawNumbers(_In_ Platform::String ^ localizedDisplayValue);
CalculationManager::Command ConvertToOperatorsEnum(NumbersAndOperatorsEnum operation);
void DisableButtons(CalculationManager::CommandType selectedExpressionCommandType);
Platform::String ^ m_feedbackForButtonPress;
void OnButtonPressed(Platform::Object ^ parameter);
void OnClearMemoryCommand(Platform::Object ^ parameter);
std::wstring AddPadding(std::wstring);
size_t LengthWithoutPadding(std::wstring);
std::shared_ptr<std::vector<std::pair<std::wstring, int>>> m_tokens;
std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> m_commands;
// Token types
bool IsUnaryOp(CalculationManager::Command command);
bool IsBinOp(CalculationManager::Command command);
bool IsTrigOp(CalculationManager::Command command);
bool IsOpnd(CalculationManager::Command command);
bool IsRecoverableCommand(CalculationManager::Command command);
void SaveEditedCommand(_In_ unsigned int index, _In_ CalculationManager::Command command);
CalculatorApp::Common::ViewMode GetCalculatorMode();
friend class CalculatorDisplay;
friend class CalculatorFunctionalTests::HistoryTests;
friend class CalculatorUnitTests::MultiWindowUnitTests;
};
}
}