]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
small fixes...
[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                         string const key = frontStrip(keys);
64                         vec.push_back(keys);
65                         break;
66                 }
67
68                 string const key = strip(frontStrip(keys.substr(0, idx)));
69                 if (!key.empty())
70                         vec.push_back(key);
71
72                 string::size_type const start = idx + delim.size();
73                 keys = keys.substr(start);
74         }
75
76         return vec;
77 }
78
79 string const browseFile(LyXView * lv, string const & filename,
80                         string const & title,
81                         string const & pattern, 
82                         pair<string,string> const & dir1,
83                         pair<string,string> const & dir2)
84 {
85         string lastPath = ".";
86         if (!filename.empty()) 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                 WriteAlert(_("Filename can't contain any "
104                         "of these characters:"),
105                         _("space, '#', '~', '$' or '%'."));
106         }
107
108         return result.second;
109 }
110
111