2003-08-17 11:28:23 +00:00
|
|
|
/**
|
2007-04-25 01:24:38 +00:00
|
|
|
* \file InsetBranch.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.
|
|
|
|
*
|
|
|
|
* \author Martin Vermeer
|
|
|
|
*
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-04-25 01:24:38 +00:00
|
|
|
#include "InsetBranch.h"
|
2003-09-05 09:01:27 +00:00
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferParams.h"
|
2003-12-14 16:33:56 +00:00
|
|
|
#include "BranchList.h"
|
2007-08-12 21:43:58 +00:00
|
|
|
#include "Counters.h"
|
2007-04-26 14:56:30 +00:00
|
|
|
#include "Cursor.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "DispatchResult.h"
|
|
|
|
#include "FuncRequest.h"
|
2005-04-22 08:57:22 +00:00
|
|
|
#include "FuncStatus.h"
|
2003-09-05 09:01:27 +00:00
|
|
|
#include "gettext.h"
|
2007-04-26 17:34:20 +00:00
|
|
|
#include "Color.h"
|
2007-04-26 11:30:54 +00:00
|
|
|
#include "Lexer.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "OutputParams.h"
|
2003-08-17 11:28:23 +00:00
|
|
|
|
2004-07-24 10:55:30 +00:00
|
|
|
#include <sstream>
|
2003-09-05 09:01:27 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
2006-09-09 18:52:00 +00:00
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2003-08-17 11:28:23 +00:00
|
|
|
using std::auto_ptr;
|
2003-09-05 18:02:24 +00:00
|
|
|
using std::istringstream;
|
2003-09-05 09:01:27 +00:00
|
|
|
using std::ostream;
|
2003-09-05 18:02:24 +00:00
|
|
|
using std::ostringstream;
|
2003-09-05 09:01:27 +00:00
|
|
|
|
2003-08-17 11:28:23 +00:00
|
|
|
|
|
|
|
void InsetBranch::init()
|
|
|
|
{
|
2003-09-22 07:46:27 +00:00
|
|
|
setButtonLabel();
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-14 16:33:56 +00:00
|
|
|
InsetBranch::InsetBranch(BufferParams const & bp,
|
|
|
|
InsetBranchParams const & params)
|
|
|
|
: InsetCollapsable(bp), params_(params)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InsetBranch::InsetBranch(InsetBranch const & in)
|
|
|
|
: InsetCollapsable(in), params_(in.params_)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InsetBranch::~InsetBranch()
|
|
|
|
{
|
2003-12-10 14:49:51 +00:00
|
|
|
InsetBranchMailer(*this).hideDialog();
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
auto_ptr<Inset> InsetBranch::doClone() const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2007-04-29 13:39:47 +00:00
|
|
|
return auto_ptr<Inset>(new InsetBranch(*this));
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-09 18:52:00 +00:00
|
|
|
docstring const InsetBranch::editMessage() const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2006-09-09 18:52:00 +00:00
|
|
|
return _("Opened Branch Inset");
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-28 07:41:31 +00:00
|
|
|
void InsetBranch::write(Buffer const & buf, ostream & os) const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
|
|
|
params_.write(os);
|
|
|
|
InsetCollapsable::write(buf, os);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void InsetBranch::read(Buffer const & buf, Lexer & lex)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2003-12-14 16:33:56 +00:00
|
|
|
params_.read(lex);
|
2003-08-17 11:28:23 +00:00
|
|
|
InsetCollapsable::read(buf, lex);
|
|
|
|
setButtonLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetBranch::setButtonLabel()
|
|
|
|
{
|
2007-04-29 18:17:15 +00:00
|
|
|
Font font(Font::ALL_SANE);
|
2003-08-17 11:28:23 +00:00
|
|
|
font.decSize();
|
|
|
|
font.decSize();
|
|
|
|
|
2006-11-03 15:16:45 +00:00
|
|
|
docstring s = _("Branch: ") + params_.branch;
|
2005-11-02 13:54:33 +00:00
|
|
|
if (!params_.branch.empty()) {
|
2006-11-03 15:16:45 +00:00
|
|
|
// FIXME UNICODE
|
2007-04-26 17:34:20 +00:00
|
|
|
Color_color c = lcolor.getFromLyXName(to_utf8(params_.branch));
|
|
|
|
if (c == Color::none) {
|
2006-10-08 09:36:16 +00:00
|
|
|
s = _("Undef: ") + s;
|
2005-11-02 13:54:33 +00:00
|
|
|
}
|
2007-06-12 13:45:49 +00:00
|
|
|
}
|
|
|
|
font.setColor(Color::foreground);
|
2005-11-02 13:54:33 +00:00
|
|
|
setLabel(isOpen() ? s : getNewLabel(s) );
|
2003-08-17 11:28:23 +00:00
|
|
|
setLabelFont(font);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-12 13:45:49 +00:00
|
|
|
Color_color InsetBranch::backgroundColor() const
|
|
|
|
{
|
|
|
|
if (!params_.branch.empty()) {
|
|
|
|
// FIXME UNICODE
|
|
|
|
Color_color c = lcolor.getFromLyXName(to_utf8(params_.branch));
|
|
|
|
if (c == Color::none) {
|
|
|
|
c = Color::error;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
} else
|
|
|
|
return Inset::backgroundColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-17 11:28:23 +00:00
|
|
|
bool InsetBranch::showInsetDialog(BufferView * bv) const
|
|
|
|
{
|
2003-12-10 14:49:51 +00:00
|
|
|
InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
|
2003-08-17 11:28:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 14:56:30 +00:00
|
|
|
void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
|
|
|
switch (cmd.action) {
|
2003-11-03 19:52:47 +00:00
|
|
|
case LFUN_INSET_MODIFY: {
|
2003-08-17 11:28:23 +00:00
|
|
|
InsetBranchParams params;
|
2006-10-21 00:16:43 +00:00
|
|
|
InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
|
2003-08-17 11:28:23 +00:00
|
|
|
params_.branch = params.branch;
|
|
|
|
setButtonLabel();
|
2004-02-16 11:58:51 +00:00
|
|
|
break;
|
2003-11-03 19:52:47 +00:00
|
|
|
}
|
2003-11-04 12:36:59 +00:00
|
|
|
|
|
|
|
case LFUN_MOUSE_PRESS:
|
2003-08-28 07:41:31 +00:00
|
|
|
if (cmd.button() != mouse_button::button3)
|
2004-11-24 21:58:42 +00:00
|
|
|
InsetCollapsable::doDispatch(cur, cmd);
|
2004-02-16 11:58:51 +00:00
|
|
|
else
|
2004-03-01 17:12:09 +00:00
|
|
|
cur.undispatched();
|
2004-02-16 11:58:51 +00:00
|
|
|
break;
|
2003-11-03 19:52:47 +00:00
|
|
|
|
2003-08-17 11:28:23 +00:00
|
|
|
case LFUN_INSET_DIALOG_UPDATE:
|
2004-01-20 14:25:24 +00:00
|
|
|
InsetBranchMailer(*this).updateDialog(&cur.bv());
|
2004-02-16 11:58:51 +00:00
|
|
|
break;
|
2003-11-03 19:52:47 +00:00
|
|
|
|
2003-08-17 11:28:23 +00:00
|
|
|
case LFUN_MOUSE_RELEASE:
|
2004-02-16 11:58:51 +00:00
|
|
|
if (cmd.button() == mouse_button::button3 && hitButton(cmd))
|
2004-01-20 14:25:24 +00:00
|
|
|
InsetBranchMailer(*this).showDialog(&cur.bv());
|
2004-02-16 11:58:51 +00:00
|
|
|
else
|
2004-11-24 21:58:42 +00:00
|
|
|
InsetCollapsable::doDispatch(cur, cmd);
|
2004-02-16 11:58:51 +00:00
|
|
|
break;
|
2004-01-05 17:33:57 +00:00
|
|
|
|
2004-03-30 12:36:33 +00:00
|
|
|
|
|
|
|
case LFUN_INSET_TOGGLE:
|
2007-06-11 05:12:10 +00:00
|
|
|
if (cmd.argument() == "assign") {
|
2005-05-06 20:00:31 +00:00
|
|
|
// The branch inset uses "assign".
|
2005-10-25 15:21:49 +00:00
|
|
|
if (isBranchSelected(cur.buffer())) {
|
2004-03-30 12:36:33 +00:00
|
|
|
if (status() != Open)
|
2005-05-06 20:00:31 +00:00
|
|
|
setStatus(cur, Open);
|
2004-03-30 12:36:33 +00:00
|
|
|
else
|
|
|
|
cur.undispatched();
|
|
|
|
} else {
|
2005-05-06 20:00:31 +00:00
|
|
|
if (status() != Collapsed)
|
|
|
|
setStatus(cur, Collapsed);
|
|
|
|
else
|
2004-03-30 12:36:33 +00:00
|
|
|
cur.undispatched();
|
|
|
|
}
|
|
|
|
}
|
2005-10-24 09:42:20 +00:00
|
|
|
else
|
|
|
|
InsetCollapsable::doDispatch(cur, cmd);
|
2004-03-30 12:36:33 +00:00
|
|
|
break;
|
|
|
|
|
2003-08-17 11:28:23 +00:00
|
|
|
default:
|
2004-11-24 21:58:42 +00:00
|
|
|
InsetCollapsable::doDispatch(cur, cmd);
|
2004-02-16 11:58:51 +00:00
|
|
|
break;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 14:56:30 +00:00
|
|
|
bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
|
2005-04-22 08:57:22 +00:00
|
|
|
FuncStatus & flag) const
|
|
|
|
{
|
|
|
|
switch (cmd.action) {
|
|
|
|
case LFUN_INSET_MODIFY:
|
|
|
|
case LFUN_INSET_DIALOG_UPDATE:
|
|
|
|
flag.enabled(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LFUN_INSET_TOGGLE:
|
2006-09-01 15:41:38 +00:00
|
|
|
if (cmd.argument() == "open" || cmd.argument() == "close" ||
|
|
|
|
cmd.argument() == "toggle")
|
2005-04-22 08:57:22 +00:00
|
|
|
flag.enabled(true);
|
2006-09-01 15:41:38 +00:00
|
|
|
else if (cmd.argument() == "assign"
|
|
|
|
|| cmd.argument().empty()) {
|
2005-10-25 15:21:49 +00:00
|
|
|
if (isBranchSelected(cur.buffer()))
|
2005-04-22 15:41:29 +00:00
|
|
|
flag.enabled(status() != Open);
|
|
|
|
else
|
|
|
|
flag.enabled(status() != Collapsed);
|
2005-04-22 08:57:22 +00:00
|
|
|
} else
|
|
|
|
flag.enabled(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return InsetCollapsable::getStatus(cur, cmd, flag);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-25 15:21:49 +00:00
|
|
|
bool InsetBranch::isBranchSelected(Buffer const & buffer) const
|
2003-12-14 16:33:56 +00:00
|
|
|
{
|
2005-10-25 15:21:49 +00:00
|
|
|
Buffer const & realbuffer = *buffer.getMasterBuffer();
|
|
|
|
BranchList const & branchlist = realbuffer.params().branchlist();
|
2003-12-14 16:33:56 +00:00
|
|
|
BranchList::const_iterator const end = branchlist.end();
|
2004-01-05 17:33:57 +00:00
|
|
|
BranchList::const_iterator it =
|
|
|
|
std::find_if(branchlist.begin(), end,
|
|
|
|
BranchNamesEqual(params_.branch));
|
2003-12-14 16:33:56 +00:00
|
|
|
if (it == end)
|
|
|
|
return false;
|
|
|
|
return it->getSelected();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-12 21:43:58 +00:00
|
|
|
void InsetBranch::updateLabels(Buffer const & buf, ParIterator const & it)
|
|
|
|
{
|
|
|
|
if (isBranchSelected(buf))
|
|
|
|
InsetCollapsable::updateLabels(buf, it);
|
|
|
|
else {
|
|
|
|
TextClass const & tclass = buf.params().getTextClass();
|
|
|
|
Counters savecnt = tclass.counters();
|
|
|
|
InsetCollapsable::updateLabels(buf, it);
|
|
|
|
tclass.counters() = savecnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-19 16:51:30 +00:00
|
|
|
int InsetBranch::latex(Buffer const & buf, odocstream & os,
|
2007-05-28 22:27:45 +00:00
|
|
|
OutputParams const & runparams) const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2005-10-25 15:21:49 +00:00
|
|
|
return isBranchSelected(buf) ?
|
2004-03-25 09:16:36 +00:00
|
|
|
InsetText::latex(buf, os, runparams) : 0;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-16 09:28:25 +00:00
|
|
|
int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
|
2007-05-28 22:27:45 +00:00
|
|
|
OutputParams const & runparams) const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2007-02-16 09:28:25 +00:00
|
|
|
if (!isBranchSelected(buf))
|
|
|
|
return 0;
|
|
|
|
|
2007-05-01 08:26:40 +00:00
|
|
|
os << '[' << buf.B_("branch") << ' ' << params_.branch << ":\n";
|
2007-02-16 09:28:25 +00:00
|
|
|
InsetText::plaintext(buf, os, runparams);
|
|
|
|
os << "\n]";
|
|
|
|
|
2007-02-20 17:52:41 +00:00
|
|
|
return PLAINTEXT_NEWLINE + 1; // one char on a separate line
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-16 09:28:25 +00:00
|
|
|
int InsetBranch::docbook(Buffer const & buf, odocstream & os,
|
2007-05-28 22:27:45 +00:00
|
|
|
OutputParams const & runparams) const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2005-10-25 15:21:49 +00:00
|
|
|
return isBranchSelected(buf) ?
|
2007-02-16 09:28:25 +00:00
|
|
|
InsetText::docbook(buf, os, runparams) : 0;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-19 16:23:47 +00:00
|
|
|
void InsetBranch::textString(Buffer const & buf, odocstream & os) const
|
|
|
|
{
|
|
|
|
if (isBranchSelected(buf))
|
|
|
|
os << paragraphs().begin()->asString(buf, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-17 11:28:23 +00:00
|
|
|
void InsetBranch::validate(LaTeXFeatures & features) const
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
InsetText::validate(features);
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
string const InsetBranchMailer::name_("branch");
|
2003-12-10 14:49:51 +00:00
|
|
|
|
|
|
|
InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
|
|
|
|
: inset_(inset)
|
|
|
|
{}
|
2003-08-17 11:28:23 +00:00
|
|
|
|
|
|
|
|
2003-12-14 16:33:56 +00:00
|
|
|
string const InsetBranchMailer::inset2string(Buffer const &) const
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2003-12-14 16:33:56 +00:00
|
|
|
return params2string(inset_.params());
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-10 14:49:51 +00:00
|
|
|
string const InsetBranchMailer::params2string(InsetBranchParams const & params)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
|
|
|
ostringstream data;
|
2003-12-10 14:49:51 +00:00
|
|
|
data << name_ << ' ';
|
2003-08-17 11:28:23 +00:00
|
|
|
params.write(data);
|
2003-09-15 11:00:00 +00:00
|
|
|
return data.str();
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetBranchMailer::string2params(string const & in,
|
2003-12-10 14:49:51 +00:00
|
|
|
InsetBranchParams & params)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
|
|
|
params = InsetBranchParams();
|
|
|
|
if (in.empty())
|
|
|
|
return;
|
|
|
|
|
2003-09-15 11:00:00 +00:00
|
|
|
istringstream data(in);
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer lex(0,0);
|
2003-08-17 11:28:23 +00:00
|
|
|
lex.setStream(data);
|
2003-12-10 14:49:51 +00:00
|
|
|
|
|
|
|
string name;
|
|
|
|
lex >> name;
|
|
|
|
if (name != name_)
|
2003-12-11 15:23:15 +00:00
|
|
|
return print_mailer_error("InsetBranchMailer", in, 1, name_);
|
2003-12-10 14:49:51 +00:00
|
|
|
|
2003-12-14 16:33:56 +00:00
|
|
|
// This is part of the inset proper that is usually swallowed
|
2007-04-29 23:33:02 +00:00
|
|
|
// by Text::readInset
|
2003-12-14 16:33:56 +00:00
|
|
|
string id;
|
|
|
|
lex >> id;
|
|
|
|
if (!lex || id != "Branch")
|
|
|
|
return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
|
|
|
|
|
2003-08-17 11:28:23 +00:00
|
|
|
params.read(lex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetBranchParams::write(ostream & os) const
|
|
|
|
{
|
2006-11-03 15:16:45 +00:00
|
|
|
os << "Branch " << to_utf8(branch) << '\n';
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void InsetBranchParams::read(Lexer & lex)
|
2003-08-17 11:28:23 +00:00
|
|
|
{
|
2003-12-14 16:33:56 +00:00
|
|
|
lex >> branch;
|
2003-08-17 11:28:23 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
} // namespace lyx
|