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");
101 void Counters::newCounter(string const & newc)
103 // First check if newc already exist
104 CounterList::iterator cit = counterList.find(newc);
105 // if already exist give warning and return
106 if (cit != counterList.end()) {
107 lyxerr << "The new counter already exists." << endl;
111 cit = counterList.find(newc);
112 cit->second.setMaster("");
116 void Counters::newCounter(string const & newc, string const & masterc)
118 // First check if newc already exists
119 CounterList::iterator cit = counterList.find(newc);
120 // if already existant give warning and return
121 if (cit != counterList.end()) {
122 lyxerr << "The new counter already exists." << endl;
125 // then check if masterc exists
126 CounterList::iterator it = counterList.find(masterc);
127 // if not give warning and return
128 if (it == counterList.end()) {
129 lyxerr << "The master counter does not exist." << endl;
134 cit = counterList.find(newc);
135 cit->second.setMaster(masterc);
139 void Counters::set(string const & ctr, int val)
141 CounterList::iterator it = counterList.find(ctr);
142 if (it == counterList.end()) {
143 lyxerr << "set: Counter does not exist: " << ctr << endl;
150 void Counters::addto(string const & ctr, int val)
152 CounterList::iterator it = counterList.find(ctr);
153 if (it == counterList.end()) {
154 lyxerr << "addto: Counter does not exist: " << ctr << endl;
157 it->second.addto(val);
161 int Counters::value(string const & ctr) const
163 CounterList::const_iterator cit = counterList.find(ctr);
164 if (cit == counterList.end()) {
165 lyxerr << "value: Counter does not exist: " << ctr << endl;
168 return cit->second.value();
172 void Counters::step(string const & ctr)
174 CounterList::iterator it = counterList.find(ctr);
175 if (it == counterList.end()) {
176 lyxerr << "step: Counter does not exist: " << ctr << endl;
181 it = counterList.begin();
182 CounterList::iterator end = counterList.end();
183 for (; it != end; ++it) {
184 if (it->second.master() == ctr) {
191 void Counters::reset()
193 CounterList::iterator it = counterList.begin();
194 CounterList::iterator end = counterList.end();
195 for (; it != end; ++it) {
201 void Counters::reset(string const & match)
203 lyx::Assert(!match.empty());
205 CounterList::iterator it = counterList.begin();
206 CounterList::iterator end = counterList.end();
207 for (; it != end; ++it) {
208 if (it->first.find(match) != string::npos)
214 void Counters::copy(Counters & from, Counters & to, string const & match)
216 CounterList::iterator it = counterList.begin();
217 CounterList::iterator end = counterList.end();
218 for (; it != end; ++it) {
219 if (it->first.find(match) != string::npos || match == "") {
220 to.set(it->first, from.value(it->first));
229 char loweralphaCounter(int n)
239 char alphaCounter(int n)
249 char hebrewCounter(int n)
251 static const char hebrew[22] = {
252 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
253 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
264 string const romanCounter(int n)
266 static char const * roman[20] = {
267 "i", "ii", "iii", "iv", "v",
268 "vi", "vii", "viii", "ix", "x",
269 "xi", "xii", "xiii", "xiv", "xv",
270 "xvi", "xvii", "xviii", "xix", "xx"
281 string Counters::labelItem(string const & ctr,
282 string const & numbertype,
283 string const & langtype,
289 CounterList::iterator it = counterList.find(ctr);
290 if (it == counterList.end()) {
291 lyxerr << "Counter does not exist." << endl;
296 s << "." << value(ctr);
298 if (numbertype == "sectioning" || numbertype == "appendix") {
299 if (numbertype == "appendix") {
300 if (langtype == "hebrew") {
301 o << hebrewCounter(value(ctr));
303 o << alphaCounter(value(ctr));
305 } else o << value(ctr);
313 string Counters::numberLabel(string const & ctr,
314 string const & numbertype,
315 string const & langtype,
319 if (numbertype == "sectioning" || numbertype == "appendix") {
320 if (ctr == "chapter" && head == 0) {
321 s << labelItem("chapter", numbertype, langtype, true);
322 } else if (ctr == "section" && head <= 1) {
323 s << numberLabel("chapter", numbertype, langtype, head)
324 << labelItem("section", numbertype, langtype, head == 1);
325 } else if (ctr == "subsection" && head <= 2) {
326 s << numberLabel("section", numbertype, langtype, head)
327 << labelItem("subsection", numbertype, langtype, head == 2);
328 } else if (ctr == "subsubsection" && head <= 3) {
329 s << numberLabel("subsection", numbertype, langtype, head)
330 << labelItem("subsubsection", numbertype, langtype, head == 3);
331 } else if (ctr == "paragraph" && head <= 4) {
332 s << numberLabel("subsubsection", numbertype, langtype, head)
333 << labelItem("paragraph", numbertype, langtype, head == 4);
334 } else if (ctr == "subparagraph" && head <= 5) {
335 s << numberLabel("paragraph", numbertype, langtype, head)
336 << labelItem("subparagraph", numbertype, langtype, head == 5);
337 } else if (ctr == "figure" || ctr == "table") {
338 // figure, table, ...
339 lyxerr << "Counter:" << ctr << endl;
340 s << numberLabel("chapter", numbertype, langtype, head)
341 << labelItem(ctr, numbertype, langtype, head == 1);
344 } else if (numbertype == "enumeration") {
345 ostringstream ei, eii, eiii, eiv;
346 //string ei, eiii, eiv;
348 if (langtype == "hebrew") {
349 ei << '.' << value("enumi");
350 eii << '(' << hebrewCounter(value("enumii")) << ')';
351 eiii << '.' << romanCounter(value("enumiii"));
352 eiv << '.' << alphaCounter(value("enumiv"));
354 ei << value("enumi") << '.';
355 eii << '(' << loweralphaCounter(value("enumii")) << ')';
356 eiii << romanCounter(value("enumiii")) << '.';
357 eiv << alphaCounter(value("enumiv")) << '.';
359 if (ctr == "enumii") {
361 } else if (ctr == "enumi") {
363 } else if (ctr == "enumiii") {
365 } else if (ctr == "enumiv") {