]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
obvious stuff
[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 Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11
12 #include <config.h>
13
14 #include "helper_funcs.h"
15
16 #include "gettext.h"
17
18 #include "frontends/Alert.h"
19 #include "frontends/FileDialog.h"
20
21 #include "support/filetools.h" // OnlyPath, OnlyFilename
22 #include "support/globbing.h"
23
24 using std::pair;
25 using std::vector;
26 using std::string;
27
28 // sorry this is just a temporary hack we should include vspace.h! (Jug)
29 extern const char * stringFromUnit(int);
30
31 namespace lyx {
32
33 using support::FileFilterList;
34 using support::MakeAbsPath;
35 using support::MakeRelPath;
36 using support::OnlyFilename;
37 using support::OnlyPath;
38 using support::prefixIs;
39
40 namespace frontend {
41
42
43 string const browseFile(string const & filename,
44                         string const & title,
45                         FileFilterList const & filters,
46                         bool save,
47                         pair<string,string> const & dir1,
48                         pair<string,string> const & dir2)
49 {
50         string lastPath(".");
51         if (!filename.empty())
52                 lastPath = OnlyPath(filename);
53
54         FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
55
56         FileDialog::Result result;
57
58         while (true) {
59                 if (save)
60                         result = fileDlg.save(lastPath, filters,
61                                 OnlyFilename(filename));
62                 else
63                         result = fileDlg.open(lastPath, filters,
64                                 OnlyFilename(filename));
65
66                 if (result.second.empty())
67                         return result.second;
68
69                 lastPath = OnlyPath(result.second);
70
71                 if (result.second.find_first_of("#~$% ") == string::npos)
72                         break;
73
74                 Alert::error(_("Invalid filename"),
75                         _("Filename can't contain any "
76                         "of these characters:\n"
77                         "space, '#', '~', '$' or '%'."));
78         }
79
80         return result.second;
81 }
82
83
84 string const browseRelFile(string const & filename,
85                            string const & refpath,
86                            string const & title,
87                            FileFilterList const & filters,
88                            bool save,
89                            pair<string,string> const & dir1,
90                            pair<string,string> const & dir2)
91 {
92         string const fname = MakeAbsPath(filename, refpath);
93
94         string const outname = browseFile(fname, title, filters, save,
95                                           dir1, dir2);
96         string const reloutname = MakeRelPath(outname, refpath);
97         if (prefixIs(reloutname, "../"))
98                 return outname;
99         else
100                 return reloutname;
101 }
102
103
104 string const browseDir(string const & pathname,
105                         string const & title,
106                         pair<string,string> const & dir1,
107                         pair<string,string> const & dir2)
108 {
109         string lastPath(".");
110         if (!pathname.empty())
111                 lastPath = OnlyPath(pathname);
112
113         FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
114
115         FileDialog::Result result;
116
117         while (true) {
118                 result = fileDlg.opendir(lastPath,
119                                 OnlyFilename(pathname));
120
121                 if (result.second.empty())
122                         return result.second;
123
124                 lastPath = OnlyPath(result.second);
125
126                 if (result.second.find_first_of("#~$% ") == string::npos)
127                         break;
128
129                 Alert::error(_("Invalid filename"),
130                         _("Filename can't contain any "
131                         "of these characters:\n"
132                         "space, '#', '~', '$' or '%'."));
133         }
134
135         return result.second;
136 }
137
138
139 vector<string> const getLatexUnits()
140 {
141         vector<string> units;
142         char const * str;
143         for (int i = 0; (str = stringFromUnit(i)); ++i)
144             units.push_back(str);
145
146         return units;
147 }
148
149 } // namespace frontend
150 } // namespace lyx