Use DfAllEnt.pkg
Use Std_Help.pkg
Use vWin32fh.pkg
Use cRegistry.pkg

Register_Object Client_Area
Register_Object oMain

//----------------------------------------------------------------------------------------------------->
// Global Data Replacement Utility,
//
// Version 1.3
//
// Author
//     Bob Worsley  rlworsley@gmail.com
//
// Notes
//     Replace any data in any DataFlex data file with selection
//     capabilities.
//
// Update history
//  Date     Who          Module      Change
//  07/05/02 Bob Worsley              Initial version
//  07/24/02 Bob Worsley  Global.vw   Add clear command to indirect file
//                                    at top of loop for repeating operations
//  7/30/02  Bob Worsley  Global.vw   Accomodate foreign databases
//  7/31/02  Bob Worsley  Global.src  Update for VDF 8.2
//  8/01/02  WvA          Global.src  Rearranged the code a little bit to keep it IDE compatible
//  8/05/02  Bob Worsley  Global.src  Fix for foreign databases, register_object oMain
//  5/29/04  Bob Worsley  Global.src  Change IFDEF to fmac_version
//                        Global.src  Add property to remember lastworkspace
//                        Global.vw   Fix bug in Process_expr when matching similar
//                                    field names
//                        All         Change local variable & property names to
//                                    more conventional naming
//                        GlbFieldList & GlbFileList -- add sort to show names in order
//                                                      comment sort_items line to remove
//  6/02/04  Bob Worsley  Global.Src & GlbHelp - Store Global help path so help works for all workspaces
//                        Global.vw   Seed deleting so won't restart record search for each new record
//                        Global.vw   Further changes to Process_Expr, add new function IsOperator
//  2/06/07  Bob Worsley  All         Remove IFDEFs & Studio tags for earlier versions of VDF - prior
//                                    to vdf 8.
//                                    Convert for Use in VDF 12 - Essentially the workspace code which
//                                      was lifted almost intact from the CodeMaintenance project
//                                    Change the dual field name arrays to a single DAW Multidimentional
//                                      array
//                                    Add the ability to remember the last workspace by use of a
//                                      registry entry
//                                    Fix the help (again) so that the help file can be found for all
//                                      workspaces - Another registry entry.
//                                    The application label now shows the current workspace
//                                    Fixed index "jump_in" and use of the "AND" and "OR" operators in
//                                      the selection field
//  09/01/07 Bob Worsley  Global.src & .vw - replaced registry VDF version number with SYSCONF call so
//                                    that it doesn't have to be changed for each new revision
//  09/08/08 Bob Worsley  All         Converted to VDF 14, both old and new screen sizes are still in
//                                    the code, VDF 12 and lower are commented out.  Search on "14.0"
//  08/27/09 Bob Worsley  Global.Vw   Replace all recnum references so Global will work with "Standard"
//                                    tables without recnums
//                                    Remove all unused local variables
//                                    Fix 2 bugs in index "jumpIn" and "jumpOut" functionality
//  9/20/09 Bob Worsley   Global.Vw   Further fix for Standard tables - eliminate "Recnum" from field list
//  9/20/09 Seanyboy      Global.Vw   Fix contributed by sean@theguru.co.uk to overcome problem of 
//                                    comparing similar file names - that might differ by 1 character
//
//----------------------------------------------------------------------------------------------------->

Use DfAClnt.Pkg
Use File_dlg.Pkg

Use GlobalFunctions\f_WorkspacePathFromSWS.pkg
Use GlobalFunctions\f_DefaultWorkspacePath.pkg
Use GlobalFunctions\f_CurrentUserWorkspace.pkg

Use cApplication.pkg

//  Set date attributes as needed
Set_Date_Attribute sysdate4_State to (TRUE)
Set_Date_Attribute Date4_State    to (TRUE)
Set_Date_Attribute epoch_value    to 30

Object oRegistry is a cRegistry
End_Object

Object oApplication is a cApplication

    { DesignTime = False }
    Property String psWorkspaceSWSFileName
    Property String psViewLabel "Global"
    Set psAutoOpenWorkspace to "Global.ws"

    Procedure OnCreate
        String sWorkspaceFileName sRegKey sRegValue sWorkSpaceName sExt sHelpDir
        Boolean bOk bOpened
        Integer iError

        Forward Send OnCreate // Loads the commandline options

        //-------------------------------------------------------------------------------->
        // See if there's a previously selected workspace in the registry.  If no
        // registry entry, create it
        //
        // The default is Hkey_Local_User which is what we want
        // Open the Key or create it if not there
        //--------------------------------------------------------------------------------->
        Move ("Software\Data Access Worldwide\Visual DataFlex\" + Sysconf (SYSCONF_DATAFLEX_REV)) to sRegKey
        Move "CurrGlobalWorkspace" to sRegValue

        Get OpenKey of oRegistry sRegKey to bOpened
        If bOpened Begin
            If (ValueExists(oRegistry(Self), sRegValue));
                Get ReadString of oRegistry sRegValue to sWorkspaceFileName
            Else Begin
                Get CreateKey of oRegistry sRegKey to iError
                If (iError =0) Send WriteString of oRegistry sRegValue ""
            End
            Send CloseKey to oRegistry
        End

        If (sWorkspaceFileName <> "") Begin
            Get OpenWorkspace sWorkspaceFileName to bOk
            Get ParseFileName sWorkspaceFileName to sWorkspaceName
            Get ParseFileExtension sWorkspaceFileName to sExt
            Move (Replace(("."+sExt), sWorkSpaceName, "")) to sWorkSpaceName
            Set psViewLabel to sWorkSpaceName
        End

        If (not (bOk)) Begin
            // Then try To open the default workspace (if it exists)
            Move (CurrentUserWorkspace ()) to sWorkspaceFileName
            If (sWorkspaceFileName <> "") Begin

                //-------------------------------------------------------------------------------->
                // Store the original Global workspace to use for finding the help file.  Problem
                // is when switching workspaces, how do we refind the Global help?  We stash the
                // location in the registry the first time the user starts up Global when we
                // create that registry entry.
                //-------------------------------------------------------------------------------->
                Move ("Software\Data Access Worldwide\Visual DataFlex\" + Sysconf (SYSCONF_DATAFLEX_REV)) to sRegKey
                Move "GlobalHelpDirectory" to sRegValue

                Get OpenKey of oRegistry sRegKey to bOpened
                If bOpened Begin
                    If (ValueExists(oRegistry(Self), sRegValue));
                        Get ReadString of oRegistry sRegValue to sHelpDir
                    Else Begin
                        Get CreateKey of oRegistry sRegKey to iError
                        // Parse off the path
                        Get ParseFolderName sWorkspaceFileName to sHelpDir
                        Get vFolderFormat sHelpDir to sHelpDir
                        If (iError =0) Send WriteString of oRegistry sRegValue (sHelpDir+"Help\Global.chm")
                    End
                    Send CloseKey to oRegistry
                End


                Get OpenWorkspace sWorkspaceFileName to bOk
            End

            If (not (bOk)) Begin
                // If not ok, either we could not open or the default did not exist.
                // Now we ask the user To select a workspace
                Send ChangeWorkspace // this can exit application
            End
        End

    End_Procedure // OnCreate

    Function OpenWorkspace String sWorkspaceName Returns Boolean
        String sWorkspaceExtension sError sMessage
        Handle hoWorkspace
        Integer eOpened iResult
        Boolean bGoodWorkspace

        Get phoWorkspace to hoWorkspace
        If (hoWorkspace > 0) Begin
            Repeat
                Set psWorkspaceSWSFileName to ""
                If (IsFileNameQualified (sWorkspaceName)) Begin
                    Move (Uppercase (Right (sWorkspaceName, 4))) to sWorkspaceExtension
                    If (sWorkspaceExtension = ".SWS") Begin
                        Set psWorkspaceSWSFileName to sWorkspaceName
                        Move (WorkspacePathFromSWS (sWorkspaceName)) to sWorkspaceName
                    End
                    If (IsFileNameQualified (sWorkspaceName)) Begin
                        Send DoClearPaths of hoWorkspace
                        Get OpenWorkspaceFile of hoWorkspace sWorkspaceName to eOpened
                        Move (eOpened = wsWorkspaceOpened) to bGoodWorkspace
                        If (not (bGoodWorkspace)) Begin
                            Get OpenWorkspaceErrorMessage of hoWorkspace eOpened to sError
                            Move ("The following error has occured while initializing the '" + sWorkspaceName) to sMessage
                            Move (sMessage + "' Workspace:\n\n" + sError) to sMessage
                            Move (sMessage + "\n\nDo you want To load a different Workspace?\n\nClick YES, To load a different Workspace\nClick NO To exit the CodeMaintenance Utility") to sMessage
                            Move (YesNo_Box (sMessage, "Workspace Error" , MB_DEFBUTTON1)) to iResult
                            If (iResult = MBR_YES) Begin
                                Get SelectWorkspace to sWorkSpaceName
                                If (sWorkSpaceName = "") Begin
                                    Move MBR_NO to iResult
                                End
                            End
                            Else Begin
                                Abort
                            End
                        End
                    End
                End
            Until (bGoodWorkspace or iResult = MBR_NO)
        End

        Function_Return bGoodWorkspace
    End_Function // OpenWorkspace

    Function SelectWorkspace Returns String
        Handle hoDialog
        String sPath sWorkspaceFileName sWorkSpaceName sExt sRegKey sRegValue
        Boolean bSelected bOpened

        Move (DefaultWorkspacePath ()) to sPath

        Get Create U_OpenDialog to hoDialog
        If (hoDialog <> 0) Begin
            Set Dialog_Caption of hoDialog to "Open a workspace"
            Set Filter_String of hoDialog to "Workspace|*.ws;*.sws"
            Set FileMustExist_State of hoDialog to True
            Set PathMustExist_State of hoDialog to True
            Set Hidereadonly_State of hoDialog to True
            Set CreatePrompt_State of hoDialog to False
            Set NoChangeDir_State of hoDialog to True
            If (sPath <> "") Begin
			    Set Initial_Folder of hoDialog to sPath
            End

            Get Show_Dialog of hoDialog to bSelected
            If (bSelected) Begin
                Get File_Name of hoDialog to sWorkspaceFileName
                If (Right (Uppercase (sWorkspaceFileName), 4) = ".SWS") Begin
                    Move (WorkspacePathFromSWS (sWorkspaceFileName)) to sWorkspaceFileName
                    Get ParseFileName sWorkspaceFileName to sWorkspaceName
                    Get ParseFileExtension sWorkspaceFileName to sExt
                    Move (Replace(("."+sExt), sWorkSpaceName, "")) to sWorkSpaceName
                End
            End
            Send Destroy of hoDialog
        End

        Set Label of oMain to sWorkSpaceName
        Send Request_Clear to (oReplace_View(Client_Area(oMain(Self))))

        //-------------------------------------------------------------------------------->
        // Store the selected workspace if such
        //-------------------------------------------------------------------------------->
        Move ("Software\Data Access Worldwide\Visual DataFlex\" + Sysconf (SYSCONF_DATAFLEX_REV)) to sRegKey
        Move "CurrGlobalWorkspace" to sRegValue

        Get OpenKey of oRegistry sRegKey to bOpened
        If bOpened Begin
            If (ValueExists(oRegistry(Self), sRegValue));
                Send WriteString of oRegistry sRegValue sWorkSpaceFileName
            Send CloseKey to oRegistry
        End

        Function_Return sWorkspaceFileName
    End_Function // SelectWorkspace

    // change workspace. Either succeed or end the application
    Procedure ChangeWorkspace
        Boolean bGoodWorkspace 
        String sWorkspace

        Repeat
            Get SelectWorkspace to sWorkspace
            If (sWorkspace <> "") Begin
                Get OpenWorkspace sWorkspace to bGoodWorkspace
            End
        Until (bGoodWorkspace or sWorkspace = "")

    End_Procedure // ChangeWorkspace


End_Object  //oApplication

Register_Object oReplace_View
Register_Object FileLookUp
Register_Object FieldLookUp
Register_Object oFile_Numb
Register_Object index_fields
Register_Object index_list
Register_Object index_Values


Object oMain is a Panel

  Set View_Mode to ViewMode_Normal

  Set Label to "Global Data Replacement"
  Use GlBMenu.MN //  Menu package.

  Use DFStdBtn.pkg //  Tool-Bar object.

  Object Client_Area is a AppClientArea
     //  Include all views
     Use Global.VW

  End_Object  // Client_Area

  Use DFStdSbr.pkg //  Status-Bar object.

  //----------------------------------------------------->
  // save original application title
  //----------------------------------------------------->
  Procedure Activating
      String sLabel sWorkspace 

      forward send Activating

      get Label to sLabel

      Get psDescription of (phoWorkspace(oApplication(self))) to sWorkSpace

      Set Label of oMain to (psViewLabel(oApplication(Self)))
  End_Procedure  // Activating

  Procedure OpenView
      send Activate_oReplace_View of (Client_Area(Current_Object))
  End_Procedure  // OpenView

  Use StdAbout.pkg // Standard "About" package.

End_Object

Send OpenView to oMain

Start_UI