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