]> git.lyx.org Git - lyx.git/blob - src/Counters.h
Fix compilation on win
[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
53          *  we want the version shown in an appendix.
54          */
55         docstring const & labelString(bool in_appendix) const;
56         /// Returns a LaTeX-like string to format the counter. 
57         /** This is similar to what one gets in LaTeX when using
58          *  "\the<counter>". The \c in_appendix bool tells whether
59          *  we want the version shown in an appendix. This version does 
60          * not contain any \\the<counter> expression.
61          */
62         docstring const & flatLabelString(bool in_appendix) const;
63         /// set the \c flatLabelString values.
64         void setFlatLabelStrings(docstring const & fls, docstring const & flsa);
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         /// A version of the labelstring with \\the<counter> expressions expanded
79         docstring flatlabelstring_;
80         /// A version of the appendix labelstring with \\the<counter> expressions expanded
81         docstring flatlabelstringappendix_;
82 };
83
84
85 /// This is a class of (La)TeX type counters.
86 /// Every instantiation is an array of counters of type Counter.
87 class Counters {
88 public:
89         ///
90         Counters() : appendix_(false), subfloat_(false) {}
91         /// Add new counter newc having masterc as its master, 
92         /// ls as its label, and lsa as its appendix label.
93         void newCounter(docstring const & newc,
94                         docstring const & masterc,
95                         docstring const & ls,
96                         docstring const & lsa);
97         /// Checks whether the given counter exists.
98         bool hasCounter(docstring const & c) const;
99         /// reads the counter name
100         /// \return true on success
101         bool read(Lexer & lex, docstring const & name);
102         ///
103         void set(docstring const & ctr, int val);
104         ///
105         void addto(docstring const & ctr, int val);
106         ///
107         int value(docstring const & ctr) const;
108         /// Increment by one counter named by arg, and zeroes slave
109         /// counter(s) for which it is the master.
110         /** Sub-slaves not zeroed! That happens at slave's first step
111          *  0->1. Seems to be sufficient.
112          */
113         void step(docstring const & ctr);
114         /// Reset all counters.
115         void reset();
116         /// Reset counters matched by match string.
117         void reset(docstring const & match);
118         /// Copy counters whose name matches match from the &from to
119         /// the &to array of counters. Empty string matches all.
120         void copy(Counters & from, Counters & to,
121                   docstring const & match = docstring());
122         /// returns the expanded string representation of the counter.
123         docstring theCounter(docstring const & c) const;
124         /// Replace in \c format all the LaTeX-like macros that depend on
125         /// counters.
126         docstring counterLabel(docstring const & format) const;
127         /// Are we in appendix?
128         bool appendix() const { return appendix_; };
129         /// Set the state variable indicating whether we are in appendix.
130         void appendix(bool a) { appendix_ = a; };
131         /// Returns the current enclosing float.
132         std::string const & current_float() const { return current_float_; }
133         /// Sets the current enclosing float.
134         void current_float(std::string const & f) { current_float_ = f; }
135         /// Are we in a subfloat?
136         bool isSubfloat() const { return subfloat_; }
137         /// Set the state variable indicating whether we are in a subfloat.
138         void isSubfloat(bool s) { subfloat_ = s; };
139 private:
140         /// expands recusrsively any \\the<counter> macro in the
141         /// labelstring of \c counter.
142         docstring flattenLabelString(docstring const & counter, bool in_appendix, 
143                                      std::vector<docstring> & callers) const;
144         /// Returns the value of the counter according to the
145         /// numbering scheme numbertype.
146         /** Available numbering schemes are arabic (1, 2,...), roman
147          *  (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
148          *  B,...) and hebrew.
149          */
150         docstring labelItem(docstring const & ctr,
151                             docstring const & numbertype) const;
152         /// Maps counter (layout) names to actual counters.
153         typedef std::map<docstring, Counter> CounterList;
154         /// Instantiate.
155         CounterList counterList_;
156         /// Are we in an appendix?
157         bool appendix_;
158         /// The current enclosing float.
159         std::string current_float_;
160         /// Are we in a subfloat?
161         bool subfloat_;
162 };
163
164
165 } // namespace lyx
166
167 #endif