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/path_defines.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::prefixIs;
44 using support::system_lyxdir;
45 using support::user_lyxdir;
50 string const browseFile(string const & filename,
52 FileFilterList const & filters,
54 pair<string,string> const & dir1,
55 pair<string,string> const & dir2)
58 if (!filename.empty())
59 lastPath = OnlyPath(filename);
61 FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
63 FileDialog::Result result;
67 result = fileDlg.save(lastPath, filters,
68 OnlyFilename(filename));
70 result = fileDlg.open(lastPath, filters,
71 OnlyFilename(filename));
73 if (result.second.empty())
76 lastPath = OnlyPath(result.second);
78 if (result.second.find_first_of("#~$% ") == string::npos)
81 Alert::error(_("Invalid filename"),
82 _("Filename can't contain any "
83 "of these characters:\n"
84 "space, '#', '~', '$' or '%'."));
91 string const browseRelFile(string const & filename,
92 string const & refpath,
94 FileFilterList const & filters,
96 pair<string,string> const & dir1,
97 pair<string,string> const & dir2)
99 string const fname = MakeAbsPath(filename, refpath);
101 string const outname = browseFile(fname, title, filters, save,
103 string const reloutname = MakeRelPath(outname, refpath);
104 if (prefixIs(reloutname, "../"))
112 string const browseLibFile(string const & dir,
115 string const & title,
116 FileFilterList const & filters)
118 pair<string,string> const dir1(_("System files|#S#s"),
119 AddName(system_lyxdir(), dir));
121 pair<string,string> const dir2(_("User files|#U#u"),
122 AddName(user_lyxdir(), dir));
124 string const result = browseFile(LibFileSearch(dir, name, ext), title,
125 filters, false, dir1, dir2);
127 // remove the extension if it is the default one
129 if (GetExtension(result) == ext)
130 noextresult = ChangeExtension(result, string());
132 noextresult = result;
134 // remove the directory, if it is the default one
135 string const file = OnlyFilename(noextresult);
136 if (LibFileSearch(dir, file, ext) == result)
143 string const browseDir(string const & pathname,
144 string const & title,
145 pair<string,string> const & dir1,
146 pair<string,string> const & dir2)
148 string lastPath(".");
149 if (!pathname.empty())
150 lastPath = OnlyPath(pathname);
152 FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
154 FileDialog::Result result;
157 result = fileDlg.opendir(lastPath,
158 OnlyFilename(pathname));
160 if (result.second.empty())
161 return result.second;
163 lastPath = OnlyPath(result.second);
165 if (result.second.find_first_of("#~$% ") == string::npos)
168 Alert::error(_("Invalid filename"),
169 _("Filename can't contain any "
170 "of these characters:\n"
171 "space, '#', '~', '$' or '%'."));
174 return result.second;
178 vector<string> const getLatexUnits()
180 vector<string> units;
182 for (int i = 0; (str = stringFromUnit(i)); ++i)
183 units.push_back(str);
188 } // namespace frontend