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 docstring s = _("Branch: ") + params_.branch;
88 Buffer const & realbuffer = *buffer().masterBuffer();
89 BranchList const & branchlist = realbuffer.params().branchlist();
90 bool const inmaster = branchlist.find(params_.branch);
91 bool const inchild = buffer().params().branchlist().find(params_.branch);
92 if (!inmaster && inchild)
93 s = _("Branch (child only): ") + params_.branch;
94 else if (inmaster && !inchild)
95 s = _("Branch (master only): ") + params_.branch;
97 s = _("Branch (undefined): ") + params_.branch;
98 if (!params_.branch.empty()) {
100 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
102 s = _("Undef: ") + s;
104 bool const master_selected = isBranchSelected();
105 bool const child_selected = isBranchSelected(true);
106 docstring symb = docstring(1, char_type(master_selected ? 0x2714 : 0x2716));
107 if (inchild && master_selected != child_selected)
108 symb += char_type(child_selected ? 0x2714 : 0x2716);
109 if (decoration() == InsetLayout::CLASSIC)
110 return symb + (isOpen(bv) ? s : getNewLabel(s));
112 return symb + params_.branch + ": " + getNewLabel(s);
116 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
118 if (params_.branch.empty())
119 return Inset::backgroundColor(pi);
121 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
128 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
130 switch (cmd.action()) {
131 case LFUN_INSET_MODIFY: {
132 InsetBranchParams params;
133 InsetBranch::string2params(to_utf8(cmd.argument()), params);
135 cur.recordUndoInset(ATOMIC_UNDO, this);
136 params_.branch = params.branch;
137 // what we really want here is a TOC update, but that means
138 // a full buffer update
139 cur.forceBufferUpdate();
142 case LFUN_BRANCH_ACTIVATE:
143 case LFUN_BRANCH_DEACTIVATE:
144 case LFUN_BRANCH_MASTER_ACTIVATE:
145 case LFUN_BRANCH_MASTER_DEACTIVATE: {
146 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
147 || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
148 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
151 Branch * our_branch = buf->params().branchlist().find(params_.branch);
155 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
156 || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
157 if (our_branch->isSelected() != activate) {
158 // FIXME If the branch is in the master document, we cannot
159 // call recordUndo..., because the master may be hidden, and
160 // the code presently assumes that hidden documents can never
161 // be dirty. See GuiView::closeBufferAll(), for example.
162 // An option would be to check if the master is hidden.
165 buffer().undo().recordUndoFullDocument(cur);
167 // at least issue a warning for now (ugly, but better than dataloss).
168 frontend::Alert::warning(_("Branch state changes in master document"),
169 lyx::support::bformat(_("The state of the branch '%1$s' "
170 "was changed in the master file. "
171 "Please make sure to save the master."), params_.branch), true);
172 our_branch->setSelected(activate);
173 // cur.forceBufferUpdate() is not enough
178 case LFUN_BRANCH_ADD:
179 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
181 case LFUN_INSET_TOGGLE:
182 if (cmd.argument() == "assign")
183 setStatus(cur, isBranchSelected() ? Open : Collapsed);
185 InsetCollapsable::doDispatch(cur, cmd);
189 InsetCollapsable::doDispatch(cur, cmd);
195 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
196 FuncStatus & flag) const
198 bool const known_branch =
199 buffer().params().branchlist().find(params_.branch);
201 switch (cmd.action()) {
202 case LFUN_INSET_MODIFY:
203 flag.setEnabled(true);
206 case LFUN_BRANCH_ACTIVATE:
207 flag.setEnabled(known_branch && !isBranchSelected(true));
210 case LFUN_BRANCH_ADD:
211 flag.setEnabled(!known_branch);
214 case LFUN_BRANCH_DEACTIVATE:
215 flag.setEnabled(isBranchSelected(true));
218 case LFUN_BRANCH_MASTER_ACTIVATE:
219 flag.setEnabled(buffer().parent()
220 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
221 && !isBranchSelected());
224 case LFUN_BRANCH_MASTER_DEACTIVATE:
225 flag.setEnabled(buffer().parent() && isBranchSelected());
228 case LFUN_INSET_TOGGLE:
229 if (cmd.argument() == "assign")
230 flag.setEnabled(true);
232 return InsetCollapsable::getStatus(cur, cmd, flag);
236 return InsetCollapsable::getStatus(cur, cmd, flag);
242 bool InsetBranch::isBranchSelected(bool const child) const
244 Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
245 BranchList const & branchlist = realbuffer.params().branchlist();
246 Branch const * ourBranch = branchlist.find(params_.branch);
249 // this branch is defined in child only
250 ourBranch = buffer().params().branchlist().find(params_.branch);
254 return ourBranch->isSelected();
258 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
260 if (isBranchSelected())
261 InsetText::latex(os, runparams);
265 int InsetBranch::plaintext(odocstream & os,
266 OutputParams const & runparams) const
268 if (!isBranchSelected())
271 int len = InsetText::plaintext(os, runparams);
276 int InsetBranch::docbook(odocstream & os,
277 OutputParams const & runparams) const
279 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
283 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
285 if (isBranchSelected()) {
286 OutputParams newrp = rp;
288 newrp.par_end = text().paragraphs().size();
289 xhtmlParagraphs(text(), buffer(), xs, newrp);
295 void InsetBranch::toString(odocstream & os) const
297 if (isBranchSelected())
298 InsetCollapsable::toString(os);
302 void InsetBranch::forToc(docstring & os, size_t maxlen) const
304 if (isBranchSelected())
305 InsetCollapsable::forToc(os, maxlen);
309 void InsetBranch::validate(LaTeXFeatures & features) const
311 if (isBranchSelected())
312 InsetCollapsable::validate(features);
316 string InsetBranch::contextMenuName() const
318 return "context-branch";
322 bool InsetBranch::isMacroScope() const
324 // Its own scope if not selected by buffer
325 return !isBranchSelected();
329 string InsetBranch::params2string(InsetBranchParams const & params)
337 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
339 params = InsetBranchParams();
343 istringstream data(in);
346 lex.setContext("InsetBranch::string2params");
351 void InsetBranch::addToToc(DocIterator const & cpit) const
353 DocIterator pit = cpit;
354 pit.push_back(CursorSlice(const_cast<InsetBranch &>(*this)));
356 Toc & toc = buffer().tocBackend().toc("branch");
357 docstring str = params_.branch + ": ";
358 text().forToc(str, TOC_ENTRY_LENGTH);
359 toc.push_back(TocItem(pit, 0, str, toolTipText(docstring(), 3, 60)));
360 // Proceed with the rest of the inset.
361 InsetCollapsable::addToToc(cpit);
365 void InsetBranchParams::write(ostream & os) const
367 os << to_utf8(branch);
371 void InsetBranchParams::read(Lexer & lex)
374 branch = lex.getDocString();