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)
47 InsetBranch::~InsetBranch()
49 hideDialogs("branch", this);
53 docstring InsetBranch::editMessage() const
55 return _("Opened Branch Inset");
59 void InsetBranch::write(ostream & os) const
62 InsetCollapsable::write(os);
66 void InsetBranch::read(Lexer & lex)
69 InsetCollapsable::read(lex);
73 docstring InsetBranch::toolTip(BufferView const &, int, int) const
75 return _("Branch: ") + params_.branch;
79 void InsetBranch::setButtonLabel()
81 docstring s = _("Branch: ") + params_.branch;
82 if (!params_.branch.empty()) {
84 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
88 if (decoration() == InsetLayout::Classic)
89 setLabel(isOpen() ? s : getNewLabel(s) );
91 setLabel(params_.branch + ": " + getNewLabel(s));
95 ColorCode InsetBranch::backgroundColor() const
97 if (params_.branch.empty())
98 return Inset::backgroundColor();
100 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
107 bool InsetBranch::showInsetDialog(BufferView * bv) const
109 bv->showDialog("branch", params2string(params()),
110 const_cast<InsetBranch *>(this));
115 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
117 switch (cmd.action) {
118 case LFUN_INSET_MODIFY: {
119 InsetBranchParams params;
120 InsetBranch::string2params(to_utf8(cmd.argument()), params);
121 params_.branch = params.branch;
122 setLayout(cur.buffer().params());
126 case LFUN_MOUSE_PRESS:
127 if (cmd.button() != mouse_button::button3)
128 InsetCollapsable::doDispatch(cur, cmd);
133 case LFUN_INSET_DIALOG_UPDATE:
134 cur.bv().updateDialog("branch", params2string(params()));
137 case LFUN_INSET_TOGGLE:
138 if (cmd.argument() == "assign") {
139 // The branch inset uses "assign".
140 if (isBranchSelected()) {
141 if (status() != Open)
142 setStatus(cur, Open);
146 if (status() != Collapsed)
147 setStatus(cur, Collapsed);
153 InsetCollapsable::doDispatch(cur, cmd);
157 InsetCollapsable::doDispatch(cur, cmd);
163 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
164 FuncStatus & flag) const
166 switch (cmd.action) {
167 case LFUN_INSET_MODIFY:
168 case LFUN_INSET_DIALOG_UPDATE:
169 flag.setEnabled(true);
172 case LFUN_INSET_TOGGLE:
173 if (cmd.argument() == "open" || cmd.argument() == "close" ||
174 cmd.argument() == "toggle")
175 flag.setEnabled(true);
176 else if (cmd.argument() == "assign" || cmd.argument().empty()) {
177 if (isBranchSelected())
178 flag.setEnabled(status() != Open);
180 flag.setEnabled(status() != Collapsed);
182 flag.setEnabled(true);
186 return InsetCollapsable::getStatus(cur, cmd, flag);
192 bool InsetBranch::isBranchSelected() const
194 Buffer const & realbuffer = *buffer().masterBuffer();
195 BranchList const & branchlist = realbuffer.params().branchlist();
196 Branch const * ourBranch = branchlist.find(params_.branch);
199 return ourBranch->getSelected();
203 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
205 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
209 int InsetBranch::plaintext(odocstream & os,
210 OutputParams const & runparams) const
212 if (!isBranchSelected())
215 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
216 InsetText::plaintext(os, runparams);
219 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
223 int InsetBranch::docbook(odocstream & os,
224 OutputParams const & runparams) const
226 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
230 void InsetBranch::textString(odocstream & os) const
232 if (isBranchSelected())
233 os << paragraphs().begin()->asString(AS_STR_LABEL | AS_STR_INSETS);
237 void InsetBranch::validate(LaTeXFeatures & features) const
239 InsetText::validate(features);
243 bool InsetBranch::isMacroScope() const
245 // Its own scope if not selected by buffer
246 return !isBranchSelected();
250 string InsetBranch::params2string(InsetBranchParams const & params)
253 data << "branch" << ' ';
259 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
261 params = InsetBranchParams();
265 istringstream data(in);
268 lex.setContext("InsetBranch::string2params");
269 lex >> "branch" >> "Branch";
274 void InsetBranch::addToToc(DocIterator const & cpit)
276 DocIterator pit = cpit;
277 pit.push_back(CursorSlice(*this));
279 Toc & toc = buffer().tocBackend().toc("branch");
280 docstring const str = params_.branch + ": " + text().getPar(0).asString();
281 toc.push_back(TocItem(pit, 0, str));
282 // Proceed with the rest of the inset.
283 InsetCollapsable::addToToc(cpit);
287 void InsetBranchParams::write(ostream & os) const
289 os << "Branch " << to_utf8(branch) << '\n';
293 void InsetBranchParams::read(Lexer & lex)