]> git.lyx.org Git - features.git/blob - src/BranchList.cpp
Prevent branch background color from leaking out of the document
[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 #include "ColorSet.h"
17
18 #include "frontends/Application.h"
19
20 #include "support/convert.h"
21 #include "support/lstrings.h"
22
23 #include <algorithm>
24
25 using namespace std;
26
27
28 namespace lyx {
29
30 Branch::Branch()
31         : selected_(false), filenameSuffix_(false)
32 {
33         lmcolor_ = "background";
34         dmcolor_ = "background";
35 }
36
37
38 docstring const & Branch::branch() const
39 {
40         return branch_;
41 }
42
43
44 void Branch::setBranch(docstring const & s)
45 {
46         branch_ = s;
47 }
48
49
50 bool Branch::isSelected() const
51 {
52         return selected_;
53 }
54
55
56 bool Branch::setSelected(bool b)
57 {
58         if (b == selected_)
59                 return false;
60         selected_ = b;
61         return true;
62 }
63
64
65 bool Branch::hasFileNameSuffix() const
66 {
67         return filenameSuffix_;
68 }
69
70
71 void Branch::setFileNameSuffix(bool b)
72 {
73         filenameSuffix_ = b;
74 }
75
76
77 string const & Branch::color() const
78 {
79         return (theApp() && theApp()->isInDarkMode())
80                         ? dmcolor_ : lmcolor_;
81 }
82
83
84 string const & Branch::lightModeColor() const
85 {
86         return lmcolor_;
87 }
88
89
90 string const & Branch::darkModeColor() const
91 {
92         return dmcolor_;
93 }
94
95
96 void Branch::setColor(string const & col)
97 {
98         if (theApp() && theApp()->isInDarkMode())
99                 setColors(string(), col);
100         else
101                 setColors(col);
102 }
103
104
105 void Branch::setColors(string const & lmcol, string const & dmcol)
106 {
107         if (lmcol.empty() && lmcolor_ == "background" && support::prefixIs(dmcol, "#"))
108                 lmcolor_ = X11hexname(inverseRGBColor(rgbFromHexName(dmcol)));
109         else if (!lmcol.empty())
110                 lmcolor_ = lmcol;
111         if (dmcol.empty() && dmcolor_ == "background" && support::prefixIs(lmcol, "#"))
112                 dmcolor_ = X11hexname(inverseRGBColor(rgbFromHexName(lmcol)));
113         else if (!dmcol.empty())
114                 dmcolor_ = dmcol;
115
116         // Update the Color table
117         string lmcolor = lmcolor_;
118         string dmcolor = dmcolor_;
119         if (lmcolor == "none")
120                 lmcolor = lcolor.getX11HexName(Color_background);
121         else if (lmcolor.size() != 7 || lmcolor[0] != '#')
122                 lmcolor = lcolor.getX11HexName(lcolor.getFromLyXName(lmcolor));
123         if (dmcolor == "none")
124                 lmcolor = lcolor.getX11HexName(Color_background, true);
125         else if (dmcolor.size() != 7 || dmcolor[0] != '#')
126                 dmcolor = lcolor.getX11HexName(lcolor.getFromLyXName(dmcolor), true);
127
128         // FIXME UNICODE
129         lcolor.setColor("branch" + convert<string>(branch_list_id_)
130                         + to_utf8(branch_), lmcolor, dmcolor);
131 }
132
133
134 namespace {
135
136 std::function<bool (Branch const &)> BranchNameIs(docstring const & d)
137 {
138         return [d](Branch const & b){ return b.branch() == d; };
139 }
140
141 } // namespace
142
143
144 Branch * BranchList::find(docstring const & name)
145 {
146         List::iterator it =
147                 find_if(list_.begin(), list_.end(), BranchNameIs(name));
148         return it == list_.end() ? nullptr : &*it;
149 }
150
151
152 Branch const * BranchList::find(docstring const & name) const
153 {
154         List::const_iterator it =
155                 find_if(list_.begin(), list_.end(), BranchNameIs(name));
156         return it == list_.end() ? nullptr : &*it;
157 }
158
159
160 bool BranchList::add(docstring const & s)
161 {
162         bool added = false;
163         size_t i = 0;
164         while (true) {
165                 size_t const j = s.find_first_of(separator_, i);
166                 docstring name;
167                 if (j == docstring::npos)
168                         name = s.substr(i);
169                 else
170                         name = s.substr(i, j - i);
171                 // Is this name already in the list?
172                 bool const already = find(name);
173                 if (!already) {
174                         added = true;
175                         Branch br;
176                         br.setBranch(name);
177                         br.setSelected(false);
178                         br.setFileNameSuffix(false);
179                         br.setListID(id_);
180                         list_.push_back(br);
181                 }
182                 if (j == docstring::npos)
183                         break;
184                 i = j + 1;
185         }
186         return added;
187 }
188
189
190 bool BranchList::remove(docstring const & s)
191 {
192         size_t const size = list_.size();
193         list_.remove_if(BranchNameIs(s));
194         return size != list_.size();
195 }
196
197
198 bool BranchList::rename(docstring const & oldname,
199         docstring const & newname, bool const merge)
200 {
201         if (newname.empty())
202                 return false;
203         if (find(newname)) {
204                 // new name already taken
205                 if (merge)
206                         return remove(oldname);
207                 return false;
208         }
209
210         Branch * branch = find(oldname);
211         if (!branch)
212                 return false;
213         branch->setBranch(newname);
214         return true;
215 }
216
217
218 docstring BranchList::getFileNameSuffix() const
219 {
220         docstring result;
221         for (auto const & br : list_) {
222                 if (br.isSelected() && br.hasFileNameSuffix())
223                         result += "-" + br.branch();
224         }
225         return support::subst(result, from_ascii("/"), from_ascii("_"));
226 }
227
228 } // namespace lyx