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