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"
27 #include "TextClass.h"
35 using std::istringstream;
37 using std::ostringstream;
40 InsetBranch::InsetBranch(BufferParams const & bp,
41 InsetBranchParams const & params)
42 : InsetCollapsable(bp), params_(params)
46 InsetBranch::InsetBranch(InsetBranch const & in)
47 : InsetCollapsable(in), params_(in.params_)
51 InsetBranch::~InsetBranch()
53 InsetBranchMailer(*this).hideDialog();
57 Inset * InsetBranch::clone() const
59 return new InsetBranch(*this);
63 docstring const InsetBranch::editMessage() const
65 return _("Opened Branch Inset");
69 void InsetBranch::write(Buffer const & buf, ostream & os) const
72 InsetCollapsable::write(buf, os);
76 void InsetBranch::read(Buffer const & buf, Lexer & lex)
79 InsetCollapsable::read(buf, lex);
83 void InsetBranch::setButtonLabel()
85 docstring s = _("Branch: ") + params_.branch;
86 if (!params_.branch.empty()) {
88 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
89 if (c == Color_none) {
93 if (decoration() == Classic)
94 setLabel(isOpen() ? s : getNewLabel(s) );
96 setLabel(params_.branch + ": " + getNewLabel(s));
100 ColorCode InsetBranch::backgroundColor() const
102 if (!params_.branch.empty()) {
104 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
105 if (c == Color_none) {
110 return Inset::backgroundColor();
114 bool InsetBranch::showInsetDialog(BufferView * bv) const
116 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
121 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
123 switch (cmd.action) {
124 case LFUN_INSET_MODIFY: {
125 InsetBranchParams params;
126 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
127 params_.branch = params.branch;
128 setLayout(cur.buffer().params());
132 case LFUN_MOUSE_PRESS:
133 if (cmd.button() != mouse_button::button3)
134 InsetCollapsable::doDispatch(cur, cmd);
139 case LFUN_INSET_DIALOG_UPDATE:
140 InsetBranchMailer(*this).updateDialog(&cur.bv());
143 case LFUN_MOUSE_RELEASE:
144 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
145 InsetBranchMailer(*this).showDialog(&cur.bv());
147 InsetCollapsable::doDispatch(cur, cmd);
151 case LFUN_INSET_TOGGLE:
152 if (cmd.argument() == "assign") {
153 // The branch inset uses "assign".
154 if (isBranchSelected(cur.buffer())) {
155 if (status() != Open)
156 setStatus(cur, Open);
160 if (status() != Collapsed)
161 setStatus(cur, Collapsed);
167 InsetCollapsable::doDispatch(cur, cmd);
171 InsetCollapsable::doDispatch(cur, cmd);
177 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
178 FuncStatus & flag) const
180 switch (cmd.action) {
181 case LFUN_INSET_MODIFY:
182 case LFUN_INSET_DIALOG_UPDATE:
186 case LFUN_INSET_TOGGLE:
187 if (cmd.argument() == "open" || cmd.argument() == "close" ||
188 cmd.argument() == "toggle")
190 else if (cmd.argument() == "assign"
191 || cmd.argument().empty()) {
192 if (isBranchSelected(cur.buffer()))
193 flag.enabled(status() != Open);
195 flag.enabled(status() != Collapsed);
201 return InsetCollapsable::getStatus(cur, cmd, flag);
207 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
209 Buffer const & realbuffer = *buffer.masterBuffer();
210 BranchList const & branchlist = realbuffer.params().branchlist();
211 BranchList::const_iterator const end = branchlist.end();
212 BranchList::const_iterator it =
213 std::find_if(branchlist.begin(), end,
214 BranchNamesEqual(params_.branch));
217 return it->getSelected();
221 void InsetBranch::updateLabels(Buffer const & buf, ParIterator const & it)
223 if (isBranchSelected(buf))
224 InsetCollapsable::updateLabels(buf, it);
226 TextClass const & tclass = buf.params().getTextClass();
227 Counters savecnt = tclass.counters();
228 InsetCollapsable::updateLabels(buf, it);
229 tclass.counters() = savecnt;
234 int InsetBranch::latex(Buffer const & buf, odocstream & os,
235 OutputParams const & runparams) const
237 return isBranchSelected(buf) ?
238 InsetText::latex(buf, os, runparams) : 0;
242 int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
243 OutputParams const & runparams) const
245 if (!isBranchSelected(buf))
248 os << '[' << buf.B_("branch") << ' ' << params_.branch << ":\n";
249 InsetText::plaintext(buf, os, runparams);
252 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
256 int InsetBranch::docbook(Buffer const & buf, odocstream & os,
257 OutputParams const & runparams) const
259 return isBranchSelected(buf) ?
260 InsetText::docbook(buf, os, runparams) : 0;
264 void InsetBranch::textString(Buffer const & buf, odocstream & os) const
266 if (isBranchSelected(buf))
267 os << paragraphs().begin()->asString(buf, true);
271 void InsetBranch::validate(LaTeXFeatures & features) const
273 InsetText::validate(features);
278 string const InsetBranchMailer::name_("branch");
280 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
285 string const InsetBranchMailer::inset2string(Buffer const &) const
287 return params2string(inset_.params());
291 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
294 data << name_ << ' ';
300 void InsetBranchMailer::string2params(string const & in,
301 InsetBranchParams & params)
303 params = InsetBranchParams();
307 istringstream data(in);
314 return print_mailer_error("InsetBranchMailer", in, 1, name_);
316 // This is part of the inset proper that is usually swallowed
317 // by Text::readInset
320 if (!lex || id != "Branch")
321 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
327 void InsetBranchParams::write(ostream & os) const
329 os << "Branch " << to_utf8(branch) << '\n';
333 void InsetBranchParams::read(Lexer & lex)