]> git.lyx.org Git - lyx.git/blobdiff - src/counters.C
Fix the missing "Figure #:" label from the caption of a figure float.
[lyx.git] / src / counters.C
index e9c0ed0455f275ed5ff6b2c481b1a0eb4d5e1a1a..087a0bd2c454d041f813df7148876b3e41cbc1e9 100644 (file)
@@ -1,27 +1,29 @@
-/* This file is part of
- * ======================================================
+/**
+ * \file counters.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *           LyX, The Document Processor
+ * \author Lars Gullik Bjønnes
+ * \author Martin Vermeer
+ * \author André Pönitz
  *
- *           Copyright 1995 Matthias Ettrich
- *           Copyright 1995-2001 The LyX Team.
- *
- *
- * ====================================================== */
-
-#ifdef __GNUG__
-#pragma implementation
-#endif
+ * Full author contact details are available in file CREDITS.
+ */
 
 #include <config.h>
 
 #include "counters.h"
 #include "debug.h"
+
 #include "support/lstrings.h"
-#include "support/LAssert.h"
+#include "support/std_sstream.h"
+#include "support/tostr.h"
+
+#include <boost/assert.hpp>
 
 using std::endl;
-using std::vector;
+using std::ostringstream;
+using std::string;
 
 
 Counter::Counter()
@@ -59,57 +61,19 @@ void Counter::reset()
        value_ = 0;
 }
 
+
 string Counter::master() const
 {
        return master_;
 }
 
+
 void Counter::setMaster(string const & m)
 {
        master_ = m;
 }
 
 
-Counters::Counters()
-{
-       // Ehh, should this take a textclass arg?
-
-       // Sectioning counters:
-       newCounter("part");
-       newCounter("chapter");
-       newCounter("section", "chapter");
-       newCounter("subsection", "section");
-       newCounter("subsubsection", "subsection");
-       newCounter("paragraph", "subsubsection");
-       newCounter("subparagraph", "paragraph");
-
-       sects.push_back("chapter");
-       sects.push_back("section");
-       sects.push_back("subsection");
-       sects.push_back("subsubsection");
-       sects.push_back("paragraph");
-       sects.push_back("subparagraph");
-
-       // Enumeration counters:
-       newCounter("enumi");
-       newCounter("enumii", "enumi");
-       newCounter("enumiii", "enumii");
-       newCounter("enumiv", "enumiii");
-
-       enums.push_back("enumi");
-       enums.push_back("enumii");
-       enums.push_back("enumiii");
-       enums.push_back("enumiv");
-
-       // Biblio:
-       newCounter("bibitem");
-
-       // Float counters:
-       newCounter("Figure");
-       newCounter("Table");
-}
-
-
 void Counters::newCounter(string const & newc)
 {
        // First check if newc already exist
@@ -120,8 +84,6 @@ void Counters::newCounter(string const & newc)
                return;
        }
        counterList[newc];
-       cit = counterList.find(newc);
-       cit->second.setMaster("");
 }
 
 
@@ -142,9 +104,7 @@ void Counters::newCounter(string const & newc, string const & masterc)
                return;
        }
 
-       counterList[newc];
-       cit = counterList.find(newc);
-       cit->second.setMaster(masterc);
+       counterList[newc].setMaster(masterc);
 }
 
 
@@ -212,7 +172,7 @@ void Counters::reset()
 
 void Counters::reset(string const & match)
 {
-       lyx::Assert(!match.empty());
+       BOOST_ASSERT(!match.empty());
 
        CounterList::iterator it = counterList.begin();
        CounterList::iterator end = counterList.end();
@@ -222,6 +182,7 @@ void Counters::reset(string const & match)
        }
 }
 
+
 void Counters::copy(Counters & from, Counters & to, string const & match)
 {
        CounterList::iterator it = counterList.begin();
@@ -236,25 +197,22 @@ void Counters::copy(Counters & from, Counters & to, string const & match)
 
 namespace {
 
-inline
 char loweralphaCounter(int n)
 {
        if (n < 1 || n > 26)
                return '?';
-       else
-               return 'a' + n - 1;
+       return 'a' + n - 1;
 }
 
-inline
+
 char alphaCounter(int n)
 {
        if (n < 1 || n > 26)
                return '?';
-       else
-               return 'A' + n - 1;
+       return 'A' + n - 1;
 }
 
-inline
+
 char hebrewCounter(int n)
 {
        static const char hebrew[22] = {
@@ -262,14 +220,14 @@ char hebrewCounter(int n)
                'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
                '÷', 'ø', 'ù', 'ú'
        };
+
        if (n < 1 || n > 22)
                return '?';
-       else
-               return hebrew[n-1];
+       return hebrew[n - 1];
 }
 
-inline
-string const romanCounter(int n)
+
+string const lowerromanCounter(int n)
 {
        static char const * roman[20] = {
                "i",   "ii",  "iii", "iv", "v",
@@ -277,98 +235,108 @@ string const romanCounter(int n)
                "xi",  "xii", "xiii", "xiv", "xv",
                "xvi", "xvii", "xviii", "xix", "xx"
        };
+
+       if (n < 1 || n > 20)
+               return "??";
+       return roman[n - 1];
+}
+
+
+string const romanCounter(int n)
+{
+       static char const * roman[20] = {
+               "I",   "II",  "III", "IV", "V",
+               "VI",  "VII", "VIII", "IX", "X",
+               "XI",  "XII", "XIII", "XIV", "XV",
+               "XVI", "XVII", "XVIII", "XIX", "XX"
+       };
+
        if (n < 1 || n > 20)
                return "??";
-       else
-               return roman[n-1];
+       return roman[n - 1];
 }
 
 } // namespace anon
 
-string Counters::labelItem(string const & ctr,
-               string const & numbertype,
-               string const & langtype,
-               bool first)
+
+string Counters::labelItem(string const & ctr, string const & numbertype)
 {
-       ostringstream s, o;
-       CounterList::iterator it = counterList.find(ctr);
-       if (it == counterList.end()) {
-               lyxerr << "Counter does not exist." << endl;
-               return "";
-       }
-       string mstr = it->second.master();
-       if (!first) {
-               s << "." << value(ctr);
-       } else {
-               if (numbertype == "sectioning" || numbertype == "appendix") {
-                       if (numbertype == "appendix") {
-                               if (langtype == "hebrew") {
-                                       o << hebrewCounter(value(ctr));
-                               } else {
-                                       o << alphaCounter(value(ctr));
-                               }
-                       } else o << value(ctr);
-               }
-               s << o.str();
+       if (counterList.find(ctr) == counterList.end()) {
+               lyxerr << "Counter " << ctr << " does not exist." << endl;
+               return string();
        }
-       return s.str();
+
+       if (numbertype == "hebrew")
+               return string(1, hebrewCounter(value(ctr)));
+
+       if (numbertype == "alph")
+               return string(1, loweralphaCounter(value(ctr)));
+
+       if (numbertype == "Alph")
+               return string(1, alphaCounter(value(ctr)));
+
+       if (numbertype == "roman")
+               return lowerromanCounter(value(ctr));
+
+       if (numbertype == "Roman")
+               return romanCounter(value(ctr));
+
+       return tostr(value(ctr));
 }
 
-string Counters::numberLabel(string const & ctr,
-               string const & numbertype,
-               string const & langtype,
-               int head)
+
+string Counters::counterLabel(string const & format)
 {
-       ostringstream s, o;
-       if (numbertype == "sectioning" || numbertype == "appendix") {
-               if (ctr == "chapter" && head == 0) {
-                       s << labelItem("chapter", numbertype, langtype, true);
-               } else if (ctr == "section" && head <= 1) {
-                       s << numberLabel("chapter", numbertype, langtype, head)
-                         << labelItem("section", numbertype, langtype, head == 1);
-               } else if (ctr == "subsection" && head <= 2) {
-                       s << numberLabel("section", numbertype, langtype, head)
-                         << labelItem("subsection", numbertype, langtype, head == 2);
-               } else if (ctr == "subsubsection" && head <= 3) {
-                       s << numberLabel("subsection", numbertype, langtype, head)
-                         << labelItem("subsubsection", numbertype, langtype, head == 3);
-               } else if (ctr == "paragraph" && head <= 4) {
-                       s << numberLabel("subsubsection", numbertype, langtype, head)
-                         << labelItem("paragraph", numbertype, langtype, head == 4);
-               } else if (ctr == "subparagraph" && head <= 5) {
-                       s << numberLabel("paragraph", numbertype, langtype, head)
-                         << labelItem("subparagraph", numbertype, langtype, head == 5);
-               } else if (ctr == "Figure" || ctr == "Table") {
-                       // figure, table, ...
-                       lyxerr << "Counter:" << ctr << endl;
-                       s << numberLabel("chapter", numbertype, langtype, head)
-                         << labelItem(ctr, numbertype, langtype, head == 1);
-               }
+       string label = format;
+       while (true) {
+#ifdef WITH_WARNINGS
+#warning Using boost::regex would make this code a lot simpler... (Lgb)
+#endif
 
-       } else if (numbertype == "enumeration") {
-               ostringstream ei, eii, eiii, eiv;
-               //string ei, eiii, eiv;
-               //char eii;
-               if (langtype == "hebrew") {
-                       ei << '.' << value("enumi");
-                       eii << '(' << hebrewCounter(value("enumii")) << ')';
-                       eiii << '.' << romanCounter(value("enumiii"));
-                       eiv << '.' << alphaCounter(value("enumiv"));
-               } else {
-                       ei << value("enumi") << '.';
-                       eii << '(' << loweralphaCounter(value("enumii")) << ')';
-                       eiii << romanCounter(value("enumiii")) << '.';
-                       eiv << alphaCounter(value("enumiv")) << '.';
-               }
-               if (ctr == "enumii") {
-                       s << eii.str();
-               } else if (ctr == "enumi") {
-                       s << ei.str();
-               } else if (ctr == "enumiii") {
-                       s << eiii.str();
-               } else if (ctr == "enumiv") {
-                       s << eiv.str();
-               }
+               size_t const i = label.find('\\', 0);
+               if (i == string::npos)
+                       break;
+               size_t const j = label.find('{', i + 1);
+               if (j == string::npos)
+                       break;
+               size_t const k = label.find('}', j + 1);
+               if (k == string::npos)
+                       break;
+               string const numbertype(label, i + 1, j - i - 1);
+               string const counter(label, j + 1, k - j - 1);
+               string const rep = labelItem(counter, numbertype);
+               label = string(label, 0, i) + rep + string(label, k + 1, string::npos);
+               //lyxerr << "  : " << " (" << counter  << ","
+               //      << numbertype << ") -> " << label << endl;
        }
-       return s.str();
+       //lyxerr << "counterLabel: " << format  << " -> "       << label << endl;
+       return label;
+}
+
+
+string Counters::enumLabel(string const & ctr, string const & langtype)
+{
+       ostringstream os;
+
+       if (langtype == "hebrew") {
+               if (ctr == "enumi")
+                       os << '.' << value("enumi");
+               else if (ctr == "enumii")
+                       os << '(' << hebrewCounter(value("enumii")) << ')';
+               else if (ctr == "enumiii")
+                       os << '.' << lowerromanCounter(value("enumiii"));
+               else if (ctr == "enumiv")
+                       os << '.' << alphaCounter(value("enumiv"));
+       } else {
+               if (ctr == "enumi")
+                       os << value("enumi") << '.';
+               else if (ctr == "enumii")
+                       os << '(' << loweralphaCounter(value("enumii")) << ')';
+               else if (ctr == "enumiii")
+                       os << lowerromanCounter(value("enumiii")) << '.';
+               else if (ctr == "enumiv")
+                       os << alphaCounter(value("enumiv")) << '.';
+       }
+
+       return os.str();
 }