|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include "pch.h" |
| 5 | +#include "EquationTextBox.h" |
| 6 | +#include "CalcViewModel/GraphingCalculator/EquationViewModel.h" |
| 7 | + |
| 8 | +using namespace std; |
| 9 | +using namespace Platform; |
| 10 | +using namespace CalculatorApp; |
| 11 | +using namespace CalculatorApp::Common; |
| 12 | +using namespace CalculatorApp::Controls; |
| 13 | +using namespace CalculatorApp::ViewModel; |
| 14 | +using namespace Windows::Foundation; |
| 15 | +using namespace Windows::UI::Xaml; |
| 16 | +using namespace Windows::UI::Xaml::Controls; |
| 17 | +using namespace Windows::UI::Xaml::Input; |
| 18 | +using namespace Windows::UI::Xaml::Controls::Primitives; |
| 19 | +using namespace Windows::UI::Text; |
| 20 | + |
| 21 | +DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, EquationColor); |
| 22 | +DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, KeyGraphFeaturesContent); |
| 23 | +DEPENDENCY_PROPERTY_INITIALIZATION(EquationTextBox, ColorChooserFlyout); |
| 24 | + |
| 25 | +void EquationTextBox::OnApplyTemplate() |
| 26 | +{ |
| 27 | + m_equationButton = dynamic_cast<Button^>(GetTemplateChild("EquationButton")); |
| 28 | + m_richEditBox = dynamic_cast<RichEditBox^>(GetTemplateChild("EquationTextBox")); |
| 29 | + m_deleteButton = dynamic_cast<Button^>(GetTemplateChild("DeleteButton")); |
| 30 | + m_removeButton = dynamic_cast<Button^>(GetTemplateChild("RemoveButton")); |
| 31 | + m_functionButton = dynamic_cast<Button^>(GetTemplateChild("FunctionButton")); |
| 32 | + m_colorChooserButton = dynamic_cast<ToggleButton^>(GetTemplateChild("ColorChooserButton")); |
| 33 | + |
| 34 | + if (m_richEditBox != nullptr) |
| 35 | + { |
| 36 | + m_richEditBox->GotFocus += ref new RoutedEventHandler(this, &EquationTextBox::OnRichEditBoxGotFocus); |
| 37 | + m_richEditBox->LostFocus += ref new RoutedEventHandler(this, &EquationTextBox::OnRichEditBoxLostFocus); |
| 38 | + m_richEditBox->TextChanged += ref new RoutedEventHandler(this, &EquationTextBox::OnRichEditBoxTextChanged); |
| 39 | + } |
| 40 | + |
| 41 | + if (m_equationButton != nullptr) |
| 42 | + { |
| 43 | + m_equationButton->Click += ref new RoutedEventHandler(this, &EquationTextBox::OnEquationButtonClicked); |
| 44 | + } |
| 45 | + |
| 46 | + if (m_deleteButton != nullptr) |
| 47 | + { |
| 48 | + m_deleteButton->Click += ref new RoutedEventHandler(this, &EquationTextBox::OnDeleteButtonClicked); |
| 49 | + } |
| 50 | + |
| 51 | + if (m_removeButton != nullptr) |
| 52 | + { |
| 53 | + m_removeButton->Click += ref new RoutedEventHandler(this, &EquationTextBox::OnRemoveButtonClicked); |
| 54 | + } |
| 55 | + |
| 56 | + if (m_colorChooserButton != nullptr) |
| 57 | + { |
| 58 | + m_colorChooserButton->Click += ref new RoutedEventHandler(this, &EquationTextBox::OnColorChooserButtonClicked); |
| 59 | + } |
| 60 | + |
| 61 | + if (m_functionButton != nullptr) |
| 62 | + { |
| 63 | + m_functionButton->Click += ref new RoutedEventHandler(this, &EquationTextBox::OnFunctionButtonClicked); |
| 64 | + } |
| 65 | + |
| 66 | + if (ColorChooserFlyout != nullptr) |
| 67 | + { |
| 68 | + ColorChooserFlyout->Opened += ref new EventHandler<Object^>(this, &EquationTextBox::OnColorFlyoutOpened); |
| 69 | + ColorChooserFlyout->Closed += ref new EventHandler<Object^>(this, &EquationTextBox::OnColorFlyoutClosed); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +void EquationTextBox::OnPointerEntered(PointerRoutedEventArgs^ e) |
| 74 | +{ |
| 75 | + m_isPointerOver = true; |
| 76 | + UpdateCommonVisualState(); |
| 77 | +} |
| 78 | + |
| 79 | +void EquationTextBox::OnPointerExited(PointerRoutedEventArgs^ e) |
| 80 | +{ |
| 81 | + m_isPointerOver = false; |
| 82 | + UpdateCommonVisualState(); |
| 83 | +} |
| 84 | + |
| 85 | +void EquationTextBox::OnPointerCanceled(PointerRoutedEventArgs^ e) |
| 86 | +{ |
| 87 | + m_isPointerOver = false; |
| 88 | + UpdateCommonVisualState(); |
| 89 | +} |
| 90 | + |
| 91 | +void EquationTextBox::OnPointerCaptureLost(PointerRoutedEventArgs^ e) |
| 92 | +{ |
| 93 | + m_isPointerOver = false; |
| 94 | + UpdateCommonVisualState(); |
| 95 | +} |
| 96 | + |
| 97 | +void EquationTextBox::OnColorFlyoutOpened(Object^ sender, Object^ e) |
| 98 | +{ |
| 99 | + m_isColorChooserFlyoutOpen = true; |
| 100 | + UpdateCommonVisualState(); |
| 101 | +} |
| 102 | + |
| 103 | +void EquationTextBox::OnColorFlyoutClosed(Object^ sender, Object^ e) |
| 104 | +{ |
| 105 | + m_colorChooserButton->IsChecked = false; |
| 106 | + m_isColorChooserFlyoutOpen = false; |
| 107 | + UpdateCommonVisualState(); |
| 108 | +} |
| 109 | + |
| 110 | +void EquationTextBox::OnRichEditBoxTextChanged(Object^ sender, RoutedEventArgs^ e) |
| 111 | +{ |
| 112 | + UpdateDeleteButtonVisualState(); |
| 113 | +} |
| 114 | + |
| 115 | +void EquationTextBox::OnRichEditBoxGotFocus(Object^ sender, RoutedEventArgs^ e) |
| 116 | +{ |
| 117 | + m_isFocused = true; |
| 118 | + UpdateCommonVisualState(); |
| 119 | + UpdateDeleteButtonVisualState(); |
| 120 | +} |
| 121 | + |
| 122 | +void EquationTextBox::OnRichEditBoxLostFocus(Object^ sender, RoutedEventArgs^ e) |
| 123 | +{ |
| 124 | + m_isFocused = false; |
| 125 | + UpdateCommonVisualState(); |
| 126 | + UpdateDeleteButtonVisualState(); |
| 127 | +} |
| 128 | + |
| 129 | +void EquationTextBox::OnDeleteButtonClicked(Object^ sender, RoutedEventArgs^ e) |
| 130 | +{ |
| 131 | + if (m_richEditBox != nullptr) |
| 132 | + { |
| 133 | + m_richEditBox->TextDocument->SetText(::TextSetOptions::None, L""); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +void EquationTextBox::OnEquationButtonClicked(Object^ sender, RoutedEventArgs^ e) |
| 138 | +{ |
| 139 | + |
| 140 | +} |
| 141 | + |
| 142 | +void EquationTextBox::OnRemoveButtonClicked(Object^ sender, RoutedEventArgs^ e) |
| 143 | +{ |
| 144 | + RemoveButtonClicked(this, ref new RoutedEventArgs()); |
| 145 | +} |
| 146 | + |
| 147 | +void EquationTextBox::OnColorChooserButtonClicked(Object^ sender, RoutedEventArgs^ e) |
| 148 | +{ |
| 149 | + if (ColorChooserFlyout != nullptr && m_richEditBox != nullptr) |
| 150 | + { |
| 151 | + ColorChooserFlyout->ShowAt(m_richEditBox); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +void EquationTextBox::OnFunctionButtonClicked(Object^ sender, RoutedEventArgs^ e) |
| 156 | +{ |
| 157 | + auto equationViewModel = static_cast<EquationViewModel^>(DataContext); |
| 158 | + equationViewModel->KeyGraphFeaturesVisibility = (equationViewModel->KeyGraphFeaturesVisibility == ::Visibility::Collapsed) ? ::Visibility::Visible : ::Visibility::Collapsed; |
| 159 | + UpdateCommonVisualState(); |
| 160 | +} |
| 161 | + |
| 162 | +void EquationTextBox::UpdateDeleteButtonVisualState() |
| 163 | +{ |
| 164 | + String^ state; |
| 165 | + |
| 166 | + if (ShouldDeleteButtonBeVisible()) |
| 167 | + { |
| 168 | + state = "ButtonVisible"; |
| 169 | + } |
| 170 | + else |
| 171 | + { |
| 172 | + state = "ButtonCollapsed"; |
| 173 | + } |
| 174 | + |
| 175 | + VisualStateManager::GoToState(this, state, true); |
| 176 | +} |
| 177 | + |
| 178 | +void EquationTextBox::UpdateCommonVisualState() |
| 179 | +{ |
| 180 | + String^ state = "Normal"; |
| 181 | + |
| 182 | + if (m_isFocused) |
| 183 | + { |
| 184 | + state = "Focused"; |
| 185 | + } |
| 186 | + else if (m_isPointerOver || m_isColorChooserFlyoutOpen) |
| 187 | + { |
| 188 | + state = "PointerOver"; |
| 189 | + } |
| 190 | + |
| 191 | + VisualStateManager::GoToState(this, state, true); |
| 192 | +} |
| 193 | + |
| 194 | + |
| 195 | +Platform::String^ EquationTextBox::GetEquationText() |
| 196 | +{ |
| 197 | + String^ text; |
| 198 | + |
| 199 | + if (m_richEditBox != nullptr) |
| 200 | + { |
| 201 | + m_richEditBox->TextDocument->GetText(::TextGetOptions::NoHidden, &text); |
| 202 | + } |
| 203 | + |
| 204 | + return text; |
| 205 | +} |
| 206 | + |
| 207 | +void EquationTextBox::SetEquationText(Platform::String^ equationText) |
| 208 | +{ |
| 209 | + if (m_richEditBox != nullptr) |
| 210 | + { |
| 211 | + m_richEditBox->TextDocument->SetText(::TextSetOptions::None, equationText); |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | + |
| 216 | +bool EquationTextBox::ShouldDeleteButtonBeVisible() |
| 217 | +{ |
| 218 | + return (!GetEquationText()->IsEmpty() && m_isFocused); |
| 219 | + |
| 220 | +} |
0 commit comments