]> git.lyx.org Git - features.git/blob - src/TocBackend.cpp
Inset::addToToc(): change signature. Use DocIterator instead of ParConstIterator...
[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         return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
80 }
81
82
83 ///////////////////////////////////////////////////////////////////////////
84 //
85 // TocBackend implementation
86 //
87 ///////////////////////////////////////////////////////////////////////////
88
89 Toc const & TocBackend::toc(string const & type) const
90 {
91         // Is the type already supported?
92         TocList::const_iterator it = tocs_.find(type);
93         LASSERT(it != tocs_.end(), /**/);
94
95         return it->second;
96 }
97
98
99 Toc & TocBackend::toc(string const & type)
100 {
101         return tocs_[type];
102 }
103
104
105 void TocBackend::updateItem(DocIterator const & dit)
106 {
107         if (toc("tableofcontents").empty()) {
108                 // FIXME: should not happen, 
109                 // a call to TocBackend::update() is missing somewhere
110                 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
111                 return;
112         }
113
114         BufferParams const & bufparams = buffer_->params();
115         const int min_toclevel = bufparams.documentClass().min_toclevel();
116
117         TocIterator toc_item = item("tableofcontents", dit);
118
119         docstring tocstring;
120
121         // For each paragraph, traverse its insets and let them add
122         // their toc items
123         Paragraph & par = toc_item->dit_.paragraph();
124         InsetList::const_iterator it = par.insetList().begin();
125         InsetList::const_iterator end = par.insetList().end();
126         for (; it != end; ++it) {
127                 Inset & inset = *it->inset;
128                 if (inset.lyxCode() == OPTARG_CODE) {
129                         if (!tocstring.empty())
130                                 break;
131                         Paragraph const & inset_par =
132                                 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
133                         if (!par.labelString().empty())
134                                 tocstring = par.labelString() + ' ';
135                         tocstring += inset_par.asString(false);
136                         break;
137                 }
138         }
139
140         int const toclevel = par.layout().toclevel;
141         if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
142                 && tocstring.empty())
143                         tocstring = par.asString(true);
144
145         const_cast<TocItem &>(*toc_item).str_ = tocstring;
146 }
147
148
149 void TocBackend::update()
150 {
151         tocs_.clear();
152
153         BufferParams const & bufparams = buffer_->params();
154         const int min_toclevel = bufparams.documentClass().min_toclevel();
155
156         Toc & toc = tocs_["tableofcontents"];
157         ParConstIterator pit = buffer_->par_iterator_begin();
158         ParConstIterator end = buffer_->par_iterator_end();
159         for (; pit != end; ++pit) {
160
161                 // the string that goes to the toc (could be the optarg)
162                 docstring tocstring;
163
164                 // For each paragraph, traverse its insets and let them add
165                 // their toc items
166                 InsetList::const_iterator it = pit->insetList().begin();
167                 InsetList::const_iterator end = pit->insetList().end();
168                 for (; it != end; ++it) {
169                         Inset & inset = *it->inset;
170                         pit.pos() = it->pos;
171                         //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
172                         inset.addToToc(pit);
173                         switch (inset.lyxCode()) {
174                         case OPTARG_CODE: {
175                                 if (!tocstring.empty())
176                                         break;
177                                 Paragraph const & par =
178                                         *static_cast<InsetOptArg&>(inset).paragraphs().begin();
179                                 if (!pit->labelString().empty())
180                                         tocstring = pit->labelString() + ' ';
181                                 tocstring += par.asString(false);
182                                 break;
183                         }
184                         default:
185                                 break;
186                         }
187                 }
188
189                 /// now the toc entry for the paragraph
190                 int const toclevel = pit->layout().toclevel;
191                 if (toclevel != Layout::NOT_IN_TOC
192                     && toclevel >= min_toclevel) {
193                         // insert this into the table of contents
194                         if (tocstring.empty())
195                                 tocstring = pit->asString(true);
196                         toc.push_back(TocItem(pit, toclevel - min_toclevel,
197                                 tocstring));
198                 }
199         }
200 }
201
202
203 TocIterator TocBackend::item(string const & type,
204                 DocIterator const & dit) const
205 {
206         TocList::const_iterator toclist_it = tocs_.find(type);
207         // Is the type supported?
208         LASSERT(toclist_it != tocs_.end(), /**/);
209
210         Toc const & toc_vector = toclist_it->second;
211         TocIterator last = toc_vector.begin();
212         TocIterator it = toc_vector.end();
213         if (it == last)
214                 return it;
215
216         --it;
217
218         ParConstIterator par_it_text(dit);
219         if (par_it_text.inMathed()) {
220                 // We are only interested in text so remove the math CursorSlice.
221                 while (par_it_text.inMathed())
222                         par_it_text.pop_back();
223         }
224
225         for (; it != last; --it) {
226                 // We verify that we don't compare contents of two
227                 // different document. This happens when you
228                 // have parent and child documents.
229                 if (&it->dit_[0].inset() != &par_it_text[0].inset())
230                         continue;
231                 if (it->dit_ <= par_it_text)
232                         return it;
233         }
234
235         // We are before the first Toc Item:
236         return last;
237 }
238
239
240 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
241 {
242         TocList::const_iterator cit = tocs_.find(type);
243         if (cit != tocs_.end()) {
244                 TocIterator ccit = cit->second.begin();
245                 TocIterator end = cit->second.end();
246                 for (; ccit != end; ++ccit)
247                         os << ccit->asString() << from_utf8("\n");
248         }
249 }
250
251
252 } // namespace lyx