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"
34 using support::addName;
35 using support::changeExtension;
36 using support::FileFilterList;
37 using support::getExtension;
38 using support::libFileSearch;
39 using support::makeAbsPath;
40 using support::makeRelPath;
41 using support::onlyFilename;
42 using support::onlyPath;
43 using support::package;
44 using support::prefixIs;
49 docstring const browseFile(docstring const & filename,
50 docstring const & title,
51 FileFilterList const & filters,
53 pair<docstring,docstring> const & dir1,
54 pair<docstring,docstring> const & dir2)
56 docstring lastPath = from_ascii(".");
57 if (!filename.empty())
58 lastPath = lyx::from_utf8(onlyPath(lyx::to_utf8(filename)));
60 FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
62 FileDialog::Result result;
65 result = fileDlg.save(lastPath, filters,
66 lyx::from_utf8(onlyFilename(lyx::to_utf8(filename))));
68 result = fileDlg.open(lastPath, filters,
69 lyx::from_utf8(onlyFilename(lyx::to_utf8(filename))));
75 docstring const browseRelFile(docstring const & filename,
76 docstring const & refpath,
77 docstring const & title,
78 FileFilterList const & filters,
80 pair<docstring,docstring> const & dir1,
81 pair<docstring,docstring> const & dir2)
83 docstring const fname = lyx::from_utf8(
84 makeAbsPath(lyx::to_utf8(filename), lyx::to_utf8(refpath)));
86 docstring const outname = browseFile(fname, title, filters, save,
88 docstring const reloutname = lyx::from_utf8(
89 makeRelPath(lyx::to_utf8(outname), lyx::to_utf8(refpath)));
90 if (prefixIs(lyx::to_utf8(reloutname), "../"))
98 docstring const browseLibFile(docstring const & dir,
99 docstring const & name,
100 docstring const & ext,
101 docstring const & title,
102 FileFilterList const & filters)
105 pair<docstring, docstring> const dir1(_("System files|#S#s"),
106 lyx::from_utf8(addName(package().system_support(), lyx::to_utf8(dir))));
108 pair<docstring, docstring> const dir2(_("User files|#U#u"),
109 lyx::from_utf8(addName(package().user_support(), lyx::to_utf8(dir))));
111 docstring const result = browseFile(lyx::from_utf8(
112 libFileSearch(to_utf8(dir), to_utf8(name), to_utf8(ext)).absFilename()),
113 title, filters, false, dir1, dir2);
115 // remove the extension if it is the default one
116 docstring noextresult;
117 if (lyx::from_utf8(getExtension(lyx::to_utf8(result))) == ext)
118 noextresult = lyx::from_utf8(changeExtension(lyx::to_utf8(result), string()));
120 noextresult = result;
122 // remove the directory, if it is the default one
123 docstring const file = lyx::from_utf8(onlyFilename(lyx::to_utf8(noextresult)));
124 if (from_utf8(libFileSearch(to_utf8(dir), to_utf8(file), to_utf8(ext)).absFilename()) == result)
131 docstring const browseDir(docstring const & pathname,
132 docstring const & title,
133 pair<docstring,docstring> const & dir1,
134 pair<docstring,docstring> const & dir2)
136 docstring lastPath = lyx::from_ascii(".");
137 if (!pathname.empty())
138 lastPath = lyx::from_utf8(onlyPath(lyx::to_utf8(pathname)));
140 FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
142 FileDialog::Result const result =
143 fileDlg.opendir(lastPath, lyx::from_utf8(onlyFilename(lyx::to_utf8(pathname))));
145 return result.second;
149 vector<docstring> const getLatexUnits()
151 vector<docstring> units;
153 char const * str = stringFromUnit(i);
154 for (; str != 0; ++i, str = stringFromUnit(i))
155 units.push_back(lyx::from_ascii(str));
160 } // namespace frontend