whitespace.
[lyx.git] / src / BranchList.cpp
bloba593c5478fc91e9b421537f6cc849b32b2590238
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;
23 namespace lyx {
25 namespace {
27 class BranchNamesEqual : public std::unary_function<Branch, bool>
29 public:
30 BranchNamesEqual(docstring const & name) : name_(name) {}
32 bool operator()(Branch const & branch) const
34 return branch.branch() == name_;
36 private:
37 docstring name_;
42 Branch::Branch() : selected_(false)
44 // no theApp() with command line export
45 if (theApp())
46 theApp()->getRgbColor(Color_background, color_);
50 docstring const & Branch::branch() const
52 return branch_;
56 void Branch::setBranch(docstring const & s)
58 branch_ = s;
62 bool Branch::isSelected() const
64 return selected_;
68 bool Branch::setSelected(bool b)
70 if (b == selected_)
71 return false;
72 selected_ = b;
73 return true;
77 RGBColor const & Branch::color() const
79 return color_;
83 void Branch::setColor(RGBColor const & c)
85 color_ = c;
89 void Branch::setColor(string const & str)
91 if (str.size() == 7 && str[0] == '#')
92 color_ = rgbFromHexName(str);
93 else
94 // no color set or invalid color - use normal background
95 theApp()->getRgbColor(Color_background, color_);
99 Branch * BranchList::find(docstring const & name)
101 List::iterator it =
102 find_if(list.begin(), list.end(), BranchNamesEqual(name));
103 return it == list.end() ? 0 : &*it;
107 Branch const * BranchList::find(docstring const & name) const
109 List::const_iterator it =
110 find_if(list.begin(), list.end(), BranchNamesEqual(name));
111 return it == list.end() ? 0 : &*it;
115 bool BranchList::add(docstring const & s)
117 bool added = false;
118 size_t i = 0;
119 while (true) {
120 size_t const j = s.find_first_of(separator_, i);
121 docstring name;
122 if (j == docstring::npos)
123 name = s.substr(i);
124 else
125 name = s.substr(i, j - i);
126 // Is this name already in the list?
127 bool const already =
128 find_if(list.begin(), list.end(),
129 BranchNamesEqual(name)) != list.end();
130 if (!already) {
131 added = true;
132 Branch br;
133 br.setBranch(name);
134 br.setSelected(false);
135 list.push_back(br);
137 if (j == docstring::npos)
138 break;
139 i = j + 1;
141 return added;
145 bool BranchList::remove(docstring const & s)
147 size_t const size = list.size();
148 list.remove_if(BranchNamesEqual(s));
149 return size != list.size();
153 } // namespace lyx