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