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/gettext.h"
28 #include "support/lstrings.h"
37 InsetFlex::InsetFlex(Buffer * buf, string const & layoutName)
38 : InsetCollapsable(buf), name_(layoutName)
42 InsetFlex::InsetFlex(InsetFlex const & in)
43 : InsetCollapsable(in), name_(in.name_)
47 // special code for InsetFlex when there is not the explicit Flex:: prefix
48 InsetLayout const & InsetFlex::getLayout() const
51 return DocumentClass::plainInsetLayout();
53 DocumentClass const & dc = buffer().params().documentClass();
54 docstring const dname = from_utf8(name_);
55 if (dc.hasInsetLayout(dname))
56 return dc.insetLayout(dname);
57 return dc.insetLayout(from_utf8("Flex:" + name_));
61 InsetLayout::InsetDecoration InsetFlex::decoration() const
63 InsetLayout::InsetDecoration const dec = getLayout().decoration();
64 return dec == InsetLayout::DEFAULT ? InsetLayout::CONGLOMERATE : dec;
68 void InsetFlex::write(ostream & os) const
75 InsetLayout const & il = getLayout();
76 // use il.name(), since this resolves obsoleted InsetLayout names
77 if (il.name() == "undefined")
78 // This is the name of the plain_insetlayout_. We assume that the
79 // name resolution has failed.
82 name = to_utf8(il.name());
83 // Remove the "Flex:" prefix, if it is present
84 if (support::prefixIs(name, "Flex:"))
85 name = support::split(name, ':');
89 InsetCollapsable::write(os);
93 bool InsetFlex::getStatus(Cursor & cur, FuncRequest const & cmd,
94 FuncStatus & flag) const
96 switch (cmd.action()) {
97 case LFUN_INSET_DISSOLVE:
98 if (!cmd.argument().empty()) {
99 InsetLayout const & il = getLayout();
100 InsetLayout::InsetLyXType const type =
101 translateLyXType(to_utf8(cmd.argument()));
102 if (il.lyxtype() == type) {
103 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
104 return InsetCollapsable::getStatus(cur, temp_cmd, flag);
110 return InsetCollapsable::getStatus(cur, cmd, flag);
115 void InsetFlex::doDispatch(Cursor & cur, FuncRequest & cmd)
117 switch (cmd.action()) {
118 case LFUN_INSET_DISSOLVE:
119 if (!cmd.argument().empty()) {
120 InsetLayout const & il = getLayout();
121 InsetLayout::InsetLyXType const type =
122 translateLyXType(to_utf8(cmd.argument()));
124 if (il.lyxtype() == type) {
125 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
126 InsetCollapsable::doDispatch(cur, temp_cmd);
133 InsetCollapsable::doDispatch(cur, cmd);
139 void InsetFlex::updateBuffer(ParIterator const & it, UpdateType utype)
141 BufferParams const & bp = buffer().masterBuffer()->params();
142 InsetLayout const & il = getLayout();
143 docstring custom_label = translateIfPossible(il.labelstring());
145 Counters & cnts = bp.documentClass().counters();
146 docstring const & count = il.counter();
147 bool const have_counter = cnts.hasCounter(count);
149 cnts.step(count, utype);
150 custom_label += ' ' +
151 cnts.theCounter(count, it.paragraph().getParLanguage(bp)->code());
153 setLabel(custom_label);
155 bool const save_counter = have_counter && utype == OutputUpdate;
157 // we assume the counter is local to this inset
158 // if this turns out to be wrong in some case, we will
159 // need a layout flag
160 cnts.saveLastCounter();
162 InsetCollapsable::updateBuffer(it, utype);
164 cnts.restoreLastCounter();