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"
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/Application.h"
44 InsetBranch::InsetBranch(Buffer * buf, InsetBranchParams const & params)
45 : InsetCollapsable(buf, InsetText::DefaultLayout), params_(params)
49 void InsetBranch::write(ostream & os) const
54 InsetCollapsable::write(os);
58 void InsetBranch::read(Lexer & lex)
61 InsetCollapsable::read(lex);
65 docstring InsetBranch::toolTip(BufferView const & bv, int, int) const
67 docstring const masterstatus = isBranchSelected() ?
68 _("active") : _("non-active");
69 docstring const childstatus = isBranchSelected(true) ?
70 _("active") : _("non-active");
71 docstring const status = (masterstatus == childstatus) ?
73 support::bformat(_("master: %1$s, child: %2$s"),
74 masterstatus, childstatus);
75 docstring const heading =
76 support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
79 return toolTipText(heading + from_ascii("\n"));
83 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
85 docstring s = _("Branch: ") + params_.branch;
86 Buffer const & realbuffer = *buffer().masterBuffer();
87 BranchList const & branchlist = realbuffer.params().branchlist();
88 bool const inmaster = branchlist.find(params_.branch);
89 bool const inchild = buffer().params().branchlist().find(params_.branch);
90 if (!inmaster && inchild)
91 s = _("Branch (child only): ") + params_.branch;
92 else if (inmaster && !inchild)
93 s = _("Branch (master only): ") + params_.branch;
95 s = _("Branch (undefined): ") + params_.branch;
96 if (!params_.branch.empty()) {
98 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
100 s = _("Undef: ") + s;
102 bool const master_selected = isBranchSelected();
103 bool const child_selected = isBranchSelected(true);
104 docstring symb = docstring(1, char_type(master_selected ? 0x2714 : 0x2716));
105 if (inchild && master_selected != child_selected)
106 symb += char_type(child_selected ? 0x2714 : 0x2716);
108 if (decoration() == InsetLayout::CLASSIC)
109 return isOpen(bv) ? s : getNewLabel(s);
111 return params_.branch + ": " + getNewLabel(s);
115 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
117 if (params_.branch.empty())
118 return Inset::backgroundColor(pi);
120 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
127 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
129 switch (cmd.action()) {
130 case LFUN_INSET_MODIFY: {
131 InsetBranchParams params;
132 InsetBranch::string2params(to_utf8(cmd.argument()), params);
134 cur.recordUndoInset(ATOMIC_UNDO, this);
135 params_.branch = params.branch;
136 // what we really want here is a TOC update, but that means
137 // a full buffer update
138 cur.forceBufferUpdate();
141 case LFUN_BRANCH_ACTIVATE:
142 case LFUN_BRANCH_DEACTIVATE:
143 case LFUN_BRANCH_MASTER_ACTIVATE:
144 case LFUN_BRANCH_MASTER_DEACTIVATE: {
145 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
146 || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
147 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
150 Branch * our_branch = buf->params().branchlist().find(params_.branch);
154 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
155 || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
156 if (our_branch->isSelected() != activate) {
157 // FIXME If the branch is in the master document, we cannot
158 // call recordUndo..., because the master may be hidden, and
159 // the code presently assumes that hidden documents can never
160 // be dirty. See GuiView::closeBufferAll(), for example.
162 buffer().undo().recordUndoFullDocument(cur);
163 our_branch->setSelected(activate);
164 cur.forceBufferUpdate();
168 case LFUN_INSET_TOGGLE:
169 if (cmd.argument() == "assign")
170 setStatus(cur, isBranchSelected() ? Open : Collapsed);
172 InsetCollapsable::doDispatch(cur, cmd);
176 InsetCollapsable::doDispatch(cur, cmd);
182 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
183 FuncStatus & flag) const
185 switch (cmd.action()) {
186 case LFUN_INSET_MODIFY:
187 flag.setEnabled(true);
190 case LFUN_BRANCH_ACTIVATE:
191 flag.setEnabled(!isBranchSelected(true));
194 case LFUN_BRANCH_DEACTIVATE:
195 flag.setEnabled(isBranchSelected(true));
198 case LFUN_BRANCH_MASTER_ACTIVATE:
199 flag.setEnabled(buffer().parent() && !isBranchSelected());
202 case LFUN_BRANCH_MASTER_DEACTIVATE:
203 flag.setEnabled(buffer().parent() && isBranchSelected());
206 case LFUN_INSET_TOGGLE:
207 if (cmd.argument() == "assign")
208 flag.setEnabled(true);
210 return InsetCollapsable::getStatus(cur, cmd, flag);
214 return InsetCollapsable::getStatus(cur, cmd, flag);
220 bool InsetBranch::isBranchSelected(bool const child) const
222 Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
223 BranchList const & branchlist = realbuffer.params().branchlist();
224 Branch const * ourBranch = branchlist.find(params_.branch);
227 // this branch is defined in child only
228 ourBranch = buffer().params().branchlist().find(params_.branch);
232 return ourBranch->isSelected();
236 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
238 if (isBranchSelected())
239 InsetText::latex(os, runparams);
243 int InsetBranch::plaintext(odocstream & os,
244 OutputParams const & runparams) const
246 if (!isBranchSelected())
249 int len = InsetText::plaintext(os, runparams);
254 int InsetBranch::docbook(odocstream & os,
255 OutputParams const & runparams) const
257 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
261 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
263 if (isBranchSelected()) {
264 OutputParams newrp = rp;
266 newrp.par_end = text().paragraphs().size();
267 xhtmlParagraphs(text(), buffer(), xs, newrp);
273 void InsetBranch::toString(odocstream & os) const
275 if (isBranchSelected())
276 InsetCollapsable::toString(os);
280 void InsetBranch::forToc(docstring & os, size_t maxlen) const
282 if (isBranchSelected())
283 InsetCollapsable::forToc(os, maxlen);
287 void InsetBranch::validate(LaTeXFeatures & features) const
289 if (isBranchSelected())
290 InsetCollapsable::validate(features);
294 string InsetBranch::contextMenuName() const
296 return "context-branch";
300 bool InsetBranch::isMacroScope() const
302 // Its own scope if not selected by buffer
303 return !isBranchSelected();
307 string InsetBranch::params2string(InsetBranchParams const & params)
315 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
317 params = InsetBranchParams();
321 istringstream data(in);
324 lex.setContext("InsetBranch::string2params");
329 void InsetBranch::addToToc(DocIterator const & cpit) const
331 DocIterator pit = cpit;
332 pit.push_back(CursorSlice(const_cast<InsetBranch &>(*this)));
334 Toc & toc = buffer().tocBackend().toc("branch");
335 docstring str = params_.branch + ": ";
336 text().forToc(str, TOC_ENTRY_LENGTH);
337 toc.push_back(TocItem(pit, 0, str, toolTipText(docstring(), 3, 60)));
338 // Proceed with the rest of the inset.
339 InsetCollapsable::addToToc(cpit);
343 void InsetBranchParams::write(ostream & os) const
345 os << to_utf8(branch);
349 void InsetBranchParams::read(Lexer & lex)
352 branch = lex.getDocString();