]> git.lyx.org Git - lyx.git/blob - src/Counters.h
Fix some issues with textmode. We'll let SetMode() handle as much of
[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         /// \param makeNew whether to make a new counter if one 
103         ///        doesn't already exist
104         /// \return true on success
105         bool read(Lexer & lex, docstring const & name, bool makenew);
106         ///
107         void set(docstring const & ctr, int val);
108         ///
109         void addto(docstring const & ctr, int val);
110         ///
111         int value(docstring const & ctr) const;
112         /// Increment by one counter named by arg, and zeroes slave
113         /// counter(s) for which it is the master.
114         /** Sub-slaves not zeroed! That happens at slave's first step
115          *  0->1. Seems to be sufficient.
116          */
117         void step(docstring const & ctr);
118         /// Reset all counters.
119         void reset();
120         /// Reset counters matched by match string.
121         void reset(docstring const & match);
122         /// Copy counters whose name matches match from the &from to
123         /// the &to array of counters. Empty string matches all.
124         void copy(Counters & from, Counters & to,
125                   docstring const & match = docstring());
126         /** returns the expanded string representation of counter \c
127          *  c. The \c lang code is used to translate the string.
128          */
129         docstring theCounter(docstring const & c,
130                              std::string const & lang) const;
131         /** Replace in \c format all the LaTeX-like macros that depend
132          * on counters. The \c lang code is used to translate the
133          * string.
134          */
135         docstring counterLabel(docstring const & format,
136                                std::string const & lang) const;
137         /// Are we in appendix?
138         bool appendix() const { return appendix_; }
139         /// Set the state variable indicating whether we are in appendix.
140         void appendix(bool a) { appendix_ = a; }
141         /// Returns the current enclosing float.
142         std::string const & current_float() const { return current_float_; }
143         /// Sets the current enclosing float.
144         void current_float(std::string const & f) { current_float_ = f; }
145         /// Are we in a subfloat?
146         bool isSubfloat() const { return subfloat_; }
147         /// Set the state variable indicating whether we are in a subfloat.
148         void isSubfloat(bool s) { subfloat_ = s; }
149 private:
150         /** expands recusrsively any \\the<counter> macro in the
151          *  labelstring of \c counter.  The \c lang code is used to
152          *  translate the string.
153          */
154         docstring flattenLabelString(docstring const & counter, bool in_appendix,
155                                      std::string const &lang,
156                                      std::vector<docstring> & callers) const;
157         /// Returns the value of the counter according to the
158         /// numbering scheme numbertype.
159         /** Available numbering schemes are arabic (1, 2,...), roman
160          *  (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
161          *  B,...) and hebrew.
162          */
163         docstring labelItem(docstring const & ctr,
164                             docstring const & numbertype) const;
165         /// Maps counter (layout) names to actual counters.
166         typedef std::map<docstring, Counter> CounterList;
167         /// Instantiate.
168         CounterList counterList_;
169         /// Are we in an appendix?
170         bool appendix_;
171         /// The current enclosing float.
172         std::string current_float_;
173         /// Are we in a subfloat?
174         bool subfloat_;
175 };
176
177
178 } // namespace lyx
179
180 #endif