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"
27 #include "support/debug.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
38 InsetFlex::InsetFlex(Buffer * buf, string const & layoutName)
39 : InsetCollapsible(buf), name_(layoutName)
43 InsetFlex::InsetFlex(InsetFlex const & in)
44 : InsetCollapsible(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 InsetDecoration InsetFlex::decoration() const
64 InsetDecoration const dec = getLayout().decoration();
65 return dec == InsetDecoration::DEFAULT ? InsetDecoration::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 InsetCollapsible::write(os);
94 bool InsetFlex::getStatus(Cursor & cur, FuncRequest const & cmd,
95 FuncStatus & flag) const
97 switch (cmd.action()) {
98 case LFUN_INSET_SPLIT:
99 case LFUN_INSET_DISSOLVE:
100 if (!cmd.argument().empty()) {
101 InsetLayout const & il = getLayout();
102 InsetLyXType const type =
103 translateLyXType(to_utf8(cmd.argument()));
104 if (il.lyxtype() == type
105 || (il.name() == DocumentClass::plainInsetLayout().name()
106 && type == InsetLyXType::CHARSTYLE)) {
107 FuncRequest temp_cmd(cmd.action());
108 return InsetCollapsible::getStatus(cur, temp_cmd, flag);
114 return InsetCollapsible::getStatus(cur, cmd, flag);
119 void InsetFlex::doDispatch(Cursor & cur, FuncRequest & cmd)
121 switch (cmd.action()) {
122 case LFUN_INSET_SPLIT:
123 case LFUN_INSET_DISSOLVE:
124 if (!cmd.argument().empty()) {
125 InsetLayout const & il = getLayout();
126 InsetLyXType const type =
127 translateLyXType(to_utf8(cmd.argument()));
129 if (il.lyxtype() == type
130 || (il.name() == DocumentClass::plainInsetLayout().name()
131 && type == InsetLyXType::CHARSTYLE)) {
132 FuncRequest temp_cmd(cmd.action());
133 InsetCollapsible::doDispatch(cur, temp_cmd);
140 InsetCollapsible::doDispatch(cur, cmd);
146 void InsetFlex::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
148 BufferParams const & bp = buffer().masterBuffer()->params();
149 InsetLayout const & il = getLayout();
150 docstring custom_label = translateIfPossible(il.labelstring());
152 Counters & cnts = bp.documentClass().counters();
154 // Special case for `subequations' module.
155 if (il.latextype() == InsetLaTeXType::ENVIRONMENT &&
156 il.latexname() == "subequations") {
157 docstring equation(from_ascii("equation"));
158 docstring parentequation(from_ascii("parentequation"));
160 cnts.step(equation, utype);
161 // save a copy of the equation counter definition
162 cnts.copy(equation, parentequation);
163 // redefine the equation counter definition
164 docstring const eqlabel = deleted ? from_ascii("#")
165 : cnts.theCounter(equation, it->getParLanguage(bp)->code());
166 cnts.newCounter(equation, parentequation,
167 eqlabel + from_ascii("\\alph{equation}"),
168 eqlabel + from_ascii("\\alph{equation}"),
169 cnts.guiName(parentequation));
170 InsetCollapsible::updateBuffer(it, utype, deleted);
171 // reset equation counter as it was.
172 cnts.copy(parentequation, equation);
173 cnts.remove(parentequation);
177 docstring const & count = il.counter();
178 bool const have_counter = cnts.hasCounter(count);
181 cnts.step(count, utype);
182 custom_label += ' ' +
183 cnts.theCounter(count, it.paragraph().getParLanguage(bp)->code());
185 custom_label += ' ' + from_ascii("#");
187 setLabel(custom_label);
189 bool const save_counter = have_counter && utype == OutputUpdate;
191 // we assume the counter is local to this inset
192 // if this turns out to be wrong in some case, we will
193 // need a layout flag
194 cnts.saveLastCounter();
196 InsetCollapsible::updateBuffer(it, utype, deleted);
198 cnts.restoreLastCounter();