2 * \file IndicesList.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jürgen Spitzmüller
8 * Full author contact details are available in file CREDITS.
13 #include "IndicesList.h"
16 #include "frontends/Application.h"
18 #include "support/convert.h"
19 #include "support/lstrings.h"
24 using namespace lyx::support;
30 class IndexNamesEqual : public std::unary_function<Index, bool>
33 IndexNamesEqual(docstring const & name) : name_(name) {}
35 bool operator()(Index const & index) const
37 return index.index() == name_;
44 class IndexHasShortcut : public std::unary_function<Index, bool>
47 IndexHasShortcut(docstring const & shortcut) : shortc_(shortcut) {}
49 bool operator()(Index const & index) const
51 return index.shortcut() == shortc_;
60 /////////////////////////////////////////////////////////////////////
64 /////////////////////////////////////////////////////////////////////
69 // no theApp() with command line export
71 theApp()->getRgbColor(Color_indexlabel, color_);
75 docstring const & Index::index() const
81 void Index::setIndex(docstring const & s)
87 docstring const & Index::shortcut() const
93 void Index::setShortcut(docstring const & s)
99 RGBColor const & Index::color() const
105 void Index::setColor(RGBColor const & c)
111 void Index::setColor(string const & str)
113 if (str.size() == 7 && str[0] == '#')
114 color_ = rgbFromHexName(str);
116 // no color set or invalid color -- use predefined color
117 theApp()->getRgbColor(Color_indexlabel, color_);
121 /////////////////////////////////////////////////////////////////////
125 /////////////////////////////////////////////////////////////////////
128 Index * IndicesList::find(docstring const & name)
131 find_if(list.begin(), list.end(), IndexNamesEqual(name));
132 return it == list.end() ? 0 : &*it;
136 Index const * IndicesList::find(docstring const & name) const
138 List::const_iterator it =
139 find_if(list.begin(), list.end(), IndexNamesEqual(name));
140 return it == list.end() ? 0 : &*it;
144 Index * IndicesList::findShortcut(docstring const & shortcut)
147 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
148 return it == list.end() ? 0 : &*it;
152 Index const * IndicesList::findShortcut(docstring const & shortcut) const
154 List::const_iterator it =
155 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
156 return it == list.end() ? 0 : &*it;
160 bool IndicesList::add(docstring const & n, docstring const & s)
165 size_t const j = n.find_first_of(separator_, i);
167 if (j == docstring::npos)
170 name = n.substr(i, j - i);
171 // Is this name already in the list?
173 find_if(list.begin(), list.end(),
174 IndexNamesEqual(name)) != list.end();
179 docstring sc = s.empty() ?
180 trim(lowercase(name.substr(0, 3))) : s;
181 if (findShortcut(sc) != 0) {
183 docstring scn = sc + convert<docstring>(i);
184 while (findShortcut(scn) != 0) {
186 scn = sc + convert<docstring>(i);
193 if (j == docstring::npos)
201 bool IndicesList::addDefault(docstring const & n)
203 if (findShortcut(from_ascii("idx")) != 0)
204 // we already have a default
206 return add(n, from_ascii("idx"));
209 bool IndicesList::remove(docstring const & s)
211 size_t const size = list.size();
212 list.remove_if(IndexNamesEqual(s));
213 return size != list.size();
217 bool IndicesList::rename(docstring const & oldname,
218 docstring const & newname)
222 if (find_if(list.begin(), list.end(),
223 IndexNamesEqual(newname)) != list.end())
224 // new name already taken
227 Index * index = find(oldname);
230 index->setIndex(newname);