2 * \file InsetBranch.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Martin Vermeer
8 * Full author contact details are available in file CREDITS.
13 #include "InsetBranch.h"
16 #include "BufferParams.h"
17 #include "BranchList.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
25 #include "OutputParams.h"
33 using std::istringstream;
35 using std::ostringstream;
38 void InsetBranch::init()
44 InsetBranch::InsetBranch(BufferParams const & bp,
45 InsetBranchParams const & params)
46 : InsetCollapsable(bp), params_(params)
52 InsetBranch::InsetBranch(InsetBranch const & in)
53 : InsetCollapsable(in), params_(in.params_)
59 InsetBranch::~InsetBranch()
61 InsetBranchMailer(*this).hideDialog();
65 Inset * InsetBranch::clone() const
67 return new InsetBranch(*this);
71 docstring const InsetBranch::editMessage() const
73 return _("Opened Branch Inset");
77 void InsetBranch::write(Buffer const & buf, ostream & os) const
80 InsetCollapsable::write(buf, os);
84 void InsetBranch::read(Buffer const & buf, Lexer & lex)
87 InsetCollapsable::read(buf, lex);
91 void InsetBranch::setButtonLabel()
93 docstring s = _("Branch: ") + params_.branch;
94 if (!params_.branch.empty()) {
96 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
97 if (c == Color_none) {
101 if (decoration() == Classic)
102 setLabel(isOpen() ? s : getNewLabel(s) );
104 setLabel(params_.branch + ": " + getNewLabel(s));
108 ColorCode InsetBranch::backgroundColor() const
110 if (!params_.branch.empty()) {
112 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
113 if (c == Color_none) {
118 return Inset::backgroundColor();
122 bool InsetBranch::showInsetDialog(BufferView * bv) const
124 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
129 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
131 switch (cmd.action) {
132 case LFUN_INSET_MODIFY: {
133 InsetBranchParams params;
134 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
135 params_.branch = params.branch;
136 setLayout(cur.buffer().params());
140 case LFUN_MOUSE_PRESS:
141 if (cmd.button() != mouse_button::button3)
142 InsetCollapsable::doDispatch(cur, cmd);
147 case LFUN_INSET_DIALOG_UPDATE:
148 InsetBranchMailer(*this).updateDialog(&cur.bv());
151 case LFUN_MOUSE_RELEASE:
152 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
153 InsetBranchMailer(*this).showDialog(&cur.bv());
155 InsetCollapsable::doDispatch(cur, cmd);
159 case LFUN_INSET_TOGGLE:
160 if (cmd.argument() == "assign") {
161 // The branch inset uses "assign".
162 if (isBranchSelected(cur.buffer())) {
163 if (status() != Open)
164 setStatus(cur, Open);
168 if (status() != Collapsed)
169 setStatus(cur, Collapsed);
175 InsetCollapsable::doDispatch(cur, cmd);
179 InsetCollapsable::doDispatch(cur, cmd);
185 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
186 FuncStatus & flag) const
188 switch (cmd.action) {
189 case LFUN_INSET_MODIFY:
190 case LFUN_INSET_DIALOG_UPDATE:
194 case LFUN_INSET_TOGGLE:
195 if (cmd.argument() == "open" || cmd.argument() == "close" ||
196 cmd.argument() == "toggle")
198 else if (cmd.argument() == "assign"
199 || cmd.argument().empty()) {
200 if (isBranchSelected(cur.buffer()))
201 flag.enabled(status() != Open);
203 flag.enabled(status() != Collapsed);
209 return InsetCollapsable::getStatus(cur, cmd, flag);
215 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
217 Buffer const & realbuffer = *buffer.masterBuffer();
218 BranchList const & branchlist = realbuffer.params().branchlist();
219 BranchList::const_iterator const end = branchlist.end();
220 BranchList::const_iterator it =
221 std::find_if(branchlist.begin(), end,
222 BranchNamesEqual(params_.branch));
225 return it->getSelected();
229 void InsetBranch::updateLabels(Buffer const & buf, ParIterator const & it)
231 if (isBranchSelected(buf))
232 InsetCollapsable::updateLabels(buf, it);
234 TextClass const & tclass = buf.params().getTextClass();
235 Counters savecnt = tclass.counters();
236 InsetCollapsable::updateLabels(buf, it);
237 tclass.counters() = savecnt;
242 int InsetBranch::latex(Buffer const & buf, odocstream & os,
243 OutputParams const & runparams) const
245 return isBranchSelected(buf) ?
246 InsetText::latex(buf, os, runparams) : 0;
250 int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
251 OutputParams const & runparams) const
253 if (!isBranchSelected(buf))
256 os << '[' << buf.B_("branch") << ' ' << params_.branch << ":\n";
257 InsetText::plaintext(buf, os, runparams);
260 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
264 int InsetBranch::docbook(Buffer const & buf, odocstream & os,
265 OutputParams const & runparams) const
267 return isBranchSelected(buf) ?
268 InsetText::docbook(buf, os, runparams) : 0;
272 void InsetBranch::textString(Buffer const & buf, odocstream & os) const
274 if (isBranchSelected(buf))
275 os << paragraphs().begin()->asString(buf, true);
279 void InsetBranch::validate(LaTeXFeatures & features) const
281 InsetText::validate(features);
286 string const InsetBranchMailer::name_("branch");
288 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
293 string const InsetBranchMailer::inset2string(Buffer const &) const
295 return params2string(inset_.params());
299 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
302 data << name_ << ' ';
308 void InsetBranchMailer::string2params(string const & in,
309 InsetBranchParams & params)
311 params = InsetBranchParams();
315 istringstream data(in);
322 return print_mailer_error("InsetBranchMailer", in, 1, name_);
324 // This is part of the inset proper that is usually swallowed
325 // by Text::readInset
328 if (!lex || id != "Branch")
329 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
335 void InsetBranchParams::write(ostream & os) const
337 os << "Branch " << to_utf8(branch) << '\n';
341 void InsetBranchParams::read(Lexer & lex)