]> git.lyx.org Git - lyx.git/blob - src/counters.C
fix to #241 and #300 from John
[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 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include <config.h>
17
18 #include "counters.h"
19 #include "debug.h"
20
21 using std::endl;
22
23
24 Counter::Counter()
25 {
26         reset();
27 }
28
29
30 void Counter::set(int v)
31 {
32         value_ = v;
33 }
34
35
36 void Counter::addto(int v)
37 {
38         value_ += v;
39 }
40
41
42 int Counter::value() const
43 {
44         return value_;
45 }
46
47
48 void Counter::step()
49 {
50         ++value_;
51         onstep.emit();
52 }
53
54
55 void Counter::reset()
56 {
57         value_ = 0;
58 }
59
60
61 Counters::~Counters()
62 {
63         // We need this since we store the Counter's as pointers in
64         // the counterList.
65         for (CounterList::iterator it = counterList.begin();
66              it != counterList.end();
67              ++it)
68                 delete it->second;
69 }
70
71
72 void Counters::newCounter(string const & newc)
73 {
74         // First check if newc already exist
75         CounterList::iterator cit = counterList.find(newc);
76         // if alrady exist give warning and return
77         if (cit != counterList.end()) {
78                 lyxerr << "The new counter already exist." << endl;
79                 return;
80         }
81         counterList[newc] = new Counter;
82 }
83
84
85 void Counters::newCounter(string const & newc, string const & oldc)
86 {
87         // First check if newc already exist
88         CounterList::iterator cit = counterList.find(newc);
89         // if already existant give warning and return
90         if (cit != counterList.end()) {
91                 lyxerr << "The new counter already exist." << endl;
92                 return;
93         }
94         // then check if oldc exist
95         CounterList::iterator it = counterList.find(oldc);
96         // if not give warning and return
97         if (it == counterList.end()) {
98                 lyxerr << "The old counter does not exist." << endl;
99                 return;
100         }
101
102         Counter * tmp = new Counter;
103         it->second->onstep.connect(SigC::slot(tmp,
104                                               &Counter::reset));
105         counterList[newc] = tmp;
106 }
107
108
109 void Counters::set(string const & ctr, int val)
110 {
111         CounterList::iterator it = counterList.find(ctr);
112         if (it == counterList.end()) {
113                 lyxerr << "Counter does not exist." << endl;
114                 return;
115         }
116         it->second->set(val);
117 }
118
119
120 void Counters::addto(string const & ctr, int val)
121 {
122         CounterList::iterator it = counterList.find(ctr);
123         if (it == counterList.end()) {
124                 lyxerr << "Counter does not exist." << endl;
125                 return;
126         }
127         it->second->addto(val);
128 }
129
130
131 int Counters::value(string const & ctr) const
132 {
133         CounterList::const_iterator cit = counterList.find(ctr);
134         if (cit == counterList.end()) {
135                 lyxerr << "Counter does not exist." << endl;
136                 return 0;
137         }
138         return cit->second->value();
139 }
140
141
142 void Counters::step(string const & ctr)
143 {
144         CounterList::iterator it = counterList.find(ctr);
145         if (it == counterList.end()) {
146                 lyxerr << "Counter does not exist." << endl;
147                 return;
148         }
149         it->second->step();
150 }