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 & 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() == InsetLayout::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 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
145 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
147 switch (cmd.action()) {
148 case LFUN_INSET_MODIFY: {
149 InsetBranchParams params;
150 InsetBranch::string2params(to_utf8(cmd.argument()), params);
152 cur.recordUndoInset(this);
153 params_.branch = params.branch;
154 params_.inverted = params.inverted;
155 // what we really want here is a TOC update, but that means
156 // a full buffer update
157 cur.forceBufferUpdate();
160 case LFUN_BRANCH_ACTIVATE:
161 case LFUN_BRANCH_DEACTIVATE:
162 case LFUN_BRANCH_MASTER_ACTIVATE:
163 case LFUN_BRANCH_MASTER_DEACTIVATE: {
164 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
165 || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
166 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
169 Branch * our_branch = buf->params().branchlist().find(params_.branch);
173 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
174 || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
175 if (our_branch->isSelected() != activate) {
176 // FIXME If the branch is in the master document, we cannot
177 // call recordUndo..., because the master may be hidden, and
178 // the code presently assumes that hidden documents can never
179 // be dirty. See GuiView::closeBufferAll(), for example.
180 // An option would be to check if the master is hidden.
183 buffer().undo().recordUndoBufferParams(cur);
185 // at least issue a warning for now (ugly, but better than dataloss).
186 frontend::Alert::warning(_("Branch state changes in master document"),
187 lyx::support::bformat(_("The state of the branch '%1$s' "
188 "was changed in the master file. "
189 "Please make sure to save the master."), params_.branch), true);
190 our_branch->setSelected(activate);
191 // cur.forceBufferUpdate() is not enough
196 case LFUN_BRANCH_INVERT:
197 cur.recordUndoInset(this);
198 params_.inverted = !params_.inverted;
199 // what we really want here is a TOC update, but that means
200 // a full buffer update
201 cur.forceBufferUpdate();
203 case LFUN_BRANCH_ADD:
204 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
206 case LFUN_INSET_TOGGLE:
207 if (cmd.argument() == "assign")
208 setStatus(cur, isBranchSelected() ? Open : Collapsed);
210 InsetCollapsable::doDispatch(cur, cmd);
214 InsetCollapsable::doDispatch(cur, cmd);
220 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
221 FuncStatus & flag) const
223 bool const known_branch =
224 buffer().params().branchlist().find(params_.branch);
226 switch (cmd.action()) {
227 case LFUN_INSET_MODIFY:
228 flag.setEnabled(true);
231 case LFUN_BRANCH_ACTIVATE:
232 flag.setEnabled(known_branch && !isBranchSelected(true));
235 case LFUN_BRANCH_INVERT:
236 flag.setEnabled(true);
237 flag.setOnOff(params_.inverted);
240 case LFUN_BRANCH_ADD:
241 flag.setEnabled(!known_branch);
244 case LFUN_BRANCH_DEACTIVATE:
245 flag.setEnabled(isBranchSelected(true));
248 case LFUN_BRANCH_MASTER_ACTIVATE:
249 flag.setEnabled(buffer().parent()
250 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
251 && !isBranchSelected());
254 case LFUN_BRANCH_MASTER_DEACTIVATE:
255 flag.setEnabled(buffer().parent() && isBranchSelected());
258 case LFUN_INSET_TOGGLE:
259 if (cmd.argument() == "assign")
260 flag.setEnabled(true);
262 return InsetCollapsable::getStatus(cur, cmd, flag);
266 return InsetCollapsable::getStatus(cur, cmd, flag);
272 bool InsetBranch::isBranchSelected(bool const child) const
274 Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
275 BranchList const & branchlist = realbuffer.params().branchlist();
276 Branch const * ourBranch = branchlist.find(params_.branch);
279 // this branch is defined in child only
280 ourBranch = buffer().params().branchlist().find(params_.branch);
284 return ourBranch->isSelected();
288 bool InsetBranch::producesOutput() const
290 return isBranchSelected() != params_.inverted;
294 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
296 if (producesOutput())
297 InsetText::latex(os, runparams);
301 int InsetBranch::plaintext(odocstringstream & os,
302 OutputParams const & runparams, size_t max_length) const
304 if (!producesOutput())
307 int len = InsetText::plaintext(os, runparams, max_length);
312 int InsetBranch::docbook(odocstream & os,
313 OutputParams const & runparams) const
315 return producesOutput() ? InsetText::docbook(os, runparams) : 0;
319 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
321 if (producesOutput()) {
322 OutputParams newrp = rp;
324 newrp.par_end = text().paragraphs().size();
325 xhtmlParagraphs(text(), buffer(), xs, newrp);
331 void InsetBranch::toString(odocstream & os) const
333 if (producesOutput())
334 InsetCollapsable::toString(os);
338 void InsetBranch::forOutliner(docstring & os, size_t const maxlen,
339 bool const shorten) const
341 if (producesOutput())
342 InsetCollapsable::forOutliner(os, maxlen, shorten);
346 void InsetBranch::validate(LaTeXFeatures & features) const
348 if (producesOutput())
349 InsetCollapsable::validate(features);
353 string InsetBranch::contextMenuName() const
355 return "context-branch";
359 bool InsetBranch::isMacroScope() const
361 // Its own scope if not selected by buffer
362 return !producesOutput();
366 string InsetBranch::params2string(InsetBranchParams const & params)
374 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
376 params = InsetBranchParams();
380 istringstream data(in);
383 lex.setContext("InsetBranch::string2params");
388 void InsetBranch::addToToc(DocIterator const & cpit, bool output_active,
389 UpdateType utype) const
391 DocIterator pit = cpit;
392 pit.push_back(CursorSlice(const_cast<InsetBranch &>(*this)));
395 text().forOutliner(str, TOC_ENTRY_LENGTH);
396 str = params_.branch + (params_.inverted ? " (-):" : ": ") + str;
398 shared_ptr<Toc> toc = buffer().tocBackend().toc("branch");
399 toc->push_back(TocItem(pit, 0, str, output_active));
401 // Proceed with the rest of the inset.
402 bool const doing_output = output_active && producesOutput();
403 InsetCollapsable::addToToc(cpit, doing_output, utype);
407 void InsetBranchParams::write(ostream & os) const
409 os << to_utf8(branch)
416 void InsetBranchParams::read(Lexer & lex)
419 lex >> "inverted" >> inverted;