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"
26 #include "OutputParams.h"
34 using std::istringstream;
36 using std::ostringstream;
39 void InsetBranch::init()
45 InsetBranch::InsetBranch(BufferParams const & bp,
46 InsetBranchParams const & params)
47 : InsetCollapsable(bp), params_(params)
54 InsetBranch::InsetBranch(InsetBranch const & in)
55 : InsetCollapsable(in), params_(in.params_)
61 InsetBranch::~InsetBranch()
63 InsetBranchMailer(*this).hideDialog();
67 Inset * InsetBranch::clone() const
69 return new InsetBranch(*this);
73 docstring const InsetBranch::editMessage() const
75 return _("Opened Branch Inset");
79 void InsetBranch::write(Buffer const & buf, ostream & os) const
82 InsetCollapsable::write(buf, os);
86 void InsetBranch::read(Buffer const & buf, Lexer & lex)
89 InsetCollapsable::read(buf, lex);
90 setLayout(buf.params());
95 void InsetBranch::setButtonLabel()
97 docstring s = _("Branch: ") + params_.branch;
98 if (!params_.branch.empty()) {
100 Color_color c = lcolor.getFromLyXName(to_utf8(params_.branch));
101 if (c == Color::none) {
102 s = _("Undef: ") + s;
105 if (decoration() == Classic)
106 setLabel(isOpen() ? s : getNewLabel(s) );
108 setLabel(params_.branch + ": " + getNewLabel(s));
112 Color_color InsetBranch::backgroundColor() const
114 if (!params_.branch.empty()) {
116 Color_color c = lcolor.getFromLyXName(to_utf8(params_.branch));
117 if (c == Color::none) {
122 return Inset::backgroundColor();
126 bool InsetBranch::showInsetDialog(BufferView * bv) const
128 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
133 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
135 switch (cmd.action) {
136 case LFUN_INSET_MODIFY: {
137 InsetBranchParams params;
138 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
139 params_.branch = params.branch;
140 setLayout(cur.buffer().params());
145 case LFUN_MOUSE_PRESS:
146 if (cmd.button() != mouse_button::button3)
147 InsetCollapsable::doDispatch(cur, cmd);
152 case LFUN_INSET_DIALOG_UPDATE:
153 InsetBranchMailer(*this).updateDialog(&cur.bv());
156 case LFUN_MOUSE_RELEASE:
157 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
158 InsetBranchMailer(*this).showDialog(&cur.bv());
160 InsetCollapsable::doDispatch(cur, cmd);
164 case LFUN_INSET_TOGGLE:
165 if (cmd.argument() == "assign") {
166 // The branch inset uses "assign".
167 if (isBranchSelected(cur.buffer())) {
168 if (status() != Open)
169 setStatus(cur, Open);
173 if (status() != Collapsed)
174 setStatus(cur, Collapsed);
180 InsetCollapsable::doDispatch(cur, cmd);
184 InsetCollapsable::doDispatch(cur, cmd);
190 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
191 FuncStatus & flag) const
193 switch (cmd.action) {
194 case LFUN_INSET_MODIFY:
195 case LFUN_INSET_DIALOG_UPDATE:
199 case LFUN_INSET_TOGGLE:
200 if (cmd.argument() == "open" || cmd.argument() == "close" ||
201 cmd.argument() == "toggle")
203 else if (cmd.argument() == "assign"
204 || cmd.argument().empty()) {
205 if (isBranchSelected(cur.buffer()))
206 flag.enabled(status() != Open);
208 flag.enabled(status() != Collapsed);
214 return InsetCollapsable::getStatus(cur, cmd, flag);
220 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
222 Buffer const & realbuffer = *buffer.getMasterBuffer();
223 BranchList const & branchlist = realbuffer.params().branchlist();
224 BranchList::const_iterator const end = branchlist.end();
225 BranchList::const_iterator it =
226 std::find_if(branchlist.begin(), end,
227 BranchNamesEqual(params_.branch));
230 return it->getSelected();
234 void InsetBranch::updateLabels(Buffer const & buf, ParIterator const & it)
236 if (isBranchSelected(buf))
237 InsetCollapsable::updateLabels(buf, it);
239 TextClass const & tclass = buf.params().getTextClass();
240 Counters savecnt = tclass.counters();
241 InsetCollapsable::updateLabels(buf, it);
242 tclass.counters() = savecnt;
247 int InsetBranch::latex(Buffer const & buf, odocstream & os,
248 OutputParams const & runparams) const
250 return isBranchSelected(buf) ?
251 InsetText::latex(buf, os, runparams) : 0;
255 int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
256 OutputParams const & runparams) const
258 if (!isBranchSelected(buf))
261 os << '[' << buf.B_("branch") << ' ' << params_.branch << ":\n";
262 InsetText::plaintext(buf, os, runparams);
265 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
269 int InsetBranch::docbook(Buffer const & buf, odocstream & os,
270 OutputParams const & runparams) const
272 return isBranchSelected(buf) ?
273 InsetText::docbook(buf, os, runparams) : 0;
277 void InsetBranch::textString(Buffer const & buf, odocstream & os) const
279 if (isBranchSelected(buf))
280 os << paragraphs().begin()->asString(buf, true);
284 void InsetBranch::validate(LaTeXFeatures & features) const
286 InsetText::validate(features);
291 string const InsetBranchMailer::name_("branch");
293 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
298 string const InsetBranchMailer::inset2string(Buffer const &) const
300 return params2string(inset_.params());
304 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
307 data << name_ << ' ';
313 void InsetBranchMailer::string2params(string const & in,
314 InsetBranchParams & params)
316 params = InsetBranchParams();
320 istringstream data(in);
327 return print_mailer_error("InsetBranchMailer", in, 1, name_);
329 // This is part of the inset proper that is usually swallowed
330 // by Text::readInset
333 if (!lex || id != "Branch")
334 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
340 void InsetBranchParams::write(ostream & os) const
342 os << "Branch " << to_utf8(branch) << '\n';
346 void InsetBranchParams::read(Lexer & lex)