]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
Enable the user to input "file names with spaces" only if LaTeX can
[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
19 #include "frontends/FileDialog.h"
20
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23 #include "support/package.h"
24
25 using std::pair;
26 using std::vector;
27 using std::string;
28
29 // sorry this is just a temporary hack we should include vspace.h! (Jug)
30 extern const char * stringFromUnit(int);
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 string const browseFile(string const & filename,
50                         string const & title,
51                         FileFilterList const & filters,
52                         bool save,
53                         pair<string,string> const & dir1,
54                         pair<string,string> const & dir2)
55 {
56         string lastPath(".");
57         if (!filename.empty())
58                 lastPath = OnlyPath(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                                       OnlyFilename(filename));
67         else
68                 result = fileDlg.open(lastPath, filters,
69                                       OnlyFilename(filename));
70
71         return result.second;
72 }
73
74
75 string const browseRelFile(string const & filename,
76                            string const & refpath,
77                            string const & title,
78                            FileFilterList const & filters,
79                            bool save,
80                            pair<string,string> const & dir1,
81                            pair<string,string> const & dir2)
82 {
83         string const fname = MakeAbsPath(filename, refpath);
84
85         string const outname = browseFile(fname, title, filters, save,
86                                           dir1, dir2);
87         string const reloutname = MakeRelPath(outname, refpath);
88         if (prefixIs(reloutname, "../"))
89                 return outname;
90         else
91                 return reloutname;
92 }
93
94
95
96 string const browseLibFile(string const & dir,
97                            string const & name,
98                            string const & ext,
99                            string const & title,
100                            FileFilterList const & filters)
101 {
102         pair<string,string> const dir1(_("System files|#S#s"),
103                                        AddName(package().system_support(), dir));
104
105         pair<string,string> const dir2(_("User files|#U#u"),
106                                        AddName(package().user_support(), dir));
107
108         string const result = browseFile(LibFileSearch(dir, name, ext), title,
109                                    filters, false, dir1, dir2);
110
111         // remove the extension if it is the default one
112         string noextresult;
113         if (GetExtension(result) == ext)
114                 noextresult = ChangeExtension(result, string());
115         else
116                 noextresult = result;
117
118         // remove the directory, if it is the default one
119         string const file = OnlyFilename(noextresult);
120         if (LibFileSearch(dir, file, ext) == result)
121                 return file;
122         else
123                 return noextresult;
124 }
125
126
127 string const browseDir(string const & pathname,
128                         string const & title,
129                         pair<string,string> const & dir1,
130                         pair<string,string> const & dir2)
131 {
132         string lastPath(".");
133         if (!pathname.empty())
134                 lastPath = OnlyPath(pathname);
135
136         FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
137
138         FileDialog::Result const result =
139                 fileDlg.opendir(lastPath, OnlyFilename(pathname));
140
141         return result.second;
142 }
143
144
145 vector<string> const getLatexUnits()
146 {
147         vector<string> units;
148         char const * str;
149         for (int i = 0; (str = stringFromUnit(i)); ++i)
150             units.push_back(str);
151
152         return units;
153 }
154
155 } // namespace frontend
156 } // namespace lyx