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 docstring InsetBranch::toolTip(BufferView const & bv, int x, int y) const
81 return _("Branch: ") + params_.branch;
85 void InsetBranch::setButtonLabel()
87 docstring s = _("Branch: ") + params_.branch;
88 if (!params_.branch.empty()) {
90 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
91 if (c == Color_none) {
95 if (decoration() == InsetLayout::Classic)
96 setLabel(isOpen() ? s : getNewLabel(s) );
98 setLabel(params_.branch + ": " + getNewLabel(s));
102 ColorCode InsetBranch::backgroundColor() const
104 if (!params_.branch.empty()) {
106 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
107 if (c == Color_none) {
112 return Inset::backgroundColor();
116 bool InsetBranch::showInsetDialog(BufferView * bv) const
118 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
123 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
125 switch (cmd.action) {
126 case LFUN_INSET_MODIFY: {
127 InsetBranchParams params;
128 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
129 params_.branch = params.branch;
130 setLayout(cur.buffer().params());
134 case LFUN_MOUSE_PRESS:
135 if (cmd.button() != mouse_button::button3)
136 InsetCollapsable::doDispatch(cur, cmd);
141 case LFUN_INSET_DIALOG_UPDATE:
142 InsetBranchMailer(*this).updateDialog(&cur.bv());
145 case LFUN_MOUSE_RELEASE:
146 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
147 InsetBranchMailer(*this).showDialog(&cur.bv());
149 InsetCollapsable::doDispatch(cur, cmd);
153 case LFUN_INSET_TOGGLE:
154 if (cmd.argument() == "assign") {
155 // The branch inset uses "assign".
156 if (isBranchSelected(cur.buffer())) {
157 if (status() != Open)
158 setStatus(cur, Open);
162 if (status() != Collapsed)
163 setStatus(cur, Collapsed);
169 InsetCollapsable::doDispatch(cur, cmd);
173 InsetCollapsable::doDispatch(cur, cmd);
179 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
180 FuncStatus & flag) const
182 switch (cmd.action) {
183 case LFUN_INSET_MODIFY:
184 case LFUN_INSET_DIALOG_UPDATE:
188 case LFUN_INSET_TOGGLE:
189 if (cmd.argument() == "open" || cmd.argument() == "close" ||
190 cmd.argument() == "toggle")
192 else if (cmd.argument() == "assign"
193 || cmd.argument().empty()) {
194 if (isBranchSelected(cur.buffer()))
195 flag.enabled(status() != Open);
197 flag.enabled(status() != Collapsed);
203 return InsetCollapsable::getStatus(cur, cmd, flag);
209 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
211 Buffer const & realbuffer = *buffer.masterBuffer();
212 BranchList const & branchlist = realbuffer.params().branchlist();
213 BranchList::const_iterator const end = branchlist.end();
214 BranchList::const_iterator it =
215 find_if(branchlist.begin(), end,
216 BranchNamesEqual(params_.branch));
219 return it->getSelected();
223 void InsetBranch::updateLabels(Buffer const & buf, ParIterator const & it)
225 if (isBranchSelected(buf))
226 InsetCollapsable::updateLabels(buf, it);
228 TextClass const & tclass = buf.params().textClass();
229 Counters savecnt = tclass.counters();
230 InsetCollapsable::updateLabels(buf, it);
231 tclass.counters() = savecnt;
236 int InsetBranch::latex(Buffer const & buf, odocstream & os,
237 OutputParams const & runparams) const
239 return isBranchSelected(buf) ?
240 InsetText::latex(buf, os, runparams) : 0;
244 int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
245 OutputParams const & runparams) const
247 if (!isBranchSelected(buf))
250 os << '[' << buf.B_("branch") << ' ' << params_.branch << ":\n";
251 InsetText::plaintext(buf, os, runparams);
254 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
258 int InsetBranch::docbook(Buffer const & buf, odocstream & os,
259 OutputParams const & runparams) const
261 return isBranchSelected(buf) ?
262 InsetText::docbook(buf, os, runparams) : 0;
266 void InsetBranch::textString(Buffer const & buf, odocstream & os) const
268 if (isBranchSelected(buf))
269 os << paragraphs().begin()->asString(buf, true);
273 void InsetBranch::validate(LaTeXFeatures & features) const
275 InsetText::validate(features);
279 bool InsetBranch::isMacroScope(Buffer const & buf) const
281 // Its own scope if not selected by buffer
282 return !isBranchSelected(buf);
286 string const InsetBranchMailer::name_("branch");
288 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
293 string const InsetBranchMailer::inset2string(Buffer const &) const
295 return params2string(inset_.params());
299 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
302 data << name_ << ' ';
308 void InsetBranchMailer::string2params(string const & in,
309 InsetBranchParams & params)
311 params = InsetBranchParams();
315 istringstream data(in);
322 return print_mailer_error("InsetBranchMailer", in, 1, name_);
324 // This is part of the inset proper that is usually swallowed
325 // by Text::readInset
328 if (!lex || id != "Branch")
329 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
335 void InsetBranchParams::write(ostream & os) const
337 os << "Branch " << to_utf8(branch) << '\n';
341 void InsetBranchParams::read(Lexer & lex)