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));
85 if (c == Color_none) {
89 if (decoration() == InsetLayout::Classic)
90 setLabel(isOpen() ? s : getNewLabel(s) );
92 setLabel(params_.branch + ": " + getNewLabel(s));
96 ColorCode InsetBranch::backgroundColor() const
98 if (!params_.branch.empty()) {
100 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
101 if (c == Color_none) {
106 return Inset::backgroundColor();
110 bool InsetBranch::showInsetDialog(BufferView * bv) const
112 bv->showDialog("branch", params2string(params()),
113 const_cast<InsetBranch *>(this));
118 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
120 switch (cmd.action) {
121 case LFUN_INSET_MODIFY: {
122 InsetBranchParams params;
123 InsetBranch::string2params(to_utf8(cmd.argument()), params);
124 params_.branch = params.branch;
125 setLayout(cur.buffer().params());
129 case LFUN_MOUSE_PRESS:
130 if (cmd.button() != mouse_button::button3)
131 InsetCollapsable::doDispatch(cur, cmd);
136 case LFUN_INSET_DIALOG_UPDATE:
137 cur.bv().updateDialog("branch", params2string(params()));
140 case LFUN_INSET_TOGGLE:
141 if (cmd.argument() == "assign") {
142 // The branch inset uses "assign".
143 if (isBranchSelected()) {
144 if (status() != Open)
145 setStatus(cur, Open);
149 if (status() != Collapsed)
150 setStatus(cur, Collapsed);
156 InsetCollapsable::doDispatch(cur, cmd);
160 InsetCollapsable::doDispatch(cur, cmd);
166 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
167 FuncStatus & flag) const
169 switch (cmd.action) {
170 case LFUN_INSET_MODIFY:
171 case LFUN_INSET_DIALOG_UPDATE:
172 flag.setEnabled(true);
175 case LFUN_INSET_TOGGLE:
176 if (cmd.argument() == "open" || cmd.argument() == "close" ||
177 cmd.argument() == "toggle")
178 flag.setEnabled(true);
179 else if (cmd.argument() == "assign" || cmd.argument().empty()) {
180 if (isBranchSelected())
181 flag.setEnabled(status() != Open);
183 flag.setEnabled(status() != Collapsed);
185 flag.setEnabled(true);
189 return InsetCollapsable::getStatus(cur, cmd, flag);
195 bool InsetBranch::isBranchSelected() const
197 Buffer const & realbuffer = *buffer().masterBuffer();
198 BranchList const & branchlist = realbuffer.params().branchlist();
199 Branch const * ourBranch = branchlist.find(params_.branch);
202 return ourBranch->getSelected();
206 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
208 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
212 int InsetBranch::plaintext(odocstream & os,
213 OutputParams const & runparams) const
215 if (!isBranchSelected())
218 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
219 InsetText::plaintext(os, runparams);
222 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
226 int InsetBranch::docbook(odocstream & os,
227 OutputParams const & runparams) const
229 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
233 void InsetBranch::textString(odocstream & os) const
235 if (isBranchSelected())
236 os << paragraphs().begin()->asString(AS_STR_LABEL | AS_STR_INSETS);
240 void InsetBranch::validate(LaTeXFeatures & features) const
242 InsetText::validate(features);
246 bool InsetBranch::isMacroScope() const
248 // Its own scope if not selected by buffer
249 return !isBranchSelected();
253 string InsetBranch::params2string(InsetBranchParams const & params)
256 data << "branch" << ' ';
262 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
264 params = InsetBranchParams();
268 istringstream data(in);
271 lex.setContext("InsetBranch::string2params");
272 lex >> "branch" >> "Branch";
277 void InsetBranch::addToToc(DocIterator const & cpit)
279 DocIterator pit = cpit;
280 pit.push_back(CursorSlice(*this));
282 Toc & toc = buffer().tocBackend().toc("branch");
283 docstring const str = params_.branch + ": " + text_.getPar(0).asString();
284 toc.push_back(TocItem(pit, 0, str));
288 void InsetBranchParams::write(ostream & os) const
290 os << "Branch " << to_utf8(branch) << '\n';
294 void InsetBranchParams::read(Lexer & lex)