]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFlex.cpp
Merge branch 'master' into features/indexmacros
[lyx.git] / src / insets / InsetFlex.cpp
1 /**
2  * \file InsetFlex.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetFlex.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "Cursor.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "Language.h"
23 #include "Lexer.h"
24 #include "ParIterator.h"
25 #include "TextClass.h"
26
27 #include "support/debug.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30
31 #include <ostream>
32
33 using namespace std;
34
35 namespace lyx {
36
37
38 InsetFlex::InsetFlex(Buffer * buf, string const & layoutName)
39         : InsetCollapsible(buf), name_(layoutName)
40 {}
41
42
43 InsetFlex::InsetFlex(InsetFlex const & in)
44         : InsetCollapsible(in), name_(in.name_)
45 {}
46
47
48 // special code for InsetFlex when there is not the explicit Flex:: prefix
49 InsetLayout const & InsetFlex::getLayout() const
50 {
51         if (!buffer_)
52                 return DocumentClass::plainInsetLayout();
53
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_));
59 }
60
61
62 InsetDecoration InsetFlex::decoration() const
63 {
64         InsetDecoration const dec = getLayout().decoration();
65         return dec == InsetDecoration::DEFAULT ? InsetDecoration::CONGLOMERATE : dec;
66 }
67
68
69 void InsetFlex::write(ostream & os) const
70 {
71         os << "Flex ";
72         string name;
73         if (name_.empty())
74                 name = "undefined";
75         else {
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.
81                         name = name_;
82                 else {
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, ':');
87                 }
88         }
89         os << name << "\n";
90         InsetCollapsible::write(os);
91 }
92
93
94 bool InsetFlex::getStatus(Cursor & cur, FuncRequest const & cmd,
95                 FuncStatus & flag) const
96 {
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);
109                         } else
110                                 return false;
111                 }
112                 // fall-through
113         default:
114                 return InsetCollapsible::getStatus(cur, cmd, flag);
115         }
116 }
117
118
119 void InsetFlex::doDispatch(Cursor & cur, FuncRequest & cmd)
120 {
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()));
128
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);
134                         } else
135                                 cur.undispatched();
136                         break;
137                 }
138                 // fall-through
139         default:
140                 InsetCollapsible::doDispatch(cur, cmd);
141                 break;
142         }
143 }
144
145
146 void InsetFlex::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
147 {
148         BufferParams const & bp = buffer().masterBuffer()->params();
149         InsetLayout const & il = getLayout();
150         docstring custom_label = translateIfPossible(il.labelstring());
151
152         Counters & cnts = bp.documentClass().counters();
153
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"));
159                 if (!deleted)
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);
174                 return;
175         }
176
177         docstring const & count = il.counter();
178         bool const have_counter = cnts.hasCounter(count);
179         if (have_counter) {
180                 if (!deleted) {
181                         cnts.step(count, utype);
182                         custom_label += ' ' +
183                                 cnts.theCounter(count, it.paragraph().getParLanguage(bp)->code());
184                 } else
185                         custom_label += ' ' + from_ascii("#");
186         }
187         setLabel(custom_label);
188
189         bool const save_counter = have_counter && utype == OutputUpdate;
190         if (save_counter) {
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();
195         }
196         InsetCollapsible::updateBuffer(it, utype, deleted);
197         if (save_counter)
198                 cnts.restoreLastCounter();
199 }
200
201
202 } // namespace lyx