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(Buffer const & buf, InsetBranchParams const & params)
37 : InsetCollapsable(buf), params_(params)
41 InsetBranch::~InsetBranch()
43 InsetBranchMailer(*this).hideDialog();
47 docstring InsetBranch::editMessage() const
49 return _("Opened Branch Inset");
53 void InsetBranch::write(ostream & os) const
56 InsetCollapsable::write(os);
60 void InsetBranch::read(Lexer & lex)
63 InsetCollapsable::read(lex);
67 docstring InsetBranch::toolTip(BufferView const &, int, int) const
69 return _("Branch: ") + params_.branch;
73 void InsetBranch::setButtonLabel()
75 docstring s = _("Branch: ") + params_.branch;
76 if (!params_.branch.empty()) {
78 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
79 if (c == Color_none) {
83 if (decoration() == InsetLayout::Classic)
84 setLabel(isOpen() ? s : getNewLabel(s) );
86 setLabel(params_.branch + ": " + getNewLabel(s));
90 ColorCode InsetBranch::backgroundColor() const
92 if (!params_.branch.empty()) {
94 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
95 if (c == Color_none) {
100 return Inset::backgroundColor();
104 bool InsetBranch::showInsetDialog(BufferView * bv) const
106 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
111 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
113 switch (cmd.action) {
114 case LFUN_INSET_MODIFY: {
115 InsetBranchParams params;
116 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
117 params_.branch = params.branch;
118 setLayout(cur.buffer().params());
122 case LFUN_MOUSE_PRESS:
123 if (cmd.button() != mouse_button::button3)
124 InsetCollapsable::doDispatch(cur, cmd);
129 case LFUN_INSET_DIALOG_UPDATE:
130 InsetBranchMailer(*this).updateDialog(&cur.bv());
133 case LFUN_INSET_TOGGLE:
134 if (cmd.argument() == "assign") {
135 // The branch inset uses "assign".
136 if (isBranchSelected()) {
137 if (status() != Open)
138 setStatus(cur, Open);
142 if (status() != Collapsed)
143 setStatus(cur, Collapsed);
149 InsetCollapsable::doDispatch(cur, cmd);
153 InsetCollapsable::doDispatch(cur, cmd);
159 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
160 FuncStatus & flag) const
162 switch (cmd.action) {
163 case LFUN_INSET_MODIFY:
164 case LFUN_INSET_DIALOG_UPDATE:
168 case LFUN_INSET_TOGGLE:
169 if (cmd.argument() == "open" || cmd.argument() == "close" ||
170 cmd.argument() == "toggle")
172 else if (cmd.argument() == "assign" || cmd.argument().empty()) {
173 if (isBranchSelected())
174 flag.enabled(status() != Open);
176 flag.enabled(status() != Collapsed);
182 return InsetCollapsable::getStatus(cur, cmd, flag);
188 bool InsetBranch::isBranchSelected() const
190 Buffer const & realbuffer = *buffer().masterBuffer();
191 BranchList const & branchlist = realbuffer.params().branchlist();
192 BranchList::const_iterator const end = branchlist.end();
193 BranchList::const_iterator it =
194 find_if(branchlist.begin(), end,
195 BranchNamesEqual(params_.branch));
198 return it->getSelected();
202 void InsetBranch::updateLabels(ParIterator const & it)
204 if (isBranchSelected())
205 InsetCollapsable::updateLabels(it);
207 DocumentClass const & tclass = buffer().params().documentClass();
208 Counters savecnt = tclass.counters();
209 InsetCollapsable::updateLabels(it);
210 tclass.counters() = savecnt;
215 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
217 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
221 int InsetBranch::plaintext(odocstream & os,
222 OutputParams const & runparams) const
224 if (!isBranchSelected())
227 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
228 InsetText::plaintext(os, runparams);
231 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
235 int InsetBranch::docbook(odocstream & os,
236 OutputParams const & runparams) const
238 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
242 void InsetBranch::textString(odocstream & os) const
244 if (isBranchSelected())
245 os << paragraphs().begin()->asString(true);
249 void InsetBranch::validate(LaTeXFeatures & features) const
251 InsetText::validate(features);
255 bool InsetBranch::isMacroScope() const
257 // Its own scope if not selected by buffer
258 return !isBranchSelected();
262 string const InsetBranchMailer::name_("branch");
264 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
269 string const InsetBranchMailer::inset2string(Buffer const &) const
271 return params2string(inset_.params());
275 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
278 data << name_ << ' ';
284 void InsetBranchMailer::string2params(string const & in,
285 InsetBranchParams & params)
287 params = InsetBranchParams();
291 istringstream data(in);
298 return print_mailer_error("InsetBranchMailer", in, 1, name_);
300 // This is part of the inset proper that is usually swallowed
301 // by Text::readInset
304 if (!lex || id != "Branch")
305 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
311 void InsetBranchParams::write(ostream & os) const
313 os << "Branch " << to_utf8(branch) << '\n';
317 void InsetBranchParams::read(Lexer & lex)