]> git.lyx.org Git - lyx.git/blob - src/BranchList.C
minimal effort implementation of:
[lyx.git] / src / BranchList.C
1 /**
2  * \file BranchList.C
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 "LColor.h"
15 #include "frontends/lyx_gui.h"
16 #include <algorithm>
17
18 using std::string;
19
20
21 Branch::Branch()
22 {
23         lyx_gui::getRGBColor(LColor::background, color_);
24 }
25
26
27 string const & Branch::getBranch() const
28 {
29         return branch_;
30 }
31
32
33 void Branch::setBranch(string const & s)
34 {
35         branch_ = s;
36 }
37
38
39 bool Branch::getSelected() const
40 {
41         return selected_;
42 }
43
44
45 bool Branch::setSelected(bool b)
46 {
47         if (b == selected_)
48                 return false;
49         selected_ = b;
50         return true;
51 }
52
53
54 lyx::RGBColor const & Branch::getColor() const
55 {
56         return color_;
57 }
58
59
60 void Branch::setColor(lyx::RGBColor const & c)
61 {
62         color_ = c;
63 }
64
65
66 void Branch::setColor(string const & c)
67 {
68         if (c.size() == 7 && c[0] == '#')
69                 color_ = lyx::RGBColor(c);
70         else
71                 // no color set or invalid color - use normal background
72                 lyx_gui::getRGBColor(LColor::background, color_);
73 }
74
75
76 Branch * BranchList::find(std::string const & name)
77 {
78         List::iterator it =
79                 std::find_if(list.begin(), list.end(), BranchNamesEqual(name));
80         return it == list.end() ? 0 : &*it;
81 }
82
83
84 Branch const * BranchList::find(std::string const & name) const
85 {
86         List::const_iterator it =
87                 std::find_if(list.begin(), list.end(), BranchNamesEqual(name));
88         return it == list.end() ? 0 : &*it;
89 }
90
91
92 bool BranchList::add(string const & s)
93 {
94         bool added = false;
95         string::size_type i = 0;
96         while (true) {
97                 string::size_type const j = s.find_first_of(separator_, i);
98                 string name;
99                 if (j == string::npos)
100                         name = s.substr(i);
101                 else
102                         name = s.substr(i, j - i);
103                 // Is this name already in the list?
104                 bool const already =
105                         std::find_if(list.begin(), list.end(),
106                                      BranchNamesEqual(name)) != list.end();
107                 if (!already) {
108                         added = true;
109                         Branch br;
110                         br.setBranch(name);
111                         br.setSelected(false);
112                         list.push_back(br);
113                 }
114                 if (j == string::npos)
115                         break;
116                 i = j + 1;
117         }
118         return added;
119 }
120
121
122 bool BranchList::remove(string const & s)
123 {
124         List::size_type const size = list.size();
125         list.remove_if(BranchNamesEqual(s));
126         return size != list.size();
127 }