4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author Martin Vermeer
12 * Full author contact details are available in file CREDITS.
18 #include "OutputEnums.h"
20 #include "support/docstring.h"
32 /// This represents a single counter.
38 Counter(docstring const & mc, docstring const & ls,
39 docstring const & lsa);
40 /// \return true on success
41 bool read(Lexer & lex);
52 /// Returns the master counter of this counter.
53 docstring const & master() const;
54 /// Returns a LaTeX-like string to format the counter.
55 /** This is similar to what one gets in LaTeX when using
56 * "\the<counter>". The \c in_appendix bool tells whether we
57 * want the version shown in an appendix.
59 docstring const & labelString(bool in_appendix) const;
60 /// Returns a map of LaTeX-like strings to format the counter.
61 /** For each language, the string is similar to what one gets
62 * in LaTeX when using "\the<counter>". The \c in_appendix
63 * bool tells whether we want the version shown in an
64 * appendix. This version does not contain any \\the<counter>
67 typedef std::map<std::string, docstring> StringMap;
68 StringMap & flatLabelStrings(bool in_appendix) const;
72 /// contains master counter name.
73 /** The master counter is the counter that, if stepped
74 * (incremented) zeroes this counter. E.g. "subsection"'s
75 * master is "section".
78 /// Contains a LaTeX-like string to format the counter.
79 docstring labelstring_;
80 /// The same as labelstring_, but in appendices.
81 docstring labelstringappendix_;
82 /// Cache of the labelstring with \\the<counter> expressions expanded,
83 /// indexed by language
84 mutable StringMap flatlabelstring_;
85 /// Cache of the appendix labelstring with \\the<counter> expressions expanded,
86 /// indexed by language
87 mutable StringMap flatlabelstringappendix_;
91 /// This is a class of (La)TeX type counters.
92 /// Every instantiation is an array of counters of type Counter.
96 Counters() : appendix_(false), subfloat_(false) {}
97 /// Add new counter newc having masterc as its master,
98 /// ls as its label, and lsa as its appendix label.
99 void newCounter(docstring const & newc,
100 docstring const & masterc,
101 docstring const & ls,
102 docstring const & lsa);
103 /// Checks whether the given counter exists.
104 bool hasCounter(docstring const & c) const;
105 /// reads the counter name
106 /// \param makeNew whether to make a new counter if one
107 /// doesn't already exist
108 /// \return true on success
109 bool read(Lexer & lex, docstring const & name, bool makenew);
111 void set(docstring const & ctr, int val);
113 void addto(docstring const & ctr, int val);
115 int value(docstring const & ctr) const;
116 /// Increment by one counter named by arg, and zeroes slave
117 /// counter(s) for which it is the master.
118 /// Sub-slaves are not zeroed! That happens at slave's first
119 /// step 0->1. Seems to be sufficient.
120 /// \param utype determines whether we track the counters.
121 void step(docstring const & ctr, UpdateType utype);
122 /// Reset all counters.
124 /// Reset counters matched by match string.
125 void reset(docstring const & match);
126 /// Copy counters whose name matches match from the &from to
127 /// the &to array of counters. Empty string matches all.
128 void copy(Counters & from, Counters & to,
129 docstring const & match = docstring());
130 /** returns the expanded string representation of counter \c
131 * c. The \c lang code is used to translate the string.
133 docstring theCounter(docstring const & c,
134 std::string const & lang) const;
135 /** Replace in \c format all the LaTeX-like macros that depend
136 * on counters. The \c lang code is used to translate the
139 docstring counterLabel(docstring const & format,
140 std::string const & lang) const;
141 /// Are we in appendix?
142 bool appendix() const { return appendix_; }
143 /// Set the state variable indicating whether we are in appendix.
144 void appendix(bool a) { appendix_ = a; }
145 /// Returns the current enclosing float.
146 std::string const & current_float() const { return current_float_; }
147 /// Sets the current enclosing float.
148 void current_float(std::string const & f) { current_float_ = f; }
149 /// Are we in a subfloat?
150 bool isSubfloat() const { return subfloat_; }
151 /// Set the state variable indicating whether we are in a subfloat.
152 void isSubfloat(bool s) { subfloat_ = s; }
154 /// \name refstepcounter
156 /// The currently active counter, so far as references go.
157 /// We're trying to track \refstepcounter in LaTeX, more or less.
158 /// Note that this may be empty.
159 docstring currentCounter() const;
160 /// Called during update labels as we go through various paragraphs,
161 /// to track the layouts as we go through.
162 void setActiveLayout(Layout const & lay);
163 /// Also for updateLabels().
164 /// Call this when entering things like footnotes, where there is now
165 /// no "last layout" and we want to restore the "last layout" on exit.
166 void clearLastLayout() { layout_stack_.push_back(0); }
167 /// Call then when existing things like footnotes.
168 void restoreLastLayout() { layout_stack_.pop_back(); }
170 void saveLastCounter()
171 { counter_stack_.push_back(counter_stack_.back()); }
173 void restoreLastCounter() { counter_stack_.pop_back(); }
176 /** expands recursively any \\the<counter> macro in the
177 * labelstring of \c counter. The \c lang code is used to
178 * translate the string.
180 docstring flattenLabelString(docstring const & counter, bool in_appendix,
181 std::string const &lang,
182 std::vector<docstring> & callers) const;
183 /// Returns the value of the counter according to the
184 /// numbering scheme numbertype.
185 /** Available numbering schemes are arabic (1, 2,...), roman
186 * (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
189 docstring labelItem(docstring const & ctr,
190 docstring const & numbertype) const;
191 /// Used for managing the counter_stack_.
193 void beginEnvironment();
194 void endEnvironment();
196 /// Maps counter (layout) names to actual counters.
197 typedef std::map<docstring, Counter> CounterList;
199 CounterList counterList_;
200 /// Are we in an appendix?
202 /// The current enclosing float.
203 std::string current_float_;
204 /// Are we in a subfloat?
206 /// Used to keep track of active counters.
207 std::deque<docstring> counter_stack_;
208 /// Same, but for last layout.
209 std::deque<Layout const *> layout_stack_;