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 =
78 support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
81 return toolTipText(heading + from_ascii("\n"));
85 docstring const InsetBranch::buttonLabel(BufferView const &) const
87 static char_type const tick = 0x2714; // ✔ U+2714 HEAVY CHECK MARK
88 static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
90 Buffer const & realbuffer = *buffer().masterBuffer();
91 BranchList const & branchlist = realbuffer.params().branchlist();
92 bool const inmaster = branchlist.find(params_.branch);
93 bool const inchild = buffer().params().branchlist().find(params_.branch);
95 bool const master_selected = isBranchSelected();
96 bool const child_selected = isBranchSelected(true);
98 docstring symb = docstring(1, master_selected ? tick : cross);
99 if (inchild && master_selected != child_selected)
100 symb += child_selected ? tick : cross;
102 if (decoration() == InsetLayout::MINIMALISTIC)
103 return symb + params_.branch;
106 if (inmaster && inchild)
108 else if (inchild) // && !inmaster
109 s = _("Branch (child only): ");
110 else if (inmaster) // && !inchild
111 s = _("Branch (master only): ");
112 else // !inmaster && !inchild
113 s = _("Branch (undefined): ");
120 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
122 if (params_.branch.empty())
123 return Inset::backgroundColor(pi);
125 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
132 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
134 switch (cmd.action()) {
135 case LFUN_INSET_MODIFY: {
136 InsetBranchParams params;
137 InsetBranch::string2params(to_utf8(cmd.argument()), params);
139 cur.recordUndoInset(this);
140 params_.branch = params.branch;
141 // what we really want here is a TOC update, but that means
142 // a full buffer update
143 cur.forceBufferUpdate();
146 case LFUN_BRANCH_ACTIVATE:
147 case LFUN_BRANCH_DEACTIVATE:
148 case LFUN_BRANCH_MASTER_ACTIVATE:
149 case LFUN_BRANCH_MASTER_DEACTIVATE: {
150 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
151 || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
152 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
155 Branch * our_branch = buf->params().branchlist().find(params_.branch);
159 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
160 || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
161 if (our_branch->isSelected() != activate) {
162 // FIXME If the branch is in the master document, we cannot
163 // call recordUndo..., because the master may be hidden, and
164 // the code presently assumes that hidden documents can never
165 // be dirty. See GuiView::closeBufferAll(), for example.
166 // An option would be to check if the master is hidden.
169 buffer().undo().recordUndoBufferParams(cur);
171 // at least issue a warning for now (ugly, but better than dataloss).
172 frontend::Alert::warning(_("Branch state changes in master document"),
173 lyx::support::bformat(_("The state of the branch '%1$s' "
174 "was changed in the master file. "
175 "Please make sure to save the master."), params_.branch), true);
176 our_branch->setSelected(activate);
177 // cur.forceBufferUpdate() is not enough
182 case LFUN_BRANCH_ADD:
183 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
185 case LFUN_INSET_TOGGLE:
186 if (cmd.argument() == "assign")
187 setStatus(cur, isBranchSelected() ? Open : Collapsed);
189 InsetCollapsable::doDispatch(cur, cmd);
193 InsetCollapsable::doDispatch(cur, cmd);
199 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
200 FuncStatus & flag) const
202 bool const known_branch =
203 buffer().params().branchlist().find(params_.branch);
205 switch (cmd.action()) {
206 case LFUN_INSET_MODIFY:
207 flag.setEnabled(true);
210 case LFUN_BRANCH_ACTIVATE:
211 flag.setEnabled(known_branch && !isBranchSelected(true));
214 case LFUN_BRANCH_ADD:
215 flag.setEnabled(!known_branch);
218 case LFUN_BRANCH_DEACTIVATE:
219 flag.setEnabled(isBranchSelected(true));
222 case LFUN_BRANCH_MASTER_ACTIVATE:
223 flag.setEnabled(buffer().parent()
224 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
225 && !isBranchSelected());
228 case LFUN_BRANCH_MASTER_DEACTIVATE:
229 flag.setEnabled(buffer().parent() && isBranchSelected());
232 case LFUN_INSET_TOGGLE:
233 if (cmd.argument() == "assign")
234 flag.setEnabled(true);
236 return InsetCollapsable::getStatus(cur, cmd, flag);
240 return InsetCollapsable::getStatus(cur, cmd, flag);
246 bool InsetBranch::isBranchSelected(bool const child) const
248 Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
249 BranchList const & branchlist = realbuffer.params().branchlist();
250 Branch const * ourBranch = branchlist.find(params_.branch);
253 // this branch is defined in child only
254 ourBranch = buffer().params().branchlist().find(params_.branch);
258 return ourBranch->isSelected();
262 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
264 if (isBranchSelected())
265 InsetText::latex(os, runparams);
269 int InsetBranch::plaintext(odocstringstream & os,
270 OutputParams const & runparams, size_t max_length) const
272 if (!isBranchSelected())
275 int len = InsetText::plaintext(os, runparams, max_length);
280 int InsetBranch::docbook(odocstream & os,
281 OutputParams const & runparams) const
283 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
287 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
289 if (isBranchSelected()) {
290 OutputParams newrp = rp;
292 newrp.par_end = text().paragraphs().size();
293 xhtmlParagraphs(text(), buffer(), xs, newrp);
299 void InsetBranch::toString(odocstream & os) const
301 if (isBranchSelected())
302 InsetCollapsable::toString(os);
306 void InsetBranch::forOutliner(docstring & os, size_t const maxlen,
307 bool const shorten) const
309 if (isBranchSelected())
310 InsetCollapsable::forOutliner(os, maxlen, shorten);
314 void InsetBranch::validate(LaTeXFeatures & features) const
316 if (isBranchSelected())
317 InsetCollapsable::validate(features);
321 string InsetBranch::contextMenuName() const
323 return "context-branch";
327 bool InsetBranch::isMacroScope() const
329 // Its own scope if not selected by buffer
330 return !isBranchSelected();
334 string InsetBranch::params2string(InsetBranchParams const & params)
342 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
344 params = InsetBranchParams();
348 istringstream data(in);
351 lex.setContext("InsetBranch::string2params");
356 void InsetBranch::addToToc(DocIterator const & cpit, bool output_active,
357 UpdateType utype) const
359 DocIterator pit = cpit;
360 pit.push_back(CursorSlice(const_cast<InsetBranch &>(*this)));
363 text().forOutliner(tooltip, TOC_ENTRY_LENGTH);
364 docstring str = params_.branch + ": " + tooltip;
365 tooltip = support::wrapParas(tooltip, 0, 60, 2);
367 shared_ptr<Toc> toc = buffer().tocBackend().toc("branch");
368 toc->push_back(TocItem(pit, 0, str, output_active, tooltip));
370 // Proceed with the rest of the inset.
371 bool const doing_output = output_active && isBranchSelected();
372 InsetCollapsable::addToToc(cpit, doing_output, utype);
376 void InsetBranchParams::write(ostream & os) const
378 os << to_utf8(branch);
382 void InsetBranchParams::read(Lexer & lex)
385 branch = lex.getDocString();