]> git.lyx.org Git - features.git/blob - src/insets/InsetCaptionable.cpp
DocBook: refactor code about retrieving captions and labels.
[features.git] / src / insets / InsetCaptionable.cpp
1 /**
2  * \file InsetCaptionable.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Jürgen Vigna
8  * \author Lars Gullik Bjønnes
9  * \author Guillaume Munch
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "InsetBox.h"
17 #include "InsetCaptionable.h"
18 #include "InsetCaption.h"
19 #include "InsetLabel.h"
20
21 #include "Buffer.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"
29
30 #include "support/docstream.h"
31
32 using namespace std;
33
34
35 namespace lyx {
36
37
38 void InsetCaptionable::setCaptionType(std::string const & type)
39 {
40         caption_type_ = type.empty() ? "senseless" : type;
41 }
42
43
44 /// common to InsetFloat and InsetWrap
45 docstring InsetCaptionable::floatName(string const & type) const
46 {
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());
51 }
52
53
54 InsetCaption const * InsetCaptionable::getCaptionInset() const
55 {
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);
64                                 return ins;
65                         }
66                 }
67         }
68         return nullptr;
69 }
70
71
72 InsetLabel const * InsetCaptionable::getLabelInset() const
73 {
74         // A wrong hypothesis would be to limit the search to the caption: it is most likely there, but not necessarily!
75
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);
81
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)) {
84                                 continue;
85                         }
86
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);
90
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))
94                                 continue;
95
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);
104                                         }
105                                 }
106                         }
107
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.
110                 }
111         }
112
113         return nullptr;
114 }
115
116
117 docstring InsetCaptionable::getCaptionText(OutputParams const & runparams) const
118 {
119         InsetCaption const * ins = getCaptionInset();
120         if (!ins)
121                 return docstring();
122
123         odocstringstream ods;
124         ins->getCaptionAsPlaintext(ods, runparams);
125         return ods.str();
126 }
127
128
129 docstring InsetCaptionable::getCaptionDocBook(OutputParams const & runparams) const
130 {
131         InsetCaption const * ins = getCaptionInset();
132         if (ins == nullptr)
133                 return docstring();
134
135         odocstringstream ods;
136         XMLStream xs(ods);
137         ins->getCaptionAsDocBook(xs, runparams);
138         return ods.str();
139 }
140
141
142 docstring InsetCaptionable::getCaptionHTML(OutputParams const & runparams) const
143 {
144         InsetCaption const * ins = getCaptionInset();
145         if (ins == 0)
146                 return docstring();
147
148         odocstringstream ods;
149         XMLStream xs(ods);
150         docstring def = ins->getCaptionAsHTML(xs, runparams);
151         if (!def.empty())
152                 // should already have been escaped
153                 xs << XMLStream::ESCAPE_NONE << def << '\n';
154         return ods.str();
155 }
156
157
158 void InsetCaptionable::addToToc(DocIterator const & cpit, bool output_active,
159                                                                 UpdateType utype, TocBackend & backend) const
160 {
161         DocIterator pit = cpit;
162         pit.push_back(CursorSlice(const_cast<InsetCaptionable &>(*this)));
163         docstring str;
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
166         // non-empty.
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);
173         b.pop();
174 }
175
176 void InsetCaptionable::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
177 {
178         Counters & cnts =
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();
185         }
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);
194         // Restore counters
195         cnts.current_float(saveflt);
196         if (utype == OutputUpdate)
197                 cnts.restoreLastCounter();
198         cnts.isSubfloat(savesubflt);
199 }
200
201
202 bool InsetCaptionable::insetAllowed(InsetCode c) const
203 {
204         return (c == CAPTION_CODE) || InsetCollapsible::insetAllowed(c);
205 }
206
207
208 } // namespace lyx