]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFlex.cpp
Fix conflicting inset font defaults (bug #8874)
[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 InsetLayout::InsetDecoration InsetFlex::decoration() const
61 {
62         InsetLayout::InsetDecoration const dec = getLayout().decoration();
63         return dec == InsetLayout::DEFAULT ? InsetLayout::CONGLOMERATE : dec;
64 }
65
66
67 void InsetFlex::write(ostream & os) const
68 {
69         os << "Flex " <<
70                 (name_.empty() ? "undefined" : name_) << "\n";
71         InsetCollapsable::write(os);
72 }
73
74
75 bool InsetFlex::getStatus(Cursor & cur, FuncRequest const & cmd,
76                 FuncStatus & flag) const
77 {
78         switch (cmd.action()) {
79         case LFUN_INSET_DISSOLVE:
80                 if (!cmd.argument().empty()) {
81                         InsetLayout const & il = getLayout();
82                         InsetLayout::InsetLyXType const type = 
83                                 translateLyXType(to_utf8(cmd.argument()));
84                         if (il.lyxtype() == type) {
85                                 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
86                                 return InsetCollapsable::getStatus(cur, temp_cmd, flag);
87                         } else
88                                 return false;
89                 }
90                 // fall-through
91         default:
92                 return InsetCollapsable::getStatus(cur, cmd, flag);
93         }
94 }
95
96
97 void InsetFlex::doDispatch(Cursor & cur, FuncRequest & cmd)
98 {
99         switch (cmd.action()) {
100         case LFUN_INSET_DISSOLVE:
101                 if (!cmd.argument().empty()) {
102                         InsetLayout const & il = getLayout();
103                         InsetLayout::InsetLyXType const type = 
104                                 translateLyXType(to_utf8(cmd.argument()));
105                         
106                         if (il.lyxtype() == type) {
107                                 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
108                                 InsetCollapsable::doDispatch(cur, temp_cmd);
109                         } else
110                                 cur.undispatched();
111                         break;
112                 }
113                 // fall-through
114         default:
115                 InsetCollapsable::doDispatch(cur, cmd);
116                 break;
117         }
118 }
119
120
121 void InsetFlex::updateBuffer(ParIterator const & it, UpdateType utype)
122 {
123         BufferParams const & bp = buffer().masterBuffer()->params();
124         InsetLayout const & il = getLayout();
125         docstring custom_label = translateIfPossible(il.labelstring());
126
127         Counters & cnts = bp.documentClass().counters();
128         docstring const & count = il.counter();
129         bool const have_counter = cnts.hasCounter(count);
130         if (have_counter) {
131                 cnts.step(count, utype);
132                 custom_label += ' ' +
133                         cnts.theCounter(count, it.paragraph().getParLanguage(bp)->code());
134         }
135         setLabel(custom_label);
136         
137         bool const save_counter = have_counter && utype == OutputUpdate;
138         if (save_counter) {
139                 // we assume the counter is local to this inset
140                 // if this turns out to be wrong in some case, we will
141                 // need a layout flag
142                 cnts.saveLastCounter();
143         }
144         InsetCollapsable::updateBuffer(it, utype);
145         if (save_counter)
146                 cnts.restoreLastCounter();
147 }
148
149
150 } // namespace lyx