@@ -115,41 +115,86 @@ void MathRichEditBox::SetMathTextProperty(String ^ newValue)
115115 }
116116
117117 this ->IsReadOnly = readOnlyState;
118- SetValue (MathTextProperty, newValue);
119118}
120119
121120void CalculatorApp::Controls::MathRichEditBox::OnLosingFocus (Windows::UI::Xaml::UIElement ^ sender, Windows::UI::Xaml::Input::LosingFocusEventArgs ^ args)
122121{
123- auto newVal = GetMathTextProperty ();
124- if (MathText != newVal)
125- {
126- SetValue (MathTextProperty, newVal);
127- EquationSubmitted (this , ref new MathRichEditBoxSubmission (true , EquationSubmissionSource::FOCUS_LOST));
128- }
129- else
130- {
131- EquationSubmitted (this , ref new MathRichEditBoxSubmission (false , EquationSubmissionSource::FOCUS_LOST));
132- }
122+ SubmitEquation (EquationSubmissionSource::FOCUS_LOST);
133123}
134124
135125void CalculatorApp::Controls::MathRichEditBox::OnKeyUp (Platform::Object ^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs ^ e)
136126{
137127 if (e->Key == VirtualKey::Enter)
138128 {
139- auto newVal = GetMathTextProperty ();
140- if (MathText != newVal)
141- {
142- SetValue (MathTextProperty, newVal);
143- EquationSubmitted (this , ref new MathRichEditBoxSubmission (true , EquationSubmissionSource::ENTER_KEY));
144- }
145- else
146- {
147- EquationSubmitted (this , ref new MathRichEditBoxSubmission (true , EquationSubmissionSource::ENTER_KEY));
148- }
129+ SubmitEquation (EquationSubmissionSource::ENTER_KEY);
149130 }
150131}
151132
152133void MathRichEditBox::OnMathTextPropertyChanged (Platform::String ^ oldValue, Platform::String ^ newValue)
153134{
154135 SetMathTextProperty (newValue);
136+ SetValue (MathTextProperty, newValue);
137+ }
138+
139+ void MathRichEditBox::InsertText (Platform::String ^ text, int cursorOffSet, int selectionLength)
140+ {
141+ // If the rich edit is empty, the math zone may not exist, and so selection (and thus the resulting text) will not be in a math zone.
142+ // If the rich edit has content already, then the mathzone will already be created due to mathonly mode being set and the selection will exist inside the
143+ // math zone. To handle this, we will force a math zone to be created in teh case of the text being empty and then replacing the text inside of the math
144+ // zone with the newly inserted text.
145+ if (GetMathTextProperty () == nullptr )
146+ {
147+ SetMathTextProperty (" <math xmlns=\" http://www.w3.org/1998/Math/MathML\" ><mi>x</mi></math>" );
148+ TextDocument->Selection ->StartPosition = 0 ;
149+ TextDocument->Selection ->EndPosition = 1 ;
150+ }
151+
152+ // insert the text in place of selection
153+ TextDocument->Selection ->SetText (Windows::UI::Text::TextSetOptions::FormatRtf, text);
154+
155+ // Move the cursor to the next logical place for users to enter text.
156+ TextDocument->Selection ->StartPosition += cursorOffSet;
157+ TextDocument->Selection ->EndPosition = TextDocument->Selection ->StartPosition + selectionLength;
158+ }
159+
160+ void MathRichEditBox::BackSpace ()
161+ {
162+ // if anything is selected, just delete the selection. Note: EndPosition can be before start position.
163+ if (TextDocument->Selection ->StartPosition != TextDocument->Selection ->EndPosition )
164+ {
165+ TextDocument->Selection ->SetText (Windows::UI::Text::TextSetOptions::None, L" " );
166+ return ;
167+ }
168+
169+ // if we are at the start of the string, do nothing
170+ if (TextDocument->Selection ->StartPosition == 0 )
171+ {
172+ return ;
173+ }
174+
175+ // Select the previous group.
176+ TextDocument->Selection ->EndPosition = TextDocument->Selection ->StartPosition ;
177+ TextDocument->Selection ->StartPosition -= 1 ;
178+
179+ // If the group contains anything complex, we want to give the user a chance to preview the deletion.
180+ // If it's a single character, then just delete it. Otherwise do nothing until the user triggers backspace again.
181+ auto text = TextDocument->Selection ->Text ;
182+ if (text->Length () == 1 )
183+ {
184+ TextDocument->Selection ->SetText (Windows::UI::Text::TextSetOptions::None, L" " );
185+ }
186+ }
187+
188+ void MathRichEditBox::SubmitEquation (EquationSubmissionSource source)
189+ {
190+ auto newVal = GetMathTextProperty ();
191+ if (MathText != newVal)
192+ {
193+ SetValue (MathTextProperty, newVal);
194+ EquationSubmitted (this , ref new MathRichEditBoxSubmission (true , source));
195+ }
196+ else
197+ {
198+ EquationSubmitted (this , ref new MathRichEditBoxSubmission (false , source));
199+ }
155200}
0 commit comments