]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
enable Font cache only for MacOSX and inline width() for other platform.
[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 std::pair;
27 using std::vector;
28 using std::string;
29
30 namespace lyx {
31
32 using support::addName;
33 using support::changeExtension;
34 using support::FileFilterList;
35 using support::getExtension;
36 using support::libFileSearch;
37 using support::makeAbsPath;
38 using support::makeRelPath;
39 using support::onlyFilename;
40 using support::onlyPath;
41 using support::package;
42 using support::prefixIs;
43
44 namespace frontend {
45
46
47 string const browseFile(string const & filename,
48                         string const & title,
49                         FileFilterList const & filters,
50                         bool save,
51                         pair<string,string> const & dir1,
52                         pair<string,string> const & dir2)
53 {
54         string lastPath(".");
55         if (!filename.empty())
56                 lastPath = onlyPath(filename);
57
58         FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
59
60         FileDialog::Result result;
61
62         if (save)
63                 result = fileDlg.save(lastPath, filters,
64                                       onlyFilename(filename));
65         else
66                 result = fileDlg.open(lastPath, filters,
67                                       onlyFilename(filename));
68
69         return result.second;
70 }
71
72
73 string const browseRelFile(string const & filename,
74                            string const & refpath,
75                            string const & title,
76                            FileFilterList const & filters,
77                            bool save,
78                            pair<string,string> const & dir1,
79                            pair<string,string> const & dir2)
80 {
81         string const fname = makeAbsPath(filename, refpath);
82
83         string const outname = browseFile(fname, title, filters, save,
84                                           dir1, dir2);
85         string const reloutname = makeRelPath(outname, refpath);
86         if (prefixIs(reloutname, "../"))
87                 return outname;
88         else
89                 return reloutname;
90 }
91
92
93
94 string const browseLibFile(string const & dir,
95                            string const & name,
96                            string const & ext,
97                            string const & title,
98                            FileFilterList const & filters)
99 {
100         // FIXME UNICODE
101         pair<string,string> const dir1(lyx::to_utf8(_("System files|#S#s")),
102                                        addName(package().system_support(), dir));
103
104         pair<string,string> const dir2(lyx::to_utf8(_("User files|#U#u")),
105                                        addName(package().user_support(), dir));
106
107         string const result = browseFile(libFileSearch(dir, name, ext), title,
108                                          filters, false, dir1, dir2);
109
110         // remove the extension if it is the default one
111         string noextresult;
112         if (getExtension(result) == ext)
113                 noextresult = changeExtension(result, string());
114         else
115                 noextresult = result;
116
117         // remove the directory, if it is the default one
118         string const file = onlyFilename(noextresult);
119         if (libFileSearch(dir, file, ext) == result)
120                 return file;
121         else
122                 return noextresult;
123 }
124
125
126 string const browseDir(string const & pathname,
127                        string const & title,
128                        pair<string,string> const & dir1,
129                        pair<string,string> const & dir2)
130 {
131         string lastPath(".");
132         if (!pathname.empty())
133                 lastPath = onlyPath(pathname);
134
135         FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
136
137         FileDialog::Result const result =
138                 fileDlg.opendir(lastPath, onlyFilename(pathname));
139
140         return result.second;
141 }
142
143
144 vector<string> const getLatexUnits()
145 {
146         vector<string> units;
147         int i = 0;
148         char const * str = stringFromUnit(i);
149         for (; str != 0; ++i, str = stringFromUnit(i))
150                 units.push_back(str);
151
152         return units;
153 }
154
155 } // namespace frontend
156 } // namespace lyx