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/convert.h"
32 #include "support/debug.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
36 #include "frontends/alert.h"
37 #include "frontends/Application.h"
46 InsetBranch::InsetBranch(Buffer * buf, InsetBranchParams const & params)
47 : InsetCollapsible(buf, InsetText::DefaultLayout), params_(params)
51 void InsetBranch::write(ostream & os) const
56 InsetCollapsible::write(os);
60 void InsetBranch::read(Lexer & lex)
63 InsetCollapsible::read(lex);
67 docstring InsetBranch::toolTip(BufferView const & bv, int, int) const
69 docstring const masterstatus = isBranchSelected() ?
70 _("active") : _("non-active");
71 docstring const childstatus = isBranchSelected(true) ?
72 _("active") : _("non-active");
73 docstring const status = (masterstatus == childstatus) ?
75 support::bformat(_("master %1$s, child %2$s"),
76 masterstatus, childstatus);
78 docstring const masteron = producesOutput() ?
80 docstring const childon =
81 (isBranchSelected(true) != params_.inverted) ?
83 docstring const onoff = (masteron == childon) ?
85 support::bformat(_("master %1$s, child %2$s"),
88 docstring const heading =
89 support::bformat(_("Branch Name: %1$s\nBranch Status: %2$s\nInset Status: %3$s"),
90 params_.branch, status, onoff);
94 return toolTipText(heading + from_ascii("\n"));
98 docstring const InsetBranch::buttonLabel(BufferView const &) const
100 static char_type const tick = 0x2714; // ✔ U+2714 HEAVY CHECK MARK
101 static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
103 Buffer const & realbuffer = *buffer().masterBuffer();
104 BranchList const & branchlist = realbuffer.params().branchlist();
105 bool const inmaster = branchlist.find(params_.branch);
106 bool const inchild = buffer().params().branchlist().find(params_.branch);
108 bool const master_selected = producesOutput();
109 bool const child_selected = isBranchSelected(true) != params_.inverted;
111 docstring symb = docstring(1, master_selected ? tick : cross);
112 if (inchild && master_selected != child_selected)
113 symb += (child_selected ? tick : cross);
115 docstring inv_symb = from_ascii(params_.inverted ? "~" : "");
117 if (decoration() == InsetDecoration::MINIMALISTIC)
118 return symb + inv_symb + params_.branch;
121 if (inmaster && inchild)
123 else if (inchild) // && !inmaster
124 s = _("Branch (child): ");
125 else if (inmaster) // && !inchild
126 s = _("Branch (master): ");
127 else // !inmaster && !inchild
128 s = _("Branch (undefined): ");
129 s += inv_symb + params_.branch;
135 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
137 if (params_.branch.empty())
138 return Inset::backgroundColor(pi);
140 string const branchcol = "branch" + convert<string>(buffer().params().branchlist().id())
141 + to_utf8(params_.branch);
142 ColorCode c = lcolor.getFromLyXName(branchcol);
149 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
151 switch (cmd.action()) {
152 case LFUN_INSET_MODIFY: {
153 InsetBranchParams params;
154 InsetBranch::string2params(to_utf8(cmd.argument()), params);
156 cur.recordUndoInset(this);
157 params_.branch = params.branch;
158 params_.inverted = params.inverted;
159 // what we really want here is a TOC update, but that means
160 // a full buffer update
161 cur.forceBufferUpdate();
164 case LFUN_BRANCH_ACTIVATE:
165 case LFUN_BRANCH_DEACTIVATE:
166 case LFUN_BRANCH_MASTER_ACTIVATE:
167 case LFUN_BRANCH_MASTER_DEACTIVATE: {
168 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
169 || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
170 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
173 Branch * our_branch = buf->params().branchlist().find(params_.branch);
177 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
178 || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
179 if (our_branch->isSelected() != activate) {
180 // FIXME If the branch is in the master document, we cannot
181 // call recordUndo..., because the master may be hidden, and
182 // the code presently assumes that hidden documents can never
183 // be dirty. See GuiView::closeBufferAll(), for example.
184 // An option would be to check if the master is hidden.
187 buffer().undo().recordUndoBufferParams(cur);
189 // at least issue a warning for now (ugly, but better than dataloss).
190 frontend::Alert::warning(_("Branch state changes in master document"),
191 lyx::support::bformat(_("The state of the branch '%1$s' "
192 "was changed in the master file. "
193 "Please make sure to save the master."), params_.branch), true);
194 our_branch->setSelected(activate);
195 // cur.forceBufferUpdate() is not enough
199 // if branch exists in a descendant, update previews.
200 // TODO: only needed if "Show preview" is enabled in the included inset.
201 bool exists_in_desc = false;
202 for (auto const & it : buf->getDescendants()) {
203 if (it->params().branchlist().find(params_.branch))
204 exists_in_desc = true;
206 if (exists_in_desc) {
207 // TODO: ideally we would only update the previews of the
208 // specific children that have this branch directly or
209 // in one of their descendants
210 buf->removePreviews();
211 buf->updatePreviews();
215 case LFUN_BRANCH_INVERT:
216 cur.recordUndoInset(this);
217 params_.inverted = !params_.inverted;
218 // what we really want here is a TOC update, but that means
219 // a full buffer update
220 cur.forceBufferUpdate();
222 case LFUN_BRANCH_ADD:
223 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
225 case LFUN_INSET_TOGGLE:
226 if (cmd.argument() == "assign")
227 setStatus(cur, isBranchSelected() ? Open : Collapsed);
229 InsetCollapsible::doDispatch(cur, cmd);
233 InsetCollapsible::doDispatch(cur, cmd);
239 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
240 FuncStatus & flag) const
242 bool const known_branch =
243 buffer().params().branchlist().find(params_.branch);
245 switch (cmd.action()) {
246 case LFUN_INSET_MODIFY:
247 flag.setEnabled(true);
250 case LFUN_BRANCH_ACTIVATE:
251 flag.setEnabled(known_branch && !isBranchSelected(true));
254 case LFUN_BRANCH_INVERT:
255 flag.setEnabled(true);
256 flag.setOnOff(params_.inverted);
259 case LFUN_BRANCH_ADD:
260 flag.setEnabled(!known_branch);
263 case LFUN_BRANCH_DEACTIVATE:
264 flag.setEnabled(isBranchSelected(true));
267 case LFUN_BRANCH_MASTER_ACTIVATE:
268 flag.setEnabled(buffer().parent()
269 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
270 && !isBranchSelected());
273 case LFUN_BRANCH_MASTER_DEACTIVATE:
274 flag.setEnabled(buffer().parent() && isBranchSelected());
277 case LFUN_INSET_TOGGLE:
278 if (cmd.argument() == "assign")
279 flag.setEnabled(true);
281 return InsetCollapsible::getStatus(cur, cmd, flag);
285 return InsetCollapsible::getStatus(cur, cmd, flag);
291 bool InsetBranch::isBranchSelected(bool const child) const
293 Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
294 BranchList const & branchlist = realbuffer.params().branchlist();
295 Branch const * ourBranch = branchlist.find(params_.branch);
298 // this branch is defined in child only
299 ourBranch = buffer().params().branchlist().find(params_.branch);
303 return ourBranch->isSelected();
307 bool InsetBranch::producesOutput() const
309 return isBranchSelected() != params_.inverted;
313 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
315 if (producesOutput()) {
316 OutputParams rp = runparams;
318 InsetText::latex(os, rp);
319 // These need to be passed upstream
320 runparams.need_maketitle = rp.need_maketitle;
321 runparams.have_maketitle = rp.have_maketitle;
326 int InsetBranch::plaintext(odocstringstream & os,
327 OutputParams const & runparams, size_t max_length) const
329 if (!producesOutput())
332 int len = InsetText::plaintext(os, runparams, max_length);
337 void InsetBranch::docbook(XMLStream & xs, OutputParams const & runparams) const
339 if (producesOutput()) {
340 OutputParams rp = runparams;
342 rp.par_end = text().paragraphs().size();
343 docbookParagraphs(text(), buffer(), xs, rp);
348 docstring InsetBranch::xhtml(XMLStream & xs, OutputParams const & rp) const
350 if (producesOutput()) {
351 OutputParams newrp = rp;
353 newrp.par_end = text().paragraphs().size();
354 xhtmlParagraphs(text(), buffer(), xs, newrp);
360 void InsetBranch::toString(odocstream & os) const
362 if (producesOutput())
363 InsetCollapsible::toString(os);
367 void InsetBranch::forOutliner(docstring & os, size_t const maxlen,
368 bool const shorten) const
370 if (producesOutput())
371 InsetCollapsible::forOutliner(os, maxlen, shorten);
375 void InsetBranch::validate(LaTeXFeatures & features) const
377 if (producesOutput())
378 InsetCollapsible::validate(features);
382 string InsetBranch::contextMenuName() const
384 return "context-branch";
388 bool InsetBranch::isMacroScope() const
390 // Its own scope if not selected by buffer
391 return !producesOutput();
395 string InsetBranch::params2string(InsetBranchParams const & params)
403 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
405 params = InsetBranchParams();
409 istringstream data(in);
412 lex.setContext("InsetBranch::string2params");
417 void InsetBranch::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
419 setLabel(params_.branch + (params_.inverted ? " (-)" : ""));
420 InsetCollapsible::updateBuffer(it, utype, deleted);
424 void InsetBranchParams::write(ostream & os) const
426 os << to_utf8(branch)
433 void InsetBranchParams::read(Lexer & lex)
435 // There may be a space in branch name
436 // if we wanted to use lex>>, the branch name should be properly in quotes
438 branch = lex.getDocString();
439 lex >> "inverted" >> inverted;