]> git.lyx.org Git - lyx.git/blob - src/TocBackend.C
Improve fix for bug 3305
[lyx.git] / src / TocBackend.C
1 /**
2  * \file TocBackend.C
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 "LyXAction.h"
22 #include "paragraph.h"
23 #include "debug.h"
24
25 #include "insets/insetoptarg.h"
26
27 #include "support/convert.h"
28
29
30 namespace lyx {
31
32 using std::vector;
33 using std::string;
34
35
36 ///////////////////////////////////////////////////////////////////////////
37 // TocItem implementation
38
39 TocItem::TocItem(ParConstIterator const & par_it, int d,
40                 docstring const & s)
41                 : par_it_(par_it), depth_(d), str_(s)
42 {
43 /*
44         if (!uid_.empty())
45                 return;
46
47         size_t pos = s.find(" ");
48         if (pos == string::npos) {
49                 // Non labelled item
50                 uid_ = s;
51                 return;
52         }
53
54         string s2 = s.substr(0, pos);
55
56         if (s2 == "Chapter" || s2 == "Part") {
57                 size_t pos2 = s.find(" ", pos + 1);
58                 if (pos2 == string::npos) {
59                         // Unnumbered Chapter?? This should not happen.
60                         uid_ = s.substr(pos + 1);
61                         return;
62                 }
63                 // Chapter or Part
64                 uid_ = s.substr(pos2 + 1);
65                 return;
66         }
67         // Numbered Item.
68         uid_ = s.substr(pos + 1);
69         */
70 }
71
72 bool const TocItem::isValid() const
73 {
74         return depth_ != -1;
75 }
76
77
78 int const TocItem::id() const
79 {
80         return par_it_->id();
81 }
82
83
84 int const TocItem::depth() const
85 {
86         return depth_;
87 }
88
89
90 docstring const & TocItem::str() const
91 {
92         return str_;
93 }
94
95
96 docstring const TocItem::asString() const
97 {
98         return docstring(4 * depth_, ' ') + str_;
99 }
100
101
102 FuncRequest TocItem::action() const
103 {
104         return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
105 }
106
107
108
109
110
111 ///////////////////////////////////////////////////////////////////////////
112 // TocBackend implementation
113
114 Toc const & TocBackend::toc(std::string const & type) const
115 {
116         // Is the type already supported?
117         TocList::const_iterator it = tocs_.find(type);
118         BOOST_ASSERT(it != tocs_.end());
119
120         return it->second;
121 }
122
123
124 bool TocBackend::addType(std::string const & type)
125 {
126         // Is the type already supported?
127         TocList::iterator toclist_it = tocs_.find(type);
128         if (toclist_it != tocs_.end())
129                 return false;
130
131         tocs_.insert(make_pair(type, Toc()));
132         types_.push_back(type);
133
134         return true;
135 }
136
137 void TocBackend::updateItem(ParConstIterator const & par_it)
138 {
139         BufferParams const & bufparams = buffer_->params();
140         const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
141
142         TocIterator toc_item = item("tableofcontents", par_it);
143
144         docstring tocstring;
145
146         // For each paragraph, traverse its insets and let them add
147         // their toc items
148         InsetList::const_iterator it = toc_item->par_it_->insetlist.begin();
149         InsetList::const_iterator end = toc_item->par_it_->insetlist.end();
150         for (; it != end; ++it) {
151                 InsetBase & inset = *it->inset;
152                 if (inset.lyxCode() == InsetBase::OPTARG_CODE) {
153                         if (!tocstring.empty())
154                                 break;
155                         Paragraph const & par = 
156                                 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
157                         if (!toc_item->par_it_->getLabelstring().empty())
158                                 tocstring = toc_item->par_it_->getLabelstring() + ' ';
159                         tocstring += par.asString(*buffer_, false);
160                         break;
161                 }
162         }
163
164         int const toclevel = toc_item->par_it_->layout()->toclevel;
165         if (toclevel != LyXLayout::NOT_IN_TOC
166             && toclevel >= min_toclevel
167             && toclevel <= bufparams.tocdepth
168                 && tocstring.empty())
169                         tocstring = toc_item->par_it_->asString(*buffer_, true);
170
171         const_cast<TocItem &>(*toc_item).str_ = tocstring;
172 }
173
174
175 void TocBackend::update()
176 {
177         tocs_.clear();
178         types_.clear();
179
180         BufferParams const & bufparams = buffer_->params();
181         const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
182
183         Toc & toc = tocs_["tableofcontents"];
184         ParConstIterator pit = buffer_->par_iterator_begin();
185         ParConstIterator end = buffer_->par_iterator_end();
186         for (; pit != end; ++pit) {
187
188                 // the string that goes to the toc (could be the optarg)
189                 docstring tocstring;
190
191                 // For each paragraph, traverse its insets and let them add
192                 // their toc items
193                 InsetList::const_iterator it = pit->insetlist.begin();
194                 InsetList::const_iterator end = pit->insetlist.end();
195                 for (; it != end; ++it) {
196                         InsetBase & inset = *it->inset;
197                         inset.addToToc(tocs_, *buffer_);
198                         switch (inset.lyxCode()) {
199                         case InsetBase::OPTARG_CODE: {
200                                 if (!tocstring.empty())
201                                         break;
202                                 Paragraph const & par = 
203                                         *static_cast<InsetOptArg&>(inset).paragraphs().begin();
204                                 if (!pit->getLabelstring().empty())
205                                         tocstring = pit->getLabelstring() + ' ';
206                                 tocstring += par.asString(*buffer_, false);
207                                 break;
208                         }
209                         default:
210                                 break;
211                         }
212                 }
213
214                 /// now the toc entry for the paragraph
215                 int const toclevel = pit->layout()->toclevel;
216                 if (toclevel != LyXLayout::NOT_IN_TOC
217                     && toclevel >= min_toclevel
218                     && toclevel <= bufparams.tocdepth) {
219                         // insert this into the table of contents
220                         if (tocstring.empty())
221                                 tocstring = pit->asString(*buffer_, true);
222                         toc.push_back(
223                                 TocItem(pit, toclevel - min_toclevel, tocstring));
224                 }
225         }
226
227         TocList::iterator it = tocs_.begin();
228         for (; it != tocs_.end(); ++it)
229                 types_.push_back(it->first);
230 }
231
232
233 TocIterator const TocBackend::item(
234         std::string const & type, ParConstIterator const & par_it) const
235 {
236         TocList::const_iterator toclist_it = tocs_.find(type);
237         // Is the type supported?
238         BOOST_ASSERT(toclist_it != tocs_.end());
239
240         Toc const & toc_vector = toclist_it->second;
241         TocIterator last = toc_vector.begin();
242         TocIterator it = toc_vector.end();
243         if (it == last)
244                 return it;
245
246         --it;
247
248         ParConstIterator par_it_text = par_it;
249         if (par_it_text.inMathed())
250                 // It would be better to do
251                 //   par_it_text.backwardInset();
252                 // but this method does not exist.
253                 while (par_it_text.inMathed())
254                         par_it_text.backwardPos();
255
256         for (; it != last; --it) {
257                 
258                 // A good solution for Items inside insets would be to do:
259                 //
260                 //if (std::distance(it->par_it_, current) <= 0)
261                 //      return it;
262                 //
263                 // But for an unknown reason, std::distance(current, it->par_it_) always
264                 // returns  a positive value and std::distance(it->par_it_, current) takes forever...
265                 // So for now, we do:
266                 if (it->par_it_.pit() <= par_it_text.pit())
267                         return it;
268         }
269
270         // We are before the first Toc Item:
271         return last;
272 }
273
274
275 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
276 {
277         TocList::const_iterator cit = tocs_.find(type);
278         if (cit != tocs_.end()) {
279                 TocIterator ccit = cit->second.begin();
280                 TocIterator end = cit->second.end();
281                 for (; ccit != end; ++ccit)
282                         os << ccit->asString() << '\n';
283         }
284 }
285
286
287 } // namespace lyx