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