3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Martin Vermeer
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "BranchList.h"
18 #include "frontends/Application.h"
20 #include "support/convert.h"
21 #include "support/lstrings.h"
30 docstring const & Branch::branch() const
36 void Branch::setBranch(docstring const & s)
42 bool Branch::isSelected() const
48 bool Branch::setSelected(bool b)
57 bool Branch::hasFileNameSuffix() const
59 return filenameSuffix_;
63 void Branch::setFileNameSuffix(bool b)
69 string const & Branch::color() const
71 return (theApp() && theApp()->isInDarkMode())
72 ? dmcolor_ : lmcolor_;
76 string const & Branch::lightModeColor() const
82 string const & Branch::darkModeColor() const
88 void Branch::setColor(string const & col)
90 if (theApp() && theApp()->isInDarkMode())
91 setColors(string(), col);
97 void Branch::setColors(string const & lmcol, string const & dmcol)
99 if (lmcol.empty() && lmcolor_ == "background" && support::prefixIs(dmcol, "#"))
100 lmcolor_ = X11hexname(inverseRGBColor(rgbFromHexName(dmcol)));
101 else if (!lmcol.empty())
103 if (dmcol.empty() && dmcolor_ == "background" && support::prefixIs(lmcol, "#"))
104 dmcolor_ = X11hexname(inverseRGBColor(rgbFromHexName(lmcol)));
105 else if (!dmcol.empty())
108 // Update the Color table
109 string lmcolor = lmcolor_;
110 string dmcolor = dmcolor_;
111 if (lmcolor == "none")
112 lmcolor = lcolor.getX11HexName(Color_background);
113 else if (lmcolor.size() != 7 || lmcolor[0] != '#')
114 lmcolor = lcolor.getX11HexName(lcolor.getFromLyXName(lmcolor));
115 if (dmcolor == "none")
116 dmcolor = lcolor.getX11HexName(Color_background, true);
117 else if (dmcolor.size() != 7 || dmcolor[0] != '#')
118 dmcolor = lcolor.getX11HexName(lcolor.getFromLyXName(dmcolor), true);
121 lcolor.setColor("branch" + convert<string>(branch_list_id_)
122 + to_utf8(branch_), lmcolor, dmcolor);
128 std::function<bool (Branch const &)> BranchNameIs(docstring const & d)
130 return [d](Branch const & b){ return b.branch() == d; };
136 Branch * BranchList::find(docstring const & name)
139 find_if(list_.begin(), list_.end(), BranchNameIs(name));
140 return it == list_.end() ? nullptr : &*it;
144 Branch const * BranchList::find(docstring const & name) const
146 List::const_iterator it =
147 find_if(list_.begin(), list_.end(), BranchNameIs(name));
148 return it == list_.end() ? nullptr : &*it;
152 bool BranchList::add(docstring const & s)
157 size_t const j = s.find_first_of(separator_, i);
159 if (j == docstring::npos)
162 name = s.substr(i, j - i);
163 // Is this name already in the list?
164 bool const already = find(name);
169 br.setSelected(false);
170 br.setFileNameSuffix(false);
174 if (j == docstring::npos)
182 bool BranchList::remove(docstring const & s)
184 size_t const size = list_.size();
185 list_.remove_if(BranchNameIs(s));
186 return size != list_.size();
190 bool BranchList::rename(docstring const & oldname,
191 docstring const & newname, bool const merge)
196 // new name already taken
198 return remove(oldname);
202 Branch * branch = find(oldname);
205 branch->setBranch(newname);
210 docstring BranchList::getFileNameSuffix() const
213 for (auto const & br : list_) {
214 if (br.isSelected() && br.hasFileNameSuffix())
215 result += "-" + br.branch();
217 return support::subst(result, from_ascii("/"), from_ascii("_"));