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"
31 : selected_(false), filenameSuffix_(false)
33 lmcolor_ = "background";
34 dmcolor_ = "background";
38 docstring const & Branch::branch() const
44 void Branch::setBranch(docstring const & s)
50 bool Branch::isSelected() const
56 bool Branch::setSelected(bool b)
65 bool Branch::hasFileNameSuffix() const
67 return filenameSuffix_;
71 void Branch::setFileNameSuffix(bool b)
77 string const & Branch::color() const
79 return (theApp() && theApp()->isInDarkMode())
80 ? dmcolor_ : lmcolor_;
84 string const & Branch::lightModeColor() const
90 string const & Branch::darkModeColor() const
96 void Branch::setColor(string const & col)
98 if (theApp() && theApp()->isInDarkMode())
99 setColors(string(), col);
105 void Branch::setColors(string const & lmcol, string const & dmcol)
107 if (lmcol.empty() && lmcolor_ == "background" && support::prefixIs(dmcol, "#"))
108 lmcolor_ = X11hexname(inverseRGBColor(rgbFromHexName(dmcol)));
109 else if (!lmcol.empty())
111 if (dmcol.empty() && dmcolor_ == "background" && support::prefixIs(lmcol, "#"))
112 dmcolor_ = X11hexname(inverseRGBColor(rgbFromHexName(lmcol)));
113 else if (!dmcol.empty())
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);
129 lcolor.setColor("branch" + convert<string>(branch_list_id_)
130 + to_utf8(branch_), lmcolor, dmcolor);
136 std::function<bool (Branch const &)> BranchNameIs(docstring const & d)
138 return [d](Branch const & b){ return b.branch() == d; };
144 Branch * BranchList::find(docstring const & name)
147 find_if(list_.begin(), list_.end(), BranchNameIs(name));
148 return it == list_.end() ? nullptr : &*it;
152 Branch const * BranchList::find(docstring const & name) const
154 List::const_iterator it =
155 find_if(list_.begin(), list_.end(), BranchNameIs(name));
156 return it == list_.end() ? nullptr : &*it;
160 bool BranchList::add(docstring const & s)
165 size_t const j = s.find_first_of(separator_, i);
167 if (j == docstring::npos)
170 name = s.substr(i, j - i);
171 // Is this name already in the list?
172 bool const already = find(name);
177 br.setSelected(false);
178 br.setFileNameSuffix(false);
182 if (j == docstring::npos)
190 bool BranchList::remove(docstring const & s)
192 size_t const size = list_.size();
193 list_.remove_if(BranchNameIs(s));
194 return size != list_.size();
198 bool BranchList::rename(docstring const & oldname,
199 docstring const & newname, bool const merge)
204 // new name already taken
206 return remove(oldname);
210 Branch * branch = find(oldname);
213 branch->setBranch(newname);
218 docstring BranchList::getFileNameSuffix() const
221 for (auto const & br : list_) {
222 if (br.isSelected() && br.hasFileNameSuffix())
223 result += "-" + br.branch();
225 return support::subst(result, from_ascii("/"), from_ascii("_"));