Skip to content

Commit

Permalink
- Improved hit testing API
Browse files Browse the repository at this point in the history
- Clicking on an element now set the selection next to clicked element (allow edits)
- Fixed: GetSelectionAfter/GetSelectionBefore on root element now returns selection at origin/end
- Removed all ByVal from method signatures
- Added LocationIn(Element), ConvertPoint(SourcePoint, SourceElem) and ConvertRect(SourceRect, SourceElem)
- Added SelectionChanged and SelectionChanging events to MathDocument
- Fixed: selection inside textedit (both points at origin or end) should be moved to outside the element
- Fixed: inserting '+' or '-' inside a number text edit if there's '+' or '-' already (change IsAccepted(Char, IsFirst) to IsAccepted(Char, Position))
- Added DisplayCharCode in UnicodeGlyph
  • Loading branch information
FremyCompany_cp authored and FremyCompany_cp committed Jun 13, 2012
1 parent eac2d2e commit 9a7671e
Show file tree
Hide file tree
Showing 19 changed files with 241 additions and 72 deletions.
7 changes: 5 additions & 2 deletions FC.MathEditor/TestApp/Module1.vb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
Module Module1

Sub Main()
Dim x As New System.Windows.Media.FontFamily("Consolas, Courier New")
Console.WriteLine(x.ToString())

Dim X As New FC.MathEditor.MathDocument()
Dim X1 = FC.MathEditor.IdentifierTextEdit.FromChar(Asc("x"))
Dim X2 = FC.MathEditor.IdentifierTextEdit.FromChar(Asc("y"))

Console.ReadLine()
End Sub

Expand Down
2 changes: 1 addition & 1 deletion FC.MathEditor/TestApp/My Project/Resources.Designer.vb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion FC.MathEditor/TestApp/My Project/Settings.Designer.vb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions FC.MathEditor/TestApp/TestApp.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@
<LastGenOutput>Settings.Designer.vb</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WpfMathEditor\EquationEditor.vbproj">
<Project>{EC8480D7-0F22-45EB-A359-FF6F443C3618}</Project>
<Name>EquationEditor</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
44 changes: 28 additions & 16 deletions FC.MathEditor/WpfMathEditor/Classes/Helpers/ExportHelper.vb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Public MustInherit Class ExportHelper

Protected WithEvents This As MathElement

Public Sub New(This As MathElement)
Me.This = This
End Sub
Expand Down Expand Up @@ -48,13 +49,13 @@
Return SB.ToString()
End Function

Public MustOverride Sub AppendKeyboardInput(ByVal SB As System.Text.StringBuilder)
Public MustOverride Sub AppendLaTeX(ByVal SB As System.Text.StringBuilder)
Public MustOverride Sub AppendMathML(ByVal SB As System.Text.StringBuilder)
Public MustOverride Sub AppendSimpleText(ByVal SB As System.Text.StringBuilder)
Public MustOverride Sub AppendKeyboardInput(SB As System.Text.StringBuilder)
Public MustOverride Sub AppendLaTeX(SB As System.Text.StringBuilder)
Public MustOverride Sub AppendMathML(SB As System.Text.StringBuilder)
Public MustOverride Sub AppendSimpleText(SB As System.Text.StringBuilder)

Protected MustOverride Sub Draw_Internal(ByVal DG As DrawingContext)
Public Sub Draw(ByVal DG As DrawingContext)
Protected MustOverride Sub Draw_Internal(DG As DrawingContext)
Public Sub Draw(DG As DrawingContext)


' Shift the drawing zone using the inner margin
Expand Down Expand Up @@ -134,7 +135,7 @@
''' Reset layout state to None in order to trigger relayout on next use.
''' </summary>
''' <param name="ForwardToParent">If True, indicates that the parent element should be reset, too.</param>
Public Sub InvalidateLayout(Optional ByVal ForwardToParent As Boolean = False)
Public Sub InvalidateLayout(Optional ForwardToParent As Boolean = False)

' Notify the layout should be recomputed
LayoutCompletion = LayoutCompletionState.None
Expand Down Expand Up @@ -226,7 +227,7 @@
Get
Return _LayoutCompletion
End Get
Protected Set(ByVal value As LayoutCompletionState)
Protected Set(value As LayoutCompletionState)
_LayoutCompletion = value
End Set
End Property
Expand Down Expand Up @@ -364,13 +365,6 @@
End Get
End Property

Private Shared Function FitRect(ChildRect As Rect, InitalParentRect As Rect, FinalParentRect As Rect) As Rect
ChildRect.Offset(-CType(InitalParentRect.Location, Vector))
ChildRect.Scale(FinalParentRect.Width / InitalParentRect.Width, FinalParentRect.Height / InitalParentRect.Height)
ChildRect.Offset(FinalParentRect.Location)
Return ChildRect
End Function

Public ReadOnly Property Size As Size
Get
PerformLayout()
Expand Down Expand Up @@ -455,14 +449,32 @@

Public ReadOnly Property LocationInRoot As Rect
Get

' Special case
If This.ParentElement Is Nothing Then Return LocationInParent

This.Root.Export.PerformLayout()
' Recursive case
This.Root.Export.PerformLayout() ' only for recursivity reasons
Return FitRect(LocationInParent, This.ParentElement.Export.SizeRect, This.ParentElement.Export.LocationInRoot)

End Get
End Property

Public ReadOnly Property LocationIn(Ancestror As MathElement) As Rect
Get

' Special cases
If This Is Ancestror Then Return SizeRect
If This.ParentElement Is Nothing Then Throw New ArgumentException("Ancestror wasn't an ancestror of the element whose LocationIn(Ancestror) has been requested")
If This.ParentElement Is Ancestror Then Return LocationInParent

' Recursive case
This.Root.Export.PerformLayout() ' only for recursivity reasons
Return FitRect(LocationInParent, This.ParentElement.Export.SizeRect, This.ParentElement.Export.LocationIn(Ancestror))

End Get
End Property

Public ReadOnly Property SelectionRectInParent() As Rect
Get

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
Private WithEvents _Parent As SelectionPoint
Private Valid As Boolean = True

Protected Sub ValidateIndex(ByVal ChildIndex As Integer)
Protected Sub ValidateIndex(ChildIndex As Integer)
If ChildIndex < 0 Then Throw New ArgumentException("ChildIndex can't be negative. It should be a positive number between 0 and SelectionHost.Children.Count.")
If ChildIndex > ParentElement.Children.Count Then Throw New ArgumentException("ChildIndex can't be superior to the number of children. It should be a positive number between 0 and SelectionHost.Children.Count.")
End Sub
Expand All @@ -74,7 +74,7 @@
Get
Return __Index
End Get
Set(ByVal value As Integer)
Set(value As Integer)

If Index <> value Then

Expand Down Expand Up @@ -272,6 +272,37 @@
GetParentPoint(EndPoint, EndPointAdvanced)
End While

' EXCEPTION: Selecting full content inside a text edit select the text edit itself
If StartPoint.ParentElement.IsTextEdit Then
If StartPoint.IsAtOrigin AndAlso StartPoint.IsAtEnd Then
' First case: Left to Right order
StartPoint = StartPoint.ParentElement.GetSelectionBefore()
EndPoint = StartPoint.Increment(1)
StartPointAdvanced = False
EndPointAdvanced = False
ElseIf StartPoint.IsAtEnd AndAlso EndPoint.IsAtOrigin Then
' Second case: Right to left order
EndPoint = StartPoint.ParentElement.GetSelectionBefore()
StartPoint = EndPoint.Increment(1)
StartPointAdvanced = False
EndPointAdvanced = False
ElseIf StartPoint = EndPoint Then
If StartPoint.IsAtOrigin Then
' Third case: Both at left
StartPoint = StartPoint.ParentElement.GetSelectionBefore()
EndPoint = StartPoint
StartPointAdvanced = False
EndPointAdvanced = False
ElseIf StartPoint.IsAtEnd Then
' Fourth case: Both at right
StartPoint = StartPoint.ParentElement.GetSelectionAfter()
EndPoint = StartPoint
StartPointAdvanced = False
EndPointAdvanced = False
End If
End If
End If

' Return the final result
If StartPoint.ChildIndex < EndPoint.ChildIndex Then
If EndPointAdvanced Then
Expand Down Expand Up @@ -315,14 +346,14 @@
'++ Detect changes
'++

Private Sub Host_ChildAdded(ByVal sender As Object, ByVal e As MathElement.TreeEventArgs) Handles _Host.ChildAdded
Private Sub Host_ChildAdded(sender As Object, e As MathElement.TreeEventArgs) Handles _Host.ChildAdded
' NOTE : < is not possible because the insertion point should move after the inserted element in the input flow
If e.ChildIndex <= Me.ChildIndex Then
Me.Index += 1
End If
End Sub

Private Sub Host_ChildRemoved(ByVal sender As Object, ByVal e As MathElement.TreeEventArgs) Handles _Host.ChildRemoved
Private Sub Host_ChildRemoved(sender As Object, e As MathElement.TreeEventArgs) Handles _Host.ChildRemoved
' NOTE : <= is not possible because, when the selection content is deleted, end and start point should be egal
If e.ChildIndex < Me.ChildIndex Then
Me.Index -= 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
''' <summary>
''' Called when the selection end or start point is being invalidated
''' </summary>
Private Sub SP_Invalidated(ByVal sender As Object, ByVal e As System.EventArgs) Handles SEP.Invalidated, SSP.Invalidated
Private Sub SP_Invalidated(sender As Object, e As System.EventArgs) Handles SEP.Invalidated, SSP.Invalidated
SetSelection(SSP.FirstValidParent, SEP.FirstValidParent)
End Sub

Expand Down
4 changes: 3 additions & 1 deletion FC.MathEditor/WpfMathEditor/Classes/MathElement.Input.vb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@
Return New SelectionHelper.SelectionPoint(Me, Me.Children.Count)
End Function

Public Function GetSelectionAt(ByVal Index As Integer) As SelectionHelper.SelectionPoint
Public Function GetSelectionAt(Index As Integer) As SelectionHelper.SelectionPoint
Return New SelectionHelper.SelectionPoint(Me, Index)
End Function

Public Function GetSelectionAfter() As SelectionHelper.SelectionPoint
If ParentElement Is Nothing Then Return GetSelectionAtEnd()
Return New SelectionHelper.SelectionPoint(ParentElement, Me.ChildIndex + 1)
End Function

Public Function GetSelectionBefore() As SelectionHelper.SelectionPoint
If ParentElement Is Nothing Then Return GetSelectionAtOrigin()
Return New SelectionHelper.SelectionPoint(ParentElement, Me.ChildIndex)
End Function

Expand Down
44 changes: 41 additions & 3 deletions FC.MathEditor/WpfMathEditor/Classes/MathElement.Layout.vb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Return Export.ToString()
End Function

Public Overridable Function GetElementFromRelativePoint(ByVal Location As Point) As SelectionHelper.SelectionPoint
Public Overridable Function GetElementFromRelativePoint(Location As Point, Optional ConsiderOnlyChildren As Boolean = False, Optional ShouldBeIncluded As Boolean = False) As MathElement

'
' Initialize variables for algorithm
Expand Down Expand Up @@ -44,10 +44,28 @@ NewBestFound:

Next

'
' Check post conditions and return result
'
If Not ConsiderOnlyChildren AndAlso BestIsIncluded Then Return BestElement.GetElementFromRelativePoint(BestElement.ConvertPoint(Location, Me), ConsiderOnlyChildren, ShouldBeIncluded)
If BestElement Is Nothing Then BestElement = Me : BestIsIncluded = True
If ShouldBeIncluded AndAlso Not BestIsIncluded Then BestElement = Me : BestIsIncluded = True

Return BestElement

End Function

Public Function GetSelectionPointFromRelativePoint(Location As Point) As SelectionHelper.SelectionPoint

'
' Get the best element
'
Dim BestElement = GetElementFromRelativePoint(Location)

'
' Create selection point from element
'
Dim TrueHDistance As Double = Location.X - (BestElement.Export.LocationInParent.Left + BestElement.Export.LocationInParent.Right) / 2
Dim TrueHDistance As Double = Location.X - (BestElement.Export.LocationIn(Me).Left + BestElement.Export.LocationIn(Me).Right) / 2
If TrueHDistance >= 0 Then
Return BestElement.GetSelectionAfter()
Else
Expand All @@ -56,6 +74,26 @@ NewBestFound:

End Function

''' <summary>
''' Convert a point relative to another element to a point relative to this element.
''' </summary>
''' <param name="Location">Original location to convert.</param>
''' <param name="RelativeTo">Element this location is relative to.</param>
Public Function ConvertPoint(Location As Point, RelativeTo As MathElement) As Point
Return ConvertRect(New Rect(Location, Location), RelativeTo).TopLeft
End Function

Public Function ConvertRect(Location As Rect, RelativeTo As MathElement) As Rect
' TODO: Check if this method is correct
Dim CA = RelativeTo.GetCommonAncestrorWith(Me)
Dim IntermediaryResult = FitRect(Location, RelativeTo.Export.SizeRect, RelativeTo.Export.LocationIn(CA))
Return FitRect(IntermediaryResult, Me.Export.LocationIn(CA), Me.Export.SizeRect)
End Function

'++
'++ Layout events
'++

Public Event SizeChanged As EventHandler
Public Event VisualChanged As EventHandler

Expand Down Expand Up @@ -206,7 +244,7 @@ NewBestFound:
End Set
End Property

Public Sub SetMathVariant(ByVal value As String)
Public Sub SetMathVariant(value As String)
If value Is Nothing Then
RemoveAttribute("mathvariant")
Else
Expand Down
18 changes: 17 additions & 1 deletion FC.MathEditor/WpfMathEditor/Classes/MathElement.XmlTree.vb
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,24 @@ Partial Public MustInherit Class MathElement
Return GetCommonAncestrorBetween(el1, Me)
End Function

''' <summary>
''' Returns the first element that's an ancestror of both el1 and el2 (bubbling).
''' </summary>
''' <param name="el1">The first element.</param>
''' <param name="el2">The second element.</param>
Public Shared Function GetCommonAncestrorBetween(el1 As MathElement, el2 As MathElement) As MathElement

'
' Check that the two elements belong to the same tree
'
If el1 Is Nothing Then Throw New ArgumentNullException("el1")
If el1 Is Nothing Then Throw New ArgumentNullException("el2")
If el1.Root IsNot el2.Root Then Throw New ArgumentException("It is impossible to find a common ancestror between el1 <" & el1.ToString() & "> and el2 <" & el2.ToString() & "> because they don't belong to the same tree.")

'
' Find common ancestror
'

Dim Delta As Integer = el2.TreeDepht - el1.TreeDepht
If Delta > 0 Then
For x = 1 To Delta
Expand Down Expand Up @@ -384,7 +400,7 @@ Partial Public MustInherit Class MathElement

End Function

Public Sub SetProperty(AttributeName As String, Parser As PropertyParser, ByVal value As Object)
Public Sub SetProperty(AttributeName As String, Parser As PropertyParser, value As Object)

Dim Result As Object = Nothing
SetAttribute(AttributeName, Parser.Serialize(value, Me))
Expand Down
10 changes: 10 additions & 0 deletions FC.MathEditor/WpfMathEditor/Classes/Modules/HelpersModule.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Friend Module HelpersModule

Public Function FitRect(ChildRect As Rect, InitalParentRect As Rect, FinalParentRect As Rect) As Rect
ChildRect.Offset(-CType(InitalParentRect.Location, Vector))
ChildRect.Scale(FinalParentRect.Width / InitalParentRect.Width, FinalParentRect.Height / InitalParentRect.Height)
ChildRect.Offset(FinalParentRect.Location)
Return ChildRect
End Function

End Module
2 changes: 1 addition & 1 deletion FC.MathEditor/WpfMathEditor/Classes/Types/Length.vb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
End Select
End Function

Public Function ToRoundPixels(Optional ByVal RelativeUnit As Double = 1) As Integer
Public Function ToRoundPixels(Optional RelativeUnit As Double = 1) As Integer
Return Math.Round(ToPixels(RelativeUnit))
End Function

Expand Down
1 change: 1 addition & 0 deletions FC.MathEditor/WpfMathEditor/EquationEditor.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
<Compile Include="Classes\MathElement.Layout.vb" />
<Compile Include="Classes\MathElement.XmlTree.vb" />
<Compile Include="Classes\MathElement.Type.vb" />
<Compile Include="Classes\Modules\HelpersModule.vb" />
<Compile Include="Classes\Types\Length.vb" />
<Compile Include="Classes\UserCallableAttribute.vb" />
<Compile Include="Formatters\FormatterChildrenHelper.vb" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Public Class FormatterChildrenHelper : Inherits ChildrenHelper

Public Sub New(ByVal El As MathElement)
Public Sub New(El As MathElement)
MyBase.New(El)
End Sub

Expand Down Expand Up @@ -46,7 +46,7 @@ Public Class FormatterChildrenHelper : Inherits ChildrenHelper
End Get
End Property

Protected Overrides Sub ValidateNewElement_Internal(ByVal NewElement As MathElement)
Protected Overrides Sub ValidateNewElement_Internal(NewElement As MathElement)
MyBase.ValidateNewElement_Internal(NewElement)
If Not (TypeOf NewElement Is RowLayoutEngine) Then Throw New ArgumentException("Un élément au design fixé (" & Me.GetType().Name & ") ne peut avoir que des RowLayoutEngine pour enfant.")
End Sub
Expand Down
Loading

0 comments on commit 9a7671e

Please sign in to comment.