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