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)
225 void Counters::copy(Counters & from, Counters & to, string const & match)
227 CounterList::iterator it = counterList.begin();
228 CounterList::iterator end = counterList.end();
229 for (; it != end; ++it) {
230 if (it->first.find(match) != string::npos || match == "") {
231 to.set(it->first, from.value(it->first));
240 char loweralphaCounter(int n)
249 char alphaCounter(int n)
258 char hebrewCounter(int n)
260 static const char hebrew[22] = {
261 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
262 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
272 string const romanCounter(int n)
274 static char const * roman[20] = {
275 "i", "ii", "iii", "iv", "v",
276 "vi", "vii", "viii", "ix", "x",
277 "xi", "xii", "xiii", "xiv", "xv",
278 "xvi", "xvii", "xviii", "xix", "xx"
288 string Counters::labelItem(string const & ctr,
289 string const & numbertype,
290 string const & langtype,
294 CounterList::iterator it = counterList.find(ctr);
295 if (it == counterList.end()) {
296 lyxerr << "Counter does not exist." << endl;
299 string mstr = it->second.master();
301 s << "." << value(ctr);
303 if (numbertype == "sectioning" || numbertype == "appendix") {
304 if (numbertype == "appendix") {
305 if (langtype == "hebrew") {
306 o << hebrewCounter(value(ctr));
308 o << alphaCounter(value(ctr));
310 } else o << value(ctr);
317 string Counters::numberLabel(string const & ctr,
318 string const & numbertype,
319 string const & langtype,
323 if (numbertype == "sectioning" || numbertype == "appendix") {
324 if (ctr == "chapter" && head == 0) {
325 s << labelItem("chapter", numbertype, langtype, true);
326 } else if (ctr == "section" && head <= 1) {
327 s << numberLabel("chapter", numbertype, langtype, head)
328 << labelItem("section", numbertype, langtype, head == 1);
329 } else if (ctr == "subsection" && head <= 2) {
330 s << numberLabel("section", numbertype, langtype, head)
331 << labelItem("subsection", numbertype, langtype, head == 2);
332 } else if (ctr == "subsubsection" && head <= 3) {
333 s << numberLabel("subsection", numbertype, langtype, head)
334 << labelItem("subsubsection", numbertype, langtype, head == 3);
335 } else if (ctr == "paragraph" && head <= 4) {
336 s << numberLabel("subsubsection", numbertype, langtype, head)
337 << labelItem("paragraph", numbertype, langtype, head == 4);
338 } else if (ctr == "subparagraph" && head <= 5) {
339 s << numberLabel("paragraph", numbertype, langtype, head)
340 << labelItem("subparagraph", numbertype, langtype, head == 5);
341 } else if (ctr == "Figure" || ctr == "Table") {
342 // figure, table, ...
343 lyxerr << "Counter:" << ctr << endl;
344 s << numberLabel("chapter", numbertype, langtype, head)
345 << labelItem(ctr, numbertype, langtype, head == 1);
348 } else if (numbertype == "enumeration") {
349 ostringstream ei, eii, eiii, eiv;
350 //string ei, eiii, eiv;
352 if (langtype == "hebrew") {
353 ei << '.' << value("enumi");
354 eii << '(' << hebrewCounter(value("enumii")) << ')';
355 eiii << '.' << romanCounter(value("enumiii"));
356 eiv << '.' << alphaCounter(value("enumiv"));
358 ei << value("enumi") << '.';
359 eii << '(' << loweralphaCounter(value("enumii")) << ')';
360 eiii << romanCounter(value("enumiii")) << '.';
361 eiv << alphaCounter(value("enumiv")) << '.';
363 if (ctr == "enumii") {
365 } else if (ctr == "enumi") {
367 } else if (ctr == "enumiii") {
369 } else if (ctr == "enumiv") {