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