]> git.lyx.org Git - lyx.git/blob - src/Counters.h
Update credits
[lyx.git] / src / Counters.h
1 // -*- C++ -*-
2 /**
3  * \file Counters.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author John Levon
10  * \author Martin Vermeer
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef COUNTERS_H
16 #define COUNTERS_H
17
18 #include "OutputEnums.h"
19
20 #include "support/docstring.h"
21
22 #include <map>
23 #include <vector>
24
25
26 namespace lyx {
27
28 class Layout;
29 class Lexer;
30
31 /// This represents a single counter.
32 class Counter {
33 public:
34         ///
35         Counter();
36         ///
37         Counter(docstring const & mc, docstring const & ls,
38                 docstring const & lsa, docstring const & prettyformat,
39                 docstring const & guiname);
40         /// \return true on success
41         bool read(Lexer & lex);
42         ///
43         void set(int v);
44         ///
45         void addto(int v);
46         ///
47         int value() const;
48         ///
49         void saveValue();
50         ///
51         void restoreValue();
52         ///
53         void step();
54         ///
55         void reset();
56         /// Returns the parent counter of this counter.
57         docstring const & parent() const;
58         /// Checks if the parent counter is cnt, and if so removes
59         /// it. This is used when a counter is deleted.
60         /// \return whether we removed the parent.
61         bool checkAndRemoveParent(docstring const & cnt);
62         /// Returns a LaTeX-like string to format the counter.
63         /** This is similar to what one gets in LaTeX when using
64          *  "\the<counter>". The \c in_appendix bool tells whether we
65          *  want the version shown in an appendix.
66          */
67         docstring const & labelString(bool in_appendix) const;
68         /// Similar, but used for formatted references in XHTML output.
69         /// E.g., for a section counter it might be "section \thesection"
70         docstring const & prettyFormat() const { return prettyformat_; }
71         ///
72         docstring const & refFormat(docstring const & prefix) const;
73         ///
74         docstring const & guiName() const { return guiname_; }
75         ///
76         docstring const & latexName() const { return latexname_; }
77
78         /// Returns a map of LaTeX-like strings to format the counter.
79         /** For each language, the string is similar to what one gets
80          *  in LaTeX when using "\the<counter>". The \c in_appendix
81          *  bool tells whether we want the version shown in an
82          *  appendix. This version does not contain any \\the<counter>
83          *  expression.
84          */
85         typedef std::map<std::string, docstring> StringMap;
86         StringMap & flatLabelStrings(bool in_appendix) const;
87 private:
88         ///
89         int value_;
90         /// This is actually one less than the initial value, since the
91         /// counter is always stepped before being used.
92         int initial_value_;
93         ///
94         int saved_value_;
95         /// contains parent counter name.
96         /** The parent counter is the counter that, if stepped
97          *  (incremented) zeroes this counter. E.g. "subsection"'s
98          *  parent is "section".
99          */
100         docstring parent_;
101         /// Contains a LaTeX-like string to format the counter.
102         docstring labelstring_;
103         /// The same as labelstring_, but in appendices.
104         docstring labelstringappendix_;
105         /// Similar, but used for formatted references in XHTML output
106         docstring prettyformat_;
107         ///
108         std::map<docstring, docstring> ref_formats_;
109         ///
110         docstring guiname_;
111         /// The name used for the counter in LaTeX
112         docstring latexname_;
113         /// Cache of the labelstring with \\the<counter> expressions expanded,
114         /// indexed by language
115         mutable StringMap flatlabelstring_;
116         /// Cache of the appendix labelstring with \\the<counter> expressions expanded,
117         /// indexed by language
118         mutable StringMap flatlabelstringappendix_;
119 };
120
121
122 /// This is a class of (La)TeX type counters.
123 /// Every instantiation is an array of counters of type Counter.
124 class Counters {
125 public:
126         /// NOTE Do not call this in an attempt to clear the counters.
127         /// That will wipe out all the information we have about them
128         /// from the document class (e.g., which ones are defined).
129         /// Instead, call Counters::reset().
130         Counters();
131         /// Add new counter newc having parentc its parent,
132         /// ls as its label, and lsa as its appendix label.
133         void newCounter(docstring const & newc,
134                         docstring const & parentc,
135                         docstring const & ls,
136                         docstring const & lsa,
137                         docstring const & prettyformat,
138                         docstring const & guiname);
139         /// Checks whether the given counter exists.
140         bool hasCounter(docstring const & c) const;
141         /// reads the counter name
142         /// \param makeNew whether to make a new counter if one
143         ///        doesn't already exist
144         /// \return true on success
145         bool read(Lexer & lex, docstring const & name, bool makenew);
146         ///
147         void set(docstring const & ctr, int val);
148         ///
149         void addto(docstring const & ctr, int val);
150         ///
151         int value(docstring const & ctr) const;
152         ///
153         void saveValue(docstring const & ctr) const;
154         ///
155         void restoreValue(docstring const & ctr) const;
156         /// Reset recursively all the counters that are children of the one named by \c ctr.
157         void resetChildren(docstring const & ctr);
158         /// Increment by one the parent of counter named by \c ctr.
159         /// This also resets the counter named by \c ctr.
160         /// \param utype determines whether we track the counters.
161         void stepParent(docstring const & ctr, UpdateType utype);
162         /// Increment by one counter named by \c ctr, and zeroes child
163         /// counter(s) for which it is the parent.
164         /// \param utype determines whether we track the counters.
165         void step(docstring const & ctr, UpdateType utype);
166         /// Reset all counters, and all the internal data structures
167         /// used for keeping track of their values.
168         void reset();
169         /// Reset counters matched by match string.
170         void reset(docstring const & match);
171         /// Copy counter \p cnt to \p newcnt.
172         bool copy(docstring const & cnt, docstring const & newcnt);
173         /// Remove counter \p cnt.
174         bool remove(docstring const & cnt);
175         /** returns the expanded string representation of counter \c
176          *  c. The \c lang code is used to translate the string.
177          */
178         docstring theCounter(docstring const & c,
179                              std::string const & lang) const;
180         /** Replace in \c format all the LaTeX-like macros that depend
181          * on counters. The \c lang code is used to translate the
182          * string.
183          */
184         docstring counterLabel(docstring const & format,
185                                std::string const & lang) const;
186         /// returns a formatted version of the counter, using the
187         /// format given by Counter::prettyFormat().
188         docstring prettyCounter(docstring const & cntr,
189                                std::string const & lang) const;
190         /// returns a formatted version of the counter, using the
191         /// format given by Counter::prettyFormat().
192         docstring formattedCounter(docstring const & cntr,
193                                         docstring const & prefix,
194                                         std::string const & lang) const;
195         ///
196         docstring const & guiName(docstring const & cntr) const;
197         ///
198         docstring const & latexName(docstring const & cntr) const;
199         /// Are we in appendix?
200         bool appendix() const { return appendix_; }
201         /// Set the state variable indicating whether we are in appendix.
202         void appendix(bool a) { appendix_ = a; }
203         /// Returns the current enclosing float.
204         std::string const & current_float() const { return current_float_; }
205         /// Sets the current enclosing float.
206         void current_float(std::string const & f) { current_float_ = f; }
207         /// Are we in a subfloat?
208         bool isSubfloat() const { return subfloat_; }
209         /// Set the state variable indicating whether we are in a subfloat.
210         void isSubfloat(bool s) { subfloat_ = s; }
211         /// Are we in a longtable?
212         bool isLongtable() const { return longtable_; }
213         /// Set the state variable indicating whether we are in a longtable.
214         void isLongtable(bool s) { longtable_ = s; }
215
216         /// \name refstepcounter
217         // @{
218         /// The currently active counter, so far as references go.
219         /// We're trying to track \refstepcounter in LaTeX, more or less.
220         /// Note that this may be empty.
221         docstring currentCounter() const;
222         /// Called during updateBuffer() as we go through various paragraphs,
223         /// to track the layouts as we go through.
224         void setActiveLayout(Layout const & lay);
225         /// Also for updateBuffer().
226         /// Call this when entering things like footnotes, where there is now
227         /// no "last layout" and we want to restore the "last layout" on exit.
228         void clearLastLayout() { layout_stack_.push_back(nullptr); }
229         /// Call this when exiting things like footnotes.
230         void restoreLastLayout() { layout_stack_.pop_back(); }
231         ///
232         void saveLastCounter()
233                 { counter_stack_.push_back(counter_stack_.back()); }
234         ///
235         void restoreLastCounter() { counter_stack_.pop_back(); }
236         // @}
237         ///
238         std::vector<docstring> listOfCounters() const;
239 private:
240         /** expands recursively any \\the<counter> macro in the
241          *  labelstring of \c counter.  The \c lang code is used to
242          *  translate the string.
243          */
244         docstring flattenLabelString(docstring const & counter, bool in_appendix,
245                                      std::string const &lang,
246                                      std::vector<docstring> & callers) const;
247         /// Returns the value of the counter according to the
248         /// numbering scheme numbertype.
249         /** Available numbering schemes are arabic (1, 2,...), roman
250          *  (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
251          *  B,...) and hebrew.
252          */
253         docstring labelItem(docstring const & ctr,
254                             docstring const & numbertype) const;
255         /// Used for managing the counter_stack_.
256         // @{
257         void beginEnvironment();
258         void endEnvironment();
259         // @}
260         /// Maps counter (layout) names to actual counters.
261         typedef std::map<docstring, Counter> CounterList;
262         /// Instantiate.
263         CounterList counterList_;
264         /// Are we in an appendix?
265         bool appendix_;
266         /// The current enclosing float.
267         std::string current_float_;
268         /// Are we in a subfloat?
269         bool subfloat_;
270         /// Are we in a longtable?
271         bool longtable_;
272         /// Used to keep track of active counters.
273         std::vector<docstring> counter_stack_;
274         /// Same, but for last layout.
275         std::vector<Layout const *> layout_stack_;
276 };
277
278 } // namespace lyx
279
280 #endif