]> git.lyx.org Git - lyx.git/blobdiff - src/BranchList.C
Real fix from Bernhard Roider
[lyx.git] / src / BranchList.C
index f9ab44df823e0681dddd30fe498190a42c0c7cf8..9cec82928edfc11763b0efd08fb944f850946958 100644 (file)
@@ -1,32 +1,41 @@
 /**
- * \file BranchList.C 
+ * \file BranchList.C
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
  * \author Martin Vermeer
- * 
+ *
  * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
 #include "BranchList.h"
-#include "support/LAssert.h"
+#include "LColor.h"
+
+#include "frontends/Application.h"
+
+#include <algorithm>
 
-#include <functional>
 
-using std::bind2nd;
-using std::binary_function;
-using namespace lyx::support;
+namespace lyx {
 
+using std::string;
 
-string const Branch::getBranch() const
+
+Branch::Branch()
+{
+       theApp()->getRgbColor(LColor::background, color_);
+}
+
+
+docstring const & Branch::getBranch() const
 {
        return branch_;
 }
 
 
-void Branch::setBranch(string const & s)
+void Branch::setBranch(docstring const & s)
 {
        branch_ = s;
 }
@@ -38,167 +47,89 @@ bool Branch::getSelected() const
 }
 
 
-void Branch::setSelected(bool b)
+bool Branch::setSelected(bool b)
 {
+       if (b == selected_)
+               return false;
        selected_ = b;
+       return true;
 }
 
 
-string const Branch::getColor() const
+RGBColor const & Branch::getColor() const
 {
        return color_;
 }
 
 
-void Branch::setColor(string const & c)
+void Branch::setColor(RGBColor const & c)
 {
        color_ = c;
 }
 
 
-void BranchList::clear()
-{
-       list.clear();
-}
-
-
-string BranchList::getColor(string const & s) const
+void Branch::setColor(string const & c)
 {
-       List::const_iterator it = list.begin();
-       List::const_iterator end = list.end();
-       for (; it != end; ++it) {
-               if (s == it->getBranch()) {
-                       return it->getColor();
-               }
-       }
-       Assert(false); // Always
-       return string(); // never gets here
+       if (c.size() == 7 && c[0] == '#')
+               color_ = RGBColor(c);
+       else
+               // no color set or invalid color - use normal background
+               theApp()->getRgbColor(LColor::background, color_);
 }
 
 
-
-void BranchList::setColor(string const & s, string const & val)
+Branch * BranchList::find(docstring const & name)
 {
-       List::iterator it = list.begin();
-       List::iterator end = list.end();
-       for (; it != end; ++it) {
-               if (s == it->getBranch()) {
-                       it->setColor(val);
-                       return;
-               }
-       }
-       Assert(false);
+       List::iterator it =
+               std::find_if(list.begin(), list.end(), BranchNamesEqual(name));
+       return it == list.end() ? 0 : &*it;
 }
 
 
-void BranchList::setSelected(string const & s, bool val)
+Branch const * BranchList::find(docstring const & name) const
 {
-       List::iterator it = list.begin();
-       List::iterator end = list.end();
-       for (; it != end; ++it) {
-               if (s.find(it->getBranch(), 0) != string::npos)
-                       it->setSelected(val);
-       }
+       List::const_iterator it =
+               std::find_if(list.begin(), list.end(), BranchNamesEqual(name));
+       return it == list.end() ? 0 : &*it;
 }
 
 
-void BranchList::add(string const & s)
+bool BranchList::add(docstring const & s)
 {
-       string::size_type i = 0;
+       bool added = false;
+       docstring::size_type i = 0;
        while (true) {
-               string::size_type const j = s.find_first_of(separator(), i);
-               string name;
-               if (j == string::npos)
+               docstring::size_type const j = s.find_first_of(separator_, i);
+               docstring name;
+               if (j == docstring::npos)
                        name = s.substr(i);
                else
                        name = s.substr(i, j - i);
                // Is this name already in the list?
-               List::const_iterator it = list.begin();
-               List::const_iterator end = list.end();
-               bool already = false;
-               for (; it != end; ++it) {
-                       if (it->getBranch() == name) {
-                               already = true;
-                               break;
-                       }
-               }
+               bool const already =
+                       std::find_if(list.begin(), list.end(),
+                                    BranchNamesEqual(name)) != list.end();
                if (!already) {
+                       added = true;
                        Branch br;
                        br.setBranch(name);
                        br.setSelected(false);
-                       br.setColor("none");
                        list.push_back(br);
                }
-               if (j == string::npos)
+               if (j == docstring::npos)
                        break;
-               i = j + 1; 
+               i = j + 1;
        }
+       return added;
 }
 
 
-namespace {
-
-struct match : public binary_function<Branch, string, bool> {
-       bool operator()(Branch const & br, string const & s) const {
-       return (br.getBranch() == s);
-       }                                                                       
-};
-
-} // namespace anon.
-
-
-void BranchList::remove(string const & s)
-{
-       list.remove_if(bind2nd(match(), s));
-}
-
-
-bool BranchList::selected(string const & s) const
-{
-       List::const_iterator it = list.begin();
-       List::const_iterator end = list.end();
-       for (; it != end; ++it) {
-               if (s == it->getBranch()) 
-                       return it->getSelected();
-       }
-       return false;
-}
-
-
-string BranchList::allBranches() const
+bool BranchList::remove(docstring const & s)
 {
-       List::const_iterator it = list.begin();
-       List::const_iterator end = list.end();
-       string ret;
-       for (; it != end; ++it) {
-               ret += it->getBranch() + separator();
-       }
-       // remove final '|':
-       string::size_type i = ret.find_last_of(separator());
-       if (i != string::npos)
-               ret.erase(i);
-       return ret;
-}
-
-
-string BranchList::allSelected() const
-{      
-       List::const_iterator it = list.begin();
-       List::const_iterator end = list.end();
-       string ret;
-       for (; it != end; ++it) {
-               if (it->getSelected()) 
-                       ret += it->getBranch() + separator();
-       }
-       // remove final '|':
-       string::size_type i = ret.find_last_of(separator());
-       if (i != string::npos)
-               ret.erase(i);
-       return ret;
+       size_t const size = list.size();
+       list.remove_if(BranchNamesEqual(s));
+       return size != list.size();
 }
 
 
-string const BranchList::separator() const
-{
-       return separator_;
-}
+} // namespace lyx