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