]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
The file dialog browser doesn't need a LyXView...
[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::alert(_("Filename can't contain any "
61                         "of these characters:"),
62                         _("space, '#', '~', '$' or '%'."));
63         }
64
65         return result.second;
66 }
67
68
69 string const browseRelFile(string const & filename,
70                         string const & refpath,
71                         string const & title,
72                         string const & pattern,
73                         bool save,
74                         pair<string,string> const & dir1,
75                         pair<string,string> const & dir2)
76 {
77         string const fname = MakeAbsPath(filename, refpath);
78
79         string const outname = browseFile(fname, title, pattern, save,
80                                           dir1, dir2);
81         string const reloutname = MakeRelPath(outname, refpath);
82         if (prefixIs(reloutname, "../"))
83                 return outname;
84         else
85                 return reloutname;
86 }
87
88
89 string const browseDir(string const & pathname,
90                         string const & title,
91                         pair<string,string> const & dir1,
92                         pair<string,string> const & dir2)
93 {
94         string lastPath(".");
95         if (!pathname.empty())
96                 lastPath = OnlyPath(pathname);
97
98         FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
99
100         FileDialog::Result result;
101
102         while (true) {
103                 result = fileDlg.opendir(lastPath,
104                                 OnlyFilename(pathname));
105
106                 if (result.second.empty())
107                         return result.second;
108
109                 lastPath = OnlyPath(result.second);
110
111                 if (result.second.find_first_of("#~$% ") == string::npos)
112                         break;
113
114                 Alert::alert(_("directory name can't contain any "
115                         "of these characters:"),
116                         _("space, '#', '~', '$' or '%'."));
117         }
118
119         return result.second;
120 }
121
122
123 // sorry this is just a temporary hack we should include vspace.h! (Jug)
124 extern const char * stringFromUnit(int);
125
126 vector<string> const getLatexUnits()
127 {
128         vector<string> units;
129         char const * str;
130         for (int i = 0; (str = stringFromUnit(i)); ++i)
131             units.push_back(str);
132
133         return units;
134 }