2000-07-17 18:27:53 +00:00
|
|
|
// -*- C++ -*-
|
2003-08-23 00:17:00 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file Counters.h
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
* \author John Levon
|
|
|
|
* \author Martin Vermeer
|
2000-07-24 13:53:19 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
2000-07-17 18:27:53 +00:00
|
|
|
|
|
|
|
#ifndef COUNTERS_H
|
2001-02-12 14:09:09 +00:00
|
|
|
#define COUNTERS_H
|
2000-07-17 18:27:53 +00:00
|
|
|
|
2010-01-20 19:42:12 +00:00
|
|
|
#include "OutputEnums.h"
|
|
|
|
|
2006-10-20 20:30:00 +00:00
|
|
|
#include "support/docstring.h"
|
|
|
|
|
2002-05-29 16:21:03 +00:00
|
|
|
#include <map>
|
2010-01-20 19:03:17 +00:00
|
|
|
#include <deque>
|
2009-04-19 20:05:26 +00:00
|
|
|
#include <vector>
|
2002-09-05 12:58:10 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2010-01-20 19:03:17 +00:00
|
|
|
class Layout;
|
2008-10-06 14:37:28 +00:00
|
|
|
class Lexer;
|
|
|
|
|
2002-08-07 14:15:06 +00:00
|
|
|
/// This represents a single counter.
|
2002-08-06 22:40:59 +00:00
|
|
|
class Counter {
|
2000-07-17 18:27:53 +00:00
|
|
|
public:
|
|
|
|
///
|
|
|
|
Counter();
|
|
|
|
///
|
2007-08-16 11:07:00 +00:00
|
|
|
Counter(docstring const & mc, docstring const & ls,
|
|
|
|
docstring const & lsa);
|
2008-10-06 14:37:28 +00:00
|
|
|
/// \return true on success
|
|
|
|
bool read(Lexer & lex);
|
2007-08-16 11:07:00 +00:00
|
|
|
///
|
2000-07-17 18:27:53 +00:00
|
|
|
void set(int v);
|
|
|
|
///
|
|
|
|
void addto(int v);
|
|
|
|
///
|
|
|
|
int value() const;
|
|
|
|
///
|
|
|
|
void step();
|
|
|
|
///
|
|
|
|
void reset();
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Returns the master counter of this counter.
|
2007-08-11 18:23:30 +00:00
|
|
|
docstring const & master() const;
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Returns a LaTeX-like string to format the counter.
|
2008-10-06 12:16:17 +00:00
|
|
|
/** This is similar to what one gets in LaTeX when using
|
2009-07-12 20:09:53 +00:00
|
|
|
* "\the<counter>". The \c in_appendix bool tells whether we
|
|
|
|
* want the version shown in an appendix.
|
2007-08-16 14:44:22 +00:00
|
|
|
*/
|
2009-04-19 20:05:26 +00:00
|
|
|
docstring const & labelString(bool in_appendix) const;
|
2010-01-20 20:33:36 +00:00
|
|
|
/// Similar, but used for formatted references in XHTML output.
|
|
|
|
/// E.g., for a section counter it might be "section \thesection"
|
|
|
|
docstring const & prettyFormat() const { return prettyformat_; }
|
|
|
|
|
2009-07-12 20:09:53 +00:00
|
|
|
/// Returns a map of LaTeX-like strings to format the counter.
|
|
|
|
/** For each language, the string is similar to what one gets
|
|
|
|
* in LaTeX when using "\the<counter>". The \c in_appendix
|
|
|
|
* bool tells whether we want the version shown in an
|
|
|
|
* appendix. This version does not contain any \\the<counter>
|
|
|
|
* expression.
|
2007-08-16 14:44:22 +00:00
|
|
|
*/
|
2009-07-12 20:09:53 +00:00
|
|
|
typedef std::map<std::string, docstring> StringMap;
|
|
|
|
StringMap & flatLabelStrings(bool in_appendix) const;
|
2002-08-06 22:40:59 +00:00
|
|
|
private:
|
2002-09-05 12:58:10 +00:00
|
|
|
///
|
2000-07-17 18:27:53 +00:00
|
|
|
int value_;
|
2007-08-16 14:44:22 +00:00
|
|
|
/// contains master counter name.
|
2008-10-06 12:16:17 +00:00
|
|
|
/** The master counter is the counter that, if stepped
|
|
|
|
* (incremented) zeroes this counter. E.g. "subsection"'s
|
|
|
|
* master is "section".
|
2007-08-16 14:44:22 +00:00
|
|
|
*/
|
2007-08-11 18:23:30 +00:00
|
|
|
docstring master_;
|
2008-10-06 12:16:17 +00:00
|
|
|
/// Contains a LaTeX-like string to format the counter.
|
2007-08-16 11:07:00 +00:00
|
|
|
docstring labelstring_;
|
2008-10-06 12:16:17 +00:00
|
|
|
/// The same as labelstring_, but in appendices.
|
2007-08-16 11:07:00 +00:00
|
|
|
docstring labelstringappendix_;
|
2010-01-20 20:33:36 +00:00
|
|
|
/// Similar, but used for formatted references in XHTML output
|
|
|
|
docstring prettyformat_;
|
2009-07-12 20:09:53 +00:00
|
|
|
/// Cache of the labelstring with \\the<counter> expressions expanded,
|
|
|
|
/// indexed by language
|
|
|
|
mutable StringMap flatlabelstring_;
|
|
|
|
/// Cache of the appendix labelstring with \\the<counter> expressions expanded,
|
|
|
|
/// indexed by language
|
|
|
|
mutable StringMap flatlabelstringappendix_;
|
2000-07-17 18:27:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-08-11 20:34:20 +00:00
|
|
|
/// This is a class of (La)TeX type counters.
|
2002-08-07 14:15:06 +00:00
|
|
|
/// Every instantiation is an array of counters of type Counter.
|
2000-07-17 18:27:53 +00:00
|
|
|
class Counters {
|
|
|
|
public:
|
2008-03-13 10:28:02 +00:00
|
|
|
///
|
|
|
|
Counters() : appendix_(false), subfloat_(false) {}
|
2008-10-06 12:24:31 +00:00
|
|
|
/// Add new counter newc having masterc as its master,
|
|
|
|
/// ls as its label, and lsa as its appendix label.
|
2007-08-11 18:23:30 +00:00
|
|
|
void newCounter(docstring const & newc,
|
2007-08-16 11:07:00 +00:00
|
|
|
docstring const & masterc,
|
|
|
|
docstring const & ls,
|
|
|
|
docstring const & lsa);
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Checks whether the given counter exists.
|
2007-08-11 18:23:30 +00:00
|
|
|
bool hasCounter(docstring const & c) const;
|
2008-10-06 14:37:28 +00:00
|
|
|
/// reads the counter name
|
2009-08-12 14:50:01 +00:00
|
|
|
/// \param makeNew whether to make a new counter if one
|
|
|
|
/// doesn't already exist
|
2008-10-06 14:37:28 +00:00
|
|
|
/// \return true on success
|
2009-08-12 14:50:01 +00:00
|
|
|
bool read(Lexer & lex, docstring const & name, bool makenew);
|
2000-07-17 18:27:53 +00:00
|
|
|
///
|
2007-08-11 18:23:30 +00:00
|
|
|
void set(docstring const & ctr, int val);
|
2000-07-17 18:27:53 +00:00
|
|
|
///
|
2007-08-11 18:23:30 +00:00
|
|
|
void addto(docstring const & ctr, int val);
|
|
|
|
///
|
|
|
|
int value(docstring const & ctr) const;
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Increment by one counter named by arg, and zeroes slave
|
|
|
|
/// counter(s) for which it is the master.
|
2010-01-20 19:03:17 +00:00
|
|
|
/// Sub-slaves are not zeroed! That happens at slave's first
|
|
|
|
/// step 0->1. Seems to be sufficient.
|
2010-01-20 19:47:27 +00:00
|
|
|
/// \param utype determines whether we track the counters.
|
|
|
|
void step(docstring const & ctr, UpdateType utype);
|
2002-08-11 20:34:20 +00:00
|
|
|
/// Reset all counters.
|
|
|
|
void reset();
|
|
|
|
/// Reset counters matched by match string.
|
2007-08-11 18:23:30 +00:00
|
|
|
void reset(docstring const & match);
|
2002-08-11 20:34:20 +00:00
|
|
|
/// Copy counters whose name matches match from the &from to
|
2002-08-07 14:15:06 +00:00
|
|
|
/// the &to array of counters. Empty string matches all.
|
2006-10-20 20:30:00 +00:00
|
|
|
void copy(Counters & from, Counters & to,
|
2007-08-11 18:23:30 +00:00
|
|
|
docstring const & match = docstring());
|
2009-07-12 20:09:53 +00:00
|
|
|
/** returns the expanded string representation of counter \c
|
|
|
|
* c. The \c lang code is used to translate the string.
|
|
|
|
*/
|
|
|
|
docstring theCounter(docstring const & c,
|
|
|
|
std::string const & lang) const;
|
|
|
|
/** Replace in \c format all the LaTeX-like macros that depend
|
|
|
|
* on counters. The \c lang code is used to translate the
|
|
|
|
* string.
|
|
|
|
*/
|
|
|
|
docstring counterLabel(docstring const & format,
|
|
|
|
std::string const & lang) const;
|
2010-01-20 20:33:36 +00:00
|
|
|
/// returns a formatted version of the counter, using the
|
|
|
|
/// format given by Counter::prettyFormat().
|
|
|
|
docstring prettyCounter(docstring const & cntr,
|
|
|
|
std::string const & lang) const;
|
2009-04-19 20:05:26 +00:00
|
|
|
/// Are we in appendix?
|
2009-11-15 01:18:13 +00:00
|
|
|
bool appendix() const { return appendix_; }
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Set the state variable indicating whether we are in appendix.
|
2009-11-15 01:18:13 +00:00
|
|
|
void appendix(bool a) { appendix_ = a; }
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Returns the current enclosing float.
|
2007-08-11 18:23:30 +00:00
|
|
|
std::string const & current_float() const { return current_float_; }
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Sets the current enclosing float.
|
2007-08-11 18:23:30 +00:00
|
|
|
void current_float(std::string const & f) { current_float_ = f; }
|
2008-03-02 11:30:50 +00:00
|
|
|
/// Are we in a subfloat?
|
|
|
|
bool isSubfloat() const { return subfloat_; }
|
|
|
|
/// Set the state variable indicating whether we are in a subfloat.
|
2009-11-15 01:18:13 +00:00
|
|
|
void isSubfloat(bool s) { subfloat_ = s; }
|
2010-01-20 19:03:17 +00:00
|
|
|
|
|
|
|
/// \name refstepcounter
|
|
|
|
// @{
|
|
|
|
/// The currently active counter, so far as references go.
|
|
|
|
/// We're trying to track \refstepcounter in LaTeX, more or less.
|
|
|
|
/// Note that this may be empty.
|
|
|
|
docstring currentCounter() const;
|
|
|
|
/// Called during update labels as we go through various paragraphs,
|
|
|
|
/// to track the layouts as we go through.
|
|
|
|
void setActiveLayout(Layout const & lay);
|
|
|
|
/// Also for updateLabels().
|
|
|
|
/// Call this when entering things like footnotes, where there is now
|
|
|
|
/// no "last layout" and we want to restore the "last layout" on exit.
|
|
|
|
void clearLastLayout() { layout_stack_.push_back(0); }
|
|
|
|
/// Call then when existing things like footnotes.
|
|
|
|
void restoreLastLayout() { layout_stack_.pop_back(); }
|
|
|
|
///
|
|
|
|
void saveLastCounter()
|
|
|
|
{ counter_stack_.push_back(counter_stack_.back()); }
|
|
|
|
///
|
|
|
|
void restoreLastCounter() { counter_stack_.pop_back(); }
|
|
|
|
// @}
|
2000-07-17 18:27:53 +00:00
|
|
|
private:
|
2010-01-20 19:03:17 +00:00
|
|
|
/** expands recursively any \\the<counter> macro in the
|
2009-07-12 20:09:53 +00:00
|
|
|
* labelstring of \c counter. The \c lang code is used to
|
|
|
|
* translate the string.
|
|
|
|
*/
|
|
|
|
docstring flattenLabelString(docstring const & counter, bool in_appendix,
|
|
|
|
std::string const &lang,
|
2009-04-19 20:05:26 +00:00
|
|
|
std::vector<docstring> & callers) const;
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Returns the value of the counter according to the
|
|
|
|
/// numbering scheme numbertype.
|
2008-10-06 12:16:17 +00:00
|
|
|
/** Available numbering schemes are arabic (1, 2,...), roman
|
|
|
|
* (i, ii,...), Roman (I, II,...), alph (a, b,...), Alpha (A,
|
|
|
|
* B,...) and hebrew.
|
2007-08-16 14:44:22 +00:00
|
|
|
*/
|
2007-08-11 18:23:30 +00:00
|
|
|
docstring labelItem(docstring const & ctr,
|
2009-04-19 10:06:17 +00:00
|
|
|
docstring const & numbertype) const;
|
2010-01-20 19:03:17 +00:00
|
|
|
/// Used for managing the counter_stack_.
|
|
|
|
// @{
|
|
|
|
void beginEnvironment();
|
|
|
|
void endEnvironment();
|
|
|
|
// @}
|
2002-08-07 14:15:06 +00:00
|
|
|
/// Maps counter (layout) names to actual counters.
|
2007-08-11 18:23:30 +00:00
|
|
|
typedef std::map<docstring, Counter> CounterList;
|
2002-08-07 14:15:06 +00:00
|
|
|
/// Instantiate.
|
2009-04-19 10:06:17 +00:00
|
|
|
CounterList counterList_;
|
2007-08-16 14:44:22 +00:00
|
|
|
/// Are we in an appendix?
|
2007-07-11 12:52:50 +00:00
|
|
|
bool appendix_;
|
2007-08-16 14:44:22 +00:00
|
|
|
/// The current enclosing float.
|
2007-08-11 18:23:30 +00:00
|
|
|
std::string current_float_;
|
2008-03-02 11:30:50 +00:00
|
|
|
/// Are we in a subfloat?
|
|
|
|
bool subfloat_;
|
2010-01-20 19:03:17 +00:00
|
|
|
/// Used to keep track of active counters.
|
|
|
|
std::deque<docstring> counter_stack_;
|
|
|
|
/// Same, but for last layout.
|
|
|
|
std::deque<Layout const *> layout_stack_;
|
2000-07-17 18:27:53 +00:00
|
|
|
};
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|
|
|
|
|
2000-07-17 18:27:53 +00:00
|
|
|
#endif
|