]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFlex.cpp
Implement IsTocCaption for InsetArgument
[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 #include "TocBackend.h"
27
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         : InsetCollapsable(buf), name_(layoutName)
40 {}
41
42
43 InsetFlex::InsetFlex(InsetFlex const & in)
44         : InsetCollapsable(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 InsetLayout::InsetDecoration InsetFlex::decoration() const
63 {
64         InsetLayout::InsetDecoration const dec = getLayout().decoration();
65         return dec == InsetLayout::DEFAULT ? InsetLayout::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         InsetCollapsable::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_DISSOLVE:
99                 if (!cmd.argument().empty()) {
100                         InsetLayout const & il = getLayout();
101                         InsetLayout::InsetLyXType const type = 
102                                 translateLyXType(to_utf8(cmd.argument()));
103                         if (il.lyxtype() == type) {
104                                 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
105                                 return InsetCollapsable::getStatus(cur, temp_cmd, flag);
106                         } else
107                                 return false;
108                 }
109                 // fall-through
110         default:
111                 return InsetCollapsable::getStatus(cur, cmd, flag);
112         }
113 }
114
115
116 void InsetFlex::doDispatch(Cursor & cur, FuncRequest & cmd)
117 {
118         switch (cmd.action()) {
119         case LFUN_INSET_DISSOLVE:
120                 if (!cmd.argument().empty()) {
121                         InsetLayout const & il = getLayout();
122                         InsetLayout::InsetLyXType const type = 
123                                 translateLyXType(to_utf8(cmd.argument()));
124                         
125                         if (il.lyxtype() == type) {
126                                 FuncRequest temp_cmd(LFUN_INSET_DISSOLVE);
127                                 InsetCollapsable::doDispatch(cur, temp_cmd);
128                         } else
129                                 cur.undispatched();
130                         break;
131                 }
132                 // fall-through
133         default:
134                 InsetCollapsable::doDispatch(cur, cmd);
135                 break;
136         }
137 }
138
139
140 void InsetFlex::updateBuffer(ParIterator const & it, UpdateType utype)
141 {
142         BufferParams const & bp = buffer().masterBuffer()->params();
143         InsetLayout const & il = getLayout();
144         docstring custom_label = translateIfPossible(il.labelstring());
145
146         Counters & cnts = bp.documentClass().counters();
147         docstring const & count = il.counter();
148         bool const have_counter = cnts.hasCounter(count);
149         if (have_counter) {
150                 cnts.step(count, utype);
151                 custom_label += ' ' +
152                         cnts.theCounter(count, it.paragraph().getParLanguage(bp)->code());
153         }
154         setLabel(custom_label);
155         
156         bool const save_counter = have_counter && utype == OutputUpdate;
157         if (save_counter) {
158                 // we assume the counter is local to this inset
159                 // if this turns out to be wrong in some case, we will
160                 // need a layout flag
161                 cnts.saveLastCounter();
162         }
163         InsetCollapsable::updateBuffer(it, utype);
164         if (save_counter)
165                 cnts.restoreLastCounter();
166 }
167
168
169 void InsetFlex::addToToc(DocIterator const & cpit, bool output_active,
170                          UpdateType utype) const
171 {
172         InsetLayout const & layout = getLayout();
173         if (layout.addToToc()) {
174                 TocBuilder & b = buffer().tocBackend().builder(layout.tocType());
175                 // Cursor inside the inset
176                 DocIterator pit = cpit;
177                 pit.push_back(CursorSlice(const_cast<InsetFlex &>(*this)));
178                 b.pushItem(pit, getLabel(), output_active);
179                 // Proceed with the rest of the inset.
180                 InsetCollapsable::addToToc(cpit, output_active, utype);
181                 if (layout.isTocCaption()) {
182                         docstring str;
183                         text().forOutliner(str, TOC_ENTRY_LENGTH);
184                         b.argumentItem(str);
185                 }
186                 b.pop();
187         } else
188                 InsetCollapsable::addToToc(cpit, output_active, utype);
189 }
190
191
192 } // namespace lyx