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