]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
hopefully fix tex2lyx linking.
[lyx.git] / src / frontends / controllers / helper_funcs.C
1 /**
2  * \file helper_funcs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12
13 #include <config.h>
14
15 #include "helper_funcs.h"
16
17 #include "gettext.h"
18 #include "lyxlength.h"
19
20 #include "frontends/FileDialog.h"
21
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
24 #include "support/package.h"
25
26 using lyx::docstring;
27
28 using std::pair;
29 using std::string;
30 using std::vector;
31
32 namespace lyx {
33
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;
45
46 namespace frontend {
47
48
49 docstring const browseFile(docstring const & filename,
50                         docstring const & title,
51                         FileFilterList const & filters,
52                         bool save,
53                         pair<docstring,docstring> const & dir1,
54                         pair<docstring,docstring> const & dir2)
55 {
56         docstring lastPath = from_ascii(".");
57         if (!filename.empty())
58                 lastPath = lyx::from_utf8(onlyPath(lyx::to_utf8(filename)));
59
60         FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
61
62         FileDialog::Result result;
63
64         if (save)
65                 result = fileDlg.save(lastPath, filters,
66                                       lyx::from_utf8(onlyFilename(lyx::to_utf8(filename))));
67         else
68                 result = fileDlg.open(lastPath, filters,
69                                       lyx::from_utf8(onlyFilename(lyx::to_utf8(filename))));
70
71         return result.second;
72 }
73
74
75 docstring const browseRelFile(docstring const & filename,
76                            docstring const & refpath,
77                            docstring const & title,
78                            FileFilterList const & filters,
79                            bool save,
80                            pair<docstring,docstring> const & dir1,
81                            pair<docstring,docstring> const & dir2)
82 {
83         docstring const fname = lyx::from_utf8(
84                 makeAbsPath(lyx::to_utf8(filename), lyx::to_utf8(refpath)));
85
86         docstring const outname = browseFile(fname, title, filters, save,
87                                           dir1, dir2);
88         docstring const reloutname = lyx::from_utf8(
89                 makeRelPath(lyx::to_utf8(outname), lyx::to_utf8(refpath)));
90         if (prefixIs(lyx::to_utf8(reloutname), "../"))
91                 return outname;
92         else
93                 return reloutname;
94 }
95
96
97
98 docstring const browseLibFile(docstring const & dir,
99                            docstring const & name,
100                            docstring const & ext,
101                            docstring const & title,
102                            FileFilterList const & filters)
103 {
104         // FIXME UNICODE
105         pair<docstring, docstring> const dir1(_("System files|#S#s"),
106                                        lyx::from_utf8(addName(package().system_support(), lyx::to_utf8(dir))));
107
108         pair<docstring, docstring> const dir2(_("User files|#U#u"),
109                                        lyx::from_utf8(addName(package().user_support(), lyx::to_utf8(dir))));
110
111         docstring const result = browseFile(lyx::from_utf8(
112                 libFileSearch(lyx::to_utf8(dir), lyx::to_utf8(name), lyx::to_utf8(ext))),
113                 title, filters, false, dir1, dir2);
114
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()));
119         else
120                 noextresult = result;
121
122         // remove the directory, if it is the default one
123         docstring const file = lyx::from_utf8(onlyFilename(lyx::to_utf8(noextresult)));
124         if (lyx::from_utf8(libFileSearch(lyx::to_utf8(dir), lyx::to_utf8(file), lyx::to_utf8(ext))) == result)
125                 return file;
126         else
127                 return noextresult;
128 }
129
130
131 docstring const browseDir(docstring const & pathname,
132                        docstring const & title,
133                        pair<docstring,docstring> const & dir1,
134                        pair<docstring,docstring> const & dir2)
135 {
136         docstring lastPath = lyx::from_ascii(".");
137         if (!pathname.empty())
138                 lastPath = lyx::from_utf8(onlyPath(lyx::to_utf8(pathname)));
139
140         FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
141
142         FileDialog::Result const result =
143                 fileDlg.opendir(lastPath, lyx::from_utf8(onlyFilename(lyx::to_utf8(pathname))));
144
145         return result.second;
146 }
147
148
149 vector<docstring> const getLatexUnits()
150 {
151         vector<docstring> units;
152         int i = 0;
153         char const * str = stringFromUnit(i);
154         for (; str != 0; ++i, str = stringFromUnit(i))
155                 units.push_back(lyx::from_ascii(str));
156
157         return units;
158 }
159
160 } // namespace frontend
161 } // namespace lyx