]> git.lyx.org Git - lyx.git/blobdiff - src/counters.C
Add GTK bibitem dialog
[lyx.git] / src / counters.C
index 2a72bf99f4f50f1409a6c18a0c0438db6748f140..573eeb63c35ee7311eb709a70727c1263a74ad91 100644 (file)
@@ -1,14 +1,30 @@
+/**
+ * \file counters.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Lars Gullik Bjønnes
+ * \author Martin Vermeer
+ * \author André Pönitz
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
 #include <config.h>
 
 #include "counters.h"
 #include "debug.h"
 
-#ifdef SIGC_CXX_NAMESPACES
-using SigC::Connection;
-using SigC::slot;
-#endif
+#include "support/lstrings.h"
+#include "support/convert.h"
+
+#include <boost/assert.hpp>
+
+#include <sstream>
 
 using std::endl;
+using std::ostringstream;
+using std::string;
 
 
 Counter::Counter()
@@ -38,7 +54,6 @@ int Counter::value() const
 void Counter::step()
 {
        ++value_;
-       onstep.emit();
 }
 
 
@@ -48,84 +63,82 @@ void Counter::reset()
 }
 
 
-Counters::~Counters() 
+string Counter::master() const
 {
-       // We need this since we store the Counter's as pointers in
-       // the counterList.
-       for (CounterList::iterator it = counterList.begin();
-            it != counterList.end();
-            ++it)
-               delete (*it).second;
+       return master_;
+}
+
+
+void Counter::setMaster(string const & m)
+{
+       master_ = m;
 }
 
 
 void Counters::newCounter(string const & newc)
 {
        // First check if newc already exist
-       CounterList::const_iterator cit = counterList.find(newc);
-       // if alrady exist give warning and return
+       CounterList::iterator const cit = counterList.find(newc);
+       // if already exist give warning and return
        if (cit != counterList.end()) {
-               lyxerr << "The new counter already exist." << endl;
+               lyxerr << "New counter already exists: " << newc << endl;
                return;
        }
-       counterList[newc] = new Counter;
+       counterList[newc];
 }
 
 
-void Counters::newCounter(string const & newc, string const & oldc)
+void Counters::newCounter(string const & newc, string const & masterc)
 {
-       // First check if newc already exist
-       CounterList::const_iterator cit = counterList.find(newc);
+       // First check if newc already exists
+       CounterList::iterator const cit = counterList.find(newc);
        // if already existant give warning and return
        if (cit != counterList.end()) {
-               lyxerr << "The new counter already exist." << endl;
+               lyxerr << "New counter already exists: " << newc << endl;
                return;
        }
-       // then check if oldc exist
-       CounterList::iterator it = counterList.find(oldc);
+       // then check if masterc exists
+       CounterList::iterator const it = counterList.find(masterc);
        // if not give warning and return
        if (it == counterList.end()) {
-               lyxerr << "The old counter does not exist." << endl;
+               lyxerr << "Master counter does not exist: " << masterc << endl;
                return;
        }
 
-       Counter * tmp = new Counter;
-       (*it).second->onstep.connect(slot(tmp,
-                                        &Counter::reset));
-       counterList[newc] = tmp;
+       counterList[newc].setMaster(masterc);
 }
 
 
-void Counters::set(string const & ctr, int val) 
+void Counters::set(string const & ctr, int const val)
 {
-       CounterList::iterator it = counterList.find(ctr);
+       CounterList::iterator const it = counterList.find(ctr);
        if (it == counterList.end()) {
-               lyxerr << "Counter does not exist." << endl;
+               lyxerr << "set: Counter does not exist: " << ctr << endl;
                return;
        }
-       (*it).second->set(val);
+       it->second.set(val);
 }
 
 
-void Counters::addto(string const & ctr, int val)
+void Counters::addto(string const & ctr, int const val)
 {
-       CounterList::iterator it = counterList.find(ctr);
+       CounterList::iterator const it = counterList.find(ctr);
        if (it == counterList.end()) {
-               lyxerr << "Counter does not exist." << endl;
+               lyxerr << "addto: Counter does not exist: " << ctr << endl;
                return;
        }
-       (*it).second->addto(val);
+       it->second.addto(val);
 }
 
 
-int Counters::value(string const & ctr) const 
+int Counters::value(string const & ctr) const
 {
-       CounterList::const_iterator cit = counterList.find(ctr);
+       CounterList::const_iterator const cit = counterList.find(ctr);
        if (cit == counterList.end()) {
-               lyxerr << "Counter does not exist." << endl;
+               lyxerr << "value: Counter does not exist: " << ctr << endl;
                return 0;
        }
-       return (*cit).second->value();
+       return cit->second.value();
 }
 
 
@@ -133,8 +146,170 @@ void Counters::step(string const & ctr)
 {
        CounterList::iterator it = counterList.find(ctr);
        if (it == counterList.end()) {
-               lyxerr << "Counter does not exist." << endl;
+               lyxerr << "step: Counter does not exist: " << ctr << endl;
                return;
        }
-       (*it).second->step();
+
+       it->second.step();
+       it = counterList.begin();
+       CounterList::iterator const end = counterList.end();
+       for (; it != end; ++it) {
+               if (it->second.master() == ctr) {
+                       it->second.reset();
+               }
+       }
+}
+
+
+void Counters::reset()
+{
+       CounterList::iterator it = counterList.begin();
+       CounterList::iterator const end = counterList.end();
+       for (; it != end; ++it) {
+               it->second.reset();
+       }
+}
+
+
+void Counters::reset(string const & match)
+{
+       BOOST_ASSERT(!match.empty());
+
+       CounterList::iterator it = counterList.begin();
+       CounterList::iterator end = counterList.end();
+       for (; it != end; ++it) {
+               if (it->first.find(match) != string::npos)
+                       it->second.reset();
+       }
+}
+
+
+void Counters::copy(Counters & from, Counters & to, string const & match)
+{
+       CounterList::iterator it = counterList.begin();
+       CounterList::iterator end = counterList.end();
+       for (; it != end; ++it) {
+               if (it->first.find(match) != string::npos || match == "") {
+                       to.set(it->first, from.value(it->first));
+               }
+       }
+}
+
+
+namespace {
+
+char loweralphaCounter(int const n)
+{
+       if (n < 1 || n > 26)
+               return '?';
+       return 'a' + n - 1;
+}
+
+
+char alphaCounter(int const n)
+{
+       if (n < 1 || n > 26)
+               return '?';
+       return 'A' + n - 1;
+}
+
+
+char hebrewCounter(int const n)
+{
+       static const char hebrew[22] = {
+               'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
+               'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
+               '÷', 'ø', 'ù', 'ú'
+       };
+
+       if (n < 1 || n > 22)
+               return '?';
+       return hebrew[n - 1];
+}
+
+
+string const lowerromanCounter(int const n)
+{
+       static char const * 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 "??";
+       return roman[n - 1];
+}
+
+
+string const romanCounter(int const n)
+{
+       static char const * 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 "??";
+       return roman[n - 1];
+}
+
+} // namespace anon
+
+
+string Counters::labelItem(string const & ctr, string const & numbertype)
+{
+       if (counterList.find(ctr) == counterList.end()) {
+               lyxerr << "Counter " << ctr << " does not exist." << endl;
+               return string();
+       }
+
+       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 convert<string>(value(ctr));
+}
+
+
+string Counters::counterLabel(string const & format)
+{
+       string label = format;
+       while (true) {
+#ifdef WITH_WARNINGS
+#warning Using boost::regex or boost::spirit would make this code a lot simpler... (Lgb)
+#endif
+
+               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;
+       }
+       //lyxerr << "counterLabel: " << format  << " -> "       << label << endl;
+       return label;
 }