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, int) 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);
73 return toolTipText(heading + from_ascii("\n"));
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);
120 cur.recordUndoInset(ATOMIC_UNDO, this);
121 params_.branch = params.branch;
122 // what we really want here is a TOC update, but that means
123 // a full buffer update
124 cur.forceBufferUpdate();
127 case LFUN_BRANCH_ACTIVATE:
128 case LFUN_BRANCH_DEACTIVATE: {
129 // FIXME: I do not like this cast, but have no other idea...
130 Buffer const * buf = buffer().masterBuffer();
131 BranchList const & branchlist = buf->params().branchlist();
132 Branch * our_branch = const_cast<Branch *>(branchlist.find(params_.branch));
135 our_branch = buffer().params().branchlist().find(params_.branch);
139 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE);
140 if (our_branch->isSelected() != activate) {
141 our_branch->setSelected(activate);
142 cur.forceBufferUpdate();
146 case LFUN_INSET_TOGGLE:
147 if (cmd.argument() == "assign")
148 setStatus(cur, isBranchSelected() ? Open : Collapsed);
150 InsetCollapsable::doDispatch(cur, cmd);
154 InsetCollapsable::doDispatch(cur, cmd);
160 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
161 FuncStatus & flag) const
163 switch (cmd.action()) {
164 case LFUN_INSET_MODIFY:
165 flag.setEnabled(true);
168 case LFUN_BRANCH_ACTIVATE:
169 flag.setEnabled(!isBranchSelected());
172 case LFUN_BRANCH_DEACTIVATE:
173 flag.setEnabled(isBranchSelected());
176 case LFUN_INSET_TOGGLE:
177 if (cmd.argument() == "assign")
178 flag.setEnabled(true);
180 return InsetCollapsable::getStatus(cur, cmd, flag);
184 return InsetCollapsable::getStatus(cur, cmd, flag);
190 bool InsetBranch::isBranchSelected() const
192 Buffer const & realbuffer = *buffer().masterBuffer();
193 BranchList const & branchlist = realbuffer.params().branchlist();
194 Branch const * ourBranch = branchlist.find(params_.branch);
197 // this branch is defined in child only
198 ourBranch = buffer().params().branchlist().find(params_.branch);
202 return ourBranch->isSelected();
206 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
208 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
212 int InsetBranch::plaintext(odocstream & os,
213 OutputParams const & runparams) const
215 if (!isBranchSelected())
218 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
219 InsetText::plaintext(os, runparams);
222 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
226 int InsetBranch::docbook(odocstream & os,
227 OutputParams const & runparams) const
229 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
233 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
235 if (isBranchSelected())
236 return InsetText::xhtml(xs, rp);
241 void InsetBranch::tocString(odocstream & os) const
243 if (isBranchSelected())
244 InsetCollapsable::tocString(os);
248 void InsetBranch::validate(LaTeXFeatures & features) const
250 if (isBranchSelected())
251 InsetCollapsable::validate(features);
255 docstring InsetBranch::contextMenuName() const
257 return from_ascii("context-branch");
261 bool InsetBranch::isMacroScope() const
263 // Its own scope if not selected by buffer
264 return !isBranchSelected();
268 string InsetBranch::params2string(InsetBranchParams const & params)
276 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
278 params = InsetBranchParams();
282 istringstream data(in);
285 lex.setContext("InsetBranch::string2params");
290 void InsetBranch::addToToc(DocIterator const & cpit)
292 DocIterator pit = cpit;
293 pit.push_back(CursorSlice(*this));
295 Toc & toc = buffer().tocBackend().toc("branch");
296 docstring const str = params_.branch + ": " + text().getPar(0).asString();
297 toc.push_back(TocItem(pit, 0, str, toolTipText()));
298 // Proceed with the rest of the inset.
299 InsetCollapsable::addToToc(cpit);
303 void InsetBranchParams::write(ostream & os) const
305 os << to_utf8(branch);
309 void InsetBranchParams::read(Lexer & lex)