]> git.lyx.org Git - features.git/blob - src/Counters.h
Apparently deques aren't so cool.
[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 <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);
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 step();
49         ///
50         void reset();
51         /// Returns the master counter of this counter.
52         docstring const & master() const;
53         /// Returns a LaTeX-like string to format the counter. 
54         /** This is similar to what one gets in LaTeX when using
55          *  "\the<counter>". The \c in_appendix bool tells whether we
56          *  want the version shown in an appendix.
57          */
58         docstring const & labelString(bool in_appendix) const;
59         /// Similar, but used for formatted references in XHTML output.
60         /// E.g., for a section counter it might be "section \thesection"
61         docstring const & prettyFormat() const { return prettyformat_; }
62         
63         /// Returns a map of LaTeX-like strings to format the counter. 
64         /** For each language, the string is similar to what one gets
65          *  in LaTeX when using "\the<counter>". The \c in_appendix
66          *  bool tells whether we want the version shown in an
67          *  appendix. This version does not contain any \\the<counter>
68          *  expression.
69          */
70         typedef std::map<std::string, docstring> StringMap;
71         StringMap & flatLabelStrings(bool in_appendix) const;
72 private:
73         ///
74         int value_;
75         /// contains master counter name.
76         /** The master counter is the counter that, if stepped
77          *  (incremented) zeroes this counter. E.g. "subsection"'s
78          *  master is "section".
79          */
80         docstring master_;
81         /// Contains a LaTeX-like string to format the counter.
82         docstring labelstring_;
83         /// The same as labelstring_, but in appendices.
84         docstring labelstringappendix_;
85         /// Similar, but used for formatted references in XHTML output
86         docstring prettyformat_;
87         /// Cache of the labelstring with \\the<counter> expressions expanded, 
88         /// indexed by language
89         mutable StringMap flatlabelstring_;
90         /// Cache of the appendix labelstring with \\the<counter> expressions expanded, 
91         /// indexed by language
92         mutable StringMap flatlabelstringappendix_;
93 };
94
95
96 /// This is a class of (La)TeX type counters.
97 /// Every instantiation is an array of counters of type Counter.
98 class Counters {
99 public:
100         ///
101         Counters();
102         /// Add new counter newc having masterc as its master, 
103         /// ls as its label, and lsa as its appendix label.
104         void newCounter(docstring const & newc,
105                         docstring const & masterc,
106                         docstring const & ls,
107                         docstring const & lsa);
108         /// Checks whether the given counter exists.
109         bool hasCounter(docstring const & c) const;
110         /// reads the counter name
111         /// \param makeNew whether to make a new counter if one 
112         ///        doesn't already exist
113         /// \return true on success
114         bool read(Lexer & lex, docstring const & name, bool makenew);
115         ///
116         void set(docstring const & ctr, int val);
117         ///
118         void addto(docstring const & ctr, int val);
119         ///
120         int value(docstring const & ctr) const;
121         /// Increment by one counter named by arg, and zeroes slave
122         /// counter(s) for which it is the master.
123         /// Sub-slaves are not zeroed! That happens at slave's first 
124         /// step 0->1. Seems to be sufficient.
125         /// \param utype determines whether we track the counters.
126         void step(docstring const & ctr, UpdateType utype);
127         /// Reset all counters.
128         void reset();
129         /// Reset counters matched by match string.
130         void reset(docstring const & match);
131         /// Copy counters whose name matches match from the &from to
132         /// the &to array of counters. Empty string matches all.
133         void copy(Counters & from, Counters & to,
134                   docstring const & match = docstring());
135         /** returns the expanded string representation of counter \c
136          *  c. The \c lang code is used to translate the string.
137          */
138         docstring theCounter(docstring const & c,
139                              std::string const & lang) const;
140         /** Replace in \c format all the LaTeX-like macros that depend
141          * on counters. The \c lang code is used to translate the
142          * string.
143          */
144         docstring counterLabel(docstring const & format,
145                                std::string const & lang) const;
146         /// returns a formatted version of the counter, using the 
147         /// format given by Counter::prettyFormat().
148         docstring prettyCounter(docstring const & cntr,
149                                std::string const & lang) const;
150         /// Are we in appendix?
151         bool appendix() const { return appendix_; }
152         /// Set the state variable indicating whether we are in appendix.
153         void appendix(bool a) { appendix_ = a; }
154         /// Returns the current enclosing float.
155         std::string const & current_float() const { return current_float_; }
156         /// Sets the current enclosing float.
157         void current_float(std::string const & f) { current_float_ = f; }
158         /// Are we in a subfloat?
159         bool isSubfloat() const { return subfloat_; }
160         /// Set the state variable indicating whether we are in a subfloat.
161         void isSubfloat(bool s) { subfloat_ = s; }
162         
163         /// \name refstepcounter        
164         // @{
165         /// The currently active counter, so far as references go.
166         /// We're trying to track \refstepcounter in LaTeX, more or less.
167         /// Note that this may be empty.
168         docstring currentCounter() const;
169         /// Called during updateBuffer() as we go through various paragraphs,
170         /// to track the layouts as we go through.
171         void setActiveLayout(Layout const & lay);
172         /// Also for updateBuffer().
173         /// Call this when entering things like footnotes, where there is now
174         /// no "last layout" and we want to restore the "last layout" on exit.
175         void clearLastLayout() { layout_stack_.push_back(0); }
176         /// Call this when exiting things like footnotes.
177         void restoreLastLayout() { layout_stack_.pop_back(); }
178         /// 
179         void saveLastCounter()
180                 { counter_stack_.push_back(counter_stack_.back()); }
181         /// 
182         void restoreLastCounter() { counter_stack_.pop_back(); }
183         // @}
184 private:
185         /** expands recursively any \\the<counter> macro in the
186          *  labelstring of \c counter.  The \c lang code is used to
187          *  translate the string.
188          */
189         docstring flattenLabelString(docstring const & counter, bool in_appendix,
190                                      std::string const &lang,
191                                      std::vector<docstring> & callers) const;
192         /// Returns the value of the counter according to the
193         /// numbering scheme numbertype.
194         /** Available numbering schemes are arabic (1, 2,...), roman
195          *  (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
196          *  B,...) and hebrew.
197          */
198         docstring labelItem(docstring const & ctr,
199                             docstring const & numbertype) const;
200         /// Used for managing the counter_stack_.
201         // @{
202         void beginEnvironment();
203         void endEnvironment();
204         // @}
205         /// Maps counter (layout) names to actual counters.
206         typedef std::map<docstring, Counter> CounterList;
207         /// Instantiate.
208         CounterList counterList_;
209         /// Are we in an appendix?
210         bool appendix_;
211         /// The current enclosing float.
212         std::string current_float_;
213         /// Are we in a subfloat?
214         bool subfloat_;
215         /// Used to keep track of active counters.
216         std::vector<docstring> counter_stack_;
217         /// Same, but for last layout.
218         std::vector<Layout const *> layout_stack_;
219 };
220
221 } // namespace lyx
222
223 #endif