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"
24 #include "support/gettext.h"
26 #include "OutputParams.h"
27 #include "TextClass.h"
36 InsetBranch::InsetBranch(BufferParams const & bp,
37 InsetBranchParams const & params)
38 : InsetCollapsable(bp), params_(params)
42 InsetBranch::InsetBranch(InsetBranch const & in)
43 : InsetCollapsable(in), params_(in.params_)
47 InsetBranch::~InsetBranch()
49 InsetBranchMailer(*this).hideDialog();
53 Inset * InsetBranch::clone() const
55 return new InsetBranch(*this);
59 docstring const InsetBranch::editMessage() const
61 return _("Opened Branch Inset");
65 void InsetBranch::write(Buffer const & buf, ostream & os) const
68 InsetCollapsable::write(buf, os);
72 void InsetBranch::read(Buffer const & buf, Lexer & lex)
75 InsetCollapsable::read(buf, lex);
79 void InsetBranch::setButtonLabel()
81 docstring s = _("Branch: ") + params_.branch;
82 if (!params_.branch.empty()) {
84 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
85 if (c == Color_none) {
89 if (decoration() == Classic)
90 setLabel(isOpen() ? s : getNewLabel(s) );
92 setLabel(params_.branch + ": " + getNewLabel(s));
96 ColorCode InsetBranch::backgroundColor() const
98 if (!params_.branch.empty()) {
100 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
101 if (c == Color_none) {
106 return Inset::backgroundColor();
110 bool InsetBranch::showInsetDialog(BufferView * bv) const
112 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
117 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
119 switch (cmd.action) {
120 case LFUN_INSET_MODIFY: {
121 InsetBranchParams params;
122 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
123 params_.branch = params.branch;
124 setLayout(cur.buffer().params());
128 case LFUN_MOUSE_PRESS:
129 if (cmd.button() != mouse_button::button3)
130 InsetCollapsable::doDispatch(cur, cmd);
135 case LFUN_INSET_DIALOG_UPDATE:
136 InsetBranchMailer(*this).updateDialog(&cur.bv());
139 case LFUN_MOUSE_RELEASE:
140 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
141 InsetBranchMailer(*this).showDialog(&cur.bv());
143 InsetCollapsable::doDispatch(cur, cmd);
147 case LFUN_INSET_TOGGLE:
148 if (cmd.argument() == "assign") {
149 // The branch inset uses "assign".
150 if (isBranchSelected(cur.buffer())) {
151 if (status() != Open)
152 setStatus(cur, Open);
156 if (status() != Collapsed)
157 setStatus(cur, Collapsed);
163 InsetCollapsable::doDispatch(cur, cmd);
167 InsetCollapsable::doDispatch(cur, cmd);
173 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
174 FuncStatus & flag) const
176 switch (cmd.action) {
177 case LFUN_INSET_MODIFY:
178 case LFUN_INSET_DIALOG_UPDATE:
182 case LFUN_INSET_TOGGLE:
183 if (cmd.argument() == "open" || cmd.argument() == "close" ||
184 cmd.argument() == "toggle")
186 else if (cmd.argument() == "assign"
187 || cmd.argument().empty()) {
188 if (isBranchSelected(cur.buffer()))
189 flag.enabled(status() != Open);
191 flag.enabled(status() != Collapsed);
197 return InsetCollapsable::getStatus(cur, cmd, flag);
203 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
205 Buffer const & realbuffer = *buffer.masterBuffer();
206 BranchList const & branchlist = realbuffer.params().branchlist();
207 BranchList::const_iterator const end = branchlist.end();
208 BranchList::const_iterator it =
209 find_if(branchlist.begin(), end,
210 BranchNamesEqual(params_.branch));
213 return it->getSelected();
217 void InsetBranch::updateLabels(Buffer const & buf, ParIterator const & it)
219 if (isBranchSelected(buf))
220 InsetCollapsable::updateLabels(buf, it);
222 TextClass const & tclass = buf.params().getTextClass();
223 Counters savecnt = tclass.counters();
224 InsetCollapsable::updateLabels(buf, it);
225 tclass.counters() = savecnt;
230 int InsetBranch::latex(Buffer const & buf, odocstream & os,
231 OutputParams const & runparams) const
233 return isBranchSelected(buf) ?
234 InsetText::latex(buf, os, runparams) : 0;
238 int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
239 OutputParams const & runparams) const
241 if (!isBranchSelected(buf))
244 os << '[' << buf.B_("branch") << ' ' << params_.branch << ":\n";
245 InsetText::plaintext(buf, os, runparams);
248 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
252 int InsetBranch::docbook(Buffer const & buf, odocstream & os,
253 OutputParams const & runparams) const
255 return isBranchSelected(buf) ?
256 InsetText::docbook(buf, os, runparams) : 0;
260 void InsetBranch::textString(Buffer const & buf, odocstream & os) const
262 if (isBranchSelected(buf))
263 os << paragraphs().begin()->asString(buf, true);
267 void InsetBranch::validate(LaTeXFeatures & features) const
269 InsetText::validate(features);
273 bool InsetBranch::isMacroScope(Buffer const & buf) const
275 // Its own scope if not selected by buffer
276 return !isBranchSelected(buf);
280 string const InsetBranchMailer::name_("branch");
282 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
287 string const InsetBranchMailer::inset2string(Buffer const &) const
289 return params2string(inset_.params());
293 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
296 data << name_ << ' ';
302 void InsetBranchMailer::string2params(string const & in,
303 InsetBranchParams & params)
305 params = InsetBranchParams();
309 istringstream data(in);
316 return print_mailer_error("InsetBranchMailer", in, 1, name_);
318 // This is part of the inset proper that is usually swallowed
319 // by Text::readInset
322 if (!lex || id != "Branch")
323 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
329 void InsetBranchParams::write(ostream & os) const
331 os << "Branch " << to_utf8(branch) << '\n';
335 void InsetBranchParams::read(Lexer & lex)