Files
EgtWPFLib/EgtCalculator.xaml.vb
2023-10-10 18:54:46 +02:00

422 lines
16 KiB
VB.net

'----------------------------------------------------------------------------
' EgalTech 2014-2015
'----------------------------------------------------------------------------
' File : EgtCalculator.xaml.vb Data : 28.12.15 Versione : 1
' Contenuto : Calcolatrice touch per EgtWPFLib.
'
' Creato da : Emmanuele Sassi
' Modificato da :
'
' Modifiche :
'
'----------------------------------------------------------------------------
Imports System.Windows.Interop
Imports System.Globalization
Imports EgtUILib
Public Class EgtCalculator
Private Const ASPECTRATIO = 1
Private Const WIDTHFONTRATIO = 22 / 426.5
Private Const BUTTONIMAGERATIO = 65 / 85.3
Private Const MINIMUMDIMENSION = 200
' Variabile statica che indica se è già aperta una tastiera
Private Shared bIsActive As Boolean = False
'Parametri ricevuti
Private m_Width As Double = 0
Private m_WidthType As WidthType = WidthType.NULL
' Riferimento alla finestra che contiene la EgtCalculator
Private m_Owner As Window
' Riferimento alla TxBx che ha attivato la tastiera
Private m_SourceTxBx As Primitives.TextBoxBase = Nothing
' Valore ricevuto nel caso in cui la calcolatrice non sia attivata da una textbox
Private m_dStartValue As Double = 0
' Stringa che contiene il messaggio d'errore della calcolatrice
Private m_sErrorString As String = String.Empty
' Flag che indica se c'è stato un errore
Private m_bErrorState As Boolean = False
' Variabile che conserva l'espressione prima di valutarla per poterla riscrivere nel caso generi un errore
Private m_sBeforeEvaluate As String = String.Empty
' Variabile che contiene il risultato numerico
Private m_DoubleResult As Double
' Flag che indica se il risultato è valido o è stato annullato
Private m_bConfirmedResult As Boolean = False
' Evento che indica la chiusura della tastiera
Public Event EgtClosed As EventHandler
Public ReadOnly Property dResult As Double
Get
Return m_DoubleResult
End Get
End Property
Public ReadOnly Property bConfirmResult As Boolean
Get
Return m_bConfirmedResult
End Get
End Property
Public ReadOnly Property dFontSize As Double
Get
Return Width * WIDTHFONTRATIO
End Get
End Property
'Funzione che restituisce il valore di bIsActive per usarlo all'esterno della libreria
Public Shared Function GetbIsActive() As Boolean
Return bIsActive
End Function
#Region "Constructors and support functions"
Sub New(Owner As Window, TextBoxBase As Primitives.TextBoxBase)
bIsActive = True
m_Owner = Owner
m_SourceTxBx = TextBoxBase
Me.Width = Owner.ActualWidth / 4
Me.Height = Me.Width * (1 / ASPECTRATIO)
InitializeComponent()
If InitializeEgtWPFLib.ResourceDictionary = 0 Then
Dim rd As ResourceDictionary = New ResourceDictionary()
rd.Source = New Uri("/EgtWPFLib;component/EgtWPFLibDictionary.xaml", UriKind.Relative)
Me.Resources.Clear()
Me.Resources = rd
Else
Dim rd As ResourceDictionary = New ResourceDictionary()
rd.Source = New Uri("/EgtWPFLib;component/EgtWPFLibDarkDictionary.xaml", UriKind.Relative)
Me.Resources.Clear()
Me.Resources = rd
End If
Dim FontSize As Double = Width * WIDTHFONTRATIO
Me.Resources("EgtKeyboard_FontSize") = New FontSizeConverter().ConvertFrom(CStr(FontSize))
Dim IconWidth As Double = Width / 5 * BUTTONIMAGERATIO
Me.Resources("EgtCalculator_IconWidth") = New FontSizeConverter().ConvertFrom(CStr(IconWidth))
'Me.Top = Owner.Top + (Owner.Height / 2 - Me.Height / 2)
'Me.Left = Owner.Left + (Owner.Width / 2 - Me.Width / 2)
End Sub
Sub New(Owner As Window, dStartValue As Double)
bIsActive = True
m_Owner = Owner
m_dStartValue = dStartValue
Me.Width = Owner.ActualWidth / 4
Me.Height = Me.Width * (1 / ASPECTRATIO)
InitializeComponent()
If InitializeEgtWPFLib.ResourceDictionary = 0 Then
Dim rd As ResourceDictionary = New ResourceDictionary()
rd.Source = New Uri("/EgtWPFLib;component/EgtWPFLibDictionary.xaml", UriKind.Relative)
Me.Resources.Clear()
Me.Resources = rd
Else
Dim rd As ResourceDictionary = New ResourceDictionary()
rd.Source = New Uri("/EgtWPFLib;component/EgtWPFLibDarkDictionary.xaml", UriKind.Relative)
Me.Resources.Clear()
Me.Resources = rd
End If
Dim FontSize As Double = Width * WIDTHFONTRATIO
Me.Resources("EgtKeyboard_FontSize") = New FontSizeConverter().ConvertFrom(CStr(FontSize))
Dim IconWidth As Double = Width / 5 * BUTTONIMAGERATIO
Me.Resources("EgtCalculator_IconWidth") = New FontSizeConverter().ConvertFrom(CStr(IconWidth))
'Me.Top = Owner.Top + (Owner.Height / 2 - Me.Height / 2)
'Me.Left = Owner.Left + (Owner.Width / 2 - Me.Width / 2)
End Sub
Sub New(Owner As Window, Width As Double, WidthType As WidthType, TextBoxBase As Primitives.TextBoxBase, Optional IsLength As Boolean = False)
bIsActive = True
m_Owner = Owner
m_SourceTxBx = TextBoxBase
m_Width = Width
m_WidthType = WidthType
m_IsLength = IsLength
InitializeComponent()
If InitializeEgtWPFLib.ResourceDictionary = 0 Then
Dim rd As ResourceDictionary = New ResourceDictionary()
rd.Source = New Uri("/EgtWPFLib;component/EgtWPFLibDictionary.xaml", UriKind.Relative)
Me.Resources.Clear()
Me.Resources = rd
Else
Dim rd As ResourceDictionary = New ResourceDictionary()
rd.Source = New Uri("/EgtWPFLib;component/EgtWPFLibDarkDictionary.xaml", UriKind.Relative)
Me.Resources.Clear()
Me.Resources = rd
End If
Dim FontSize As Double = Width * WIDTHFONTRATIO
Me.Resources("EgtCalculator_FontSize") = New FontSizeConverter().ConvertFrom(CStr(FontSize))
Dim IconWidth As Double = Width / 5 * BUTTONIMAGERATIO
Me.Resources("EgtCalculator_IconWidth") = New FontSizeConverter().ConvertFrom(CStr(IconWidth))
'Me.Top = Owner.Top + (Owner.Height / 2 - Height / 2)
'Me.Left = Owner.Left + (Owner.Width / 2 - Width / 2)
End Sub
Sub New(Owner As Window, dStartValue As Double, Width As Double, WidthType As WidthType)
bIsActive = True
m_Owner = Owner
m_dStartValue = dStartValue
m_Width = Width
m_WidthType = WidthType
InitializeComponent()
If InitializeEgtWPFLib.ResourceDictionary = 0 Then
Dim rd As ResourceDictionary = New ResourceDictionary()
rd.Source = New Uri("/EgtWPFLib;component/EgtWPFLibDictionary.xaml", UriKind.Relative)
Me.Resources.Clear()
Me.Resources = rd
Else
Dim rd As ResourceDictionary = New ResourceDictionary()
rd.Source = New Uri("/EgtWPFLib;component/EgtWPFLibDarkDictionary.xaml", UriKind.Relative)
Me.Resources.Clear()
Me.Resources = rd
End If
Dim FontSize As Double = Width * WIDTHFONTRATIO
Me.Resources("EgtCalculator_FontSize") = New FontSizeConverter().ConvertFrom(CStr(FontSize))
Dim IconWidth As Double = Width / 5 * BUTTONIMAGERATIO
Me.Resources("EgtCalculator_IconWidth") = New FontSizeConverter().ConvertFrom(CStr(IconWidth))
'Me.Top = Owner.Top + (Owner.Height / 2 - Height / 2)
'Me.Left = Owner.Left + (Owner.Width / 2 - Width / 2)
End Sub
#End Region
#Region "Control lifetime events"
' DependencyProperty aggiuntive per gestire la tastiera
Private m_IsLength As Boolean = True
Public Property IsLength As Boolean
Get
Return m_IsLength
End Get
Set(value As Boolean)
m_IsLength = value
End Set
End Property
Private Sub EgtCalculator_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
'Calcolo dimensioni e posizione della finestra
Select Case m_WidthType
Case WidthType.NULL
Me.Height = m_Owner.Height / InitializeEgtWPFLib.BaseGridHeight * 5
Me.Width = m_Owner.Width / InitializeEgtWPFLib.BaseGridWidth * 5
Case WidthType.GRID
Me.Height = m_Owner.Height / InitializeEgtWPFLib.BaseGridHeight * 5 * m_Width / 5
Me.Width = m_Owner.Width / InitializeEgtWPFLib.BaseGridWidth * m_Width
Case WidthType.PIXEL
' Fisso una dimensione minima affinchè non diventi inutilizzabile
If m_Width < MINIMUMDIMENSION Then
m_Width = MINIMUMDIMENSION
End If
Me.Height = m_Width / 5 * 5
Me.Width = m_Width
End Select
' La calcolatrice in pollici deve essere abilitata solo se programma NON in MM e IsLength = True
m_IsLength = m_IsLength And Not EgtUiUnitsAreMM()
If m_IsLength AndAlso nCurrFractionPattern <> FractionPattern.None Then
DotBtn.Visibility = Visibility.Collapsed
BackspaceBtn.Visibility = Visibility.Collapsed
FeetBtn.Visibility = Visibility.Visible
InchesBtn.Visibility = Visibility.Visible
BackspaceLenBtn.Visibility = Visibility.Visible
EvaluateBtn.SetValue(Grid.ColumnProperty, 3)
EvaluateBtn.SetValue(Grid.RowProperty, 0)
ValueTxBx.SetValue(Grid.ColumnSpanProperty, 1)
Else
DotBtn.Visibility = Visibility.Visible
BackspaceBtn.Visibility = Visibility.Visible
FeetBtn.Visibility = Visibility.Collapsed
InchesBtn.Visibility = Visibility.Collapsed
BackspaceLenBtn.Visibility = Visibility.Collapsed
EvaluateBtn.SetValue(Grid.ColumnProperty, 2)
EvaluateBtn.SetValue(Grid.RowProperty, 3)
ValueTxBx.SetValue(Grid.ColumnSpanProperty, 3)
End If
End Sub
Private Sub EgtCalculator_Loaded(sender As Object, e As EventArgs) Handles Me.Loaded
m_sErrorString = EgtMsg(MSG_EGTCALCULATOR + 1)
If Not IsNothing(m_SourceTxBx) Then
Dim SourceTxBx As TextBox = m_SourceTxBx
ValueTxBx.Text = SourceTxBx.Text
Else
ValueTxBx.Text = DoubleToString(m_dStartValue, 3)
End If
End Sub
#End Region
#Region "Button events and support functions"
Private Sub AddCharacter(KeyCharacter As String)
ValueTxBx.Focus()
Dim EventArgs = New TextCompositionEventArgs(Keyboard.PrimaryDevice, New TextComposition(InputManager.Current, Keyboard.FocusedElement, KeyCharacter)) With {
.RoutedEvent = TextInputEvent
}
InputManager.Current.ProcessInput(EventArgs)
End Sub
Private Sub NumberBtn_Click(sender As Object, e As RoutedEventArgs) Handles OneBtn.Click, TwoBtn.Click, ThreeBtn.Click, FourBtn.Click, FiveBtn.Click, SixBtn.Click, SevenBtn.Click, EightBtn.Click, NineBtn.Click, ZeroBtn.Click,
PlusBtn.Click, MinusBtn.Click, DotBtn.Click, InchesBtn.Click, FeetBtn.Click, TimesBtn.Click, DividedBtn.Click
If m_bErrorState Then
m_bErrorState = False
ValueTxBx.Foreground = Brushes.Black
ValueTxBx.TextAlignment = TextAlignment.Right
ValueTxBx.Text = m_sBeforeEvaluate
ValueTxBx.Focus()
ValueTxBx.SelectAll()
Else
Dim KeyButton As Button = e.Source
Dim Key As String = String.Empty
If ReturnKeyNumber(KeyButton, Key) Then
AddCharacter(Key)
End If
End If
End Sub
Private Function ReturnKeyNumber(KeyButton As Button, ByRef Key As String) As Boolean
Select Case KeyButton.Name
Case OneBtn.Name
Key = "1"
Return True
Case TwoBtn.Name
Key = "2"
Return True
Case ThreeBtn.Name
Key = "3"
Return True
Case FourBtn.Name
Key = "4"
Return True
Case FiveBtn.Name
Key = "5"
Return True
Case SixBtn.Name
Key = "6"
Return True
Case SevenBtn.Name
Key = "7"
Return True
Case EightBtn.Name
Key = "8"
Return True
Case NineBtn.Name
Key = "9"
Return True
Case ZeroBtn.Name
Key = "0"
Return True
Case PlusBtn.Name
Key = "+"
Return True
Case MinusBtn.Name
Key = "-"
Return True
Case DotBtn.Name
Key = "."
Return True
Case InchesBtn.Name
Key = """"
Return True
Case FeetBtn.Name
Key = "'"
Return True
Case TimesBtn.Name
Key = "*"
Return True
Case DividedBtn.Name
Key = "/"
Return True
End Select
Return False
End Function
Private Sub BackspaceBtn_Click(sender As Object, e As RoutedEventArgs) Handles BackspaceBtn.Click, BackspaceLenBtn.Click
ValueTxBx.Focus()
Dim eventArgs As New KeyEventArgs(Keyboard.PrimaryDevice, New HwndSource(0, 0, 0, 0, 0, "", IntPtr.Zero), 0, Key.Back) With {
.RoutedEvent = Keyboard.KeyDownEvent
}
InputManager.Current.ProcessInput(eventArgs)
End Sub
Private Sub EvaluateBtn_Click(sender As Object, e As RoutedEventArgs) Handles EvaluateBtn.Click
Evaluate()
End Sub
Private Sub Evaluate()
m_sBeforeEvaluate = ValueTxBx.Text
Dim bOk As Boolean = False
' se impostato per dati in frazioni
Dim sDecimal As String = String.Empty
If nCurrFractionPattern <> FractionPattern.None AndAlso StringFractionToStringDecimal(ValueTxBx.Text, sDecimal) Then
bOk = EgtUILib.EgtLuaEvalNumExpr(sDecimal, m_DoubleResult)
Else
bOk = EgtUILib.EgtLuaEvalNumExpr(ValueTxBx.Text, m_DoubleResult)
End If
If bOk Then
If nCurrFractionPattern <> FractionPattern.None And Not EgtUiUnitsAreMM() And m_IsLength Then
ValueTxBx.Text = DoubleToStringFraction(m_DoubleResult, dPrecision)
Else
ValueTxBx.Text = DoubleToString(m_DoubleResult, 6)
m_sBeforeEvaluate = String.Empty
End If
Else
m_bErrorState = True
ValueTxBx.Foreground = Brushes.Red
ValueTxBx.TextAlignment = TextAlignment.Left
ValueTxBx.Text = m_sErrorString
End If
'Dim bOk As Boolean = EgtUILib.EgtLuaEvalNumExpr(ValueTxBx.Text, m_DoubleResult)
'If bOk Then
' ValueTxBx.Text = DoubleToString(m_DoubleResult, 6)
' m_sBeforeEvaluate = String.Empty
'Else
' m_bErrorState = True
' ValueTxBx.Foreground = Brushes.Red
' ValueTxBx.TextAlignment = TextAlignment.Left
' ValueTxBx.Text = m_sErrorString
'End If
ValueTxBx.Focus()
ValueTxBx.CaretIndex = ValueTxBx.Text.Length
End Sub
Private Sub CloseBtn_Click(sender As Object, e As RoutedEventArgs) Handles VBtn.Click, XBtn.Click
Dim ClickedButton As Button = e.Source
If ClickedButton.Name = "VBtn" Then
Evaluate()
If Not m_bErrorState And Not IsNothing(ValueTxBx.Text) Then
If Not IsNothing(m_SourceTxBx) Then
Dim SourceTxBx As TextBox = m_SourceTxBx
SourceTxBx.Text = ValueTxBx.Text
SourceTxBx.Focus()
End If
m_bConfirmedResult = True
End If
Else
m_bConfirmedResult = False
End If
bIsActive = False
Keyboard.ClearFocus()
RaiseEvent EgtClosed(sender, e)
End Sub
#End Region
End Class