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