mIRC Home    About    Download    Register    News    Help

Print Thread
Basic Calculator Script (FULL) #15118 13/03/03 12:35 AM
Joined: Jan 2003
Posts: 44
L
lambroger Offline OP
Ameglian cow
OP Offline
Ameglian cow
L
Joined: Jan 2003
Posts: 44
I wrote this today on March 12, 2003, my first foray (venture) into Dialogs, someone may find this useful.

My reasoning for doing it:
a) see how it could be done in mirc script
b) i'm a heavy gamer and I figure since I mirc open while i'm gaming might as well use it's cpu cycles for the calculator rather than starting another calc.exe process.

have fun.
Code:
;created by Roger Lamb
;lambroger@cox-internet.com
;March 12, 2003 using Mirc 6.03
dialog calculator_layout {
  ;properties for the whole layout
  title "Calculator"
  size -1 -1 140 108
  ;end of properties for the whole layout

  ;output box
  edit "",40,1 1 139 20,right read

  ;hidden storage box
  edit "",41,1 1 139 20,right read hide

  ;hidden memory box
  edit "",42,1 1 139 20,right read hide

  ;1st row
  button "7",7,1 22 20 20,default
  button "8",8,21 22 20 20,default
  button "9",9,41 22 20 20,default

  ;2nd row
  button "4",4,1 42 20 20,default
  button "5",5,21 42 20 20,default
  button "6",6,41 42 20 20,default

  ;3rd row
  button "1",1,1 62 20 20,default
  button "2",2,21 62 20 20,default
  button "3",3,41 62 20 20,default

  ;4th row
  button "0",10,21 82 20 20,default

  ;functions
  button ".",11,71 82 20 20,default
  button "=",12,91 82 20 20,default
  button "+",13,71 22 20 20,default
  button "-",14,71 42 20 20,default
  button "*",15,91 42 20 20,default
  button "/",16,91 22 20 20,default

  button "CE",17,1 82 20 20,default
  button "AC",18,41 82 20 20,default

  button "M+",19,115 62 22 20,default
  button "M-",20,115 82 22 20,default
  button "MR",21,115 42 22 20,default
  button "MC",22,115 22 22 20,default

  button "-+",23,71 62 20 20,default
  text "",24,96 65 12 12,center
}

alias calc {
  if ( %calc_active ) {
    dialog -v calculator
    return
  }
  dialog -m calculator calculator_layout
}

menu * {
  -
  Tools
  .Calculator: /calc
}

alias read_data {
  ;ensure there is data
  if( !$did(calculator,40) ) return 0
  return $did(calculator,40)
}

on 1:dialog:calculator:init:*: {
  set %calc_active 1

  set %decimal  11
  set %equal 12
  set %add  13
  set %subtract  14
  set %multiply  15
  set %divide  16

  set %clear_entry 17
  set %all_clear 18
  set %memory_add 19
  set %memory_sub 20
  set %memory_recall 21
  set %memory_clear 22

  set %sign 23
}

on 1:dialog:calculator:close:*: {
  unset %calc_active

  unset %decimal
  unset %equal
  unset %add
  unset %subtract
  unset %multiply
  unset %divide

  unset %clear_entry
  unset %all_clear
  unset %memory_add
  unset %memory_sub
  unset %memory_recall
  unset %memory_clear

  unset %sign

  unset %last_function
  unset %enter_again
}

on 1:dialog:calculator:sclick:*: {
  var %id = $did

  read_data
  var %calc_data = $result

  ;insert numbers and leave the routine
  if ( %id <= 10 ) {
    if ( %wipe_me ) {
      %calc_data = $chr(32)
      unset %wipe_me
    }
    %calc_data = %calc_data $+ $right(%id,1)
    did -o calculator 40 1 %calc_data
    return
  }
  if ( %id == 11 ) {
    if ( %wipe_me ) {
      %calc_data = 0
      unset %wipe_me
    }
    %calc_data = %calc_data $+ .
    did -o calculator 40 1 %calc_data
    return
  }

  ;ok if we get here, then it's because one of the
  ;function buttons were pressed
  var %function = %id

  ;determine which function was accessed

  if ( %function == %clear_entry ) {
    did -r calculator 40
    return
  }

  if ( %function == %sign ) {
    did -o calculator 40 1 $calc( $did(calculator,40) * -1 )
    return
  }

  if ( %last_function && %calc_data && $did(calculator,41) ) || ( %function == %equal ) {
    if ( %last_function == %multiply ) %calc_data = $calc( $did(calculator,41) * %calc_data )
    if ( %last_function == %add ) %calc_data = $calc( $did(calculator,41) + %calc_data )
    if ( %last_function == %subtract ) %calc_data = $calc( $did(calculator,41) - %calc_data )
    if ( %last_function == %divide ) %calc_data = $calc( $did(calculator,41) / %calc_data )
    did -o calculator 40 1 %calc_data
    did -r calculator 41
    unset %last_function
    if ( %function == %equal ) {
      set %wipe_me 1
      return
    }
  }

  if ( %function == %all_clear ) {
    did -r calculator 40
    did -r calculator 41
    unset %last_function
    unset %wipe_me
    return
  }

  if ( %function == %memory_recall ) {
    %calc_data = $did(calculator,42)
    did -o calculator 40 1 %calc_data
    return
  }

  if ( %function == %memory_clear ) {
    did -r calculator 42
    did -r calculator 24
    return
  }

  if ( %function == %memory_sub ) {
    did -o calculator 42 1 $calc( $did(calculator,42) - %calc_data )
    did -o calculator 24 1 M
    return
  }

  if ( %function == %memory_add ) {
    did -o calculator 42 1 $calc( $did(calculator,42) + %calc_data )
    did -o calculator 24 1 M
    return
  }

  ;ok it was a math key
  ;store old value into hidden box
  did -o calculator 41 1 %calc_data
  ;set last function
  set %last_function %id
  ;set wipe flag for numbers
  set %wipe_me 1
}
  

Re: Basic Calculator Script (FULL) #15119 13/03/03 04:00 AM
Joined: Mar 2003
Posts: 23
N
nitestarzz Offline
Ameglian cow
Offline
Ameglian cow
N
Joined: Mar 2003
Posts: 23
No ok/cancel id in the script

* /dialog: 'calculator_layout' invalid table, no ok or cancel id (line 62, script.ini)

think u need to add one =)

its a nice little function =)


blushcoolcrazy frownlaughmadshockedsmile:tongue:wink
Re: Basic Calculator Script (FULL) #15120 13/03/03 04:17 AM
Joined: Dec 2002
Posts: 3,138
C
Collective Offline
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 3,138
Quote:
No ok/cancel id in the script

* /dialog: 'calculator_layout' invalid table, no ok or cancel id (line 62, script.ini)

think u need to add one =)

its a nice little function =)

mIRC v6.02 and upwards don't need an Ok/Cancel ID for custom dialogs.

Re: Basic Calculator Script (FULL) #15121 13/03/03 01:10 PM
Joined: Dec 2002
Posts: 143
A
Aubs Offline
Vogon poet
Offline
Vogon poet
A
Joined: Dec 2002
Posts: 143
fyi, every button I press, I get:
* /if(: not connected to server (line 73, Calculator.ini)

and line 73 is the IF in this:
Code:
alias read_data {
  ;ensure there is data
  if( !$did(calculator,40) ) return 0
  return $did(calculator,40)
}


mIRC 6.03, Win2kProf


Aubs.
cool

Re: Basic Calculator Script (FULL) #15122 13/03/03 02:53 PM
Joined: Jan 2003
Posts: 44
L
lambroger Offline OP
Ameglian cow
OP Offline
Ameglian cow
L
Joined: Jan 2003
Posts: 44
ok, thanks, i'm fixing that, it's because the ( is too close to the F in IF

Re: Basic Calculator Script (FULL) #15123 13/03/03 02:58 PM
Joined: Jan 2003
Posts: 44
L
lambroger Offline OP
Ameglian cow
OP Offline
Ameglian cow
L
Joined: Jan 2003
Posts: 44
corrected script follows:

the if( error is fixed.

backwards compatibility added for older mirc users, ok/cancle buttons hidden from view

Code:
;created by Roger Lamb
;lambroger@cox-internet.com
;March 12, 2003 using Mirc 6.03
dialog calculator_layout {
  ;properties for the whole layout
  title "Calculator"
  size -1 -1 140 108
  ;end of properties for the whole layout

  ;output box
  edit "",40,1 1 139 20,right read

  ;hidden storage box
  edit "",41,1 1 139 20,right read hide

  ;hidden memory box
  edit "",42,1 1 139 20,right read hide

  ;1st row
  button "7",7,1 22 20 20,default
  button "8",8,21 22 20 20,default
  button "9",9,41 22 20 20,default

  ;2nd row
  button "4",4,1 42 20 20,default
  button "5",5,21 42 20 20,default
  button "6",6,41 42 20 20,default

  ;3rd row
  button "1",1,1 62 20 20,default
  button "2",2,21 62 20 20,default
  button "3",3,41 62 20 20,default

  ;4th row
  button "0",10,21 82 20 20,default

  ;functions
  button ".",11,71 82 20 20,default
  button "=",12,91 82 20 20,default
  button "+",13,71 22 20 20,default
  button "-",14,71 42 20 20,default
  button "*",15,91 42 20 20,default
  button "/",16,91 22 20 20,default

  button "CE",17,1 82 20 20,default
  button "AC",18,41 82 20 20,default

  button "M+",19,115 62 22 20,default
  button "M-",20,115 82 22 20,default
  button "MR",21,115 42 22 20,default
  button "MC",22,115 22 22 20,default

  button "-+",23,71 62 20 20,default
  text "",24,96 65 12 12,center

  ;backwards compat only, controls hidden
  button "",99,400 400 1 1,ok
  button "",100,400 400 1 1,cancel


}

alias calc {
  if ( %calc_active ) {
    dialog -v calculator
    return
  }
  dialog -m calculator calculator_layout
}

menu * {
  -
  Tools
  .Calculator: /calc
}

alias read_data {
  ;ensure there is data
  if ( !$did(calculator,40) ) return 0
  return $did(calculator,40)
}

on 1:dialog:calculator:init:*: {
  set %calc_active 1

  set %decimal  11
  set %equal 12
  set %add  13
  set %subtract  14
  set %multiply  15
  set %divide  16

  set %clear_entry 17
  set %all_clear 18
  set %memory_add 19
  set %memory_sub 20
  set %memory_recall 21
  set %memory_clear 22

  set %sign 23
}

on 1:dialog:calculator:close:*: {
  unset %calc_active

  unset %decimal
  unset %equal
  unset %add
  unset %subtract
  unset %multiply
  unset %divide

  unset %clear_entry
  unset %all_clear
  unset %memory_add
  unset %memory_sub
  unset %memory_recall
  unset %memory_clear

  unset %sign

  unset %last_function
  unset %enter_again
}

on 1:dialog:calculator:sclick:*: {
  var %id = $did

  read_data
  var %calc_data = $result

  ;insert numbers and leave the routine
  if ( %id <= 10 ) {
    if ( %wipe_me ) {
      %calc_data = $chr(32)
      unset %wipe_me
    }
    %calc_data = %calc_data $+ $right(%id,1)
    did -o calculator 40 1 %calc_data
    return
  }
  if ( %id == 11 ) {
    if ( %wipe_me ) {
      %calc_data = 0
      unset %wipe_me
    }
    %calc_data = %calc_data $+ .
    did -o calculator 40 1 %calc_data
    return
  }

  ;ok if we get here, then it's because one of the
  ;function buttons were pressed
  var %function = %id

  ;determine which function was accessed

  if ( %function == %clear_entry ) {
    did -r calculator 40
    return
  }

  if ( %function == %sign ) {
    did -o calculator 40 1 $calc( $did(calculator,40) * -1 )
    return
  }

  if ( %last_function && %calc_data && $did(calculator,41) ) || ( %function == %equal ) {
    if ( %last_function == %multiply ) %calc_data = $calc( $did(calculator,41) * %calc_data )
    if ( %last_function == %add ) %calc_data = $calc( $did(calculator,41) + %calc_data )
    if ( %last_function == %subtract ) %calc_data = $calc( $did(calculator,41) - %calc_data )
    if ( %last_function == %divide ) %calc_data = $calc( $did(calculator,41) / %calc_data )
    did -o calculator 40 1 %calc_data
    did -r calculator 41
    unset %last_function
    if ( %function == %equal ) {
      set %wipe_me 1
      return
    }
  }

  if ( %function == %all_clear ) {
    did -r calculator 40
    did -r calculator 41
    unset %last_function
    unset %wipe_me
    return
  }

  if ( %function == %memory_recall ) {
    %calc_data = $did(calculator,42)
    did -o calculator 40 1 %calc_data
    return
  }

  if ( %function == %memory_clear ) {
    did -r calculator 42
    did -r calculator 24
    return
  }

  if ( %function == %memory_sub ) {
    did -o calculator 42 1 $calc( $did(calculator,42) - %calc_data )
    did -o calculator 24 1 M
    return
  }

  if ( %function == %memory_add ) {
    did -o calculator 42 1 $calc( $did(calculator,42) + %calc_data )
    did -o calculator 24 1 M
    return
  }

  ;ok it was a math key
  ;store old value into hidden box
  did -o calculator 41 1 %calc_data
  ;set last function
  set %last_function %id
  ;set wipe flag for numbers
  set %wipe_me 1
}
  

Last edited by lambroger; 13/03/03 03:06 PM.
Re: Basic Calculator Script (FULL) #15124 14/03/03 05:07 AM
Joined: Jan 2003
Posts: 44
L
lambroger Offline OP
Ameglian cow
OP Offline
Ameglian cow
L
Joined: Jan 2003
Posts: 44
I'm surprised no body caught the other errors that were missed in my script/addon.

changes:
can only add 1 decimal now
sometimes only 0. is overwritten --- fixed

new code follows should be solid now:

Code:
;created by Roger Lamb
;lambroger@cox-internet.com
;March 12, 2003 using Mirc 6.03
dialog calculator_layout {
  ;properties for the whole layout
  title "Calculator"
  size -1 -1 140 108
  ;end of properties for the whole layout

  ;output box
  edit "",40,1 1 139 20,right read

  ;hidden storage box
  edit "",41,1 1 139 20,right read hide

  ;hidden memory box
  edit "",42,1 1 139 20,right read hide

  ;1st row
  button "7",7,1 22 20 20,default
  button "8",8,21 22 20 20,default
  button "9",9,41 22 20 20,default

  ;2nd row
  button "4",4,1 42 20 20,default
  button "5",5,21 42 20 20,default
  button "6",6,41 42 20 20,default

  ;3rd row
  button "1",1,1 62 20 20,default
  button "2",2,21 62 20 20,default
  button "3",3,41 62 20 20,default

  ;4th row
  button "0",10,21 82 20 20,default

  ;functions
  button ".",11,71 82 20 20,default
  button "=",12,91 82 20 20,default
  button "+",13,71 22 20 20,default
  button "-",14,71 42 20 20,default
  button "*",15,91 42 20 20,default
  button "/",16,91 22 20 20,default

  button "CE",17,1 82 20 20,default
  button "AC",18,41 82 20 20,default

  button "M+",19,115 62 22 20,default
  button "M-",20,115 82 22 20,default
  button "MR",21,115 42 22 20,default
  button "MC",22,115 22 22 20,default

  button "-+",23,71 62 20 20,default
  text "",24,96 65 12 12,center

  ;backwards compat only, controls hidden
  button "",99,400 400 1 1,ok
  button "",100,400 400 1 1,cancel
}

alias calc {
  if ( %calc_active ) {
    dialog -v calculator
    return
  }
  dialog -m calculator calculator_layout
}

menu * {
  -
  Tools
  .Calculator: /calc
}

alias read_data {
  ;ensure there is data
  if ( !$did(calculator,40) && ( $did(calculator,40) != 0. ) ) return 0
  return $did(calculator,40)
}

on 1:dialog:calculator:init:*: {
  set %calc_active 1

  set %decimal  11
  set %equal 12
  set %add  13
  set %subtract  14
  set %multiply  15
  set %divide  16

  set %clear_entry 17
  set %all_clear 18
  set %memory_add 19
  set %memory_sub 20
  set %memory_recall 21
  set %memory_clear 22

  set %sign 23
}

on 1:dialog:calculator:close:*: {
  unset %calc_active

  unset %decimal
  unset %equal
  unset %add
  unset %subtract
  unset %multiply
  unset %divide

  unset %clear_entry
  unset %all_clear
  unset %memory_add
  unset %memory_sub
  unset %memory_recall
  unset %memory_clear

  unset %sign

  unset %last_function
  unset %enter_again
}

on 1:dialog:calculator:sclick:*: {
  var %id = $did

  read_data
  var %calc_data = $result

  ;insert numbers and leave the routine
  if ( %id <= 10 ) {
    if ( %wipe_me ) {
      %calc_data = $chr(32)
      unset %wipe_me
    }
    %calc_data = %calc_data $+ $right(%id,1)
    did -o calculator 40 1 %calc_data
    return
  }
  if ( %id == 11 ) {
    if ( %wipe_me ) {
      %calc_data = 0
      unset %wipe_me
    }
    if ( . isin %calc_data ) return
    %calc_data = %calc_data $+ .
    did -o calculator 40 1 %calc_data
    return
  }

  ;ok if we get here, then it's because one of the
  ;function buttons were pressed
  var %function = %id

  ;determine which function was accessed

  if ( %function == %clear_entry ) {
    did -r calculator 40
    return
  }

  if ( %function == %sign ) {
    did -o calculator 40 1 $calc( $did(calculator,40) * -1 )
    return
  }

  if ( %last_function && %calc_data && $did(calculator,41) ) || ( %function == %equal ) {
    if ( %last_function == %multiply ) %calc_data = $calc( $did(calculator,41) * %calc_data )
    if ( %last_function == %add ) %calc_data = $calc( $did(calculator,41) + %calc_data )
    if ( %last_function == %subtract ) %calc_data = $calc( $did(calculator,41) - %calc_data )
    if ( %last_function == %divide ) %calc_data = $calc( $did(calculator,41) / %calc_data )
    did -o calculator 40 1 %calc_data
    did -r calculator 41
    unset %last_function
    if ( %function == %equal ) {
      set %wipe_me 1
      return
    }
  }

  if ( %function == %all_clear ) {
    did -r calculator 40
    did -r calculator 41
    unset %last_function
    unset %wipe_me
    return
  }

  if ( %function == %memory_recall ) {
    %calc_data = $did(calculator,42)
    did -o calculator 40 1 %calc_data
    return
  }

  if ( %function == %memory_clear ) {
    did -r calculator 42
    did -r calculator 24
    return
  }

  if ( %function == %memory_sub ) {
    did -o calculator 42 1 $calc( $did(calculator,42) - %calc_data )
    did -o calculator 24 1 M
    return
  }

  if ( %function == %memory_add ) {
    did -o calculator 42 1 $calc( $did(calculator,42) + %calc_data )
    did -o calculator 24 1 M
    return
  }

  ;ok it was a math key
  ;store old value into hidden box
  did -o calculator 41 1 %calc_data
  ;set last function
  set %last_function %id
  ;set wipe flag for numbers
  set %wipe_me 1
}


Additional notes:
Using a dialog there is no way that I know (staying within mircscript) to process keyboard key clicks.

The only way to do that would be to create a free floating window that looks like the dialog in question, but would require probably way more work than is worth it.

If i get bored (good possibility there) I'll make a version that uses a free floating window so that keys can processed as well.

Re: Basic Calculator Script (FULL) #15125 14/03/03 07:44 PM
Joined: Dec 2002
Posts: 271
N
NightChillz Offline
Fjord artisan
Offline
Fjord artisan
N
Joined: Dec 2002
Posts: 271
first of all..... a few of your "dialog events" were wrong:

on *:dialog:name:init:0 {
not :init:*:{

same for :close:

also, you can make it was smaller in a few places:


Code:
 
on 1:dialog:calculator:close:0 {
  unset %calc_active  
  unset %decimal  
  unset %equal  
  unset %add  
  unset %subtract  
  unset %multiply  
  unset %divide  
  unset %clear_entry  
  unset %all_clear  
  unset %memory_add  
  unset %memory_sub  
  unset %memory_recall  
  unset %memory_clear  
  unset %sign  
  unset %last_function 
  unset %enter_again
}
 


Can Become:

Code:
 
on 1:dialog:calculator:close:0 {
  unset %calc_active %decimal %equal %add %subtract %multiply %divide %clear_entry %all_clear %memory*  %sign %last_function %enter_again
}
 



And yet, and even better way to do it, would be to defina all your variable with a certain prefix:

%Calculator.add
%calculator.subtract
%calculator.devide
%calculator.clear_entry
%calculator.etc

so then you can turn a long code, into a simple one:

Code:
on 1:dialog:calculator:close:0 {
  unset %calculator.*
}

  


also, a few places you have... for example:

did -r blah blah1
did -r blah blah2

you can simplify this by doing:

did -r blah blah1,blah2,blah3,etc


also an error with MR:


if i do 666 + "MR" it doesnt allow it, and resets the + param, thus MR is my new and only param.... uif you understand what i mean....just thought i'd take a look at it and see if i can give ya more work wink

Re: Basic Calculator Script (FULL) #15126 18/03/03 03:39 PM
Joined: Jan 2003
Posts: 44
L
lambroger Offline OP
Ameglian cow
OP Offline
Ameglian cow
L
Joined: Jan 2003
Posts: 44
ok, thanks on the actual bugs part

as far as optimizing it, this is my style, i program things this way so I can easily see what I was doing 10 months down the road.

on the init and mr bugs, thanks again.

Re: Basic Calculator Script (FULL) #15127 20/03/03 02:33 AM
Joined: Jan 2003
Posts: 44
L
lambroger Offline OP
Ameglian cow
OP Offline
Ameglian cow
L
Joined: Jan 2003
Posts: 44
ok, i performed some code beautification for readability and possibly some small optimizations (probably not enough to notice).

For all of the global variables that use set and unset, i've renamed them so that they will be unique to the calculator and thus also easier to unset.

The MR bug is fixed as well.

Some of you will notice that I have pretty much converted this to almost reflect a procedural style. I did this after realizing I could do that. I had not realized before that I could treat aliases in that way. This actually made coding it easier and increased it's readability.

instead of having in my code:

where somealias is a sub routine

.....
somealias argument
echo -a $result
....

it now appears as

....
echo -a $somealias($result)
....

In some sections this resulted in a massive decrease in code.
It also made it much easier to debug, by allowing me to use
my alias inside of calculations and other formulae

before where I would've done

somealias argument
var %somedata = $result
var %otherdata = The data is $+ $result

I can now do

var %otherdata = The data is $+ $somealias(argument)

in the case of aliases that don't take arguments I simply use
(), so that I can still embed them, this feels very natural me.

thanks to whomever posted a script previously that had this other way of handling aliases.

here is the new calculator code:
Code:
;created by Roger Lamb
;lambroger@cox-internet.com
;March 12, 2003 using Mirc 6.03
dialog calculator_layout {
  ;properties for the whole layout
  title "Calculator"
  size -1 -1 140 108
  ;end of properties for the whole layout

  ;output box: use read_display, store_display and clear_display functions
  edit "",40,1 1 139 20,right read

  ;hidden storage box: use read_storage, store_storage and clear_storage functions
  edit "",41,1 1 139 20,right read hide

  ;hidden memory box: use read_memory, store_memory and clear_memory functions
  edit "",42,1 1 139 20,right read hide

  ;1st row
  button "7",7,1 22 20 20,default
  button "8",8,21 22 20 20,default
  button "9",9,41 22 20 20,default

  ;2nd row
  button "4",4,1 42 20 20,default
  button "5",5,21 42 20 20,default
  button "6",6,41 42 20 20,default

  ;3rd row
  button "1",1,1 62 20 20,default
  button "2",2,21 62 20 20,default
  button "3",3,41 62 20 20,default

  ;4th row
  button "0",10,21 82 20 20,default

  ;functions
  button ".",11,71 82 20 20,default
  button "=",12,91 82 20 20,default
  button "+",13,71 22 20 20,default
  button "-",14,71 42 20 20,default
  button "*",15,91 42 20 20,default
  button "/",16,91 22 20 20,default

  button "CE",17,1 82 20 20,default
  button "AC",18,41 82 20 20,default

  button "M+",19,115 62 22 20,default
  button "M-",20,115 82 22 20,default
  button "MR",21,115 42 22 20,default
  button "MC",22,115 22 22 20,default

  button "-+",23,71 62 20 20,default
  text "",24,96 65 12 12,center

  ;backwards compat only, controls hidden
  button "",99,400 400 1 1,ok
  button "",100,400 400 1 1,cancel
}

;ie /calc not $calc
alias calc {
  if ( %calculator_active ) {
    dialog -v calculator
    return
  }
  dialog -m calculator calculator_layout
}

menu * {
  -
  Tools
  .Calculator: /calc
}

;display access functions
alias read_display {
  return $did(calculator,40)
}

alias clear_display {
  did -r calculator 40
}

alias store_display {
  did -o calculator 40 1 $1
}

;storage access functions
alias read_storage {
  return $did(calculator,41)
}

alias clear_storage {
  did -r calculator 41
}

alias store_storage {
  did -o calculator 41 1 $1
}

;memory access functiosn
alias read_memory {
  return $did(calculator,42)
}

alias clear_memory {
  did -r calculator 42
  did -r calculator 24
}

alias store_memory {
  did -o calculator 42 1 $1
  did -o calculator 24 1 M
}

alias read_data {
  ;ensure there is data
  if ( ( !$read_display() ) && ( $read_display() != 3 ) ) return 0
  return $read_display()
}

on 1:dialog:calculator:init:*: {
  set %calculator_active 1

  set %calculator_equal 12
  set %calculator_add  13
  set %calculator_subtract  14
  set %calculator_multiply  15
  set %calculator_divide  16

  set %calculator_clear_entry 17
  set %calculator_all_clear 18
  set %calculator_memory_add 19
  set %calculator_memory_sub 20
  set %calculator_memory_recall 21
  set %calculator_memory_clear 22

  set %calculator_sign 23
}

on 1:dialog:calculator:close:*: {
  unset %calculator*
}

on 1:dialog:calculator:sclick:*: {
  var %id = $did

  ;insert numbers and leave the routine
  if ( %id <= 10 ) {
    ;check if a wipe has been requested
    if ( %wipe_me ) {
      $clear_display()
      unset %wipe_me
    }
    $store_display( $read_display() $+ $right(%id,1) )
    return
  }

  if ( %id == 11 ) {
    if ( . isin $read_display() ) return
    ;check if a wipe has been requested
    if ( %wipe_me ) {
      $clear_display()
      unset %wipe_me
    }
    $store_display( $read_display() $+ . )
    return
  }

  ;ok if we get here, then it's because one of the
  ;function buttons were pressed
  var %function = %id

  ;determine which function was accessed

  if ( %function == %calculator_clear_entry ) {
    $clear_display()
    return
  }

  if ( %function == %calculator_sign ) {
    $store_display($calc( $read_display() * -1 ))
    return
  }

  if ( %function == %calculator_memory_recall ) {
    $store_display($read_memory())
    return
  }

  if ( %last_function && $read_display() && $read_storage() ) || ( %function == %calculator_equal ) {
    if ( %last_function == %calculator_multiply ) $store_display($calc( $read_storage() * $read_display() ))
    elseif ( %last_function == %calculator_add ) $store_display($calc( $read_storage() + $read_display() ))
    elseif ( %last_function == %calculator_subtract ) $store_display($calc( $read_storage - $read_display() ))
    elseif ( %last_function == %calculator_divide ) $store_display($calc( $read_storage / $read_display() ))
    ;    $clear_storage()
    unset %last_function
  }

  if ( %function == %calculator_all_clear ) {
    $clear_display()
    $clear_storage()
    $clear_memory()
    unset %last_function
    unset %wipe_me
    return
  }

  if ( %function == %calculator_memory_clear ) {
    $clear_memory()
    return
  }

  if ( %function == %calculator_memory_sub ) {
    $store_memory($calc( $read_memory() - $read_display() ))
    set %wipe_me 1
    return
  }

  if ( %function == %calculator_memory_add ) {
    $store_memory($calc( $read_memory() + $read_display() ))
    set %wipe_me 1
    return
  }

  ;ok it was a math key
  ;store old value into hidden box
  $store_storage( $read_display() )

  ;set last function
  set %last_function %id

  ;set wipe flag for numbers
  set %wipe_me 1
}

enjoy