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