]> git.lyx.org Git - lyx.git/blob - src/BranchList.cpp
BranchList:
[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  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "BranchList.h"
15 #include "Color.h"
16
17 #include "frontends/Application.h"
18
19 #include "support/lstrings.h"
20
21 #include <algorithm>
22
23 using namespace std;
24
25
26 namespace lyx {
27
28 namespace {
29
30 class BranchNamesEqual : public std::unary_function<Branch, bool>
31 {
32 public:
33         BranchNamesEqual(docstring const & name) : name_(name) {}
34
35         bool operator()(Branch const & branch) const
36         {
37                 return branch.branch() == name_;
38         }
39 private:
40         docstring name_;
41 };
42 }
43
44
45 Branch::Branch()
46         : selected_(false), filenameSuffix_(false)
47 {
48         // no theApp() with command line export
49         if (theApp())
50                 theApp()->getRgbColor(Color_background, color_);
51 }
52
53
54 docstring const & Branch::branch() const
55 {
56         return branch_;
57 }
58
59
60 void Branch::setBranch(docstring const & s)
61 {
62         branch_ = s;
63 }
64
65
66 bool Branch::isSelected() const
67 {
68         return selected_;
69 }
70
71
72 bool Branch::setSelected(bool b)
73 {
74         if (b == selected_)
75                 return false;
76         selected_ = b;
77         return true;
78 }
79
80
81 bool Branch::hasFileNameSuffix() const
82 {
83         return filenameSuffix_;
84 }
85
86
87 void Branch::setFileNameSuffix(bool b)
88 {
89         filenameSuffix_ = b;
90 }
91
92
93 RGBColor const & Branch::color() const
94 {
95         return color_;
96 }
97
98
99 void Branch::setColor(RGBColor const & c)
100 {
101         color_ = c;
102 }
103
104
105 void Branch::setColor(string const & str)
106 {
107         if (str.size() == 7 && str[0] == '#')
108                 color_ = rgbFromHexName(str);
109         else
110                 // no color set or invalid color - use normal background
111                 theApp()->getRgbColor(Color_background, color_);
112 }
113
114
115 Branch * BranchList::find(docstring const & name)
116 {
117         List::iterator it =
118                 find_if(list.begin(), list.end(), BranchNamesEqual(name));
119         return it == list.end() ? 0 : &*it;
120 }
121
122
123 Branch const * BranchList::find(docstring const & name) const
124 {
125         List::const_iterator it =
126                 find_if(list.begin(), list.end(), BranchNamesEqual(name));
127         return it == list.end() ? 0 : &*it;
128 }
129
130
131 bool BranchList::add(docstring const & s)
132 {
133         bool added = false;
134         size_t i = 0;
135         while (true) {
136                 size_t const j = s.find_first_of(separator_, i);
137                 docstring name;
138                 if (j == docstring::npos)
139                         name = s.substr(i);
140                 else
141                         name = s.substr(i, j - i);
142                 // Is this name already in the list?
143                 bool const already =
144                         find_if(list.begin(), list.end(),
145                                      BranchNamesEqual(name)) != list.end();
146                 if (!already) {
147                         added = true;
148                         Branch br;
149                         br.setBranch(name);
150                         br.setSelected(false);
151                         br.setFileNameSuffix(false);
152                         list.push_back(br);
153                 }
154                 if (j == docstring::npos)
155                         break;
156                 i = j + 1;
157         }
158         return added;
159 }
160
161
162 bool BranchList::remove(docstring const & s)
163 {
164         size_t const size = list.size();
165         list.remove_if(BranchNamesEqual(s));
166         return size != list.size();
167 }
168
169
170 bool BranchList::rename(docstring const & oldname,
171         docstring const & newname, bool const merge)
172 {
173         if (newname.empty())
174                 return false;
175         if (find_if(list.begin(), list.end(),
176                     BranchNamesEqual(newname)) != list.end()) {
177                 // new name already taken
178                 if (merge)
179                       return remove(oldname);
180                 return false;
181         }
182
183         Branch * branch = find(oldname);
184         if (!branch)
185                 return false;
186         branch->setBranch(newname);
187         return true;
188 }
189
190
191 docstring BranchList::getFileNameSuffix() const
192 {
193         docstring result;
194         List::const_iterator it = list.begin();
195         for (; it != list.end(); ++it) {
196                 if (it->isSelected() && it->hasFileNameSuffix())
197                         result += "-" + it->branch();
198         }
199         return support::subst(result, from_ascii("/"), from_ascii("_"));
200 }
201
202 } // namespace lyx