]> git.lyx.org Git - lyx.git/blob - src/Counters.h
Hide some internals of Changes:
[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 "support/docstring.h"
19
20 #include <map>
21 #include <vector>
22
23
24 namespace lyx {
25
26 class Lexer;
27
28 /// This represents a single counter.
29 class Counter {
30 public:
31         ///
32         Counter();
33         ///
34         Counter(docstring const & mc, docstring const & ls, 
35                 docstring const & lsa);
36         /// \return true on success
37         bool read(Lexer & lex);
38         ///
39         void set(int v);
40         ///
41         void addto(int v);
42         ///
43         int value() const;
44         ///
45         void step();
46         ///
47         void reset();
48         /// Returns the master counter of this counter.
49         docstring const & master() const;
50         /// Returns a LaTeX-like string to format the counter. 
51         /** This is similar to what one gets in LaTeX when using
52          *  "\the<counter>". The \c in_appendix bool tells whether we
53          *  want the version shown in an appendix.
54          */
55         docstring const & labelString(bool in_appendix) const;
56         /// Returns a map of LaTeX-like strings to format the counter. 
57         /** For each language, the string is similar to what one gets
58          *  in LaTeX when using "\the<counter>". The \c in_appendix
59          *  bool tells whether we want the version shown in an
60          *  appendix. This version does not contain any \\the<counter>
61          *  expression.
62          */
63         typedef std::map<std::string, docstring> StringMap;
64         StringMap & flatLabelStrings(bool in_appendix) const;
65 private:
66         ///
67         int value_;
68         /// contains master counter name.
69         /** The master counter is the counter that, if stepped
70          *  (incremented) zeroes this counter. E.g. "subsection"'s
71          *  master is "section".
72          */
73         docstring master_;
74         /// Contains a LaTeX-like string to format the counter.
75         docstring labelstring_;
76         /// The same as labelstring_, but in appendices.
77         docstring labelstringappendix_;
78         /// Cache of the labelstring with \\the<counter> expressions expanded, 
79         /// indexed by language
80         mutable StringMap flatlabelstring_;
81         /// Cache of the appendix labelstring with \\the<counter> expressions expanded, 
82         /// indexed by language
83         mutable StringMap flatlabelstringappendix_;
84 };
85
86
87 /// This is a class of (La)TeX type counters.
88 /// Every instantiation is an array of counters of type Counter.
89 class Counters {
90 public:
91         ///
92         Counters() : appendix_(false), subfloat_(false) {}
93         /// Add new counter newc having masterc as its master, 
94         /// ls as its label, and lsa as its appendix label.
95         void newCounter(docstring const & newc,
96                         docstring const & masterc,
97                         docstring const & ls,
98                         docstring const & lsa);
99         /// Checks whether the given counter exists.
100         bool hasCounter(docstring const & c) const;
101         /// reads the counter name
102         /// \return true on success
103         bool read(Lexer & lex, docstring const & name);
104         ///
105         void set(docstring const & ctr, int val);
106         ///
107         void addto(docstring const & ctr, int val);
108         ///
109         int value(docstring const & ctr) const;
110         /// Increment by one counter named by arg, and zeroes slave
111         /// counter(s) for which it is the master.
112         /** Sub-slaves not zeroed! That happens at slave's first step
113          *  0->1. Seems to be sufficient.
114          */
115         void step(docstring const & ctr);
116         /// Reset all counters.
117         void reset();
118         /// Reset counters matched by match string.
119         void reset(docstring const & match);
120         /// Copy counters whose name matches match from the &from to
121         /// the &to array of counters. Empty string matches all.
122         void copy(Counters & from, Counters & to,
123                   docstring const & match = docstring());
124         /** returns the expanded string representation of counter \c
125          *  c. The \c lang code is used to translate the string.
126          */
127         docstring theCounter(docstring const & c,
128                              std::string const & lang) const;
129         /** Replace in \c format all the LaTeX-like macros that depend
130          * on counters. The \c lang code is used to translate the
131          * string.
132          */
133         docstring counterLabel(docstring const & format,
134                                std::string const & lang) const;
135         /// Are we in appendix?
136         bool appendix() const { return appendix_; };
137         /// Set the state variable indicating whether we are in appendix.
138         void appendix(bool a) { appendix_ = a; };
139         /// Returns the current enclosing float.
140         std::string const & current_float() const { return current_float_; }
141         /// Sets the current enclosing float.
142         void current_float(std::string const & f) { current_float_ = f; }
143         /// Are we in a subfloat?
144         bool isSubfloat() const { return subfloat_; }
145         /// Set the state variable indicating whether we are in a subfloat.
146         void isSubfloat(bool s) { subfloat_ = s; };
147 private:
148         /** expands recusrsively any \\the<counter> macro in the
149          *  labelstring of \c counter.  The \c lang code is used to
150          *  translate the string.
151          */
152         docstring flattenLabelString(docstring const & counter, bool in_appendix,
153                                      std::string const &lang,
154                                      std::vector<docstring> & callers) const;
155         /// Returns the value of the counter according to the
156         /// numbering scheme numbertype.
157         /** Available numbering schemes are arabic (1, 2,...), roman
158          *  (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
159          *  B,...) and hebrew.
160          */
161         docstring labelItem(docstring const & ctr,
162                             docstring const & numbertype) const;
163         /// Maps counter (layout) names to actual counters.
164         typedef std::map<docstring, Counter> CounterList;
165         /// Instantiate.
166         CounterList counterList_;
167         /// Are we in an appendix?
168         bool appendix_;
169         /// The current enclosing float.
170         std::string current_float_;
171         /// Are we in a subfloat?
172         bool subfloat_;
173 };
174
175
176 } // namespace lyx
177
178 #endif