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