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"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
26 #include "OutputParams.h"
27 #include "output_xhtml.h"
28 #include "TextClass.h"
29 #include "TocBackend.h"
31 #include "support/debug.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
35 #include "frontends/alert.h"
36 #include "frontends/Application.h"
45 InsetBranch::InsetBranch(Buffer * buf, InsetBranchParams const & params)
46 : InsetCollapsible(buf, InsetText::DefaultLayout), params_(params)
50 void InsetBranch::write(ostream & os) const
55 InsetCollapsible::write(os);
59 void InsetBranch::read(Lexer & lex)
62 InsetCollapsible::read(lex);
66 docstring InsetBranch::toolTip(BufferView const & bv, int, int) const
68 docstring const masterstatus = isBranchSelected() ?
69 _("active") : _("non-active");
70 docstring const childstatus = isBranchSelected(true) ?
71 _("active") : _("non-active");
72 docstring const status = (masterstatus == childstatus) ?
74 support::bformat(_("master %1$s, child %2$s"),
75 masterstatus, childstatus);
77 docstring const masteron = producesOutput() ?
79 docstring const childon =
80 (isBranchSelected(true) != params_.inverted) ?
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);
93 return toolTipText(heading + from_ascii("\n"));
97 docstring const InsetBranch::buttonLabel(BufferView const &) const
99 static char_type const tick = 0x2714; // ✔ U+2714 HEAVY CHECK MARK
100 static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
102 Buffer const & realbuffer = *buffer().masterBuffer();
103 BranchList const & branchlist = realbuffer.params().branchlist();
104 bool const inmaster = branchlist.find(params_.branch);
105 bool const inchild = buffer().params().branchlist().find(params_.branch);
107 bool const master_selected = producesOutput();
108 bool const child_selected = isBranchSelected(true) != params_.inverted;
110 docstring symb = docstring(1, master_selected ? tick : cross);
111 if (inchild && master_selected != child_selected)
112 symb += (child_selected ? tick : cross);
114 if (decoration() == InsetLayout::MINIMALISTIC)
115 return symb + params_.branch;
118 if (inmaster && inchild)
120 else if (inchild) // && !inmaster
121 s = _("Branch (child): ");
122 else if (inmaster) // && !inchild
123 s = _("Branch (master): ");
124 else // !inmaster && !inchild
125 s = _("Branch (undefined): ");
132 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
134 if (params_.branch.empty())
135 return Inset::backgroundColor(pi);
137 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
144 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
146 switch (cmd.action()) {
147 case LFUN_INSET_MODIFY: {
148 InsetBranchParams params;
149 InsetBranch::string2params(to_utf8(cmd.argument()), params);
151 cur.recordUndoInset(this);
152 params_.branch = params.branch;
153 params_.inverted = params.inverted;
154 // what we really want here is a TOC update, but that means
155 // a full buffer update
156 cur.forceBufferUpdate();
159 case LFUN_BRANCH_ACTIVATE:
160 case LFUN_BRANCH_DEACTIVATE:
161 case LFUN_BRANCH_MASTER_ACTIVATE:
162 case LFUN_BRANCH_MASTER_DEACTIVATE: {
163 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
164 || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
165 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
168 Branch * our_branch = buf->params().branchlist().find(params_.branch);
172 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
173 || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
174 if (our_branch->isSelected() != activate) {
175 // FIXME If the branch is in the master document, we cannot
176 // call recordUndo..., because the master may be hidden, and
177 // the code presently assumes that hidden documents can never
178 // be dirty. See GuiView::closeBufferAll(), for example.
179 // An option would be to check if the master is hidden.
182 buffer().undo().recordUndoBufferParams(cur);
184 // at least issue a warning for now (ugly, but better than dataloss).
185 frontend::Alert::warning(_("Branch state changes in master document"),
186 lyx::support::bformat(_("The state of the branch '%1$s' "
187 "was changed in the master file. "
188 "Please make sure to save the master."), params_.branch), true);
189 our_branch->setSelected(activate);
190 // cur.forceBufferUpdate() is not enough
195 case LFUN_BRANCH_INVERT:
196 cur.recordUndoInset(this);
197 params_.inverted = !params_.inverted;
198 // what we really want here is a TOC update, but that means
199 // a full buffer update
200 cur.forceBufferUpdate();
202 case LFUN_BRANCH_ADD:
203 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
205 case LFUN_INSET_TOGGLE:
206 if (cmd.argument() == "assign")
207 setStatus(cur, isBranchSelected() ? Open : Collapsed);
209 InsetCollapsible::doDispatch(cur, cmd);
213 InsetCollapsible::doDispatch(cur, cmd);
219 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
220 FuncStatus & flag) const
222 bool const known_branch =
223 buffer().params().branchlist().find(params_.branch);
225 switch (cmd.action()) {
226 case LFUN_INSET_MODIFY:
227 flag.setEnabled(true);
230 case LFUN_BRANCH_ACTIVATE:
231 flag.setEnabled(known_branch && !isBranchSelected(true));
234 case LFUN_BRANCH_INVERT:
235 flag.setEnabled(true);
236 flag.setOnOff(params_.inverted);
239 case LFUN_BRANCH_ADD:
240 flag.setEnabled(!known_branch);
243 case LFUN_BRANCH_DEACTIVATE:
244 flag.setEnabled(isBranchSelected(true));
247 case LFUN_BRANCH_MASTER_ACTIVATE:
248 flag.setEnabled(buffer().parent()
249 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
250 && !isBranchSelected());
253 case LFUN_BRANCH_MASTER_DEACTIVATE:
254 flag.setEnabled(buffer().parent() && isBranchSelected());
257 case LFUN_INSET_TOGGLE:
258 if (cmd.argument() == "assign")
259 flag.setEnabled(true);
261 return InsetCollapsible::getStatus(cur, cmd, flag);
265 return InsetCollapsible::getStatus(cur, cmd, flag);
271 bool InsetBranch::isBranchSelected(bool const child) const
273 Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
274 BranchList const & branchlist = realbuffer.params().branchlist();
275 Branch const * ourBranch = branchlist.find(params_.branch);
278 // this branch is defined in child only
279 ourBranch = buffer().params().branchlist().find(params_.branch);
283 return ourBranch->isSelected();
287 bool InsetBranch::producesOutput() const
289 return isBranchSelected() != params_.inverted;
293 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
295 if (producesOutput())
296 InsetText::latex(os, runparams);
300 int InsetBranch::plaintext(odocstringstream & os,
301 OutputParams const & runparams, size_t max_length) const
303 if (!producesOutput())
306 int len = InsetText::plaintext(os, runparams, max_length);
311 int InsetBranch::docbook(odocstream & os,
312 OutputParams const & runparams) const
314 return producesOutput() ? InsetText::docbook(os, runparams) : 0;
318 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
320 if (producesOutput()) {
321 OutputParams newrp = rp;
323 newrp.par_end = text().paragraphs().size();
324 xhtmlParagraphs(text(), buffer(), xs, newrp);
330 void InsetBranch::toString(odocstream & os) const
332 if (producesOutput())
333 InsetCollapsible::toString(os);
337 void InsetBranch::forOutliner(docstring & os, size_t const maxlen,
338 bool const shorten) const
340 if (producesOutput())
341 InsetCollapsible::forOutliner(os, maxlen, shorten);
345 void InsetBranch::validate(LaTeXFeatures & features) const
347 if (producesOutput())
348 InsetCollapsible::validate(features);
352 string InsetBranch::contextMenuName() const
354 return "context-branch";
358 bool InsetBranch::isMacroScope() const
360 // Its own scope if not selected by buffer
361 return !producesOutput();
365 string InsetBranch::params2string(InsetBranchParams const & params)
373 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
375 params = InsetBranchParams();
379 istringstream data(in);
382 lex.setContext("InsetBranch::string2params");
387 void InsetBranch::updateBuffer(ParIterator const & it, UpdateType utype)
389 setLabel(params_.branch + (params_.inverted ? " (-)" : ""));
390 InsetCollapsible::updateBuffer(it, utype);
394 void InsetBranchParams::write(ostream & os) const
396 os << to_utf8(branch)
403 void InsetBranchParams::read(Lexer & lex)
405 // There may be a space in branch name
406 // if we wanted to use lex>>, the branch name should be properly in quotes
408 branch = lex.getDocString();
409 lex >> "inverted" >> inverted;