2003-08-17 11:28:23 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file BranchList.cpp
|
2003-08-17 11:28:23 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2003-08-23 00:17:00 +00:00
|
|
|
*
|
2003-08-17 11:28:23 +00:00
|
|
|
* \author Martin Vermeer
|
2009-07-13 14:30:08 +00:00
|
|
|
* \author Jürgen Spitzmüller
|
2003-09-09 17:25:35 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-08-17 11:28:23 +00:00
|
|
|
*/
|
|
|
|
|
2003-08-23 00:17:00 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2003-08-17 11:28:23 +00:00
|
|
|
#include "BranchList.h"
|
2007-11-02 23:42:27 +00:00
|
|
|
#include "Color.h"
|
2006-10-12 14:10:13 +00:00
|
|
|
|
|
|
|
#include "frontends/Application.h"
|
|
|
|
|
2009-07-13 14:30:08 +00:00
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
2005-01-18 14:15:57 +00:00
|
|
|
#include <algorithm>
|
2003-09-09 17:25:35 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2008-09-21 19:27:20 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2008-05-27 18:57:16 +00:00
|
|
|
namespace {
|
2008-09-21 19:27:20 +00:00
|
|
|
|
|
|
|
class BranchNamesEqual : public std::unary_function<Branch, bool>
|
|
|
|
{
|
2008-05-27 18:57:16 +00:00
|
|
|
public:
|
2012-10-27 13:45:27 +00:00
|
|
|
BranchNamesEqual(docstring const & name)
|
|
|
|
: name_(name)
|
|
|
|
{}
|
2008-09-21 19:27:20 +00:00
|
|
|
|
2008-05-27 18:57:16 +00:00
|
|
|
bool operator()(Branch const & branch) const
|
|
|
|
{
|
2008-09-21 19:27:20 +00:00
|
|
|
return branch.branch() == name_;
|
2008-05-27 18:57:16 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
docstring name_;
|
|
|
|
};
|
2012-10-27 13:45:27 +00:00
|
|
|
|
2008-05-27 18:57:16 +00:00
|
|
|
}
|
|
|
|
|
2003-08-17 11:28:23 +00:00
|
|
|
|
2009-07-13 14:30:08 +00:00
|
|
|
Branch::Branch()
|
|
|
|
: selected_(false), filenameSuffix_(false)
|
2006-03-23 20:04:05 +00:00
|
|
|
{
|
2007-10-08 09:21:56 +00:00
|
|
|
// no theApp() with command line export
|
|
|
|
if (theApp())
|
2007-10-25 12:41:02 +00:00
|
|
|
theApp()->getRgbColor(Color_background, color_);
|
2011-01-06 18:40:39 +00:00
|
|
|
else
|
|
|
|
frontend::Application::getRgbColorUncached(Color_background, color_);
|
2006-03-23 20:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-21 19:27:20 +00:00
|
|
|
docstring const & Branch::branch() const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
|
|
|
return branch_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-03 15:16:45 +00:00
|
|
|
void Branch::setBranch(docstring const & s)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
|
|
|
branch_ = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-21 19:27:20 +00:00
|
|
|
bool Branch::isSelected() const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
|
|
|
return selected_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-14 16:33:56 +00:00
|
|
|
bool Branch::setSelected(bool b)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2003-12-14 16:33:56 +00:00
|
|
|
if (b == selected_)
|
|
|
|
return false;
|
2003-08-17 11:28:23 +00:00
|
|
|
selected_ = b;
|
2003-12-14 16:33:56 +00:00
|
|
|
return true;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 01:19:35 +00:00
|
|
|
bool Branch::hasFileNameSuffix() const
|
2009-07-13 14:30:08 +00:00
|
|
|
{
|
|
|
|
return filenameSuffix_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 01:19:35 +00:00
|
|
|
void Branch::setFileNameSuffix(bool b)
|
2009-07-13 14:30:08 +00:00
|
|
|
{
|
|
|
|
filenameSuffix_ = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-21 19:27:20 +00:00
|
|
|
RGBColor const & Branch::color() const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
|
|
|
return color_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
void Branch::setColor(RGBColor const & c)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
|
|
|
color_ = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-02 23:42:27 +00:00
|
|
|
void Branch::setColor(string const & str)
|
2006-03-23 20:04:05 +00:00
|
|
|
{
|
2007-11-02 23:42:27 +00:00
|
|
|
if (str.size() == 7 && str[0] == '#')
|
|
|
|
color_ = rgbFromHexName(str);
|
2011-01-06 18:40:39 +00:00
|
|
|
else {
|
2006-03-23 20:04:05 +00:00
|
|
|
// no color set or invalid color - use normal background
|
2011-01-06 18:40:39 +00:00
|
|
|
// no theApp() with command line export
|
|
|
|
if (theApp())
|
|
|
|
theApp()->getRgbColor(Color_background, color_);
|
|
|
|
else
|
|
|
|
frontend::Application::getRgbColorUncached(Color_background, color_);
|
|
|
|
}
|
2006-03-23 20:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-03 15:16:45 +00:00
|
|
|
Branch * BranchList::find(docstring const & name)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2003-12-14 16:33:56 +00:00
|
|
|
List::iterator it =
|
2007-12-12 19:28:07 +00:00
|
|
|
find_if(list.begin(), list.end(), BranchNamesEqual(name));
|
2003-12-14 16:33:56 +00:00
|
|
|
return it == list.end() ? 0 : &*it;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
2004-01-05 15:49:40 +00:00
|
|
|
|
2006-11-03 15:16:45 +00:00
|
|
|
Branch const * BranchList::find(docstring const & name) const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2003-12-14 16:33:56 +00:00
|
|
|
List::const_iterator it =
|
2007-12-12 19:28:07 +00:00
|
|
|
find_if(list.begin(), list.end(), BranchNamesEqual(name));
|
2003-12-14 16:33:56 +00:00
|
|
|
return it == list.end() ? 0 : &*it;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
2004-01-05 15:49:40 +00:00
|
|
|
|
2006-11-03 15:16:45 +00:00
|
|
|
bool BranchList::add(docstring const & s)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2003-12-14 16:33:56 +00:00
|
|
|
bool added = false;
|
2008-09-21 19:27:20 +00:00
|
|
|
size_t i = 0;
|
2003-08-17 11:28:23 +00:00
|
|
|
while (true) {
|
2008-09-21 19:27:20 +00:00
|
|
|
size_t const j = s.find_first_of(separator_, i);
|
2006-11-03 15:16:45 +00:00
|
|
|
docstring name;
|
|
|
|
if (j == docstring::npos)
|
2003-08-17 11:28:23 +00:00
|
|
|
name = s.substr(i);
|
|
|
|
else
|
|
|
|
name = s.substr(i, j - i);
|
|
|
|
// Is this name already in the list?
|
2004-01-05 15:49:40 +00:00
|
|
|
bool const already =
|
2007-12-12 19:28:07 +00:00
|
|
|
find_if(list.begin(), list.end(),
|
2004-01-05 15:49:40 +00:00
|
|
|
BranchNamesEqual(name)) != list.end();
|
2003-08-17 11:28:23 +00:00
|
|
|
if (!already) {
|
2003-12-14 16:33:56 +00:00
|
|
|
added = true;
|
2003-08-17 11:28:23 +00:00
|
|
|
Branch br;
|
|
|
|
br.setBranch(name);
|
|
|
|
br.setSelected(false);
|
2010-04-21 01:19:35 +00:00
|
|
|
br.setFileNameSuffix(false);
|
2003-08-17 11:28:23 +00:00
|
|
|
list.push_back(br);
|
|
|
|
}
|
2006-11-03 15:16:45 +00:00
|
|
|
if (j == docstring::npos)
|
2003-08-17 11:28:23 +00:00
|
|
|
break;
|
2003-09-09 17:25:35 +00:00
|
|
|
i = j + 1;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
2003-12-14 16:33:56 +00:00
|
|
|
return added;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-03 15:16:45 +00:00
|
|
|
bool BranchList::remove(docstring const & s)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2006-10-21 00:16:43 +00:00
|
|
|
size_t const size = list.size();
|
2004-01-05 15:49:40 +00:00
|
|
|
list.remove_if(BranchNamesEqual(s));
|
2003-12-14 16:33:56 +00:00
|
|
|
return size != list.size();
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2009-07-10 06:49:51 +00:00
|
|
|
bool BranchList::rename(docstring const & oldname,
|
|
|
|
docstring const & newname, bool const merge)
|
|
|
|
{
|
|
|
|
if (newname.empty())
|
|
|
|
return false;
|
|
|
|
if (find_if(list.begin(), list.end(),
|
|
|
|
BranchNamesEqual(newname)) != list.end()) {
|
|
|
|
// new name already taken
|
|
|
|
if (merge)
|
|
|
|
return remove(oldname);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Branch * branch = find(oldname);
|
|
|
|
if (!branch)
|
|
|
|
return false;
|
|
|
|
branch->setBranch(newname);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 01:19:35 +00:00
|
|
|
docstring BranchList::getFileNameSuffix() const
|
2009-07-13 14:30:08 +00:00
|
|
|
{
|
|
|
|
docstring result;
|
|
|
|
List::const_iterator it = list.begin();
|
|
|
|
for (; it != list.end(); ++it) {
|
2010-04-21 01:19:35 +00:00
|
|
|
if (it->isSelected() && it->hasFileNameSuffix())
|
2009-07-13 14:30:08 +00:00
|
|
|
result += "-" + it->branch();
|
|
|
|
}
|
|
|
|
return support::subst(result, from_ascii("/"), from_ascii("_"));
|
|
|
|
}
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|