]> git.lyx.org Git - lyx.git/blob - src/TocBackend.cpp
e47ca9b63066502ff39b1666161afd9c2d18b913
[lyx.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 #include "support/lassert.h"
35 #include "support/lstrings.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         static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
72         static char_type const thin = 0x2009; // U+2009 THIN SPACE
73         docstring prefix;
74         if (!output_) {
75                 prefix += cross;
76                 prefix += thin;
77         }
78         return prefix + str_;
79 }
80
81 namespace {
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 } // namespace anon
92
93 FuncRequest TocItem::action() const
94 {
95         if (action_.action() == LFUN_UNKNOWN_ACTION) {
96                 return FuncRequest(LFUN_PARAGRAPH_GOTO, paragraph_goto_arg(dit_));
97         } else
98                 return action_;
99 }
100
101
102 ///////////////////////////////////////////////////////////////////////////
103 //
104 // Toc implementation
105 //
106 ///////////////////////////////////////////////////////////////////////////
107
108 TocIterator Toc::item(DocIterator const & dit) const
109 {
110         TocIterator last = begin();
111         TocIterator it = end();
112         if (it == last)
113                 return it;
114
115         --it;
116
117         DocIterator dit_text = dit;
118         if (dit_text.inMathed()) {
119                 // We are only interested in text so remove the math CursorSlice.
120                 while (dit_text.inMathed())
121                         dit_text.pop_back();
122         }
123
124         for (; it != last; --it) {
125                 // We verify that we don't compare contents of two
126                 // different document. This happens when you
127                 // have parent and child documents.
128                 if (&it->dit_[0].inset() != &dit_text[0].inset())
129                         continue;
130                 if (it->dit_ <= dit_text)
131                         return it;
132         }
133
134         // We are before the first Toc Item:
135         return last;
136 }
137
138
139 Toc::iterator Toc::item(int depth, docstring const & str)
140 {
141         if (empty())
142                 return end();
143         iterator it = begin();
144         iterator itend = end();
145         for (; it != itend; ++it) {
146                 if (it->depth() == depth && it->str() == str)
147                         break;
148         }
149         return it;
150 }
151
152
153 ///////////////////////////////////////////////////////////////////////////
154 //
155 // TocBuilder implementation
156 //
157 ///////////////////////////////////////////////////////////////////////////
158
159 TocBuilder::TocBuilder(shared_ptr<Toc> toc)
160         : toc_(toc ? toc : lyx::make_shared<Toc>()),
161           stack_()
162 {
163         LATTEST(toc);
164 }
165
166 void TocBuilder::pushItem(DocIterator const & dit, docstring const & s,
167                                                   bool output_active, bool is_captioned)
168 {
169         toc_->push_back(TocItem(dit, stack_.size(), s, output_active));
170         frame f = {
171                 toc_->size() - 1, //pos
172                 is_captioned, //is_captioned
173         };
174         stack_.push(f);
175 }
176
177 void TocBuilder::captionItem(DocIterator const & dit, docstring const & s,
178                                                          bool output_active)
179 {
180         // first show the float before moving to the caption
181         docstring arg = "paragraph-goto " + paragraph_goto_arg(dit);
182         if (!stack_.empty())
183                 arg = "paragraph-goto " +
184                         paragraph_goto_arg((*toc_)[stack_.top().pos].dit_) + ";" + arg;
185         FuncRequest func(LFUN_COMMAND_SEQUENCE, arg);
186         
187         if (!stack_.empty() && !stack_.top().is_captioned) {
188                 // The float we entered has not yet been assigned a caption.
189                 // Assign the caption string to it.
190                 TocItem & captionable = (*toc_)[stack_.top().pos];
191                 captionable.str(s);
192                 captionable.setAction(func);
193                 stack_.top().is_captioned = true;
194         } else {
195                 // This is a new entry.
196                 pop();
197                 // the dit is at the float's level, e.g. for the contextual menu of
198                 // outliner entries
199                 DocIterator captionable_dit = dit;
200                 captionable_dit.pop_back();
201                 pushItem(captionable_dit, s, output_active, true);
202                 (*toc_)[stack_.top().pos].setAction(func);
203         }
204 }
205
206 void TocBuilder::pop()
207 {
208         if (!stack_.empty())
209                 stack_.pop();
210 }
211
212
213
214 ///////////////////////////////////////////////////////////////////////////
215 //
216 // TocBuilderStore implementation
217 //
218 ///////////////////////////////////////////////////////////////////////////
219
220 shared_ptr<TocBuilder> TocBuilderStore::get(string const & type,
221                                                                                         shared_ptr<Toc> toc)
222 {
223         map_t::const_iterator it = map_.find(type);
224         if (it == map_.end()) {
225                 it = map_.insert(std::make_pair(type,
226                                                                         lyx::make_shared<TocBuilder>(toc))).first;
227         }
228         return it->second;
229 }
230
231
232
233 ///////////////////////////////////////////////////////////////////////////
234 //
235 // TocBackend implementation
236 //
237 ///////////////////////////////////////////////////////////////////////////
238
239 shared_ptr<Toc const> TocBackend::toc(string const & type) const
240 {
241         // Is the type already supported?
242         TocList::const_iterator it = tocs_.find(type);
243         LASSERT(it != tocs_.end(), { return lyx::make_shared<Toc>(); });
244         return it->second;
245 }
246
247
248 shared_ptr<Toc> TocBackend::toc(string const & type)
249 {
250         TocList::const_iterator it = tocs_.find(type);
251         if (it == tocs_.end()) {
252                 it = tocs_.insert(std::make_pair(type, lyx::make_shared<Toc>())).first;
253         }
254         return it->second;
255 }
256
257
258 shared_ptr<TocBuilder> TocBackend::builder(string const & type)
259 {
260         return builders_.get(type, toc(type));
261 }
262
263
264 // FIXME: This function duplicates functionality from InsetText::iterateForToc.
265 // Both have their own way of computing the TocItem for "tableofcontents". The
266 // TocItem creation and update should be made in a dedicated function and
267 // updateItem should be rewritten to uniformly update the matching items from
268 // all TOCs.
269 bool TocBackend::updateItem(DocIterator const & dit)
270 {
271         if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
272                 return false;
273
274         if (toc("tableofcontents")->empty()) {
275                 // FIXME: should not happen, 
276                 // a call to TocBackend::update() is missing somewhere
277                 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
278                 return false;
279         }
280
281         BufferParams const & bufparams = buffer_->params();
282         const int min_toclevel = bufparams.documentClass().min_toclevel();
283
284         TocIterator toc_item = item("tableofcontents", dit);
285
286         docstring tocstring;
287
288         // For each paragraph, traverse its insets and let them add
289         // their toc items
290         //
291         // FIXME: This is supposed to accomplish the same as the body of
292         // InsetText::iterateForToc(), probably
293         Paragraph & par = toc_item->dit_.paragraph();
294         InsetList::const_iterator it = par.insetList().begin();
295         InsetList::const_iterator end = par.insetList().end();
296         for (; it != end; ++it) {
297                 Inset & inset = *it->inset;
298                 if (inset.lyxCode() == ARG_CODE) {
299                         tocstring = par.labelString();
300                         if (!tocstring.empty())
301                                 tocstring += ' ';
302                         inset.asInsetText()->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
303                         break;
304                 }
305         }
306
307         int const toclevel = toc_item->dit_.text()->
308                 getTocLevel(toc_item->dit_.pit());
309         if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
310                 && tocstring.empty())
311                 par.forOutliner(tocstring, TOC_ENTRY_LENGTH);
312
313         support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
314         const_cast<TocItem &>(*toc_item).str(tocstring);
315
316         buffer_->updateTocItem("tableofcontents", dit);
317         return true;
318 }
319
320
321 void TocBackend::update(bool output_active, UpdateType utype)
322 {
323         for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
324                 it->second->clear();
325         tocs_.clear();
326         builders_.clear();
327         if (!buffer_->isInternal()) {
328                 DocIterator dit;
329                 buffer_->inset().addToToc(dit, output_active, utype);
330         }
331 }
332
333
334 TocIterator TocBackend::item(string const & type,
335                 DocIterator const & dit) const
336 {
337         TocList::const_iterator toclist_it = tocs_.find(type);
338         // Is the type supported?
339         // We will try to make the best of it in release mode
340         LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
341         return toclist_it->second->item(dit);
342 }
343
344
345 void TocBackend::writePlaintextTocList(string const & type,
346         odocstringstream & os, size_t max_length) const
347 {
348         TocList::const_iterator cit = tocs_.find(type);
349         if (cit != tocs_.end()) {
350                 TocIterator ccit = cit->second->begin();
351                 TocIterator end = cit->second->end();
352                 for (; ccit != end; ++ccit) {
353                         os << ccit->asString() << from_utf8("\n");
354                         if (os.str().size() > max_length)
355                                 break;
356                 }
357         }
358 }
359
360
361 } // namespace lyx