]> git.lyx.org Git - lyx.git/blob - src/BranchList.cpp
Rename a couple routines in preparation for more.
[lyx.git] / src / BranchList.cpp
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.
5  *
6  * \author Martin Vermeer
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "BranchList.h"
14 #include "Color.h"
15
16 #include "frontends/Application.h"
17
18 #include <algorithm>
19
20 using namespace std;
21
22
23 namespace lyx {
24
25 namespace {
26
27 class BranchNamesEqual : public std::unary_function<Branch, bool>
28 {
29 public:
30         BranchNamesEqual(docstring const & name) : name_(name) {}
31
32         bool operator()(Branch const & branch) const
33         {
34                 return branch.branch() == name_;
35         }
36 private:
37         docstring name_;
38 };
39 }
40
41
42 Branch::Branch() : selected_(false)
43 {
44         // no theApp() with command line export
45         if (theApp())
46                 theApp()->getRgbColor(Color_background, color_);
47 }
48
49
50 docstring const & Branch::branch() const
51 {
52         return branch_;
53 }
54
55
56 void Branch::setBranch(docstring const & s)
57 {
58         branch_ = s;
59 }
60
61
62 bool Branch::isSelected() const
63 {
64         return selected_;
65 }
66
67
68 bool Branch::setSelected(bool b)
69 {
70         if (b == selected_)
71                 return false;
72         selected_ = b;
73         return true;
74 }
75
76
77 RGBColor const & Branch::color() const
78 {
79         return color_;
80 }
81
82
83 void Branch::setColor(RGBColor const & c)
84 {
85         color_ = c;
86 }
87
88
89 void Branch::setColor(string const & str)
90 {
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_);
96 }
97
98
99 Branch * BranchList::find(docstring const & name)
100 {
101         List::iterator it =
102                 find_if(list.begin(), list.end(), BranchNamesEqual(name));
103         return it == list.end() ? 0 : &*it;
104 }
105
106
107 Branch const * BranchList::find(docstring const & name) const
108 {
109         List::const_iterator it =
110                 find_if(list.begin(), list.end(), BranchNamesEqual(name));
111         return it == list.end() ? 0 : &*it;
112 }
113
114
115 bool BranchList::add(docstring const & s)
116 {
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);
136                 }
137                 if (j == docstring::npos)
138                         break;
139                 i = j + 1;
140         }
141         return added;
142 }
143
144
145 bool BranchList::remove(docstring const & s)
146 {
147         size_t const size = list.size();
148         list.remove_if(BranchNamesEqual(s));
149         return size != list.size();
150 }
151
152
153 } // namespace lyx