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