]> git.lyx.org Git - lyx.git/blob - src/IndicesList.cpp
Enable dissolve in undefined Flex inset context menu
[lyx.git] / src / IndicesList.cpp
1 /**
2  * \file IndicesList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Spitzmüller
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "IndicesList.h"
14 #include "Color.h"
15
16 #include "frontends/Application.h"
17
18 #include "support/convert.h"
19 #include "support/lstrings.h"
20
21 #include <algorithm>
22
23 using namespace std;
24 using namespace lyx::support;
25
26 namespace lyx {
27
28 namespace {
29
30 class IndexNamesEqual : public std::unary_function<Index, bool>
31 {
32 public:
33         IndexNamesEqual(docstring const & name)
34                 : name_(name)
35         {}
36
37         bool operator()(Index const & index) const
38         {
39                 return index.index() == name_;
40         }
41 private:
42         docstring name_;
43 };
44
45
46 class IndexHasShortcut : public std::unary_function<Index, bool>
47 {
48 public:
49         IndexHasShortcut(docstring const & shortcut)
50                 : shortc_(shortcut)
51         {}
52
53         bool operator()(Index const & index) const
54         {
55                 return index.shortcut() == shortc_;
56         }
57 private:
58         docstring shortc_;
59 };
60
61 } // namespace
62
63
64 /////////////////////////////////////////////////////////////////////
65 //
66 // Index
67 //
68 /////////////////////////////////////////////////////////////////////
69
70
71 Index::Index()
72 {
73         // no theApp() with command line export
74         if (theApp())
75                 theApp()->getRgbColor(Color_indexlabel, color_);
76         else
77                 frontend::Application::getRgbColorUncached(Color_indexlabel, color_);
78 }
79
80
81 docstring const & Index::index() const
82 {
83         return index_;
84 }
85
86
87 void Index::setIndex(docstring const & s)
88 {
89         index_ = s;
90 }
91
92
93 docstring const & Index::shortcut() const
94 {
95         return shortcut_;
96 }
97
98
99 void Index::setShortcut(docstring const & s)
100 {
101         shortcut_ = s;
102 }
103
104
105 RGBColor const & Index::color() const
106 {
107         return color_;
108 }
109
110
111 void Index::setColor(RGBColor const & c)
112 {
113         color_ = c;
114 }
115
116
117 void Index::setColor(string const & str)
118 {
119         if (str.size() == 7 && str[0] == '#')
120                 color_ = rgbFromHexName(str);
121         else {
122                 // no color set or invalid color -- use predefined color
123                 // no theApp() with command line export
124                 if (theApp())
125                         theApp()->getRgbColor(Color_indexlabel, color_);
126                 else
127                         frontend::Application::getRgbColorUncached(Color_indexlabel, color_);
128         }
129 }
130
131
132 /////////////////////////////////////////////////////////////////////
133 //
134 // IndicesList
135 //
136 /////////////////////////////////////////////////////////////////////
137
138
139 Index * IndicesList::find(docstring const & name)
140 {
141         List::iterator it =
142                 find_if(list.begin(), list.end(), IndexNamesEqual(name));
143         return it == list.end() ? 0 : &*it;
144 }
145
146
147 Index const * IndicesList::find(docstring const & name) const
148 {
149         List::const_iterator it =
150                 find_if(list.begin(), list.end(), IndexNamesEqual(name));
151         return it == list.end() ? 0 : &*it;
152 }
153
154
155 Index * IndicesList::findShortcut(docstring const & shortcut)
156 {
157         List::iterator it =
158                 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
159         return it == list.end() ? 0 : &*it;
160 }
161
162
163 Index const * IndicesList::findShortcut(docstring const & shortcut) const
164 {
165         List::const_iterator it =
166                 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
167         return it == list.end() ? 0 : &*it;
168 }
169
170
171 bool IndicesList::add(docstring const & n, docstring const & s)
172 {
173         bool added = false;
174         size_t i = 0;
175         while (true) {
176                 size_t const j = n.find_first_of(separator_, i);
177                 docstring name;
178                 if (j == docstring::npos)
179                         name = n.substr(i);
180                 else
181                         name = n.substr(i, j - i);
182                 // Is this name already in the list?
183                 bool const already =
184                         find_if(list.begin(), list.end(),
185                                      IndexNamesEqual(name)) != list.end();
186                 if (!already) {
187                         added = true;
188                         Index in;
189                         in.setIndex(name);
190                         docstring sc = s.empty() ?
191                                 trim(lowercase(name.substr(0, 3))) : s;
192                         if (findShortcut(sc) != 0) {
193                                 int i = 1;
194                                 docstring scn = sc + convert<docstring>(i);
195                                 while (findShortcut(scn) != 0) {
196                                         ++i;
197                                         scn = sc + convert<docstring>(i);
198                                 }
199                                 in.setShortcut(scn);
200                         } else
201                                 in.setShortcut(sc);
202                         list.push_back(in);
203                 }
204                 if (j == docstring::npos)
205                         break;
206                 i = j + 1;
207         }
208         return added;
209 }
210
211
212 bool IndicesList::addDefault(docstring const & n)
213 {
214         if (findShortcut(from_ascii("idx")) != 0)
215                 // we already have a default
216                 return false;
217         return add(n, from_ascii("idx"));
218 }
219
220 bool IndicesList::remove(docstring const & s)
221 {
222         size_t const size = list.size();
223         list.remove_if(IndexNamesEqual(s));
224         return size != list.size();
225 }
226
227
228 bool IndicesList::rename(docstring const & oldname,
229         docstring const & newname)
230 {
231         if (newname.empty())
232                 return false;
233         if (find_if(list.begin(), list.end(),
234                     IndexNamesEqual(newname)) != list.end())
235                 // new name already taken
236                 return false;
237
238         Index * index = find(oldname);
239         if (!index)
240                 return false;
241         index->setIndex(newname);
242         return true;
243 }
244
245
246 } // namespace lyx