#Include "FiveWin.Ch"
* By: Adhemar Cuéllar
*
Function Main()
Local oDlg
Local oGet
local cVar := 0.00
local lCalcula := .f.
DEFINE DIALOG oDlg from 0,0 to 400,400 pixel
@ 15,15 get oGet var cVar picture "@E" bitmap "..\bitmaps\calc.bmp" action Calc(oDlg,oGet,@cVar);
size 120,12 of oDlg pixel
oGet:lAdjustBtn := .t.
ACTIVATE DIALOG oDlg
return nil
Function Calc(oDlg,oGet,cVar)
Local oCalc:=tCalc():new()
oCalc:calculadora(oDlg,'@E 9,999,999.9999',.F.,oGet)
cVar := iif(!Empty(oCalc:nResult), oCalc:nResult, cVar)
oGet :Refresh()
oCalc:End()
Return .T.
*
CLASS TCalc
DATA nResult
DATA aRolloCalc
DATA cUltima, cUltimaop
DATA cPatronSalida
METHOD New() CONSTRUCTOR
METHOD Init()
METHOD End()
METHOD nCalculadigual(nGet)
METHOD calculadora(oPadre,cPatronSalida,lBorraAlInicio)
METHOD TCalc_Teclas(nKey, oDlg)
METHOD Init(oGet,nKey)
ENDCLASS
//----------------------------------------------------------------------------//
METHOD New() CLASS TCalc
::aRolloCalc:={{'C',' '}}
::nResult :=0
::cUltima :=''
::cUltimaop :=''
return SELF
//----------------------------------------------------------------------------//
METHOD Init(oGet,nKey) CLASS tCalc
LOCAL oCalc
IF nKey <> VK_F3
RETURN(1)
ENDIF
oCalc:=tCalc():new()
oCalc:calculadora( oGet:oWnd, '@E 9,999,999.9999 ',,oGet )
RETURN(0)
//----------------------------------------------------------------------------//
METHOD End() CLASS TCalc
::aRolloCalc:={{'C',' '}}
::nResult :=0
::cUltima :=''
::cUltimaop :=''
return NIL
//----------------------------------------------------------------------------//
METHOD calculadora(oPadre,cPatronSalida,lBorraAlInicio,oTGet) CLASS TCalc
local oDlg
local oBtnSalir,oBtnTrans
local oBtnmenos, oBtnmas, oBtnpor, oBtndividir, oBtnigual, oBtnBorra
local oBtn0, oBtn1, oBtn2, oBtn3, oBtn4, oBtn5, oBtn6, oBtn7, oBtn8, oBtn9, oBtnP, oBtnSigno
local oBtnConv, oBtnConv2
local oBtnDel
local oSay
local oBrw, n:=1
local oGet :=0 ,cGet:='', nGetAnt:=0,nGet:=0
local oFont
local oFont1
local oFontLst
::nResult := IIF(VALTYPE(oTGet) == "O" .AND. "GET" $oTGet:ClassName .AND.;
VAL(oTGet:GetText()) > 0,VAL(oTGet:GetText()),;
IIF(VALTYPE(oTGet) == "N",oTGet,0))
if cPatronSalida=NIL .and. ::cPatronSalida=NIL
cPatronSalida:='@E 9,999,999,999.99 '
endif
default lBorraAlInicio :=.f.
::cPatronSalida:=cPatronSalida
DEFINE FONT oFont NAME "Ms Sans Serif" SIZE 0,-14 BOLD
DEFINE FONT oFont1 NAME "Arial" SIZE 0,-20
DEFINE FONT oFontLst NAME "MS Sans Serif" SIZE 0,-14
DEFINE DIALOG oDlg RESOURCE 'Calculadora' OF oPadre TITLE '(OASyS) Calculadora' FONT oFont
REDEFINE BUTTON oBtnTrans ID 112 OF oDlg MESSAGE 'Lleva el resultado para o campo numérico.';
ACTION oDlg:end() CANCEL UPDATE
oBtnTrans:cargo:='T'
REDEFINE BUTTON oBtnSalir ID 500 OF oDlg MESSAGE 'Cancela';
ACTION (::nResult:=0,oDlg:end()) default CANCEL UPDATE
oBtnSalir:cargo:='S'
REDEFINE BUTTON oBtnmenos ID 401 OF oDlg MESSAGE 'Resta';
ACTION (iif(nGet<>0,(nGetAnt:=nGet,aadd(::aRolloCalc,{transform(nGet,::cPatronSalida),'-'})),(::nResult:=::nResult-nGetAnt,aadd(::aRolloCalc,{transform(iif(::cUltimaOp='=',::nResult,nGetAnt),::cPatronSalida),'-'}))) ,;
oBrw:gobottom(),;
oBrw:refresh(),;
iif(::nResult=0,::nResult:=nGet,::nResult:=::nResult-nGet),;
::cUltimaOp:='-' ,;
oSay:SetText(transform(::nResult,::cPatronSalida)),;
nGet:=0 ,;
cGet:='' ,;
oGet:SetText('') ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtnmenos:cargo:='-'
REDEFINE BUTTON oBtnmas ID 402 OF oDlg MESSAGE 'Suma';
ACTION (iif(nGet<>0,(nGetAnt:=nGet,aadd(::aRolloCalc,{transform(nGet,::cPatronSalida),'+'})),(::nResult:=::nResult+nGetAnt,aadd(::aRolloCalc,{transform(iif(::cUltimaOp='=',::nResult,nGetAnt),::cPatronSalida),'+'}))) ,;
oBrw:gobottom(),;
oBrw:refresh(),;
iif(::nResult=0,::nResult:=nGet,::nResult:=::nResult+nGet),;
::cUltimaOp:='+' ,;
oSay:SetText(transform(::nResult,::cPatronSalida)),;
nGet:=0 ,;
cGet:='' ,;
oGet:SetText('') ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtnmas:cargo:='+'
REDEFINE BUTTON oBtnpor ID 403 OF oDlg MESSAGE 'Multiplica';
ACTION (aadd( ::aRolloCalc, iif(nGet=0 .and. ::nResult<>0,{transform(::nResult,::cPatronSalida),'x'},{transform(nGet,::cPatronSalida),'x'}) ),;
oBrw:gobottom(),;
oBrw:refresh(),;
iif(::nResult=0,::nResult:=nGet,iif(nGet<>0,::nResult:=::nResult*nGet,)),;
::cUltimaOp:='*' ,;
oSay:SetText(transform(::nResult,::cPatronSalida)),;
nGet:=0 ,;
cGet:='' ,;
oGet:SetText('') ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtnpor:cargo:='*'
REDEFINE BUTTON oBtndividir ID 404 OF oDlg MESSAGE 'Divide';
ACTION (aadd( ::aRolloCalc, iif(nGet=0 .and. ::nResult<>0,{transform(::nResult,::cPatronSalida),'/'},{transform(nGet,::cPatronSalida),'/'}) ),;
oBrw:gobottom(),;
oBrw:refresh(),;
iif(::nResult=0,::nResult:=nGet,iif(nGet<>0,::nResult:=::nResult/nGet,)),;
::cUltimaOp:='/' ,;
oSay:SetText(transform(::nResult,::cPatronSalida)),;
nGet:=0 ,;
cGet:='' ,;
oGet:SetText('') ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtndividir:cargo:='/'
REDEFINE BUTTON oBtnigual ID 405 OF oDlg MESSAGE 'Calcula resultado';
ACTION (iif(nGet=0,,aadd( ::aRolloCalc, {transform(nGet,::cPatronSalida),iif(::cUltimaOp='+' .or. ::cUltimaOp='-' .or. ::cUltimaOp='*' .or. ::cUltimaOp='/',' ',::cUltimaOp)} )),;
::nCalculadIgual(nGet),;
aadd( ::aRolloCalc, {transform(::nResult,::cPatronSalida),'='} ),;
oBrw:gobottom(),oBrw:gobottom(),;
oBrw:refresh(),;
::cUltimaOp:='=' ,;
oSay:SetText(transform(::nResult,::cPatronSalida)),;
nGet:=0 ,;
nGetAnt:=0 ,;
cGet:='' ,;
oGet:SetText('') ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtnigual:cargo:='='
REDEFINE BUTTON oBtnBorra ID 425 OF oDlg MESSAGE 'Borra todos los cálculos';
ACTION (iif( len( ::aRolloCalc ) > 0,;
( aSize( ::aRolloCalc, 0 ), oBrw:Refresh() ), ),;
aadd( ::aRolloCalc, {' ','C'} ),;
oBrw:gotop() ,;
oBrw:refresh() ,;
::nResult:=0 ,;
::cUltimaOp:='R' ,;
oSay:SetText('0') ,;
nGet:=0 ,;
cGet:='' ,;
oGet:SetText('') ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtnBorra:cargo:='R'
REDEFINE BUTTON oBtnDel ID 426 OF oDlg MESSAGE 'Borra último dígito';
ACTION (::cUltima:='CE' ,;
iif(::cUltimaOp='=' .or. ::cUltimaOp='AP' .or. ::cUltimaOp='AE',(::cUltimaOp:='',::nResult:=0,aadd( ::aRolloCalc, {' ',' '} ), oBrw:gobottom(),oBrw:refresh()),),;
iif(nGet<>0,(cGet:=LEFT(cGet,len(cget)-1),nGet:=val(cGet)),) ,;
oGet:SetText(cGet) ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtnDel:cargo:=chr(8)
REDEFINE BUTTON oBtn0 ID 100 OF oDlg;
ACTION (::cUltima:='0' ,;
iif(::cUltimaOp='=' .or. ::cUltimaOp='AP' .or. ::cUltimaOp='AE',(::cUltimaOp:='',::nResult:=0,aadd( ::aRolloCalc, {' ',' '} ), oBrw:gobottom(),oBrw:refresh()),),;
cGet:=cGet+::cUltima ,;
nGet:=val(cGet) ,;
oGet:SetText(cGet) ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtn0:cargo:='0'
REDEFINE BUTTON oBtn1 ID 101 OF oDlg;
ACTION (::cUltima:='1' ,;
iif(::cUltimaOp='=' .or. ::cUltimaOp='AP' .or. ::cUltimaOp='AE',(::cUltimaOp:='',::nResult:=0,aadd( ::aRolloCalc, {' ',' '} ), oBrw:gobottom(),oBrw:refresh()),),;
cGet:=cGet+::cUltima ,;
nGet:=val(cGet) ,;
oGet:SetText(cGet) ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtn1:cargo:='1'
REDEFINE BUTTON oBtn2 ID 102 OF oDlg;
ACTION (::cUltima:='2' ,;
iif(::cUltimaOp='=' .or. ::cUltimaOp='AP' .or. ::cUltimaOp='AE',(::cUltimaOp:='',::nResult:=0,aadd( ::aRolloCalc, {' ',' '} ), oBrw:gobottom(),oBrw:refresh()),),;
cGet:=cGet+::cUltima ,;
nGet:=val(cGet) ,;
oGet:SetText(cGet) ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtn2:cargo:='2'
REDEFINE BUTTON oBtn3 ID 103 OF oDlg;
ACTION (::cUltima:='3' ,;
iif(::cUltimaOp='=' .or. ::cUltimaOp='AP' .or. ::cUltimaOp='AE',(::cUltimaOp:='',::nResult:=0,aadd( ::aRolloCalc, {' ',' '} ), oBrw:gobottom(),oBrw:refresh()),),;
cGet:=cGet+::cUltima ,;
nGet:=val(cGet) ,;
oGet:SetText(cGet) ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtn3:cargo:='3'
REDEFINE BUTTON oBtn4 ID 104 OF oDlg;
ACTION (::cUltima:='4' ,;
iif(::cUltimaOp='=' .or. ::cUltimaOp='AP' .or. ::cUltimaOp='AE',(::cUltimaOp:='',::nResult:=0,aadd( ::aRolloCalc, {' ',' '} ), oBrw:gobottom(),oBrw:refresh()),),;
cGet:=cGet+::cUltima ,;
nGet:=val(cGet) ,;
oGet:SetText(cGet) ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtn4:cargo:='4'
REDEFINE BUTTON oBtn5 ID 105 OF oDlg;
ACTION (::cUltima:='5' ,;
iif(::cUltimaOp='=' .or. ::cUltimaOp='AP' .or. ::cUltimaOp='AE',(::cUltimaOp:='',::nResult:=0,aadd( ::aRolloCalc, {' ',' '} ), oBrw:gobottom(),oBrw:refresh()),),;
cGet:=cGet+::cUltima ,;
nGet:=val(cGet) ,;
oGet:SetText(cGet) ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtn5:cargo:='5'
REDEFINE BUTTON oBtn6 ID 106 OF oDlg;
ACTION (::cUltima:='6' ,;
iif(::cUltimaOp='=' .or. ::cUltimaOp='AP' .or. ::cUltimaOp='AE',(::cUltimaOp:='',::nResult:=0,aadd( ::aRolloCalc, {' ',' '} ), oBrw:gobottom(),oBrw:refresh()),),;
cGet:=cGet+::cUltima ,;
nGet:=val(cGet) ,;
oGet:SetText(cGet) ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtn6:cargo:='6'
REDEFINE BUTTON oBtn7 ID 107 OF oDlg;
ACTION (::cUltima:='7' ,;
iif(::cUltimaOp='=' .or. ::cUltimaOp='AP' .or. ::cUltimaOp='AE',(::cUltimaOp:='',::nResult:=0,aadd( ::aRolloCalc, {' ',' '} ), oBrw:gobottom(),oBrw:refresh()),),;
cGet:=cGet+::cUltima ,;
nGet:=val(cGet) ,;
oGet:SetText(cGet) ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtn7:cargo:='7'
REDEFINE BUTTON oBtn8 ID 108 OF oDlg;
ACTION (::cUltima:='8' ,;
iif(::cUltimaOp='=' .or. ::cUltimaOp='AP' .or. ::cUltimaOp='AE',(::cUltimaOp:='',::nResult:=0,aadd( ::aRolloCalc, {' ',' '} ), oBrw:gobottom(),oBrw:refresh()),),;
cGet:=cGet+::cUltima ,;
nGet:=val(cGet) ,;
oGet:SetText(cGet) ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtn8:cargo:='8'
REDEFINE BUTTON oBtn9 ID 109 OF oDlg;
ACTION (::cUltima:='9' ,;
iif(::cUltimaOp='=' .or. ::cUltimaOp='AP' .or. ::cUltimaOp='AE',(::cUltimaOp:='',::nResult:=0,aadd( ::aRolloCalc, {' ',' '} ), oBrw:gobottom(),oBrw:refresh()),),;
cGet:=cGet+::cUltima ,;
nGet:=val(cGet) ,;
oGet:SetText(cGet) ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtn9:cargo:='9'
REDEFINE BUTTON oBtnP ID 110 OF oDlg;
ACTION (::cUltima:='.' ,;
iif(::cUltimaOp='=' .or. ::cUltimaOp='AP' .or. ::cUltimaOp='AE',(::cUltimaOp:='',::nResult:=0,aadd( ::aRolloCalc, {' ',' '} ), oBrw:gobottom(),oBrw:refresh()),),;
cGet:=cGet+::cUltima ,;
nGet:=val(cGet) ,;
oGet:SetText(cGet) ,;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE WHEN int(nGet)==nGet
oBtnP:cargo:='.'
REDEFINE BUTTON oBtnSigno ID 111 OF oDlg;
ACTION (::cUltima:='signo' ,;
iif(::cUltimaOp='=' .or. ::cUltimaOp='AP' .or. ::cUltimaOp='AE',(::cUltimaOp:='',::nResult:=0,aadd( ::aRolloCalc, {' ',' '} ), oBrw:gobottom(),oBrw:refresh()),),;
nGet:=(-1)*nGet ,;
oGet:SetText(nGet) ,;
cGet:=alltrim(str(nGet)),;
oBtnIgual:Setfocus() ,;
sysrefresh() ) UPDATE
oBtnSigno:cargo:=' '
REDEFINE SAY oGet VAR nGet ID 211 OF oDlg FONT oFont1 ;
COLOR nrgb( 0, 0, 0),nrgb(255,255,200) UPDATE
REDEFINE SAY oSay VAR ::nResult ID 200 OF oDlg FONT oFont1 ;
COLOR nrgb(50,250,50),nrgb(0,0,0) UPDATE
REDEFINE LISTBOX oBrw FIELDS ;
::aRolloCalc[n][1],;
::aRolloCalc[n][2];
HEAD 'valor','op';
ID 212 OF oDlg FONT oFontLst COLOR nrgb(0,0,0),nrgb(255,255,200) ;//clrLtrBrow,clrFonBrow ;
UPDATE
oBrw:aJustify :={.t.,.f.}
oBrw:aColSizes :={GetFontInfo(oFontLst)[2] * 1.25 * 17, GetFontInfo(oFontLst)[2] * 1.25 * 3} //aMatrizTc({17,3},oFontLst)
oBrw:bGotop := { || n := 1 }
oBrw:bGoBottom := { || n := eval( oBrw:bLogicLen ) }
oBrw:bSkip := { | nwant, nold | nold := n , n += nwant,;
n := max( 1, min( n, eval( oBrw:bLogicLen ))),;
n - nOld }
oBrw:bLogicLen := { || len( ::aRolloCalc ) }
oBrw:nClrPane:={|| iif(::aRolloCalc[n][2]=='=',nrgb(200,255,125),nrgb(255,255,200)) }
oDlg:bKeyDown:={|nKey| ::TCalc_Teclas(nKey,oDlg,oTget)}
ACTIVATE DIALOG oDlg CENTERED ON INIT (if(lBorraAlInicio,oBtnBorra:CLICK(),.f.),oBrw:gobottom(),oBrw:refresh())
oFont:end()
oFont1:end()
oFontLst:end()
return ::nResult
*
METHOD nCalculadigual(nGet) CLASS TCalc
do CASE
CASE ::cUltimaOp = '-'
::nResult := ::nResult - nGet
CASE ::cUltimaOp = '+'
::nResult := ::nResult + nGet
CASE ::cUltimaOp = '*'
::nResult := ::nResult * nGet
CASE ::cUltimaOp = '/'
::nResult := ::nResult / nGet
endcase
return NIL
*
METHOD TCalc_Teclas(nKey, oDlg,oTGet) class TCalc
local ny
for ny:=1 TO len(oDlg:aControls)
if oDlg:aControls[ny]:cargo==upper(chr(nKey))
oDlg:aControls[ny]:CLICK()
endif
next ny
IF nKey == 84 .AND. VALTYPE(oTGet) == "O"
oDlg:End()
oTGet:VarPut(::nResult)
oTGet:REFRESH()
ENDIF
return nil