]> git.lyx.org Git - lyx.git/blob - src/Counters.h
Move several common types to support/types.h
[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         /// 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 parent counter name.
91         /** The parent counter is the counter that, if stepped
92          *  (incremented) zeroes this counter. E.g. "subsection"'s
93          *  parent is "section".
94          */
95         docstring parent_;
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 parentc its parent,
123         /// ls as its label, and lsa as its appendix label.
124         void newCounter(docstring const & newc,
125                         docstring const & parentc,
126                         docstring const & ls,
127                         docstring const & lsa,
128                         docstring const & guiname);
129         /// Checks whether the given counter exists.
130         bool hasCounter(docstring const & c) const;
131         /// reads the counter name
132         /// \param makeNew whether to make a new counter if one
133         ///        doesn't already exist
134         /// \return true on success
135         bool read(Lexer & lex, docstring const & name, bool makenew);
136         ///
137         void set(docstring const & ctr, int val);
138         ///
139         void addto(docstring const & ctr, int val);
140         ///
141         int value(docstring const & ctr) const;
142         ///
143         void saveValue(docstring const & ctr) const;
144         ///
145         void restoreValue(docstring const & ctr) const;
146         /// Reset recursively all the counters that are children of the one named by \c ctr.
147         void resetChildren(docstring const & ctr);
148         /// Increment by one the parent of counter named by \c ctr.
149         /// This also resets the counter named by \c ctr.
150         /// \param utype determines whether we track the counters.
151         void stepParent(docstring const & ctr, UpdateType utype);
152         /// Increment by one counter named by \c ctr, and zeroes child
153         /// counter(s) for which it is the parent.
154         /// \param utype determines whether we track the counters.
155         void step(docstring const & ctr, UpdateType utype);
156         /// Reset all counters, and all the internal data structures
157         /// used for keeping track of their values.
158         void reset();
159         /// Reset counters matched by match string.
160         void reset(docstring const & match);
161         /// Remove counter \p cnt.
162         bool remove(docstring const & cnt);
163         /// Copy counters whose name matches match from the &from to
164         /// the &to array of counters. Empty string matches all.
165         void copy(Counters const & from, Counters & to,
166                   docstring const & match = docstring());
167         /** returns the expanded string representation of counter \c
168          *  c. The \c lang code is used to translate the string.
169          */
170         docstring theCounter(docstring const & c,
171                              std::string const & lang) const;
172         /** Replace in \c format all the LaTeX-like macros that depend
173          * on counters. The \c lang code is used to translate the
174          * string.
175          */
176         docstring counterLabel(docstring const & format,
177                                std::string const & lang) const;
178         /// returns a formatted version of the counter, using the
179         /// format given by Counter::prettyFormat().
180         docstring prettyCounter(docstring const & cntr,
181                                std::string const & lang) const;
182         ///
183         docstring const & guiName(docstring const & cntr) const;
184         /// Are we in appendix?
185         bool appendix() const { return appendix_; }
186         /// Set the state variable indicating whether we are in appendix.
187         void appendix(bool a) { appendix_ = a; }
188         /// Returns the current enclosing float.
189         std::string const & current_float() const { return current_float_; }
190         /// Sets the current enclosing float.
191         void current_float(std::string const & f) { current_float_ = f; }
192         /// Are we in a subfloat?
193         bool isSubfloat() const { return subfloat_; }
194         /// Set the state variable indicating whether we are in a subfloat.
195         void isSubfloat(bool s) { subfloat_ = s; }
196         /// Are we in a longtable?
197         bool isLongtable() const { return longtable_; }
198         /// Set the state variable indicating whether we are in a longtable.
199         void isLongtable(bool s) { longtable_ = s; }
200
201         /// \name refstepcounter
202         // @{
203         /// The currently active counter, so far as references go.
204         /// We're trying to track \refstepcounter in LaTeX, more or less.
205         /// Note that this may be empty.
206         docstring currentCounter() const;
207         /// Called during updateBuffer() as we go through various paragraphs,
208         /// to track the layouts as we go through.
209         void setActiveLayout(Layout const & lay);
210         /// Also for updateBuffer().
211         /// Call this when entering things like footnotes, where there is now
212         /// no "last layout" and we want to restore the "last layout" on exit.
213         void clearLastLayout() { layout_stack_.push_back(nullptr); }
214         /// Call this when exiting things like footnotes.
215         void restoreLastLayout() { layout_stack_.pop_back(); }
216         ///
217         void saveLastCounter()
218                 { counter_stack_.push_back(counter_stack_.back()); }
219         ///
220         void restoreLastCounter() { counter_stack_.pop_back(); }
221         // @}
222         ///
223         std::vector<docstring> listOfCounters() const;
224 private:
225         /** expands recursively any \\the<counter> macro in the
226          *  labelstring of \c counter.  The \c lang code is used to
227          *  translate the string.
228          */
229         docstring flattenLabelString(docstring const & counter, bool in_appendix,
230                                      std::string const &lang,
231                                      std::vector<docstring> & callers) const;
232         /// Returns the value of the counter according to the
233         /// numbering scheme numbertype.
234         /** Available numbering schemes are arabic (1, 2,...), roman
235          *  (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
236          *  B,...) and hebrew.
237          */
238         docstring labelItem(docstring const & ctr,
239                             docstring const & numbertype) const;
240         /// Used for managing the counter_stack_.
241         // @{
242         void beginEnvironment();
243         void endEnvironment();
244         // @}
245         /// Maps counter (layout) names to actual counters.
246         typedef std::map<docstring, Counter> CounterList;
247         /// Instantiate.
248         CounterList counterList_;
249         /// Are we in an appendix?
250         bool appendix_;
251         /// The current enclosing float.
252         std::string current_float_;
253         /// Are we in a subfloat?
254         bool subfloat_;
255         /// Are we in a longtable?
256         bool longtable_;
257         /// Used to keep track of active counters.
258         std::vector<docstring> counter_stack_;
259         /// Same, but for last layout.
260         std::vector<Layout const *> layout_stack_;
261 };
262
263 } // namespace lyx
264
265 #endif