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, InsetText::DefaultLayout), 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 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
81 docstring s = _("Branch: ") + params_.branch;
82 Buffer const & realbuffer = *buffer().masterBuffer();
83 BranchList const & branchlist = realbuffer.params().branchlist();
84 if (!branchlist.find(params_.branch))
85 s = _("Branch (child only): ") + params_.branch;
86 if (!params_.branch.empty()) {
88 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
92 s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
93 if (decoration() == InsetLayout::CLASSIC)
94 return isOpen(bv) ? s : getNewLabel(s);
96 return 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_BRANCH_ACTIVATE:
143 case LFUN_BRANCH_DEACTIVATE: {
144 // FIXME: I do not like this cast, but have no other idea...
145 Buffer * realbuffer = const_cast<Buffer *>(buffer().masterBuffer());
146 BranchList & branchlist = realbuffer->params().branchlist();
147 Branch * ourBranch = branchlist.find(params_.branch);
150 ourBranch->setSelected(cmd.action == LFUN_BRANCH_ACTIVATE);
154 case LFUN_INSET_TOGGLE:
155 if (cmd.argument() == "assign")
156 setStatus(cur, isBranchSelected() ? Open : 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 case LFUN_INSET_SETTINGS:
175 flag.setEnabled(true);
178 case LFUN_BRANCH_ACTIVATE:
179 flag.setEnabled(!isBranchSelected());
182 case LFUN_BRANCH_DEACTIVATE:
183 flag.setEnabled(isBranchSelected());
186 case LFUN_INSET_TOGGLE:
187 if (cmd.argument() == "assign")
188 flag.setEnabled(true);
190 return InsetCollapsable::getStatus(cur, cmd, flag);
193 return InsetCollapsable::getStatus(cur, cmd, flag);
199 bool InsetBranch::isBranchSelected() const
201 Buffer const & realbuffer = *buffer().masterBuffer();
202 BranchList const & branchlist = realbuffer.params().branchlist();
203 Branch const * ourBranch = branchlist.find(params_.branch);
206 return ourBranch->isSelected();
210 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
212 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
216 int InsetBranch::plaintext(odocstream & os,
217 OutputParams const & runparams) const
219 if (!isBranchSelected())
222 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
223 InsetText::plaintext(os, runparams);
226 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
230 int InsetBranch::docbook(odocstream & os,
231 OutputParams const & runparams) const
233 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
237 void InsetBranch::tocString(odocstream & os) const
239 if (isBranchSelected())
240 InsetCollapsable::tocString(os);
244 void InsetBranch::validate(LaTeXFeatures & features) const
246 if (isBranchSelected())
247 InsetCollapsable::validate(features);
251 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
253 return from_ascii("context-branch");
257 bool InsetBranch::isMacroScope() const
259 // Its own scope if not selected by buffer
260 return !isBranchSelected();
264 string InsetBranch::params2string(InsetBranchParams const & params)
267 data << "branch" << ' ';
273 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
275 params = InsetBranchParams();
279 istringstream data(in);
282 lex.setContext("InsetBranch::string2params");
283 lex >> "branch" >> "Branch";
288 void InsetBranch::addToToc(DocIterator const & cpit)
290 DocIterator pit = cpit;
291 pit.push_back(CursorSlice(*this));
293 Toc & toc = buffer().tocBackend().toc("branch");
294 docstring const str = params_.branch + ": " + text().getPar(0).asString();
295 toc.push_back(TocItem(pit, 0, str));
296 // Proceed with the rest of the inset.
297 InsetCollapsable::addToToc(cpit);
301 void InsetBranchParams::write(ostream & os) const
303 os << "Branch " << to_utf8(branch) << '\n';
307 void InsetBranchParams::read(Lexer & lex)
310 branch = lex.getDocString();