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"
24 #include "support/globbing.h"
30 // sorry this is just a temporary hack we should include vspace.h! (Jug)
31 extern const char * stringFromUnit(int);
35 using support::AddName;
36 using support::ChangeExtension;
37 using support::FileFilterList;
38 using support::GetExtension;
39 using support::LibFileSearch;
40 using support::MakeAbsPath;
41 using support::MakeRelPath;
42 using support::OnlyFilename;
43 using support::OnlyPath;
44 using support::prefixIs;
45 using support::system_lyxdir;
46 using support::user_lyxdir;
51 string const browseFile(string const & filename,
53 FileFilterList const & filters,
55 pair<string,string> const & dir1,
56 pair<string,string> const & dir2)
59 if (!filename.empty())
60 lastPath = OnlyPath(filename);
62 FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
64 FileDialog::Result result;
68 result = fileDlg.save(lastPath, filters,
69 OnlyFilename(filename));
71 result = fileDlg.open(lastPath, filters,
72 OnlyFilename(filename));
74 if (result.second.empty())
77 lastPath = OnlyPath(result.second);
79 if (result.second.find_first_of("#~$% ") == string::npos)
82 Alert::error(_("Invalid filename"),
83 _("Filename can't contain any "
84 "of these characters:\n"
85 "space, '#', '~', '$' or '%'."));
92 string const browseRelFile(string const & filename,
93 string const & refpath,
95 FileFilterList const & filters,
97 pair<string,string> const & dir1,
98 pair<string,string> const & dir2)
100 string const fname = MakeAbsPath(filename, refpath);
102 string const outname = browseFile(fname, title, filters, save,
104 string const reloutname = MakeRelPath(outname, refpath);
105 if (prefixIs(reloutname, "../"))
113 string const browseLibFile(string const & dir,
116 string const & title,
117 FileFilterList const & filters)
119 pair<string,string> const dir1(_("System files|#S#s"),
120 AddName(system_lyxdir(), dir));
122 pair<string,string> const dir2(_("User files|#U#u"),
123 AddName(user_lyxdir(), dir));
125 string const result = browseFile(LibFileSearch(dir, name, ext), title,
126 filters, false, dir1, dir2);
128 // remove the extension if it is the default one
130 if (GetExtension(result) == ext)
131 noextresult = ChangeExtension(result, string());
133 noextresult = result;
135 // remove the directory, if it is the default one
136 string const file = OnlyFilename(noextresult);
137 if (LibFileSearch(dir, file, ext) == result)
144 string const browseDir(string const & pathname,
145 string const & title,
146 pair<string,string> const & dir1,
147 pair<string,string> const & dir2)
149 string lastPath(".");
150 if (!pathname.empty())
151 lastPath = OnlyPath(pathname);
153 FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
155 FileDialog::Result result;
158 result = fileDlg.opendir(lastPath,
159 OnlyFilename(pathname));
161 if (result.second.empty())
162 return result.second;
164 lastPath = OnlyPath(result.second);
166 if (result.second.find_first_of("#~$% ") == string::npos)
169 Alert::error(_("Invalid filename"),
170 _("Filename can't contain any "
171 "of these characters:\n"
172 "space, '#', '~', '$' or '%'."));
175 return result.second;
179 vector<string> const getLatexUnits()
181 vector<string> units;
183 for (int i = 0; (str = stringFromUnit(i)); ++i)
184 units.push_back(str);
189 } // namespace frontend