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"
33 #include "frontends/Application.h"
42 InsetBranch::InsetBranch(Buffer const & buf, InsetBranchParams const & params)
43 : InsetCollapsable(buf), params_(params)
45 // override the default for InsetCollapsable, which is to
46 // use the plain layout.
47 DocumentClass const & dc = buf.params().documentClass();
48 paragraphs().back().setDefaultLayout(dc);
52 InsetBranch::~InsetBranch()
54 hideDialogs("branch", this);
58 docstring InsetBranch::editMessage() const
60 return _("Opened Branch Inset");
64 void InsetBranch::write(ostream & os) const
67 InsetCollapsable::write(os);
71 void InsetBranch::read(Lexer & lex)
74 InsetCollapsable::read(lex);
78 docstring InsetBranch::toolTip(BufferView const &, int, int) const
80 return _("Branch: ") + params_.branch;
84 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
86 docstring s = _("Branch: ") + params_.branch;
87 Buffer const & realbuffer = *buffer().masterBuffer();
88 BranchList const & branchlist = realbuffer.params().branchlist();
89 if (!branchlist.find(params_.branch))
90 s = _("Branch (child only): ") + params_.branch;
91 if (!params_.branch.empty()) {
93 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
97 if (decoration() == InsetLayout::CLASSIC)
98 return isOpen(bv) ? s : getNewLabel(s);
100 return params_.branch + ": " + getNewLabel(s);
104 ColorCode InsetBranch::backgroundColor() const
106 if (params_.branch.empty())
107 return Inset::backgroundColor();
109 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
116 bool InsetBranch::showInsetDialog(BufferView * bv) const
118 bv->showDialog("branch", params2string(params()),
119 const_cast<InsetBranch *>(this));
124 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
126 switch (cmd.action) {
127 case LFUN_INSET_MODIFY: {
128 InsetBranchParams params;
129 InsetBranch::string2params(to_utf8(cmd.argument()), params);
130 params_.branch = params.branch;
131 setLayout(cur.buffer()->params());
135 case LFUN_MOUSE_PRESS:
136 if (cmd.button() != mouse_button::button3)
137 InsetCollapsable::doDispatch(cur, cmd);
142 case LFUN_INSET_DIALOG_UPDATE:
143 cur.bv().updateDialog("branch", params2string(params()));
146 case LFUN_BRANCH_ACTIVATE:
147 case LFUN_BRANCH_DEACTIVATE: {
148 // FIXME: I do not like this cast, but have no other idea...
149 Buffer * realbuffer = const_cast<Buffer *>(buffer().masterBuffer());
150 BranchList & branchlist = realbuffer->params().branchlist();
151 Branch * ourBranch = branchlist.find(params_.branch);
154 ourBranch->setSelected(cmd.action == LFUN_BRANCH_ACTIVATE);
158 case LFUN_INSET_TOGGLE:
159 if (cmd.argument() == "assign")
160 setStatus(cur, isBranchSelected() ? Open : Collapsed);
162 InsetCollapsable::doDispatch(cur, cmd);
166 InsetCollapsable::doDispatch(cur, cmd);
172 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
173 FuncStatus & flag) const
175 switch (cmd.action) {
176 case LFUN_INSET_MODIFY:
177 case LFUN_INSET_DIALOG_UPDATE:
178 flag.setEnabled(true);
181 case LFUN_BRANCH_ACTIVATE:
182 flag.setEnabled(!isBranchSelected());
185 case LFUN_BRANCH_DEACTIVATE:
186 flag.setEnabled(isBranchSelected());
189 case LFUN_INSET_TOGGLE:
190 if (cmd.argument() == "assign")
191 flag.setEnabled(true);
193 return InsetCollapsable::getStatus(cur, cmd, flag);
196 return InsetCollapsable::getStatus(cur, cmd, flag);
202 bool InsetBranch::isBranchSelected() const
204 Buffer const & realbuffer = *buffer().masterBuffer();
205 BranchList const & branchlist = realbuffer.params().branchlist();
206 Branch const * ourBranch = branchlist.find(params_.branch);
209 return ourBranch->isSelected();
213 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
215 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
219 int InsetBranch::plaintext(odocstream & os,
220 OutputParams const & runparams) const
222 if (!isBranchSelected())
225 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
226 InsetText::plaintext(os, runparams);
229 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
233 int InsetBranch::docbook(odocstream & os,
234 OutputParams const & runparams) const
236 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
240 void InsetBranch::tocString(odocstream & os) const
242 if (isBranchSelected())
243 InsetCollapsable::tocString(os);
247 void InsetBranch::validate(LaTeXFeatures & features) const
249 if (isBranchSelected())
250 InsetCollapsable::validate(features);
254 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
256 return from_ascii("context-branch");
260 bool InsetBranch::isMacroScope() const
262 // Its own scope if not selected by buffer
263 return !isBranchSelected();
267 string InsetBranch::params2string(InsetBranchParams const & params)
270 data << "branch" << ' ';
276 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
278 params = InsetBranchParams();
282 istringstream data(in);
285 lex.setContext("InsetBranch::string2params");
286 lex >> "branch" >> "Branch";
291 void InsetBranch::addToToc(DocIterator const & cpit)
293 DocIterator pit = cpit;
294 pit.push_back(CursorSlice(*this));
296 Toc & toc = buffer().tocBackend().toc("branch");
297 docstring const str = params_.branch + ": " + text().getPar(0).asString();
298 toc.push_back(TocItem(pit, 0, str));
299 // Proceed with the rest of the inset.
300 InsetCollapsable::addToToc(cpit);
304 void InsetBranchParams::write(ostream & os) const
306 os << "Branch " << to_utf8(branch) << '\n';
310 void InsetBranchParams::read(Lexer & lex)
313 branch = lex.getDocString();