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
71 InsetLayout const & il = getLayout();
75 // use il.name(), since this resolves obsoleted
77 string name = to_utf8(il.name());
78 // Remove the "Flex:" prefix, if it is present
79 if (support::prefixIs(name, "Flex:"))
80 name = support::token(name, ':', 1);
84 InsetCollapsable::write(os);
88 bool InsetFlex::getStatus(Cursor & cur, FuncRequest const & cmd,
89 FuncStatus & flag) const
91 switch (cmd.action()) {
92 case LFUN_INSET_DISSOLVE:
93 if (!cmd.argument().empty()) {
94 InsetLayout const & il = getLayout();
95 InsetLayout::InsetLyXType const type =
96 translateLyXType(to_utf8(cmd.argument()));
97 if (il.lyxtype() == type) {
98 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
99 return InsetCollapsable::getStatus(cur, temp_cmd, flag);
105 return InsetCollapsable::getStatus(cur, cmd, flag);
110 void InsetFlex::doDispatch(Cursor & cur, FuncRequest & cmd)
112 switch (cmd.action()) {
113 case LFUN_INSET_DISSOLVE:
114 if (!cmd.argument().empty()) {
115 InsetLayout const & il = getLayout();
116 InsetLayout::InsetLyXType const type =
117 translateLyXType(to_utf8(cmd.argument()));
119 if (il.lyxtype() == type) {
120 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
121 InsetCollapsable::doDispatch(cur, temp_cmd);
128 InsetCollapsable::doDispatch(cur, cmd);
134 void InsetFlex::updateBuffer(ParIterator const & it, UpdateType utype)
136 BufferParams const & bp = buffer().masterBuffer()->params();
137 InsetLayout const & il = getLayout();
138 docstring custom_label = translateIfPossible(il.labelstring());
140 Counters & cnts = bp.documentClass().counters();
141 docstring const & count = il.counter();
142 bool const have_counter = cnts.hasCounter(count);
144 cnts.step(count, utype);
145 custom_label += ' ' +
146 cnts.theCounter(count, it.paragraph().getParLanguage(bp)->code());
148 setLabel(custom_label);
150 bool const save_counter = have_counter && utype == OutputUpdate;
152 // we assume the counter is local to this inset
153 // if this turns out to be wrong in some case, we will
154 // need a layout flag
155 cnts.saveLastCounter();
157 InsetCollapsable::updateBuffer(it, utype);
159 cnts.restoreLastCounter();