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)
53 InsetBranch::InsetBranch(InsetBranch const & in)
54 : InsetCollapsable(in), params_(in.params_)
60 InsetBranch::~InsetBranch()
62 InsetBranchMailer(*this).hideDialog();
66 Inset * InsetBranch::clone() const
68 return new InsetBranch(*this);
72 docstring const InsetBranch::editMessage() const
74 return _("Opened Branch Inset");
78 void InsetBranch::write(Buffer const & buf, ostream & os) const
81 InsetCollapsable::write(buf, os);
85 void InsetBranch::read(Buffer const & buf, Lexer & lex)
88 InsetCollapsable::read(buf, lex);
89 setLayout(buf.params());
94 void InsetBranch::setButtonLabel()
96 docstring s = _("Branch: ") + params_.branch;
97 if (!params_.branch.empty()) {
99 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
100 if (c == Color_none) {
101 s = _("Undef: ") + s;
104 if (decoration() == Classic)
105 setLabel(isOpen() ? s : getNewLabel(s) );
107 setLabel(params_.branch + ": " + getNewLabel(s));
111 ColorCode InsetBranch::backgroundColor() const
113 if (!params_.branch.empty()) {
115 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
116 if (c == Color_none) {
121 return Inset::backgroundColor();
125 bool InsetBranch::showInsetDialog(BufferView * bv) const
127 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
132 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
134 switch (cmd.action) {
135 case LFUN_INSET_MODIFY: {
136 InsetBranchParams params;
137 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
138 params_.branch = params.branch;
139 setLayout(cur.buffer().params());
144 case LFUN_MOUSE_PRESS:
145 if (cmd.button() != mouse_button::button3)
146 InsetCollapsable::doDispatch(cur, cmd);
151 case LFUN_INSET_DIALOG_UPDATE:
152 InsetBranchMailer(*this).updateDialog(&cur.bv());
155 case LFUN_MOUSE_RELEASE:
156 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
157 InsetBranchMailer(*this).showDialog(&cur.bv());
159 InsetCollapsable::doDispatch(cur, cmd);
163 case LFUN_INSET_TOGGLE:
164 if (cmd.argument() == "assign") {
165 // The branch inset uses "assign".
166 if (isBranchSelected(cur.buffer())) {
167 if (status() != Open)
168 setStatus(cur, Open);
172 if (status() != Collapsed)
173 setStatus(cur, Collapsed);
179 InsetCollapsable::doDispatch(cur, cmd);
183 InsetCollapsable::doDispatch(cur, cmd);
189 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
190 FuncStatus & flag) const
192 switch (cmd.action) {
193 case LFUN_INSET_MODIFY:
194 case LFUN_INSET_DIALOG_UPDATE:
198 case LFUN_INSET_TOGGLE:
199 if (cmd.argument() == "open" || cmd.argument() == "close" ||
200 cmd.argument() == "toggle")
202 else if (cmd.argument() == "assign"
203 || cmd.argument().empty()) {
204 if (isBranchSelected(cur.buffer()))
205 flag.enabled(status() != Open);
207 flag.enabled(status() != Collapsed);
213 return InsetCollapsable::getStatus(cur, cmd, flag);
219 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
221 Buffer const & realbuffer = *buffer.masterBuffer();
222 BranchList const & branchlist = realbuffer.params().branchlist();
223 BranchList::const_iterator const end = branchlist.end();
224 BranchList::const_iterator it =
225 std::find_if(branchlist.begin(), end,
226 BranchNamesEqual(params_.branch));
229 return it->getSelected();
233 void InsetBranch::updateLabels(Buffer const & buf, ParIterator const & it)
235 if (isBranchSelected(buf))
236 InsetCollapsable::updateLabels(buf, it);
238 TextClass const & tclass = buf.params().getTextClass();
239 Counters savecnt = tclass.counters();
240 InsetCollapsable::updateLabels(buf, it);
241 tclass.counters() = savecnt;
246 int InsetBranch::latex(Buffer const & buf, odocstream & os,
247 OutputParams const & runparams) const
249 return isBranchSelected(buf) ?
250 InsetText::latex(buf, os, runparams) : 0;
254 int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
255 OutputParams const & runparams) const
257 if (!isBranchSelected(buf))
260 os << '[' << buf.B_("branch") << ' ' << params_.branch << ":\n";
261 InsetText::plaintext(buf, os, runparams);
264 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
268 int InsetBranch::docbook(Buffer const & buf, odocstream & os,
269 OutputParams const & runparams) const
271 return isBranchSelected(buf) ?
272 InsetText::docbook(buf, os, runparams) : 0;
276 void InsetBranch::textString(Buffer const & buf, odocstream & os) const
278 if (isBranchSelected(buf))
279 os << paragraphs().begin()->asString(buf, true);
283 void InsetBranch::validate(LaTeXFeatures & features) const
285 InsetText::validate(features);
290 string const InsetBranchMailer::name_("branch");
292 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
297 string const InsetBranchMailer::inset2string(Buffer const &) const
299 return params2string(inset_.params());
303 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
306 data << name_ << ' ';
312 void InsetBranchMailer::string2params(string const & in,
313 InsetBranchParams & params)
315 params = InsetBranchParams();
319 istringstream data(in);
326 return print_mailer_error("InsetBranchMailer", in, 1, name_);
328 // This is part of the inset proper that is usually swallowed
329 // by Text::readInset
332 if (!lex || id != "Branch")
333 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
339 void InsetBranchParams::write(ostream & os) const
341 os << "Branch " << to_utf8(branch) << '\n';
345 void InsetBranchParams::read(Lexer & lex)