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
53 InsetCollapsable::write(os);
57 void InsetBranch::read(Lexer & lex)
60 InsetCollapsable::read(lex);
64 docstring InsetBranch::toolTip(BufferView const & bv, int x, int y) const
66 docstring const status = isBranchSelected() ?
67 _("active") : _("non-active");
68 docstring const heading =
69 support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
70 docstring const contents = InsetCollapsable::toolTip(bv, x, y);
71 if (isOpen(bv) || contents.empty())
74 return heading + from_ascii("\n") + contents;
78 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
80 docstring s = _("Branch: ") + params_.branch;
81 Buffer const & realbuffer = *buffer().masterBuffer();
82 BranchList const & branchlist = realbuffer.params().branchlist();
83 if (!branchlist.find(params_.branch)
84 && buffer().params().branchlist().find(params_.branch))
85 s = _("Branch (child only): ") + params_.branch;
86 else if (!branchlist.find(params_.branch))
87 s = _("Branch (undefined): ") + params_.branch;
88 if (!params_.branch.empty()) {
90 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
94 s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
95 if (decoration() == InsetLayout::CLASSIC)
96 return isOpen(bv) ? s : getNewLabel(s);
98 return params_.branch + ": " + getNewLabel(s);
102 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
104 if (params_.branch.empty())
105 return Inset::backgroundColor(pi);
107 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
114 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
116 switch (cmd.action) {
117 case LFUN_INSET_MODIFY: {
118 InsetBranchParams params;
119 InsetBranch::string2params(to_utf8(cmd.argument()), params);
120 params_.branch = params.branch;
123 case LFUN_BRANCH_ACTIVATE:
124 case LFUN_BRANCH_DEACTIVATE: {
125 // FIXME: I do not like this cast, but have no other idea...
126 Buffer const * buf = buffer().masterBuffer();
127 BranchList const & branchlist = buf->params().branchlist();
128 Branch * our_branch = const_cast<Branch *>(branchlist.find(params_.branch));
131 our_branch = buffer().params().branchlist().find(params_.branch);
135 our_branch->setSelected(cmd.action == LFUN_BRANCH_ACTIVATE);
138 case LFUN_INSET_TOGGLE:
139 if (cmd.argument() == "assign")
140 setStatus(cur, isBranchSelected() ? Open : Collapsed);
142 InsetCollapsable::doDispatch(cur, cmd);
146 InsetCollapsable::doDispatch(cur, cmd);
152 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
153 FuncStatus & flag) const
155 switch (cmd.action) {
156 case LFUN_INSET_MODIFY:
157 flag.setEnabled(true);
160 case LFUN_BRANCH_ACTIVATE:
161 flag.setEnabled(!isBranchSelected());
164 case LFUN_BRANCH_DEACTIVATE:
165 flag.setEnabled(isBranchSelected());
168 case LFUN_INSET_TOGGLE:
169 if (cmd.argument() == "assign")
170 flag.setEnabled(true);
172 return InsetCollapsable::getStatus(cur, cmd, flag);
175 return InsetCollapsable::getStatus(cur, cmd, flag);
181 bool InsetBranch::isBranchSelected() const
183 Buffer const & realbuffer = *buffer().masterBuffer();
184 BranchList const & branchlist = realbuffer.params().branchlist();
185 Branch const * ourBranch = branchlist.find(params_.branch);
188 // this branch is defined in child only
189 ourBranch = buffer().params().branchlist().find(params_.branch);
193 return ourBranch->isSelected();
197 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
199 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
203 int InsetBranch::plaintext(odocstream & os,
204 OutputParams const & runparams) const
206 if (!isBranchSelected())
209 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
210 InsetText::plaintext(os, runparams);
213 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
217 int InsetBranch::docbook(odocstream & os,
218 OutputParams const & runparams) const
220 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
224 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
226 if (isBranchSelected())
227 return InsetText::xhtml(xs, rp);
232 void InsetBranch::tocString(odocstream & os) const
234 if (isBranchSelected())
235 InsetCollapsable::tocString(os);
239 void InsetBranch::validate(LaTeXFeatures & features) const
241 if (isBranchSelected())
242 InsetCollapsable::validate(features);
246 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
248 return from_ascii("context-branch");
252 bool InsetBranch::isMacroScope() const
254 // Its own scope if not selected by buffer
255 return !isBranchSelected();
259 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");
283 void InsetBranch::addToToc(DocIterator const & cpit)
285 DocIterator pit = cpit;
286 pit.push_back(CursorSlice(*this));
288 Toc & toc = buffer().tocBackend().toc("branch");
289 docstring const str = params_.branch + ": " + text().getPar(0).asString();
290 toc.push_back(TocItem(pit, 0, str));
291 // Proceed with the rest of the inset.
292 InsetCollapsable::addToToc(cpit);
296 void InsetBranchParams::write(ostream & os) const
298 os << "Branch " << to_utf8(branch);
302 void InsetBranchParams::read(Lexer & lex)
305 branch = lex.getDocString();