]> git.lyx.org Git - lyx.git/blob - src/TocBackend.cpp
77929a5f5d05615fe67f13dc77a5e60e3419e585
[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(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(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                         //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
166                         inset.addToToc(pit);
167                         switch (inset.lyxCode()) {
168                         case OPTARG_CODE: {
169                                 if (!tocstring.empty())
170                                         break;
171                                 Paragraph const & par =
172                                         *static_cast<InsetOptArg&>(inset).paragraphs().begin();
173                                 if (!pit->labelString().empty())
174                                         tocstring = pit->labelString() + ' ';
175                                 tocstring += par.asString(false);
176                                 break;
177                         }
178                         default:
179                                 break;
180                         }
181                 }
182
183                 /// now the toc entry for the paragraph
184                 int const toclevel = pit->layout()->toclevel;
185                 if (toclevel != Layout::NOT_IN_TOC
186                     && toclevel >= min_toclevel) {
187                         // insert this into the table of contents
188                         if (tocstring.empty())
189                                 tocstring = pit->asString(true);
190                         toc.push_back(TocItem(pit, toclevel - min_toclevel,
191                                 tocstring));
192                 }
193         }
194 }
195
196
197 TocIterator TocBackend::item(string const & type,
198                 ParConstIterator const & par_it) const
199 {
200         TocList::const_iterator toclist_it = tocs_.find(type);
201         // Is the type supported?
202         BOOST_ASSERT(toclist_it != tocs_.end());
203
204         Toc const & toc_vector = toclist_it->second;
205         TocIterator last = toc_vector.begin();
206         TocIterator it = toc_vector.end();
207         if (it == last)
208                 return it;
209
210         --it;
211
212         ParConstIterator par_it_text = par_it;
213         if (par_it_text.inMathed()) {
214                 // We are only interested in text so remove the math CursorSlice.
215                 while (par_it_text.inMathed())
216                         par_it_text.pop_back();
217         }
218
219         for (; it != last; --it) {
220                 // We verify that we don't compare contents of two
221                 // different document. This happens when you
222                 // have parent and child documents.
223                 if (&it->par_it_[0].inset() != &par_it_text[0].inset())
224                         continue;
225                 if (it->par_it_ <= par_it_text)
226                         return it;
227         }
228
229         // We are before the first Toc Item:
230         return last;
231 }
232
233
234 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
235 {
236         TocList::const_iterator cit = tocs_.find(type);
237         if (cit != tocs_.end()) {
238                 TocIterator ccit = cit->second.begin();
239                 TocIterator end = cit->second.end();
240                 for (; ccit != end; ++ccit)
241                         os << ccit->asString() << from_utf8("\n");
242         }
243 }
244
245
246 } // namespace lyx