]> git.lyx.org Git - lyx.git/blob - src/TocBackend.cpp
Further cleanup of InsetFlex, InsetCollapsable and InsetLayout:
[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 "debug.h"
20 #include "FloatList.h"
21 #include "FuncRequest.h"
22 #include "InsetList.h"
23 #include "Layout.h"
24 #include "LyXAction.h"
25 #include "Paragraph.h"
26
27 #include "insets/InsetOptArg.h"
28
29 #include "support/convert.h"
30
31 using std::string;
32
33 namespace lyx {
34
35 ///////////////////////////////////////////////////////////////////////////
36 //
37 // TocItem implementation
38 //
39 ///////////////////////////////////////////////////////////////////////////
40
41 TocItem::TocItem(ParConstIterator const & par_it, int d,
42                 docstring const & s)
43                 : par_it_(par_it), depth_(d), str_(s)
44 {
45 }
46
47
48 int TocItem::id() const
49 {
50         return par_it_->id();
51 }
52
53
54 int TocItem::depth() const
55 {
56         return depth_;
57 }
58
59
60 docstring const & TocItem::str() const
61 {
62         return str_;
63 }
64
65
66 docstring const TocItem::asString() const
67 {
68         return docstring(4 * depth_, ' ') + str_;
69 }
70
71
72 FuncRequest TocItem::action() const
73 {
74         return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
75 }
76
77
78 ///////////////////////////////////////////////////////////////////////////
79 //
80 // TocBackend implementation
81 //
82 ///////////////////////////////////////////////////////////////////////////
83
84 Toc const & TocBackend::toc(std::string const & type) const
85 {
86         // Is the type already supported?
87         TocList::const_iterator it = tocs_.find(type);
88         BOOST_ASSERT(it != tocs_.end());
89
90         return it->second;
91 }
92
93
94 void TocBackend::updateItem(ParConstIterator const & par_it)
95 {
96         if (toc("tableofcontents").empty()) {
97                 // FIXME: should not happen, 
98                 // a call to TocBackend::update() is missing somewhere
99                 lyxerr << "TocBackend::updateItem called but the TOC is empty!"
100                         << std::endl;
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() << '\n';
237         }
238 }
239
240
241 } // namespace lyx