]> git.lyx.org Git - lyx.git/blob - src/TocBackend.cpp
00a55a987097d56475c809145c5bf1daa6e098a1
[lyx.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 "TextClass.h"
26
27 #include "insets/InsetOptArg.h"
28
29 #include "support/convert.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32
33 using namespace std;
34
35 namespace lyx {
36
37 ///////////////////////////////////////////////////////////////////////////
38 //
39 // TocItem implementation
40 //
41 ///////////////////////////////////////////////////////////////////////////
42
43 TocItem::TocItem(ParConstIterator const & par_it, int d, docstring const & s)
44         : par_it_(par_it), depth_(d), str_(s)
45 {
46 }
47
48
49 int TocItem::id() const
50 {
51         return par_it_->id();
52 }
53
54
55 int TocItem::depth() const
56 {
57         return depth_;
58 }
59
60
61 docstring const & TocItem::str() const
62 {
63         return str_;
64 }
65
66
67 docstring const TocItem::asString() const
68 {
69         return docstring(4 * depth_, ' ') + str_;
70 }
71
72
73 FuncRequest TocItem::action() const
74 {
75         return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
76 }
77
78
79 ///////////////////////////////////////////////////////////////////////////
80 //
81 // TocBackend implementation
82 //
83 ///////////////////////////////////////////////////////////////////////////
84
85 Toc const & TocBackend::toc(string const & type) const
86 {
87         // Is the type already supported?
88         TocList::const_iterator it = tocs_.find(type);
89         BOOST_ASSERT(it != tocs_.end());
90
91         return it->second;
92 }
93
94
95 Toc & TocBackend::toc(string const & type)
96 {
97         return tocs_[type];
98 }
99
100
101 void TocBackend::updateItem(ParConstIterator const & par_it)
102 {
103         if (toc("tableofcontents").empty()) {
104                 // FIXME: should not happen, 
105                 // a call to TocBackend::update() is missing somewhere
106                 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
107                 return;
108         }
109
110         BufferParams const & bufparams = buffer_->params();
111         const int min_toclevel = bufparams.textClass().min_toclevel();
112
113         TocIterator toc_item = item("tableofcontents", par_it);
114
115         docstring tocstring;
116
117         // For each paragraph, traverse its insets and let them add
118         // their toc items
119         InsetList::const_iterator it = toc_item->par_it_->insetList().begin();
120         InsetList::const_iterator end = toc_item->par_it_->insetList().end();
121         for (; it != end; ++it) {
122                 Inset & inset = *it->inset;
123                 if (inset.lyxCode() == OPTARG_CODE) {
124                         if (!tocstring.empty())
125                                 break;
126                         Paragraph const & par =
127                                 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
128                         if (!toc_item->par_it_->labelString().empty())
129                                 tocstring = toc_item->par_it_->labelString() + ' ';
130                         tocstring += par.asString(*buffer_, false);
131                         break;
132                 }
133         }
134
135         int const toclevel = toc_item->par_it_->layout()->toclevel;
136         if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
137                 && tocstring.empty()) 
138                         tocstring = toc_item->par_it_->asString(*buffer_, true);
139
140         const_cast<TocItem &>(*toc_item).str_ = tocstring;
141 }
142
143
144 void TocBackend::update()
145 {
146         tocs_.clear();
147
148         BufferParams const & bufparams = buffer_->params();
149         const int min_toclevel = bufparams.textClass().min_toclevel();
150
151         Toc & toc = tocs_["tableofcontents"];
152         ParConstIterator pit = buffer_->par_iterator_begin();
153         ParConstIterator end = buffer_->par_iterator_end();
154         for (; pit != end; ++pit) {
155
156                 // the string that goes to the toc (could be the optarg)
157                 docstring tocstring;
158
159                 // For each paragraph, traverse its insets and let them add
160                 // their toc items
161                 InsetList::const_iterator it = pit->insetList().begin();
162                 InsetList::const_iterator end = pit->insetList().end();
163                 for (; it != end; ++it) {
164                         Inset & inset = *it->inset;
165                         inset.addToToc(*buffer_, pit);
166                         switch (inset.lyxCode()) {
167                         case OPTARG_CODE: {
168                                 if (!tocstring.empty())
169                                         break;
170                                 Paragraph const & par =
171                                         *static_cast<InsetOptArg&>(inset).paragraphs().begin();
172                                 if (!pit->labelString().empty())
173                                         tocstring = pit->labelString() + ' ';
174                                 tocstring += par.asString(*buffer_, false);
175                                 break;
176                         }
177                         default:
178                                 break;
179                         }
180                 }
181
182                 /// now the toc entry for the paragraph
183                 int const toclevel = pit->layout()->toclevel;
184                 if (toclevel != Layout::NOT_IN_TOC
185                     && toclevel >= min_toclevel) {
186                         // insert this into the table of contents
187                         if (tocstring.empty())
188                                 tocstring = pit->asString(*buffer_, true);
189                         toc.push_back(TocItem(pit, toclevel - min_toclevel,
190                                 tocstring));
191                 }
192         }
193 }
194
195
196 TocIterator TocBackend::item(string const & type,
197                 ParConstIterator const & par_it) const
198 {
199         TocList::const_iterator toclist_it = tocs_.find(type);
200         // Is the type supported?
201         BOOST_ASSERT(toclist_it != tocs_.end());
202
203         Toc const & toc_vector = toclist_it->second;
204         TocIterator last = toc_vector.begin();
205         TocIterator it = toc_vector.end();
206         if (it == last)
207                 return it;
208
209         --it;
210
211         ParConstIterator par_it_text = par_it;
212         if (par_it_text.inMathed()) {
213                 // We are only interested in text so remove the math CursorSlice.
214                 while (par_it_text.inMathed())
215                         par_it_text.pop_back();
216         }
217
218         for (; it != last; --it) {
219                 // We verify that we don't compare contents of two
220                 // different document. This happens when you
221                 // have parent and child documents.
222                 if (&it->par_it_[0].inset() != &par_it_text[0].inset())
223                         continue;
224                 if (it->par_it_ <= par_it_text)
225                         return it;
226         }
227
228         // We are before the first Toc Item:
229         return last;
230 }
231
232
233 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
234 {
235         TocList::const_iterator cit = tocs_.find(type);
236         if (cit != tocs_.end()) {
237                 TocIterator ccit = cit->second.begin();
238                 TocIterator end = cit->second.end();
239                 for (; ccit != end; ++ccit)
240                         os << ccit->asString() << from_utf8("\n");
241         }
242 }
243
244
245 } // namespace lyx