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)
37 bool operator()(Index const & index) const
39 return index.index() == name_;
46 class IndexHasShortcut : public std::unary_function<Index, bool>
49 IndexHasShortcut(docstring const & shortcut)
53 bool operator()(Index const & index) const
55 return index.shortcut() == shortc_;
64 /////////////////////////////////////////////////////////////////////
68 /////////////////////////////////////////////////////////////////////
73 // no theApp() with command line export
75 theApp()->getRgbColor(Color_indexlabel, color_);
77 frontend::Application::getRgbColorUncached(Color_indexlabel, color_);
81 docstring const & Index::index() const
87 void Index::setIndex(docstring const & s)
93 docstring const & Index::shortcut() const
99 void Index::setShortcut(docstring const & s)
105 RGBColor const & Index::color() const
111 void Index::setColor(RGBColor const & c)
117 void Index::setColor(string const & str)
119 if (str.size() == 7 && str[0] == '#')
120 color_ = rgbFromHexName(str);
122 // no color set or invalid color -- use predefined color
123 // no theApp() with command line export
125 theApp()->getRgbColor(Color_indexlabel, color_);
127 frontend::Application::getRgbColorUncached(Color_indexlabel, color_);
132 /////////////////////////////////////////////////////////////////////
136 /////////////////////////////////////////////////////////////////////
139 Index * IndicesList::find(docstring const & name)
142 find_if(list.begin(), list.end(), IndexNamesEqual(name));
143 return it == list.end() ? 0 : &*it;
147 Index const * IndicesList::find(docstring const & name) const
149 List::const_iterator it =
150 find_if(list.begin(), list.end(), IndexNamesEqual(name));
151 return it == list.end() ? 0 : &*it;
155 Index * IndicesList::findShortcut(docstring const & shortcut)
158 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
159 return it == list.end() ? 0 : &*it;
163 Index const * IndicesList::findShortcut(docstring const & shortcut) const
165 List::const_iterator it =
166 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
167 return it == list.end() ? 0 : &*it;
171 bool IndicesList::add(docstring const & n, docstring const & s)
176 size_t const j = n.find_first_of(separator_, i);
178 if (j == docstring::npos)
181 name = n.substr(i, j - i);
182 // Is this name already in the list?
184 find_if(list.begin(), list.end(),
185 IndexNamesEqual(name)) != list.end();
190 docstring sc = s.empty() ?
191 trim(lowercase(name.substr(0, 3))) : s;
192 if (findShortcut(sc) != 0) {
194 docstring scn = sc + convert<docstring>(i);
195 while (findShortcut(scn) != 0) {
197 scn = sc + convert<docstring>(i);
204 if (j == docstring::npos)
212 bool IndicesList::addDefault(docstring const & n)
214 if (findShortcut(from_ascii("idx")) != 0)
215 // we already have a default
217 return add(n, from_ascii("idx"));
220 bool IndicesList::remove(docstring const & s)
222 size_t const size = list.size();
223 list.remove_if(IndexNamesEqual(s));
224 return size != list.size();
228 bool IndicesList::rename(docstring const & oldname,
229 docstring const & newname)
233 if (find_if(list.begin(), list.end(),
234 IndexNamesEqual(newname)) != list.end())
235 // new name already taken
238 Index * index = find(oldname);
241 index->setIndex(newname);