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 "BufferView.h"
18 #include "BranchList.h"
22 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
26 #include "OutputParams.h"
27 #include "output_xhtml.h"
28 #include "TextClass.h"
29 #include "TocBackend.h"
31 #include "support/debug.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
35 #include "frontends/Application.h"
44 InsetBranch::InsetBranch(Buffer * buf, InsetBranchParams const & params)
45 : InsetCollapsable(buf, InsetText::DefaultLayout), params_(params)
49 void InsetBranch::write(ostream & os) const
52 InsetCollapsable::write(os);
56 void InsetBranch::read(Lexer & lex)
59 InsetCollapsable::read(lex);
63 docstring InsetBranch::toolTip(BufferView const & bv, int x, int y) const
65 docstring const status = isBranchSelected() ?
66 _("active") : _("non-active");
67 docstring const heading =
68 support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
69 docstring const contents = InsetCollapsable::toolTip(bv, x, y);
70 if (isOpen(bv) || contents.empty())
73 return heading + from_ascii("\n") + contents;
77 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
79 docstring s = _("Branch: ") + params_.branch;
80 Buffer const & realbuffer = *buffer().masterBuffer();
81 BranchList const & branchlist = realbuffer.params().branchlist();
82 if (!branchlist.find(params_.branch)
83 && buffer().params().branchlist().find(params_.branch))
84 s = _("Branch (child only): ") + params_.branch;
85 else if (!branchlist.find(params_.branch))
86 s = _("Branch (undefined): ") + params_.branch;
87 if (!params_.branch.empty()) {
89 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
93 s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
94 if (decoration() == InsetLayout::CLASSIC)
95 return isOpen(bv) ? s : getNewLabel(s);
97 return params_.branch + ": " + getNewLabel(s);
101 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
103 if (params_.branch.empty())
104 return Inset::backgroundColor(pi);
106 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
113 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
115 switch (cmd.action) {
116 case LFUN_INSET_MODIFY: {
117 InsetBranchParams params;
118 InsetBranch::string2params(to_utf8(cmd.argument()), params);
119 params_.branch = params.branch;
122 case LFUN_BRANCH_ACTIVATE:
123 case LFUN_BRANCH_DEACTIVATE: {
124 // FIXME: I do not like this cast, but have no other idea...
125 Buffer const * buf = buffer().masterBuffer();
126 BranchList const & branchlist = buf->params().branchlist();
127 Branch * our_branch = const_cast<Branch *>(branchlist.find(params_.branch));
130 our_branch = buffer().params().branchlist().find(params_.branch);
134 our_branch->setSelected(cmd.action == LFUN_BRANCH_ACTIVATE);
137 case LFUN_INSET_TOGGLE:
138 if (cmd.argument() == "assign")
139 setStatus(cur, isBranchSelected() ? Open : Collapsed);
141 InsetCollapsable::doDispatch(cur, cmd);
145 InsetCollapsable::doDispatch(cur, cmd);
151 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
152 FuncStatus & flag) const
154 switch (cmd.action) {
155 case LFUN_INSET_MODIFY:
156 flag.setEnabled(true);
159 case LFUN_BRANCH_ACTIVATE:
160 flag.setEnabled(!isBranchSelected());
163 case LFUN_BRANCH_DEACTIVATE:
164 flag.setEnabled(isBranchSelected());
167 case LFUN_INSET_TOGGLE:
168 if (cmd.argument() == "assign")
169 flag.setEnabled(true);
171 return InsetCollapsable::getStatus(cur, cmd, flag);
174 return InsetCollapsable::getStatus(cur, cmd, flag);
180 bool InsetBranch::isBranchSelected() const
182 Buffer const & realbuffer = *buffer().masterBuffer();
183 BranchList const & branchlist = realbuffer.params().branchlist();
184 Branch const * ourBranch = branchlist.find(params_.branch);
187 // this branch is defined in child only
188 ourBranch = buffer().params().branchlist().find(params_.branch);
192 return ourBranch->isSelected();
196 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
198 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
202 int InsetBranch::plaintext(odocstream & os,
203 OutputParams const & runparams) const
205 if (!isBranchSelected())
208 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
209 InsetText::plaintext(os, runparams);
212 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
216 int InsetBranch::docbook(odocstream & os,
217 OutputParams const & runparams) const
219 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
223 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
225 if (isBranchSelected())
226 return InsetText::xhtml(xs, rp);
231 void InsetBranch::tocString(odocstream & os) const
233 if (isBranchSelected())
234 InsetCollapsable::tocString(os);
238 void InsetBranch::validate(LaTeXFeatures & features) const
240 if (isBranchSelected())
241 InsetCollapsable::validate(features);
245 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
247 return from_ascii("context-branch");
251 bool InsetBranch::isMacroScope() const
253 // Its own scope if not selected by buffer
254 return !isBranchSelected();
258 string InsetBranch::params2string(InsetBranchParams const & params)
261 data << "branch" << ' ';
267 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
269 params = InsetBranchParams();
273 istringstream data(in);
276 lex.setContext("InsetBranch::string2params");
277 lex >> "branch" >> "Branch";
282 void InsetBranch::addToToc(DocIterator const & cpit)
284 DocIterator pit = cpit;
285 pit.push_back(CursorSlice(*this));
287 Toc & toc = buffer().tocBackend().toc("branch");
288 docstring const str = params_.branch + ": " + text().getPar(0).asString();
289 toc.push_back(TocItem(pit, 0, str));
290 // Proceed with the rest of the inset.
291 InsetCollapsable::addToToc(cpit);
295 void InsetBranchParams::write(ostream & os) const
297 os << "Branch " << to_utf8(branch) << '\n';
301 void InsetBranchParams::read(Lexer & lex)
304 branch = lex.getDocString();