Re: Specify path when opening file?
- Posted by Tone Škoda <tskoda at email.si> Aug 21, 2005
- 478 views
Tony Steward wrote: > > When using win32lib command getOpenFileName how can I set the default > path. Setting the current directory has no effect as it automatically > goes to the last directory used when this dialog was used. Hi, If using win32lib v0.60.5, you have to modify w32file.ew. Replace these 3 routines:
----------------------------------------------------------------------------- -- COMMON DIALOGS: SAVE FILE NAME -- ----------------------------------------------------------------------------- integer vFileNameBufferSize vFileNameBufferSize = 8000 --/topic Dialogs --/func buildDefaultOfn( integer window, sequence file, sequence filters, atom flags ) --/desc Creates a OpenFile structure used by the standard dialog. --/ret The address of the structure. --Note, you must call w32release_mem() when you have finished with the structure. -- --/code -- atom lOfn -- lOfn = buildDefaultOfn(0, "newfile.txt", {"Text Files","*.txt"}, -- OFN_FILEMUSTEXIST) -- w32store(lOfn, OfnDefExt, "TXT") -- if w32Func(xGetOpenFileName, {lOfn}) then -- -- get the name -- fName = w32fetch( lOfn, ofnFile ) -- fNamePtr = w32fetch( lOfn, ofnFileOffset) -- fExtPtr = w32fetch( lOfn, ofnFileExtension) -- else -- fName = "" -- end if -- w32release_mem(lOfn) --/endcode global function buildDefaultOfn( integer id, sequence fName, sequence filters, atom exflags, --// start tone skoda object intial_dir --// end tone skoda ) -- builds the ofn structure shared by open and save file -- filters is in the form: -- { "Text Files", "*.TXT,*.DOC", ... } atom ofn, flags, filenamebuffer sequence filterList integer indx -- set up default flags. flags = w32or_all( { exflags, -- from caller OFN_EXPLORER, -- looks like windows explorer OFN_LONGNAMES -- use long filenames } ) -- path must exist -- Check to see if a directory name was supplied. if length(fName) > 0 and fName[length(fName)] = '\\' then fName &= "*.*" end if -- Allocate the structure ofn = w32acquire_mem(0, SIZEOF_OPENFILENAME + length(fName) + 1 + vFileNameBufferSize ) -- Allocate the buffer to hold the returned values. filenamebuffer = ofn + SIZEOF_OPENFILENAME poke(filenamebuffer, fName) poke(filenamebuffer+length(fName), 0) -- build the filter list by concatenating the elements -- and ending them with zeros filterList = {} indx = 1 while indx <= length( filters ) do if equal(upper(filters[indx]), "DIALOG FLAGS") then indx += 1 if indx <= length(filters) then if sequence(filters[indx]) then flags = w32or_all(filters[indx] & flags) else flags = or_bits(filters[indx], flags) end if end if else filterList &= filters[indx] & NULL end if indx += 1 end while -- ends with nulls filterList &= NULL & NULL -- size of structure, in bytes w32store( ofn, ofnStructSize, SIZEOF_OPENFILENAME ) -- window that owns the dialog box w32store( ofn, ofnOwner, call_func(r_getHandle,{ w32iff(id=0, call_func(r_getMainWindow,{}),id) })) -- identifies the data block containing a dialog box template -- specified in ofnTemplateName. not used w32store( ofn, ofnInstance, NULL ) -- filters w32store( ofn, ofnFilter, filterList ) w32store( ofn, ofnCustomFilter, NULL ) w32store( ofn, ofnMaxCustFilter, NULL ) -- index of which filter to default to w32store( ofn, ofnFilterIndex, 1 ) w32store( ofn, ofnFile, filenamebuffer ) w32store( ofn, ofnMaxFile, vFileNameBufferSize ) w32store( ofn, ofnFileTitle, NULL ) w32store( ofn, ofnMaxFileTitle, NULL ) --// Start Tone Skoda, 20. september 2004. --// w32store( ofn, ofnInitialDir, NULL ) w32store( ofn, ofnInitialDir, intial_dir ) --// End Tone Skoda w32store( ofn, ofnTitle, NULL ) -- NEW! 0.43 added 'flags' w32store( ofn, ofnFlags, flags) w32store( ofn, ofnFileOffset, NULL ) w32store( ofn, ofnFileExtension, NULL ) -- default extension w32store( ofn, ofnDefExt, NULL ) -- custom data w32store( ofn, ofnCustData, NULL ) w32store( ofn, ofnHook, NULL ) w32store( ofn, ofnTemplateName, NULL ) return ofn end function ----------------------------------------------------------------------------- --/topic Dialogs --/func getOpenFileName( window, file, filters ) --/desc "Open File" dialog. --/ret Selected file name, or empty sequence if cancelled. -- Calling this function brings up the modal "Open File" dialog, allowing -- the user to select a file name. /i file is a sequence holding the default -- file name. /i filters is a list of patterns to limit displayed files to, -- in the format: -- --/code -- { "text", pattern, "text", pattern ... } --/endcode -- -- For example: --/code -- constant FileTypes = { -- "Text File", "*.TXT", -- "Euphoria Program", "*.EX;*.EXW;*.E;*.EW", -- "All Files", "*.*" } --/endcode -- -- Note that a pattern can contain several different values. -- -- Example: -- --/code -- -- get file name to open -- sequence filename -- -- filename = getOpenFileName( -- TheWindow, -- parent window -- "", -- no default name -- { "Text File", "*.TXT", -- text files -- "All Files", "*.*" } ) -- everything else --/endcode -- -- It is possible to modify the default flags set for the dialog by -- adding a special 'pattern' of "DIALOG FLAGS" followed by the -- additional flags required. The usual use of this is to allow -- multiple files to be selected. -- -- /b "Multiple Selections" /n -- When doing this, the routine returns a sequence of sequences. The first element -- is the directory name, which always ends with a '\', and each subsequent -- element is a file name selected from that directory. -- --/code -- filename = getOpenFileName( -- TheWindow, -- parent window -- "", -- no default name -- { "Dialog Flags", {OFN_ALLOWMULTISELECT}, -- "Text File", "*.TXT", -- text files -- "All Files", "*.*" } ) -- everything else -- -- if length(filename) > 0 then -- theDir = filename[1] -- for i = 2 to length(filename) do -- ProcessTheFile( theDir, filename[i]) -- end for -- end if --/endcode atom vOpenedFlags vOpenedFlags = 0 global function getOpenFileName( integer id, sequence fName, sequence filters, --// start tone skoda object intial_dir --// end tone skoda ) atom ofn, flags, fadr sequence text integer lPosn lPosn = find("DIALOG FLAGS", upper(filters)) if lPosn = 0 or lPosn = length(filters) then -- Default setting flags = OFN_FILEMUSTEXIST else flags = 0 end if -- build the structure ofn = buildDefaultOfn( id, fName, filters, flags, intial_dir --// tone skoda ) -- call the routine flags = w32fetch( ofn, ofnFlags) if w32Func(xGetOpenFileName, {ofn}) then -- get the name vOpenedFlags = w32fetch( ofn, ofnFlags) if and_bits(vOpenedFlags, OFN_ALLOWMULTISELECT) then -- Posible multiple files selected -- -- Get address of first string fadr = peek4u(ofn +ofnFile[1]) fName = {} -- Get a file string text = w32peek_string(fadr) -- Repeat until no more left while length(text) != 0 do fName = append(fName,text) -- Point to next string fadr += length(text) + 1 -- Get it. text = w32peek_string(fadr) end while -- Check for one a single selection. if length(fName) = 1 then fName = fName[1] -- Otherwise ensure that the 1st string (the Directory) -- has a trailing '\' character. elsif fName[1][length(fName[1])] != '\\' then fName[1] &= '\\' end if else fName = w32fetch( ofn, ofnFile ) end if else -- return blank fName = "" end if -- release the structure and strings w32release_mem( ofn ) return fName end function ----------------------------------------------------------------------------- --/topic Dialogs --/func getSaveFileName( window, file, filters ) --/desc "Save File" dialog. --/ret Selected file name, or empty sequence if cancelled. -- Calling this function brings up the modal "Save File" dialog, allowing -- the user to select a file name. /i file is a sequence holding the default -- file name. /i filters is a list of patterns to limit displayed files to, -- in the format: -- --/code -- { "text", pattern, "text", pattern ... } --/endcode -- -- For example: --/code -- constant FileTypes = { -- "Text File", "*.TXT", -- "Euphoria Program", "*.EX;*.EXW;*.E;*.EW", -- "All Files", "*.*" } --/endcode -- -- Note that a pattern can contain several different values. -- -- Example: -- --/code -- -- get file name to save -- filename = getSaveFileName( -- TheWindow, -- parent window -- "MyFile.txt", -- default name -- { "Text File", "*.TXT", -- text files -- "All Files", "*.*" } ) -- everything else --/endcode global function getSaveFileName( integer id, sequence fName, sequence filters, --// start tone skoda object intial_dir --// end tone skoda ) atom ofn, flags integer lPosn -- build the structure lPosn = find("DIALOG FLAGS", upper(filters)) if lPosn = 0 or lPosn = length(filters) then -- Default setting flags = w32or_all({OFN_OVERWRITEPROMPT, OFN_HIDEREADONLY, OFN_PATHMUSTEXIST}) else flags = 0 end if ofn = buildDefaultOfn( id, fName, filters, flags, intial_dir --// tone skoda ) -- warn if exists -- call the routine if w32Func(xGetSaveFileName, {ofn}) then -- get the name fName = w32fetch( ofn, ofnFile ) else -- cancelled fName = "" end if -- release the structure and strings w32release_mem( ofn ) -- return result return fName end function