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 if (decoration() == InsetDecoration::MINIMALISTIC)
116 return symb + params_.branch;
119 if (inmaster && inchild)
121 else if (inchild) // && !inmaster
122 s = _("Branch (child): ");
123 else if (inmaster) // && !inchild
124 s = _("Branch (master): ");
125 else // !inmaster && !inchild
126 s = _("Branch (undefined): ");
133 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
135 if (params_.branch.empty())
136 return Inset::backgroundColor(pi);
138 string const branchcol = "branch" + convert<string>(buffer().params().branchlist().id())
139 + to_utf8(params_.branch);
140 ColorCode c = lcolor.getFromLyXName(branchcol);
147 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
149 switch (cmd.action()) {
150 case LFUN_INSET_MODIFY: {
151 InsetBranchParams params;
152 InsetBranch::string2params(to_utf8(cmd.argument()), params);
154 cur.recordUndoInset(this);
155 params_.branch = params.branch;
156 params_.inverted = params.inverted;
157 // what we really want here is a TOC update, but that means
158 // a full buffer update
159 cur.forceBufferUpdate();
162 case LFUN_BRANCH_ACTIVATE:
163 case LFUN_BRANCH_DEACTIVATE:
164 case LFUN_BRANCH_MASTER_ACTIVATE:
165 case LFUN_BRANCH_MASTER_DEACTIVATE: {
166 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
167 || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
168 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
171 Branch * our_branch = buf->params().branchlist().find(params_.branch);
175 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
176 || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
177 if (our_branch->isSelected() != activate) {
178 // FIXME If the branch is in the master document, we cannot
179 // call recordUndo..., because the master may be hidden, and
180 // the code presently assumes that hidden documents can never
181 // be dirty. See GuiView::closeBufferAll(), for example.
182 // An option would be to check if the master is hidden.
185 buffer().undo().recordUndoBufferParams(cur);
187 // at least issue a warning for now (ugly, but better than dataloss).
188 frontend::Alert::warning(_("Branch state changes in master document"),
189 lyx::support::bformat(_("The state of the branch '%1$s' "
190 "was changed in the master file. "
191 "Please make sure to save the master."), params_.branch), true);
192 our_branch->setSelected(activate);
193 // cur.forceBufferUpdate() is not enough
197 // if branch exists in a descendant, update previews.
198 // TODO: only needed if "Show preview" is enabled in the included inset.
199 bool exists_in_desc = false;
200 for (auto const & it : buf->getDescendants()) {
201 if (it->params().branchlist().find(params_.branch))
202 exists_in_desc = true;
204 if (exists_in_desc) {
205 // TODO: ideally we would only update the previews of the
206 // specific children that have this branch directly or
207 // in one of their descendants
208 buf->removePreviews();
209 buf->updatePreviews();
213 case LFUN_BRANCH_INVERT:
214 cur.recordUndoInset(this);
215 params_.inverted = !params_.inverted;
216 // what we really want here is a TOC update, but that means
217 // a full buffer update
218 cur.forceBufferUpdate();
220 case LFUN_BRANCH_ADD:
221 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
223 case LFUN_INSET_TOGGLE:
224 if (cmd.argument() == "assign")
225 setStatus(cur, isBranchSelected() ? Open : Collapsed);
227 InsetCollapsible::doDispatch(cur, cmd);
231 InsetCollapsible::doDispatch(cur, cmd);
237 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
238 FuncStatus & flag) const
240 bool const known_branch =
241 buffer().params().branchlist().find(params_.branch);
243 switch (cmd.action()) {
244 case LFUN_INSET_MODIFY:
245 flag.setEnabled(true);
248 case LFUN_BRANCH_ACTIVATE:
249 flag.setEnabled(known_branch && !isBranchSelected(true));
252 case LFUN_BRANCH_INVERT:
253 flag.setEnabled(true);
254 flag.setOnOff(params_.inverted);
257 case LFUN_BRANCH_ADD:
258 flag.setEnabled(!known_branch);
261 case LFUN_BRANCH_DEACTIVATE:
262 flag.setEnabled(isBranchSelected(true));
265 case LFUN_BRANCH_MASTER_ACTIVATE:
266 flag.setEnabled(buffer().parent()
267 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
268 && !isBranchSelected());
271 case LFUN_BRANCH_MASTER_DEACTIVATE:
272 flag.setEnabled(buffer().parent() && isBranchSelected());
275 case LFUN_INSET_TOGGLE:
276 if (cmd.argument() == "assign")
277 flag.setEnabled(true);
279 return InsetCollapsible::getStatus(cur, cmd, flag);
283 return InsetCollapsible::getStatus(cur, cmd, flag);
289 bool InsetBranch::isBranchSelected(bool const child) const
291 Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
292 BranchList const & branchlist = realbuffer.params().branchlist();
293 Branch const * ourBranch = branchlist.find(params_.branch);
296 // this branch is defined in child only
297 ourBranch = buffer().params().branchlist().find(params_.branch);
301 return ourBranch->isSelected();
305 bool InsetBranch::producesOutput() const
307 return isBranchSelected() != params_.inverted;
311 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
313 if (producesOutput()) {
314 OutputParams rp = runparams;
316 InsetText::latex(os, rp);
317 // These need to be passed upstream
318 runparams.need_maketitle = rp.need_maketitle;
319 runparams.have_maketitle = rp.have_maketitle;
324 int InsetBranch::plaintext(odocstringstream & os,
325 OutputParams const & runparams, size_t max_length) const
327 if (!producesOutput())
330 int len = InsetText::plaintext(os, runparams, max_length);
335 void InsetBranch::docbook(XMLStream & xs, OutputParams const & runparams) const
337 if (producesOutput()) {
338 OutputParams rp = runparams;
340 rp.par_end = text().paragraphs().size();
341 docbookParagraphs(text(), buffer(), xs, rp);
346 docstring InsetBranch::xhtml(XMLStream & xs, OutputParams const & rp) const
348 if (producesOutput()) {
349 OutputParams newrp = rp;
351 newrp.par_end = text().paragraphs().size();
352 xhtmlParagraphs(text(), buffer(), xs, newrp);
358 void InsetBranch::toString(odocstream & os) const
360 if (producesOutput())
361 InsetCollapsible::toString(os);
365 void InsetBranch::forOutliner(docstring & os, size_t const maxlen,
366 bool const shorten) const
368 if (producesOutput())
369 InsetCollapsible::forOutliner(os, maxlen, shorten);
373 void InsetBranch::validate(LaTeXFeatures & features) const
375 if (producesOutput())
376 InsetCollapsible::validate(features);
380 string InsetBranch::contextMenuName() const
382 return "context-branch";
386 bool InsetBranch::isMacroScope() const
388 // Its own scope if not selected by buffer
389 return !producesOutput();
393 string InsetBranch::params2string(InsetBranchParams const & params)
401 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
403 params = InsetBranchParams();
407 istringstream data(in);
410 lex.setContext("InsetBranch::string2params");
415 void InsetBranch::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
417 setLabel(params_.branch + (params_.inverted ? " (-)" : ""));
418 InsetCollapsible::updateBuffer(it, utype, deleted);
422 void InsetBranchParams::write(ostream & os) const
424 os << to_utf8(branch)
431 void InsetBranchParams::read(Lexer & lex)
433 // There may be a space in branch name
434 // if we wanted to use lex>>, the branch name should be properly in quotes
436 branch = lex.getDocString();
437 lex >> "inverted" >> inverted;