// AnyFlex - Database editer.
// by Allan Kim Eriksen.
Use AnyFlexEnt.pkg
Use cApplication.pkg
Use SysInfoDialog.Dg
Use dfTimer.pkg
Use cCJCommandBarSystem.pkg
Use cProcessStatusPanel.pkg
Use Defines.pkg

Object oApplication is a cApplication
    Set psCompany to "Allan Kim Eriksen"
    Set psProduct to "AnyFlex"
    //Set psVersion to ""
    //Set psProgram to ""
    //Set psHelpFile To ""

    // Do not save window size. If this is set to true
    // the next time AnyFlex i started it will start i maximized mode.
    Set pbPreserveEnvironment to False
    Set psAutoOpenWorkspace to ""
    
End_Object  // oApplication


Procedure Activate_About Desktop
    Send popup of oSystemInformationDialog
End_Procedure
On_Key Key_CTRL+Key_F1 Send Activate_About


Use Info_panel.pkg
Use Resinfo.pkg


Procedure DoShowWorking String sBesked
    Send DoShowWorking of info_panel sBesked
End_Procedure


Procedure DoLukInfoPanel
    Send Close_panel of info_panel
End_Procedure


Procedure DoCloseAllFiles Desktop
    Close DF_ALL
End_Procedure


Function FieldTypeDescription Desktop Integer iFieldType Returns String
    String sDescription
    If (iFieldType = DF_ASCII) Move "ASCII" to sDescription
    Else If (iFieldType = DF_BCD) Move "NUMERIC" to sDescription
    Else If (iFieldType = DF_DATE) Move "DATE" to sDescription
    Else If (iFieldType = DF_TEXT) Move "TEXT" to sDescription
    Else If (iFieldType = DF_BINARY) Move "BINARY" to sDescription
    Else If (iFieldType = DF_OVERLAP) Move "OVERLAP" to sDescription
    Function_Return sDescription
End_Function


Function FieldType Desktop String sFieldType Returns Integer
    Integer iFieldType
    If (sFieldType = "ASCII") Move DF_ASCII to iFieldType
    Else If (sFieldType = "NUMERIC") Move DF_BCD to iFieldType
    Else If (sFieldType = "DATE") Move DF_DATE to iFieldType
    Else If (sFieldType = "TEXT") Move DF_TEXT to iFieldType
    Else If (sFieldType = "BINARY") Move DF_BINARY to iFieldType
    Else If (sFieldType = "OVERLAP") Move DF_OVERLAP to iFieldType
    Function_Return iFieldType
End_Function


Object Main is a Panel
    Set Label to "AnyFlex - Database editor"
    Set Location to 5 13
    Set Size to 271 456
    Set piMinSize to 102 300

    Object oCommandBarSystem is a cCJCommandBarSystem
        
        Object oStatusbar is a cCJStatusBar
            
            Object oStatusIdle is a cCJStatusBarPane
                Set piId to sbpIDIdlePane
                Set piWidth to 200
            End_Object     
    
        End_Object
        
    End_Object


    Object Client_Area is a AppClientArea


        Property Integer pSystemFile Public False       // DF_FILE_IS_SYSTEM_FILE
        Property Number pDataNumFields Public 0         // DF_FILE_NUMBER_FIELDS

        Property String psFileName Public ""

        Property Boolean pbFilelistUsed Public False        // If the filelist can be used for opening files.

        Property Integer piFileNumber Public 0          // Contains the choosen filenumber from the filelist.
        
        Property Boolean pbRecnumFile public False      // Does the file understand recnum fields.
        
        Property String psCurrentDriver ""              // The current driver loaded for the current table
        
        Property Integer pInternalFilenumber 0          // When no filelist this represent the last used internal filenumber

        Property String[] psDriverName                  // Contrains a list of valid drivernames.

        // Properties used for importing text file
        Property String psImportFile Public ""
        Property String psRecord Public ""
        Property String psFieldSeperator Public ","
        Property String psTextQualification Public '"'
        Property String psDateFormat Public ""
        Property String psDateSeperator Public ""
        Property Boolean pbFourDigitYear Public True
        Property String psDecimalSign Public ""
        Property Boolean pbToOEM Public True
        Property Boolean pbFirstRowIsFieldNames Public False
        Property Boolean pbTextFormatIsFixedLength Public False
        Property Integer piCurrentFilenumber Public 0
        Property String psDFDecimalSeperator Public ""
        Property String psDFDateSeperator Public ""

        Object aFilDef is a Array
        End_Object    // aFilDef

        Object oRefreshTimer is a dfTimer
            Set Auto_Start_State to False
            Set Auto_Stop_State to False
            Set Timer_Active_State to False

            //OnTimer is a predefined method in the dfTimer class
            //OnTimer executes each time the TimeOut interval passes
            Procedure OnTimer Integer wParam Integer lParam
                Forward Send OnTimer wParam lParam
                Send OnRefreshTimer
            End_Procedure // OnTimer

        End_Object    // oRefreshTimer


        Function IsInt Global String sIntegerValue Returns Boolean
            Ifnot (stringConsistsOf(sIntegerValue,"0123456789")) Function_Return False
            Function_Return True
        End_Function


        Function NextFreeFilenumber Integer iFrom Returns Integer
            Integer iFile iOffset
            Boolean bFilelistOk
            Move 0 to iOffset
            If (num_arguments = 1) Move iFrom to iOffset
            Get pbFilelistUsed to bFilelistOk
            If (bFilelistOk) Begin
                Get_Attribute DF_FILE_NEXT_EMPTY of iOffset to iFile
            End
            Else Begin
                Get pInternalFilenumber to iFile
                If (iOffset > iFile) Move iOffset to iFile
                Increment iFile
                Set pInternalFilenumber to iFile
            End
            Function_Return iFile
        End_Function


        Function DriverForOpenAs String sAktFil Returns String
          String sLine sDriver
          Integer iDrvFound iSeqFilenumber
          Move (Trim(sAktFil)) to sAktFil
          Move (toAnsi(sAktFil)) to sAktFil
        //  Uppercase sAktFil
        //  If ".INT" In sAktFil Begin
        //      Move 0 to iDrvFound
        //      Get Seq_New_Channel to iSeqFilenumber
        //      Direct_Input Channel iSeqFilenumber sAktfil
        //      While ((iDrvFound = 0) and (not(Seqeof)))
        //          Readln sLine
        //          If "DRIVER_NAME " In sLine Begin
        //              Right sLine to sDriver (Length(sLine) - 12)
        //              Increment iDrvFound
        //          End
        //      Loop
        //      Close_Input Channel iSeqFilenumber
        //      Send Seq_Release_Channel iSeqFilenumber
        //      If (Pos((sDriver+":"), sAktFil) = 0) Move (sDriver+":"+sAktFil) to sAktFil
        //  End
          Function_Return sAktFil
        End_Function


        Function FirstFieldnumber Returns Integer
            Integer iNumber iColumnFilter
            Boolean bRecnumFile
            Get pbRecnumFile to bRecnumFile
            If (bRecnumFile = True) Move 0 to iNumber
            Else Move 1 to iNumber
            Function_Return iNumber
        End_Function


        Function FirstIndexNumber Returns Integer
            Integer iNumber
            Boolean bRecnumFile
            Get pbRecnumFile to bRecnumFile
            If (bRecnumFile = True) Move 0 to iNumber
            Else Move 1 to iNumber
            Function_Return iNumber
        End_Function


        Procedure DoInitProp
            String[] sDrivername
            Move "DATAFLEX" to sDrivername[0]
            Move "MSSQLDRV" to sDrivername[1]
            Move "DFBTRDRV" to sDrivername[2]
            Move "DB2_DRV"  to sDrivername[3]
            Move "ODBC_DRV" to sDrivername[4]
            Set psDrivername to sDrivername
        End_Procedure


        // Returns the drivername if sEntry begins with a valid drivername.
        Function ExtractDrivername String sEntry Returns String
            String[] sDrivername
            Integer iAkt iMax
            Get psDrivername to sDrivername
            Move (SizeOfArray(sDrivername)) to iMax
            Decrement iMax
            For iAkt From 0 to iMax
                If (sEntry Contains (sDrivername[iAkt]+":")) Function_Return sDrivername[iAkt]
            Loop
            Function_Return ""
        End_Function


        // Returns true if sEntry begins with one of the valid drivernames.
        Function ContainsDrivername String sEntry Returns Boolean
            String sDrivername
            Get ExtractDrivername sEntry to sDrivername
            If (sDrivername <> "") Function_Return True
            Function_Return False
        End_Function


        // Remove leading drivername and traling .INT
        Function StripPrefix String sEntry Returns String
            String sDrivername
            Move (Trim(Uppercase(sEntry))) to sEntry
            Get ExtractDrivername sEntry to sDrivername
            If (sDrivername <> "") Move (Replace((sDrivername+":"), sEntry, "")) to sEntry
            Move (Replace(".INT", sEntry, "")) to sEntry
            Move (Replace(".DAT", sEntry, "")) to sEntry
            Function_Return sEntry
        End_Function

        Use AnyFlex.vw
        Use ImportTextFieldMarkup.dg

        Procedure Activate
            String sValue
            Integer iValue
            Send DoInitProp
            Forward Send Activate
            Get_Attribute DF_DECIMAL_SEPARATOR to iValue
            Move (Character(iValue)) to sValue
            Set psDFDecimalSeperator to sValue
            Get_Attribute DF_DATE_SEPARATOR to iValue
            Move (Character(iValue)) to sValue
            Set psDFDateSeperator to sValue
        End_Procedure


        Procedure OnRefreshTimer
            Send OnRefreshTimer of oEditorView
        End_Procedure

    End_Object    // Client_Area


    On_Key Key_F1 Send DoHelp


    Procedure DoHelp
        String sPath
        Get GetApplicationPath of oApplication to sPath
        Send vShellExecute "OPEN" (sPath+"\AnyFlex.chm") "" sPath
    End_Procedure


    Procedure OnRefreshTimer
        Send OnRefreshTimer of Client_Area
    End_Procedure


    Procedure exit_application
        // Close vdfquery down before forwarding exit_application
        // If vdfquery is not closed it will give an error "invalid message msg_show_status_help"
//        Broadcast Send Close_Query_View of Client_Area
        Forward Send exit_application
    End_Procedure

End_Object

Start_UI
