]> git.lyx.org Git - features.git/blob - src/insets/InsetCaptionable.cpp
Enhancements and bugfixes to the TOCs
[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 "InsetCaptionable.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "FloatList.h"
22 #include "TextClass.h"
23 #include "TocBackend.h"
24
25 using namespace std;
26
27
28 namespace lyx {
29
30
31 void InsetCaptionable::setCaptionType(std::string const & type)
32 {
33         caption_type_ = type.empty() ? "senseless" : type;
34 }
35
36
37 /// common to InsetFloat and InsetWrap
38 docstring InsetCaptionable::floatName(string const & type) const
39 {
40         BufferParams const & bp = buffer().params();
41         FloatList const & floats = bp.documentClass().floats();
42         FloatList::const_iterator it = floats[type];
43         // FIXME UNICODE
44         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
45 }
46
47
48 void InsetCaptionable::addToToc(DocIterator const & cpit, bool output_active,
49                                                                 UpdateType utype) const
50 {
51         DocIterator pit = cpit;
52         pit.push_back(CursorSlice(const_cast<InsetCaptionable &>(*this)));
53         docstring str;
54         // Leave str empty if we generate for output (e.g. xhtml lists of figures).
55         // This ensures that there is a caption if and only if the string is
56         // non-empty.
57         if (utype != OutputUpdate)
58                 text().forOutliner(str, TOC_ENTRY_LENGTH);
59         shared_ptr<TocBuilder> b = buffer().tocBackend().builder(caption_type_);
60         b->pushItem(pit, str, output_active);
61         // Proceed with the rest of the inset.
62         InsetCollapsable::addToToc(cpit, output_active, utype);
63         b->pop();
64 }
65
66 void InsetCaptionable::updateBuffer(ParIterator const & it, UpdateType utype)
67 {
68         Counters & cnts =
69                 buffer().masterBuffer()->params().documentClass().counters();
70         string const saveflt = cnts.current_float();
71         bool const savesubflt = cnts.isSubfloat();
72         if (utype == OutputUpdate) {
73                 // counters are local to the float
74                 cnts.saveLastCounter();
75         }
76         bool const subflt = hasSubCaptions(it);
77         // floats can only embed subfloats of their own kind
78         if (subflt && !saveflt.empty() && saveflt != "senseless")
79                 setCaptionType(saveflt);
80         // Tell captions what the current float is
81         cnts.current_float(caption_type_);
82         cnts.isSubfloat(subflt);
83         InsetCollapsable::updateBuffer(it, utype);
84         // Restore counters
85         cnts.current_float(saveflt);
86         if (utype == OutputUpdate)
87                 cnts.restoreLastCounter();
88         cnts.isSubfloat(savesubflt);
89 }
90
91
92 bool InsetCaptionable::insetAllowed(InsetCode c) const
93 {
94         return (c == CAPTION_CODE) || InsetCollapsable::insetAllowed(c);
95 }
96
97
98 } // namespace lyx