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"
27 #include "OutputParams.h"
28 #include "output_xhtml.h"
29 #include "TextClass.h"
30 #include "TocBackend.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 : InsetCollapsable(buf, InsetText::DefaultLayout), params_(params)
51 void InsetBranch::write(ostream & os) const
56 InsetCollapsable::write(os);
60 void InsetBranch::read(Lexer & lex)
63 InsetCollapsable::read(lex);
67 docstring InsetBranch::toolTip(BufferView const &, 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 = producesOutput(true) ?
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);
90 return toolTipText(heading);
94 docstring const InsetBranch::buttonLabel(BufferView const &) const
96 static char_type const tick = 0x2714; // ✔ U+2714 HEAVY CHECK MARK
97 static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
99 Buffer const & realbuffer = *buffer().masterBuffer();
100 BranchList const & branchlist = realbuffer.params().branchlist();
101 bool const inmaster = branchlist.find(params_.branch);
102 bool const inchild = buffer().params().branchlist().find(params_.branch);
104 bool const master_selected = producesOutput();
105 bool const child_selected = producesOutput(true);
107 docstring symb = docstring(1, master_selected ? tick : cross);
108 if (inchild && master_selected != child_selected)
109 symb += (child_selected ? tick : cross);
111 if (decoration() == InsetLayout::MINIMALISTIC)
112 return symb + params_.branch;
115 if (inmaster && inchild)
117 else if (inchild) // && !inmaster
118 s = _("Branch (child): ");
119 else if (inmaster) // && !inchild
120 s = _("Branch (master): ");
121 else // !inmaster && !inchild
122 s = _("Branch (undefined): ");
129 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
131 if (params_.branch.empty())
132 return Inset::backgroundColor(pi);
134 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
141 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
143 switch (cmd.action()) {
144 case LFUN_INSET_MODIFY: {
145 InsetBranchParams params;
146 InsetBranch::string2params(to_utf8(cmd.argument()), params);
148 cur.recordUndoInset(this);
149 params_.branch = params.branch;
150 params_.inverted = params.inverted;
151 // what we really want here is a TOC update, but that means
152 // a full buffer update
153 cur.forceBufferUpdate();
156 case LFUN_BRANCH_ACTIVATE:
157 case LFUN_BRANCH_DEACTIVATE:
158 case LFUN_BRANCH_MASTER_ACTIVATE:
159 case LFUN_BRANCH_MASTER_DEACTIVATE: {
160 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
161 || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
162 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
165 Branch * our_branch = buf->params().branchlist().find(params_.branch);
169 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
170 || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
171 if (our_branch->isSelected() != activate) {
172 // FIXME If the branch is in the master document, we cannot
173 // call recordUndo..., because the master may be hidden, and
174 // the code presently assumes that hidden documents can never
175 // be dirty. See GuiView::closeBufferAll(), for example.
176 // An option would be to check if the master is hidden.
179 buffer().undo().recordUndoBufferParams(cur);
181 // at least issue a warning for now (ugly, but better than dataloss).
182 frontend::Alert::warning(_("Branch state changes in master document"),
183 lyx::support::bformat(_("The state of the branch '%1$s' "
184 "was changed in the master file. "
185 "Please make sure to save the master."), params_.branch), true);
186 our_branch->setSelected(activate);
187 // cur.forceBufferUpdate() is not enough
192 case LFUN_BRANCH_INVERT:
193 cur.recordUndoInset(this);
194 params_.inverted = !params_.inverted;
195 // what we really want here is a TOC update, but that means
196 // a full buffer update
197 cur.forceBufferUpdate();
199 case LFUN_BRANCH_ADD:
200 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
202 case LFUN_INSET_TOGGLE:
203 if (cmd.argument() == "assign")
204 setStatus(cur, isBranchSelected() ? Open : Collapsed);
206 InsetCollapsable::doDispatch(cur, cmd);
210 InsetCollapsable::doDispatch(cur, cmd);
216 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
217 FuncStatus & flag) const
219 bool const known_branch =
220 buffer().params().branchlist().find(params_.branch);
222 switch (cmd.action()) {
223 case LFUN_INSET_MODIFY:
224 flag.setEnabled(true);
227 case LFUN_BRANCH_ACTIVATE:
228 flag.setEnabled(known_branch && !isBranchSelected(true));
231 case LFUN_BRANCH_INVERT:
232 flag.setEnabled(true);
233 flag.setOnOff(params_.inverted);
236 case LFUN_BRANCH_ADD:
237 flag.setEnabled(!known_branch);
240 case LFUN_BRANCH_DEACTIVATE:
241 flag.setEnabled(isBranchSelected(true));
244 case LFUN_BRANCH_MASTER_ACTIVATE:
245 flag.setEnabled(buffer().parent()
246 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
247 && !isBranchSelected());
250 case LFUN_BRANCH_MASTER_DEACTIVATE:
251 flag.setEnabled(buffer().parent() && isBranchSelected());
254 case LFUN_INSET_TOGGLE:
255 if (cmd.argument() == "assign")
256 flag.setEnabled(true);
258 return InsetCollapsable::getStatus(cur, cmd, flag);
262 return InsetCollapsable::getStatus(cur, cmd, flag);
268 bool InsetBranch::isBranchSelected(bool const child) const
270 Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
271 BranchList const & branchlist = realbuffer.params().branchlist();
272 Branch const * ourBranch = branchlist.find(params_.branch);
275 // this branch is defined in child only
276 ourBranch = buffer().params().branchlist().find(params_.branch);
280 return ourBranch->isSelected();
284 bool InsetBranch::producesOutput(bool const child) const
286 return isBranchSelected(child) != params_.inverted;
290 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
292 if (producesOutput())
293 InsetText::latex(os, runparams);
297 int InsetBranch::plaintext(odocstringstream & os,
298 OutputParams const & runparams, size_t max_length) const
300 if (!producesOutput())
303 int len = InsetText::plaintext(os, runparams, max_length);
308 int InsetBranch::docbook(odocstream & os,
309 OutputParams const & runparams) const
311 return producesOutput() ? InsetText::docbook(os, runparams) : 0;
315 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
317 if (producesOutput()) {
318 OutputParams newrp = rp;
320 newrp.par_end = text().paragraphs().size();
321 xhtmlParagraphs(text(), buffer(), xs, newrp);
327 void InsetBranch::toString(odocstream & os) const
329 if (producesOutput())
330 InsetCollapsable::toString(os);
334 void InsetBranch::forOutliner(docstring & os, size_t const maxlen,
335 bool const shorten) const
337 if (producesOutput())
338 InsetCollapsable::forOutliner(os, maxlen, shorten);
342 void InsetBranch::validate(LaTeXFeatures & features) const
344 if (producesOutput())
345 InsetCollapsable::validate(features);
349 string InsetBranch::contextMenuName() const
351 return "context-branch";
355 bool InsetBranch::isMacroScope() const
357 // Its own scope if not selected by buffer
358 return !producesOutput();
362 string InsetBranch::params2string(InsetBranchParams const & params)
370 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
372 params = InsetBranchParams();
376 istringstream data(in);
379 lex.setContext("InsetBranch::string2params");
384 void InsetBranch::addToToc(DocIterator const & cpit, bool output_active,
385 UpdateType utype) const
387 DocIterator pit = cpit;
388 pit.push_back(CursorSlice(const_cast<InsetBranch &>(*this)));
391 text().forOutliner(str, TOC_ENTRY_LENGTH);
392 str = params_.branch + (params_.inverted ? " (-):" : ": ") + str;
394 shared_ptr<Toc> toc = buffer().tocBackend().toc("branch");
395 toc->push_back(TocItem(pit, 0, str, output_active));
397 // Proceed with the rest of the inset.
398 bool const doing_output = output_active && producesOutput();
399 InsetCollapsable::addToToc(cpit, doing_output, utype);
403 void InsetBranchParams::write(ostream & os) const
405 os << to_utf8(branch)
412 void InsetBranchParams::read(Lexer & lex)
415 lex >> "inverted" >> inverted;