]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFlex.cpp
Whitespace
[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 #include "support/lstrings.h"
29
30 #include <ostream>
31
32 using namespace std;
33
34 namespace lyx {
35
36
37 InsetFlex::InsetFlex(Buffer * buf, string const & layoutName)
38         : InsetCollapsable(buf), name_(layoutName)
39 {}
40
41
42 InsetFlex::InsetFlex(InsetFlex const & in)
43         : InsetCollapsable(in), name_(in.name_)
44 {}
45
46
47 // special code for InsetFlex when there is not the explicit Flex:: prefix
48 InsetLayout const & InsetFlex::getLayout() const
49 {
50         if (!buffer_)
51                 return DocumentClass::plainInsetLayout();
52
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_));
58 }
59
60
61 InsetLayout::InsetDecoration InsetFlex::decoration() const
62 {
63         InsetLayout::InsetDecoration const dec = getLayout().decoration();
64         return dec == InsetLayout::DEFAULT ? InsetLayout::CONGLOMERATE : dec;
65 }
66
67
68 void InsetFlex::write(ostream & os) const
69 {
70         os << "Flex ";
71         InsetLayout const & il = getLayout();
72         if (name_.empty())
73                 os << "undefined";
74         else {
75                 // use il.name(), since this resolves obsoleted
76                 // InsetLayout names
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);
81                 os << name;
82         }
83         os << "\n";
84         InsetCollapsable::write(os);
85 }
86
87
88 bool InsetFlex::getStatus(Cursor & cur, FuncRequest const & cmd,
89                 FuncStatus & flag) const
90 {
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);
100                         } else
101                                 return false;
102                 }
103                 // fall-through
104         default:
105                 return InsetCollapsable::getStatus(cur, cmd, flag);
106         }
107 }
108
109
110 void InsetFlex::doDispatch(Cursor & cur, FuncRequest & cmd)
111 {
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()));
118                         
119                         if (il.lyxtype() == type) {
120                                 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
121                                 InsetCollapsable::doDispatch(cur, temp_cmd);
122                         } else
123                                 cur.undispatched();
124                         break;
125                 }
126                 // fall-through
127         default:
128                 InsetCollapsable::doDispatch(cur, cmd);
129                 break;
130         }
131 }
132
133
134 void InsetFlex::updateBuffer(ParIterator const & it, UpdateType utype)
135 {
136         BufferParams const & bp = buffer().masterBuffer()->params();
137         InsetLayout const & il = getLayout();
138         docstring custom_label = translateIfPossible(il.labelstring());
139
140         Counters & cnts = bp.documentClass().counters();
141         docstring const & count = il.counter();
142         bool const have_counter = cnts.hasCounter(count);
143         if (have_counter) {
144                 cnts.step(count, utype);
145                 custom_label += ' ' +
146                         cnts.theCounter(count, it.paragraph().getParLanguage(bp)->code());
147         }
148         setLabel(custom_label);
149         
150         bool const save_counter = have_counter && utype == OutputUpdate;
151         if (save_counter) {
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();
156         }
157         InsetCollapsable::updateBuffer(it, utype);
158         if (save_counter)
159                 cnts.restoreLastCounter();
160 }
161
162
163 } // namespace lyx