3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Martin Vermeer
9 * Full author contact details are available in file CREDITS.
18 #include "support/lstrings.h"
20 #include <boost/assert.hpp>
22 #include "support/std_sstream.h"
26 using std::ostringstream;
35 void Counter::set(int v)
41 void Counter::addto(int v)
47 int Counter::value() const
65 string Counter::master() const
71 void Counter::setMaster(string const & m)
78 void Counters::newCounter(string const & newc)
80 // First check if newc already exist
81 CounterList::iterator cit = counterList.find(newc);
82 // if already exist give warning and return
83 if (cit != counterList.end()) {
84 lyxerr << "The new counter already exists." << endl;
91 void Counters::newCounter(string const & newc, string const & masterc)
93 // First check if newc already exists
94 CounterList::iterator cit = counterList.find(newc);
95 // if already existant give warning and return
96 if (cit != counterList.end()) {
97 lyxerr << "The new counter already exists." << endl;
100 // then check if masterc exists
101 CounterList::iterator it = counterList.find(masterc);
102 // if not give warning and return
103 if (it == counterList.end()) {
104 lyxerr << "The master counter does not exist." << endl;
108 counterList[newc].setMaster(masterc);
112 void Counters::set(string const & ctr, int val)
114 CounterList::iterator it = counterList.find(ctr);
115 if (it == counterList.end()) {
116 lyxerr << "set: Counter does not exist: " << ctr << endl;
123 void Counters::addto(string const & ctr, int val)
125 CounterList::iterator it = counterList.find(ctr);
126 if (it == counterList.end()) {
127 lyxerr << "addto: Counter does not exist: " << ctr << endl;
130 it->second.addto(val);
134 int Counters::value(string const & ctr) const
136 CounterList::const_iterator cit = counterList.find(ctr);
137 if (cit == counterList.end()) {
138 lyxerr << "value: Counter does not exist: " << ctr << endl;
141 return cit->second.value();
145 void Counters::step(string const & ctr)
147 CounterList::iterator it = counterList.find(ctr);
148 if (it == counterList.end()) {
149 lyxerr << "step: Counter does not exist: " << ctr << endl;
154 it = counterList.begin();
155 CounterList::iterator end = counterList.end();
156 for (; it != end; ++it) {
157 if (it->second.master() == ctr) {
164 void Counters::reset()
166 CounterList::iterator it = counterList.begin();
167 CounterList::iterator end = counterList.end();
168 for (; it != end; ++it) {
174 void Counters::reset(string const & match)
176 BOOST_ASSERT(!match.empty());
178 CounterList::iterator it = counterList.begin();
179 CounterList::iterator end = counterList.end();
180 for (; it != end; ++it) {
181 if (it->first.find(match) != string::npos)
187 void Counters::copy(Counters & from, Counters & to, string const & match)
189 CounterList::iterator it = counterList.begin();
190 CounterList::iterator end = counterList.end();
191 for (; it != end; ++it) {
192 if (it->first.find(match) != string::npos || match == "") {
193 to.set(it->first, from.value(it->first));
202 char loweralphaCounter(int n)
212 char alphaCounter(int n)
222 char hebrewCounter(int n)
224 static const char hebrew[22] = {
225 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
226 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
237 string const romanCounter(int n)
239 static char const * roman[20] = {
240 "i", "ii", "iii", "iv", "v",
241 "vi", "vii", "viii", "ix", "x",
242 "xi", "xii", "xiii", "xiv", "xv",
243 "xvi", "xvii", "xviii", "xix", "xx"
254 string Counters::labelItem(string const & ctr,
255 string const & numbertype,
256 string const & langtype,
262 CounterList::iterator it = counterList.find(ctr);
263 if (it == counterList.end()) {
264 lyxerr << "Counter does not exist." << endl;
269 s << '.' << value(ctr);
271 if (numbertype == "sectioning" || numbertype == "appendix") {
272 if (numbertype == "appendix") {
273 if (langtype == "hebrew") {
274 o << hebrewCounter(value(ctr));
276 o << alphaCounter(value(ctr));
278 } else o << value(ctr);
283 return STRCONV(s.str());
287 string Counters::numberLabel(string const & ctr,
288 string const & numbertype,
289 string const & langtype,
294 if (numbertype == "sectioning" || numbertype == "appendix") {
295 if (ctr == "chapter" && head == 0) {
296 s << labelItem("chapter", numbertype, langtype, true);
297 } else if (ctr == "section" && head <= 1) {
298 s << numberLabel("chapter", numbertype, langtype, head)
299 << labelItem("section", numbertype, langtype, head == 1);
300 } else if (ctr == "subsection" && head <= 2) {
301 s << numberLabel("section", numbertype, langtype, head)
302 << labelItem("subsection", numbertype, langtype, head == 2);
303 } else if (ctr == "subsubsection" && head <= 3) {
304 s << numberLabel("subsection", numbertype, langtype, head)
305 << labelItem("subsubsection", numbertype, langtype, head == 3);
306 } else if (ctr == "paragraph" && head <= 4) {
307 s << numberLabel("subsubsection", numbertype, langtype, head)
308 << labelItem("paragraph", numbertype, langtype, head == 4);
309 } else if (ctr == "subparagraph" && head <= 5) {
310 s << numberLabel("paragraph", numbertype, langtype, head)
311 << labelItem("subparagraph", numbertype, langtype, head == 5);
312 } else if (ctr == "figure" || ctr == "table") {
313 // figure, table, ...
314 lyxerr << "Counter:" << ctr << endl;
315 s << numberLabel("chapter", numbertype, langtype, head)
316 << labelItem(ctr, numbertype, langtype, head == 1);
319 } else if (numbertype == "enumeration") {
325 if (langtype == "hebrew") {
326 ei << '.' << value("enumi");
327 eii << '(' << hebrewCounter(value("enumii")) << ')';
328 eiii << '.' << romanCounter(value("enumiii"));
329 eiv << '.' << alphaCounter(value("enumiv"));
331 ei << value("enumi") << '.';
332 eii << '(' << loweralphaCounter(value("enumii")) << ')';
333 eiii << romanCounter(value("enumiii")) << '.';
334 eiv << alphaCounter(value("enumiv")) << '.';
336 if (ctr == "enumii") {
338 } else if (ctr == "enumi") {
340 } else if (ctr == "enumiii") {
342 } else if (ctr == "enumiv") {
347 return STRCONV(s.str());