]> git.lyx.org Git - lyx.git/blob - src/BranchList.cpp
* GuiToolbar.cpp:
[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 namespace lyx {
23
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
30         {
31                 return branch.getBranch() == name_;
32         }
33 private:
34         docstring name_;
35 };
36 }
37
38
39 Branch::Branch() : selected_(false)
40 {
41         // no theApp() with command line export
42         if (theApp())
43                 theApp()->getRgbColor(Color_background, color_);
44 }
45
46
47 docstring const & Branch::getBranch() const
48 {
49         return branch_;
50 }
51
52
53 void Branch::setBranch(docstring const & s)
54 {
55         branch_ = s;
56 }
57
58
59 bool Branch::getSelected() const
60 {
61         return selected_;
62 }
63
64
65 bool Branch::setSelected(bool b)
66 {
67         if (b == selected_)
68                 return false;
69         selected_ = b;
70         return true;
71 }
72
73
74 RGBColor const & Branch::getColor() const
75 {
76         return color_;
77 }
78
79
80 void Branch::setColor(RGBColor const & c)
81 {
82         color_ = c;
83 }
84
85
86 void Branch::setColor(string const & str)
87 {
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_);
93 }
94
95
96 Branch * BranchList::find(docstring const & name)
97 {
98         List::iterator it =
99                 find_if(list.begin(), list.end(), BranchNamesEqual(name));
100         return it == list.end() ? 0 : &*it;
101 }
102
103
104 Branch const * BranchList::find(docstring const & name) const
105 {
106         List::const_iterator it =
107                 find_if(list.begin(), list.end(), BranchNamesEqual(name));
108         return it == list.end() ? 0 : &*it;
109 }
110
111
112 bool BranchList::add(docstring const & s)
113 {
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);
133                 }
134                 if (j == docstring::npos)
135                         break;
136                 i = j + 1;
137         }
138         return added;
139 }
140
141
142 bool BranchList::remove(docstring const & s)
143 {
144         size_t const size = list.size();
145         list.remove_if(BranchNamesEqual(s));
146         return size != list.size();
147 }
148
149
150 } // namespace lyx