]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
some tabular fixes for the problems reported by Helge
[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/Alert.h"
20 #include "frontends/FileDialog.h"
21
22 #include "support/filetools.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         while (true) {
65                 if (save)
66                         result = fileDlg.save(lastPath, filters,
67                                 OnlyFilename(filename));
68                 else
69                         result = fileDlg.open(lastPath, filters,
70                                 OnlyFilename(filename));
71
72                 if (result.second.empty())
73                         return result.second;
74
75                 lastPath = OnlyPath(result.second);
76
77                 if (result.second.find_first_of("#~$% ") == string::npos)
78                         break;
79
80                 Alert::error(_("Invalid filename"),
81                         _("Filename can't contain any "
82                         "of these characters:\n"
83                         "space, '#', '~', '$' or '%'."));
84         }
85
86         return result.second;
87 }
88
89
90 string const browseRelFile(string const & filename,
91                            string const & refpath,
92                            string const & title,
93                            FileFilterList const & filters,
94                            bool save,
95                            pair<string,string> const & dir1,
96                            pair<string,string> const & dir2)
97 {
98         string const fname = MakeAbsPath(filename, refpath);
99
100         string const outname = browseFile(fname, title, filters, save,
101                                           dir1, dir2);
102         string const reloutname = MakeRelPath(outname, refpath);
103         if (prefixIs(reloutname, "../"))
104                 return outname;
105         else
106                 return reloutname;
107 }
108
109
110
111 string const browseLibFile(string const & dir,
112                            string const & name,
113                            string const & ext,
114                            string const & title,
115                            FileFilterList const & filters)
116 {
117         pair<string,string> const dir1(_("System files|#S#s"),
118                                        AddName(package().system_support(), dir));
119
120         pair<string,string> const dir2(_("User files|#U#u"),
121                                        AddName(package().user_support(), dir));
122
123         string const result = browseFile(LibFileSearch(dir, name, ext), title,
124                                    filters, false, dir1, dir2);
125
126         // remove the extension if it is the default one
127         string noextresult;
128         if (GetExtension(result) == ext)
129                 noextresult = ChangeExtension(result, string());
130         else
131                 noextresult = result;
132
133         // remove the directory, if it is the default one
134         string const file = OnlyFilename(noextresult);
135         if (LibFileSearch(dir, file, ext) == result)
136                 return file;
137         else
138                 return noextresult;
139 }
140
141
142 string const browseDir(string const & pathname,
143                         string const & title,
144                         pair<string,string> const & dir1,
145                         pair<string,string> const & dir2)
146 {
147         string lastPath(".");
148         if (!pathname.empty())
149                 lastPath = OnlyPath(pathname);
150
151         FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
152
153         FileDialog::Result result;
154
155         while (true) {
156                 result = fileDlg.opendir(lastPath,
157                                 OnlyFilename(pathname));
158
159                 if (result.second.empty())
160                         return result.second;
161
162                 lastPath = OnlyPath(result.second);
163
164                 if (result.second.find_first_of("#~$% ") == string::npos)
165                         break;
166
167                 Alert::error(_("Invalid filename"),
168                         _("Filename can't contain any "
169                         "of these characters:\n"
170                         "space, '#', '~', '$' or '%'."));
171         }
172
173         return result.second;
174 }
175
176
177 vector<string> const getLatexUnits()
178 {
179         vector<string> units;
180         char const * str;
181         for (int i = 0; (str = stringFromUnit(i)); ++i)
182             units.push_back(str);
183
184         return units;
185 }
186
187 } // namespace frontend
188 } // namespace lyx