]> git.lyx.org Git - features.git/blob - src/TocBackend.cpp
Avoid a complete Toc reset in case when only a toc item update is needed.
[features.git] / src / TocBackend.cpp
1 /**
2  * \file TocBackend.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  * \author Angus Leeming
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "TocBackend.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "FloatList.h"
20 #include "FuncRequest.h"
21 #include "InsetList.h"
22 #include "Layout.h"
23 #include "LyXAction.h"
24 #include "Paragraph.h"
25 #include "ParIterator.h"
26 #include "TextClass.h"
27
28 #include "insets/InsetOptArg.h"
29
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33
34 #include "support/lassert.h"
35
36 using namespace std;
37
38
39 namespace lyx {
40
41 ///////////////////////////////////////////////////////////////////////////
42 //
43 // TocItem implementation
44 //
45 ///////////////////////////////////////////////////////////////////////////
46
47 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s)
48         : dit_(dit), depth_(d), str_(s)
49 {
50 }
51
52
53 int TocItem::id() const
54 {
55         return dit_.paragraph().id();
56 }
57
58
59 int TocItem::depth() const
60 {
61         return depth_;
62 }
63
64
65 docstring const & TocItem::str() const
66 {
67         return str_;
68 }
69
70
71 docstring const TocItem::asString() const
72 {
73         return docstring(4 * depth_, ' ') + str_;
74 }
75
76
77 FuncRequest TocItem::action() const
78 {
79         string const arg = convert<string>(dit_.paragraph().id())
80                 + ' ' + convert<string>(dit_.pos());
81         return FuncRequest(LFUN_PARAGRAPH_GOTO, arg);
82 }
83
84
85 ///////////////////////////////////////////////////////////////////////////
86 //
87 // TocBackend implementation
88 //
89 ///////////////////////////////////////////////////////////////////////////
90
91 Toc const & TocBackend::toc(string const & type) const
92 {
93         // Is the type already supported?
94         TocList::const_iterator it = tocs_.find(type);
95         LASSERT(it != tocs_.end(), /**/);
96
97         return it->second;
98 }
99
100
101 Toc & TocBackend::toc(string const & type)
102 {
103         return tocs_[type];
104 }
105
106
107 bool TocBackend::updateItem(DocIterator const & dit)
108 {
109 //      Inset * inset = dit.paragraph().inInset();
110 //      inset->addToToc(dit);
111         if (dit.paragraph().layout().toclevel == Layout::NOT_IN_TOC)
112                 return false;
113
114         if (toc("tableofcontents").empty()) {
115                 // FIXME: should not happen, 
116                 // a call to TocBackend::update() is missing somewhere
117                 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
118                 return false;
119         }
120
121         BufferParams const & bufparams = buffer_->params();
122         const int min_toclevel = bufparams.documentClass().min_toclevel();
123
124         TocIterator toc_item = item("tableofcontents", dit);
125
126         docstring tocstring;
127
128         // For each paragraph, traverse its insets and let them add
129         // their toc items
130         Paragraph & par = toc_item->dit_.paragraph();
131         InsetList::const_iterator it = par.insetList().begin();
132         InsetList::const_iterator end = par.insetList().end();
133         for (; it != end; ++it) {
134                 Inset & inset = *it->inset;
135                 if (inset.lyxCode() == OPTARG_CODE) {
136                         if (!tocstring.empty())
137                                 break;
138                         Paragraph const & inset_par =
139                                 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
140                         if (!par.labelString().empty())
141                                 tocstring = par.labelString() + ' ';
142                         tocstring += inset_par.asString();
143                         break;
144                 }
145         }
146
147         int const toclevel = par.layout().toclevel;
148         if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
149                 && tocstring.empty())
150                         tocstring = par.asString(AS_STR_LABEL);
151
152         const_cast<TocItem &>(*toc_item).str_ = tocstring;
153
154         buffer_->updateTocItem("tableofcontents", dit);
155         return true;
156 }
157
158
159 void TocBackend::update()
160 {
161         tocs_.clear();
162         DocIterator dit;
163         buffer_->inset().addToToc(dit);
164 }
165
166
167 TocIterator TocBackend::item(string const & type,
168                 DocIterator const & dit) const
169 {
170         TocList::const_iterator toclist_it = tocs_.find(type);
171         // Is the type supported?
172         LASSERT(toclist_it != tocs_.end(), /**/);
173         return toclist_it->second.item(dit);
174 }
175
176
177 TocIterator Toc::item(DocIterator const & dit) const
178 {
179         TocIterator last = begin();
180         TocIterator it = end();
181         if (it == last)
182                 return it;
183
184         --it;
185
186         DocIterator dit_text = dit;
187         if (dit_text.inMathed()) {
188                 // We are only interested in text so remove the math CursorSlice.
189                 while (dit_text.inMathed())
190                         dit_text.pop_back();
191         }
192
193         for (; it != last; --it) {
194                 // We verify that we don't compare contents of two
195                 // different document. This happens when you
196                 // have parent and child documents.
197                 if (&it->dit_[0].inset() != &dit_text[0].inset())
198                         continue;
199                 if (it->dit_ <= dit_text)
200                         return it;
201         }
202
203         // We are before the first Toc Item:
204         return last;
205 }
206
207
208 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
209 {
210         TocList::const_iterator cit = tocs_.find(type);
211         if (cit != tocs_.end()) {
212                 TocIterator ccit = cit->second.begin();
213                 TocIterator end = cit->second.end();
214                 for (; ccit != end; ++ccit)
215                         os << ccit->asString() << from_utf8("\n");
216         }
217 }
218
219
220 } // namespace lyx