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 s = _("Branch (child only): ") + params_.branch;
95 if (!params_.branch.empty()) {
97 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
101 s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
102 if (decoration() == InsetLayout::CLASSIC)
103 return isOpen(bv) ? s : getNewLabel(s);
105 return params_.branch + ": " + getNewLabel(s);
109 ColorCode InsetBranch::backgroundColor() const
111 if (params_.branch.empty())
112 return Inset::backgroundColor();
114 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
121 bool InsetBranch::showInsetDialog(BufferView * bv) const
123 bv->showDialog("branch", params2string(params()),
124 const_cast<InsetBranch *>(this));
129 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
131 switch (cmd.action) {
132 case LFUN_INSET_MODIFY: {
133 InsetBranchParams params;
134 InsetBranch::string2params(to_utf8(cmd.argument()), params);
135 params_.branch = params.branch;
136 setLayout(cur.buffer()->params());
140 case LFUN_INSET_DIALOG_UPDATE:
141 cur.bv().updateDialog("branch", params2string(params()));
144 case LFUN_BRANCH_ACTIVATE:
145 case LFUN_BRANCH_DEACTIVATE: {
146 // FIXME: I do not like this cast, but have no other idea...
147 Buffer const * buf = buffer().masterBuffer();
148 BranchList const & branchlist = buf->params().branchlist();
149 Branch * our_branch = const_cast<Branch *>(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 return ourBranch->isSelected();
211 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
213 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
217 int InsetBranch::plaintext(odocstream & os,
218 OutputParams const & runparams) const
220 if (!isBranchSelected())
223 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
224 InsetText::plaintext(os, runparams);
227 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
231 int InsetBranch::docbook(odocstream & os,
232 OutputParams const & runparams) const
234 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
238 int InsetBranch::xhtml(odocstream & os,
239 OutputParams const & runparams) const
241 return isBranchSelected() ? InsetText::xhtml(os, runparams) : 0;
245 void InsetBranch::tocString(odocstream & os) const
247 if (isBranchSelected())
248 InsetCollapsable::tocString(os);
252 void InsetBranch::validate(LaTeXFeatures & features) const
254 if (isBranchSelected())
255 InsetCollapsable::validate(features);
259 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
261 return from_ascii("context-branch");
265 bool InsetBranch::isMacroScope() const
267 // Its own scope if not selected by buffer
268 return !isBranchSelected();
272 string InsetBranch::params2string(InsetBranchParams const & params)
275 data << "branch" << ' ';
281 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
283 params = InsetBranchParams();
287 istringstream data(in);
290 lex.setContext("InsetBranch::string2params");
291 lex >> "branch" >> "Branch";
296 void InsetBranch::addToToc(DocIterator const & cpit)
298 DocIterator pit = cpit;
299 pit.push_back(CursorSlice(*this));
301 Toc & toc = buffer().tocBackend().toc("branch");
302 docstring const str = params_.branch + ": " + text().getPar(0).asString();
303 toc.push_back(TocItem(pit, 0, str));
304 // Proceed with the rest of the inset.
305 InsetCollapsable::addToToc(cpit);
309 void InsetBranchParams::write(ostream & os) const
311 os << "Branch " << to_utf8(branch) << '\n';
315 void InsetBranchParams::read(Lexer & lex)
318 branch = lex.getDocString();