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 docstring InsetBranch::editMessage() const
56 return _("Opened Branch Inset");
60 void InsetBranch::write(ostream & os) const
63 InsetCollapsable::write(os);
67 void InsetBranch::read(Lexer & lex)
70 InsetCollapsable::read(lex);
74 docstring InsetBranch::toolTip(BufferView const & bv, int x, int y) const
76 docstring const status = isBranchSelected() ?
77 _("active") : _("non-active");
78 docstring const heading =
79 support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
80 docstring const contents = InsetCollapsable::toolTip(bv, x, y);
81 if (isOpen(bv) || contents.empty())
84 return heading + from_ascii("\n") + contents;
88 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
90 docstring s = _("Branch: ") + params_.branch;
91 Buffer const & realbuffer = *buffer().masterBuffer();
92 BranchList const & branchlist = realbuffer.params().branchlist();
93 if (!branchlist.find(params_.branch)
94 && buffer().params().branchlist().find(params_.branch))
95 s = _("Branch (child only): ") + params_.branch;
96 else if (!branchlist.find(params_.branch))
97 s = _("Branch (undefined): ") + params_.branch;
98 if (!params_.branch.empty()) {
100 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
102 s = _("Undef: ") + s;
104 s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
105 if (decoration() == InsetLayout::CLASSIC)
106 return isOpen(bv) ? s : getNewLabel(s);
108 return params_.branch + ": " + getNewLabel(s);
112 ColorCode InsetBranch::backgroundColor() const
114 if (params_.branch.empty())
115 return Inset::backgroundColor();
117 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
124 bool InsetBranch::showInsetDialog(BufferView * bv) const
126 bv->showDialog("branch", params2string(params()),
127 const_cast<InsetBranch *>(this));
132 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
134 switch (cmd.action) {
135 case LFUN_INSET_MODIFY: {
136 InsetBranchParams params;
137 InsetBranch::string2params(to_utf8(cmd.argument()), params);
138 params_.branch = params.branch;
139 setLayout(cur.buffer()->params());
143 case LFUN_INSET_DIALOG_UPDATE:
144 cur.bv().updateDialog("branch", params2string(params()));
147 case LFUN_BRANCH_ACTIVATE:
148 case LFUN_BRANCH_DEACTIVATE: {
149 // FIXME: I do not like this cast, but have no other idea...
150 Buffer const * buf = buffer().masterBuffer();
151 BranchList const & branchlist = buf->params().branchlist();
152 Branch * our_branch = const_cast<Branch *>(branchlist.find(params_.branch));
155 our_branch = buffer().params().branchlist().find(params_.branch);
159 our_branch->setSelected(cmd.action == LFUN_BRANCH_ACTIVATE);
163 case LFUN_INSET_TOGGLE:
164 if (cmd.argument() == "assign")
165 setStatus(cur, isBranchSelected() ? Open : Collapsed);
167 InsetCollapsable::doDispatch(cur, cmd);
171 InsetCollapsable::doDispatch(cur, cmd);
177 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
178 FuncStatus & flag) const
180 switch (cmd.action) {
181 case LFUN_INSET_MODIFY:
182 case LFUN_INSET_DIALOG_UPDATE:
183 flag.setEnabled(true);
186 case LFUN_BRANCH_ACTIVATE:
187 flag.setEnabled(!isBranchSelected());
190 case LFUN_BRANCH_DEACTIVATE:
191 flag.setEnabled(isBranchSelected());
194 case LFUN_INSET_TOGGLE:
195 if (cmd.argument() == "assign")
196 flag.setEnabled(true);
198 return InsetCollapsable::getStatus(cur, cmd, flag);
201 return InsetCollapsable::getStatus(cur, cmd, flag);
207 bool InsetBranch::isBranchSelected() const
209 Buffer const & realbuffer = *buffer().masterBuffer();
210 BranchList const & branchlist = realbuffer.params().branchlist();
211 Branch const * ourBranch = branchlist.find(params_.branch);
214 // this branch is defined in child only
215 ourBranch = buffer().params().branchlist().find(params_.branch);
219 return ourBranch->isSelected();
223 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
225 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
229 int InsetBranch::plaintext(odocstream & os,
230 OutputParams const & runparams) const
232 if (!isBranchSelected())
235 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
236 InsetText::plaintext(os, runparams);
239 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
243 int InsetBranch::docbook(odocstream & os,
244 OutputParams const & runparams) const
246 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
250 docstring InsetBranch::xhtml(odocstream & os, OutputParams const & rp) const
252 if (isBranchSelected())
253 return InsetText::xhtml(os, rp);
258 void InsetBranch::tocString(odocstream & os) const
260 if (isBranchSelected())
261 InsetCollapsable::tocString(os);
265 void InsetBranch::validate(LaTeXFeatures & features) const
267 if (isBranchSelected())
268 InsetCollapsable::validate(features);
272 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
274 return from_ascii("context-branch");
278 bool InsetBranch::isMacroScope() const
280 // Its own scope if not selected by buffer
281 return !isBranchSelected();
285 string InsetBranch::params2string(InsetBranchParams const & params)
288 data << "branch" << ' ';
294 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
296 params = InsetBranchParams();
300 istringstream data(in);
303 lex.setContext("InsetBranch::string2params");
304 lex >> "branch" >> "Branch";
309 void InsetBranch::addToToc(DocIterator const & cpit)
311 DocIterator pit = cpit;
312 pit.push_back(CursorSlice(*this));
314 Toc & toc = buffer().tocBackend().toc("branch");
315 docstring const str = params_.branch + ": " + text().getPar(0).asString();
316 toc.push_back(TocItem(pit, 0, str));
317 // Proceed with the rest of the inset.
318 InsetCollapsable::addToToc(cpit);
322 void InsetBranchParams::write(ostream & os) const
324 os << "Branch " << to_utf8(branch) << '\n';
328 void InsetBranchParams::read(Lexer & lex)
331 branch = lex.getDocString();