]> git.lyx.org Git - lyx.git/blob - src/TocBackend.cpp
InsetListings: separate first and last lines to a separate group box called range...
[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 "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, bool child)
41                 : par_it_(par_it), depth_(d), str_(s), child_(child)
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 void TocBackend::updateItem(ParConstIterator const & par_it)
125 {
126         // TODO should not happen, 
127         // a call to TocBackend::update() is missing somewhere
128         if (toc("tableofcontents").empty())
129                 return;
130
131         BufferParams const & bufparams = buffer_->params();
132         const int min_toclevel = bufparams.getTextClass().min_toclevel();
133
134         TocIterator toc_item = item("tableofcontents", par_it);
135
136         docstring tocstring;
137
138         // For each paragraph, traverse its insets and let them add
139         // their toc items
140         InsetList::const_iterator it = toc_item->par_it_->insetlist.begin();
141         InsetList::const_iterator end = toc_item->par_it_->insetlist.end();
142         for (; it != end; ++it) {
143                 Inset & inset = *it->inset;
144                 if (inset.lyxCode() == Inset::OPTARG_CODE) {
145                         if (!tocstring.empty())
146                                 break;
147                         Paragraph const & par = 
148                                 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
149                         if (!toc_item->par_it_->getLabelstring().empty())
150                                 tocstring = toc_item->par_it_->getLabelstring() + ' ';
151                         tocstring += par.asString(*buffer_, false);
152                         break;
153                 }
154         }
155
156         int const toclevel = toc_item->par_it_->layout()->toclevel;
157         if (toclevel != Layout::NOT_IN_TOC
158             && toclevel >= min_toclevel
159                 && tocstring.empty())
160                         tocstring = toc_item->par_it_->asString(*buffer_, true);
161
162         const_cast<TocItem &>(*toc_item).str_ = tocstring;
163 }
164
165
166 void TocBackend::update()
167 {
168         tocs_.clear();
169
170         BufferParams const & bufparams = buffer_->params();
171         const int min_toclevel = bufparams.getTextClass().min_toclevel();
172
173         // Is this a child document?
174         bool const child_document = buffer_->getMasterBuffer() != buffer_;
175
176         Toc & toc = tocs_["tableofcontents"];
177         ParConstIterator pit = buffer_->par_iterator_begin();
178         ParConstIterator end = buffer_->par_iterator_end();
179         for (; pit != end; ++pit) {
180
181                 // the string that goes to the toc (could be the optarg)
182                 docstring tocstring;
183
184                 // For each paragraph, traverse its insets and let them add
185                 // their toc items
186                 InsetList::const_iterator it = pit->insetlist.begin();
187                 InsetList::const_iterator end = pit->insetlist.end();
188                 for (; it != end; ++it) {
189                         Inset & inset = *it->inset;
190                         inset.addToToc(tocs_, *buffer_);
191                         switch (inset.lyxCode()) {
192                         case Inset::OPTARG_CODE: {
193                                 if (!tocstring.empty())
194                                         break;
195                                 Paragraph const & par = 
196                                         *static_cast<InsetOptArg&>(inset).paragraphs().begin();
197                                 if (!pit->getLabelstring().empty())
198                                         tocstring = pit->getLabelstring() + ' ';
199                                 tocstring += par.asString(*buffer_, false);
200                                 break;
201                         }
202                         default:
203                                 break;
204                         }
205                 }
206
207                 /// now the toc entry for the paragraph
208                 int const toclevel = pit->layout()->toclevel;
209                 if (toclevel != Layout::NOT_IN_TOC
210                     && toclevel >= min_toclevel) {
211                         // insert this into the table of contents
212                         if (tocstring.empty())
213                                 tocstring = pit->asString(*buffer_, true);
214                         toc.push_back(TocItem(pit, toclevel - min_toclevel,
215                                 tocstring, child_document));
216                 }
217         }
218 }
219
220
221 TocIterator const TocBackend::item(
222         std::string const & type, ParConstIterator const & par_it) const
223 {
224         TocList::const_iterator toclist_it = tocs_.find(type);
225         // Is the type supported?
226         BOOST_ASSERT(toclist_it != tocs_.end());
227
228         Toc const & toc_vector = toclist_it->second;
229         TocIterator last = toc_vector.begin();
230         TocIterator it = toc_vector.end();
231         if (it == last)
232                 return it;
233
234         --it;
235
236         ParConstIterator par_it_text = par_it;
237         if (par_it_text.inMathed())
238                 // It would be better to do
239                 //   par_it_text.backwardInset();
240                 // but this method does not exist.
241                 while (par_it_text.inMathed())
242                         par_it_text.backwardPos();
243
244         for (; it != last; --it) {
245                 if (it->child_)
246                         continue;
247                 if (it->par_it_ <= par_it_text)
248                         return it;
249         }
250
251         // We are before the first Toc Item:
252         return last;
253 }
254
255
256 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
257 {
258         TocList::const_iterator cit = tocs_.find(type);
259         if (cit != tocs_.end()) {
260                 TocIterator ccit = cit->second.begin();
261                 TocIterator end = cit->second.end();
262                 for (; ccit != end; ++ccit)
263                         os << ccit->asString() << '\n';
264         }
265 }
266
267
268 } // namespace lyx