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"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
26 #include "output_docbook.h"
27 #include "output_xhtml.h"
28 #include "TextClass.h"
29 #include "TocBackend.h"
31 #include "support/debug.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
35 #include "frontends/alert.h"
36 #include "frontends/Application.h"
45 InsetBranch::InsetBranch(Buffer * buf, InsetBranchParams const & params)
46 : InsetCollapsible(buf, InsetText::DefaultLayout), params_(params)
50 void InsetBranch::write(ostream & os) const
55 InsetCollapsible::write(os);
59 void InsetBranch::read(Lexer & lex)
62 InsetCollapsible::read(lex);
66 docstring InsetBranch::toolTip(BufferView const & bv, int, int) const
68 docstring const masterstatus = isBranchSelected() ?
69 _("active") : _("non-active");
70 docstring const childstatus = isBranchSelected(true) ?
71 _("active") : _("non-active");
72 docstring const status = (masterstatus == childstatus) ?
74 support::bformat(_("master %1$s, child %2$s"),
75 masterstatus, childstatus);
77 docstring const masteron = producesOutput() ?
79 docstring const childon =
80 (isBranchSelected(true) != params_.inverted) ?
82 docstring const onoff = (masteron == childon) ?
84 support::bformat(_("master %1$s, child %2$s"),
87 docstring const heading =
88 support::bformat(_("Branch Name: %1$s\nBranch Status: %2$s\nInset Status: %3$s"),
89 params_.branch, status, onoff);
93 return toolTipText(heading + from_ascii("\n"));
97 docstring const InsetBranch::buttonLabel(BufferView const &) const
99 static char_type const tick = 0x2714; // ✔ U+2714 HEAVY CHECK MARK
100 static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
102 Buffer const & realbuffer = *buffer().masterBuffer();
103 BranchList const & branchlist = realbuffer.params().branchlist();
104 bool const inmaster = branchlist.find(params_.branch);
105 bool const inchild = buffer().params().branchlist().find(params_.branch);
107 bool const master_selected = producesOutput();
108 bool const child_selected = isBranchSelected(true) != params_.inverted;
110 docstring symb = docstring(1, master_selected ? tick : cross);
111 if (inchild && master_selected != child_selected)
112 symb += (child_selected ? tick : cross);
114 if (decoration() == InsetDecoration::MINIMALISTIC)
115 return symb + params_.branch;
118 if (inmaster && inchild)
120 else if (inchild) // && !inmaster
121 s = _("Branch (child): ");
122 else if (inmaster) // && !inchild
123 s = _("Branch (master): ");
124 else // !inmaster && !inchild
125 s = _("Branch (undefined): ");
132 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
134 if (params_.branch.empty())
135 return Inset::backgroundColor(pi);
137 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
144 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
146 switch (cmd.action()) {
147 case LFUN_INSET_MODIFY: {
148 InsetBranchParams params;
149 InsetBranch::string2params(to_utf8(cmd.argument()), params);
151 cur.recordUndoInset(this);
152 params_.branch = params.branch;
153 params_.inverted = params.inverted;
154 // what we really want here is a TOC update, but that means
155 // a full buffer update
156 cur.forceBufferUpdate();
159 case LFUN_BRANCH_ACTIVATE:
160 case LFUN_BRANCH_DEACTIVATE:
161 case LFUN_BRANCH_MASTER_ACTIVATE:
162 case LFUN_BRANCH_MASTER_DEACTIVATE: {
163 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
164 || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
165 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
168 Branch * our_branch = buf->params().branchlist().find(params_.branch);
172 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
173 || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
174 if (our_branch->isSelected() != activate) {
175 // FIXME If the branch is in the master document, we cannot
176 // call recordUndo..., because the master may be hidden, and
177 // the code presently assumes that hidden documents can never
178 // be dirty. See GuiView::closeBufferAll(), for example.
179 // An option would be to check if the master is hidden.
182 buffer().undo().recordUndoBufferParams(cur);
184 // at least issue a warning for now (ugly, but better than dataloss).
185 frontend::Alert::warning(_("Branch state changes in master document"),
186 lyx::support::bformat(_("The state of the branch '%1$s' "
187 "was changed in the master file. "
188 "Please make sure to save the master."), params_.branch), true);
189 our_branch->setSelected(activate);
190 // cur.forceBufferUpdate() is not enough
194 // if branch exists in a descendant, update previews.
195 // TODO: only needed if "Show preview" is enabled in the included inset.
196 bool exists_in_desc = false;
197 for (auto const & it : buf->getDescendants()) {
198 if (it->params().branchlist().find(params_.branch))
199 exists_in_desc = true;
201 if (exists_in_desc) {
202 // TODO: ideally we would only update the previews of the
203 // specific children that have this branch directly or
204 // in one of their descendants
205 buf->removePreviews();
206 buf->updatePreviews();
210 case LFUN_BRANCH_INVERT:
211 cur.recordUndoInset(this);
212 params_.inverted = !params_.inverted;
213 // what we really want here is a TOC update, but that means
214 // a full buffer update
215 cur.forceBufferUpdate();
217 case LFUN_BRANCH_ADD:
218 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
220 case LFUN_INSET_TOGGLE:
221 if (cmd.argument() == "assign")
222 setStatus(cur, isBranchSelected() ? Open : Collapsed);
224 InsetCollapsible::doDispatch(cur, cmd);
228 InsetCollapsible::doDispatch(cur, cmd);
234 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
235 FuncStatus & flag) const
237 bool const known_branch =
238 buffer().params().branchlist().find(params_.branch);
240 switch (cmd.action()) {
241 case LFUN_INSET_MODIFY:
242 flag.setEnabled(true);
245 case LFUN_BRANCH_ACTIVATE:
246 flag.setEnabled(known_branch && !isBranchSelected(true));
249 case LFUN_BRANCH_INVERT:
250 flag.setEnabled(true);
251 flag.setOnOff(params_.inverted);
254 case LFUN_BRANCH_ADD:
255 flag.setEnabled(!known_branch);
258 case LFUN_BRANCH_DEACTIVATE:
259 flag.setEnabled(isBranchSelected(true));
262 case LFUN_BRANCH_MASTER_ACTIVATE:
263 flag.setEnabled(buffer().parent()
264 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
265 && !isBranchSelected());
268 case LFUN_BRANCH_MASTER_DEACTIVATE:
269 flag.setEnabled(buffer().parent() && isBranchSelected());
272 case LFUN_INSET_TOGGLE:
273 if (cmd.argument() == "assign")
274 flag.setEnabled(true);
276 return InsetCollapsible::getStatus(cur, cmd, flag);
280 return InsetCollapsible::getStatus(cur, cmd, flag);
286 bool InsetBranch::isBranchSelected(bool const child) const
288 Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
289 BranchList const & branchlist = realbuffer.params().branchlist();
290 Branch const * ourBranch = branchlist.find(params_.branch);
293 // this branch is defined in child only
294 ourBranch = buffer().params().branchlist().find(params_.branch);
298 return ourBranch->isSelected();
302 bool InsetBranch::producesOutput() const
304 return isBranchSelected() != params_.inverted;
308 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
310 if (producesOutput()) {
311 OutputParams rp = runparams;
313 InsetText::latex(os, rp);
314 // These need to be passed upstream
315 runparams.need_maketitle = rp.need_maketitle;
316 runparams.have_maketitle = rp.have_maketitle;
321 int InsetBranch::plaintext(odocstringstream & os,
322 OutputParams const & runparams, size_t max_length) const
324 if (!producesOutput())
327 int len = InsetText::plaintext(os, runparams, max_length);
332 void InsetBranch::docbook(XMLStream & xs, OutputParams const & runparams) const
334 if (producesOutput()) {
335 OutputParams rp = runparams;
337 rp.par_end = text().paragraphs().size();
338 docbookParagraphs(text(), buffer(), xs, rp);
343 docstring InsetBranch::xhtml(XMLStream & xs, OutputParams const & rp) const
345 if (producesOutput()) {
346 OutputParams newrp = rp;
348 newrp.par_end = text().paragraphs().size();
349 xhtmlParagraphs(text(), buffer(), xs, newrp);
355 void InsetBranch::toString(odocstream & os) const
357 if (producesOutput())
358 InsetCollapsible::toString(os);
362 void InsetBranch::forOutliner(docstring & os, size_t const maxlen,
363 bool const shorten) const
365 if (producesOutput())
366 InsetCollapsible::forOutliner(os, maxlen, shorten);
370 void InsetBranch::validate(LaTeXFeatures & features) const
372 if (producesOutput())
373 InsetCollapsible::validate(features);
377 string InsetBranch::contextMenuName() const
379 return "context-branch";
383 bool InsetBranch::isMacroScope() const
385 // Its own scope if not selected by buffer
386 return !producesOutput();
390 string InsetBranch::params2string(InsetBranchParams const & params)
398 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
400 params = InsetBranchParams();
404 istringstream data(in);
407 lex.setContext("InsetBranch::string2params");
412 void InsetBranch::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
414 setLabel(params_.branch + (params_.inverted ? " (-)" : ""));
415 InsetCollapsible::updateBuffer(it, utype, deleted);
419 void InsetBranchParams::write(ostream & os) const
421 os << to_utf8(branch)
428 void InsetBranchParams::read(Lexer & lex)
430 // There may be a space in branch name
431 // if we wanted to use lex>>, the branch name should be properly in quotes
433 branch = lex.getDocString();
434 lex >> "inverted" >> inverted;