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