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