#include "buffer.h"
#include "bufferparams.h"
-#include "BufferView.h"
+#include "BranchList.h"
+#include "cursor.h"
#include "dispatchresult.h"
#include "funcrequest.h"
#include "gettext.h"
#include "lyxlex.h"
#include "paragraph.h"
-#include "support/std_sstream.h"
-
+#include <sstream>
using std::string;
using std::auto_ptr;
}
-InsetBranch::InsetBranch(BufferParams const & bp, string const & label)
- : InsetCollapsable(bp)
+InsetBranch::InsetBranch(BufferParams const & bp,
+ InsetBranchParams const & params)
+ : InsetCollapsable(bp), params_(params)
{
- params_.branch = label;
- // Hack: stash the list of all allowable branch labels from this
- // buffer into inset's parm list as a "stowaway":
- params_.branchlist = bp.branchlist();
init();
}
InsetBranch::~InsetBranch()
{
- InsetBranchMailer mailer("branch", *this);
- mailer.hideDialog();
+ InsetBranchMailer(*this).hideDialog();
}
void InsetBranch::read(Buffer const & buf, LyXLex & lex)
{
- if (lex.isOK()) {
- lex.next();
- params_.branch = lex.getString();
- }
+ params_.read(lex);
InsetCollapsable::read(buf, lex);
setButtonLabel();
}
font.decSize();
font.decSize();
- setLabel("Branch: " + params_.branch);
+ string s = "Branch: " + params_.branch;
+ setLabel(isOpen() ? s : getNewLabel(s) );
font.setColor(LColor::foreground);
if (!params_.branch.empty())
setBackgroundColor(lcolor.getFromLyXName(params_.branch));
bool InsetBranch::showInsetDialog(BufferView * bv) const
{
- InsetBranchMailer("branch", const_cast<InsetBranch &>(*this)).showDialog(bv);
+ InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
return true;
}
-DispatchResult
-InsetBranch::priv_dispatch(FuncRequest const & cmd,
- idx_type & idx, pos_type & pos)
+void InsetBranch::priv_dispatch(LCursor & cur, FuncRequest & cmd)
{
- BufferView * bv = cmd.view();
switch (cmd.action) {
case LFUN_INSET_MODIFY: {
InsetBranchParams params;
InsetBranchMailer::string2params(cmd.argument, params);
params_.branch = params.branch;
setButtonLabel();
- bv->updateInset(this);
- return DispatchResult(true, true);
+ break;
}
case LFUN_MOUSE_PRESS:
if (cmd.button() != mouse_button::button3)
- return InsetCollapsable::priv_dispatch(cmd, idx, pos);
- return DispatchResult(false);
+ InsetCollapsable::priv_dispatch(cur, cmd);
+ else
+ cur.undispatched();
+ break;
case LFUN_INSET_DIALOG_UPDATE:
- InsetBranchMailer("branch", *this).updateDialog(bv);
- return DispatchResult(true);
+ InsetBranchMailer(*this).updateDialog(&cur.bv());
+ break;
case LFUN_MOUSE_RELEASE:
- if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
- InsetBranchMailer("branch", *this).showDialog(bv);
- return DispatchResult(true);
+ if (cmd.button() == mouse_button::button3 && hitButton(cmd))
+ InsetBranchMailer(*this).showDialog(&cur.bv());
+ else
+ InsetCollapsable::priv_dispatch(cur, cmd);
+ break;
+
+
+ case LFUN_INSET_TOGGLE:
+ // We assume that this lfun is indeed going to be dispatched.
+ cur.dispatched();
+
+ if (cmd.argument == "open")
+ setStatus(Open);
+ else if (cmd.argument == "close") {
+ setStatus(Collapsed);
+ leaveInset(cur, *this);
+ } else if (cmd.argument == "toggle") {
+ if (isOpen()) {
+ setStatus(Collapsed);
+ leaveInset(cur, *this);
+ } else
+ setStatus(Open);
+
+ // The branch inset uses "assign".
+ } else if (cmd.argument == "assign"
+ || cmd.argument.empty()) {
+ BranchList const & branchlist =
+ cur.buffer().params().branchlist();
+ if (isBranchSelected(branchlist)) {
+ if (status() != Open)
+ setStatus(Open);
+ else
+ cur.undispatched();
+ } else {
+ if (status() != Collapsed) {
+ setStatus(Collapsed);
+ leaveInset(cur, *this);
+ } else
+ cur.undispatched();
+ }
}
- return InsetCollapsable::priv_dispatch(cmd, idx, pos);
-
+ break;
+
default:
- return InsetCollapsable::priv_dispatch(cmd, idx, pos);
+ InsetCollapsable::priv_dispatch(cur, cmd);
+ break;
}
}
+bool InsetBranch::isBranchSelected(BranchList const & branchlist) const
+{
+ BranchList::const_iterator const end = branchlist.end();
+ BranchList::const_iterator it =
+ std::find_if(branchlist.begin(), end,
+ BranchNamesEqual(params_.branch));
+ if (it == end)
+ return false;
+ return it->getSelected();
+}
+
+
int InsetBranch::latex(Buffer const & buf, ostream & os,
- OutputParams const & runparams) const
+ OutputParams const & runparams) const
{
- string const branch_sel = buf.params().branchlist().allSelected();
- if (branch_sel.find(params_.branch, 0) != string::npos)
- return inset.latex(buf, os, runparams);
- return 0;
+ return isBranchSelected(buf.params().branchlist()) ?
+ InsetText::latex(buf, os, runparams) : 0;
}
int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
OutputParams const & runparams) const
{
- string const branch_sel = buf.params().branchlist().allSelected();
- if (branch_sel.find(params_.branch, 0) != string::npos)
- return inset.linuxdoc(buf, os, runparams);
- return 0;
+ return isBranchSelected(buf.params().branchlist()) ?
+ InsetText::linuxdoc(buf, os, runparams) : 0;
}
int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
OutputParams const & runparams) const
{
- string const branch_sel = buf.params().branchlist().allSelected();
- if (branch_sel.find(params_.branch, 0) != string::npos)
- return inset.docbook(buf, os, runparams);
- return 0;
+ return isBranchSelected(buf.params().branchlist()) ?
+ InsetText::docbook(buf, os, runparams) : 0;
}
int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
- OutputParams const & runparams) const
+ OutputParams const & runparams) const
{
- string const branch_sel = buf.params().branchlist().allSelected();
- if (branch_sel.find(params_.branch, 0) != string::npos) {
- return inset.plaintext(buf, os, runparams);
- }
- return 0;
+ return isBranchSelected(buf.params().branchlist()) ?
+ InsetText::plaintext(buf, os, runparams): 0;
}
void InsetBranch::validate(LaTeXFeatures & features) const
{
- inset.validate(features);
+ InsetText::validate(features);
}
-InsetBranchMailer::InsetBranchMailer(string const & name,
- InsetBranch & inset)
- : name_(name), inset_(inset)
-{
-}
+string const InsetBranchMailer::name_("branch");
+
+InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
+ : inset_(inset)
+{}
-string const InsetBranchMailer::inset2string(Buffer const & buf) const
+string const InsetBranchMailer::inset2string(Buffer const &) const
{
- InsetBranchParams params = inset_.params();
- params.branchlist = buf.params().branchlist();
- inset_.setParams(params);
- return params2string(name_, params);
+ return params2string(inset_.params());
}
-string const InsetBranchMailer::params2string(string const & name,
- InsetBranchParams const & params)
+string const InsetBranchMailer::params2string(InsetBranchParams const & params)
{
ostringstream data;
- data << name << ' ';
+ data << name_ << ' ';
params.write(data);
- // Add all_branches parameter to data:
- data << params.branchlist.allBranches() << "\n";
return data.str();
}
void InsetBranchMailer::string2params(string const & in,
- InsetBranchParams & params)
+ InsetBranchParams & params)
{
params = InsetBranchParams();
-
if (in.empty())
return;
istringstream data(in);
LyXLex lex(0,0);
lex.setStream(data);
+
+ string name;
+ lex >> name;
+ if (name != name_)
+ return print_mailer_error("InsetBranchMailer", in, 1, name_);
+
+ // This is part of the inset proper that is usually swallowed
+ // by LyXText::readInset
+ string id;
+ lex >> id;
+ if (!lex || id != "Branch")
+ return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
+
params.read(lex);
- // Process all_branches here:
- if (lex.isOK()) {
- lex.next();
- params.branchlist.add(lex.getString());
- }
}
void InsetBranchParams::write(ostream & os) const
{
- os << "Branch" << " " << branch << "\n";
+ os << "Branch " << branch << '\n';
}
void InsetBranchParams::read(LyXLex & lex)
{
- if (lex.isOK()) {
- lex.next();
- string token = lex.getString();
- }
- if (lex.isOK()) {
- lex.next();
- string token = lex.getString();
- }
- if (lex.isOK()) {
- lex.next();
- branch = lex.getString();
- }
+ lex >> branch;
}