3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jean-Marc Lasgouttes
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
15 #include "helper_funcs.h"
18 #include "lyxlength.h"
20 #include "frontends/FileDialog.h"
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
24 #include "support/package.h"
32 using support::addName;
33 using support::changeExtension;
34 using support::FileFilterList;
35 using support::getExtension;
36 using support::libFileSearch;
37 using support::makeAbsPath;
38 using support::makeRelPath;
39 using support::onlyFilename;
40 using support::onlyPath;
41 using support::package;
42 using support::prefixIs;
47 string const browseFile(string const & filename,
49 FileFilterList const & filters,
51 pair<string,string> const & dir1,
52 pair<string,string> const & dir2)
55 if (!filename.empty())
56 lastPath = onlyPath(filename);
58 FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
60 FileDialog::Result result;
63 result = fileDlg.save(lastPath, filters,
64 onlyFilename(filename));
66 result = fileDlg.open(lastPath, filters,
67 onlyFilename(filename));
73 string const browseRelFile(string const & filename,
74 string const & refpath,
76 FileFilterList const & filters,
78 pair<string,string> const & dir1,
79 pair<string,string> const & dir2)
81 string const fname = makeAbsPath(filename, refpath);
83 string const outname = browseFile(fname, title, filters, save,
85 string const reloutname = makeRelPath(outname, refpath);
86 if (prefixIs(reloutname, "../"))
94 string const browseLibFile(string const & dir,
98 FileFilterList const & filters)
101 pair<string,string> const dir1(lyx::to_utf8(_("System files|#S#s")),
102 addName(package().system_support(), dir));
104 pair<string,string> const dir2(lyx::to_utf8(_("User files|#U#u")),
105 addName(package().user_support(), dir));
107 string const result = browseFile(libFileSearch(dir, name, ext), title,
108 filters, false, dir1, dir2);
110 // remove the extension if it is the default one
112 if (getExtension(result) == ext)
113 noextresult = changeExtension(result, string());
115 noextresult = result;
117 // remove the directory, if it is the default one
118 string const file = onlyFilename(noextresult);
119 if (libFileSearch(dir, file, ext) == result)
126 string const browseDir(string const & pathname,
127 string const & title,
128 pair<string,string> const & dir1,
129 pair<string,string> const & dir2)
131 string lastPath(".");
132 if (!pathname.empty())
133 lastPath = onlyPath(pathname);
135 FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
137 FileDialog::Result const result =
138 fileDlg.opendir(lastPath, onlyFilename(pathname));
140 return result.second;
144 vector<string> const getLatexUnits()
146 vector<string> units;
148 char const * str = stringFromUnit(i);
149 for (; str != 0; ++i, str = stringFromUnit(i))
150 units.push_back(str);
155 } // namespace frontend