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