]> git.lyx.org Git - features.git/blob - src/Counters.h
Make the new argument to step() mandatory, and add a comment to
[features.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 <deque>
24 #include <vector>
25
26
27 namespace lyx {
28
29 class Layout;
30 class Lexer;
31
32 /// This represents a single counter.
33 class Counter {
34 public:
35         ///
36         Counter();
37         ///
38         Counter(docstring const & mc, docstring const & ls, 
39                 docstring const & lsa);
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 step();
50         ///
51         void reset();
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.
58          */
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>
65          *  expression.
66          */
67         typedef std::map<std::string, docstring> StringMap;
68         StringMap & flatLabelStrings(bool in_appendix) const;
69 private:
70         ///
71         int value_;
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".
76          */
77         docstring master_;
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_;
88 };
89
90
91 /// This is a class of (La)TeX type counters.
92 /// Every instantiation is an array of counters of type Counter.
93 class Counters {
94 public:
95         ///
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);
110         ///
111         void set(docstring const & ctr, int val);
112         ///
113         void addto(docstring const & ctr, int val);
114         ///
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.
123         void reset();
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.
132          */
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
137          * string.
138          */
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; }
153         
154         /// \name refstepcounter        
155         // @{
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(); }
169         /// 
170         void saveLastCounter()
171                 { counter_stack_.push_back(counter_stack_.back()); }
172         /// 
173         void restoreLastCounter() { counter_stack_.pop_back(); }
174         // @}
175 private:
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.
179          */
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,
187          *  B,...) and hebrew.
188          */
189         docstring labelItem(docstring const & ctr,
190                             docstring const & numbertype) const;
191         /// Used for managing the counter_stack_.
192         // @{
193         void beginEnvironment();
194         void endEnvironment();
195         // @}
196         /// Maps counter (layout) names to actual counters.
197         typedef std::map<docstring, Counter> CounterList;
198         /// Instantiate.
199         CounterList counterList_;
200         /// Are we in an appendix?
201         bool appendix_;
202         /// The current enclosing float.
203         std::string current_float_;
204         /// Are we in a subfloat?
205         bool 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_;
210 };
211
212 } // namespace lyx
213
214 #endif