2 * \file InsetCaptionable.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * \author Lars Gullik Bjønnes
9 * \author Guillaume Munch
11 * Full author contact details are available in file CREDITS.
16 #include "InsetCaptionable.h"
18 #include "InsetCaption.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
23 #include "FloatList.h"
24 #include "InsetList.h"
25 #include "output_xhtml.h"
26 #include "TextClass.h"
27 #include "TocBackend.h"
29 #include "support/docstream.h"
37 void InsetCaptionable::setCaptionType(std::string const & type)
39 caption_type_ = type.empty() ? "senseless" : type;
43 /// common to InsetFloat and InsetWrap
44 docstring InsetCaptionable::floatName(string const & type) const
46 BufferParams const & bp = buffer().params();
47 FloatList const & floats = bp.documentClass().floats();
48 FloatList::const_iterator it = floats[type];
49 return (it == floats.end()) ? from_utf8(type) : bp.B_(it->second.name());
53 InsetCaption const * InsetCaptionable::getCaptionInset() const
55 ParagraphList::const_iterator pit = paragraphs().begin();
56 for (; pit != paragraphs().end(); ++pit) {
57 InsetList::const_iterator it = pit->insetList().begin();
58 for (; it != pit->insetList().end(); ++it) {
59 Inset & inset = *it->inset;
60 if (inset.lyxCode() == CAPTION_CODE) {
61 InsetCaption const * ins =
62 static_cast<InsetCaption const *>(it->inset);
71 docstring InsetCaptionable::getCaptionText(OutputParams const & runparams) const
73 InsetCaption const * ins = getCaptionInset();
78 ins->getCaptionAsPlaintext(ods, runparams);
83 docstring InsetCaptionable::getCaptionHTML(OutputParams const & runparams) const
85 InsetCaption const * ins = getCaptionInset();
91 docstring def = ins->getCaptionAsHTML(xs, runparams);
93 // should already have been escaped
94 xs << XHTMLStream::ESCAPE_NONE << def << '\n';
99 void InsetCaptionable::addToToc(DocIterator const & cpit, bool output_active,
100 UpdateType utype, TocBackend & backend) const
102 DocIterator pit = cpit;
103 pit.push_back(CursorSlice(const_cast<InsetCaptionable &>(*this)));
105 // Leave str empty if we generate for output (e.g. xhtml lists of figures).
106 // This ensures that there is a caption if and only if the string is
108 if (utype != OutputUpdate)
109 text().forOutliner(str, TOC_ENTRY_LENGTH);
110 TocBuilder & b = backend.builder(caption_type_);
111 b.pushItem(pit, str, output_active);
112 // Proceed with the rest of the inset.
113 InsetCollapsible::addToToc(cpit, output_active, utype, backend);
117 void InsetCaptionable::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
120 buffer().masterBuffer()->params().documentClass().counters();
121 string const saveflt = cnts.current_float();
122 bool const savesubflt = cnts.isSubfloat();
123 if (utype == OutputUpdate) {
124 // counters are local to the float
125 cnts.saveLastCounter();
127 bool const subflt = hasSubCaptions(it);
128 // floats can only embed subfloats of their own kind
129 if (subflt && !saveflt.empty() && saveflt != "senseless")
130 setCaptionType(saveflt);
131 // Tell captions what the current float is
132 cnts.current_float(caption_type_);
133 cnts.isSubfloat(subflt);
134 InsetCollapsible::updateBuffer(it, utype, deleted);
136 cnts.current_float(saveflt);
137 if (utype == OutputUpdate)
138 cnts.restoreLastCounter();
139 cnts.isSubfloat(savesubflt);
143 bool InsetCaptionable::insetAllowed(InsetCode c) const
145 return (c == CAPTION_CODE) || InsetCollapsible::insetAllowed(c);