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