]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
* Baruch's GuiBC template.
[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         for (vector<string>::size_type i=0; i<vec.size(); ++i) {
39                 if (i > 0) str += delim;
40                 str += vec[i];
41         }
42         return str;
43 }
44
45 vector<string> const getVectorFromString(string const & str,
46                                          string const & delim)
47 {
48         vector<string> vec;
49         string keys(str);
50
51         for(;;) {
52                 string::size_type const idx = keys.find(delim);
53                 if (idx == string::npos) break;
54                 
55                 vec.push_back(keys.substr(0, idx));
56
57                 string::size_type const start = idx + delim.size();
58                 keys = keys.substr(start);
59         }
60
61         if (vec.empty()) // unable to separate
62                 vec.push_back(str);
63
64         return vec;
65 }
66
67 string const browseFile(LyXView * lv, string const & filename,
68                         string const & title,
69                         string const & pattern, 
70                         pair<string,string> const & dir1,
71                         pair<string,string> const & dir2)
72 {
73         string lastPath = ".";
74         if (!filename.empty()) lastPath = OnlyPath(filename);
75
76         FileDialog fileDlg(lv, title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
77
78         FileDialog::Result result;
79  
80         while (1) {
81                 result = fileDlg.Select(lastPath, pattern, OnlyFilename(filename));
82
83                 if (result.second.empty()) 
84                         return result.second;
85
86                 lastPath = OnlyPath(result.second);
87
88                 if (result.second.find_first_of("#~$% ") == string::npos)
89                         break; 
90  
91                 WriteAlert(_("Filename can't contain any "
92                         "of these characters:"),
93                         _("space, '#', '~', '$' or '%'."));
94         }
95
96         return result.second;
97 }
98
99