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
54 InsetCollapsable::write(os);
58 void InsetBranch::read(Lexer & lex)
61 InsetCollapsable::read(lex);
65 docstring InsetBranch::toolTip(BufferView const & bv, int x, int y) const
67 docstring const status = isBranchSelected() ?
68 _("active") : _("non-active");
69 docstring const heading =
70 support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
71 docstring const contents = InsetCollapsable::toolTip(bv, x, y);
72 if (isOpen(bv) || contents.empty())
75 return heading + from_ascii("\n") + contents;
79 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
81 docstring s = _("Branch: ") + params_.branch;
82 Buffer const & realbuffer = *buffer().masterBuffer();
83 BranchList const & branchlist = realbuffer.params().branchlist();
84 if (!branchlist.find(params_.branch)
85 && buffer().params().branchlist().find(params_.branch))
86 s = _("Branch (child only): ") + params_.branch;
87 else if (!branchlist.find(params_.branch))
88 s = _("Branch (undefined): ") + params_.branch;
89 if (!params_.branch.empty()) {
91 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
95 s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
96 if (decoration() == InsetLayout::CLASSIC)
97 return isOpen(bv) ? s : getNewLabel(s);
99 return params_.branch + ": " + getNewLabel(s);
103 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
105 if (params_.branch.empty())
106 return Inset::backgroundColor(pi);
108 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
115 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
117 switch (cmd.action) {
118 case LFUN_INSET_MODIFY: {
119 InsetBranchParams params;
120 InsetBranch::string2params(to_utf8(cmd.argument()), params);
121 params_.branch = params.branch;
124 case LFUN_BRANCH_ACTIVATE:
125 case LFUN_BRANCH_DEACTIVATE: {
126 // FIXME: I do not like this cast, but have no other idea...
127 Buffer const * buf = buffer().masterBuffer();
128 BranchList const & branchlist = buf->params().branchlist();
129 Branch * our_branch = const_cast<Branch *>(branchlist.find(params_.branch));
132 our_branch = buffer().params().branchlist().find(params_.branch);
136 our_branch->setSelected(cmd.action == LFUN_BRANCH_ACTIVATE);
139 case LFUN_INSET_TOGGLE:
140 if (cmd.argument() == "assign")
141 setStatus(cur, isBranchSelected() ? Open : Collapsed);
143 InsetCollapsable::doDispatch(cur, cmd);
147 InsetCollapsable::doDispatch(cur, cmd);
153 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
154 FuncStatus & flag) const
156 switch (cmd.action) {
157 case LFUN_INSET_MODIFY:
158 flag.setEnabled(true);
161 case LFUN_BRANCH_ACTIVATE:
162 flag.setEnabled(!isBranchSelected());
165 case LFUN_BRANCH_DEACTIVATE:
166 flag.setEnabled(isBranchSelected());
169 case LFUN_INSET_TOGGLE:
170 if (cmd.argument() == "assign")
171 flag.setEnabled(true);
173 return InsetCollapsable::getStatus(cur, cmd, flag);
176 return InsetCollapsable::getStatus(cur, cmd, flag);
182 bool InsetBranch::isBranchSelected() const
184 Buffer const & realbuffer = *buffer().masterBuffer();
185 BranchList const & branchlist = realbuffer.params().branchlist();
186 Branch const * ourBranch = branchlist.find(params_.branch);
189 // this branch is defined in child only
190 ourBranch = buffer().params().branchlist().find(params_.branch);
194 return ourBranch->isSelected();
198 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
200 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
204 int InsetBranch::plaintext(odocstream & os,
205 OutputParams const & runparams) const
207 if (!isBranchSelected())
210 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
211 InsetText::plaintext(os, runparams);
214 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
218 int InsetBranch::docbook(odocstream & os,
219 OutputParams const & runparams) const
221 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
225 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
227 if (isBranchSelected())
228 return InsetText::xhtml(xs, rp);
233 void InsetBranch::tocString(odocstream & os) const
235 if (isBranchSelected())
236 InsetCollapsable::tocString(os);
240 void InsetBranch::validate(LaTeXFeatures & features) const
242 if (isBranchSelected())
243 InsetCollapsable::validate(features);
247 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
249 return from_ascii("context-branch");
253 bool InsetBranch::isMacroScope() const
255 // Its own scope if not selected by buffer
256 return !isBranchSelected();
260 string InsetBranch::params2string(InsetBranchParams const & params)
268 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
270 params = InsetBranchParams();
274 istringstream data(in);
277 lex.setContext("InsetBranch::string2params");
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 << to_utf8(branch);
301 void InsetBranchParams::read(Lexer & lex)