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 & bv) 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
89 docstring s = _("Branch: ") + params_.branch;
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);
94 if (!inmaster && inchild)
95 s = _("Branch (child only): ") + params_.branch;
96 else if (inmaster && !inchild)
97 s = _("Branch (master only): ") + params_.branch;
99 s = _("Branch (undefined): ") + params_.branch;
100 if (!params_.branch.empty()) {
102 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
104 s = _("Undef: ") + s;
106 bool const master_selected = isBranchSelected();
107 bool const child_selected = isBranchSelected(true);
108 docstring symb = docstring(1, master_selected ? tick : cross);
109 if (inchild && master_selected != child_selected)
110 symb += child_selected ? tick : cross;
111 if (decoration() == InsetLayout::CLASSIC)
112 return symb + (isOpen(bv) ? s : getNewLabel(s));
114 return symb + params_.branch + ": " + getNewLabel(s);
118 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
120 if (params_.branch.empty())
121 return Inset::backgroundColor(pi);
123 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
130 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
132 switch (cmd.action()) {
133 case LFUN_INSET_MODIFY: {
134 InsetBranchParams params;
135 InsetBranch::string2params(to_utf8(cmd.argument()), params);
137 cur.recordUndoInset(this);
138 params_.branch = params.branch;
139 // what we really want here is a TOC update, but that means
140 // a full buffer update
141 cur.forceBufferUpdate();
144 case LFUN_BRANCH_ACTIVATE:
145 case LFUN_BRANCH_DEACTIVATE:
146 case LFUN_BRANCH_MASTER_ACTIVATE:
147 case LFUN_BRANCH_MASTER_DEACTIVATE: {
148 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
149 || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
150 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
153 Branch * our_branch = buf->params().branchlist().find(params_.branch);
157 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
158 || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
159 if (our_branch->isSelected() != activate) {
160 // FIXME If the branch is in the master document, we cannot
161 // call recordUndo..., because the master may be hidden, and
162 // the code presently assumes that hidden documents can never
163 // be dirty. See GuiView::closeBufferAll(), for example.
164 // An option would be to check if the master is hidden.
167 buffer().undo().recordUndoBufferParams(cur);
169 // at least issue a warning for now (ugly, but better than dataloss).
170 frontend::Alert::warning(_("Branch state changes in master document"),
171 lyx::support::bformat(_("The state of the branch '%1$s' "
172 "was changed in the master file. "
173 "Please make sure to save the master."), params_.branch), true);
174 our_branch->setSelected(activate);
175 // cur.forceBufferUpdate() is not enough
180 case LFUN_BRANCH_ADD:
181 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
183 case LFUN_INSET_TOGGLE:
184 if (cmd.argument() == "assign")
185 setStatus(cur, isBranchSelected() ? Open : Collapsed);
187 InsetCollapsable::doDispatch(cur, cmd);
191 InsetCollapsable::doDispatch(cur, cmd);
197 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
198 FuncStatus & flag) const
200 bool const known_branch =
201 buffer().params().branchlist().find(params_.branch);
203 switch (cmd.action()) {
204 case LFUN_INSET_MODIFY:
205 flag.setEnabled(true);
208 case LFUN_BRANCH_ACTIVATE:
209 flag.setEnabled(known_branch && !isBranchSelected(true));
212 case LFUN_BRANCH_ADD:
213 flag.setEnabled(!known_branch);
216 case LFUN_BRANCH_DEACTIVATE:
217 flag.setEnabled(isBranchSelected(true));
220 case LFUN_BRANCH_MASTER_ACTIVATE:
221 flag.setEnabled(buffer().parent()
222 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
223 && !isBranchSelected());
226 case LFUN_BRANCH_MASTER_DEACTIVATE:
227 flag.setEnabled(buffer().parent() && isBranchSelected());
230 case LFUN_INSET_TOGGLE:
231 if (cmd.argument() == "assign")
232 flag.setEnabled(true);
234 return InsetCollapsable::getStatus(cur, cmd, flag);
238 return InsetCollapsable::getStatus(cur, cmd, flag);
244 bool InsetBranch::isBranchSelected(bool const child) const
246 Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
247 BranchList const & branchlist = realbuffer.params().branchlist();
248 Branch const * ourBranch = branchlist.find(params_.branch);
251 // this branch is defined in child only
252 ourBranch = buffer().params().branchlist().find(params_.branch);
256 return ourBranch->isSelected();
260 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
262 if (isBranchSelected())
263 InsetText::latex(os, runparams);
267 int InsetBranch::plaintext(odocstringstream & os,
268 OutputParams const & runparams, size_t max_length) const
270 if (!isBranchSelected())
273 int len = InsetText::plaintext(os, runparams, max_length);
278 int InsetBranch::docbook(odocstream & os,
279 OutputParams const & runparams) const
281 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
285 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
287 if (isBranchSelected()) {
288 OutputParams newrp = rp;
290 newrp.par_end = text().paragraphs().size();
291 xhtmlParagraphs(text(), buffer(), xs, newrp);
297 void InsetBranch::toString(odocstream & os) const
299 if (isBranchSelected())
300 InsetCollapsable::toString(os);
304 void InsetBranch::forOutliner(docstring & os, size_t const maxlen,
305 bool const shorten) const
307 if (isBranchSelected())
308 InsetCollapsable::forOutliner(os, maxlen, shorten);
312 void InsetBranch::validate(LaTeXFeatures & features) const
314 if (isBranchSelected())
315 InsetCollapsable::validate(features);
319 string InsetBranch::contextMenuName() const
321 return "context-branch";
325 bool InsetBranch::isMacroScope() const
327 // Its own scope if not selected by buffer
328 return !isBranchSelected();
332 string InsetBranch::params2string(InsetBranchParams const & params)
340 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
342 params = InsetBranchParams();
346 istringstream data(in);
349 lex.setContext("InsetBranch::string2params");
354 void InsetBranch::addToToc(DocIterator const & cpit, bool output_active,
355 UpdateType utype) const
357 DocIterator pit = cpit;
358 pit.push_back(CursorSlice(const_cast<InsetBranch &>(*this)));
361 text().forOutliner(tooltip, TOC_ENTRY_LENGTH);
362 docstring str = params_.branch + ": " + tooltip;
363 tooltip = support::wrapParas(tooltip, 0, 60, 2);
365 shared_ptr<Toc> toc = buffer().tocBackend().toc("branch");
366 toc->push_back(TocItem(pit, 0, str, output_active, tooltip));
368 // Proceed with the rest of the inset.
369 bool const doing_output = output_active && isBranchSelected();
370 InsetCollapsable::addToToc(cpit, doing_output, utype);
374 void InsetBranchParams::write(ostream & os) const
376 os << to_utf8(branch);
380 void InsetBranchParams::read(Lexer & lex)
383 branch = lex.getDocString();