]> git.lyx.org Git - lyx.git/blob - src/Counters.h
Refactor InsetQuotes.h enums
[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         /** returns the expanded string representation of counter \c
164          *  c. The \c lang code is used to translate the string.
165          */
166         docstring theCounter(docstring const & c,
167                              std::string const & lang) const;
168         /** Replace in \c format all the LaTeX-like macros that depend
169          * on counters. The \c lang code is used to translate the
170          * string.
171          */
172         docstring counterLabel(docstring const & format,
173                                std::string const & lang) const;
174         /// returns a formatted version of the counter, using the
175         /// format given by Counter::prettyFormat().
176         docstring prettyCounter(docstring const & cntr,
177                                std::string const & lang) const;
178         ///
179         docstring const & guiName(docstring const & cntr) const;
180         /// Are we in appendix?
181         bool appendix() const { return appendix_; }
182         /// Set the state variable indicating whether we are in appendix.
183         void appendix(bool a) { appendix_ = a; }
184         /// Returns the current enclosing float.
185         std::string const & current_float() const { return current_float_; }
186         /// Sets the current enclosing float.
187         void current_float(std::string const & f) { current_float_ = f; }
188         /// Are we in a subfloat?
189         bool isSubfloat() const { return subfloat_; }
190         /// Set the state variable indicating whether we are in a subfloat.
191         void isSubfloat(bool s) { subfloat_ = s; }
192         /// Are we in a longtable?
193         bool isLongtable() const { return longtable_; }
194         /// Set the state variable indicating whether we are in a longtable.
195         void isLongtable(bool s) { longtable_ = s; }
196
197         /// \name refstepcounter
198         // @{
199         /// The currently active counter, so far as references go.
200         /// We're trying to track \refstepcounter in LaTeX, more or less.
201         /// Note that this may be empty.
202         docstring currentCounter() const;
203         /// Called during updateBuffer() as we go through various paragraphs,
204         /// to track the layouts as we go through.
205         void setActiveLayout(Layout const & lay);
206         /// Also for updateBuffer().
207         /// Call this when entering things like footnotes, where there is now
208         /// no "last layout" and we want to restore the "last layout" on exit.
209         void clearLastLayout() { layout_stack_.push_back(nullptr); }
210         /// Call this when exiting things like footnotes.
211         void restoreLastLayout() { layout_stack_.pop_back(); }
212         ///
213         void saveLastCounter()
214                 { counter_stack_.push_back(counter_stack_.back()); }
215         ///
216         void restoreLastCounter() { counter_stack_.pop_back(); }
217         // @}
218         ///
219         std::vector<docstring> listOfCounters() const;
220 private:
221         /** expands recursively any \\the<counter> macro in the
222          *  labelstring of \c counter.  The \c lang code is used to
223          *  translate the string.
224          */
225         docstring flattenLabelString(docstring const & counter, bool in_appendix,
226                                      std::string const &lang,
227                                      std::vector<docstring> & callers) const;
228         /// Returns the value of the counter according to the
229         /// numbering scheme numbertype.
230         /** Available numbering schemes are arabic (1, 2,...), roman
231          *  (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
232          *  B,...) and hebrew.
233          */
234         docstring labelItem(docstring const & ctr,
235                             docstring const & numbertype) const;
236         /// Used for managing the counter_stack_.
237         // @{
238         void beginEnvironment();
239         void endEnvironment();
240         // @}
241         /// Maps counter (layout) names to actual counters.
242         typedef std::map<docstring, Counter> CounterList;
243         /// Instantiate.
244         CounterList counterList_;
245         /// Are we in an appendix?
246         bool appendix_;
247         /// The current enclosing float.
248         std::string current_float_;
249         /// Are we in a subfloat?
250         bool subfloat_;
251         /// Are we in a longtable?
252         bool longtable_;
253         /// Used to keep track of active counters.
254         std::vector<docstring> counter_stack_;
255         /// Same, but for last layout.
256         std::vector<Layout const *> layout_stack_;
257 };
258
259 } // namespace lyx
260
261 #endif