]> git.lyx.org Git - features.git/blob - src/TocBackend.cpp
potential compile fix
[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 "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
34 namespace lyx {
35
36 ///////////////////////////////////////////////////////////////////////////
37 //
38 // TocItem implementation
39 //
40 ///////////////////////////////////////////////////////////////////////////
41
42 TocItem::TocItem(ParConstIterator const & par_it, int d,
43                 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(std::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 void TocBackend::updateItem(ParConstIterator const & par_it)
96 {
97         if (toc("tableofcontents").empty()) {
98                 // FIXME: should not happen, 
99                 // a call to TocBackend::update() is missing somewhere
100                 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
101                 return;
102         }
103
104         BufferParams const & bufparams = buffer_->params();
105         const int min_toclevel = bufparams.getTextClass().min_toclevel();
106
107         TocIterator toc_item = item("tableofcontents", par_it);
108
109         docstring tocstring;
110
111         // For each paragraph, traverse its insets and let them add
112         // their toc items
113         InsetList::const_iterator it = toc_item->par_it_->insetList().begin();
114         InsetList::const_iterator end = toc_item->par_it_->insetList().end();
115         for (; it != end; ++it) {
116                 Inset & inset = *it->inset;
117                 if (inset.lyxCode() == OPTARG_CODE) {
118                         if (!tocstring.empty())
119                                 break;
120                         Paragraph const & par =
121                                 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
122                         if (!toc_item->par_it_->getLabelstring().empty())
123                                 tocstring = toc_item->par_it_->getLabelstring() + ' ';
124                         tocstring += par.asString(*buffer_, false);
125                         break;
126                 }
127         }
128
129         int const toclevel = toc_item->par_it_->layout()->toclevel;
130         if (toclevel != Layout::NOT_IN_TOC
131             && toclevel >= min_toclevel
132                 && tocstring.empty())
133                         tocstring = toc_item->par_it_->asString(*buffer_, true);
134
135         const_cast<TocItem &>(*toc_item).str_ = tocstring;
136 }
137
138
139 void TocBackend::update()
140 {
141         tocs_.clear();
142
143         BufferParams const & bufparams = buffer_->params();
144         const int min_toclevel = bufparams.getTextClass().min_toclevel();
145
146         Toc & toc = tocs_["tableofcontents"];
147         ParConstIterator pit = buffer_->par_iterator_begin();
148         ParConstIterator end = buffer_->par_iterator_end();
149         for (; pit != end; ++pit) {
150
151                 // the string that goes to the toc (could be the optarg)
152                 docstring tocstring;
153
154                 // For each paragraph, traverse its insets and let them add
155                 // their toc items
156                 InsetList::const_iterator it = pit->insetList().begin();
157                 InsetList::const_iterator end = pit->insetList().end();
158                 for (; it != end; ++it) {
159                         Inset & inset = *it->inset;
160                         inset.addToToc(tocs_, *buffer_, pit);
161                         switch (inset.lyxCode()) {
162                         case OPTARG_CODE: {
163                                 if (!tocstring.empty())
164                                         break;
165                                 Paragraph const & par =
166                                         *static_cast<InsetOptArg&>(inset).paragraphs().begin();
167                                 if (!pit->getLabelstring().empty())
168                                         tocstring = pit->getLabelstring() + ' ';
169                                 tocstring += par.asString(*buffer_, false);
170                                 break;
171                         }
172                         default:
173                                 break;
174                         }
175                 }
176
177                 /// now the toc entry for the paragraph
178                 int const toclevel = pit->layout()->toclevel;
179                 if (toclevel != Layout::NOT_IN_TOC
180                     && toclevel >= min_toclevel) {
181                         // insert this into the table of contents
182                         if (tocstring.empty())
183                                 tocstring = pit->asString(*buffer_, true);
184                         toc.push_back(TocItem(pit, toclevel - min_toclevel,
185                                 tocstring));
186                 }
187         }
188 }
189
190
191 TocIterator const TocBackend::item(std::string const & type,
192                 ParConstIterator const & par_it) const
193 {
194         TocList::const_iterator toclist_it = tocs_.find(type);
195         // Is the type supported?
196         BOOST_ASSERT(toclist_it != tocs_.end());
197
198         Toc const & toc_vector = toclist_it->second;
199         TocIterator last = toc_vector.begin();
200         TocIterator it = toc_vector.end();
201         if (it == last)
202                 return it;
203
204         --it;
205
206         ParConstIterator par_it_text = par_it;
207         if (par_it_text.inMathed())
208                 // It would be better to do
209                 //   par_it_text.backwardInset();
210                 // but this method does not exist.
211                 while (par_it_text.inMathed())
212                         par_it_text.backwardPos();
213
214         for (; it != last; --it) {
215                 // We verify that we don't compare contents of two
216                 // different document. This happens when you
217                 // have parent and child documents.
218                 if (&it->par_it_[0].inset() != &par_it_text[0].inset())
219                         continue;
220                 if (it->par_it_ <= par_it_text)
221                         return it;
222         }
223
224         // We are before the first Toc Item:
225         return last;
226 }
227
228
229 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
230 {
231         TocList::const_iterator cit = tocs_.find(type);
232         if (cit != tocs_.end()) {
233                 TocIterator ccit = cit->second.begin();
234                 TocIterator end = cit->second.end();
235                 for (; ccit != end; ++ccit)
236                         os << ccit->asString() << from_utf8("\n");
237         }
238 }
239
240
241 } // namespace lyx