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