* Program..: Math.prg * Author...: Jeremy Suiter * Date.....: 23/04/2003 * * Notice...: Copyright (c) 2003, Jeremy Suiter, All Rights Reserved * Notes....: Visual DbEditor count/sum/average functions #Include "Gra.ch" #Include "Xbp.ch" #Include "Appevent.ch" #Include "DbEditor.ch" #Include "Dll.ch" #Include "Font.ch" #Include "FoxDbe.ch" #Include "CdxDbe.ch" #Include "Dmlb.ch" #Include "Dbstruct.ch" #Include "Resources.ch" Static oFor, oWhile, oNext, oRest, oProgress, cType, oCount, oSum, oAverage, oCountRes, oSumRes, oAvRes ************* Func MathFunc() ************* * math functions Local nEvent:=Nil, mp1:=Nil, mp2:=Nil, aPos:={} Local oDlg:=Nil, oXbp:=Nil, drawingArea:=Nil, oGroup1:=Nil Begin Sequence cType:=ValType(oFocusBrowse:getData()[1]) aPos:=CenterPos({402,318},AppDesktop():currentSize()) oDlg:=XbpDialog():new(AppDesktop(), MainWindow(), aPos, {402,318}, DEFAULT_PRESPARAM, .F.) oDlg:icon:=ICON_MAIN oDlg:taskList:=.F. oDlg:border:=XBPDLG_RAISEDBORDERTHIN_FIXED oDlg:title:="Math Functions" oDlg:close:={|mp1,mp2,obj| PostAppEvent(xbeP_Close+1000) } oDlg:create() oDlg:setModalState(XBP_DISP_APPMODAL) drawingArea:=oDlg:drawingArea drawingArea:setFont(oDlg:setFont()) // export options oGroup1:=XbpStatic():new(drawingArea, , {12,48}, {372,236}) oGroup1:caption:="Options" oGroup1:clipSiblings:=.T. oGroup1:type:=XBPSTATIC_TYPE_GROUPBOX oGroup1:create() oCount:=XbpCheckbox():new(oGroup1, , {68,194}, {68,24}) oCount:caption:="Count" oCount:tabStop:=.T. If !cType=="N" oCount:selection:=.T. EndIf oCount:create() oSum:=XbpCheckbox():new(oGroup1, , {168,194}, {68,24}) oSum:caption:="Sum" oSum:tabStop:=.T. oSum:create() If !cType=="N" oSum:disable() EndIf oAverage:=XbpCheckbox():new(oGroup1, , {268,194}, {68,24}) oAverage:caption:="Average" oAverage:tabStop:=.T. oAverage:create() If !cType=="N" oAverage:disable() EndIf oXbp:=XbpStatic():new(oGroup1, , {12,158}, {44,24}) oXbp:caption:=GEN_FOR oXbp:clipSiblings:=.T. oXbp:options:=XBPSTATIC_TEXT_VCENTER+XBPSTATIC_TEXT_RIGHT oXbp:create() oFor:=XbpSLE():new(oGroup1, , {68,158}, {292,24}, { { XBP_PP_BGCLR, XBPSYSCLR_ENTRYFIELD } }) oFor:tabStop:=.T. oFor:create() oXbp:=XbpStatic():new(oGroup1, , {12,122}, {44,24}) oXbp:caption:=GEN_WHILE oXbp:clipSiblings:=.T. oXbp:options:=XBPSTATIC_TEXT_VCENTER+XBPSTATIC_TEXT_RIGHT oXbp:create() oWhile:=XbpSLE():new(oGroup1, , {68,122}, {292,24}, { { XBP_PP_BGCLR, XBPSYSCLR_ENTRYFIELD } }) oWhile:tabStop:=.T. oWhile:create() oXbp:=XbpStatic():new(oGroup1, , {12,86}, {44,24}) oXbp:caption:=GEN_NEXT oXbp:clipSiblings:=.T. oXbp:options:=XBPSTATIC_TEXT_VCENTER+XBPSTATIC_TEXT_RIGHT oXbp:create() oNext:=XbpSpinbutton():new(oGroup1, , {68,86}, {60,24}, { { XBP_PP_BGCLR, XBPSYSCLR_ENTRYFIELD } }) oNext:tabStop:=.T. oNext:create() oNext:setNumLimits(0,LastRec()-RecNo()) oNext:setData(0) oRest:=XbpCheckbox():new(oGroup1, , {168,86}, {96,24}) oRest:caption:=GEN_REST oRest:tabStop:=.T. oRest:create() // recessed static for progress bar oXbp:=XbpStatic():new(oGroup1, , {12,50}, {350,20}) oXbp:type:=XBPSTATIC_TYPE_RECESSEDBOX oXbp:create() oProgress:=ProgressBar():new(oGroup1, , {12,50}, {350,20},,.F.) oProgress:create() oProgress:hide() // results oXbp:=XbpStatic():new(oGroup1, , {12,12}, {30,24}) oXbp:caption:="Count" oXbp:clipSiblings:=.T. oXbp:options:=XBPSTATIC_TEXT_VCENTER+XBPSTATIC_TEXT_RIGHT oXbp:create() oXbp:=XbpStatic():new(oGroup1, , {46,14}, {62,20}) oXbp:type:=XBPSTATIC_TYPE_RECESSEDBOX oXbp:create() oCountRes:=XbpStatic():new(oGroup1, , {47,15}, {60,18}) oCountRes:options:=XBPSTATIC_TEXT_VCENTER+XBPSTATIC_TEXT_RIGHT oCountRes:clipSiblings:=.T. oCountRes:create() oXbp:=XbpStatic():new(oGroup1, , {110,12}, {24,24}) oXbp:caption:="Sum" oXbp:clipSiblings:=.T. oXbp:options:=XBPSTATIC_TEXT_VCENTER+XBPSTATIC_TEXT_RIGHT oXbp:create() oXbp:=XbpStatic():new(oGroup1, , {138,14}, {100,20}) oXbp:type:=XBPSTATIC_TYPE_RECESSEDBOX oXbp:create() oSumRes:=XbpStatic():new(oGroup1, , {139,15}, {98,18}) oSumRes:options:=XBPSTATIC_TEXT_VCENTER+XBPSTATIC_TEXT_RIGHT oSumRes:clipSiblings:=.T. oSumRes:create() oXbp:=XbpStatic():new(oGroup1, , {242,12}, {44,24}) oXbp:caption:="Average" oXbp:clipSiblings:=.T. oXbp:options:=XBPSTATIC_TEXT_VCENTER+XBPSTATIC_TEXT_RIGHT oXbp:create() oXbp:=XbpStatic():new(oGroup1, , {288,14}, {74,20}) oXbp:type:=XBPSTATIC_TYPE_RECESSEDBOX oXbp:create() oAvRes:=XbpStatic():new(oGroup1, , {289,15}, {72,18}) oAvRes:options:=XBPSTATIC_TEXT_VCENTER+XBPSTATIC_TEXT_RIGHT oAvRes:clipSiblings:=.T. oAvRes:create() // pushbuttons oXbp:=XbpPushButton():new(drawingArea, , {180,12}, {96,24}, { { XBP_PP_BGCLR, XBPSYSCLR_BUTTONMIDDLE }, { XBP_PP_FGCLR, -58 } }) oXbp:caption:="Calculate" oXbp:tabStop:=.T. oXbp:create() oXbp:activate:={|| MathCalc(cType) } oXbp:=XbpPushButton():new(drawingArea, , {288,12}, {96,24}, { { XBP_PP_BGCLR, XBPSYSCLR_BUTTONMIDDLE }, { XBP_PP_FGCLR, -58 } }) oXbp:caption:=GEN_CLOSE oXbp:tabStop:=.T. oXbp:create() oXbp:activate:={|| PostAppEvent(xbeP_Close+1000) } oDlg:show() SetAppFocus(oCount) nEvent:=xbe_None Do While nEvent<>xbeP_Close+1000 nEvent:=AppEvent(@mp1, @mp2, @oXbp) oXbp:handleEvent(nEvent, mp1, mp2) if nEvent == xbeP_Keyboard .and.; mp1 == xbeK_ESC exit endif EndDo oDlg:destroy() SetAppFocus(oFocusBrowse) End Return Nil ************* Func MathCalc(cType) ************* * calculate Local cFor:="", cWhile:="", bTemp, bFor:=Nil, bWhile:=Nil, nRecNo:=RecNo(), nCount:=0, nSum:=0, nAverage:=0, nNext:=0, cField Begin Sequence If !oCount:getData() .And. !oSum:getData() .And. !oAverage:getData() Break EndIf cFor:=oFor:getData() cWhile:=oWhile:getData() nNext:=oNext:getData() If (!Empty(cFor) .And. !TestCondition(cFor)) .Or. (!Empty(cWhile) .And. !TestCondition(cWhile)) Break EndIf If !YesNo("Calculate") Break EndIf // calculate If Empty(cFor) cFor:=".T." EndIf bTemp:=&("{|| "+cFor+" }") bFor:={|| oProgress:increment(), Eval(bTemp) } If Empty(cWhile) cWhile:=".T." EndIf bWhile:=&("{|| "+cWhile+" }") DbSuspendNotifications() oProgress:minimum:=1 oProgress:maximum:=LastRec() oProgress:current:=1 oProgress:show() cField:=oFocusBrowse:getColumn(oFocusBrowse:colpos):cargo DbEval({|| nCount++, IIf(cType=="N",nSum+=FieldGet(FieldPos(cField)),Nil) }, bFor, bWhile, Iif(nNext>0,nNext,Nil), , oRest:getData()) If oAverage:getData() nAverage:=nSum/nCount EndIf DbGoto(nRecNo) DbResumeNotifications() oProgress:hide() If oCount:getData() oCountRes:setCaption(Ntrim(nCount)) Else oCountRes:setCaption("") EndIf If oSum:getData() oSumRes:setCaption(Ntrim(nSum)) Else oSumRes:setCaption("") EndIf If oAverage:getData() oAvRes:setCaption(Ntrim(nAverage)) Else oAvRes:setCaption("") EndIf End Return Nil