3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Martin Vermeer
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
15 #include "InsetFlex.h"
18 #include "BufferParams.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
24 #include "ParIterator.h"
25 #include "TextClass.h"
26 #include "TocBackend.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
38 InsetFlex::InsetFlex(Buffer * buf, string const & layoutName)
39 : InsetCollapsable(buf), name_(layoutName)
43 InsetFlex::InsetFlex(InsetFlex const & in)
44 : InsetCollapsable(in), name_(in.name_)
48 // special code for InsetFlex when there is not the explicit Flex:: prefix
49 InsetLayout const & InsetFlex::getLayout() const
52 return DocumentClass::plainInsetLayout();
54 DocumentClass const & dc = buffer().params().documentClass();
55 docstring const dname = from_utf8(name_);
56 if (dc.hasInsetLayout(dname))
57 return dc.insetLayout(dname);
58 return dc.insetLayout(from_utf8("Flex:" + name_));
62 InsetLayout::InsetDecoration InsetFlex::decoration() const
64 InsetLayout::InsetDecoration const dec = getLayout().decoration();
65 return dec == InsetLayout::DEFAULT ? InsetLayout::CONGLOMERATE : dec;
69 void InsetFlex::write(ostream & os) const
76 InsetLayout const & il = getLayout();
77 // use il.name(), since this resolves obsoleted InsetLayout names
78 if (il.name() == "undefined")
79 // This is the name of the plain_insetlayout_. We assume that the
80 // name resolution has failed.
83 name = to_utf8(il.name());
84 // Remove the "Flex:" prefix, if it is present
85 if (support::prefixIs(name, "Flex:"))
86 name = support::split(name, ':');
90 InsetCollapsable::write(os);
94 bool InsetFlex::getStatus(Cursor & cur, FuncRequest const & cmd,
95 FuncStatus & flag) const
97 switch (cmd.action()) {
98 case LFUN_INSET_DISSOLVE:
99 if (!cmd.argument().empty()) {
100 InsetLayout const & il = getLayout();
101 InsetLayout::InsetLyXType const type =
102 translateLyXType(to_utf8(cmd.argument()));
103 if (il.lyxtype() == type) {
104 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
105 return InsetCollapsable::getStatus(cur, temp_cmd, flag);
111 return InsetCollapsable::getStatus(cur, cmd, flag);
116 void InsetFlex::doDispatch(Cursor & cur, FuncRequest & cmd)
118 switch (cmd.action()) {
119 case LFUN_INSET_DISSOLVE:
120 if (!cmd.argument().empty()) {
121 InsetLayout const & il = getLayout();
122 InsetLayout::InsetLyXType const type =
123 translateLyXType(to_utf8(cmd.argument()));
125 if (il.lyxtype() == type) {
126 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
127 InsetCollapsable::doDispatch(cur, temp_cmd);
134 InsetCollapsable::doDispatch(cur, cmd);
140 void InsetFlex::updateBuffer(ParIterator const & it, UpdateType utype)
142 BufferParams const & bp = buffer().masterBuffer()->params();
143 InsetLayout const & il = getLayout();
144 docstring custom_label = translateIfPossible(il.labelstring());
146 Counters & cnts = bp.documentClass().counters();
147 docstring const & count = il.counter();
148 bool const have_counter = cnts.hasCounter(count);
150 cnts.step(count, utype);
151 custom_label += ' ' +
152 cnts.theCounter(count, it.paragraph().getParLanguage(bp)->code());
154 setLabel(custom_label);
156 bool const save_counter = have_counter && utype == OutputUpdate;
158 // we assume the counter is local to this inset
159 // if this turns out to be wrong in some case, we will
160 // need a layout flag
161 cnts.saveLastCounter();
163 InsetCollapsable::updateBuffer(it, utype);
165 cnts.restoreLastCounter();
169 void InsetFlex::addToToc(DocIterator const & cpit, bool output_active,
170 UpdateType utype) const
172 InsetLayout const & layout = getLayout();
173 if (layout.addToToc()) {
174 TocBuilder & b = buffer().tocBackend().builder(layout.tocType());
175 // Cursor inside the inset
176 DocIterator pit = cpit;
177 pit.push_back(CursorSlice(const_cast<InsetFlex &>(*this)));
178 b.pushItem(pit, getLabel(), output_active);
179 // Proceed with the rest of the inset.
180 InsetCollapsable::addToToc(cpit, output_active, utype);
181 if (layout.isTocCaption()) {
183 text().forOutliner(str, TOC_ENTRY_LENGTH);
188 InsetCollapsable::addToToc(cpit, output_active, utype);