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"
32 #include "support/lstrings.h"
34 #include "frontends/Application.h"
43 InsetBranch::InsetBranch(Buffer const & buf, InsetBranchParams const & params)
44 : InsetCollapsable(buf, InsetText::DefaultLayout), params_(params)
48 InsetBranch::~InsetBranch()
50 hideDialogs("branch", this);
54 docstring InsetBranch::editMessage() const
56 return _("Opened Branch Inset");
60 void InsetBranch::write(ostream & os) const
63 InsetCollapsable::write(os);
67 void InsetBranch::read(Lexer & lex)
70 InsetCollapsable::read(lex);
74 docstring InsetBranch::toolTip(BufferView const &, int, int) const
76 docstring const status = isBranchSelected() ?
77 _("active") : _("non-active");
78 return support::bformat(_("Branch (%1$s): %2$s"),
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 s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
98 if (decoration() == InsetLayout::CLASSIC)
99 return isOpen(bv) ? s : getNewLabel(s);
101 return params_.branch + ": " + getNewLabel(s);
105 ColorCode InsetBranch::backgroundColor() const
107 if (params_.branch.empty())
108 return Inset::backgroundColor();
110 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
117 bool InsetBranch::showInsetDialog(BufferView * bv) const
119 bv->showDialog("branch", params2string(params()),
120 const_cast<InsetBranch *>(this));
125 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
127 switch (cmd.action) {
128 case LFUN_INSET_MODIFY: {
129 InsetBranchParams params;
130 InsetBranch::string2params(to_utf8(cmd.argument()), params);
131 params_.branch = params.branch;
132 setLayout(cur.buffer()->params());
136 case LFUN_MOUSE_PRESS:
137 if (cmd.button() != mouse_button::button3)
138 InsetCollapsable::doDispatch(cur, cmd);
143 case LFUN_INSET_DIALOG_UPDATE:
144 cur.bv().updateDialog("branch", params2string(params()));
147 case LFUN_BRANCH_ACTIVATE:
148 case LFUN_BRANCH_DEACTIVATE: {
149 // FIXME: I do not like this cast, but have no other idea...
150 Buffer * realbuffer = const_cast<Buffer *>(buffer().masterBuffer());
151 BranchList & branchlist = realbuffer->params().branchlist();
152 Branch * ourBranch = branchlist.find(params_.branch);
155 ourBranch->setSelected(cmd.action == LFUN_BRANCH_ACTIVATE);
159 case LFUN_INSET_TOGGLE:
160 if (cmd.argument() == "assign")
161 setStatus(cur, isBranchSelected() ? Open : Collapsed);
163 InsetCollapsable::doDispatch(cur, cmd);
167 InsetCollapsable::doDispatch(cur, cmd);
173 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
174 FuncStatus & flag) const
176 switch (cmd.action) {
177 case LFUN_INSET_MODIFY:
178 case LFUN_INSET_DIALOG_UPDATE:
179 flag.setEnabled(true);
182 case LFUN_BRANCH_ACTIVATE:
183 flag.setEnabled(!isBranchSelected());
186 case LFUN_BRANCH_DEACTIVATE:
187 flag.setEnabled(isBranchSelected());
190 case LFUN_INSET_TOGGLE:
191 if (cmd.argument() == "assign")
192 flag.setEnabled(true);
194 return InsetCollapsable::getStatus(cur, cmd, flag);
197 return InsetCollapsable::getStatus(cur, cmd, flag);
203 bool InsetBranch::isBranchSelected() const
205 Buffer const & realbuffer = *buffer().masterBuffer();
206 BranchList const & branchlist = realbuffer.params().branchlist();
207 Branch const * ourBranch = branchlist.find(params_.branch);
210 return ourBranch->isSelected();
214 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
216 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
220 int InsetBranch::plaintext(odocstream & os,
221 OutputParams const & runparams) const
223 if (!isBranchSelected())
226 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
227 InsetText::plaintext(os, runparams);
230 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
234 int InsetBranch::docbook(odocstream & os,
235 OutputParams const & runparams) const
237 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
241 void InsetBranch::tocString(odocstream & os) const
243 if (isBranchSelected())
244 InsetCollapsable::tocString(os);
248 void InsetBranch::validate(LaTeXFeatures & features) const
250 if (isBranchSelected())
251 InsetCollapsable::validate(features);
255 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
257 return from_ascii("context-branch");
261 bool InsetBranch::isMacroScope() const
263 // Its own scope if not selected by buffer
264 return !isBranchSelected();
268 string InsetBranch::params2string(InsetBranchParams const & params)
271 data << "branch" << ' ';
277 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
279 params = InsetBranchParams();
283 istringstream data(in);
286 lex.setContext("InsetBranch::string2params");
287 lex >> "branch" >> "Branch";
292 void InsetBranch::addToToc(DocIterator const & cpit)
294 DocIterator pit = cpit;
295 pit.push_back(CursorSlice(*this));
297 Toc & toc = buffer().tocBackend().toc("branch");
298 docstring const str = params_.branch + ": " + text().getPar(0).asString();
299 toc.push_back(TocItem(pit, 0, str));
300 // Proceed with the rest of the inset.
301 InsetCollapsable::addToToc(cpit);
305 void InsetBranchParams::write(ostream & os) const
307 os << "Branch " << to_utf8(branch) << '\n';
311 void InsetBranchParams::read(Lexer & lex)
314 branch = lex.getDocString();