]> git.lyx.org Git - lyx.git/blob - src/counters.C
remove mention of lyxrc from the splash
[lyx.git] / src / counters.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #include "counters.h"
15 #include "debug.h"
16 #include "Lsstream.h"
17
18 #include "support/lstrings.h"
19 #include "support/LAssert.h"
20
21 using std::endl;
22 using std::vector;
23
24
25 Counter::Counter()
26 {
27         reset();
28 }
29
30
31 void Counter::set(int v)
32 {
33         value_ = v;
34 }
35
36
37 void Counter::addto(int v)
38 {
39         value_ += v;
40 }
41
42
43 int Counter::value() const
44 {
45         return value_;
46 }
47
48
49 void Counter::step()
50 {
51         ++value_;
52 }
53
54
55 void Counter::reset()
56 {
57         value_ = 0;
58 }
59
60
61 string Counter::master() const
62 {
63         return master_;
64 }
65
66
67 void Counter::setMaster(string const & m)
68 {
69         master_ = m;
70 }
71
72
73
74 void Counters::newCounter(string const & newc)
75 {
76         // First check if newc already exist
77         CounterList::iterator cit = counterList.find(newc);
78         // if already exist give warning and return
79         if (cit != counterList.end()) {
80                 lyxerr << "The new counter already exists." << endl;
81                 return;
82         }
83         counterList[newc];
84 }
85
86
87 void Counters::newCounter(string const & newc, string const & masterc)
88 {
89         // First check if newc already exists
90         CounterList::iterator cit = counterList.find(newc);
91         // if already existant give warning and return
92         if (cit != counterList.end()) {
93                 lyxerr << "The new counter already exists." << endl;
94                 return;
95         }
96         // then check if masterc exists
97         CounterList::iterator it = counterList.find(masterc);
98         // if not give warning and return
99         if (it == counterList.end()) {
100                 lyxerr << "The master counter does not exist." << endl;
101                 return;
102         }
103
104         counterList[newc].setMaster(masterc);
105 }
106
107
108 void Counters::set(string const & ctr, int val)
109 {
110         CounterList::iterator it = counterList.find(ctr);
111         if (it == counterList.end()) {
112                 lyxerr << "set: Counter does not exist: " << ctr << endl;
113                 return;
114         }
115         it->second.set(val);
116 }
117
118
119 void Counters::addto(string const & ctr, int val)
120 {
121         CounterList::iterator it = counterList.find(ctr);
122         if (it == counterList.end()) {
123                 lyxerr << "addto: Counter does not exist: " << ctr << endl;
124                 return;
125         }
126         it->second.addto(val);
127 }
128
129
130 int Counters::value(string const & ctr) const
131 {
132         CounterList::const_iterator cit = counterList.find(ctr);
133         if (cit == counterList.end()) {
134                 lyxerr << "value: Counter does not exist: " << ctr << endl;
135                 return 0;
136         }
137         return cit->second.value();
138 }
139
140
141 void Counters::step(string const & ctr)
142 {
143         CounterList::iterator it = counterList.find(ctr);
144         if (it == counterList.end()) {
145                 lyxerr << "step: Counter does not exist: " << ctr << endl;
146                 return;
147         }
148
149         it->second.step();
150         it = counterList.begin();
151         CounterList::iterator end = counterList.end();
152         for (; it != end; ++it) {
153                 if (it->second.master() == ctr) {
154                         it->second.reset();
155                 }
156         }
157 }
158
159
160 void Counters::reset()
161 {
162         CounterList::iterator it = counterList.begin();
163         CounterList::iterator end = counterList.end();
164         for (; it != end; ++it) {
165                 it->second.reset();
166         }
167 }
168
169
170 void Counters::reset(string const & match)
171 {
172         lyx::Assert(!match.empty());
173
174         CounterList::iterator it = counterList.begin();
175         CounterList::iterator end = counterList.end();
176         for (; it != end; ++it) {
177                 if (it->first.find(match) != string::npos)
178                         it->second.reset();
179         }
180 }
181
182
183 void Counters::copy(Counters & from, Counters & to, string const & match)
184 {
185         CounterList::iterator it = counterList.begin();
186         CounterList::iterator end = counterList.end();
187         for (; it != end; ++it) {
188                 if (it->first.find(match) != string::npos || match == "") {
189                         to.set(it->first, from.value(it->first));
190                 }
191         }
192 }
193
194
195 namespace {
196
197 inline
198 char loweralphaCounter(int n)
199 {
200         if (n < 1 || n > 26)
201                 return '?';
202         else
203                 return 'a' + n - 1;
204 }
205
206
207 inline
208 char alphaCounter(int n)
209 {
210         if (n < 1 || n > 26)
211                 return '?';
212         else
213                 return 'A' + n - 1;
214 }
215
216
217 inline
218 char hebrewCounter(int n)
219 {
220         static const char hebrew[22] = {
221                 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
222                 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
223                 '÷', 'ø', 'ù', 'ú'
224         };
225         if (n < 1 || n > 22)
226                 return '?';
227         else
228                 return hebrew[n-1];
229 }
230
231
232 inline
233 string const romanCounter(int n)
234 {
235         static char const * roman[20] = {
236                 "i",   "ii",  "iii", "iv", "v",
237                 "vi",  "vii", "viii", "ix", "x",
238                 "xi",  "xii", "xiii", "xiv", "xv",
239                 "xvi", "xvii", "xviii", "xix", "xx"
240         };
241         if (n < 1 || n > 20)
242                 return "??";
243         else
244                 return roman[n-1];
245 }
246
247 } // namespace anon
248
249
250 string Counters::labelItem(string const & ctr,
251                            string const & numbertype,
252                            string const & langtype,
253                            bool first)
254 {
255         ostringstream s;
256         ostringstream o;
257
258         CounterList::iterator it = counterList.find(ctr);
259         if (it == counterList.end()) {
260                 lyxerr << "Counter does not exist." << endl;
261                 return string();
262         }
263
264         if (!first) {
265                 s << '.' << value(ctr);
266         } else {
267                 if (numbertype == "sectioning" || numbertype == "appendix") {
268                         if (numbertype == "appendix") {
269                                 if (langtype == "hebrew") {
270                                         o << hebrewCounter(value(ctr));
271                                 } else {
272                                         o << alphaCounter(value(ctr));
273                                 }
274                         } else o << value(ctr);
275                 }
276                 s << o.str();
277         }
278
279         return STRCONV(s.str());
280 }
281
282
283 string Counters::numberLabel(string const & ctr,
284                              string const & numbertype,
285                              string const & langtype,
286                              int head)
287 {
288         ostringstream s;
289
290         if (numbertype == "sectioning" || numbertype == "appendix") {
291                 if (ctr == "chapter" && head == 0) {
292                         s << labelItem("chapter", numbertype, langtype, true);
293                 } else if (ctr == "section" && head <= 1) {
294                         s << numberLabel("chapter", numbertype, langtype, head)
295                           << labelItem("section", numbertype, langtype, head == 1);
296                 } else if (ctr == "subsection" && head <= 2) {
297                         s << numberLabel("section", numbertype, langtype, head)
298                           << labelItem("subsection", numbertype, langtype, head == 2);
299                 } else if (ctr == "subsubsection" && head <= 3) {
300                         s << numberLabel("subsection", numbertype, langtype, head)
301                           << labelItem("subsubsection", numbertype, langtype, head == 3);
302                 } else if (ctr == "paragraph" && head <= 4) {
303                         s << numberLabel("subsubsection", numbertype, langtype, head)
304                           << labelItem("paragraph", numbertype, langtype, head == 4);
305                 } else if (ctr == "subparagraph" && head <= 5) {
306                         s << numberLabel("paragraph", numbertype, langtype, head)
307                           << labelItem("subparagraph", numbertype, langtype, head == 5);
308                 } else if (ctr == "figure" || ctr == "table") {
309                         // figure, table, ...
310                         lyxerr << "Counter:" << ctr << endl;
311                         s << numberLabel("chapter", numbertype, langtype, head)
312                           << labelItem(ctr, numbertype, langtype, head == 1);
313                 }
314
315         } else if (numbertype == "enumeration") {
316                 ostringstream ei;
317                 ostringstream eii;
318                 ostringstream eiii;
319                 ostringstream eiv;
320
321                 if (langtype == "hebrew") {
322                         ei << '.' << value("enumi");
323                         eii << '(' << hebrewCounter(value("enumii")) << ')';
324                         eiii << '.' << romanCounter(value("enumiii"));
325                         eiv << '.' << alphaCounter(value("enumiv"));
326                 } else {
327                         ei << value("enumi") << '.';
328                         eii << '(' << loweralphaCounter(value("enumii")) << ')';
329                         eiii << romanCounter(value("enumiii")) << '.';
330                         eiv << alphaCounter(value("enumiv")) << '.';
331                 }
332                 if (ctr == "enumii") {
333                         s << eii.str();
334                 } else if (ctr == "enumi") {
335                         s << ei.str();
336                 } else if (ctr == "enumiii") {
337                         s << eiii.str();
338                 } else if (ctr == "enumiv") {
339                         s << eiv.str();
340                 }
341         }
342
343         return STRCONV(s.str());
344 }