Use DFAllEnt.pkg
#IF (!@ < 200)
  Error DfErr_Compile "A minimum version of DataFlex 20.0 is needed to compile this version of the Hammer, please change compiler or use an earlier version of The Hammer."
  Abort
#ELSE

#IFNDEF IS$WIN64
  Error DfErr_Compile "32 bit support for Hammer4 has been deprecated and is likely to be removed.\nPlease change the compiler settings to 64 bit or use an earlier version of The Hammer."
#ENDIF

//CompilerLevelWarning General Off
//CompilerLevelWarning Unicode On

// Comment out if you do not want to use interface translation option
Define TH_TRANSLATION
// Source is not using obsolete filesize function
Define no_cFileSystem_filesize_here
//
Use cCJStandardCommandBarSystem.pkg
Register_Object oClientArea
Register_Object oMain
// General
Use TheHammer.h
Use Tools\TH3Workspace.pkg
Use cWorkSpaceHandlerEx.pkg     // Workspace handler (needs to be at the top)
Use cTH_StatusPanel.pkg         // Status panel for batch processing (used in the utilities)
Use cSciLexer.pkg               // Scintilla editing control
Use Start_UIExcl.Pkg            // Some abilities to check if another instance of the program is running
Use mExecute.Pkg                // Covers calls to ShellExecute.
Use cSplitterControl.Pkg        // Splitters
Use vWin32fh.pkg
Use cParser.Pkg                 // Parses the Text
Use Bitmaps.pkg                 // Get Bitmaps for Classes
Use DrgNDrop.pkg                // Drag&Drop Handler from Stuart Booth
Use oKeysArray.pkg              // An Array which contains all KeyNames.
Use mWinAPIGetKeyNameText.pkg   // Conversion of VKKeys to ANSI keycodes.
Use cFDFileReader.Pkg           // Reads Field names out of an *.FD file.
// Codemax Editor specific Packages
Use mEditorMacros.pkg
Use mEditorGeneral.Pkg          // General Functions and Definitions as: TabDialog KeyDefinitions, FileOpenDialog
Use oEditorProperties.Pkg       // Contains all Properties which can be Save (SaveIni) and loaded (LoadIni) to disk.
Use FileAssociation.pkg         // functions for associating files with the Hammer
#IFDEF TH_TRANSLATION
Use Tools\Translation.pkg
#ENDIF
Use THColorer.pkg               // Colorer
Use cCodeTip.Pkg                // Class for CodeTip support
Use cSciCodeList.pkg            // Class for Scintilla CodeList support
Use oIDETagsArray.pkg           // An Array that contains all the currently existing IDE/DDB/Studio tags (old now, but let's move it in for easy code syncing)
Use TokensList.pkg            // Available DataFlex Commands (up to v18)
Use cEditorEdit.Pkg             // subclasses the cCodeMaxEdit for the use in this editor
Use cEditorEditView.Pkg         // creates a cEditorEdit in itself. (cIconView)
Use cEditorToolView.Pkg         // Standard view for showing others then files in the editor.
Use mUserAndComputerName.pkg    // gsComputerName && gsUserName
Use Tools\WorkSpace.Sl
Use oObjectsMethods.Sl
Use Tools\RegisterCls.dg
Use Tools\EditorCmdLinePara.Sl        // Simple ModalPanel to enter a commandline. Used with get GetCommandLineParameter "Init" to sCmd
Use cDependencyScanner.pkg      // Scans all adjusted filetypes in the current directory for their #include and Use statements.
Use CodeArt.pkg
Use CodePub.pkg
//
Use Help_Ids.inc    // Developer should provide this file of help context links.
Use cHtmlHelp.pkg   // Help
Use cRPCServer.pkg  // To control from one Editor to another.
Use Tools\findfiles.dg

Object oHtmlHelp is a cHtmlHelp
End_Object

Use cCJSkinFramework.pkg

If (piUseSkins(ghoEditorProperties)) Begin
    Object oSkin is a cCJSkinFramework
       Set psSkinFile to (psCurrentSkinFile(ghoEditorProperties))
       Set psSkinIni to (psCurrentSkinIni(ghoEditorProperties))
    End_Object
End

Object oToolTipController is a cToolTipController
    Move Self to ghoToolTipController
End_Object

// This function returns Current Editor view.
Function GetCurrentEditView Global Returns Integer
    Integer hCl hVw
    Move (oClientArea(oMain(Self))) to hCl
    If (hCl) Begin
       Get Next_Mdi_Dialog of hcl True to hVw // find first view
       While hVw
          If (Active_state(hVw)) Begin
             // if this is the focus view, mark it.
             If (current_scope(desktop)=hVw and View_mode(hVw)<>VIEWMODE_ICONIZE) Begin
                Function_Return (oEdit(hVw))
             End
          End
          Get Next_Mdi_Dialog of hcl False to hVw // find next
       End
    End
End_Function

// A Procedure which assigns a keystring to a menu item (Either PopupMenu or PullDownMenu)
// correspondinf to a CommandID.
Procedure AddKeyStringToMenuItem Integer hoID Integer iItem Integer iCmd
    Integer iPos
    String  sKey sVal
    Get CMCommandToHotKeyStr iCmd to sKey
    If sKey Ne "" If hoID Ne 0 Begin
        Get value of hoID item iItem                to sVal
        Move (Pos("\a",sVal))                       to iPos
        If iPos Eq 0 Move (Pos(Character(9),sVal))  to iPos
        If iPos Gt 0 Move (Left(sVal,iPos-1))       to sVal
        Move (RTrim(sVal))                          to sVal
        Append sVal (Character(9)) sKey
        Set value of hoID item iItem                to sVal
    End
End_Procedure

Procedure AddCJKeyStringToMenuItem Integer hoID Integer iCmd
    String  sKey
    Get CMCommandToHotKeyStr iCmd to sKey
    If sKey Ne "" If hoID Ne 0 ;
        Set psShortcut of hoID to sKey
End_Procedure

Procedure RegisterEditor
	Send AsociateAllFileTypes
	Abort
End_Procedure

If (piWrkSpcRegister(ghoApplication)) Send RegisterEditor

Use proppage.dg

Object oRPCEditorClient is a cRPCClient
    Set psServerName to "EDITOR"
        // Functions which are called in an RPCServer.
    RPC_External_Procedure ExtCAFileOpen String sFileName
    RPC_External_Procedure ExtCANewFile
    Procedure TryToTransferCommandLineParameters
        Handle hwnd
        Move (isServerRunning(Self))    to hwnd
        If hwnd Begin
            If (psWrkSpcOpenFileName(ghoApplication)) Ne "" Begin
                Send WrkSpcDoForAllFiles  to ghoApplication Msg_ExtCAFileOpen Self
//                Send ExtCAFileOpen (psWrkSpcOpenFileName(ghoWorkSpace))
                Abort
            End
            If (piWrkSpcNewFile(ghoApplication)) Begin
                Send ExtCANewFile
                Abort
            End
        End
    End_Procedure
    Send TryToTransferCommandLineParameters
End_Object

Function CreateProgramLabel Global Returns String
    String sRet
    // **WvA: Removed "project part" prefix from caption so that
    // you can see more details when using the Cool tab switch (==Alt+tab)
    //Append sRet gsProjectName " - " gsProgrammname " " gsVerionsNr
    Append sRet CTH_Programm_Name " " CTH_Version_No
    Function_Return sRet
End_Function

Use oEditContextMenu.pkg
Use cTH3Panel.pkg
Use Menu\TH3FileTabContextMenu.mn
Use THCommandBars.pkg
Use THCustomMenu.pkg

Object oMain is a cHammerPanel
    Set Location     to 4 3
    Set Size         to 300 450
    Set Label        to (CreateProgramLabel())
    Set pbStartingUp to True

        // Added to support Reopen of Files based on Workspace
        // 18.01.01 Bernhard
    Procedure OnWorkspaceChanged
        Set pbWorkspaceChanging to True
        Send ShowWorkspaceInfo to (oClientArea(Self))

        If (not(pbStartingUp(Self))) Begin
          Send UnloadNonEmbeddedDrivers of ghoWorkSpaceHandlerEx
        End
        Send RegisterAllConnections of ghoConnection  //@ RRS
        Send LoginAll of ghoConnection                //@ RRS

        Send ReOpenAllFiles
        Send LoadSettings to oFindFiles
        Set pbWorkspaceChanging to False
        Send RebuildPanes to oToolPane
    End_Procedure

        // Added to support Reopen of Files based on Workspace
        // 18.01.01 Bernhard
    Function RequestChangeWorkspace String sWrkSpace Returns Integer
        Integer iRet
        Send CACreateEditorEntries to (oClientArea(Self))
        Get MSG_CACloseAllFiles of (oClientArea(Self)) sWrkSpace to iRet
        If (iRet) Function_Return False
        Function_Return True
    End_Function


    // SetRedraw set's the redraw flag of main and oClientArea directly via API
    // if set true, the objects are repainted.
    // 28.02.2003 BP
    //Procedure SetRedraw Global Integer iState
    //  // codemax only
    //End_Procedure

    Object oParserControl is a cParserControl
        Move (Self) to ghoParserControl
        Procedure destroy_object
            Move 0 to ghoParserControl
            Forward Send destroy_object
        End_Procedure
    End_Object

        // Allows other instances of the Editor to take control over some actions.
    Object oRPCEditorServer is a cRPCServer
        Set ConnectName to "EDITOR"
        RPC_Procedure ExtCAFileOpen String sFileName Integer iLine
            Send CAOpenFile to (oClientArea(Self)) sFileName
        RPC_End_Procedure
        RPC_Procedure ExtCANewFile
            Send CANewFile to (oClientArea(Self))
        RPC_End_Procedure
    End_Object

    #Include RunProgramNotify.inc   // Allows to start the compiler in the background and beeing notified when finished!
    #Include Tools\TH3ToolsPanes.inc
    #Include Tools\oFileFinder.pkg

    Use THCompiler.pkg

    Object oCommandBarSystem is a cTHCJCommandBarSystem
        // The Hammer 3.0 Menu Bar
        Object oMenuBar is a cCJMenuBar
            #Include Menu\TH3MenuFile.mn
            #Include Menu\TH3MenuEdit.mn
            #Include Menu\TH3MenuBuild.mn
            #Include Menu\TH3MenuTools.mn
            #Include Menu\TH3MenuSource.mn
            #Include Menu\TH3MenuDatabase.mn
            #Include Menu\TH3MenuWindow.mn
            #Include Menu\TH3MenuHelp.mn
        End_Object
        // The Hammer 3.0 Tool Bar
        #Include Tools\TH3ToolBar.pkg
        // The Hammer 3.0 Status Bar
        #Include Tools\TH3StatusBar.pkg
        //
        #IFDEF TH_TRANSLATION
        Procedure Translate
            // File Menu
            Set psCaption       of (oFileMenu(oMenuBar(Self))) to gILanguage[0]
            Set psDescription   of (oFileMenu(oMenuBar(Self))) to gILanguage[1]
            Set psCategory      of (oFileMenu(oMenuBar(Self))) to gILanguage[2]
            Send Translate      to (oFileMenu(oMenuBar(Self)))
            // Edit Menu
            Set psCaption       of (oEditMenu(oMenuBar(Self))) to gILanguage[59]
            Set psDescription   of (oEditMenu(oMenuBar(Self))) to gILanguage[60]
            Set psCategory      of (oEditMenu(oMenuBar(Self))) to gILanguage[61]
            Send Translate      to (oEditMenu(oMenuBar(Self)))
            // Build Menu
            Set psCaption       of (oBuildMenu(oMenuBar(Self))) to gILanguage[101]
            Set psDescription   of (oBuildMenu(oMenuBar(Self))) to gILanguage[102]
            Set psCategory      of (oBuildMenu(oMenuBar(Self))) to gILanguage[103]
            Send Translate      to (oBuildMenu(oMenuBar(Self)))
            // Tools Menu
            Set psCaption       of (oToolsMenu(oMenuBar(Self))) to gILanguage[142]
            Set psDescription   of (oToolsMenu(oMenuBar(Self))) to gILanguage[143]
            Set psCategory      of (oToolsMenu(oMenuBar(Self))) to gILanguage[144]
            Send Translate      to (oToolsMenu(oMenuBar(Self)))
            // Source Menu
            Set psCaption       of (oSourceMenu(oMenuBar(Self))) to gILanguage[256]
            Set psDescription   of (oSourceMenu(oMenuBar(Self))) to gILanguage[257]
            Set psCategory      of (oSourceMenu(oMenuBar(Self))) to gILanguage[258]
            Send Translate      to (oSourceMenu(oMenuBar(Self)))
            // Database Menu
            Set psCaption       of (oDatabaseMenu(oMenuBar(Self))) to gILanguage[291]
            Set psDescription   of (oDatabaseMenu(oMenuBar(Self))) to gILanguage[292]
            Set psCategory      of (oDatabaseMenu(oMenuBar(Self))) to gILanguage[293]
            Send Translate      to (oDatabaseMenu(oMenuBar(Self)))
            // Window Menu
            Set psCaption       of (oWindowMenu(oMenuBar(Self))) to gILanguage[309]
            Set psDescription   of (oWindowMenu(oMenuBar(Self))) to gILanguage[310]
            Set psCategory      of (oWindowMenu(oMenuBar(Self))) to gILanguage[311]
            Send Translate      to (oWindowMenu(oMenuBar(Self)))
            // Help Menu
            Set psCaption       of (oHelpMenu(oMenuBar(Self))) to gILanguage[331]
            Set psDescription   of (oHelpMenu(oMenuBar(Self))) to gILanguage[332]
            Set psCategory      of (oHelpMenu(oMenuBar(Self))) to gILanguage[333]
            Send Translate      to (oHelpMenu(oMenuBar(Self)))
            // Tool Bars
            Send Translate      to oFileToolBar
            Send Translate      to oEditToolBar
            Send Translate      to oRunToolBar
            Send Translate      to oToolsToolBar
            // Status Bar
            Send Translate      to oStatusBar
            // Panes
            If ghoToolsPane     Send Translate to ghoToolsPane  gILanguage[430]
            If ghoOutputPane    Send Translate to ghoOutputPane gILanguage[461]
        End_Procedure
        #ENDIF
        //
    End_Object

    Object oClientArea is a ClientArea
        Property String   psLastCompiledFileName ""
        Property Integer  piLastCompiledMode     0
        Property Integer  piLastCompiledRun      0
        Property Integer  piLastCompileHide      False
        Property Integer  piViewsZoomed          (piViewMode(ghoEditorProperties))
        Property Integer  piActiveView           0
        Property Integer  piLastActiveView       0
        Property String[] pasDroppedFiles

        Set pbAcceptDropFiles To True
        
        Procedure ProcessDroppedFiles String[] asDroppedFiles
            Integer iFile
            String  sFile

            For iFile From 0 To (SizeOfArray(asDroppedFiles)-1)
                Move asDroppedFiles[iFile] To sFile
                If (sFile<>"") Begin
                    Send CAOpenFile sFile
                End
            Loop
        End_Procedure

        // Process files dropped directly on the client area.
        Procedure OnFileDropped String sFilename Boolean bLast
            Integer  iFiles
            String[] asDroppedFiles EmptyArray
            
            Get pasDroppedFiles to asDroppedFiles

            // Add the filename to the list of dropped files....
            Move (SizeOfArray(asDroppedFiles)) to iFiles
            Move sFilename to asDroppedFiles[iFiles]
            
            // If this was the last file, process them and clear the list
            If (bLast) Begin
                Send ProcessDroppedFiles asDroppedFiles
                // clear the property so it is ready for next drop
                Move EmptyArray to asDroppedFiles
            End
            Set pasDroppedFiles to asDroppedFiles
        End_Procedure
        
        //
        Use Menu\TH3EditorContextMenu.mn
        Use Tools\DependencyConf.SL
        Use Tools\TH3WorkspaceConfigure.dg
        Use Tools\oHorizontalSplitter.pkg
        Use Tools\THParameters.dg
        Use Tools\TableProperties.dg
        Use Tools\CustMenu.dg
        Use ThaHammerActions.pkg
        Use dfcbldr.pkg         // Registering classes in the IDE and creating *.DFC files.
        Use Tools\THAbout.dg
        Use Tools\LoginConnectDialog.dg

    End_Object
    Send Deactivate to (Splash_Screen(Self)) //@
End_Object

//
// This will put the last edited file at the end of the saOpened array.
// The idea is that the file you last edited will open up again so that you are immediately
// back to where you were. As this was not the order things get saved in and as the "last edited"
// file is saved outside of the workspace we have to do it this way.
//
Procedure ReOrderLastEditedFile String[] ByRef saOpened
  Integer iFile
  Integer iFiles
  Integer iFound
  String  sFile
  String  sLastEdited
  
  If (pbStartingUp(oMain(Self))) Begin  // don't run this code on changing workspaces, only at startup
    // Reload the file we were editing when last shut down hammer.
    Get psLastEditedFile of ghoEditorProperties to sLastEdited
    If (sLastEdited<>"") Begin
      Move -1 To iFound
      Move (Trim(sLastEdited)) To sLastEdited
      Move (SizeOfArray(saOpened)) to iFiles
      For iFile from 0 to (iFiles-1)
        Move (Trim(saOpened[iFile])) to sFile
        If (Lowercase(sFile)=Lowercase(sLastEdited)) Begin
          Move iFile To iFound
        End
      Loop
      If (iFound>-1) Begin
        Move (RemoveFromArray(saOpened,iFound)) To saOpened
        Move sLastEdited To saOpened[iFiles-1]
      End
    End
  End
End_Procedure

Procedure ReOpenAllFiles
    Boolean bExists
    String  sFile
    String[] RemoveFiles
    tTHWorkspace THWorkspace
    //
    Integer iFile iFiles
    //
    Move True to gbDisableParser // Stop the code parser while loading files
    Move (ResizeArray(RemoveFiles,0)) To RemoveFiles
    //
    Set pbCaptionUpdateState Of oMain To False
    Get pTHWorkspace of ghoApplication To THWorkspace
    Send ReOrderLastEditedFile (&THWorkspace.saOpened)
    Move (SizeOfArray(THWorkspace.saOpened)) to iFiles
    For iFile from 0 to (iFiles-1)
        Move (Trim(THWorkspace.saOpened[iFile])) to sFile
        Get vFilePathExists sFile to bExists
        // If file cannot be found, then do not try to open it again next time.
        If (bExists=False) Begin
            Move sFile To RemoveFiles[SizeOfArray(RemoveFiles)]
        End
        Else Begin
            If (iFile=(iFiles-1)) Begin
                Move False to gbDisableParser // Switch the parser on for the last file
                Set pbCaptionUpdateState of oMain to True
            End
            If (sFile<>"") ;
                Send CAOpenFile to (oClientArea(oMain(Self))) sFile
        End
    Loop
    //
    Set pbCaptionUpdateState of oMain to True
    If (pbStartingUp(oMain(Self))) Begin
      Set pbStartingUp of oMain to False // Starting up has now completed.
    End
    //
    // Remove any files not found from the list of files to open next time
    Move (SizeOfArray(RemoveFiles)) To iFiles
    If (iFiles>0) Begin
      For iFile from 0 To (iFiles-1)
        Move RemoveFiles[iFile] To sFile
        If (sFile<>"") Begin
          Send THWS_DeleteOpened sFile
        End
      Loop
    End
    Move False To gbDisableParser // Enable code parser again (just in case)
End_Procedure

// Added 17.05.01 Bernhard
// Changed 18.05.01 Bernhard
Function RequestChangeWorkSpace String sWorkSpace Returns Integer
    Function_Return (RequestChangeWorkSpace(oMain(Self),sWorkSpace))
End_Function
Procedure OnWorkSpaceChanged
    Send THDoReadWorkspace
    Send OnWorkSpaceChanged to (oMain(Self))
    Send OnWorkSpaceChanged to (oIDETagsArray(Self))
End_Procedure
Send OnWorkSpaceChanged

    // Opens a file when -OPEN is given.
If (psWrkSpcOpenFileName(ghoApplication)) Ne "" Send WrkSpcDoForAllFiles  to ghoApplication Msg_CAOpenFile (oClientArea(oMain(Self)))
 //Send CAOpenFile To (oClientArea(oMain(Self))) (psWrkSpcOpenFileName(ghoWorkSpace))

    // Creates an empty file viw on startup when -NEW is given.
If (piWrkSpcNewFile(ghoApplication))            Send CANewFile  to (oClientArea(oMain(Self)))
    // Popups the WorkSpace/ VDF Version ModalPanel when -SETVERSION is given.
If (piWrkSpcSetVersion(ghoApplication)) Begin
    Send Popup      to (oWorkspace(Self))
    Send SaveIni    to ghoEditorProperties
End

If (psVDFRegistryVersion(ghoEditorProperties)) Eq "" Begin
    Send Popup      to (oWorkSpace(Self))
    Send SaveIni    to ghoEditorProperties
End

Start_UI
