//wxBasic Calculator -David Cuny
//
//
// Modified by Ray pope and Mark Ulrich, 17.sept.04
// Modified by Enrique Marcet, 12.dec.2013
//
//
//The identity of the buttons
 Dim ID_ADD = wxID_HIGHEST + 1,
 ID_SUB = wxID_HIGHEST + 2,
 ID_MUL = wxID_HIGHEST + 3,
 ID_DIV = wxID_HIGHEST + 4,
 ID_EQ = wxID_HIGHEST + 5,
 ID_0 = wxID_HIGHEST + 6,
 ID_1 = wxID_HIGHEST + 7,
 ID_2 = wxID_HIGHEST + 8,
 ID_3 = wxID_HIGHEST + 9,
 ID_4 = wxID_HIGHEST + 10,
 ID_5 = wxID_HIGHEST + 11,
 ID_6 = wxID_HIGHEST + 12,
 ID_7 = wxID_HIGHEST + 13,
 ID_8 = wxID_HIGHEST + 14,
 ID_9 = wxID_HIGHEST + 15,
 ID_C = wxID_HIGHEST + 16,
 ID_CE = wxID_HIGHEST + 17,
 ID_DOT = wxID_HIGHEST + 18,
 ID_PERCENT= wxID_HIGHEST + 19,
 ID_SIGN = wxID_HIGHEST + 20,
 ID_STORE= wxID_HIGHEST + 21,
 ID_RESTORE = wxID_HIGHEST + 22

 //Globals
 dim readout, framecalc, panelcalc, mem, display, op
 //Default settings
 mem = 0
 display = 0
 op = ""
 dim show = ""


 dim mode
 dim thelast
 dim repeatmode
 dim store=0


//Handle all the events
 Function handleButton( event )

 Dim id = event.GetId()

 Select Case id
   Case ID_ADD

    if repeatmode=1 then
      calculateresult()
    end if
    repeatmode=1
    mem = display
    op = "+"
    mode=1

   Case ID_SUB

    if repeatmode=1 then
      calculateresult()
    end if
    repeatmode=1
    mem = display
    mode=1
    op = "-"

   Case ID_MUL

    if repeatmode=1 then
      calculateresult()
    end if
    repeatmode=1
    mem = display
    mode=1
    op = "*"

   Case ID_DIV

    if repeatmode=1 then
      calculateresult()
    end if
    repeatmode=1
    mem = display
    mode=1
    op = "/"

   Case ID_EQ

    if mode=2 then
     dim tmp=mem
    else
     tmp = display
     if mode=0 then
      thelast=display
     end if
    end if

    repeatmode=0
    calculateresult()
    mode=2


   mem = tmp
   Case ID_0 To ID_9
    if mode=1 then
     display=0
     mode=0
     ReadOut.SetValue(show)
    end if
    dim digit = ID - ID_0
    display = val("" & display & chr(asc("0")+digit))
   Case ID_C
    display = 0
    repeatmode=0
    mode=0
   Case ID_CE
    display = 0
    mem = 0
    repeatmode=0
    mode=0
   Case ID_DOT
    If Instr(display, ".") = 0 Then
     display &= "."
    End If
   Case ID_PERCENT
    display = display /100
   Case ID_SIGN
    display = display * -1

   Case ID_STORE
    store=display

   Case ID_RESTORE
    display=store

   End Select

 ' Add decimal?
 show = "" & display
 if Instr( show, ".") = 0 then
   show &= "."
 End If

 ' trim leading zeros
 While Mid( show, 1,1 ) = "0" And Mid( show, 2,1 ) <> "."
   show = Mid( show, 2 )
 End While

 ReadOut.SetValue(show)

 End Function

 ' The controls

 framecalc = new wxMDIChildFrame(myframe,-1,"Calculadora",wxPoint(0,0),wxSize(228,230), wxCAPTION | wxSYSTEM_MENU | wxMINIMIZE_BOX  )
 panelcalc = new wxPanel(framecalc,-1)
 ReadOut = new wxTextCtrl(panelcalc,-1,"0",wxPoint(2,4),wxSize(220,30), wxTE_RIGHT | wxTE_READONLY)
 framecalc.Center()
framecalc.show(FALSE)

 ' Create button and assign callbacks
 Sub makeButton( caption, id, x, y )

 ' a button is 40x30, with 4 points of leading between the buttons and window edge
   Dim x1 = (x * 44) + 4
   Dim y1 = (y * 34) + 4

   Dim button = new wxButton (panelcalc, id, caption,wxPoint(x1, y1),wxSize(40,30))
   Connect( button, wxEVT_COMMAND_BUTTON_CLICKED, "handleButton" )
 End Sub

 Sub makeBigButton( caption, id, x, y )

 ' a button is 40x30, with 4 points of leading between the buttons and window edge
   Dim x1 = (x * 110) + 4
   Dim y1 = (y * 34) + 4

   Dim button = new wxButton (panelcalc, id, caption,wxPoint(x1, y1),wxSize(106,30))
   Connect( button, wxEVT_COMMAND_BUTTON_CLICKED, "handleButton" )
 End Sub


 'Row 1
 makeButton("7", ID_7, 0, 1 )
 makeButton("8", ID_8, 1, 1 )
 makeButton("9", ID_9, 2, 1 )
 makeButton("C", ID_C, 3, 1 )
 makeButton("CE", ID_CE, 4, 1 )
 'Row2
 makeButton("4", ID_4, 0, 2 )
 makeButton("5", ID_5, 1, 2 )
 makeButton("6", ID_6, 2, 2 )
 makeButton("+", ID_ADD, 3, 2 )
 makeButton("-", ID_SUB, 4, 2 )
 'Row3
 makeButton("1", ID_1, 0, 3 )
 makeButton("2", ID_2, 1,  3 )
 makeButton("3", ID_3, 2, 3 )
 makeButton("*", ID_MUL, 3, 3 )
 makeButton("/", ID_DIV , 4, 3 )
 'Row4
 makeButton("0", ID_0, 0, 4 )
 makeButton("+/-", ID_SIGN, 1, 4 )
 makeButton(".", ID_DOT, 2, 4 )
 makeButton("=", ID_EQ, 3, 4 )
 makeButton("%", ID_PERCENT, 4, 4 )
 'Row5
 makeBigButton("store", ID_STORE, 0, 5 )
 makeBigButton("restore", ID_RESTORE, 1, 5 )
 'Go!
 
 sub calculateresult()


 Select Case op
   Case "+"
    display = mem + display
   Case "-"
     if mode=2 then
      'wxmessagebox(thelast)
      display = display -thelast
     else
      if mode=0 then
       display = mem -display
      end if
    end if
   Case "*"
    display = mem * display
   Case "/"
    If display = 0 then
     readout.SetValue( "Error: Positive Inifinity" )
     return
 '    Exit Function
    Else
     if mode=2 then
      'wxmessagebox(thelast)
      display = display/thelast
     else
      if mode=0 then

       display = mem /display
      end if
     end if
    End If
 End Select

 End sub
 
 Sub calcclose (event)
 framecalc.show(FALSE)
 End Sub
 
Connect( framecalc, wxEVT_CLOSE_WINDOW, "calcclose" )