stack "calculator" id=9147 title="Calculator" rect="144,183,599,712" sharedvars="true" backgroundcolor="255,255,255"
    card "card" id=9148 backgroundcolor="54,54,54" _ideval="54,54,54" wtid="card"
-- Calculator Card Script
-- This handles all calculator operations

on calculate buttonValue
   set the disableScriptErrors to true
   if last char of buttonValue is " " then delete last char of buttonValue
   clear message box
   
   -- Handle number buttons (0-9)
   if buttonValue is in "0123456789" then
      put buttonValue after field "display"
   end if
   
   -- Handle open bracket
   if buttonValue is "(" then
      put buttonValue after field "display"
   end if
   
   -- Handle closed bracket
   if buttonValue is "]" then
      put ")" after field "display"
   end if
   
   -- Handle decimal point
   if buttonValue is "." then
      put buttonValue after field "display"
   end if
   
   -- Handle operators (+, -, *, /)
   if buttonValue is in "+-*/" then
      put buttonValue after field "display"
   end if
   
   -- Handle clear
   if buttonValue is "C" then
      put "" into field "display"
   end if
   
   -- Handle clear entry
   if buttonValue is "CE" then
      put the last line of field "display" into isTrailingReturn
      put the number of chars in isTrailingReturn into isTrailingReturn
      if isTrailingReturn <1 then delete the last line of field "display"
      
      delete the last line of field "display"
   end if
   
   -- Handle backspace
   if buttonValue is "←" then
      delete last char of field "display"
   end if
   
   -- Handle plus/minus toggle
   if buttonValue is "+/-" then
      put the itemdelimiter into myOldDelim
      set the itemdelimiter to "-"
      put the last line of field "display" into mysum
      put the last item of mysum into myRepl
      if mysum contains "-" then
         replace "-" with "+" in mysum
      else
         replace "+" with "-" in mysum
      end if
      put mysum into last line of field "display"
      set the itemdelimiter to myOldDelim
   end if
   
   -- Handle percentage
   if buttonValue is "%" then
      put last line of field "display" into mysum
      if mysum contains "*" then
         replace "*" with return in mysum
         put word 1 of line 1 of mysum into myBase
         put word 1 of line 2 of mysum into myAmount
         put perc(myBase,myAmount) into myPerc
         put return & myPerc & return after field "display"
      else
         put "Error: expected calculation to be in the form of [base number] * [amount]"
      end if
   end if
   
   -- Handle square root
   if buttonValue is "√" then
      put last line of field "display" into mysum
      put sqrt(mysum) into tResult
      put return & tResult & return after field "display"
   end if
   
   -- Handle equals
   if buttonValue is "=" then
      
      put last line of field "display" into mysum
      put sum(mysum) into tResult
      
      if tResult is "" then exit calculate
      put return & tResult & return after field "display"
   end if
   
   -- scroll field to bottom (this should be enough)
   set the textsize of field "display" to 34
   set the lineheight of field "display" to 34
   set the vscroll of field "display" to 9000
   
   set the disableScriptErrors to false
end calculate


on opencard
   set the gridsize to 10
   put "" into field "display"
   set the htmltext of field "display" to true
   set the textsize of field "display" to 34
   set the lineheight of field "display" to 34
   set the title to "Calculator"
end opencard
        graphic "roundedSquare nffkii" id=9170 asbackground="false" wtid="28" wttype="graphic" rect="40,0,430,520" showborder="false" fillcolor="30,56,112" originalheight=200 originalwidth=200 backgroundcolor="30,56,112" originalpoints="M10,0 L190,0 Q200,0 200,10 L200,190 Q200,200 190,200 L10,200 Q0,200 0,190 L0,10 Q0,0 10,0 Z" points="M19.5,0 L370.5,0 Q390,0 390,19.5 L390,500.5 Q390,520 370.5,520 L19.5,520 Q0,520 0,500.5 L0,19.5 Q0,0 19.5,0 Z" angle=0 layer="1" visible="true"
        end graphic
        field "display" id=9159 text="" textcolor="0,0,0" foregroundcolor="rgb(0, 0, 0)" caretcolor="" fontweight="400" styledtext="true" autotab="false" clicktext="" borderwidth="0px" field="styledText" rect="60,20,410,140" wttype="field" asbackground="false" hscroll=0 sharedtext="false" vscrollbar="true" backgroundcolor="rgb(184, 202, 198)" textalign="left" layer="2" locktext="false" widemargins="false" htmltext="true" textsize=34 textstyle="normal" angle=0 visible="true" visiblelines="1" formattingcommands="~v64:DQAAAAQAATEIAClzZXQgdGhlIHRleHRzaXplIG9mIGZpZWxkICJkaXNwbGF5IiB0byAzNAABMggANHNldCB0aGUgdGV4dGNvbG9yIG9mIGZpZWxkICJkaXNwbGF5IiB0byByZ2IoMCwgMCwgMCkAATMIAC1zZXQgdGhlIHRleHRjb2xvciBvZiBmaWVsZCAiZGlzcGxheSIgdG8gMCwwLDAAATQIAC5zZXQgdGhlIHRleHRzdHlsZSBvZiBmaWVsZCAiZGlzcGxheSIgdG8gbm9ybWFs" clickline="1" textfont="~b64:LWFwcGxlLXN5c3RlbSwgQmxpbmtNYWNTeXN0ZW1Gb250LCAiU2Vnb2UgVUkiLCBSb2JvdG8sIEFyaWFsLCBzYW5zLXNlcmlm" wtid="1" isiframe="false" dontwrap="true" ignorefind="false" bordercolor="rgb(153, 153, 153)" vscroll=0 lineheight=34
        end field
        button "8 " id=9149 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="8" borderwidth="1px" blendlevel="0" asbackground="false" wtid="10" wttype="button" icongravity="top" rect="150,260,230,300" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "7 " id=9150 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="7" borderwidth="1px" blendlevel="0" asbackground="false" wtid="9" wttype="button" icongravity="top" rect="60,260,140,300" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "6 " id=9151 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="6" borderwidth="1px" blendlevel="0" asbackground="false" wtid="8" wttype="button" icongravity="top" rect="240,310,320,350" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "4 " id=9152 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="4" borderwidth="1px" blendlevel="0" asbackground="false" wtid="6" wttype="button" icongravity="top" rect="60,310,140,350" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "1 " id=9153 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" angle=0 blendlevel="0" asbackground="false" wtid="25" borderwidth="1px" wttype="button" icongravity="top" rect="60,360,140,400" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "9 " id=9154 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="9" borderwidth="1px" blendlevel="0" asbackground="false" wtid="11" wttype="button" icongravity="top" rect="240,260,320,300" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "+" id=9155 accentcolor="" autohighlight="true" foregroundcolor="rgb(0, 0, 0)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 255, 137)" text="" textstyle="normal" clicktext="+" borderwidth="1px" blendlevel="0" asbackground="false" wtid="17" wttype="button" icongravity="top" rect="330,410,410,500" disabled="false" icon="" fontweight="400" textsize=24 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "." id=9156 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" angle=0 blendlevel="0" asbackground="false" wtid="16" borderwidth="1px" wttype="button" icongravity="top" rect="150,410,230,450" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "___" id=9157 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" angle=0 blendlevel="0" asbackground="false" wtid="23" borderwidth="1px" wttype="button" icongravity="top" rect="150,160,230,200" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button ")" id=9158 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext=")" borderwidth="1px" blendlevel="0" asbackground="false" wtid="27" wttype="button" icongravity="top" rect="330,160,410,200" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   send calculate("]") to this card
end mouseUp
        end button
        button "(" id=9160 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="(" borderwidth="1px" blendlevel="0" asbackground="false" wtid="26" wttype="button" icongravity="top" rect="240,160,320,200" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   send calculate("( ") to this card
end mouseUp
        end button
        button "2 " id=9161 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" angle=0 blendlevel="0" asbackground="false" wtid="4" borderwidth="1px" wttype="button" icongravity="top" rect="150,360,230,400" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "CE" id=9162 accentcolor="" autohighlight="true" foregroundcolor="rgb(0, 0, 0)" textfont="" textcolor="#000" backgroundcolor="rgb(255, 255, 9)" text="" textstyle="normal" clicktext="CE" borderwidth="1px" blendlevel="0" asbackground="false" wtid="13" wttype="button" icongravity="top" rect="150,210,230,250" disabled="false" icon="" fontweight="700" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "0 " id=9163 textstyle="normal" textcolor="#000" text="" backgroundcolor="rgb(137, 137, 137)" foregroundcolor="rgb(255, 255, 255)" autohighlight="true" accentcolor="" textfont="inherit" textsize=16 asbackground="false" wtid="2" borderwidth="1px" wttype="button" icongravity="top" rect="60,410,140,450" icon="" disabled="false" bordercolor="rgb(153, 153, 153)" angle=0 label="" visible="true" textalign="center" layer="3" highlight="false" fontweight="400"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "___" id=9164 accentcolor="" autohighlight="true" foregroundcolor="rgb(0, 0, 0)" textfont="" textcolor="#000" backgroundcolor="rgb(255, 255, 255)" text="" textstyle="normal" clicktext="~b64:4oaQ" borderwidth="1px" blendlevel="0" asbackground="false" wtid="14" wttype="button" icongravity="top" rect="240,210,320,250" disabled="false" icon="" fontweight="700" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "=" id=9165 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="=" borderwidth="1px" blendlevel="0" asbackground="false" wtid="21" wttype="button" icongravity="top" rect="240,460,320,500" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "+/-" id=9166 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="+/-" borderwidth="1px" blendlevel="0" asbackground="false" wtid="15" wttype="button" icongravity="top" rect="240,410,320,450" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "%" id=9167 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="%" borderwidth="1px" blendlevel="0" asbackground="false" wtid="22" wttype="button" icongravity="top" rect="330,210,410,250" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "5 " id=9168 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="5" borderwidth="1px" blendlevel="0" asbackground="false" wtid="7" wttype="button" icongravity="top" rect="150,310,230,350" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "C" id=9169 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(255, 0, 0)" text="" textstyle="normal" clicktext="C" borderwidth="1px" blendlevel="0" asbackground="false" wtid="12" wttype="button" icongravity="top" rect="60,160,140,250" disabled="false" icon="" fontweight="700" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "/" id=9171 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="/" borderwidth="1px" blendlevel="0" asbackground="false" wtid="20" wttype="button" icongravity="top" rect="330,260,410,300" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "3 " id=9172 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="3" borderwidth="1px" blendlevel="0" asbackground="false" wtid="5" wttype="button" icongravity="top" rect="240,360,320,400" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "*" id=9173 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="*" borderwidth="1px" blendlevel="0" asbackground="false" wtid="19" wttype="button" icongravity="top" rect="330,310,410,350" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
        button "-" id=9174 accentcolor="" autohighlight="true" foregroundcolor="rgb(255, 255, 255)" textfont="" textcolor="#000" backgroundcolor="rgb(137, 137, 137)" text="" textstyle="normal" clicktext="-" borderwidth="1px" blendlevel="0" asbackground="false" wtid="18" wttype="button" icongravity="top" rect="330,360,410,400" disabled="false" icon="" fontweight="400" textsize=16 textalign="center" layer="3" label="" visible="true" highlight="false" angle=0 bordercolor="rgb(153, 153, 153)"
on mouseUp
   put the name of me into myval
   send calculate(myval) to this card
end mouseUp
        end button
    end card
end stack
