]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
fix crash due to invalidated iterator
[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 Jean-Marc Lasgouttes
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12
13 #include <config.h>
14
15 #include "helper_funcs.h"
16
17 #include "gettext.h"
18 #include "lyxlength.h"
19
20 #include "frontends/FileDialog.h"
21
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
24 #include "support/package.h"
25
26 using std::pair;
27 using std::vector;
28 using std::string;
29
30 namespace lyx {
31
32 using support::AddName;
33 using support::ChangeExtension;
34 using support::FileFilterList;
35 using support::GetExtension;
36 using support::LibFileSearch;
37 using support::MakeAbsPath;
38 using support::MakeRelPath;
39 using support::OnlyFilename;
40 using support::OnlyPath;
41 using support::package;
42 using support::prefixIs;
43
44 namespace frontend {
45
46
47 string const browseFile(string const & filename,
48                         string const & title,
49                         FileFilterList const & filters,
50                         bool save,
51                         pair<string,string> const & dir1,
52                         pair<string,string> const & dir2)
53 {
54         string lastPath(".");
55         if (!filename.empty())
56                 lastPath = OnlyPath(filename);
57
58         FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
59
60         FileDialog::Result result;
61
62         if (save)
63                 result = fileDlg.save(lastPath, filters,
64                                       OnlyFilename(filename));
65         else
66                 result = fileDlg.open(lastPath, filters,
67                                       OnlyFilename(filename));
68
69         return result.second;
70 }
71
72
73 string const browseRelFile(string const & filename,
74                            string const & refpath,
75                            string const & title,
76                            FileFilterList const & filters,
77                            bool save,
78                            pair<string,string> const & dir1,
79                            pair<string,string> const & dir2)
80 {
81         string const fname = MakeAbsPath(filename, refpath);
82
83         string const outname = browseFile(fname, title, filters, save,
84                                           dir1, dir2);
85         string const reloutname = MakeRelPath(outname, refpath);
86         if (prefixIs(reloutname, "../"))
87                 return outname;
88         else
89                 return reloutname;
90 }
91
92
93
94 string const browseLibFile(string const & dir,
95                            string const & name,
96                            string const & ext,
97                            string const & title,
98                            FileFilterList const & filters)
99 {
100         pair<string,string> const dir1(_("System files|#S#s"),
101                                        AddName(package().system_support(), dir));
102
103         pair<string,string> const dir2(_("User files|#U#u"),
104                                        AddName(package().user_support(), dir));
105
106         string const result = browseFile(LibFileSearch(dir, name, ext), title,
107                                          filters, false, dir1, dir2);
108
109         // remove the extension if it is the default one
110         string noextresult;
111         if (GetExtension(result) == ext)
112                 noextresult = ChangeExtension(result, string());
113         else
114                 noextresult = result;
115
116         // remove the directory, if it is the default one
117         string const file = OnlyFilename(noextresult);
118         if (LibFileSearch(dir, file, ext) == result)
119                 return file;
120         else
121                 return noextresult;
122 }
123
124
125 string const browseDir(string const & pathname,
126                        string const & title,
127                        pair<string,string> const & dir1,
128                        pair<string,string> const & dir2)
129 {
130         string lastPath(".");
131         if (!pathname.empty())
132                 lastPath = OnlyPath(pathname);
133
134         FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
135
136         FileDialog::Result const result =
137                 fileDlg.opendir(lastPath, OnlyFilename(pathname));
138
139         return result.second;
140 }
141
142
143 vector<string> const getLatexUnits()
144 {
145         vector<string> units;
146         int i = 0;
147         char const * str = stringFromUnit(i);
148         for (; str != 0; ++i, str = stringFromUnit(i))
149                 units.push_back(str);
150
151         return units;
152 }
153
154 } // namespace frontend
155 } // namespace lyx