]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCaptionable.cpp
Fix trailing whitespace in cpp files.
[lyx.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 "InsetCaptionable.h"
17
18 #include "InsetCaption.h"
19
20 #include "Buffer.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"
28
29 #include "support/docstream.h"
30
31 using namespace std;
32
33
34 namespace lyx {
35
36
37 void InsetCaptionable::setCaptionType(std::string const & type)
38 {
39         caption_type_ = type.empty() ? "senseless" : type;
40 }
41
42
43 /// common to InsetFloat and InsetWrap
44 docstring InsetCaptionable::floatName(string const & type) const
45 {
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());
50 }
51
52
53 InsetCaption const * InsetCaptionable::getCaptionInset() const
54 {
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);
63                                 return ins;
64                         }
65                 }
66         }
67         return 0;
68 }
69
70
71 docstring InsetCaptionable::getCaptionText(OutputParams const & runparams) const
72 {
73         InsetCaption const * ins = getCaptionInset();
74         if (ins == 0)
75                 return docstring();
76
77         odocstringstream ods;
78         ins->getCaptionAsPlaintext(ods, runparams);
79         return ods.str();
80 }
81
82
83 docstring InsetCaptionable::getCaptionHTML(OutputParams const & runparams) const
84 {
85         InsetCaption const * ins = getCaptionInset();
86         if (ins == 0)
87                 return docstring();
88
89         odocstringstream ods;
90         XHTMLStream xs(ods);
91         docstring def = ins->getCaptionAsHTML(xs, runparams);
92         if (!def.empty())
93                 // should already have been escaped
94                 xs << XHTMLStream::ESCAPE_NONE << def << '\n';
95         return ods.str();
96 }
97
98
99 void InsetCaptionable::addToToc(DocIterator const & cpit, bool output_active,
100                                                                 UpdateType utype, TocBackend & backend) const
101 {
102         DocIterator pit = cpit;
103         pit.push_back(CursorSlice(const_cast<InsetCaptionable &>(*this)));
104         docstring str;
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
107         // non-empty.
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         InsetCollapsable::addToToc(cpit, output_active, utype, backend);
114         b.pop();
115 }
116
117 void InsetCaptionable::updateBuffer(ParIterator const & it, UpdateType utype)
118 {
119         Counters & cnts =
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();
126         }
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         InsetCollapsable::updateBuffer(it, utype);
135         // Restore counters
136         cnts.current_float(saveflt);
137         if (utype == OutputUpdate)
138                 cnts.restoreLastCounter();
139         cnts.isSubfloat(savesubflt);
140 }
141
142
143 bool InsetCaptionable::insetAllowed(InsetCode c) const
144 {
145         return (c == CAPTION_CODE) || InsetCollapsable::insetAllowed(c);
146 }
147
148
149 } // namespace lyx