]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
Merge natbib branch into head
[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 "gettext.h" // _()
27 #include "lyx_gui_misc.h" // WriteAlert
28
29 using std::pair;
30 using std::vector;
31 using std::make_pair;
32
33
34 string const getStringFromVector(vector<string> const & vec,
35                                  string const & delim)
36 {
37         string str;
38         int i = 0;
39         for (vector<string>::const_iterator it = vec.begin();
40              it != vec.end(); ++it) {
41                 if (it->empty()) continue;
42
43                 if (i++ > 0) str += delim;
44                 str += *it;
45         }
46         return str;
47 }
48
49 vector<string> const getVectorFromString(string const & str,
50                                          string const & delim)
51 {
52         vector<string> vec;
53         if (str.empty())
54                 return vec;
55
56         string keys(str);
57
58         for(;;) {
59                 string::size_type const idx = keys.find(delim);
60                 if (idx == string::npos) {
61                         vec.push_back(keys);
62                         break;
63                 }
64
65                 string const key = keys.substr(0, idx);
66                 if (!key.empty())
67                         vec.push_back(key);
68
69                 string::size_type const start = idx + delim.size();
70                 keys = keys.substr(start);
71         }
72
73         return vec;
74 }
75
76 string const browseFile(LyXView * lv, string const & filename,
77                         string const & title,
78                         string const & pattern, 
79                         pair<string,string> const & dir1,
80                         pair<string,string> const & dir2)
81 {
82         string lastPath = ".";
83         if (!filename.empty()) lastPath = OnlyPath(filename);
84
85         FileDialog fileDlg(lv, title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
86
87         FileDialog::Result result;
88  
89         while (1) {
90                 result = fileDlg.Select(lastPath, pattern, OnlyFilename(filename));
91
92                 if (result.second.empty()) 
93                         return result.second;
94
95                 lastPath = OnlyPath(result.second);
96
97                 if (result.second.find_first_of("#~$% ") == string::npos)
98                         break; 
99  
100                 WriteAlert(_("Filename can't contain any "
101                         "of these characters:"),
102                         _("space, '#', '~', '$' or '%'."));
103         }
104
105         return result.second;
106 }
107
108