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