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