]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
Address most (if not all?) of the problems raised by Garst about the
[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 "lyx_gui_misc.h" // WriteAlert
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()) lastPath = OnlyPath(filename);
86
87         FileDialog fileDlg(lv, title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
88
89         FileDialog::Result result;
90  
91         while (1) {
92                 result = fileDlg.Select(lastPath, pattern, OnlyFilename(filename));
93
94                 if (result.second.empty()) 
95                         return result.second;
96
97                 lastPath = OnlyPath(result.second);
98
99                 if (result.second.find_first_of("#~$% ") == string::npos)
100                         break; 
101  
102                 WriteAlert(_("Filename can't contain any "
103                         "of these characters:"),
104                         _("space, '#', '~', '$' or '%'."));
105         }
106
107         return result.second;
108 }
109
110
111 // sorry this is just a temporary hack we should include vspace.h! (Jug)
112 extern const char * stringFromUnit(int);
113
114 vector<string> const getLatexUnits()
115 {
116         vector<string> units;
117         const char * str;
118         for(int i=0; (str = stringFromUnit(i)); ++i)
119             units.push_back(str);
120
121         return units;
122 }