Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Drawing
Imports VBNET = Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Imports gmRTL.Core
Imports gmRTL.WPF

Namespace Calculator
   Public Partial Class frmCalcu : Inherits Window
      Public cmdCancel As System.Collections.Generic.SortedList(Of Integer, System.Windows.Controls.Button) = Nothing
      Public cmdFunction As System.Collections.Generic.SortedList(Of Integer, System.Windows.Controls.Button) = Nothing
      Public cmdMemory As System.Collections.Generic.SortedList(Of Integer, System.Windows.Controls.Button) = Nothing
      Public operator_migname As System.Collections.Generic.SortedList(Of Integer, System.Windows.Controls.Button) = Nothing
      Public numero As System.Collections.Generic.SortedList(Of Integer, System.Windows.Controls.Button) = Nothing
      <System.Diagnostics.DebuggerNonUserCode()>
      Public Sub New()
      MyBase.New()
         '
         ' Required for Windows Form Designer support
         '
         InitializeComponent()
         '
         ' TODO: Add any constructor code after InitializeComponent call
         '
         cmdCancel = New System.Collections.Generic.SortedList(Of Integer, System.Windows.Controls.Button)()
         cmdCancel.Add(2, _cmdCancel_2)
         cmdCancel.Add(1, _cmdCancel_1)
         cmdCancel.Add(0, _cmdCancel_0)
         cmdFunction = New System.Collections.Generic.SortedList(Of Integer, System.Windows.Controls.Button)()
         cmdFunction.Add(3, _cmdFunction_3)
         cmdFunction.Add(2, _cmdFunction_2)
         cmdFunction.Add(1, _cmdFunction_1)
         cmdFunction.Add(0, _cmdFunction_0)
         cmdMemory = New System.Collections.Generic.SortedList(Of Integer, System.Windows.Controls.Button)()
         cmdMemory.Add(3, _cmdMemory_3)
         cmdMemory.Add(2, _cmdMemory_2)
         cmdMemory.Add(1, _cmdMemory_1)
         cmdMemory.Add(0, _cmdMemory_0)
         operator_migname = New System.Collections.Generic.SortedList(Of Integer, System.Windows.Controls.Button)()
         operator_migname.Add(3, _operator_3)
         operator_migname.Add(2, _operator_2)
         operator_migname.Add(1, _operator_1)
         operator_migname.Add(0, _operator_0)
         numero = New System.Collections.Generic.SortedList(Of Integer, System.Windows.Controls.Button)()
         numero.Add(9, _numero_9)
         numero.Add(8, _numero_8)
         numero.Add(7, _numero_7)
         numero.Add(6, _numero_6)
         numero.Add(5, _numero_5)
         numero.Add(4, _numero_4)
         numero.Add(3, _numero_3)
         numero.Add(2, _numero_2)
         numero.Add(1, _numero_1)
         numero.Add(0, _numero_0)
      End Sub
      Dim tama As Boolean = False
      Dim firstnum As Double = 0.0
      Dim secondnum As Double = 0.0
      Dim sign As String = ""
      Dim memory As Double = 0.0
      Dim memrelease As Boolean = False
      Private Sub cmdCancel_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
         Dim cmdCancel As System.Windows.Controls.Button
         cmdCancel = CType(sender,System.Windows.Controls.Button)
         Dim Index As Integer = Me.cmdCancel.Keys(Me.cmdCancel.IndexOfValue(cmdCancel))
         Select Case Index
            Case 0
               'if any other button has been click
               'except for the numbers
               'dont do anything
               If (tama) Then
                  Exit Sub
               End If
               'make sure that the last number be equal to 0
               If (Strings.Len(txtDisplay.Text) = 1) Then
                  txtDisplay.Text = "0"
                  cmdEqual.Focus()
                  Exit Sub
               End If
               'erase the last inputed number
               gmRTL.Core.ErrorHandling.ResumeNext(Sub()
                  txtDisplay.Text = Mid(txtDisplay.Text,1,Strings.Len(txtDisplay.Text) - 1)
                  cmdEqual.Focus()
               End Sub)
            Case 1
               'make firstnum and secondnum equal to 0
               'so that every operation will be equal to 0
               firstnum = 0
               secondnum = 0
               txtDisplay.Text = "0"
               cmdEqual.Focus()
            Case 2
               'clear display and set text to 0
               txtDisplay.Text = "0"
               cmdEqual.Focus()
         End Select
         cmdEqual.Focus()


      End Sub
      Private Sub cmdDot_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)

         'determine if period is already present
         If (Not tama) Then
                                               'equal sign was not click
            'already present exit
            If (Strings.InStr(1,txtDisplay.Text,".",CompareMethod.Binary) > 0) Then
               cmdEqual.Focus()
               Exit Sub
            End If
            'not present append
            cmdEqual.Focus()
            txtDisplay.Text = txtDisplay.Text + cmdDot.Content
         ElseIf (tama) Then
                                               'equal sign was click
            txtDisplay.Text = "0."
            tama = False
            cmdEqual.Focus()
         End If
         cmdEqual.Focus()
      End Sub
      Private Sub cmdEqual_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
         Dim answer As Double = 0.0
         secondnum = Conversion.Val(txtDisplay.Text) 'store the second variable
         Try
            gmRTL.Core.GlobalException.ClearErrorObject()
            'perform operation
            If (sign = "+") Then
               answer = firstnum + secondnum
            End If
            If (sign = "-") Then
               answer = firstnum - secondnum
            End If
            If (sign = "*") Then
               answer = firstnum * secondnum
            End If
            If (sign = "/" And secondnum <> 0) Then
               answer = firstnum / secondnum
            End If
            txtDisplay.Text = Convert.ToString(answer)
            tama = True
            firstnum = answer
            Exit Sub
         Catch exc As System.Exception
            gmRTL.Core.GlobalException.SetErrorObject(exc)
            txtDisplay.Text = "Error"
            System.Windows.MessageBox.Show(Err.Description, "Operation Aborted", MessageBoxButton.OK,MessageBoxImage.Warning)
            tama = True
         End Try
      End Sub
      Private Sub cmdEqual_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs)
         Dim KeyAscii As Integer
         KeyAscii = KeyToAscii.GetAsciiFromKey(e.Key)
         Dim i As Integer = 0
         Dim x As Integer = 0
         'this is for the operator
         For x = 0 To 3 Step 1
            If (Strings.Chr(KeyAscii) = operator_migname(x).Content) Then
               operator_migname(x).Focus()
               operator_Click(operator_migname(x),New RoutedEventArgs())
            End If
         Next

         'this is for the period
         If (Strings.Chr(KeyAscii) = ".") Then
            cmdDot.Focus()
            cmdDot_Click(cmdDot,New RoutedEventArgs())
         End If

         'this is for the numbers
         If (Strings.Left(txtDisplay.Text,1) = "0") Then
            'if there is already a period
            If (Strings.InStr(1,txtDisplay.Text,".",CompareMethod.Binary) > 0) Then
               'append digits and exit so that
               'it wont append twice
               For i = 48 To 57 Step 1
                  'simulate a press of numbers
                  If (KeyAscii = i) Then
                                                        'ascii = 0 to 9
                     numero(CInt(Convert.ToString(Strings.Chr(KeyAscii)))).Focus()
                     numero_Click(numero(CInt(Convert.ToString(Strings.Chr(KeyAscii)))),New RoutedEventArgs())
                     Exit Sub
                  End If
               Next
            Else
               'else dont put anything
               txtDisplay.Text = ""
            End If
         End If
         'simulate a press of numbers
         For i = 48 To 57 Step 1
                                               'ascii = 0 to 9
            If (KeyAscii = i) Then
               numero(CInt(Convert.ToString(Strings.Chr(KeyAscii)))).Focus()
               numero_Click(numero(CInt(Convert.ToString(Strings.Chr(KeyAscii)))),New RoutedEventArgs())
               Exit Sub
            End If
         Next

         'this is for the cancel
         If (Conversion.Val(KeyAscii) = &H8) Then
            _cmdCancel_0.Focus()
            cmdCancel_Click(_cmdCancel_0,New RoutedEventArgs())
         End If


         If (Strings.Chr(KeyAscii) = "=") Then
            cmdEqual_Click(cmdEqual,New RoutedEventArgs())
         End If

      End Sub
      Private Sub cmdFunction_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
         Dim cmdFunction As System.Windows.Controls.Button
         cmdFunction = CType(sender,System.Windows.Controls.Button)
         Dim Index As Integer = Me.cmdFunction.Keys(Me.cmdFunction.IndexOfValue(cmdFunction))
         Select Case Index
            Case 0                             'case positve to negative
               txtDisplay.Text = Convert.ToString(-Conversion.Val(txtDisplay.Text))
            Case 1                             'case 1/x
               If (Conversion.Val(txtDisplay.Text) <> 0) Then
                  txtDisplay.Text = Convert.ToString(1 / Conversion.Val(txtDisplay.Text))
               End If
            Case 2                             'case percentage
               txtDisplay.Text = Convert.ToString(Conversion.Val(txtDisplay.Text) / 100)
            Case 3                             ' case squareroot
               If (Conversion.Val(txtDisplay.Text) = 0) Then
                  txtDisplay.Text = "Error"
                  Exit Sub
               Else
                  txtDisplay.Text = Convert.ToString(Math.Sqrt(Conversion.Val(txtDisplay.Text)))
               End If
         End Select
      End Sub
      Private Sub cmdMemory_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
         Dim cmdMemory As System.Windows.Controls.Button
         cmdMemory = CType(sender,System.Windows.Controls.Button)
         Dim Index As Integer = Me.cmdMemory.Keys(Me.cmdMemory.IndexOfValue(cmdMemory))
         Select Case Index
            Case 0                             ' clear memory (MC)
               memory = 0
               lblMem.Content = ""
            Case 1                             ' add number to memory (M+)
               If (txtDisplay.Text = "0") Then
                  Exit Sub
               End If
               memory = memory + Conversion.Val(txtDisplay.Text)
               lblMem.Content = "M"
            Case 2                             ' get value of memory (MC)
               If (txtDisplay.Text = "0") Then
                  Exit Sub
               End If
               'if operator are click release value of memory
               If (memrelease) Then
                  lblMem.Content = ""
                  memory = 0
                  'if numbers are click get value of memory
               ElseIf (Not memrelease) Then
                  lblMem.Content = "M"
                  memory = CDbl(txtDisplay.Text)
               End If
            Case 3                             'get value of memory (MR)
               txtDisplay.Text = Convert.ToString(memory)
         End Select
      End Sub
      Private Sub mnuCopy_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
         System.Windows.Forms.Clipboard.SetText(txtDisplay.Text)
      End Sub
      Private Sub mnuPaste_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
         Paste()
      End Sub
      Private Sub Paste()
         Dim str As String = ""
         str = System.Windows.Forms.Clipboard.GetText(System.Windows.Forms.TextDataFormat.Text)

         str = Strings.Replace(str," ","")

         Dim i As Integer = 0
         Dim s As String = ""

         For i = 1 To Strings.Len(str) Step 1
            s = Mid(str,i,1)
            cmdEqual_KeyPress(Nothing,new System.Windows.Input.KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(cmdEqual), 0, Strings.Asc(s)))
         Next
      End Sub
      Private Sub numero_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
         Dim numero As System.Windows.Controls.Button
         numero = CType(sender,System.Windows.Controls.Button)
         Dim Index As Integer = Me.numero.Keys(Me.numero.IndexOfValue(numero))
         memrelease = False                 'toggle the memory
         If (tama) Then
                                               'toggle the answer
            txtDisplay.Text = ""
            tama = False
         End If
         'determine if the first digit is zero
         If (Strings.Left(txtDisplay.Text,1) = "0") Then
            'if there is already a period
            If (Strings.InStr(1,txtDisplay.Text,".",CompareMethod.Binary) > 0) Then
               'append digits and exit so that
               'it wont append twice
               txtDisplay.Text = txtDisplay.Text + numero.Content
               cmdEqual.Focus()
               Exit Sub
            Else
               'else dont put anything
               txtDisplay.Text = ""
               cmdEqual.Focus()
            End If
         End If
         'if first digit is zero
         'erase 0 and append digits
         txtDisplay.Text = txtDisplay.Text + numero.Content
         cmdEqual.Focus()
      End Sub
      Private Sub operator_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
         Dim operator_migname As System.Windows.Controls.Button
         operator_migname = CType(sender,System.Windows.Controls.Button)
         Dim Index As Integer = Me.operator_migname.Keys(Me.operator_migname.IndexOfValue(operator_migname))
         memrelease = True                  'toggle memory
         firstnum = Conversion.Val(txtDisplay.Text) 'store value txtdisplay.text to variable
         'store operations to variable
         Select Case Index
            Case 0
               sign = "/"
            Case 1
               sign = "+"
            Case 2
               sign = "-"
            Case 3
               sign = "*"
         End Select
         tama = True                        'toggle answer
         cmdEqual.Focus()
      End Sub
      Private Sub txtDisplay_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs)
         Dim KeyCode As Integer
         KeyCode = KeyToAscii.GetAsciiFromKey(e.Key)
         'Override default paste so that we turn the string into button presses
         If (KeyCode = 86) Then
            Paste()
            'We handled paste so disable textbox paste
            KeyCode = 0
         End If
      End Sub
   End Class
End Namespace
