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)
47 InsetBranch::InsetBranch(InsetBranch const & in)
48 : InsetCollapsable(in), params_(in.params_)
54 InsetBranch::~InsetBranch()
56 InsetBranchMailer(*this).hideDialog();
60 Inset * InsetBranch::clone() const
62 return new InsetBranch(*this);
66 docstring const InsetBranch::editMessage() const
68 return _("Opened Branch Inset");
72 void InsetBranch::write(Buffer const & buf, ostream & os) const
75 InsetCollapsable::write(buf, os);
79 void InsetBranch::read(Buffer const & buf, Lexer & lex)
82 InsetCollapsable::read(buf, lex);
86 void InsetBranch::setButtonLabel()
88 docstring s = _("Branch: ") + params_.branch;
89 if (!params_.branch.empty()) {
91 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
92 if (c == Color_none) {
96 if (decoration() == Classic)
97 setLabel(isOpen() ? s : getNewLabel(s) );
99 setLabel(params_.branch + ": " + getNewLabel(s));
103 ColorCode InsetBranch::backgroundColor() const
105 if (!params_.branch.empty()) {
107 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
108 if (c == Color_none) {
113 return Inset::backgroundColor();
117 bool InsetBranch::showInsetDialog(BufferView * bv) const
119 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
124 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
126 switch (cmd.action) {
127 case LFUN_INSET_MODIFY: {
128 InsetBranchParams params;
129 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
130 params_.branch = params.branch;
131 setLayout(cur.buffer().params());
135 case LFUN_MOUSE_PRESS:
136 if (cmd.button() != mouse_button::button3)
137 InsetCollapsable::doDispatch(cur, cmd);
142 case LFUN_INSET_DIALOG_UPDATE:
143 InsetBranchMailer(*this).updateDialog(&cur.bv());
146 case LFUN_MOUSE_RELEASE:
147 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
148 InsetBranchMailer(*this).showDialog(&cur.bv());
150 InsetCollapsable::doDispatch(cur, cmd);
154 case LFUN_INSET_TOGGLE:
155 if (cmd.argument() == "assign") {
156 // The branch inset uses "assign".
157 if (isBranchSelected(cur.buffer())) {
158 if (status() != Open)
159 setStatus(cur, Open);
163 if (status() != Collapsed)
164 setStatus(cur, Collapsed);
170 InsetCollapsable::doDispatch(cur, cmd);
174 InsetCollapsable::doDispatch(cur, cmd);
180 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
181 FuncStatus & flag) const
183 switch (cmd.action) {
184 case LFUN_INSET_MODIFY:
185 case LFUN_INSET_DIALOG_UPDATE:
189 case LFUN_INSET_TOGGLE:
190 if (cmd.argument() == "open" || cmd.argument() == "close" ||
191 cmd.argument() == "toggle")
193 else if (cmd.argument() == "assign"
194 || cmd.argument().empty()) {
195 if (isBranchSelected(cur.buffer()))
196 flag.enabled(status() != Open);
198 flag.enabled(status() != Collapsed);
204 return InsetCollapsable::getStatus(cur, cmd, flag);
210 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
212 Buffer const & realbuffer = *buffer.masterBuffer();
213 BranchList const & branchlist = realbuffer.params().branchlist();
214 BranchList::const_iterator const end = branchlist.end();
215 BranchList::const_iterator it =
216 std::find_if(branchlist.begin(), end,
217 BranchNamesEqual(params_.branch));
220 return it->getSelected();
224 void InsetBranch::updateLabels(Buffer const & buf, ParIterator const & it)
226 if (isBranchSelected(buf))
227 InsetCollapsable::updateLabels(buf, it);
229 TextClass const & tclass = buf.params().getTextClass();
230 Counters savecnt = tclass.counters();
231 InsetCollapsable::updateLabels(buf, it);
232 tclass.counters() = savecnt;
237 int InsetBranch::latex(Buffer const & buf, odocstream & os,
238 OutputParams const & runparams) const
240 return isBranchSelected(buf) ?
241 InsetText::latex(buf, os, runparams) : 0;
245 int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
246 OutputParams const & runparams) const
248 if (!isBranchSelected(buf))
251 os << '[' << buf.B_("branch") << ' ' << params_.branch << ":\n";
252 InsetText::plaintext(buf, os, runparams);
255 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
259 int InsetBranch::docbook(Buffer const & buf, odocstream & os,
260 OutputParams const & runparams) const
262 return isBranchSelected(buf) ?
263 InsetText::docbook(buf, os, runparams) : 0;
267 void InsetBranch::textString(Buffer const & buf, odocstream & os) const
269 if (isBranchSelected(buf))
270 os << paragraphs().begin()->asString(buf, true);
274 void InsetBranch::validate(LaTeXFeatures & features) const
276 InsetText::validate(features);
281 string const InsetBranchMailer::name_("branch");
283 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
288 string const InsetBranchMailer::inset2string(Buffer const &) const
290 return params2string(inset_.params());
294 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
297 data << name_ << ' ';
303 void InsetBranchMailer::string2params(string const & in,
304 InsetBranchParams & params)
306 params = InsetBranchParams();
310 istringstream data(in);
317 return print_mailer_error("InsetBranchMailer", in, 1, name_);
319 // This is part of the inset proper that is usually swallowed
320 // by Text::readInset
323 if (!lex || id != "Branch")
324 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
330 void InsetBranchParams::write(ostream & os) const
332 os << "Branch " << to_utf8(branch) << '\n';
336 void InsetBranchParams::read(Lexer & lex)