]> git.lyx.org Git - lyx.git/blob - src/frontends/frontend_helpers.cpp
Further cleanup of InsetFlex, InsetCollapsable and InsetLayout:
[lyx.git] / src / frontends / frontend_helpers.cpp
1 /**
2  * \file frontend_helpers.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Herbert Voß
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "frontend_helpers.h"
15
16 #include "gettext.h"
17 #include "Language.h"
18
19 #include "frontends/FileDialog.h"
20 #include "frontends/alert.h"
21
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
24 #include "support/lyxalgo.h"
25 #include "support/os.h"
26 #include "support/Package.h"
27 #include "support/Path.h"
28 #include "support/Systemcall.h"
29
30 #include <boost/cregex.hpp>
31
32 #include <algorithm>
33 #include <fstream>
34
35 using std::string;
36 using std::vector;
37 using std::endl;
38
39 namespace lyx {
40 namespace frontend {
41
42 using support::addName;
43 using support::bformat;
44 using support::FileFilterList;
45 using support::FileName;
46 using support::getExtension;
47 using support::getVectorFromString;
48 using support::libFileSearch;
49 using support::makeAbsPath;
50 using support::makeRelPath;
51 using support::onlyFilename;
52 using support::onlyPath;
53 using support::package;
54 using support::prefixIs;
55 using support::quoteName;
56 using support::removeExtension;
57 using support::Systemcall;
58 using support::token;
59
60
61 namespace {
62
63 struct Sorter
64 {
65         bool operator()(LanguagePair const & lhs, LanguagePair const & rhs) const {
66                 return lhs.first < rhs.first;
67         }
68 };
69
70
71 } // namespace anon
72
73
74 vector<LanguagePair> const getLanguageData(bool character_dlg)
75 {
76         vector<LanguagePair>::size_type const size = character_dlg ?
77                 languages.size() + 2 : languages.size();
78
79         vector<LanguagePair> langs(size);
80
81         if (character_dlg) {
82                 langs[0].first = _("No change");
83                 langs[0].second = "ignore";
84                 langs[1].first = _("Reset");
85                 langs[1].second = "reset";
86         }
87
88         vector<string>::size_type i = character_dlg ? 2 : 0;
89         for (Languages::const_iterator cit = languages.begin();
90              cit != languages.end(); ++cit) {
91                 langs[i].first  = _(cit->second.display());
92                 langs[i].second = cit->second.lang();
93                 ++i;
94         }
95
96         // Don't sort "ignore" and "reset"
97         vector<LanguagePair>::iterator begin = character_dlg ?
98                 langs.begin() + 2 : langs.begin();
99
100         std::sort(begin, langs.end(), Sorter());
101
102         return langs;
103 }
104
105
106 docstring browseFile(docstring const & filename, docstring const & title,
107         FileFilterList const & filters, bool save,
108         docstring const & label1, docstring const & dir1,
109         docstring const & label2, docstring const & dir2)
110 {
111         docstring lastPath = from_ascii(".");
112         if (!filename.empty())
113                 lastPath = from_utf8(onlyPath(to_utf8(filename)));
114
115         FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
116         dlg.setButton1(label1, dir1);
117         dlg.setButton2(label2, dir2);
118
119         FileDialog::Result result;
120
121         if (save)
122                 result = dlg.save(lastPath, filters,
123                                       from_utf8(onlyFilename(to_utf8(filename))));
124         else
125                 result = dlg.open(lastPath, filters,
126                                       from_utf8(onlyFilename(to_utf8(filename))));
127
128         return result.second;
129 }
130
131
132 docstring browseRelFile(docstring const & filename, docstring const & refpath,
133         docstring const & title, FileFilterList const & filters, bool save,
134         docstring const & label1, docstring const & dir1,
135         docstring const & label2, docstring const & dir2)
136 {
137         docstring const fname = from_utf8(makeAbsPath(
138                 to_utf8(filename), to_utf8(refpath)).absFilename());
139
140         docstring const outname = browseFile(fname, title, filters, save,
141                                           label1, dir1, label2, dir2);
142         docstring const reloutname = makeRelPath(outname, refpath);
143         if (prefixIs(reloutname, from_ascii("../")))
144                 return outname;
145         else
146                 return reloutname;
147 }
148
149
150 docstring browseLibFile(docstring const & dir, docstring const & name,
151         docstring const & ext, docstring const & title,
152         FileFilterList const & filters)
153 {
154         // FIXME UNICODE
155         docstring const label1 = _("System files|#S#s");
156         docstring const dir1 =
157                 from_utf8(addName(package().system_support().absFilename(), to_utf8(dir)));
158
159         docstring const label2 = _("User files|#U#u");
160         docstring const dir2 =
161                 from_utf8(addName(package().user_support().absFilename(), to_utf8(dir)));
162
163         docstring const result = browseFile(from_utf8(
164                 libFileSearch(to_utf8(dir), to_utf8(name), to_utf8(ext)).absFilename()),
165                 title, filters, false, dir1, dir2);
166
167         // remove the extension if it is the default one
168         docstring noextresult;
169         if (from_utf8(getExtension(to_utf8(result))) == ext)
170                 noextresult = from_utf8(removeExtension(to_utf8(result)));
171         else
172                 noextresult = result;
173
174         // remove the directory, if it is the default one
175         docstring const file = from_utf8(onlyFilename(to_utf8(noextresult)));
176         if (from_utf8(libFileSearch(to_utf8(dir), to_utf8(file), to_utf8(ext)).absFilename()) == result)
177                 return file;
178         else
179                 return noextresult;
180 }
181
182
183 docstring browseDir(docstring const & pathname, docstring const & title,
184         docstring const & label1, docstring const & dir1,
185         docstring const & label2, docstring const & dir2)
186 {
187         docstring lastPath = from_ascii(".");
188         if (!pathname.empty())
189                 lastPath = from_utf8(onlyPath(to_utf8(pathname)));
190
191         FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
192         dlg.setButton1(label1, dir1);
193         dlg.setButton2(label2, dir2);
194
195         FileDialog::Result const result =
196                 dlg.opendir(lastPath, from_utf8(onlyFilename(to_utf8(pathname))));
197
198         return result.second;
199 }
200
201
202 void rescanTexStyles()
203 {
204         // Run rescan in user lyx directory
205         support::Path p(package().user_support());
206         FileName const command = libFileSearch("scripts", "TeXFiles.py");
207         Systemcall one;
208         int const status = one.startscript(Systemcall::Wait,
209                         lyx::support::os::python() + ' ' +
210                         quoteName(command.toFilesystemEncoding()));
211         if (status == 0)
212                 return;
213         // FIXME UNICODE
214         Alert::error(_("Could not update TeX information"),
215                 bformat(_("The script `%s' failed."), from_utf8(command.absFilename())));
216 }
217
218
219 void getTexFileList(string const & filename, std::vector<string> & list)
220 {
221         list.clear();
222         FileName const file = libFileSearch("", filename);
223         if (file.empty())
224                 return;
225
226         list = getVectorFromString(file.fileContents(), "\n");
227
228         // Normalise paths like /foo//bar ==> /foo/bar
229         boost::RegEx regex("/{2,}");
230         std::vector<string>::iterator it  = list.begin();
231         std::vector<string>::iterator end = list.end();
232         for (; it != end; ++it)
233                 *it = regex.Merge((*it), "/");
234
235         // remove empty items and duplicates
236         list.erase(std::remove(list.begin(), list.end(), ""), list.end());
237         eliminate_duplicates(list);
238 }
239
240 } // namespace frontend
241 } // namespace lyx