]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/helper_funcs.C
Added // -*- C++ -*- to the top of all files in controllers/ and xforms/
[lyx.git] / src / frontends / controllers / helper_funcs.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  *
5  *           LyX, The Document Processor
6  *
7  *           Copyright 2001 The LyX Team.
8  *
9  * ======================================================
10  *
11  * \file helper_funcs.C
12  * \author Angus Leeming <a.leeming@ic.ac.uk>
13  */
14
15 #include <vector>
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include <config.h>
22 #include "LString.h"
23 #include "helper_funcs.h"
24
25 #include "frontends/FileDialog.h"
26 #include "support/filetools.h" // OnlyPath, OnlyFilename
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                 if (it->empty()) continue;
43
44                 if (i++ > 0) str += delim;
45                 str += *it;
46         }
47         return str;
48 }
49
50 vector<string> const getVectorFromString(string const & str,
51                                          string const & delim)
52 {
53         vector<string> vec;
54         string keys(str);
55
56         for(;;) {
57                 string::size_type const idx = keys.find(delim);
58                 if (idx == string::npos) {
59                         vec.push_back(keys);
60                         break;
61                 }
62
63                 string const key = keys.substr(0, idx);
64                 if (!key.empty())
65                         vec.push_back(key);
66
67                 string::size_type const start = idx + delim.size();
68                 keys = keys.substr(start);
69         }
70
71         return vec;
72 }
73
74 string const browseFile(LyXView * lv, string const & filename,
75                         string const & title,
76                         string const & pattern, 
77                         pair<string,string> const & dir1,
78                         pair<string,string> const & dir2)
79 {
80         string lastPath = ".";
81         if (!filename.empty()) lastPath = OnlyPath(filename);
82
83         FileDialog fileDlg(lv, title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
84
85         FileDialog::Result result;
86  
87         while (1) {
88                 result = fileDlg.Select(lastPath, pattern, OnlyFilename(filename));
89
90                 if (result.second.empty()) 
91                         return result.second;
92
93                 lastPath = OnlyPath(result.second);
94
95                 if (result.second.find_first_of("#~$% ") == string::npos)
96                         break; 
97  
98                 WriteAlert(_("Filename can't contain any "
99                         "of these characters:"),
100                         _("space, '#', '~', '$' or '%'."));
101         }
102
103         return result.second;
104 }
105
106