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);
77 docstring const heading = params_.inverted ?
78 support::bformat(_("Branch, inverted (%1$s): %2$s"), status, params_.branch) :
79 support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
82 return toolTipText(heading + from_ascii("\n"));
86 docstring const InsetBranch::buttonLabel(BufferView const &) const
88 static char_type const tick = 0x2714; // ✔ U+2714 HEAVY CHECK MARK
89 static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
90 static char_type const itick = 0x271A; // ✚ U+271A HEAVY GREEK CROSS
91 static char_type const icross = 0x274E; // ❎ U+274E NEGATIVE SQUARED CROSS MARK
93 Buffer const & realbuffer = *buffer().masterBuffer();
94 BranchList const & branchlist = realbuffer.params().branchlist();
95 bool const inmaster = branchlist.find(params_.branch);
96 bool const inchild = buffer().params().branchlist().find(params_.branch);
98 bool const master_selected = isBranchSelected();
99 bool const child_selected = isBranchSelected(true);
101 docstring symb = docstring(1, master_selected ?
102 (params_.inverted ? icross : tick) :
103 (params_.inverted ? itick: cross));
104 if (inchild && master_selected != child_selected) {
105 symb += child_selected ?
106 (params_.inverted ? icross : tick) :
107 (params_.inverted ? itick: cross);
110 if (decoration() == InsetLayout::MINIMALISTIC)
111 return symb + params_.branch;
114 if (inmaster && inchild)
116 else if (inchild) // && !inmaster
117 s = _("Branch (child): ");
118 else if (inmaster) // && !inchild
119 s = _("Branch (master): ");
120 else // !inmaster && !inchild
121 s = _("Branch (undefined): ");
128 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
130 if (params_.branch.empty())
131 return Inset::backgroundColor(pi);
133 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
140 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
142 switch (cmd.action()) {
143 case LFUN_INSET_MODIFY: {
144 InsetBranchParams params;
145 InsetBranch::string2params(to_utf8(cmd.argument()), params);
147 cur.recordUndoInset(this);
148 params_.branch = params.branch;
149 params_.inverted = params.inverted;
150 // what we really want here is a TOC update, but that means
151 // a full buffer update
152 cur.forceBufferUpdate();
155 case LFUN_BRANCH_ACTIVATE:
156 case LFUN_BRANCH_DEACTIVATE:
157 case LFUN_BRANCH_MASTER_ACTIVATE:
158 case LFUN_BRANCH_MASTER_DEACTIVATE: {
159 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
160 || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
161 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
164 Branch * our_branch = buf->params().branchlist().find(params_.branch);
168 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
169 || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
170 if (our_branch->isSelected() != activate) {
171 // FIXME If the branch is in the master document, we cannot
172 // call recordUndo..., because the master may be hidden, and
173 // the code presently assumes that hidden documents can never
174 // be dirty. See GuiView::closeBufferAll(), for example.
175 // An option would be to check if the master is hidden.
178 buffer().undo().recordUndoBufferParams(cur);
180 // at least issue a warning for now (ugly, but better than dataloss).
181 frontend::Alert::warning(_("Branch state changes in master document"),
182 lyx::support::bformat(_("The state of the branch '%1$s' "
183 "was changed in the master file. "
184 "Please make sure to save the master."), params_.branch), true);
185 our_branch->setSelected(activate);
186 // cur.forceBufferUpdate() is not enough
191 case LFUN_BRANCH_INVERT:
192 params_.inverted = !params_.inverted;
194 case LFUN_BRANCH_ADD:
195 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
197 case LFUN_INSET_TOGGLE:
198 if (cmd.argument() == "assign")
199 setStatus(cur, isBranchSelected() ? Open : Collapsed);
201 InsetCollapsable::doDispatch(cur, cmd);
205 InsetCollapsable::doDispatch(cur, cmd);
211 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
212 FuncStatus & flag) const
214 bool const known_branch =
215 buffer().params().branchlist().find(params_.branch);
217 switch (cmd.action()) {
218 case LFUN_INSET_MODIFY:
219 flag.setEnabled(true);
222 case LFUN_BRANCH_ACTIVATE:
223 flag.setEnabled(known_branch && !isBranchSelected(true));
226 case LFUN_BRANCH_INVERT:
227 flag.setEnabled(true);
230 case LFUN_BRANCH_ADD:
231 flag.setEnabled(!known_branch);
234 case LFUN_BRANCH_DEACTIVATE:
235 flag.setEnabled(isBranchSelected(true));
238 case LFUN_BRANCH_MASTER_ACTIVATE:
239 flag.setEnabled(buffer().parent()
240 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
241 && !isBranchSelected());
244 case LFUN_BRANCH_MASTER_DEACTIVATE:
245 flag.setEnabled(buffer().parent() && isBranchSelected());
248 case LFUN_INSET_TOGGLE:
249 if (cmd.argument() == "assign")
250 flag.setEnabled(true);
252 return InsetCollapsable::getStatus(cur, cmd, flag);
256 return InsetCollapsable::getStatus(cur, cmd, flag);
262 bool InsetBranch::isBranchSelected(bool const child) const
264 Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
265 BranchList const & branchlist = realbuffer.params().branchlist();
266 Branch const * ourBranch = branchlist.find(params_.branch);
269 // this branch is defined in child only
270 ourBranch = buffer().params().branchlist().find(params_.branch);
274 return ourBranch->isSelected();
278 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
280 if (isBranchActive())
281 InsetText::latex(os, runparams);
285 int InsetBranch::plaintext(odocstringstream & os,
286 OutputParams const & runparams, size_t max_length) const
288 if (!isBranchActive())
291 int len = InsetText::plaintext(os, runparams, max_length);
296 int InsetBranch::docbook(odocstream & os,
297 OutputParams const & runparams) const
299 return isBranchActive() ? InsetText::docbook(os, runparams) : 0;
303 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
305 if (isBranchActive()) {
306 OutputParams newrp = rp;
308 newrp.par_end = text().paragraphs().size();
309 xhtmlParagraphs(text(), buffer(), xs, newrp);
315 void InsetBranch::toString(odocstream & os) const
317 if (isBranchActive())
318 InsetCollapsable::toString(os);
322 void InsetBranch::forOutliner(docstring & os, size_t const maxlen,
323 bool const shorten) const
325 if (isBranchActive())
326 InsetCollapsable::forOutliner(os, maxlen, shorten);
330 void InsetBranch::validate(LaTeXFeatures & features) const
332 if (isBranchActive())
333 InsetCollapsable::validate(features);
337 string InsetBranch::contextMenuName() const
339 return "context-branch";
343 bool InsetBranch::isMacroScope() const
345 // Its own scope if not selected by buffer
346 return !isBranchActive();
350 string InsetBranch::params2string(InsetBranchParams const & params)
358 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
360 params = InsetBranchParams();
364 istringstream data(in);
367 lex.setContext("InsetBranch::string2params");
372 void InsetBranch::addToToc(DocIterator const & cpit, bool output_active,
373 UpdateType utype) const
375 DocIterator pit = cpit;
376 pit.push_back(CursorSlice(const_cast<InsetBranch &>(*this)));
379 text().forOutliner(str, TOC_ENTRY_LENGTH);
380 str = params_.branch + (params_.inverted ? " (-):" : ": ") + str;
382 shared_ptr<Toc> toc = buffer().tocBackend().toc("branch");
383 toc->push_back(TocItem(pit, 0, str, output_active));
385 // Proceed with the rest of the inset.
386 bool const doing_output = output_active && isBranchActive();
387 InsetCollapsable::addToToc(cpit, doing_output, utype);
391 void InsetBranchParams::write(ostream & os) const
393 os << to_utf8(branch)
400 void InsetBranchParams::read(Lexer & lex)
403 lex >> "inverted" >> inverted;