calculator.ahk 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. file = c:\Users\levente.marton\Documents\temp.ahk ; any unused filename
  2. Menu Tray, Icon, Shell32.dll, 97
  3. IntMode = Decimal ; default output mode
  4. Prec = 0.2 ; default floating point precision
  5. Gui Add, ComboBox, X0 Y0 W300 vExpr
  6. Gui Add, Button, Default, OK ; button activated by Enter, Gui Show cuts it off
  7. Gui Add, Button, gIntM, &Binary ; Alt-B: binary integer output
  8. Gui Add, Button, gIntM, &Decimal ; Alt-D: decimal integer output
  9. Gui Add, Button, gIntM, &Hex ; Alt-H: hex integer output
  10. Gui Add, Button, , &Eng ; Alt-E: On/Off engineering mode float output
  11. Gui Add, Button, gPrec, &0
  12. Loop 9 ; Alt-number buttons for setting precision
  13. Gui Add, Button, gPrec, &%A_Index%
  14. Gui -Caption +Border ; small window w/o title bar
  15. !#x::
  16. Gui Show, H24 W277 ; calculator window: cut off unnecessary parts
  17. Return
  18. GuiEscape:
  19. Gui Show, Hide ; hide, keep data for next run
  20. Return
  21. IntM: ; setting decimal/hex/binary Integer output Mode
  22. StringTrimLeft IntMode, A_GuiControl, 1
  23. TrayTip,,%IntMode%
  24. Return
  25. ButtonEng: ; toggle engineering mode
  26. EngMode := !EngMode
  27. TrayTip,,Eng-Mode = %EngMode%
  28. Return
  29. Prec: ; set floating point precision
  30. StringTrimLeft p, A_GuiControl, 1
  31. If (A_TickCount > Tick0+999) ; long delay = set precision to entered digit
  32. pp = %p%
  33. Else pp = %pp%%p% ; short delay = append digit to precision
  34. TrayTip,,Precision = %pp% ; TrayTip w/o "0."
  35. Prec = 0.%pp%
  36. Tick0 = %A_TickCount%
  37. Return
  38. ButtonOK: ; calculate
  39. GuiControlGet Expr,,Expr ; get Expr from ComboBox
  40. GuiControl,,Expr,%Expr% ; append Expr to internal ComboBox list
  41. StringReplace Expr,Expr,`;,`n,All
  42. StringGetPos Last, Expr, `n, R1
  43. StringLeft Pre, Expr, Last ; separate Pre-amble code and Expr to evaluate
  44. StringTrimLeft Expr,Expr,Last+1
  45. FileDelete %file% ; delete old temporary file -> write new
  46. FileAppend #NoTrayIcon`nSetFormat Float`,%Prec%`nFileDelete %file%`n%pre%`nFileAppend `% %Expr%`,%file%,%file%
  47. RunWait %A_AhkPath% %file% ; run AHK to execute temp script, evaluate expression
  48. FileRead Result, %file% ; get result
  49. FileDelete %file%
  50. GuiControl,,Expr,% OM(Result) ; append Result to internal ComboBox list
  51. N += 2 ; count lines
  52. GuiControl Choose,Expr,%N% ; show Result
  53. Return
  54. OM(x) { ; format x according to the Output Mode
  55. Global IntMode, EngMode, Prec
  56. If x is not Integer
  57. {
  58. SetFormat Float, %Prec% ; used precision
  59. If (!EngMode OR x+0 = "" or x = 0)
  60. Return x+0
  61. Loop ; engineering mode
  62. If (Abs(x) < 1) {
  63. e--
  64. x *= 10 ; move the decimal point
  65. } Else If (Abs(x) >= 10) {
  66. e++
  67. x *= .1
  68. } Else Break ; normalized if 1 <= x < 10
  69. Return x chr(101+e-e) e ; 1.23e4, no "e" if e = ""
  70. }
  71. IfEqual IntMode,Binary, { ; binary integer output
  72. Loop {
  73. b := x&1 b
  74. x := x>>1
  75. If (x = x>>1) ; negative x: leading 11..1 omitted
  76. Break
  77. }
  78. Return b
  79. }
  80. SetFormat Integer,%IntMode% ; Decimal, Hex
  81. Return x+0
  82. }
  83. #notrayicon