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