]> git.lyx.org Git - features.git/blob - src/Counters.h
The counters labelstring patch. Part 1: the infrastructure.
[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
22
23 namespace lyx {
24
25 /// This represents a single counter.
26 class Counter {
27 public:
28         ///
29         Counter();
30         ///
31         Counter(docstring const & mc, docstring const & ls, 
32                 docstring const & lsa);
33         ///
34         void set(int v);
35         ///
36         void addto(int v);
37         ///
38         int value() const;
39         ///
40         void step();
41         ///
42         void reset();
43         /// Returns the master counter of this counter
44         docstring const & master() const;
45         /// Returns a LaTeX-like string to format the counter, similar
46         /// to LaTeX' \c \thesubsection.
47         docstring const & labelString() const;
48         /// Returns a LaTeX-like string to format the counter in
49         /// appendix, similar to LaTeX' \c \thesubsection.
50         docstring const & labelStringAppendix() const;
51 private:
52         ///
53         int value_;
54         /// contains master counter name; master counter is the counter
55         /// that, if stepped (incremented) zeroes this counter. E.g.
56         /// "subsection"'s master is "section".
57         docstring master_;
58         // Contains a LaTeX-like string to format the counter, similar
59         // to LaTeX' \c \thesubsection.
60         docstring labelstring_;
61         // The same as labelstring_, but in appendices.
62         docstring labelstringappendix_;
63 };
64
65
66 /// This is a class of (La)TeX type counters.
67 /// Every instantiation is an array of counters of type Counter.
68 class Counters {
69 public:
70         /// Add a new counter to array.
71         void newCounter(docstring const & newc);
72         /// Add new counter having oldc as its master and ls as its label.
73         void newCounter(docstring const & newc,
74                         docstring const & masterc,
75                         docstring const & ls,
76                         docstring const & lsa);
77         ///
78         bool hasCounter(docstring const & c) const;
79         ///
80         void set(docstring const & ctr, int val);
81         ///
82         void addto(docstring const & ctr, int val);
83         ///
84         int value(docstring const & ctr) const;
85         /// Step (increment by one) counter named by arg, and
86         /// zeroes slave counter(s) for which it is the master.
87         /// NOTE sub-slaves not zeroed! That happens at slave's
88         /// first step 0->1. Seems to be sufficient.
89         void step(docstring const & ctr);
90         /// Reset all counters.
91         void reset();
92         /// Reset counters matched by match string.
93         void reset(docstring const & match);
94         /// Copy counters whose name matches match from the &from to
95         /// the &to array of counters. Empty string matches all.
96         void copy(Counters & from, Counters & to,
97                   docstring const & match = docstring());
98         /// returns the string representation of the counter.
99         docstring theCounter(docstring const & c);
100         /// A complete expanded label, like 2.1.4 for a subsubsection
101         /// according to the given format
102         docstring counterLabel(docstring const & format);
103         ///
104         bool appendix() const { return appendix_; };
105         ///
106         void appendix(bool a) { appendix_ = a; };
107         ///
108         std::string const & current_float() const { return current_float_; }
109         ///
110         void current_float(std::string const & f) { current_float_ = f; }
111 private:
112         /// A counter label's single item, 1 for subsection number in
113         /// the 2.1.4 subsubsection number label.
114         docstring labelItem(docstring const & ctr,
115                             docstring const & numbertype);
116         /// Maps counter (layout) names to actual counters.
117         typedef std::map<docstring, Counter> CounterList;
118         /// Instantiate.
119         CounterList counterList;
120         ///
121         bool appendix_;
122         ///
123         std::string current_float_;
124 };
125
126
127 } // namespace lyx
128
129 #endif