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