'===========================================================================
' Subject: THE CALCULATIONS STATION           Date: 08-28-00 (19:19)       
'  Author: Robert J. Tiess                    Code: RAPIDQ                 
'  Origin: rjtmusic@hotmail.com             Packet: RAPIDQ.ABC
'===========================================================================
' The Calculations Station
' (beta version .01)
' by Robert J. Tiess
' Copyright © 2000
'
' This program is FREEWARE!
'
' Author e-mail: rjtmusic@hotmail.com
' Author website: www.mp3.com/rjt

$typecheck ON
$include "RAPIDQ.INC"

'Include icon at compile time as a resource file
'to be restributed in the same .exe file as this program.
'$resource calculationstationicon AS "calculationstation-icon.ico"

'Declare all the Subroutines and Button Click Event Handlers
DECLARE SUB PerformOperations
DECLARE SUB KeyEntry (Key AS STRING)
DECLARE SUB Button1Click (Sender AS QBUTTON)
DECLARE SUB Button2Click (Sender AS QBUTTON)
DECLARE SUB Button3Click (Sender AS QBUTTON)
DECLARE SUB Button4Click (Sender AS QBUTTON)
DECLARE SUB Button5Click (Sender AS QBUTTON)
DECLARE SUB Button6Click (Sender AS QBUTTON)
DECLARE SUB Button7Click (Sender AS QBUTTON)
DECLARE SUB Button8Click (Sender AS QBUTTON)
DECLARE SUB Button9Click (Sender AS QBUTTON)
DECLARE SUB Button10Click (Sender AS QBUTTON)
DECLARE SUB Button11Click (Sender AS QBUTTON)
DECLARE SUB Button12Click (Sender AS QBUTTON)
DECLARE SUB Button13Click (Sender AS QBUTTON)
DECLARE SUB Button14Click (Sender AS QBUTTON)
DECLARE SUB Button15Click (Sender AS QBUTTON)
DECLARE SUB Button16Click (Sender AS QBUTTON)
DECLARE SUB Button17Click (Sender AS QBUTTON)
DECLARE SUB Button18Click (Sender AS QBUTTON)
DECLARE SUB Button19Click (Sender AS QBUTTON)
DECLARE SUB Button20Click (Sender AS QBUTTON)
DECLARE SUB Button21Click (Sender AS QBUTTON)
DECLARE SUB Button22Click (Sender AS QBUTTON)
DECLARE SUB Button23Click (Sender AS QBUTTON)
DECLARE SUB Button24Click (Sender AS QBUTTON)
DECLARE SUB Button25Click (Sender AS QBUTTON)
DECLARE SUB Button26Click (Sender AS QBUTTON)
DECLARE SUB Button27Click (Sender AS QBUTTON)
DECLARE SUB Button28Click (Sender AS QBUTTON)
DECLARE SUB Button29Click (Sender AS QBUTTON)
DECLARE SUB Button30Click (Sender AS QBUTTON)
DECLARE SUB Button31Click (Sender AS QBUTTON)
DECLARE SUB Button32Click (Sender AS QBUTTON)
DECLARE SUB Button33Click (Sender AS QBUTTON)
DECLARE SUB Button34Click (Sender AS QBUTTON)
DECLARE SUB Button35Click (Sender AS QBUTTON)
DECLARE SUB Button36Click (Sender AS QBUTTON)
DECLARE SUB Button37Click (Sender AS QBUTTON)
DECLARE SUB Button38Click (Sender AS QBUTTON)
DECLARE SUB Button39Click (Sender AS QBUTTON)
DECLARE SUB Button40Click (Sender AS QBUTTON)
DECLARE SUB Button41Click (Sender AS QBUTTON)
DECLARE SUB Button42Click (Sender AS QBUTTON)
DECLARE SUB Button43Click (Sender AS QBUTTON)
DECLARE SUB EqualButtonClick (Sender AS QBUTTON)
DECLARE SUB SetFont (Sender AS QEDIT)
DECLARE SUB ScrollToBottom
DECLARE SUB RadiansButtonClick (Sender AS QRADIOBUTTON)
DECLARE SUB DegreesButtonClick (Sender AS QRADIOBUTTON)
DECLARE SUB AboutClick (Sender AS QIMAGE)

'Create the Calculations Station interface
CREATE Form AS QFORM
    Caption = "The Calculations Station"
    Width = 610
    Height = 435
    Center
    Color = &hffe080  ' Customize the form's color
    Cursor = crHandPoint  ' Set form cursor to a hand
    BorderStyle = bsToolWindow
    'IcoHandle = calculationstationicon  ' Set icon
    CREATE CalcStatIcon AS QImage
        Left = 16
        Top = 20
        Autosize = True
        ICOHandle = calculationstationicon ' Show icon
        onClick = AboutClick
    END CREATE
    CREATE Label2 AS QLABEL
        Caption = "The Calculations Station"
        Left = 120
        Top = 6
        AutoSize = True
        Transparent = 1
    END CREATE
    CREATE PoweredByRapidQ AS QLABEL
        Caption = "FREEWARE * POWERED BY RAPID-Q"
        Left = 410
        Top = 384
        AutoSize = True
        Transparent = 1
    END CREATE
    CREATE edit1 AS QEDIT ' main number display
        Text = "0"
        Left = 120
        Top = 24
        Width = 200
        Color = &hffffc0
        Height = 29
        MaxLength = 20
        AutoSize = True
        ReadOnly = True ' user must click on # buttons
        OnChange = SetFont ' on change reset font to Arial
    END CREATE
    CREATE Button1 AS QBUTTON
        Caption = "CLEAR ALL"
        Left = 16
        Top = 104
        OnClick = Button1Click
        taborder = 1
        Cursor = crHandPoint
    END CREATE
    CREATE Button2 AS QBUTTON
        Caption = "&1"
        Left = 120
        Top = 160
        Width = 51
        Height = 41
        OnClick = Button2Click
    END CREATE
    CREATE Button3 AS QBUTTON
        Caption = "&2"
        Left = 176
        Top = 160
        Width = 51
        Height = 41
        OnClick = Button3Click
    END CREATE
    CREATE Button4 AS QBUTTON
        Caption = "&3"
        Left = 232
        Top = 160
        Width = 51
        Height = 41
        OnClick = Button4Click
    END CREATE
    CREATE Button5 AS QBUTTON
        Caption = "&4"
        Left = 120
        Top = 112
        Width = 51
        Height = 41
        OnClick = Button5Click
    END CREATE
    CREATE Button6 AS QBUTTON
        Caption = "&5"
        Left = 176
        Top = 112
        Width = 51
        Height = 41
        OnClick = Button6Click
    END CREATE
    CREATE Button7 AS QBUTTON
        Caption = "&6"
        Left = 232
        Top = 112
        Width = 51
        Height = 41
        OnClick = Button7Click
    END CREATE
    CREATE Button8 AS QBUTTON
        Caption = "&7"
        Left = 120
        Top = 64
        Width = 51
        Height = 41
        OnClick = Button8Click
    END CREATE
    CREATE Button9 AS QBUTTON
        Caption = "&8"
        Left = 176
        Top = 64
        Width = 51
        Height = 41
        OnClick = Button9Click
    END CREATE
    CREATE Button10 AS QBUTTON
        Caption = "&9"
        Left = 232
        Top = 64
        Width = 51
        Height = 41
        OnClick = Button10Click
    END CREATE
    CREATE Button11 AS QBUTTON
        Caption = "&0"
        Left = 120
        Top = 208
        Width = 51
        Height = 41
        OnClick = Button11Click
    END CREATE
    CREATE Button12 AS QBUTTON
        Caption = "+ ADD"
        Left = 312
        Top = 64
        Height = 41
        OnClick = Button12Click
    END CREATE
    CREATE Button13 AS QBUTTON
        Caption = "- SUB"
        Left = 312
        Top = 112
        Height = 41
        OnClick = Button13Click
    END CREATE
    CREATE Button14 AS QBUTTON
        Caption = "X MUL"
        Left = 312
        Top = 160
        Height = 41
        OnClick = Button14Click
    END CREATE
    CREATE Button15 AS QBUTTON
        Caption = "/ DIV"
        Left = 312
        Top = 208
        Height = 41
        OnClick = Button15Click
    END CREATE
    CREATE Button16 AS QBUTTON
        Caption = "M-STORE"
        Left = 16
        Top = 144
        OnClick = Button16Click
    END CREATE
    CREATE Button17 AS QBUTTON
        Caption = "M-READ"
        Left = 16
        Top = 184
        OnClick = Button17Click
    END CREATE
    CREATE Button18 AS QBUTTON
        Caption = "M-CLEAR"
        Left = 16
        Top = 224
        OnClick = Button18Click
    END CREATE
    CREATE Button19 AS QBUTTON
        Caption = "."
        Left = 176
        Top = 208
        Width = 30
        Height = 41
        OnClick = Button19Click
    END CREATE
    CREATE Button20 AS QBUTTON
        Caption = "+/-"
        Left = 212
        Top = 208
        Width = 36
        Height = 41
        OnClick = Button20Click
    END CREATE
    CREATE EqualButton AS QBUTTON
        Caption = "="
        Left = 253
        Top = 208
        Width = 30
        Height = 41
        OnClick = EqualButtonClick
    END CREATE
    CREATE Button21 AS QBUTTON
        Caption = "SIN"
        Left = 80
        Top = 264
        Width = 51
        Height = 33
        OnClick = Button21Click
    END CREATE
    CREATE Button22 AS QBUTTON
        Caption = "COS"
        Left = 144
        Top = 264
        Width = 51
        Height = 33
        OnClick = Button22Click
    END CREATE
    CREATE Button23 AS QBUTTON
        Caption = "ASIN"
        Left = 80
        Top = 304
        Width = 51
        Height = 33
        OnClick = Button23Click
    END CREATE
    CREATE Button24 AS QBUTTON
        Caption = "ACOS"
        Left = 144
        Top = 304
        Width = 51
        Height = 33
        OnClick = Button24Click
    END CREATE
    CREATE Button25 AS QBUTTON
        Caption = "TAN"
        Left = 208
        Top = 264
        Width = 51
        Height = 33
        OnClick = Button25Click
    END CREATE
    CREATE Button26 AS QBUTTON
        Caption = "ATAN"
        Left = 208
        Top = 304
        Width = 51
        Height = 33
        OnClick = Button26Click
    END CREATE
    CREATE Button27 AS QBUTTON
        Caption = "BINARY"
        Left = 272
        Top = 344
        Width = 51
        Height = 33
        OnClick = Button27Click
    END CREATE
    CREATE Button28 AS QBUTTON
        Caption = "EXP"
        Left = 16
        Top = 264
        Width = 51
        Height = 33
        OnClick = Button28Click
    END CREATE
    CREATE Button29 AS QBUTTON
        Caption = "FIX"
        Left = 272
        Top = 264
        Width = 51
        Height = 33
        OnClick = Button29Click
    END CREATE
    CREATE Button30 AS QBUTTON
        Caption = "MOD"
        Left = 272
        Top = 304
        Width = 51
        Height = 33
        OnClick = Button30Click
    END CREATE
    CREATE Button31 AS QBUTTON
        Caption = "LOG"
        Left = 16
        Top = 344
        Width = 51
        Height = 33
        OnClick = Button31Click
    END CREATE
    CREATE Button32 AS QBUTTON
        Caption = "RND#"
        Left = 208
        Top = 344
        Width = 51
        Height = 33
        OnClick = Button32Click
    END CREATE
    CREATE Button33 AS QBUTTON
        Caption = "HEX"
        Left = 80
        Top = 344
        Width = 51
        Height = 33
        OnClick = Button33Click
    END CREATE
    CREATE Button34 AS QBUTTON
        Caption = "ROUND"
        Left = 144
        Top = 344
        Width = 51
        Height = 33
        OnClick = Button34Click
    END CREATE
    CREATE Button35 AS QBUTTON
        Caption = "SQRT"
        Left = 16
        Top = 304
        Width = 51
        Height = 33
        OnClick = Button35Click
    END CREATE
    CREATE Button36 AS QBUTTON
        Caption = "1/X"
        Left = 336
        Top = 264
        Width = 51
        Height = 33
        OnClick = Button36Click
    END CREATE
    CREATE Button41 AS QBUTTON
        Caption = "X ^ Y"
        Left = 336
        Top = 304
        Width = 51
        Height = 33
        OnClick = Button41Click
    END CREATE
    CREATE Button42 AS QBUTTON
        Caption = "CLEAR"
        Left = 16
        Top = 64
        OnClick = Button42Click
    END CREATE
    CREATE Button43 AS QBUTTON
        Caption = "PI"
        Left = 336
        Top = 344
        Width = 51
        Height = 33
        OnClick = Button43Click
    END CREATE
    CREATE ModeLabel AS QLABEL
        Caption = "MODE:"
        Left = 18
        Top = 384
        Width = 100
        Transparent = 1
    END CREATE
    CREATE RadiansButton AS QRADIOBUTTON
        Caption = "Radians"
        Checked = True
        Left = 70
        Top = 384
        Width = 73
        ShowHint = 1
        Hint = "Results in Radians"
        OnClick = RadiansButtonClick
    END CREATE
    CREATE DegreesButton AS QRADIOBUTTON
        Caption = "Degrees"
        Left = 140
        Top = 384
        Width = 73
        ShowHint = 1
        Hint = "Results in Degrees"
        OnClick = DegreesButtonClick
    END CREATE
    CREATE Label1 AS QLABEL
        Caption = "Calculations History Window"
        Left = 410
        Top = 8
        Width = 136
        Transparent = 1
    END CREATE
    CREATE RichEdit1 AS QRICHEDIT
        Left = 410
        Top = 24
        Width = 180
        Height = 310
        Color = &hffffc0
        ScrollBars = ssVertical
        HideScrollBars = False ' Show the scroll bar
        Wordwrap = True
        ReadOnly = True
        PlainText = True 
    END CREATE
    CREATE Button37 AS QBUTTON
        Caption = "Copy"
        Left = 410
        Top = 344
        Width = 40
        Height = 33
        OnClick = Button37Click
    END CREATE
    CREATE Button38 AS QBUTTON
        Caption = "Save"
        Left = 455
        Top = 344
        Width = 40
        Height = 33
        OnClick = Button38Click
    END CREATE
    CREATE Button39 AS QBUTTON
        Caption = "Print"
        Left = 500
        Top = 344
        Width = 40
        Height = 33
        OnClick = Button39Click
    END CREATE
    CREATE Button40 AS QBUTTON
        Caption = "Clear"
        Left = 545
        Top = 344
        Width = 40
        Height = 33
        OnClick = Button40Click
    END CREATE
END CREATE

' Import the SetFocus function from USER32.DLL
' - used to set remote active focus on the
'   Calculations History window (a RichEdit area)
'   so contents may be scrolled to the end
'   via RichEdit1.SelStart (set to end of contents)
DECLARE FUNCTION SetFocus LIB "USER32.DLL" ALIAS "SetFocus" _
                (hwnd AS LONG) AS LONG

' Declare all variables to be used.  VARIANTS are
' used here to allow for greatest flexibility
' in floating point/long/integer operations
DIM value as VARIANT
    value = 0
DIM total AS VARIANT
    total = 0
DIM entry AS VARIANT
    entry = 0
DIM entry2 AS VARIANT
    entry2 = 0
DIM memory AS VARIANT
    memory = 0
DIM totaltext AS STRING
    totaltext = ""
DIM entrytext AS VARIANT
    entrytext = ""
DIM lastop as INTEGER
    lastop = 0
DIM negative as INTEGER
    negative = 0
DIM newentry as INTEGER
    newentry = 1
DIM radiansordegrees AS VARIANT
    radiansordegrees = 1 'default is radians

' Set main Form and History Window fonts to Arial
DIM TextFont AS QFONT
    TextFont.Name = "Arial"
    TextFont.AddStyles(fsBold)
    TextFont.Size = 15
DIM TextFont2 AS QFONT
    TextFont2.Name = "Arial"
    TextFont2.AddStyles(fsBold)
    TextFont2.Size = 8
DIM ImageList AS QImageList

' Set the Font where the numbers are displayed
SUB SetFont (Sender AS QEDIT)
    edit1.Font = TextFont
    form.repaint
END SUB

SUB Button1Click (Sender AS QBUTTON)
'clear all operations
    lastop = 0
    total = 0
    entry = 0
    entry2 = 0
    edit1.text = "0"
    RichEdit1.AddString ("")
    RichEdit1.AddString ("** CLEAR ALL **")
    RichEdit1.AddString ("SUBTOTAL RESET TO: 0")
    RichEdit1.AddString ("")
    newentry = 1
END SUB

' Handle virtual keypad operations
SUB KeyEntry (Key AS STRING)
    SetFocus(edit1.Handle)
    if newentry = 1 then edit1.text = ""
    if edit1.text = "0" or edit1.text = "-0" then edit1.text = ""
    if len(edit1.text) < 9 then
        entrytext = edit1.text
        entrytext = entrytext + Key
        edit1.text = entrytext
    end if
    newentry = 0
END SUB

' Scroll to end of History Window
SUB ScrollToBottom (SENDER AS QRICHEDIT)
    SetFocus(RichEdit1.Handle)
    RichEdit1.SelStart = LEN(RichEdit1.text)
END SUB

' Handle number entry
SUB Button2Click (Sender AS QBUTTON)
'1
    KeyEntry("1")
END SUB

SUB Button3Click (Sender AS QBUTTON)
'2
    KeyEntry("2")
END SUB

SUB Button4Click (Sender AS QBUTTON)
'3
    KeyEntry("3")
END SUB

SUB Button5Click (Sender AS QBUTTON)
'4
    KeyEntry("4")
END SUB

SUB Button6Click (Sender AS QBUTTON)
'5
    KeyEntry("5")
END SUB

SUB Button7Click (Sender AS QBUTTON)
'6
    KeyEntry("6")
END SUB

SUB Button8Click (Sender AS QBUTTON)
'7
    KeyEntry("7")
END SUB

SUB Button9Click (Sender AS QBUTTON)
'8
    KeyEntry("8")
END SUB

SUB Button10Click (Sender AS QBUTTON)
'9
    KeyEntry("9")
END SUB

SUB Button11Click (Sender AS QBUTTON)
'0
    KeyEntry("0")
END SUB

' Perform all mathematical operations
SUB PerformOperations
    entrytext = edit1.text
    ' Zero is used here and below to scope VARIANT
    ' to numerical rather than string operations:
    entry2 = 0 + entrytext
    select case lastop
        case 1
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("ADDING " + str$(entry2))
            RichEdit1.AddString ("TO " + str$(entry))
            total = 0 + entry + entry2
            entry = total
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(total)
        case 2
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("SUBTRACTING " + str$(entry2))
            RichEdit1.AddString ("FROM " + str$(entry))
            total = 0 + entry - entry2
            entry = total
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(total)
        case 3
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("MULTIPLYING " + str$(entry))
            RichEdit1.AddString ("WITH " + str$(entry2))
            total = 0 + (entry * entry2)
            entry = total
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(total)
        case 4
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("DIVIDING " + str$(entry))
            RichEdit1.AddString ("BY " + str$(entry2))
            total = 0 + (entry / entry2)
            entry = total
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(total)
        case 5
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("SIN " + str$(entry))
            entry = SIN(entry * radiansordegrees) 
            total = entry
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 6
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("COS " + str$(entry))
            entry = COS(entry * radiansordegrees)
            total = entry
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 7
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("ASIN " + str$(entry))
            entry = ASIN(entry * radiansordegrees)
            total = entry
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 8
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("ACOS " + str$(entry))
            entry = ACOS(entry) * radiansordegrees
            total = entry
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 9
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("TAN " + str$(entry))
            entry = TAN(entry * radiansordegrees)
            total = entry
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 10
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("ATAN " + str$(entry))
            entry = ATAN(entry * radiansordegrees)
            total = entry
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 11
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("BIN " + str$(entry))
            entry = BIN$(ABS(entry)) ' string
            RichEdit1.AddString ("RESULT:" + entry)
            totaltext = entry
        case 12
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("EXP " + str$(entry))
            entry = EXP(entry)
            total = entry
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 13
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("FIX " + str$(entry))
            entry = FIX(entry)
            total = entry
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 14
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("MOD " + str$(entry) + "/" + str$(entry2))
            total = 0 + (entry MOD entry2)
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 15
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("LOG " + str$(entry))
            entry = LOG(entry)
            total = entry
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 16
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("RND " + str$(entry))
            entry = RND
            total = entry
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 17
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("ROUND " + str$(entry))
            entry = ROUND(entry)
            total = entry
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 18
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("SQR " + str$(entry))
            entry = SQR(entry)
            total = entry
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 19
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("1/x " + str$(entry))
            entry = 0 + (1/entry)
            total = entry
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(entry)
        case 20
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("RAISING " + str$(entry))
            RichEdit1.AddString ("TO THE POWER OF " + str$(entry2))
            total = 0 + (entry ^ entry2)
            entry = total
            RichEdit1.AddString ("NEW SUBTOTAL:" + str$(total))
            totaltext = str$(total)
        case 21
            RichEdit1.AddString ("")
            RichEdit1.AddString ("SUBTOTAL: " + str$(total))
            RichEdit1.AddString ("HEX " + str$(entry))
            entry = HEX$(entry) ' string
            RichEdit1.AddString ("RESULT:" + entry)
            totaltext = entry
    end select
    edit1.text = totaltext
    lastop = 0
    newentry = 1
    ScrollToBottom
END SUB

' Functions to follow handle all mathematical
' operations as user clicks buttons.
' Sometimes a user will perform several calculations
' in a row before clicking EQUALS, so lastop is used
' here so PerformOperations knows what operation was
' last operation (e.g. addition, multiplication) was
' being performed, if any.

SUB Button12Click (Sender AS QBUTTON)
'+
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 1
END SUB

SUB Button13Click (Sender AS QBUTTON)
'-
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 2
END SUB

SUB Button14Click (Sender AS QBUTTON)
'X
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 3
END SUB

SUB Button15Click (Sender AS QBUTTON)
' /
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 4
END SUB

SUB EqualButtonClick (Sender AS QBUTTON)
'=
    PerformOperations
END SUB

' Store a number in memory
SUB Button16Click (Sender AS QBUTTON)
'm-store
    entrytext = edit1.text
    entry = val(entrytext)
    memory = entry
END SUB

' Read number stored in memory; zero if none stored
SUB Button17Click (Sender AS QBUTTON)
'm-read
    edit1.text = str$(memory)
    entry = memory
END SUB

' Clear memory (memory = zero)
SUB Button18Click (Sender AS QBUTTON)
'm-clear
    memory = 0
END SUB

' Handle the decimal button
SUB Button19Click (Sender AS QBUTTON)
' . dec
    if newentry = 1 then edit1.text = ""
    entrytext = edit1.text
    entrytext = entrytext - "."
    if len(entrytext) > 0 then
        entrytext = entrytext + "."
        else
        entrytext = "."
    end if
    edit1.text = entrytext
    newentry = 0
END SUB

'Handle the positive/negative toggle button
SUB Button20Click (Sender AS QBUTTON)
' +/-
    entrytext = edit1.text
    entrytext = entrytext - "-"
    if negative = 0 then
        entrytext = "-" + entrytext
        negative = 1
        else
        negative = 0
        edit1.text = entrytext
        entry = val(entrytext)
    end if
    edit1.text = entrytext
END SUB

' More native Rapid-Q mathematical functions
SUB Button21Click (Sender AS QBUTTON)
'sin
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 5
    PerformOperations
END SUB

SUB Button22Click (Sender AS QBUTTON)
'cos
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 6
    PerformOperations
END SUB

SUB Button23Click (Sender AS QBUTTON)
'asin
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 7
    PerformOperations
END SUB

SUB Button24Click (Sender AS QBUTTON)
'acos
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 8
    PerformOperations
END SUB

SUB Button25Click (Sender AS QBUTTON)
'tan
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 9
    PerformOperations
END SUB

SUB Button26Click (Sender AS QBUTTON)
'atan
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 10
    PerformOperations
END SUB

SUB Button27Click (Sender AS QBUTTON)
'convert to binary
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 11
    PerformOperations
END SUB

SUB Button28Click (Sender AS QBUTTON)
'exp/e
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 12
    PerformOperations
END SUB

SUB Button29Click (Sender AS QBUTTON)
'fix
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 13
    PerformOperations
END SUB

SUB Button30Click (Sender AS QBUTTON)
'mod
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 14
END SUB

SUB Button31Click (Sender AS QBUTTON)
'log
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 15
    PerformOperations
END SUB

SUB Button32Click (Sender AS QBUTTON)
'Produce a random number - actually a decimal
'between 0 and 1 as no Upper Bound has been set
    randomize
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 16
    PerformOperations
END SUB

SUB Button34Click (Sender AS QBUTTON)
'round
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 17
    PerformOperations
END SUB

SUB Button35Click (Sender AS QBUTTON)
'sqrt
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 18
    PerformOperations
END SUB

SUB Button36Click (Sender AS QBUTTON)
'1/x
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 19
    PerformOperations
END SUB

SUB Button43Click (Sender AS QBUTTON)
'Produce the value of pi (using 3.1415927)
    entry = 3.1415927
    edit1.text = "3.1415927"
    lastop = 0
END SUB

SUB Button41Click (Sender AS QBUTTON)
'X ^ Y  (raise X to the power of Y)
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 20
END SUB

SUB Button33Click (Sender AS QBUTTON)
'convert to hexadecimal
    if lastop > 0 then PerformOperations
    entrytext = edit1.text
    entry = val(entrytext)
    edit1.text = ""
    lastop = 21
    PerformOperations
END SUB

'Two radio buttons toggle the degrees/radians
'that will affect functions like Sin and Cos
SUB RadiansButtonClick (Sender AS QRADIOBUTTON)
'results in radians
    DegreesButton.Checked = False
    RadiansButton.Checked = True
    radiansordegrees = 1
END SUB

SUB DegreesButtonClick (Sender AS QRADIOBUTTON)
'results in degrees
    DegreesButton.Checked = True
    RadiansButton.Checked = False
    radiansordegrees = (3.1415927 / 180)
END SUB

'HISTORY WINDOW FUNCTIONS
SUB Button37Click (Sender AS QBUTTON)
'copy history window text to clipboard
    RichEdit1.SelStart = 0
    RichEdit1.SelLength = LEN(RichEdit1.text)
    RichEdit1.CopyToClipboard
    RichEdit1.SelStart = 0
    RichEdit1.SelLength = 0
END SUB    

SUB Button38Click (Sender AS QBUTTON)
'save history window text to a text (.txt) file 
      dim SaveDialog AS QSaveDialog
      SaveDialog.Filter = "Text Files|*.txt"
      SaveDialog.FilterIndex = 0
      if SaveDialog.Execute THEN
        if FileExists(SaveDialog.FileName) THEN
            if MessageBox("FILE ALREADY EXISTS!  Are you sure you want to overwrite it?", "Close", 1) = 1 THEN
                RichEdit1.SaveToFile(SaveDialog.FileName)
            end if
        else
            RichEdit1.SaveToFile(SaveDialog.FileName)
        end if
      end if
END SUB

SUB Button39Click (Sender AS QBUTTON)
'print history window text
    if MessageBox("Print contents of Calculation History window.  Turn printer on and click OK to proceed or CANCEL.", "CANCEL", 1) = 1 THEN
        LPRINT RichEdit1.text
        LFLUSH
    end if
END SUB

SUB Button40Click (Sender AS QBUTTON)
'clear history text 
    RichEdit1.text = ""
END SUB

SUB Button42Click (Sender AS QBUTTON)
'clear the current numbered entered.
'does not clear total.
    entry = 0
    edit1.text = ""
END SUB

SUB AboutClick (Sender AS QIMAGE)
'show an About This Program message
'if user clicks the icon
    showmessage "The Calculations Station (beta version .01) was created by Robert J. Tiess © 2000." + chr$(13) + chr$(13) + "THIS PROGRAM IS FREEWARE AND IS POWERED BY RAPID-Q."
END SUB

'Set the fonts and start the program!
SetFont(edit1)
Form.Font = TextFont2
Form.ShowModal
