Module File_dlg.pkg

     1//************************************************************************
     2//
     3// Confidential Trade Secret.
     4// Copyright 1987-1997 Data Access Corporation, Miami FL, USA
     5// All Rights reserved
     6// DataFlex is a registered trademark of Data Access Corporation.
     7//
     8//****************************************************************************
     9//                                          
    10// $File name  : File_dlg.pkg               
    11// $File title : Common file dialog classes 
    12// Notice      :                            
    13// $Author(s)  : Stuart Booth               
    14//                                          
    15// $Rev History                             
    16//
    17// JvH - 28 Jan 08 - Modified to support multi-file selection in the open file dialog.                                          
    18// JT 10/22/97   Added OEM/ANSI changes.    
    19// SB ??/??/??   File created               
    20//****************************************************************************
    21
    22use Windows.pkg
    23Use GlobalFunctionsProcedures.pkg
    24
    25External_Function GetOpenFileName "GetOpenFileNameA" Comdlg32.DLL Pointer OpenStruct Returns Integer
    26External_Function GetSaveFileName "GetSaveFileNameA" Comdlg32.DLL Pointer OpenStruct Returns Integer
    27
    28Struct TOpenFileName
    29    DWord    lStructSize          // length, in bytes, of the structure.       
    30    Handle   hwndOwner            // Handle to the window that owns the dialog box. 
    31    Handle   hInstance            // handle to a memory object containing a dialog box template.
    32    Pointer  lpstrFilter          // pointer to a buffer containing pairs of null-terminated filter strings.
    33    Pointer  lpstrCustomFilter    // Pointer to a static buffer that contains a pair of null-terminated filter strings for preserving the filter pattern chosen by the user.
    34    DWord    nMaxCustFilter       // size, in chars, of the buffer identified by lpstrCustomFilter. 
    35    DWord    nFilterIndex         // index of the currently selected filter
    36    Pointer  lpstrFile            // Pointer to a buffer that contains the file name(s)
    37    DWord    nMaxFile             // Size, in CHARs, of the buffer pointed to by lpstrFile.
    38    Pointer  lpstrFileTitle       // Pointer to a buffer that receives the file name and extension
    39    DWord    nMaxFileTitle        // size, in CHARs, of the buffer pointed to by lpstrFileTitle
    40    Pointer  lpstrInitialDir      // Pointer to a NULL terminated string that can specify the initial directory.
    41    Pointer  lpstrTitle           // Pointer to a string to be placed in the title bar of the dialog box.
    42    DWord    ofnFlags             // set of bit flags you can use to initialize the dialog box. 
    43    Short    nFileOffset          // Specifies the zero-based offset, in TCHARs, from the beginning of the path to the file name in the string pointed to by lpstrFile.          
    44    Short    nFileExtension       // Specifies the zero-based offset, in TCHARs, from the beginning of the path to the file name extension in the string pointed to by lpstrFile.
    45    Pointer  lpstrDefExt          // Pointer to a buffer that contains the default extension.
    46    DWord    lCustData            // Specifies application-defined data that the system passes to the hook procedure identified by the lpfnHook member.
    47    Pointer  lpfnHook             // Pointer to a hook procedure.
    48    Pointer  lpTemplateName       // Pointer to a null-terminated string that names a dialog template resource in the module identified by the hInstance member.
    49End_Struct
    50
    51#REPLACE OFN_READONLY               |CI$000001
    52#REPLACE OFN_OVERWRITEPROMPT        |CI$000002
    53#REPLACE OFN_HIDEREADONLY           |CI$000004
    54#REPLACE OFN_NOCHANGEDIR            |CI$000008
    55#REPLACE OFN_NOVALIDATE             |CI$000100
    56#REPLACE OFN_ALLOWMULTISELECT       |CI$000200
    57#REPLACE OFN_EXTENSIONDIFFERENT     |CI$000400
    58#REPLACE OFN_PATHMUSTEXIST          |CI$000800
    59#REPLACE OFN_FILEMUSTEXIST          |CI$001000
    60#REPLACE OFN_CREATEPROMPT           |CI$002000
    61#REPLACE OFN_SHAREAWARE             |CI$004000
    62#REPLACE OFN_NOREADONLYRETURN       |CI$008000
    63#REPLACE OFN_NOTESTFILECREATE       |CI$010000
    64#Replace OFN_EXPLORER               |CI$080000     // new look commdlg
    65
    66
    67
    68Class AbstractFileDialog is a cObject
    69
    70    Procedure Construct_Object
    71        Forward Send Construct_Object
    72
    73        
    74        Property string  Filter_String
    75        
    76        Property string  Initial_Folder
    77        
    78        Property string  File_Name
    79        
    80        Property string  File_Title
    81        
    82        Property String  Dialog_Caption
    83        
    84        Property Integer Filter_Index          1
    85        
    86        
    87        Property Integer HideReadOnly_State    False
    88        
    89        
    90        Property Integer TickReadOnly_State    False
    91        
    92        
    93        Property Integer AllowReadOnly_State   True
    94        
    95        
    96        Property Integer FileMustExist_State   True
    97        
    98        
    99        Property Integer PathMustExist_State   True
   100        
   101        
   102        Property Integer CreatePrompt_State    True
   103        
   104        
   105        Property Integer ShowFileTitle_State   False
   106        
   107        
   108        Property Integer OverwritePrompt_State False
   109        
   110        
   111        Property Integer NoChangeDir_State     False
   112
   113        
   114        Property Boolean Private_MultiSelect_State False
   115
   116        
   117        Property String[] Private_Selected_Files
   118
   119        
   120        Property Integer PrivateExternal_msg
   121
   122        //Set Focus_Mode To No_Activate
   123    End_Procedure
   124
   125    
   126    Function OwnerHandle returns handle
   127        Handle hWnd
   128        Handle hoObj
   129        Get Focus of desktop to hoObj // start with the focus
   130        Move (gOwnerWindowHandle(hoObj)) to hWnd // global function finds the right handle for us
   131        function_return hWnd
   132    End_Function
   133
   134
   135    Function Show_Dialog Returns Integer // bool
   136        Handle hWnd
   137        Integer bOK iFilter iFlags bHideRO bTickRO bFileExist bPathExist iMsg
   138        Integer icInitDir bCreatePrompt bOverwritePrompt bAllowReadOnly
   139        Integer iPos iCount
   140        Integer bNoChangeDir
   141        Pointer lpsFileTitle lpsFileName lpsFilter lpsInitDir
   142        Pointer lpsTitle lpsDefExt
   143        String sFileName sFileTitle sFilter sInitDir sTitle sDefExt
   144        Pointer pVoid lpOpenFileData
   145        Boolean bMultiSelect
   146        String[] sFiles
   147        TOpenFileName OpenFileData
   148
   149        //Delegate Get Container_Handle To hWnd
   150        Get OwnerHandle to hWnd
   151
   152        If (ShowFileTitle_State(Self)) Begin
   153            Get File_Title to sFileName
   154        End
   155        
   156        Get private_MultiSelect_State to bMultiSelect
   157            
   158        Get Initial_Folder        to sInitDir
   159        Get Dialog_Caption        to sTitle
   160        Get Filter_String         to sFilter
   161        Get HideReadOnly_State    to bHideRO
   162        Get TickReadOnly_State    to bTickRO
   163        Get FileMustExist_State   to bFileExist
   164        Get PathMustExist_State   to bPathExist
   165        Get CreatePrompt_State    to bCreatePrompt
   166        Get OverwritePrompt_State to bOverwritePrompt
   167        Get AllowReadOnly_State   to bAllowReadOnly
   168        Get NoChangeDir_State     to bNoChangeDir
   169        Get Filter_Index          to iFilter
   170        
   171        Move (sFilter + Character(0) + Character(0)) to sFilter
   172        Move (AddressOf(sFilter)) to lpsFilter
   173        Move (OemToAnsi(lpsFilter, lpsFilter)) to pVoid
   174        Move (Replaces('|', sFilter, Character(0))) to sFilter
   175
   176        Append sInitDir   (character(0))
   177        Append sFileTitle (Repeat(character(0),32000))
   178        Append sFileName  (Repeat(character(0),32000))
   179        Append sTitle     (Repeat(character(0),255))
   180        Append sDefExt    (character(0))
   181
   182        Move (AddressOf(sInitDir))   to lpsInitDir
   183        Move (AddressOf(sFileName))  to lpsFileName
   184        Move (AddressOf(sFileTitle)) to lpsFileTitle
   185        Move (AddressOf(sTitle))     to lpsTitle
   186        Move (AddressOf(sDefExt))    to lpsDefExt
   187        Move (AddressOf(sFilter))    to lpsFilter
   188
   189        Move (OemToAnsi(lpsInitDir, lpsInitDir)) to pVoid
   190        Move (OemToAnsi(lpsFileName, lpsFileName)) to pVoid
   191        Move (OemToAnsi(lpsTitle, lpsTitle)) to pVoid
   192
   193        If (bMultiSelect) Begin
   194            Move (OFN_EXPLORER + OFN_ALLOWMULTISELECT) to iFlags  // Multi-select!
   195        End
   196        
   197        If bFileExist           Move (iFlags + OFN_FILEMUSTEXIST)    to iFlags
   198        If bPathExist           Move (iFlags + OFN_PATHMUSTEXIST)    to iFlags
   199        If bHideRO              Move (iFlags + OFN_HIDEREADONLY)     to iFlags
   200        If bTickRO              Move (iFlags + OFN_READONLY)         to iFlags
   201        If bCreatePrompt        Move (iFlags + OFN_CREATEPROMPT)     to iFlags
   202        If bOverwritePrompt     Move (iFlags + OFN_OVERWRITEPROMPT)  to iFlags
   203        If not bAllowReadOnly   Move (iFlags + OFN_NOREADONLYRETURN) to iFlags
   204        If bNoChangeDir         Move (iFlags + OFN_NOCHANGEDIR)      to iFlags
   205
   206        Move (SizeOfType(TOpenFileName)) to OpenFileData.lStructSize
   207        Move hWnd         to OpenFileData.hWndOwner
   208        Move iFilter      to OpenFileData.nFilterIndex
   209        Move lpsFileName  to OpenFileData.lpstrFile
   210
   211        If (sInitDir <> '') Begin
   212            Move lpsInitDir    to OpenFileData.lpstrInitialDir
   213        End
   214
   215        If (sTitle <> '') Begin
   216            Move lpsTitle      to OpenFileData.lpstrTitle
   217        End
   218
   219        Move 32000             to OpenFileData.nMaxFileTitle
   220        Move 32000             to OpenFileData.nMaxFile
   221        Move lpsFileTitle      to OpenFileData.lpstrFileTitle
   222        Move iFlags            to OpenFileData.ofnFlags
   223        Move lpsFilter         to OpenFileData.lpstrFilter
   224        Move lpsDefExt         to OpenFileData.lpstrDefExt
   225
   226        Move (AddressOf(OpenFileData)) to lpOpenFileData
   227
   228        Get PrivateExternal_msg to iMsg
   229        If (iMsg = Get_GetOpenFileName) Move (GetOpenFileName(lpOpenFileData)) to bOK
   230        If (iMsg = Get_GetSaveFileName) Move (GetSaveFileName(lpOpenFileData)) to bOK
   231
   232        If bOK Begin
   233            Move OpenFileData.ofnFlags     to iFlags
   234            Move OpenFileData.nFilterIndex to iFilter
   235            Move OpenFileData.nFileOffset  to icInitDir
   236
   237            Move (CString(Left(sFileName, icInitDir))) to sInitDir    // Trim trailing null
   238            Set Initial_Folder     to sInitDir
   239
   240            Set TickReadOnly_State to (iFlags iand OFN_READONLY)
   241            Set Filter_Index       to iFilter
   242
   243            Move (AnsiToOem(lpsFileName, lpsFileName)) to pVoid
   244            Move (AnsiToOem(lpsFileTitle, lpsFileTitle)) to pVoid
   245            Set File_Name          to (CString( sFileName ))
   246            Set File_Title         to (CString( sFileTitle ))
   247
   248            If (bMultiSelect) Begin
   249                // If this is a multi-files dialog then parse out the array of selected files....
   250                If (Right(sInitDir, 1) = "\") Move (Left(sInitDir, Length(sInitDir)-1)) to sInitDir     // Trim trailing :\". This is only there if 1 file was selected!
   251            
   252                // Get the null delimited string of file names into the sFiles array....
   253                Move 0 to iCount
   254                Move (Right(sFileName, Length(sFileName) - icInitDir)) to sFileName    // Strip the path info (we already have it)
   255                
   256                Repeat
   257                    // Get the next filename....
   258                    Move (Pos(Character(0), sFileName)) to iPos   // Filenames are null delimeted
   259                    
   260                    If (iPos > 1) Begin
   261                        Move (sInitDir + "\" + CString(Left(sFileName, iPos))) to sFiles[iCount]
   262                        Move (Right(sFileName, Length(sFileName) - iPos)) to sFileName
   263                        Increment iCount
   264                    End
   265                Until (iPos <= 1)
   266            End
   267            Else Begin
   268                Move sFileName to sFiles[0]
   269            End
   270            
   271            Set private_Selected_Files to sFiles
   272        End
   273        Function_Return (bOK)
   274    End_Function
   275End_Class
   276
   277
   278Class OpenDialog is an AbstractFileDialog
   279    Procedure Construct_Object
   280        Forward Send Construct_Object
   281        Set PrivateExternal_msg To get_GetOpenFileName
   282    End_Procedure
   283
   284        
   285    
   286    
   287    Procedure Set MultiSelect_State Boolean bState
   288        Set private_MultiSelect_State to bState
   289    End_Procedure
   290    
   291    
   292    Function MultiSelect_State Returns Boolean
   293        Boolean bState
   294        Get private_MultiSelect_State to bState
   295        Function_Return bState
   296    End_Function
   297    
   298        
   299    
   300    Function Selected_Files Returns String[]
   301        String[] sFiles
   302        Get private_Selected_Files to sFiles
   303        Function_Return sFiles
   304    End_Function
   305End_Class
   306
   307
   308Class SaveAsDialog is an AbstractFileDialog
   309    Procedure Construct_Object
   310        Forward Send Construct_Object
   311        Set FileMustExist_State   To FALSE
   312        Set CreatePrompt_State    To FALSE
   313        Set ShowFileTitle_State   To TRUE
   314        Set OverwritePrompt_State To TRUE
   315        Set HideReadOnly_State    To TRUE
   316        Set PrivateExternal_msg To get_GetSaveFileName
   317    End_Procedure
   318End_Class