1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
10 * ====================================================== */
13 #pragma implementation
20 #include "support/lstrings.h"
21 #include "support/LAssert.h"
33 void Counter::set(int v)
39 void Counter::addto(int v)
45 int Counter::value() const
62 string Counter::master() const
67 void Counter::setMaster(string const & m)
75 // Ehh, should this take a textclass arg?
77 // Sectioning counters:
79 newCounter("chapter");
80 newCounter("section", "chapter");
81 newCounter("subsection", "section");
82 newCounter("subsubsection", "subsection");
83 newCounter("paragraph", "subsubsection");
84 newCounter("subparagraph", "paragraph");
86 // Enumeration counters:
88 newCounter("enumii", "enumi");
89 newCounter("enumiii", "enumii");
90 newCounter("enumiv", "enumiii");
93 newCounter("bibitem");
98 newCounter("algorithm");
102 void Counters::newCounter(string const & newc)
104 // First check if newc already exist
105 CounterList::iterator cit = counterList.find(newc);
106 // if already exist give warning and return
107 if (cit != counterList.end()) {
108 lyxerr << "The new counter already exists." << endl;
112 cit = counterList.find(newc);
113 cit->second.setMaster("");
117 void Counters::newCounter(string const & newc, string const & masterc)
119 // First check if newc already exists
120 CounterList::iterator cit = counterList.find(newc);
121 // if already existant give warning and return
122 if (cit != counterList.end()) {
123 lyxerr << "The new counter already exists." << endl;
126 // then check if masterc exists
127 CounterList::iterator it = counterList.find(masterc);
128 // if not give warning and return
129 if (it == counterList.end()) {
130 lyxerr << "The master counter does not exist." << endl;
135 cit = counterList.find(newc);
136 cit->second.setMaster(masterc);
140 void Counters::set(string const & ctr, int val)
142 CounterList::iterator it = counterList.find(ctr);
143 if (it == counterList.end()) {
144 lyxerr << "set: Counter does not exist: " << ctr << endl;
151 void Counters::addto(string const & ctr, int val)
153 CounterList::iterator it = counterList.find(ctr);
154 if (it == counterList.end()) {
155 lyxerr << "addto: Counter does not exist: " << ctr << endl;
158 it->second.addto(val);
162 int Counters::value(string const & ctr) const
164 CounterList::const_iterator cit = counterList.find(ctr);
165 if (cit == counterList.end()) {
166 lyxerr << "value: Counter does not exist: " << ctr << endl;
169 return cit->second.value();
173 void Counters::step(string const & ctr)
175 CounterList::iterator it = counterList.find(ctr);
176 if (it == counterList.end()) {
177 lyxerr << "step: Counter does not exist: " << ctr << endl;
182 it = counterList.begin();
183 CounterList::iterator end = counterList.end();
184 for (; it != end; ++it) {
185 if (it->second.master() == ctr) {
192 void Counters::reset()
194 CounterList::iterator it = counterList.begin();
195 CounterList::iterator end = counterList.end();
196 for (; it != end; ++it) {
202 void Counters::reset(string const & match)
204 lyx::Assert(!match.empty());
206 CounterList::iterator it = counterList.begin();
207 CounterList::iterator end = counterList.end();
208 for (; it != end; ++it) {
209 if (it->first.find(match) != string::npos)
215 void Counters::copy(Counters & from, Counters & to, string const & match)
217 CounterList::iterator it = counterList.begin();
218 CounterList::iterator end = counterList.end();
219 for (; it != end; ++it) {
220 if (it->first.find(match) != string::npos || match == "") {
221 to.set(it->first, from.value(it->first));
230 char loweralphaCounter(int n)
240 char alphaCounter(int n)
250 char hebrewCounter(int n)
252 static const char hebrew[22] = {
253 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
254 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
265 string const romanCounter(int n)
267 static char const * roman[20] = {
268 "i", "ii", "iii", "iv", "v",
269 "vi", "vii", "viii", "ix", "x",
270 "xi", "xii", "xiii", "xiv", "xv",
271 "xvi", "xvii", "xviii", "xix", "xx"
282 string Counters::labelItem(string const & ctr,
283 string const & numbertype,
284 string const & langtype,
290 CounterList::iterator it = counterList.find(ctr);
291 if (it == counterList.end()) {
292 lyxerr << "Counter does not exist." << endl;
297 s << "." << value(ctr);
299 if (numbertype == "sectioning" || numbertype == "appendix") {
300 if (numbertype == "appendix") {
301 if (langtype == "hebrew") {
302 o << hebrewCounter(value(ctr));
304 o << alphaCounter(value(ctr));
306 } else o << value(ctr);
314 string Counters::numberLabel(string const & ctr,
315 string const & numbertype,
316 string const & langtype,
320 if (numbertype == "sectioning" || numbertype == "appendix") {
321 if (ctr == "chapter" && head == 0) {
322 s << labelItem("chapter", numbertype, langtype, true);
323 } else if (ctr == "section" && head <= 1) {
324 s << numberLabel("chapter", numbertype, langtype, head)
325 << labelItem("section", numbertype, langtype, head == 1);
326 } else if (ctr == "subsection" && head <= 2) {
327 s << numberLabel("section", numbertype, langtype, head)
328 << labelItem("subsection", numbertype, langtype, head == 2);
329 } else if (ctr == "subsubsection" && head <= 3) {
330 s << numberLabel("subsection", numbertype, langtype, head)
331 << labelItem("subsubsection", numbertype, langtype, head == 3);
332 } else if (ctr == "paragraph" && head <= 4) {
333 s << numberLabel("subsubsection", numbertype, langtype, head)
334 << labelItem("paragraph", numbertype, langtype, head == 4);
335 } else if (ctr == "subparagraph" && head <= 5) {
336 s << numberLabel("paragraph", numbertype, langtype, head)
337 << labelItem("subparagraph", numbertype, langtype, head == 5);
338 } else if (ctr == "figure" || ctr == "table") {
339 // figure, table, ...
340 lyxerr << "Counter:" << ctr << endl;
341 s << numberLabel("chapter", numbertype, langtype, head)
342 << labelItem(ctr, numbertype, langtype, head == 1);
345 } else if (numbertype == "enumeration") {
346 ostringstream ei, eii, eiii, eiv;
347 //string ei, eiii, eiv;
349 if (langtype == "hebrew") {
350 ei << '.' << value("enumi");
351 eii << '(' << hebrewCounter(value("enumii")) << ')';
352 eiii << '.' << romanCounter(value("enumiii"));
353 eiv << '.' << alphaCounter(value("enumiv"));
355 ei << value("enumi") << '.';
356 eii << '(' << loweralphaCounter(value("enumii")) << ')';
357 eiii << romanCounter(value("enumiii")) << '.';
358 eiv << alphaCounter(value("enumiv")) << '.';
360 if (ctr == "enumii") {
362 } else if (ctr == "enumi") {
364 } else if (ctr == "enumiii") {
366 } else if (ctr == "enumiv") {