]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
fix bug 175 (minipage in minipage); fix some warnings; apply external.diff from Herbert
[lyx.git] / src / frontends / controllers / helper_funcs.C
1 /* This file is part of
2  * ====================================================== 
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2001 The LyX Team.
7  *
8  * ======================================================
9  *
10  * \file helper_funcs.C
11  * \author Angus Leeming <a.leeming@ic.ac.uk>
12  */
13
14 #include <vector>
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include <config.h>
21 #include "LString.h"
22 #include "helper_funcs.h"
23
24 #include "frontends/FileDialog.h"
25 #include "support/filetools.h" // OnlyPath, OnlyFilename
26 #include "support/lstrings.h"
27 #include "gettext.h" // _()
28 #include "frontends/Alert.h"
29
30 using std::pair;
31 using std::vector;
32 using std::make_pair;
33
34
35 string const getStringFromVector(vector<string> const & vec,
36                                  string const & delim)
37 {
38         string str;
39         int i = 0;
40         for (vector<string>::const_iterator it = vec.begin();
41              it != vec.end(); ++it) {
42                 string item = strip(frontStrip(*it));
43                 if (item.empty()) continue;
44
45                 if (i++ > 0) str += delim;
46                 str += item;
47         }
48         return str;
49 }
50
51 vector<string> const getVectorFromString(string const & str,
52                                          string const & delim)
53 {
54         vector<string> vec;
55         if (str.empty())
56                 return vec;
57
58         string keys(strip(str));
59
60         for(;;) {
61                 string::size_type const idx = keys.find(delim);
62                 if (idx == string::npos) {
63                         vec.push_back(frontStrip(keys));
64                         break;
65                 }
66
67                 string const key = strip(frontStrip(keys.substr(0, idx)));
68                 if (!key.empty())
69                         vec.push_back(key);
70
71                 string::size_type const start = idx + delim.size();
72                 keys = keys.substr(start);
73         }
74
75         return vec;
76 }
77
78 string const browseFile(LyXView * lv, string const & filename,
79                         string const & title,
80                         string const & pattern, 
81                         pair<string,string> const & dir1,
82                         pair<string,string> const & dir2)
83 {
84         string lastPath = ".";
85         if (!filename.empty())
86                 lastPath = OnlyPath(filename);
87
88         FileDialog fileDlg(lv, title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
89
90         FileDialog::Result result;
91  
92         while (1) {
93                 result = fileDlg.Select(lastPath, pattern, OnlyFilename(filename));
94
95                 if (result.second.empty()) 
96                         return result.second;
97
98                 lastPath = OnlyPath(result.second);
99
100                 if (result.second.find_first_of("#~$% ") == string::npos)
101                         break; 
102  
103                 Alert::alert(_("Filename can't contain any "
104                         "of these characters:"),
105                         _("space, '#', '~', '$' or '%'."));
106         }
107
108         return result.second;
109 }
110
111
112 string const browseRelFile(LyXView * lv, string const & filename,
113                         string const & refpath,
114                         string const & title,
115                         string const & pattern, 
116                         pair<string,string> const & dir1,
117                         pair<string,string> const & dir2)
118 {
119         string const fname = MakeAbsPath(filename, refpath);
120         
121         string const outname = browseFile(lv, fname, title, pattern,
122                                           dir1, dir2);
123         string const reloutname = MakeRelPath(outname, refpath);
124         if(prefixIs(reloutname, "../"))
125                 return outname;
126         else
127                 return reloutname;
128 }
129
130
131 // sorry this is just a temporary hack we should include vspace.h! (Jug)
132 extern const char * stringFromUnit(int);
133
134 vector<string> const getLatexUnits()
135 {
136         vector<string> units;
137         const char * str;
138         for(int i=0; (str = stringFromUnit(i)); ++i)
139             units.push_back(str);
140
141         return units;
142 }