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_MOUSE_RELEASE:
134 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
135 InsetBranchMailer(*this).showDialog(&cur.bv());
137 InsetCollapsable::doDispatch(cur, cmd);
141 case LFUN_INSET_TOGGLE:
142 if (cmd.argument() == "assign") {
143 // The branch inset uses "assign".
144 if (isBranchSelected()) {
145 if (status() != Open)
146 setStatus(cur, Open);
150 if (status() != Collapsed)
151 setStatus(cur, Collapsed);
157 InsetCollapsable::doDispatch(cur, cmd);
161 InsetCollapsable::doDispatch(cur, cmd);
167 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
168 FuncStatus & flag) const
170 switch (cmd.action) {
171 case LFUN_INSET_MODIFY:
172 case LFUN_INSET_DIALOG_UPDATE:
176 case LFUN_INSET_TOGGLE:
177 if (cmd.argument() == "open" || cmd.argument() == "close" ||
178 cmd.argument() == "toggle")
180 else if (cmd.argument() == "assign" || cmd.argument().empty()) {
181 if (isBranchSelected())
182 flag.enabled(status() != Open);
184 flag.enabled(status() != Collapsed);
190 return InsetCollapsable::getStatus(cur, cmd, flag);
196 bool InsetBranch::isBranchSelected() const
198 Buffer const & realbuffer = *buffer().masterBuffer();
199 BranchList const & branchlist = realbuffer.params().branchlist();
200 BranchList::const_iterator const end = branchlist.end();
201 BranchList::const_iterator it =
202 find_if(branchlist.begin(), end,
203 BranchNamesEqual(params_.branch));
206 return it->getSelected();
210 void InsetBranch::updateLabels(ParIterator const & it)
212 if (isBranchSelected())
213 InsetCollapsable::updateLabels(it);
215 DocumentClass const & tclass = buffer().params().documentClass();
216 Counters savecnt = tclass.counters();
217 InsetCollapsable::updateLabels(it);
218 tclass.counters() = savecnt;
223 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
225 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
229 int InsetBranch::plaintext(odocstream & os,
230 OutputParams const & runparams) const
232 if (!isBranchSelected())
235 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
236 InsetText::plaintext(os, runparams);
239 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
243 int InsetBranch::docbook(odocstream & os,
244 OutputParams const & runparams) const
246 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
250 void InsetBranch::textString(odocstream & os) const
252 if (isBranchSelected())
253 os << paragraphs().begin()->asString(true);
257 void InsetBranch::validate(LaTeXFeatures & features) const
259 InsetText::validate(features);
263 bool InsetBranch::isMacroScope() const
265 // Its own scope if not selected by buffer
266 return !isBranchSelected();
270 string const InsetBranchMailer::name_("branch");
272 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
277 string const InsetBranchMailer::inset2string(Buffer const &) const
279 return params2string(inset_.params());
283 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
286 data << name_ << ' ';
292 void InsetBranchMailer::string2params(string const & in,
293 InsetBranchParams & params)
295 params = InsetBranchParams();
299 istringstream data(in);
306 return print_mailer_error("InsetBranchMailer", in, 1, name_);
308 // This is part of the inset proper that is usually swallowed
309 // by Text::readInset
312 if (!lex || id != "Branch")
313 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
319 void InsetBranchParams::write(ostream & os) const
321 os << "Branch " << to_utf8(branch) << '\n';
325 void InsetBranchParams::read(Lexer & lex)