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 "TextClass.h"
28 #include "TocBackend.h"
30 #include "support/debug.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
34 #include "frontends/Application.h"
43 InsetBranch::InsetBranch(Buffer const & buf, InsetBranchParams const & params)
44 : InsetCollapsable(buf, InsetText::DefaultLayout), params_(params)
48 InsetBranch::~InsetBranch()
50 hideDialogs("branch", this);
54 void InsetBranch::write(ostream & os) const
57 InsetCollapsable::write(os);
61 void InsetBranch::read(Lexer & lex)
64 InsetCollapsable::read(lex);
68 docstring InsetBranch::toolTip(BufferView const & bv, int x, int y) const
70 docstring const status = isBranchSelected() ?
71 _("active") : _("non-active");
72 docstring const heading =
73 support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
74 docstring const contents = InsetCollapsable::toolTip(bv, x, y);
75 if (isOpen(bv) || contents.empty())
78 return heading + from_ascii("\n") + contents;
82 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
84 docstring s = _("Branch: ") + params_.branch;
85 Buffer const & realbuffer = *buffer().masterBuffer();
86 BranchList const & branchlist = realbuffer.params().branchlist();
87 if (!branchlist.find(params_.branch)
88 && buffer().params().branchlist().find(params_.branch))
89 s = _("Branch (child only): ") + params_.branch;
90 else if (!branchlist.find(params_.branch))
91 s = _("Branch (undefined): ") + params_.branch;
92 if (!params_.branch.empty()) {
94 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
98 s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
99 if (decoration() == InsetLayout::CLASSIC)
100 return isOpen(bv) ? s : getNewLabel(s);
102 return params_.branch + ": " + getNewLabel(s);
106 ColorCode InsetBranch::backgroundColor() const
108 if (params_.branch.empty())
109 return Inset::backgroundColor();
111 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
118 bool InsetBranch::showInsetDialog(BufferView * bv) const
120 bv->showDialog("branch", params2string(params()),
121 const_cast<InsetBranch *>(this));
126 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
128 switch (cmd.action) {
129 case LFUN_INSET_MODIFY: {
130 InsetBranchParams params;
131 InsetBranch::string2params(to_utf8(cmd.argument()), params);
132 params_.branch = params.branch;
136 case LFUN_INSET_DIALOG_UPDATE:
137 cur.bv().updateDialog("branch", params2string(params()));
140 case LFUN_BRANCH_ACTIVATE:
141 case LFUN_BRANCH_DEACTIVATE: {
142 // FIXME: I do not like this cast, but have no other idea...
143 Buffer const * buf = buffer().masterBuffer();
144 BranchList const & branchlist = buf->params().branchlist();
145 Branch * our_branch = const_cast<Branch *>(branchlist.find(params_.branch));
148 our_branch = buffer().params().branchlist().find(params_.branch);
152 our_branch->setSelected(cmd.action == LFUN_BRANCH_ACTIVATE);
156 case LFUN_INSET_TOGGLE:
157 if (cmd.argument() == "assign")
158 setStatus(cur, isBranchSelected() ? Open : Collapsed);
160 InsetCollapsable::doDispatch(cur, cmd);
164 InsetCollapsable::doDispatch(cur, cmd);
170 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
171 FuncStatus & flag) const
173 switch (cmd.action) {
174 case LFUN_INSET_MODIFY:
175 case LFUN_INSET_DIALOG_UPDATE:
176 flag.setEnabled(true);
179 case LFUN_BRANCH_ACTIVATE:
180 flag.setEnabled(!isBranchSelected());
183 case LFUN_BRANCH_DEACTIVATE:
184 flag.setEnabled(isBranchSelected());
187 case LFUN_INSET_TOGGLE:
188 if (cmd.argument() == "assign")
189 flag.setEnabled(true);
191 return InsetCollapsable::getStatus(cur, cmd, flag);
194 return InsetCollapsable::getStatus(cur, cmd, flag);
200 bool InsetBranch::isBranchSelected() const
202 Buffer const & realbuffer = *buffer().masterBuffer();
203 BranchList const & branchlist = realbuffer.params().branchlist();
204 Branch const * ourBranch = branchlist.find(params_.branch);
207 // this branch is defined in child only
208 ourBranch = buffer().params().branchlist().find(params_.branch);
212 return ourBranch->isSelected();
216 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
218 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
222 int InsetBranch::plaintext(odocstream & os,
223 OutputParams const & runparams) const
225 if (!isBranchSelected())
228 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
229 InsetText::plaintext(os, runparams);
232 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
236 int InsetBranch::docbook(odocstream & os,
237 OutputParams const & runparams) const
239 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
243 docstring InsetBranch::xhtml(odocstream & os, OutputParams const & rp) const
245 if (isBranchSelected())
246 return InsetText::xhtml(os, rp);
251 void InsetBranch::tocString(odocstream & os) const
253 if (isBranchSelected())
254 InsetCollapsable::tocString(os);
258 void InsetBranch::validate(LaTeXFeatures & features) const
260 if (isBranchSelected())
261 InsetCollapsable::validate(features);
265 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
267 return from_ascii("context-branch");
271 bool InsetBranch::isMacroScope() const
273 // Its own scope if not selected by buffer
274 return !isBranchSelected();
278 string InsetBranch::params2string(InsetBranchParams const & params)
281 data << "branch" << ' ';
287 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
289 params = InsetBranchParams();
293 istringstream data(in);
296 lex.setContext("InsetBranch::string2params");
297 lex >> "branch" >> "Branch";
302 void InsetBranch::addToToc(DocIterator const & cpit)
304 DocIterator pit = cpit;
305 pit.push_back(CursorSlice(*this));
307 Toc & toc = buffer().tocBackend().toc("branch");
308 docstring const str = params_.branch + ": " + text().getPar(0).asString();
309 toc.push_back(TocItem(pit, 0, str));
310 // Proceed with the rest of the inset.
311 InsetCollapsable::addToToc(cpit);
315 void InsetBranchParams::write(ostream & os) const
317 os << "Branch " << to_utf8(branch) << '\n';
321 void InsetBranchParams::read(Lexer & lex)
324 branch = lex.getDocString();