Prepare ANNOUNCE and NEWS for rc2
[lyx.git] / src / BranchList.cpp
blob8967fab12f52ca2c2f4cc7c8335c1fadffef284d
1 /**
2 * \file BranchList.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Martin Vermeer
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "BranchList.h"
14 #include "Color.h"
16 #include "frontends/Application.h"
18 #include <algorithm>
20 using namespace std;
22 namespace lyx {
24 namespace {
25 class BranchNamesEqual : public std::unary_function<Branch, bool> {
26 public:
27 BranchNamesEqual(docstring const & name)
28 : name_(name) {}
29 bool operator()(Branch const & branch) const
31 return branch.getBranch() == name_;
33 private:
34 docstring name_;
39 Branch::Branch() : selected_(false)
41 // no theApp() with command line export
42 if (theApp())
43 theApp()->getRgbColor(Color_background, color_);
47 docstring const & Branch::getBranch() const
49 return branch_;
53 void Branch::setBranch(docstring const & s)
55 branch_ = s;
59 bool Branch::getSelected() const
61 return selected_;
65 bool Branch::setSelected(bool b)
67 if (b == selected_)
68 return false;
69 selected_ = b;
70 return true;
74 RGBColor const & Branch::getColor() const
76 return color_;
80 void Branch::setColor(RGBColor const & c)
82 color_ = c;
86 void Branch::setColor(string const & str)
88 if (str.size() == 7 && str[0] == '#')
89 color_ = rgbFromHexName(str);
90 else
91 // no color set or invalid color - use normal background
92 theApp()->getRgbColor(Color_background, color_);
96 Branch * BranchList::find(docstring const & name)
98 List::iterator it =
99 find_if(list.begin(), list.end(), BranchNamesEqual(name));
100 return it == list.end() ? 0 : &*it;
104 Branch const * BranchList::find(docstring const & name) const
106 List::const_iterator it =
107 find_if(list.begin(), list.end(), BranchNamesEqual(name));
108 return it == list.end() ? 0 : &*it;
112 bool BranchList::add(docstring const & s)
114 bool added = false;
115 docstring::size_type i = 0;
116 while (true) {
117 docstring::size_type const j = s.find_first_of(separator_, i);
118 docstring name;
119 if (j == docstring::npos)
120 name = s.substr(i);
121 else
122 name = s.substr(i, j - i);
123 // Is this name already in the list?
124 bool const already =
125 find_if(list.begin(), list.end(),
126 BranchNamesEqual(name)) != list.end();
127 if (!already) {
128 added = true;
129 Branch br;
130 br.setBranch(name);
131 br.setSelected(false);
132 list.push_back(br);
134 if (j == docstring::npos)
135 break;
136 i = j + 1;
138 return added;
142 bool BranchList::remove(docstring const & s)
144 size_t const size = list.size();
145 list.remove_if(BranchNamesEqual(s));
146 return size != list.size();
150 } // namespace lyx