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"
19 #include "frontends/Alert.h"
20 #include "frontends/FileDialog.h"
22 #include "support/filetools.h"
23 #include "support/package.h"
29 // sorry this is just a temporary hack we should include vspace.h! (Jug)
30 extern const char * stringFromUnit(int);
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 string const browseFile(string const & filename,
51 FileFilterList const & filters,
53 pair<string,string> const & dir1,
54 pair<string,string> const & dir2)
57 if (!filename.empty())
58 lastPath = OnlyPath(filename);
60 FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
62 FileDialog::Result result;
66 result = fileDlg.save(lastPath, filters,
67 OnlyFilename(filename));
69 result = fileDlg.open(lastPath, filters,
70 OnlyFilename(filename));
72 if (result.second.empty())
75 lastPath = OnlyPath(result.second);
77 if (result.second.find_first_of("#~$% ") == string::npos)
80 Alert::error(_("Invalid filename"),
81 _("Filename can't contain any "
82 "of these characters:\n"
83 "space, '#', '~', '$' or '%'."));
90 string const browseRelFile(string const & filename,
91 string const & refpath,
93 FileFilterList const & filters,
95 pair<string,string> const & dir1,
96 pair<string,string> const & dir2)
98 string const fname = MakeAbsPath(filename, refpath);
100 string const outname = browseFile(fname, title, filters, save,
102 string const reloutname = MakeRelPath(outname, refpath);
103 if (prefixIs(reloutname, "../"))
111 string const browseLibFile(string const & dir,
114 string const & title,
115 FileFilterList const & filters)
117 pair<string,string> const dir1(_("System files|#S#s"),
118 AddName(package().system_support(), dir));
120 pair<string,string> const dir2(_("User files|#U#u"),
121 AddName(package().user_support(), dir));
123 string const result = browseFile(LibFileSearch(dir, name, ext), title,
124 filters, false, dir1, dir2);
126 // remove the extension if it is the default one
128 if (GetExtension(result) == ext)
129 noextresult = ChangeExtension(result, string());
131 noextresult = result;
133 // remove the directory, if it is the default one
134 string const file = OnlyFilename(noextresult);
135 if (LibFileSearch(dir, file, ext) == result)
142 string const browseDir(string const & pathname,
143 string const & title,
144 pair<string,string> const & dir1,
145 pair<string,string> const & dir2)
147 string lastPath(".");
148 if (!pathname.empty())
149 lastPath = OnlyPath(pathname);
151 FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
153 FileDialog::Result result;
156 result = fileDlg.opendir(lastPath,
157 OnlyFilename(pathname));
159 if (result.second.empty())
160 return result.second;
162 lastPath = OnlyPath(result.second);
164 if (result.second.find_first_of("#~$% ") == string::npos)
167 Alert::error(_("Invalid filename"),
168 _("Filename can't contain any "
169 "of these characters:\n"
170 "space, '#', '~', '$' or '%'."));
173 return result.second;
177 vector<string> const getLatexUnits()
179 vector<string> units;
181 for (int i = 0; (str = stringFromUnit(i)); ++i)
182 units.push_back(str);
187 } // namespace frontend