]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCaptionable.cpp
f90776ab0408541f1dd672470239359efbd7ac97
[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 "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) const
49 {
50         DocIterator pit = cpit;
51         pit.push_back(CursorSlice(const_cast<InsetCaptionable &>(*this)));
52         docstring str;
53         int length = output_active ? INT_MAX : TOC_ENTRY_LENGTH;
54         text().forOutliner(str, length);
55         shared_ptr<TocBuilder> b = buffer().tocBackend().builder(caption_type_);
56         b->pushItem(pit, str, output_active);
57         // Proceed with the rest of the inset.
58         InsetCollapsable::addToToc(cpit, output_active);
59         b->pop();
60 }
61
62 void InsetCaptionable::updateBuffer(ParIterator const & it, UpdateType utype)
63 {
64         Counters & cnts =
65                 buffer().masterBuffer()->params().documentClass().counters();
66         string const saveflt = cnts.current_float();
67         bool const savesubflt = cnts.isSubfloat();
68         if (utype == OutputUpdate) {
69                 // counters are local to the float
70                 cnts.saveLastCounter();
71         }
72         bool const subflt = hasSubCaptions(it);
73         // floats can only embed subfloats of their own kind
74         if (subflt && !saveflt.empty() && saveflt != "senseless")
75                 setCaptionType(saveflt);
76         // Tell captions what the current float is
77         cnts.current_float(caption_type_);
78         cnts.isSubfloat(subflt);
79         InsetCollapsable::updateBuffer(it, utype);
80         // Restore counters
81         cnts.current_float(saveflt);
82         if (utype == OutputUpdate)
83                 cnts.restoreLastCounter();
84         cnts.isSubfloat(savesubflt);
85 }
86
87
88 bool InsetCaptionable::insetAllowed(InsetCode c) const
89 {
90         return (c == CAPTION_CODE) || InsetCollapsable::insetAllowed(c);
91 }
92
93
94 } // namespace lyx