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"
19 #include "support/LAssert.h"
21 using namespace lyx::support;
33 void Counter::set(int v)
39 void Counter::addto(int v)
45 int Counter::value() const
63 string Counter::master() const
69 void Counter::setMaster(string const & m)
76 void Counters::newCounter(string const & newc)
78 // First check if newc already exist
79 CounterList::iterator cit = counterList.find(newc);
80 // if already exist give warning and return
81 if (cit != counterList.end()) {
82 lyxerr << "The new counter already exists." << endl;
89 void Counters::newCounter(string const & newc, string const & masterc)
91 // First check if newc already exists
92 CounterList::iterator cit = counterList.find(newc);
93 // if already existant give warning and return
94 if (cit != counterList.end()) {
95 lyxerr << "The new counter already exists." << endl;
98 // then check if masterc exists
99 CounterList::iterator it = counterList.find(masterc);
100 // if not give warning and return
101 if (it == counterList.end()) {
102 lyxerr << "The master counter does not exist." << endl;
106 counterList[newc].setMaster(masterc);
110 void Counters::set(string const & ctr, int val)
112 CounterList::iterator it = counterList.find(ctr);
113 if (it == counterList.end()) {
114 lyxerr << "set: Counter does not exist: " << ctr << endl;
121 void Counters::addto(string const & ctr, int val)
123 CounterList::iterator it = counterList.find(ctr);
124 if (it == counterList.end()) {
125 lyxerr << "addto: Counter does not exist: " << ctr << endl;
128 it->second.addto(val);
132 int Counters::value(string const & ctr) const
134 CounterList::const_iterator cit = counterList.find(ctr);
135 if (cit == counterList.end()) {
136 lyxerr << "value: Counter does not exist: " << ctr << endl;
139 return cit->second.value();
143 void Counters::step(string const & ctr)
145 CounterList::iterator it = counterList.find(ctr);
146 if (it == counterList.end()) {
147 lyxerr << "step: Counter does not exist: " << ctr << endl;
152 it = counterList.begin();
153 CounterList::iterator end = counterList.end();
154 for (; it != end; ++it) {
155 if (it->second.master() == ctr) {
162 void Counters::reset()
164 CounterList::iterator it = counterList.begin();
165 CounterList::iterator end = counterList.end();
166 for (; it != end; ++it) {
172 void Counters::reset(string const & match)
174 Assert(!match.empty());
176 CounterList::iterator it = counterList.begin();
177 CounterList::iterator end = counterList.end();
178 for (; it != end; ++it) {
179 if (it->first.find(match) != string::npos)
185 void Counters::copy(Counters & from, Counters & to, string const & match)
187 CounterList::iterator it = counterList.begin();
188 CounterList::iterator end = counterList.end();
189 for (; it != end; ++it) {
190 if (it->first.find(match) != string::npos || match == "") {
191 to.set(it->first, from.value(it->first));
200 char loweralphaCounter(int n)
210 char alphaCounter(int n)
220 char hebrewCounter(int n)
222 static const char hebrew[22] = {
223 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
224 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
235 string const romanCounter(int n)
237 static char const * roman[20] = {
238 "i", "ii", "iii", "iv", "v",
239 "vi", "vii", "viii", "ix", "x",
240 "xi", "xii", "xiii", "xiv", "xv",
241 "xvi", "xvii", "xviii", "xix", "xx"
252 string Counters::labelItem(string const & ctr,
253 string const & numbertype,
254 string const & langtype,
260 CounterList::iterator it = counterList.find(ctr);
261 if (it == counterList.end()) {
262 lyxerr << "Counter does not exist." << endl;
267 s << '.' << value(ctr);
269 if (numbertype == "sectioning" || numbertype == "appendix") {
270 if (numbertype == "appendix") {
271 if (langtype == "hebrew") {
272 o << hebrewCounter(value(ctr));
274 o << alphaCounter(value(ctr));
276 } else o << value(ctr);
281 return STRCONV(s.str());
285 string Counters::numberLabel(string const & ctr,
286 string const & numbertype,
287 string const & langtype,
292 if (numbertype == "sectioning" || numbertype == "appendix") {
293 if (ctr == "chapter" && head == 0) {
294 s << labelItem("chapter", numbertype, langtype, true);
295 } else if (ctr == "section" && head <= 1) {
296 s << numberLabel("chapter", numbertype, langtype, head)
297 << labelItem("section", numbertype, langtype, head == 1);
298 } else if (ctr == "subsection" && head <= 2) {
299 s << numberLabel("section", numbertype, langtype, head)
300 << labelItem("subsection", numbertype, langtype, head == 2);
301 } else if (ctr == "subsubsection" && head <= 3) {
302 s << numberLabel("subsection", numbertype, langtype, head)
303 << labelItem("subsubsection", numbertype, langtype, head == 3);
304 } else if (ctr == "paragraph" && head <= 4) {
305 s << numberLabel("subsubsection", numbertype, langtype, head)
306 << labelItem("paragraph", numbertype, langtype, head == 4);
307 } else if (ctr == "subparagraph" && head <= 5) {
308 s << numberLabel("paragraph", numbertype, langtype, head)
309 << labelItem("subparagraph", numbertype, langtype, head == 5);
310 } else if (ctr == "figure" || ctr == "table") {
311 // figure, table, ...
312 lyxerr << "Counter:" << ctr << endl;
313 s << numberLabel("chapter", numbertype, langtype, head)
314 << labelItem(ctr, numbertype, langtype, head == 1);
317 } else if (numbertype == "enumeration") {
323 if (langtype == "hebrew") {
324 ei << '.' << value("enumi");
325 eii << '(' << hebrewCounter(value("enumii")) << ')';
326 eiii << '.' << romanCounter(value("enumiii"));
327 eiv << '.' << alphaCounter(value("enumiv"));
329 ei << value("enumi") << '.';
330 eii << '(' << loweralphaCounter(value("enumii")) << ')';
331 eiii << romanCounter(value("enumiii")) << '.';
332 eiv << alphaCounter(value("enumiv")) << '.';
334 if (ctr == "enumii") {
336 } else if (ctr == "enumi") {
338 } else if (ctr == "enumiii") {
340 } else if (ctr == "enumiv") {
345 return STRCONV(s.str());