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