]> git.lyx.org Git - features.git/blob - src/TocBackend.cpp
Enhancements and bugfixes to the TOCs
[features.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  * \author Guillaume Munch
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "TocBackend.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "FloatList.h"
21 #include "FuncRequest.h"
22 #include "InsetList.h"
23 #include "Layout.h"
24 #include "LyXAction.h"
25 #include "Paragraph.h"
26 #include "ParIterator.h"
27 #include "TextClass.h"
28
29 #include "insets/InsetArgument.h"
30
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34
35 #include "support/lassert.h"
36
37 using namespace std;
38
39
40 namespace lyx {
41
42
43 ///////////////////////////////////////////////////////////////////////////
44 //
45 // TocItem implementation
46 //
47 ///////////////////////////////////////////////////////////////////////////
48
49 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s,
50                         bool output_active, docstring const & t, FuncRequest action) :
51         dit_(dit), depth_(d), str_(s), tooltip_(t), output_(output_active),
52         action_(action)
53 {
54 }
55
56
57 int TocItem::id() const
58 {
59         return dit_.paragraph().id();
60 }
61
62
63 docstring const & TocItem::tooltip() const
64 {
65         return tooltip_.empty() ? str_ : tooltip_;
66 }
67
68
69 docstring const TocItem::asString() const
70 {
71         // U+2327 X IN A RECTANGLE BOX
72         // char_type const cross = 0x2327;
73         // U+274E NEGATIVE SQUARED CROSS MARK
74         char_type const cross = 0x274e;
75         docstring prefix;
76         if (!output_) {
77                 prefix += cross;
78                 prefix += " ";
79         }
80         return prefix + str_;
81 }
82
83 // convert a DocIterator into an argument to LFUN_PARAGRAPH_GOTO 
84 docstring paragraph_goto_arg(DocIterator const & dit)
85 {
86         CursorSlice const & s = dit.innerTextSlice();
87         return convert<docstring>(s.paragraph().id()) + ' ' +
88                 convert<docstring>(s.pos());
89 }
90
91 FuncRequest TocItem::action() const
92 {
93         if (action_.action() == LFUN_UNKNOWN_ACTION) {
94                 return FuncRequest(LFUN_PARAGRAPH_GOTO, paragraph_goto_arg(dit_));
95         } else
96                 return action_;
97 }
98
99
100 ///////////////////////////////////////////////////////////////////////////
101 //
102 // Toc implementation
103 //
104 ///////////////////////////////////////////////////////////////////////////
105
106 TocIterator Toc::item(DocIterator const & dit) const
107 {
108         TocIterator last = begin();
109         TocIterator it = end();
110         if (it == last)
111                 return it;
112
113         --it;
114
115         DocIterator dit_text = dit;
116         if (dit_text.inMathed()) {
117                 // We are only interested in text so remove the math CursorSlice.
118                 while (dit_text.inMathed())
119                         dit_text.pop_back();
120         }
121
122         for (; it != last; --it) {
123                 // We verify that we don't compare contents of two
124                 // different document. This happens when you
125                 // have parent and child documents.
126                 if (&it->dit_[0].inset() != &dit_text[0].inset())
127                         continue;
128                 if (it->dit_ <= dit_text)
129                         return it;
130         }
131
132         // We are before the first Toc Item:
133         return last;
134 }
135
136
137 Toc::iterator Toc::item(int depth, docstring const & str)
138 {
139         if (empty())
140                 return end();
141         iterator it = begin();
142         iterator itend = end();
143         for (; it != itend; ++it) {
144                 if (it->depth() == depth && it->str() == str)
145                         break;
146         }
147         return it;
148 }
149
150
151 ///////////////////////////////////////////////////////////////////////////
152 //
153 // TocBuilder implementation
154 //
155 ///////////////////////////////////////////////////////////////////////////
156
157 TocBuilder::TocBuilder(shared_ptr<Toc> toc)
158         : toc_(toc ? toc : make_shared<Toc>()),
159           stack_()
160 {
161         LATTEST(toc);
162 }
163
164 void TocBuilder::pushItem(DocIterator const & dit, docstring const & s,
165                                                   bool output_active, bool is_captioned)
166 {
167         toc_->push_back(TocItem(dit, stack_.size(), s, output_active));
168         frame f = {
169                 toc_->size() - 1, //pos
170                 is_captioned, //is_captioned
171         };
172         stack_.push(f);
173 }
174
175 void TocBuilder::captionItem(DocIterator const & dit, docstring const & s,
176                                                          bool output_active)
177 {
178         // first show the float before moving to the caption
179         docstring arg = "paragraph-goto " + paragraph_goto_arg(dit);
180         if (!stack_.empty())
181                 arg = "paragraph-goto " +
182                         paragraph_goto_arg((*toc_)[stack_.top().pos].dit_) + ";" + arg;
183         FuncRequest func(LFUN_COMMAND_SEQUENCE, arg);
184         
185         if (!stack_.empty() && !stack_.top().is_captioned) {
186                 // The float we entered has not yet been assigned a caption.
187                 // Assign the caption string to it.
188                 TocItem & captionable = (*toc_)[stack_.top().pos];
189                 captionable.str(s);
190                 captionable.setAction(func);
191                 stack_.top().is_captioned = true;
192         } else {
193                 // This is a new entry.
194                 pop();
195                 // the dit is at the float's level, e.g. for the contextual menu of
196                 // outliner entries
197                 DocIterator captionable_dit = dit;
198                 captionable_dit.pop_back();
199                 pushItem(captionable_dit, s, output_active, true);
200                 (*toc_)[stack_.top().pos].setAction(func);
201         }
202 }
203
204 void TocBuilder::pop()
205 {
206         if (!stack_.empty())
207                 stack_.pop();
208 }
209
210
211
212 ///////////////////////////////////////////////////////////////////////////
213 //
214 // TocBuilderStore implementation
215 //
216 ///////////////////////////////////////////////////////////////////////////
217
218 shared_ptr<TocBuilder> TocBuilderStore::get(string const & type,
219                                                                                         shared_ptr<Toc> toc)
220 {
221         map_t::const_iterator it = map_.find(type);
222         if (it == map_.end()) {
223                 it = map_.insert(std::make_pair(type,
224                                                                                 make_shared<TocBuilder>(toc))).first;
225         }
226         return it->second;
227 }
228
229
230
231 ///////////////////////////////////////////////////////////////////////////
232 //
233 // TocBackend implementation
234 //
235 ///////////////////////////////////////////////////////////////////////////
236
237 shared_ptr<Toc const> TocBackend::toc(string const & type) const
238 {
239         // Is the type already supported?
240         TocList::const_iterator it = tocs_.find(type);
241         LASSERT(it != tocs_.end(), { return make_shared<Toc>(); });
242         return it->second;
243 }
244
245
246 shared_ptr<Toc> TocBackend::toc(string const & type)
247 {
248         TocList::const_iterator it = tocs_.find(type);
249         if (it == tocs_.end()) {
250                 it = tocs_.insert(std::make_pair(type, make_shared<Toc>())).first;
251         }
252         return it->second;
253 }
254
255
256 shared_ptr<TocBuilder> TocBackend::builder(string const & type)
257 {
258         return builders_.get(type, toc(type));
259 }
260
261
262 bool TocBackend::updateItem(DocIterator const & dit)
263 {
264         if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
265                 return false;
266
267         if (toc("tableofcontents")->empty()) {
268                 // FIXME: should not happen, 
269                 // a call to TocBackend::update() is missing somewhere
270                 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
271                 return false;
272         }
273
274         BufferParams const & bufparams = buffer_->params();
275         const int min_toclevel = bufparams.documentClass().min_toclevel();
276
277         TocIterator toc_item = item("tableofcontents", dit);
278
279         docstring tocstring;
280
281         // For each paragraph, traverse its insets and let them add
282         // their toc items
283         Paragraph & par = toc_item->dit_.paragraph();
284         InsetList::const_iterator it = par.insetList().begin();
285         InsetList::const_iterator end = par.insetList().end();
286         for (; it != end; ++it) {
287                 Inset & inset = *it->inset;
288                 if (inset.lyxCode() == ARG_CODE) {
289                         if (!tocstring.empty())
290                                 break;
291                         Paragraph const & inset_par =
292                                 *static_cast<InsetArgument&>(inset).paragraphs().begin();
293                         if (!par.labelString().empty())
294                                 tocstring = par.labelString() + ' ';
295                         tocstring += inset_par.asString(AS_STR_INSETS);
296                         break;
297                 }
298         }
299
300         int const toclevel = toc_item->dit_.text()->getTocLevel(toc_item->dit_.pit());
301         if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
302                 && tocstring.empty())
303                         tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
304
305         const_cast<TocItem &>(*toc_item).str(tocstring);
306
307         buffer_->updateTocItem("tableofcontents", dit);
308         return true;
309 }
310
311
312 void TocBackend::update(bool output_active, UpdateType utype)
313 {
314         for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
315                 it->second->clear();
316         tocs_.clear();
317         builders_.clear();
318         if (!buffer_->isInternal()) {
319                 DocIterator dit;
320                 buffer_->inset().addToToc(dit, output_active, utype);
321         }
322 }
323
324
325 TocIterator TocBackend::item(string const & type,
326                 DocIterator const & dit) const
327 {
328         TocList::const_iterator toclist_it = tocs_.find(type);
329         // Is the type supported?
330         // We will try to make the best of it in release mode
331         LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
332         return toclist_it->second->item(dit);
333 }
334
335
336 void TocBackend::writePlaintextTocList(string const & type,
337         odocstringstream & os, size_t max_length) const
338 {
339         TocList::const_iterator cit = tocs_.find(type);
340         if (cit != tocs_.end()) {
341                 TocIterator ccit = cit->second->begin();
342                 TocIterator end = cit->second->end();
343                 for (; ccit != end; ++ccit) {
344                         os << ccit->asString() << from_utf8("\n");
345                         if (os.str().size() > max_length)
346                                 break;
347                 }
348         }
349 }
350
351
352 } // namespace lyx