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.
17 #include "InsetCaptionable.h"
18 #include "InsetCaption.h"
19 #include "InsetLabel.h"
22 #include "BufferParams.h"
23 #include "BufferView.h"
24 #include "FloatList.h"
25 #include "InsetList.h"
26 #include "output_xhtml.h"
27 #include "TextClass.h"
28 #include "TocBackend.h"
30 #include "support/docstream.h"
38 void InsetCaptionable::setCaptionType(std::string const & type)
40 caption_type_ = type.empty() ? "senseless" : type;
44 /// common to InsetFloat and InsetWrap
45 docstring InsetCaptionable::floatName(string const & type) const
47 BufferParams const & bp = buffer().params();
48 FloatList const & floats = bp.documentClass().floats();
49 FloatList::const_iterator it = floats[type];
50 return (it == floats.end()) ? from_utf8(type) : bp.B_(it->second.name());
54 InsetCaption const * InsetCaptionable::getCaptionInset() const
56 ParagraphList::const_iterator pit = paragraphs().begin();
57 for (; pit != paragraphs().end(); ++pit) {
58 InsetList::const_iterator it = pit->insetList().begin();
59 for (; it != pit->insetList().end(); ++it) {
60 Inset & inset = *it->inset;
61 if (inset.lyxCode() == CAPTION_CODE) {
62 InsetCaption const * ins =
63 static_cast<InsetCaption const *>(it->inset);
72 InsetLabel const * InsetCaptionable::getLabelInset() const
74 // A wrong hypothesis would be to limit the search to the caption: it is most likely there, but not necessarily!
76 // Iterate through the contents of the inset.
77 auto const end = paragraphs().end();
78 for (auto par = paragraphs().begin(); par != end; ++par) {
79 for (pos_type pos = 0; pos < par->size(); ++pos) {
80 const Inset * inset = par->getInset(pos);
82 // If this inset is a subfigure, skip it. Otherwise, you would return the label for the subfigure.
83 if (dynamic_cast<const InsetBox *>(inset)) {
87 // Maybe an inset is directly a label, in which case no more work is needed.
88 if (inset && dynamic_cast<const InsetLabel *>(inset))
89 return dynamic_cast<const InsetLabel *>(inset);
91 // More likely, the label is hidden in an inset of a paragraph (only if a subtype of InsetText). Thus,
92 // dig into that text.
93 if (!dynamic_cast<const InsetText *>(inset))
96 auto insetAsText = dynamic_cast<const InsetText *>(inset);
97 auto itIn = insetAsText->paragraphs().begin();
98 auto endIn = insetAsText->paragraphs().end();
99 for (; itIn != endIn; ++itIn) {
100 for (pos_type posIn = 0; posIn < itIn->size(); ++posIn) {
101 const Inset *insetIn = itIn->getInset(posIn);
102 if (insetIn && dynamic_cast<const InsetLabel *>(insetIn)) {
103 return dynamic_cast<const InsetLabel *>(insetIn);
108 // Obviously, this solution does not scale with more levels of paragraphs and insets, but this should
109 // be enough: it is only used in captions.
117 docstring InsetCaptionable::getCaptionText(OutputParams const & runparams) const
119 InsetCaption const * ins = getCaptionInset();
123 odocstringstream ods;
124 ins->getCaptionAsPlaintext(ods, runparams);
129 docstring InsetCaptionable::getCaptionDocBook(OutputParams const & runparams) const
131 InsetCaption const * ins = getCaptionInset();
135 odocstringstream ods;
137 ins->getCaptionAsDocBook(xs, runparams);
142 docstring InsetCaptionable::getCaptionHTML(OutputParams const & runparams) const
144 InsetCaption const * ins = getCaptionInset();
148 odocstringstream ods;
150 docstring def = ins->getCaptionAsHTML(xs, runparams);
152 // should already have been escaped
153 xs << XMLStream::ESCAPE_NONE << def << '\n';
158 void InsetCaptionable::addToToc(DocIterator const & cpit, bool output_active,
159 UpdateType utype, TocBackend & backend) const
161 DocIterator pit = cpit;
162 pit.push_back(CursorSlice(const_cast<InsetCaptionable &>(*this)));
164 // Leave str empty if we generate for output (e.g. xhtml lists of figures).
165 // This ensures that there is a caption if and only if the string is
167 if (utype != OutputUpdate)
168 text().forOutliner(str, TOC_ENTRY_LENGTH);
169 TocBuilder & b = backend.builder(caption_type_);
170 b.pushItem(pit, str, output_active);
171 // Proceed with the rest of the inset.
172 InsetCollapsible::addToToc(cpit, output_active, utype, backend);
176 void InsetCaptionable::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
179 buffer().masterBuffer()->params().documentClass().counters();
180 string const saveflt = cnts.current_float();
181 bool const savesubflt = cnts.isSubfloat();
182 if (utype == OutputUpdate) {
183 // counters are local to the float
184 cnts.saveLastCounter();
186 bool const subflt = hasSubCaptions(it);
187 // floats can only embed subfloats of their own kind
188 if (subflt && !saveflt.empty() && saveflt != "senseless")
189 setCaptionType(saveflt);
190 // Tell captions what the current float is
191 cnts.current_float(caption_type_);
192 cnts.isSubfloat(subflt);
193 InsetCollapsible::updateBuffer(it, utype, deleted);
195 cnts.current_float(saveflt);
196 if (utype == OutputUpdate)
197 cnts.restoreLastCounter();
198 cnts.isSubfloat(savesubflt);
202 bool InsetCaptionable::insetAllowed(InsetCode c) const
204 return (c == CAPTION_CODE) || InsetCollapsible::insetAllowed(c);