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);
110 if (decoration() == InsetLayout::CLASSIC)
111 return isOpen(bv) ? s : getNewLabel(s);
113 return params_.branch + ": " + getNewLabel(s);
117 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
119 if (params_.branch.empty())
120 return Inset::backgroundColor(pi);
122 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
129 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
131 switch (cmd.action()) {
132 case LFUN_INSET_MODIFY: {
133 InsetBranchParams params;
134 InsetBranch::string2params(to_utf8(cmd.argument()), params);
136 cur.recordUndoInset(ATOMIC_UNDO, this);
137 params_.branch = params.branch;
138 // what we really want here is a TOC update, but that means
139 // a full buffer update
140 cur.forceBufferUpdate();
143 case LFUN_BRANCH_ACTIVATE:
144 case LFUN_BRANCH_DEACTIVATE:
145 case LFUN_BRANCH_MASTER_ACTIVATE:
146 case LFUN_BRANCH_MASTER_DEACTIVATE: {
147 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
148 || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
149 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
152 Branch * our_branch = buf->params().branchlist().find(params_.branch);
156 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
157 || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
158 if (our_branch->isSelected() != activate) {
159 // FIXME If the branch is in the master document, we cannot
160 // call recordUndo..., because the master may be hidden, and
161 // the code presently assumes that hidden documents can never
162 // be dirty. See GuiView::closeBufferAll(), for example.
163 // An option would be to check if the master is hidden.
166 buffer().undo().recordUndoFullDocument(cur);
168 // at least issue a warning for now (ugly, but better than dataloss).
169 frontend::Alert::warning(_("Branch state changes in master document"),
170 lyx::support::bformat(_("The state of the branch '%1$s' "
171 "was changed in the master file. "
172 "Please make sure to save the master."), params_.branch), true);
173 our_branch->setSelected(activate);
174 // cur.forceBufferUpdate() is not enough
179 case LFUN_BRANCH_ADD:
180 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
182 case LFUN_INSET_TOGGLE:
183 if (cmd.argument() == "assign")
184 setStatus(cur, isBranchSelected() ? Open : Collapsed);
186 InsetCollapsable::doDispatch(cur, cmd);
190 InsetCollapsable::doDispatch(cur, cmd);
196 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
197 FuncStatus & flag) const
199 bool const known_branch =
200 buffer().params().branchlist().find(params_.branch);
202 switch (cmd.action()) {
203 case LFUN_INSET_MODIFY:
204 flag.setEnabled(true);
207 case LFUN_BRANCH_ACTIVATE:
208 flag.setEnabled(known_branch && !isBranchSelected(true));
211 case LFUN_BRANCH_ADD:
212 flag.setEnabled(!known_branch);
215 case LFUN_BRANCH_DEACTIVATE:
216 flag.setEnabled(isBranchSelected(true));
219 case LFUN_BRANCH_MASTER_ACTIVATE:
220 flag.setEnabled(buffer().parent()
221 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
222 && !isBranchSelected());
225 case LFUN_BRANCH_MASTER_DEACTIVATE:
226 flag.setEnabled(buffer().parent() && isBranchSelected());
229 case LFUN_INSET_TOGGLE:
230 if (cmd.argument() == "assign")
231 flag.setEnabled(true);
233 return InsetCollapsable::getStatus(cur, cmd, flag);
237 return InsetCollapsable::getStatus(cur, cmd, flag);
243 bool InsetBranch::isBranchSelected(bool const child) const
245 Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
246 BranchList const & branchlist = realbuffer.params().branchlist();
247 Branch const * ourBranch = branchlist.find(params_.branch);
250 // this branch is defined in child only
251 ourBranch = buffer().params().branchlist().find(params_.branch);
255 return ourBranch->isSelected();
259 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
261 if (isBranchSelected())
262 InsetText::latex(os, runparams);
266 int InsetBranch::plaintext(odocstream & os,
267 OutputParams const & runparams) const
269 if (!isBranchSelected())
272 int len = InsetText::plaintext(os, runparams);
277 int InsetBranch::docbook(odocstream & os,
278 OutputParams const & runparams) const
280 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
284 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
286 if (isBranchSelected()) {
287 OutputParams newrp = rp;
289 newrp.par_end = text().paragraphs().size();
290 xhtmlParagraphs(text(), buffer(), xs, newrp);
296 void InsetBranch::toString(odocstream & os) const
298 if (isBranchSelected())
299 InsetCollapsable::toString(os);
303 void InsetBranch::forToc(docstring & os, size_t maxlen) const
305 if (isBranchSelected())
306 InsetCollapsable::forToc(os, maxlen);
310 void InsetBranch::validate(LaTeXFeatures & features) const
312 if (isBranchSelected())
313 InsetCollapsable::validate(features);
317 string InsetBranch::contextMenuName() const
319 return "context-branch";
323 bool InsetBranch::isMacroScope() const
325 // Its own scope if not selected by buffer
326 return !isBranchSelected();
330 string InsetBranch::params2string(InsetBranchParams const & params)
338 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
340 params = InsetBranchParams();
344 istringstream data(in);
347 lex.setContext("InsetBranch::string2params");
352 void InsetBranch::addToToc(DocIterator const & cpit) const
354 DocIterator pit = cpit;
355 pit.push_back(CursorSlice(const_cast<InsetBranch &>(*this)));
357 Toc & toc = buffer().tocBackend().toc("branch");
358 docstring str = params_.branch + ": ";
359 text().forToc(str, TOC_ENTRY_LENGTH);
360 toc.push_back(TocItem(pit, 0, str, toolTipText(docstring(), 3, 60)));
361 // Proceed with the rest of the inset.
362 InsetCollapsable::addToToc(cpit);
366 void InsetBranchParams::write(ostream & os) const
368 os << to_utf8(branch);
372 void InsetBranchParams::read(Lexer & lex)
375 branch = lex.getDocString();