]> git.lyx.org Git - lyx.git/blob - src/BranchList.cpp
Change string
[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
25 Branch::Branch() : selected_(false)
26 {
27         // no theApp() with command line export
28         if (theApp())
29                 theApp()->getRgbColor(Color_background, color_);
30 }
31
32
33 docstring const & Branch::getBranch() const
34 {
35         return branch_;
36 }
37
38
39 void Branch::setBranch(docstring const & s)
40 {
41         branch_ = s;
42 }
43
44
45 bool Branch::getSelected() const
46 {
47         return selected_;
48 }
49
50
51 bool Branch::setSelected(bool b)
52 {
53         if (b == selected_)
54                 return false;
55         selected_ = b;
56         return true;
57 }
58
59
60 RGBColor const & Branch::getColor() const
61 {
62         return color_;
63 }
64
65
66 void Branch::setColor(RGBColor const & c)
67 {
68         color_ = c;
69 }
70
71
72 void Branch::setColor(string const & str)
73 {
74         if (str.size() == 7 && str[0] == '#')
75                 color_ = rgbFromHexName(str);
76         else
77                 // no color set or invalid color - use normal background
78                 theApp()->getRgbColor(Color_background, color_);
79 }
80
81
82 Branch * BranchList::find(docstring const & name)
83 {
84         List::iterator it =
85                 find_if(list.begin(), list.end(), BranchNamesEqual(name));
86         return it == list.end() ? 0 : &*it;
87 }
88
89
90 Branch const * BranchList::find(docstring const & name) const
91 {
92         List::const_iterator it =
93                 find_if(list.begin(), list.end(), BranchNamesEqual(name));
94         return it == list.end() ? 0 : &*it;
95 }
96
97
98 bool BranchList::add(docstring const & s)
99 {
100         bool added = false;
101         docstring::size_type i = 0;
102         while (true) {
103                 docstring::size_type const j = s.find_first_of(separator_, i);
104                 docstring name;
105                 if (j == docstring::npos)
106                         name = s.substr(i);
107                 else
108                         name = s.substr(i, j - i);
109                 // Is this name already in the list?
110                 bool const already =
111                         find_if(list.begin(), list.end(),
112                                      BranchNamesEqual(name)) != list.end();
113                 if (!already) {
114                         added = true;
115                         Branch br;
116                         br.setBranch(name);
117                         br.setSelected(false);
118                         list.push_back(br);
119                 }
120                 if (j == docstring::npos)
121                         break;
122                 i = j + 1;
123         }
124         return added;
125 }
126
127
128 bool BranchList::remove(docstring const & s)
129 {
130         size_t const size = list.size();
131         list.remove_if(BranchNamesEqual(s));
132         return size != list.size();
133 }
134
135
136 } // namespace lyx