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"
17 #include "frontends/Application.h"
19 #include "support/lstrings.h"
30 class BranchNamesEqual
: public std::unary_function
<Branch
, bool>
33 BranchNamesEqual(docstring
const & name
) : name_(name
) {}
35 bool operator()(Branch
const & branch
) const
37 return branch
.branch() == name_
;
46 : selected_(false), filenameSuffix_(false)
48 // no theApp() with command line export
50 theApp()->getRgbColor(Color_background
, color_
);
54 docstring
const & Branch::branch() const
60 void Branch::setBranch(docstring
const & s
)
66 bool Branch::isSelected() const
72 bool Branch::setSelected(bool b
)
81 bool Branch::hasFilenameSuffix() const
83 return filenameSuffix_
;
87 void Branch::setFilenameSuffix(bool b
)
93 RGBColor
const & Branch::color() const
99 void Branch::setColor(RGBColor
const & c
)
105 void Branch::setColor(string
const & str
)
107 if (str
.size() == 7 && str
[0] == '#')
108 color_
= rgbFromHexName(str
);
110 // no color set or invalid color - use normal background
111 theApp()->getRgbColor(Color_background
, color_
);
115 Branch
* BranchList::find(docstring
const & name
)
118 find_if(list
.begin(), list
.end(), BranchNamesEqual(name
));
119 return it
== list
.end() ? 0 : &*it
;
123 Branch
const * BranchList::find(docstring
const & name
) const
125 List::const_iterator it
=
126 find_if(list
.begin(), list
.end(), BranchNamesEqual(name
));
127 return it
== list
.end() ? 0 : &*it
;
131 bool BranchList::add(docstring
const & s
)
136 size_t const j
= s
.find_first_of(separator_
, i
);
138 if (j
== docstring::npos
)
141 name
= s
.substr(i
, j
- i
);
142 // Is this name already in the list?
144 find_if(list
.begin(), list
.end(),
145 BranchNamesEqual(name
)) != list
.end();
150 br
.setSelected(false);
151 br
.setFilenameSuffix(false);
154 if (j
== docstring::npos
)
162 bool BranchList::remove(docstring
const & s
)
164 size_t const size
= list
.size();
165 list
.remove_if(BranchNamesEqual(s
));
166 return size
!= list
.size();
170 bool BranchList::rename(docstring
const & oldname
,
171 docstring
const & newname
, bool const merge
)
175 if (find_if(list
.begin(), list
.end(),
176 BranchNamesEqual(newname
)) != list
.end()) {
177 // new name already taken
179 return remove(oldname
);
183 Branch
* branch
= find(oldname
);
186 branch
->setBranch(newname
);
191 docstring
BranchList::getFilenameSuffix() const
194 List::const_iterator it
= list
.begin();
195 for (; it
!= list
.end(); ++it
) {
196 if (it
->isSelected() && it
->hasFilenameSuffix())
197 result
+= "-" + it
->branch();
199 return support::subst(result
, from_ascii("/"), from_ascii("_"));