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 sects.push_back("chapter");
87 sects.push_back("section");
88 sects.push_back("subsection");
89 sects.push_back("subsubsection");
90 sects.push_back("paragraph");
91 sects.push_back("subparagraph");
93 // Enumeration counters:
95 newCounter("enumii", "enumi");
96 newCounter("enumiii", "enumii");
97 newCounter("enumiv", "enumiii");
99 enums.push_back("enumi");
100 enums.push_back("enumii");
101 enums.push_back("enumiii");
102 enums.push_back("enumiv");
105 newCounter("bibitem");
108 newCounter("Figure");
113 void Counters::newCounter(string const & newc)
115 // First check if newc already exist
116 CounterList::iterator cit = counterList.find(newc);
117 // if already exist give warning and return
118 if (cit != counterList.end()) {
119 lyxerr << "The new counter already exists." << endl;
123 cit = counterList.find(newc);
124 cit->second.setMaster("");
128 void Counters::newCounter(string const & newc, string const & masterc)
130 // First check if newc already exists
131 CounterList::iterator cit = counterList.find(newc);
132 // if already existant give warning and return
133 if (cit != counterList.end()) {
134 lyxerr << "The new counter already exists." << endl;
137 // then check if masterc exists
138 CounterList::iterator it = counterList.find(masterc);
139 // if not give warning and return
140 if (it == counterList.end()) {
141 lyxerr << "The master counter does not exist." << endl;
146 cit = counterList.find(newc);
147 cit->second.setMaster(masterc);
151 void Counters::set(string const & ctr, int val)
153 CounterList::iterator it = counterList.find(ctr);
154 if (it == counterList.end()) {
155 lyxerr << "set: Counter does not exist: " << ctr << endl;
162 void Counters::addto(string const & ctr, int val)
164 CounterList::iterator it = counterList.find(ctr);
165 if (it == counterList.end()) {
166 lyxerr << "addto: Counter does not exist: " << ctr << endl;
169 it->second.addto(val);
173 int Counters::value(string const & ctr) const
175 CounterList::const_iterator cit = counterList.find(ctr);
176 if (cit == counterList.end()) {
177 lyxerr << "value: Counter does not exist: " << ctr << endl;
180 return cit->second.value();
184 void Counters::step(string const & ctr)
186 CounterList::iterator it = counterList.find(ctr);
187 if (it == counterList.end()) {
188 lyxerr << "step: Counter does not exist: " << ctr << endl;
193 it = counterList.begin();
194 CounterList::iterator end = counterList.end();
195 for (; it != end; ++it) {
196 if (it->second.master() == ctr) {
203 void Counters::reset()
205 CounterList::iterator it = counterList.begin();
206 CounterList::iterator end = counterList.end();
207 for (; it != end; ++it) {
213 void Counters::reset(string const & match)
215 lyx::Assert(!match.empty());
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)
226 void Counters::copy(Counters & from, Counters & to, string const & match)
228 CounterList::iterator it = counterList.begin();
229 CounterList::iterator end = counterList.end();
230 for (; it != end; ++it) {
231 if (it->first.find(match) != string::npos || match == "") {
232 to.set(it->first, from.value(it->first));
241 char loweralphaCounter(int n)
251 char alphaCounter(int n)
261 char hebrewCounter(int n)
263 static const char hebrew[22] = {
264 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
265 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
276 string const romanCounter(int n)
278 static char const * roman[20] = {
279 "i", "ii", "iii", "iv", "v",
280 "vi", "vii", "viii", "ix", "x",
281 "xi", "xii", "xiii", "xiv", "xv",
282 "xvi", "xvii", "xviii", "xix", "xx"
293 string Counters::labelItem(string const & ctr,
294 string const & numbertype,
295 string const & langtype,
299 CounterList::iterator it = counterList.find(ctr);
300 if (it == counterList.end()) {
301 lyxerr << "Counter does not exist." << endl;
304 string mstr = it->second.master();
306 s << "." << value(ctr);
308 if (numbertype == "sectioning" || numbertype == "appendix") {
309 if (numbertype == "appendix") {
310 if (langtype == "hebrew") {
311 o << hebrewCounter(value(ctr));
313 o << alphaCounter(value(ctr));
315 } else o << value(ctr);
323 string Counters::numberLabel(string const & ctr,
324 string const & numbertype,
325 string const & langtype,
329 if (numbertype == "sectioning" || numbertype == "appendix") {
330 if (ctr == "chapter" && head == 0) {
331 s << labelItem("chapter", numbertype, langtype, true);
332 } else if (ctr == "section" && head <= 1) {
333 s << numberLabel("chapter", numbertype, langtype, head)
334 << labelItem("section", numbertype, langtype, head == 1);
335 } else if (ctr == "subsection" && head <= 2) {
336 s << numberLabel("section", numbertype, langtype, head)
337 << labelItem("subsection", numbertype, langtype, head == 2);
338 } else if (ctr == "subsubsection" && head <= 3) {
339 s << numberLabel("subsection", numbertype, langtype, head)
340 << labelItem("subsubsection", numbertype, langtype, head == 3);
341 } else if (ctr == "paragraph" && head <= 4) {
342 s << numberLabel("subsubsection", numbertype, langtype, head)
343 << labelItem("paragraph", numbertype, langtype, head == 4);
344 } else if (ctr == "subparagraph" && head <= 5) {
345 s << numberLabel("paragraph", numbertype, langtype, head)
346 << labelItem("subparagraph", numbertype, langtype, head == 5);
347 } else if (ctr == "Figure" || ctr == "Table") {
348 // figure, table, ...
349 lyxerr << "Counter:" << ctr << endl;
350 s << numberLabel("chapter", numbertype, langtype, head)
351 << labelItem(ctr, numbertype, langtype, head == 1);
354 } else if (numbertype == "enumeration") {
355 ostringstream ei, eii, eiii, eiv;
356 //string ei, eiii, eiv;
358 if (langtype == "hebrew") {
359 ei << '.' << value("enumi");
360 eii << '(' << hebrewCounter(value("enumii")) << ')';
361 eiii << '.' << romanCounter(value("enumiii"));
362 eiv << '.' << alphaCounter(value("enumiv"));
364 ei << value("enumi") << '.';
365 eii << '(' << loweralphaCounter(value("enumii")) << ')';
366 eiii << romanCounter(value("enumiii")) << '.';
367 eiv << alphaCounter(value("enumiv")) << '.';
369 if (ctr == "enumii") {
371 } else if (ctr == "enumi") {
373 } else if (ctr == "enumiii") {
375 } else if (ctr == "enumiv") {