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"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
26 #include "OutputParams.h"
35 using std::istringstream;
37 using std::ostringstream;
40 void InsetBranch::init()
46 InsetBranch::InsetBranch(BufferParams const & bp,
47 InsetBranchParams const & params)
48 : InsetCollapsable(bp), params_(params)
54 InsetBranch::InsetBranch(InsetBranch const & in)
55 : InsetCollapsable(in), params_(in.params_)
61 InsetBranch::~InsetBranch()
63 InsetBranchMailer(*this).hideDialog();
67 auto_ptr<Inset> InsetBranch::doClone() const
69 return auto_ptr<Inset>(new InsetBranch(*this));
73 docstring const InsetBranch::editMessage() const
75 return _("Opened Branch Inset");
79 void InsetBranch::write(Buffer const & buf, ostream & os) const
82 InsetCollapsable::write(buf, os);
86 void InsetBranch::read(Buffer const & buf, Lexer & lex)
89 InsetCollapsable::read(buf, lex);
94 void InsetBranch::setButtonLabel()
96 Font font(Font::ALL_SANE);
100 docstring s = _("Branch: ") + params_.branch;
101 if (!params_.branch.empty()) {
103 Color_color c = lcolor.getFromLyXName(to_utf8(params_.branch));
104 if (c == Color::none) {
105 s = _("Undef: ") + s;
108 font.setColor(Color::foreground);
109 setLabel(isOpen() ? s : getNewLabel(s) );
114 Color_color InsetBranch::backgroundColor() const
116 if (!params_.branch.empty()) {
118 Color_color c = lcolor.getFromLyXName(to_utf8(params_.branch));
119 if (c == Color::none) {
124 return Inset::backgroundColor();
128 bool InsetBranch::showInsetDialog(BufferView * bv) const
130 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
135 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
137 switch (cmd.action) {
138 case LFUN_INSET_MODIFY: {
139 InsetBranchParams params;
140 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
141 params_.branch = params.branch;
146 case LFUN_MOUSE_PRESS:
147 if (cmd.button() != mouse_button::button3)
148 InsetCollapsable::doDispatch(cur, cmd);
153 case LFUN_INSET_DIALOG_UPDATE:
154 InsetBranchMailer(*this).updateDialog(&cur.bv());
157 case LFUN_MOUSE_RELEASE:
158 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
159 InsetBranchMailer(*this).showDialog(&cur.bv());
161 InsetCollapsable::doDispatch(cur, cmd);
165 case LFUN_INSET_TOGGLE:
166 if (cmd.argument() == "assign") {
167 // The branch inset uses "assign".
168 if (isBranchSelected(cur.buffer())) {
169 if (status() != Open)
170 setStatus(cur, Open);
174 if (status() != Collapsed)
175 setStatus(cur, Collapsed);
181 InsetCollapsable::doDispatch(cur, cmd);
185 InsetCollapsable::doDispatch(cur, cmd);
191 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
192 FuncStatus & flag) const
194 switch (cmd.action) {
195 case LFUN_INSET_MODIFY:
196 case LFUN_INSET_DIALOG_UPDATE:
200 case LFUN_INSET_TOGGLE:
201 if (cmd.argument() == "open" || cmd.argument() == "close" ||
202 cmd.argument() == "toggle")
204 else if (cmd.argument() == "assign"
205 || cmd.argument().empty()) {
206 if (isBranchSelected(cur.buffer()))
207 flag.enabled(status() != Open);
209 flag.enabled(status() != Collapsed);
215 return InsetCollapsable::getStatus(cur, cmd, flag);
221 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
223 Buffer const & realbuffer = *buffer.getMasterBuffer();
224 BranchList const & branchlist = realbuffer.params().branchlist();
225 BranchList::const_iterator const end = branchlist.end();
226 BranchList::const_iterator it =
227 std::find_if(branchlist.begin(), end,
228 BranchNamesEqual(params_.branch));
231 return it->getSelected();
235 void InsetBranch::updateLabels(Buffer const & buf, ParIterator const & it)
237 if (isBranchSelected(buf))
238 InsetCollapsable::updateLabels(buf, it);
240 TextClass const & tclass = buf.params().getTextClass();
241 Counters savecnt = tclass.counters();
242 InsetCollapsable::updateLabels(buf, it);
243 tclass.counters() = savecnt;
248 int InsetBranch::latex(Buffer const & buf, odocstream & os,
249 OutputParams const & runparams) const
251 return isBranchSelected(buf) ?
252 InsetText::latex(buf, os, runparams) : 0;
256 int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
257 OutputParams const & runparams) const
259 if (!isBranchSelected(buf))
262 os << '[' << buf.B_("branch") << ' ' << params_.branch << ":\n";
263 InsetText::plaintext(buf, os, runparams);
266 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
270 int InsetBranch::docbook(Buffer const & buf, odocstream & os,
271 OutputParams const & runparams) const
273 return isBranchSelected(buf) ?
274 InsetText::docbook(buf, os, runparams) : 0;
278 void InsetBranch::textString(Buffer const & buf, odocstream & os) const
280 if (isBranchSelected(buf))
281 os << paragraphs().begin()->asString(buf, true);
285 void InsetBranch::validate(LaTeXFeatures & features) const
287 InsetText::validate(features);
292 string const InsetBranchMailer::name_("branch");
294 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
299 string const InsetBranchMailer::inset2string(Buffer const &) const
301 return params2string(inset_.params());
305 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
308 data << name_ << ' ';
314 void InsetBranchMailer::string2params(string const & in,
315 InsetBranchParams & params)
317 params = InsetBranchParams();
321 istringstream data(in);
328 return print_mailer_error("InsetBranchMailer", in, 1, name_);
330 // This is part of the inset proper that is usually swallowed
331 // by Text::readInset
334 if (!lex || id != "Branch")
335 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
341 void InsetBranchParams::write(ostream & os) const
343 os << "Branch " << to_utf8(branch) << '\n';
347 void InsetBranchParams::read(Lexer & lex)