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_);
73 frontend::Application::getRgbColorUncached(Color_indexlabel, color_);
77 docstring const & Index::index() const
83 void Index::setIndex(docstring const & s)
89 docstring const & Index::shortcut() const
95 void Index::setShortcut(docstring const & s)
101 RGBColor const & Index::color() const
107 void Index::setColor(RGBColor const & c)
113 void Index::setColor(string const & str)
115 if (str.size() == 7 && str[0] == '#')
116 color_ = rgbFromHexName(str);
118 // no color set or invalid color -- use predefined color
119 // no theApp() with command line export
121 theApp()->getRgbColor(Color_indexlabel, color_);
123 frontend::Application::getRgbColorUncached(Color_indexlabel, color_);
128 /////////////////////////////////////////////////////////////////////
132 /////////////////////////////////////////////////////////////////////
135 Index * IndicesList::find(docstring const & name)
138 find_if(list.begin(), list.end(), IndexNamesEqual(name));
139 return it == list.end() ? 0 : &*it;
143 Index const * IndicesList::find(docstring const & name) const
145 List::const_iterator it =
146 find_if(list.begin(), list.end(), IndexNamesEqual(name));
147 return it == list.end() ? 0 : &*it;
151 Index * IndicesList::findShortcut(docstring const & shortcut)
154 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
155 return it == list.end() ? 0 : &*it;
159 Index const * IndicesList::findShortcut(docstring const & shortcut) const
161 List::const_iterator it =
162 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
163 return it == list.end() ? 0 : &*it;
167 bool IndicesList::add(docstring const & n, docstring const & s)
172 size_t const j = n.find_first_of(separator_, i);
174 if (j == docstring::npos)
177 name = n.substr(i, j - i);
178 // Is this name already in the list?
180 find_if(list.begin(), list.end(),
181 IndexNamesEqual(name)) != list.end();
186 docstring sc = s.empty() ?
187 trim(lowercase(name.substr(0, 3))) : s;
188 if (findShortcut(sc) != 0) {
190 docstring scn = sc + convert<docstring>(i);
191 while (findShortcut(scn) != 0) {
193 scn = sc + convert<docstring>(i);
200 if (j == docstring::npos)
208 bool IndicesList::addDefault(docstring const & n)
210 if (findShortcut(from_ascii("idx")) != 0)
211 // we already have a default
213 return add(n, from_ascii("idx"));
216 bool IndicesList::remove(docstring const & s)
218 size_t const size = list.size();
219 list.remove_if(IndexNamesEqual(s));
220 return size != list.size();
224 bool IndicesList::rename(docstring const & oldname,
225 docstring const & newname)
229 if (find_if(list.begin(), list.end(),
230 IndexNamesEqual(newname)) != list.end())
231 // new name already taken
234 Index * index = find(oldname);
237 index->setIndex(newname);