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 InsetBranch::~InsetBranch()
51 hideDialogs("branch", this);
55 void InsetBranch::write(ostream & os) const
58 InsetCollapsable::write(os);
62 void InsetBranch::read(Lexer & lex)
65 InsetCollapsable::read(lex);
69 docstring InsetBranch::toolTip(BufferView const & bv, int x, int y) const
71 docstring const status = isBranchSelected() ?
72 _("active") : _("non-active");
73 docstring const heading =
74 support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
75 docstring const contents = InsetCollapsable::toolTip(bv, x, y);
76 if (isOpen(bv) || contents.empty())
79 return heading + from_ascii("\n") + contents;
83 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
85 docstring s = _("Branch: ") + params_.branch;
86 Buffer const & realbuffer = *buffer().masterBuffer();
87 BranchList const & branchlist = realbuffer.params().branchlist();
88 if (!branchlist.find(params_.branch)
89 && buffer().params().branchlist().find(params_.branch))
90 s = _("Branch (child only): ") + params_.branch;
91 else if (!branchlist.find(params_.branch))
92 s = _("Branch (undefined): ") + params_.branch;
93 if (!params_.branch.empty()) {
95 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
99 s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
100 if (decoration() == InsetLayout::CLASSIC)
101 return isOpen(bv) ? s : getNewLabel(s);
103 return params_.branch + ": " + getNewLabel(s);
107 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
109 if (params_.branch.empty())
110 return Inset::backgroundColor(pi);
112 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
119 bool InsetBranch::showInsetDialog(BufferView * bv) const
121 bv->showDialog("branch", params2string(params()),
122 const_cast<InsetBranch *>(this));
127 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
129 switch (cmd.action) {
130 case LFUN_INSET_MODIFY: {
131 InsetBranchParams params;
132 InsetBranch::string2params(to_utf8(cmd.argument()), params);
133 params_.branch = params.branch;
137 case LFUN_INSET_DIALOG_UPDATE:
138 cur.bv().updateDialog("branch", params2string(params()));
141 case LFUN_BRANCH_ACTIVATE:
142 case LFUN_BRANCH_DEACTIVATE: {
143 // FIXME: I do not like this cast, but have no other idea...
144 Buffer const * buf = buffer().masterBuffer();
145 BranchList const & branchlist = buf->params().branchlist();
146 Branch * our_branch = const_cast<Branch *>(branchlist.find(params_.branch));
149 our_branch = buffer().params().branchlist().find(params_.branch);
153 our_branch->setSelected(cmd.action == LFUN_BRANCH_ACTIVATE);
157 case LFUN_INSET_TOGGLE:
158 if (cmd.argument() == "assign")
159 setStatus(cur, isBranchSelected() ? Open : Collapsed);
161 InsetCollapsable::doDispatch(cur, cmd);
165 InsetCollapsable::doDispatch(cur, cmd);
171 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
172 FuncStatus & flag) const
174 switch (cmd.action) {
175 case LFUN_INSET_MODIFY:
176 case LFUN_INSET_DIALOG_UPDATE:
177 flag.setEnabled(true);
180 case LFUN_BRANCH_ACTIVATE:
181 flag.setEnabled(!isBranchSelected());
184 case LFUN_BRANCH_DEACTIVATE:
185 flag.setEnabled(isBranchSelected());
188 case LFUN_INSET_TOGGLE:
189 if (cmd.argument() == "assign")
190 flag.setEnabled(true);
192 return InsetCollapsable::getStatus(cur, cmd, flag);
195 return InsetCollapsable::getStatus(cur, cmd, flag);
201 bool InsetBranch::isBranchSelected() const
203 Buffer const & realbuffer = *buffer().masterBuffer();
204 BranchList const & branchlist = realbuffer.params().branchlist();
205 Branch const * ourBranch = branchlist.find(params_.branch);
208 // this branch is defined in child only
209 ourBranch = buffer().params().branchlist().find(params_.branch);
213 return ourBranch->isSelected();
217 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
219 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
223 int InsetBranch::plaintext(odocstream & os,
224 OutputParams const & runparams) const
226 if (!isBranchSelected())
229 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
230 InsetText::plaintext(os, runparams);
233 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
237 int InsetBranch::docbook(odocstream & os,
238 OutputParams const & runparams) const
240 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
244 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
246 if (isBranchSelected())
247 return InsetText::xhtml(xs, rp);
252 void InsetBranch::tocString(odocstream & os) const
254 if (isBranchSelected())
255 InsetCollapsable::tocString(os);
259 void InsetBranch::validate(LaTeXFeatures & features) const
261 if (isBranchSelected())
262 InsetCollapsable::validate(features);
266 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
268 return from_ascii("context-branch");
272 bool InsetBranch::isMacroScope() const
274 // Its own scope if not selected by buffer
275 return !isBranchSelected();
279 string InsetBranch::params2string(InsetBranchParams const & params)
282 data << "branch" << ' ';
288 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
290 params = InsetBranchParams();
294 istringstream data(in);
297 lex.setContext("InsetBranch::string2params");
298 lex >> "branch" >> "Branch";
303 void InsetBranch::addToToc(DocIterator const & cpit)
305 DocIterator pit = cpit;
306 pit.push_back(CursorSlice(*this));
308 Toc & toc = buffer().tocBackend().toc("branch");
309 docstring const str = params_.branch + ": " + text().getPar(0).asString();
310 toc.push_back(TocItem(pit, 0, str));
311 // Proceed with the rest of the inset.
312 InsetCollapsable::addToToc(cpit);
316 void InsetBranchParams::write(ostream & os) const
318 os << "Branch " << to_utf8(branch) << '\n';
322 void InsetBranchParams::read(Lexer & lex)
325 branch = lex.getDocString();