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