]> git.lyx.org Git - lyx.git/blob - src/TocBackend.C
minimal effort implementation of:
[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 using std::vector;
37 using std::max;
38 using std::ostream;
39 using std::string;
40 using std::cout;
41 using std::endl;
42
43 namespace lyx {
44
45 ///////////////////////////////////////////////////////////////////////////
46 // TocBackend::Item implementation
47
48 TocBackend::Item::Item(ParConstIterator const & par_it, int d,
49                                            std::string 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 std::string const & TocBackend::Item::str() const
100 {
101         return str_;
102 }
103
104
105 string const TocBackend::Item::asString() const
106 {
107         return string(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                 string 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                                                 + ' ';
189                                 tocstring += par.asString(*buffer_, false);
190                                 break;
191                         }
192                         default:
193                                 break;
194                         }
195                 }
196
197                 /// now the toc entry for the paragraph
198                 int const toclevel = pit->layout()->toclevel;
199                 if (toclevel != LyXLayout::NOT_IN_TOC
200                     && toclevel >= min_toclevel
201                     && toclevel <= bufparams.tocdepth) {
202                         // insert this into the table of contents
203                         if (tocstring.empty())
204                                 tocstring = pit->asString(*buffer_, true);
205                         Item const item(pit, toclevel - min_toclevel, tocstring);
206                         tocs_["TOC"].push_back(item);
207                         //cout << "item inserted str " << item.str()
208                         //      << "  id " << item.id() << endl;
209                 }
210         }
211
212         TocList::iterator it = tocs_.begin();
213         for (; it != tocs_.end(); ++it)
214                 types_.push_back(it->first);
215 }
216
217
218 TocBackend::TocIterator const TocBackend::item(std::string const & type, ParConstIterator const & par_it)
219 {
220         TocList::iterator toclist_it = tocs_.find(type);
221         // Is the type supported?
222         BOOST_ASSERT(toclist_it != tocs_.end());
223
224         Toc const & toc_vector = toclist_it->second;
225         TocBackend::TocIterator last = toc_vector.begin();
226         TocBackend::TocIterator it = toc_vector.end();
227         --it;
228
229         for (; it != last; --it) {
230                 
231                 // A good solution for Items inside insets would be to do:
232                 //
233                 //if (std::distance(it->par_it_, current) <= 0)
234                 //      return it;
235                 //
236                 // But for an unknown reason, std::distance(current, it->par_it_) always
237                 // returns  a positive value and std::distance(it->par_it_, current) takes forever...
238                 // So for now, we do:
239                 if (it->par_it_.pit() <= par_it.pit())
240                         return it;
241         }
242
243         // We are before the first Toc Item:
244         return last;
245 }
246
247
248 void TocBackend::asciiTocList(string const & type, ostream & os) const
249 {
250         TocList::const_iterator cit = tocs_.find(type);
251         if (cit != tocs_.end()) {
252                 Toc::const_iterator ccit = cit->second.begin();
253                 Toc::const_iterator end = cit->second.end();
254                 for (; ccit != end; ++ccit)
255                         os << ccit->asString() << '\n';
256         }
257 }
258
259
260 } // namespace lyx