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 void InsetBranch::setButtonLabel(BufferView const & bv)
86 docstring s = _("Branch: ") + params_.branch;
87 if (!params_.branch.empty()) {
89 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
93 if (decoration() == InsetLayout::CLASSIC)
94 setLabel(isOpen(bv) ? s : getNewLabel(s) );
96 setLabel(params_.branch + ": " + getNewLabel(s));
100 ColorCode InsetBranch::backgroundColor() const
102 if (params_.branch.empty())
103 return Inset::backgroundColor();
105 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
112 bool InsetBranch::showInsetDialog(BufferView * bv) const
114 bv->showDialog("branch", params2string(params()),
115 const_cast<InsetBranch *>(this));
120 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
122 switch (cmd.action) {
123 case LFUN_INSET_MODIFY: {
124 InsetBranchParams params;
125 InsetBranch::string2params(to_utf8(cmd.argument()), params);
126 params_.branch = params.branch;
127 setLayout(cur.buffer()->params());
131 case LFUN_MOUSE_PRESS:
132 if (cmd.button() != mouse_button::button3)
133 InsetCollapsable::doDispatch(cur, cmd);
138 case LFUN_INSET_DIALOG_UPDATE:
139 cur.bv().updateDialog("branch", params2string(params()));
142 case LFUN_INSET_TOGGLE:
143 if (cmd.argument() == "assign") {
144 // The branch inset uses "assign".
145 if (isBranchSelected()) {
146 if (status(cur.bv()) != Open)
147 setStatus(cur, Open);
151 if (status(cur.bv()) != Collapsed)
152 setStatus(cur, Collapsed);
158 InsetCollapsable::doDispatch(cur, cmd);
162 InsetCollapsable::doDispatch(cur, cmd);
168 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
169 FuncStatus & flag) const
171 switch (cmd.action) {
172 case LFUN_INSET_MODIFY:
173 case LFUN_INSET_DIALOG_UPDATE:
174 flag.setEnabled(true);
177 case LFUN_INSET_TOGGLE:
178 if (cmd.argument() == "open" || cmd.argument() == "close" ||
179 cmd.argument() == "toggle")
180 flag.setEnabled(true);
181 else if (cmd.argument() == "assign" || cmd.argument().empty()) {
182 if (isBranchSelected())
183 flag.setEnabled(status(cur.bv()) != Open);
185 flag.setEnabled(status(cur.bv()) != Collapsed);
187 flag.setEnabled(true);
191 return InsetCollapsable::getStatus(cur, cmd, flag);
197 bool InsetBranch::isBranchSelected() const
199 Buffer const & realbuffer = *buffer().masterBuffer();
200 BranchList const & branchlist = realbuffer.params().branchlist();
201 Branch const * ourBranch = branchlist.find(params_.branch);
204 return ourBranch->isSelected();
208 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
210 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
214 int InsetBranch::plaintext(odocstream & os,
215 OutputParams const & runparams) const
217 if (!isBranchSelected())
220 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
221 InsetText::plaintext(os, runparams);
224 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
228 int InsetBranch::docbook(odocstream & os,
229 OutputParams const & runparams) const
231 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
235 void InsetBranch::tocString(odocstream & os) const
237 if (isBranchSelected())
238 InsetCollapsable::tocString(os);
242 void InsetBranch::validate(LaTeXFeatures & features) const
244 if (isBranchSelected())
245 InsetCollapsable::validate(features);
249 bool InsetBranch::isMacroScope() const
251 // Its own scope if not selected by buffer
252 return !isBranchSelected();
256 string InsetBranch::params2string(InsetBranchParams const & params)
259 data << "branch" << ' ';
265 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
267 params = InsetBranchParams();
271 istringstream data(in);
274 lex.setContext("InsetBranch::string2params");
275 lex >> "branch" >> "Branch";
280 void InsetBranch::addToToc(DocIterator const & cpit)
282 DocIterator pit = cpit;
283 pit.push_back(CursorSlice(*this));
285 Toc & toc = buffer().tocBackend().toc("branch");
286 docstring const str = params_.branch + ": " + text().getPar(0).asString();
287 toc.push_back(TocItem(pit, 0, str));
288 // Proceed with the rest of the inset.
289 InsetCollapsable::addToToc(cpit);
293 void InsetBranchParams::write(ostream & os) const
295 os << "Branch " << to_utf8(branch) << '\n';
299 void InsetBranchParams::read(Lexer & lex)