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"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
26 #include "OutputParams.h"
34 using std::istringstream;
36 using std::ostringstream;
39 InsetBranch::InsetBranch(BufferParams const & bp,
40 InsetBranchParams const & params)
41 : InsetCollapsable(bp), params_(params)
45 InsetBranch::InsetBranch(InsetBranch const & in)
46 : InsetCollapsable(in), params_(in.params_)
50 InsetBranch::~InsetBranch()
52 InsetBranchMailer(*this).hideDialog();
56 Inset * InsetBranch::clone() const
58 return new InsetBranch(*this);
62 docstring const InsetBranch::editMessage() const
64 return _("Opened Branch Inset");
68 void InsetBranch::write(Buffer const & buf, ostream & os) const
71 InsetCollapsable::write(buf, os);
75 void InsetBranch::read(Buffer const & buf, Lexer & lex)
78 InsetCollapsable::read(buf, lex);
82 void InsetBranch::setButtonLabel()
84 docstring s = _("Branch: ") + params_.branch;
85 if (!params_.branch.empty()) {
87 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
88 if (c == Color_none) {
92 if (decoration() == Classic)
93 setLabel(isOpen() ? s : getNewLabel(s) );
95 setLabel(params_.branch + ": " + getNewLabel(s));
99 ColorCode InsetBranch::backgroundColor() const
101 if (!params_.branch.empty()) {
103 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
104 if (c == Color_none) {
109 return Inset::backgroundColor();
113 bool InsetBranch::showInsetDialog(BufferView * bv) const
115 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
120 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
122 switch (cmd.action) {
123 case LFUN_INSET_MODIFY: {
124 InsetBranchParams params;
125 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
126 params_.branch = params.branch;
127 setLayout(cur.buffer().params());
131 case LFUN_MOUSE_PRESS:
132 if (cmd.button() != mouse_button::button3)
133 InsetCollapsable::doDispatch(cur, cmd);
138 case LFUN_INSET_DIALOG_UPDATE:
139 InsetBranchMailer(*this).updateDialog(&cur.bv());
142 case LFUN_MOUSE_RELEASE:
143 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
144 InsetBranchMailer(*this).showDialog(&cur.bv());
146 InsetCollapsable::doDispatch(cur, cmd);
150 case LFUN_INSET_TOGGLE:
151 if (cmd.argument() == "assign") {
152 // The branch inset uses "assign".
153 if (isBranchSelected(cur.buffer())) {
154 if (status() != Open)
155 setStatus(cur, Open);
159 if (status() != Collapsed)
160 setStatus(cur, Collapsed);
166 InsetCollapsable::doDispatch(cur, cmd);
170 InsetCollapsable::doDispatch(cur, cmd);
176 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
177 FuncStatus & flag) const
179 switch (cmd.action) {
180 case LFUN_INSET_MODIFY:
181 case LFUN_INSET_DIALOG_UPDATE:
185 case LFUN_INSET_TOGGLE:
186 if (cmd.argument() == "open" || cmd.argument() == "close" ||
187 cmd.argument() == "toggle")
189 else if (cmd.argument() == "assign"
190 || cmd.argument().empty()) {
191 if (isBranchSelected(cur.buffer()))
192 flag.enabled(status() != Open);
194 flag.enabled(status() != Collapsed);
200 return InsetCollapsable::getStatus(cur, cmd, flag);
206 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
208 Buffer const & realbuffer = *buffer.masterBuffer();
209 BranchList const & branchlist = realbuffer.params().branchlist();
210 BranchList::const_iterator const end = branchlist.end();
211 BranchList::const_iterator it =
212 std::find_if(branchlist.begin(), end,
213 BranchNamesEqual(params_.branch));
216 return it->getSelected();
220 void InsetBranch::updateLabels(Buffer const & buf, ParIterator const & it)
222 if (isBranchSelected(buf))
223 InsetCollapsable::updateLabels(buf, it);
225 TextClass const & tclass = buf.params().getTextClass();
226 Counters savecnt = tclass.counters();
227 InsetCollapsable::updateLabels(buf, it);
228 tclass.counters() = savecnt;
233 int InsetBranch::latex(Buffer const & buf, odocstream & os,
234 OutputParams const & runparams) const
236 return isBranchSelected(buf) ?
237 InsetText::latex(buf, os, runparams) : 0;
241 int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
242 OutputParams const & runparams) const
244 if (!isBranchSelected(buf))
247 os << '[' << buf.B_("branch") << ' ' << params_.branch << ":\n";
248 InsetText::plaintext(buf, os, runparams);
251 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
255 int InsetBranch::docbook(Buffer const & buf, odocstream & os,
256 OutputParams const & runparams) const
258 return isBranchSelected(buf) ?
259 InsetText::docbook(buf, os, runparams) : 0;
263 void InsetBranch::textString(Buffer const & buf, odocstream & os) const
265 if (isBranchSelected(buf))
266 os << paragraphs().begin()->asString(buf, true);
270 void InsetBranch::validate(LaTeXFeatures & features) const
272 InsetText::validate(features);
277 string const InsetBranchMailer::name_("branch");
279 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
284 string const InsetBranchMailer::inset2string(Buffer const &) const
286 return params2string(inset_.params());
290 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
293 data << name_ << ' ';
299 void InsetBranchMailer::string2params(string const & in,
300 InsetBranchParams & params)
302 params = InsetBranchParams();
306 istringstream data(in);
313 return print_mailer_error("InsetBranchMailer", in, 1, name_);
315 // This is part of the inset proper that is usually swallowed
316 // by Text::readInset
319 if (!lex || id != "Branch")
320 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
326 void InsetBranchParams::write(ostream & os) const
328 os << "Branch " << to_utf8(branch) << '\n';
332 void InsetBranchParams::read(Lexer & lex)