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