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