Jump to content

TridsCalc 2.6 - wrapper for Execute()


trids
 Share

Recommended Posts

Here is a minimalist calculator that wraps AU3's Execute() function. This GUI version updates an earlier one that got by with just an InputBox() instead of a GUI ..

I use it as an add-in tool for my text editor (TextPad). But it can be used in other applications too. Or it can run standalone (maybe with a hotkey assigned to a windows-shortcut :whistle: )

Have fun - and feel free to modify for your own context:

;TridsCalc.au3 - a calculator addin for text editors
;
;Run options - any one of the following:
; 1. Call this EXE and type/paste in an expression.
; 2. Call this EXE with an expression in the clipboard.
; 3. Setup a tool in your text editor to call this EXE.
; 4. Modify this script to run in the system tray, waiting for a hotkey.
;
;Sample expressions (use any AU3 functions)
;       2 + 3 * 5
;       2+3*5
;       (2+3)*5
;       2 ^ 3
;       2 ^ 0.5
;       1.23
;       random() * 10000
;       abs(8-23)
;       Mod(22,8)
;       Log(1000) / Log(10)
;       0x1A
;       hex(2006)
;       (sqrt(5) -1) / 2
;       StringUpper ( "the quick brown fox jumps rover" ) 
;
;2006-06-30 - Now handles columns too.
;      48                   
;      49                     
;     -33                     
;      32
;     -41            (((4 / 3) - 1) * 3) - 1 = -2.22044604925031e-016           
;     -42                       
;     -40                       
;      31                       
;                           
;2006-09-05 Modified to use the GUI instead of an InputBox

#include <GUIConstants.au3>
Global $gsTitle

    $gsTitle = @SCRIPTNAME & " - 2.6"
    
;On initial startup, get the expression or the column to turn into an expression from the clipboard
    $sExpr = ClipGet()
   
;We might have a COLUMN of values .. which means we should add them
    $sExpr = _DeColumnise( $sExpr )
       
;Display the expression, calculating any changes each time until the user Escapes.
    Opt("GUIOnEventMode", 1)  ; Set OnEvent mode 

    $hWnd = GUICreate( $gsTitle , 400, 200 )
    GUISetOnEvent( $GUI_EVENT_CLOSE, "_Form_Close" ) ; responds to {Esc}
    
    $lblPrompt = GUICtrlCreateLabel( "Expression:", 10, 10, 200 )
    
    $txtExpr = GUICtrlCreateInput( $sExpr, 10, 40, 380, 100, $ES_MULTILINE + $WS_VSCROLL )
    
    $cmdPaste = GUICtrlCreateButton( "Paste", 50, 160, 90 )
    GUICtrlSetOnEvent($cmdPaste, "_cmdPaste_onclick")
    
    $cmdFormat = GUICtrlCreateButton( "Math Format", 150, 160, 90 )
    GUICtrlSetOnEvent($cmdFormat, "_cmdFormat_onclick")
    
    $cmdOK = GUICtrlCreateButton( "OK", 250, 160, 90, default, $BS_DEFPUSHBUTTON )
    GUICtrlSetOnEvent($cmdOK, "_cmdOK_onclick")
    
    GUISetState(@SW_SHOW)

    WinSetOnTop( $gsTitle, "", 1 )
    
;get the first answer
    _cmdOK_onclick()    
    
;Idle around "letting events take care of themselves" ;o)
    While 1
        Sleep(1000)  
    WEnd


Func _DeColumnise( $psX )
;Transalates @CRLFs and @TABs into "+"
Local $sX 
    $sX = $psX
    If StringInstr( $sX, @CRLF ) Then
        ;yep we're processing a column: add up all the values
        $sX = StringReplace( $sX, @CRLF, " +" )
        ;cleanup spaces on either end
        $sX = StringStripWS( $sX, 3)
        ;drop any orphan "+"-es on the RHS
        If StringRight( $sX, 1 ) = "+" Then
            $sX = StringTrimRight( $sX, 1 )
        Endif
    Endif
    If StringInstr( $sX, @TAB ) Then
        ;possibly a selection of HTML table cells
        $sX = StringReplace( $sX, @TAB, " +" )
        ;cleanup spaces on either end
        $sX = StringStripWS( $sX, 3)
        ;drop any orphan "+"-es on the RHS
        If StringRight( $sX, 1 ) = "+" Then
            $sX = StringTrimRight( $sX, 1 )
        Endif
    Endif
    Return $sX
EndFunc

Func _cmdOK_onclick()
;recalculates the current expression.
    $sExpr = GUICtrlRead ( $txtExpr, 0 )
    $sExpr = _DeColumnise( $sExpr )

    ;forget the previous results of the expression 
    ;(anything to the RHS of the "=")
    $asX = StringSplit( $sExpr, "=" )
    $sExpr = StringStripWS( $asX[1], 3 )
    $nX = Execute( $sExpr )   
    If @ERROR Then
        ;Display an error flag
        GUICtrlSetData ( $lblPrompt, "Expression: ## ERROR ##" )
    Else
        ;Clear any previous error flag
        GUICtrlSetData ( $lblPrompt, "Expression:" )
    Endif
    $sExpr = $sExpr & " = " & $nX 
    
    GUICtrlSetData ( $txtExpr, $sExpr )
    
    ;set focus back to the textbox, at the answer 
    ControlFocus( "", "", $txtExpr )
    ControlSend( "", "", $txtExpr, "{CTRLDOWN}{END}{CTRLUP}" )
EndFunc

Func _cmdPaste_onclick()
    GUICtrlSetData ( $txtExpr, ClipGet() )
EndFunc

Func _cmdFormat_onclick()
;removes commas from the expression 
;Useful for arithmetic expressions pasted in that have thousands-separators..
    
    $sExpr = GUICtrlRead ( $txtExpr, 0 )
    $sExpr = StringReplace( $sExpr, ",", "" )       ; remove  ','
    $sExpr = StringReplace( $sExpr, " ", "" )       ; remove  ' '
    $sExpr = StringReplace( $sExpr, "+", " + " )    ; replace ' ' around '+'
    $sExpr = StringReplace( $sExpr, "-", " - " )    ; replace ' ' around '-'
    $sExpr = StringReplace( $sExpr, "*", " * " )    ; replace ' ' around '*'
    $sExpr = StringReplace( $sExpr, "/", " / " )    ; replace ' ' around '/'
    $sExpr = StringReplace( $sExpr, "=", " = " )    ; replace ' ' around '/'
    $sExpr = StringReplace( $sExpr, "(", " ( " )    ; replace ' ' around '/'
    $sExpr = StringReplace( $sExpr, ")", " ) " )    ; replace ' ' around '/'
    $sExpr = StringStripWS( $sExpr, 3 )             ; clean up the ends

    GUICtrlSetData ( $txtExpr, $sExpr )
    
    ;set focus back to the textbox, at the answer 
    ControlFocus( "", "", $txtExpr )
    ControlSend( "", "", $txtExpr, "{CTRLDOWN}{END}{CTRLUP}" )
EndFunc

Func _Form_Close()
    Exit
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...