/* Custom mainline for macro */

    call RXFuncAdd "VRLoadFuncs", "VROBJ", "VRLoadFuncs"
    call VRLoadFuncs

    _VREVersion = SubWord( VRVersion( "VRObj" ), 1, 1 )
    if( _VREVersion < 2.10 )then do
        call VRMessage "", "This program requires VX-REXX version 2.1 to run.", "Error!"
        return 32000
    end

    signal on SYNTAX name _VRESyntax
    signal _VREMain

_VRESyntax:
    parse source . . _VRESourceSpec
    call VRMessage "", "Syntax error in" _VRESourceSpec "line" SIGL":" ErrorText(rc), "Error!"
    call VRFini
    exit 32000

_VREMain:
/*:VRX         Main
*/
/*  Main
*/
Main:
/*  Process the arguments.
    Get the parent window.
*/
    parse source . calledAs .
    parent = ""
    argCount = arg()
    argOff = 0
    if( calledAs \= "COMMAND" )then do
        if argCount >= 1 then do
            parent = arg(1)
            argCount = argCount - 1
            argOff = 1
        end
    end
    InitArgs.0 = argCount
    if( argCount > 0 )then do i = 1 to argCount
        InitArgs.i = arg( i + argOff )
    end
    drop calledAs argCount argOff

/*  Load the windows
*/
    call VRInit
    parse source . . spec
    _VREPrimaryWindowPath = ,
        VRParseFileName( spec, "dpn" ) || ".VRW"
    _VREPrimaryWindow = ,
        VRLoad( parent, _VREPrimaryWindowPath )
    drop parent spec
    if( _VREPrimaryWindow == "" )then do
        call VRMessage "", "Cannot load window:" VRError(), ,
            "Error!"
        _VREReturnValue = 32000
        signal _VRELeaveMain
    end

/*  Process events
*/
    call Init
    signal on halt
    do while( \ VRGet( _VREPrimaryWindow, "Shutdown" ) )
        _VREEvent = VREvent()
        interpret _VREEvent
    end
_VREHalt:
    _VREReturnValue = Fini()
    call VRDestroy _VREPrimaryWindow
_VRELeaveMain:
    call VRFini
exit _VREReturnValue

VRLoadSecondary: procedure
    name = arg( 1 )

    window = VRLoad( VRWindow(), VRWindowPath(), name )
    call VRMethod window, "CenterWindow"
    call VRSet window, "Visible", 1
    call VRMethod window, "Activate"
return window

/*:VRX         EFValue_KeyPress
*/
EFValue_KeyPress:
if( VRGet( 'EFValue', 'KeyString' ) = '{Newline}' )then
    call PBIns_Click
return

/*:VRX         Fini
*/
Fini:
    window = VRWindow()
    call VRSet window, "Visible", 0
    drop window
return 0

/*:VRX         Halt
*/
Halt:
    signal _VREHalt
return

/*:VRX         Init
*/
Init:
    call VRMethod 'Application', 'GetVar', 'args.'
    tID = args.1
    AcctDir = args.2
    window = VRWindow()
    call VRMethod window, "CenterWindow"
    call VRSet window, "Visible", 1
    call VRMethod window, "Activate"
    drop window
return

/*:VRX         LBFields_DoubleClick
*/
LBFields_DoubleClick:
call VRMethod "LBFields", "GetSelectedStringList", "sel."
if sel.0 = 0 then
    return
call VRMethod "MLESearch", "Insert", sel.1||' '
call VRMethod 'PBContains', 'SetFocus'
return

/*:VRX         MLESearch_KeyPress
*/
MLESearch_KeyPress:
if( VRGet( 'MLESearch', 'KeyString' ) = '{Newline}' )then do
    call VRMethod 'PBSearch', 'SetFocus'
    call PBSearch_Click
    call VRSet 'MLESearch', 'KeyString', ''
end
return

/*:VRX         PBAnd_Click
*/
PBAnd_Click:
call VRMethod "MLESearch", "Insert", "& "
call VRMethod 'EFValue', 'SetFocus'
call VRSet 'EFValue', 'SelectedStart', 1, 'SelectedEnd', Length( VRGet( 'EFValue', 'Value' ) ) + 1
return

/*:VRX         PBCats_Click
*/
PBCats_Click:
if( \VRFileExists( AcctDir||'\category.dat' ) )then do
    call VRMessage 'Screen', 'Category file not found.  ', 'Information'
    call VRSet 'PBCats', 'Enabled', 0
    return
end
DatFile = AcctDir||'\category.dat'
if( vrfileexists( DatFile ) )then do
    call stream DatFile, 'c', 'close'
    call stream DatFile, 'c', 'open'
    call stream DatFile, 'c', 'seek =1'
    cats.0 = 0
    count = 1
    do forever
        if( Lines( DatFile ) = 0 )then
            leave
        line = linein( DatFile )
        if line <> '' then do
            cats.count = line
            cats.0 = count
            count = count + 1
        end
    end
    call stream DatFile, 'c', 'close'
end
drop count appval lineval
if cats.0 = 0 then do
    call VRMessage VRWindow(), 'Categories were not found.  ', 'Information'
    return
end
call VRMethod 'Application', 'PutVar', 'cats.'
call 'macros\etcat.VRM'
call VRMethod 'Application', 'GetVar', 'rc'
if rc <> 'ERROR' then
    ok = VRMethod( "MLESearch", "Insert", '"'||rc||'" ' )
drop rc cats.
return

/*:VRX         PBClear_Click
*/
PBClear_Click:
call VRSet 'MLESearch', 'Value', '', 'UserData', ''
return

/*:VRX         PBContains_Click
*/
PBContains_Click:
call VRMethod "MLESearch", "Insert", "? "
call VRMethod 'EFValue', 'SetFocus'
call VRSet 'EFValue', 'SelectedStart', 1, 'SelectedEnd', Length( VRGet( 'EFValue', 'Value' ) ) + 1
return

/*:VRX         PBEquals_Click
*/
PBEquals_Click:
call VRMethod "MLESearch", "Insert", "= "
call VRMethod 'EFValue', 'SetFocus'
call VRSet 'EFValue', 'SelectedStart', 1, 'SelectedEnd', Length( VRGet( 'EFValue', 'Value' ) ) + 1
return

/*:VRX         PBGreater_Click
*/
PBGreater_Click:
call VRMethod "MLESearch", "Insert", "> "
call VRMethod 'EFValue', 'SetFocus'
call VRSet 'EFValue', 'SelectedStart', 1, 'SelectedEnd', Length( VRGet( 'EFValue', 'Value' ) ) + 1
return

/*:VRX         PBIns_Click
*/
PBIns_Click:
call VRMethod "MLESearch", "Insert", '"'||Translate( VRGet( "EFValue", "Value" ) )||'" '
call VRMethod 'PBSearch', 'SetFocus'
return

/*:VRX         PBLess_Click
*/
PBLess_Click:
call VRMethod "MLESearch", "Insert", "< "
call VRMethod 'EFValue', 'SetFocus'
call VRSet 'EFValue', 'SelectedStart', 1, 'SelectedEnd', Length( VRGet( 'EFValue', 'Value' ) ) + 1
return

/*:VRX         PBNotContains_Click
*/
PBNotContains_Click:
call VRMethod "MLESearch", "Insert", "! "
call VRMethod 'EFValue', 'SetFocus'
call VRSet 'EFValue', 'SelectedStart', 1, 'SelectedEnd', Length( VRGet( 'EFValue', 'Value' ) ) + 1
return

/*:VRX         PBNotEquals_Click
*/
PBNotEquals_Click:
call VRMethod "MLESearch", "Insert", "<> "
call VRMethod 'EFValue', 'SetFocus'
call VRSet 'EFValue', 'SelectedStart', 1, 'SelectedEnd', Length( VRGet( 'EFValue', 'Value' ) ) + 1
return

/*:VRX         PBOr_Click
*/
PBOr_Click:
call VRMethod "MLESearch", "Insert", "| "
call VRMethod 'EFValue', 'SetFocus'
call VRSet 'EFValue', 'SelectedStart', 1, 'SelectedEnd', Length( VRGet( 'EFValue', 'Value' ) ) + 1
return

/*:VRX         PBSearch_Click
*/
PBSearch_Click:
/*
 * Check criteria syntax
 */
Search = Translate( VRGet( 'MLESearch', 'Value' ) )
if Search = '' then do
    call VRMessage 'Screen', 'Search criterion not defined.  Nothing to do.  ', 'Information'
    return
end
pos = POS( '"0"', search )
if pos <> 0 then 
    do until pos = 0
        search = DelStr( search, pos + 1, 1 )
        pos = POS( '"0"', search )
    end
pos = POS( '"0.0"', search )
if pos <> 0 then
    do until pos = 0
        search = DelStr( search, pos + 1, 3 )
        pos = POS( '"0.0"', search )
    end
pos = POS( '"0.00"', search )
if pos <> 0 then
    do until pos = 0
        search = DelStr( search, pos + 1, 4 )
        pos = POS( '"0.00"', search )
    end
pos = POS( '"0,0"', search )
if pos <> 0 then
    do until pos = 0
        search = DelStr( search, pos + 1, 3 )
        pos = POS( '"0,0"', search )
    end
pos = POS( '"0,00"', search )
if pos <> 0 then
    do until pos = 0
        search = DelStr( search, pos + 1, 4 )
        pos = POS( '"0,00"', search )
    end
drop pos
call VRSet 'MLESearch', 'Value', Search
/*
 * Build formatted MLE string
 */
tmpSearch = Search
newSearch = ''
do until tmpSearch = ''
    didContains = 0
    word  = Translate( Word( tmpSearch, 1 ) )
    if word = '' then
        leave
    if( word <> 'DATE' & word <> 'NUMBER' & word <> 'PARTICULARS' & word <> 'CREDIT' & word <> 'DEBIT' & word <> 'BALANCE' & word <> 'MEMO' & word <> 'CATEGORY' & word <> 'MULTICATEGORY' & word <> '|' & word <> '&' & word <> '!' )then do
        call VRMessage 'SWSearch', 'Syntax error:  field not found.  ', 'Error'
        return
    end
    if( word = 'DATE' | word = 'NUMBER' | word = 'PARTICULARS' | word = 'CREDIT' | word = 'DEBIT' | word = 'BALANCE' | word = 'MEMO' | word = 'CATEGORY' | word = 'MULTICATEGORY' )then do
            val = 'Translate( rec.i.!'||word' )'
        tmpSearch = DelWord( tmpSearch, 1, 1 )
        word = Word( tmpSearch, 1 )
        if( word <> '?' & word <> '>' & word <> '<' & word <> '<>' & word <> '=' & word <> '!' )then do
            call VRMessage 'SWSearch', 'Syntax error:  symbol not found.  ', 'Error'
            return
        end
        if( word = '?' | word = '!' )then do
            didContains = 1
            tmpSearch = DelWord( tmpSearch, 1, 1 )
            tmpword = word
            word = ''
            if( Left( tmpSearch, 1 ) <> '"' )then do
                call VRMessage 'SWSearch', 'Syntax error:  start quote not found.  ', 'Error'
                return
            end; else do
                position  = Pos( '"', tmpSearch, 2 )
                if position = 0 then do
                    call VRMessage 'SWSearch', 'Syntax error:  end quote not found.  ', 'Error'
                    return
                end
                if tmpword = '?' then
                    newSearch = newSearch||' POS( '||SubStr( tmpSearch, 1, position + 1 )||', '||val||' ) <> 0'
                else
                    newSearch = newSearch||' POS( '||SubStr( tmpSearch, 1, position + 1 )||', '||val||' ) = 0'
                drop tmpword
                tmpSearch = DelStr( tmpSearch, 1, position + 1 )
            end            
        end
        if( word = '>' | word = '<' | word = '<>' | word = '=' )then do
            key = word
            tmpSearch = DelWord( tmpSearch, 1, 1 )
            word = ''
        end
        if didContains = 0 then do
            if( Left( tmpSearch, 1 ) <> '"' )then do
                call VRMessage 'SWSearch', 'Syntax error:  start quote not found.  ', 'Error'
                return
            end; else do
                position  = Pos( '"', tmpSearch, 2 )
                if position = 0 then do
                    call VRMessage 'SWSearch', 'Syntax error:  end quote not found.  ', 'Error'
                    return
                end
                string = SubStr( tmpSearch, 1, position + 1 )
                tmpSearch = DelStr( tmpSearch, 1, position + 1 )
            end            
            newSearch = newSearch||' '||val||' '||key||' '||string
        end
    end
    if( word = '|' | word = '&' )then do
        newSearch = newSearch||' '||word
        tmpSearch = DelWord( tmpSearch, 1, 1 )
    end
end
newSearch = Strip( newSearch, 'L', ' ' )
newSearch = Strip( newSearch, 'T', ' ' )
call VRSet 'MLESearch', 'UserData', newSearch
rc = newSearch
call VRMethod 'Application', 'PutVar', 'rc'
call VRSet 'PBUndo', 'Enabled', 1
call VRMethod 'Application', 'PostQueue', tID, 1, 'call PerformSearch'
drop search field key tmpSearch didContains string newSearch
return

/*:VRX         PBUndo_Click
*/
PBUndo_Click:
call VRMethod 'Application', 'PostQueue', tID, 1, 'call UndoSearch'
call VRSet 'PBUndo', 'Enabled', 0
return

/*:VRX         Quit
*/
Quit:
    call VRMethod 'Application', 'PostQueue', tID, 1, 'call UndoSearch'
    window = VRWindow()
    call VRSet window, "Shutdown", 1
    drop window
return

/*:VRX         Window1_Close
*/
Window1_Close:
    call Quit
return

/*:VRX         Window1_Help
*/
Window1_Help: 
address cmd 'view e-teller Search Feature'
return

