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