]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFlex.cpp
We already allowed a counter declaraton for flex insets. We just
[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/gettext.h"
28
29 #include <ostream>
30
31 using namespace std;
32
33 namespace lyx {
34
35
36 InsetFlex::InsetFlex(Buffer * buf, string const & layoutName)
37         : InsetCollapsable(buf), name_(layoutName)
38 {}
39
40
41 InsetFlex::InsetFlex(InsetFlex const & in)
42         : InsetCollapsable(in), name_(in.name_)
43 {}
44
45
46 // special code for InsetFlex when there is not the explicit Flex:: prefix
47 InsetLayout const & InsetFlex::getLayout() const
48 {
49         if (!buffer_)
50                 return DocumentClass::plainInsetLayout();
51
52         DocumentClass const & dc = buffer().params().documentClass();
53         docstring const dname = from_utf8(name_); 
54         if (dc.hasInsetLayout(dname))
55                 return dc.insetLayout(dname);
56         return dc.insetLayout(from_utf8("Flex:" + name_));
57 }
58
59
60 bool InsetFlex::resetFontEdit() const
61 {
62         if (getLayout().resetsFont())
63                 return true;
64         return InsetCollapsable::resetFontEdit();
65 }
66
67
68 InsetLayout::InsetDecoration InsetFlex::decoration() const
69 {
70         InsetLayout::InsetDecoration const dec = getLayout().decoration();
71         return dec == InsetLayout::DEFAULT ? InsetLayout::CONGLOMERATE : dec;
72 }
73
74
75 void InsetFlex::write(ostream & os) const
76 {
77         os << "Flex " <<
78                 (name_.empty() ? "undefined" : name_) << "\n";
79         InsetCollapsable::write(os);
80 }
81
82
83 bool InsetFlex::getStatus(Cursor & cur, FuncRequest const & cmd,
84                 FuncStatus & flag) const
85 {
86         switch (cmd.action()) {
87         case LFUN_INSET_DISSOLVE:
88                 if (!cmd.argument().empty()) {
89                         InsetLayout const & il = getLayout();
90                         InsetLayout::InsetLyXType const type = 
91                                 translateLyXType(to_utf8(cmd.argument()));
92                         if (il.lyxtype() == type) {
93                                 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
94                                 return InsetCollapsable::getStatus(cur, temp_cmd, flag);
95                         } else
96                                 return false;
97                 }
98                 // fall-through
99         default:
100                 return InsetCollapsable::getStatus(cur, cmd, flag);
101         }
102 }
103
104
105 void InsetFlex::doDispatch(Cursor & cur, FuncRequest & cmd)
106 {
107         switch (cmd.action()) {
108         case LFUN_INSET_DISSOLVE:
109                 if (!cmd.argument().empty()) {
110                         InsetLayout const & il = getLayout();
111                         InsetLayout::InsetLyXType const type = 
112                                 translateLyXType(to_utf8(cmd.argument()));
113                         
114                         if (il.lyxtype() == type) {
115                                 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
116                                 InsetCollapsable::doDispatch(cur, temp_cmd);
117                         } else
118                                 cur.undispatched();
119                         break;
120                 }
121                 // fall-through
122         default:
123                 InsetCollapsable::doDispatch(cur, cmd);
124                 break;
125         }
126 }
127
128
129 void InsetFlex::updateBuffer(ParIterator const & it, UpdateType utype)
130 {
131         BufferParams const & bp = buffer().masterBuffer()->params();
132         Counters & cnts = bp.documentClass().counters();
133         if (utype == OutputUpdate) {
134                 // the counter is local to this inset
135                 cnts.saveLastCounter();
136         }
137         InsetLayout const & il = getLayout();
138         docstring const & count = il.counter();
139         docstring custom_label = translateIfPossible(il.labelstring());
140         if (cnts.hasCounter(count))
141                 cnts.step(count, utype);
142         custom_label += ' ' +
143                 cnts.theCounter(count, it.paragraph().getParLanguage(bp)->code());
144         setLabel(custom_label);
145         InsetCollapsable::updateBuffer(it, utype);
146         if (utype == OutputUpdate)
147                 cnts.restoreLastCounter();
148 }
149
150
151 } // namespace lyx